-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
70 lines (60 loc) · 1.94 KB
/
index.ts
File metadata and controls
70 lines (60 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import axios from 'axios';
export class ShopifyPlanetApi {
private clientId: string;
private token: string;
constructor(params: {clientId: string; token: string}) {
const {clientId, token} = params;
if (!clientId || !token) throw new Error('Provide both: clientId and token');
this.clientId = clientId;
this.token = token;
}
// API methods
async getShopInfo(variables: {
shopifyDomain: string;
}): Promise<{shop: {allShipmentsCarbonNeutral: boolean}}> {
return this.call({
query: `
query GetShopInfo($shopifyDomain: String!) {
shop(shopifyDomain: $shopifyDomain) {
allShipmentsCarbonNeutral
}
}
`,
variables,
});
}
// Private methods
private async call(data: {query: string; variables: Record<string, any>}) {
const headers = (await this.getHeaders(JSON.stringify(data))) as any;
const resp = await axios(this.getBaseUrl(), {
method: 'post',
data,
headers,
});
return resp?.data?.data;
}
private async getHeaders(body: string) {
return {
Accept: 'application/json',
'Content-Type': 'application/json',
'X-Shopify-Planet-Client-ID': this.clientId,
'X-Shopify-Planet-Hmac-Sha256': await generateEncryptedToken(this.token, body),
};
}
private getBaseUrl() {
return 'https://shopify-planet.shopifyapps.com/api/graphql';
}
}
const generateEncryptedToken = async (token: string, body: string) => {
let encToken: string;
// if browser -- https://github.com/flexdinesh/browser-or-node/blob/master/src/index.js
if (typeof window !== 'undefined' && typeof window.document !== 'undefined') {
// TODO
console.error('[shopify-planet-api] Crypto methods are not yet supported for Browser.');
} else {
// if server
const createHmac = (await import('crypto')).createHmac;
encToken = createHmac('sha256', token).update(Buffer.from(body)).digest('base64');
}
return encToken;
};