@adtrackify/at-service-common 1.0.59 → 1.0.60
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +12 -12
- package/.vscode/settings.json +9 -9
- package/build.js +21 -21
- package/dist/index.d.ts +4 -0
- package/dist/index.js +0 -6
- package/dist/index.js.map +2 -2
- package/jest.config.ts +41 -0
- package/package.json +19 -10
- package/src/__tests__/helpers/subscription-helper.spec.ts +41 -0
- package/src/clients/generic/axios.d.ts +7 -7
- package/src/clients/generic/dynamodb-client.ts +113 -113
- package/src/clients/generic/eventbridge-client.ts +52 -52
- package/src/clients/internal-api/accounts-client.ts +107 -107
- package/src/clients/internal-api/destinations-client.ts +58 -58
- package/src/clients/internal-api/users-auth-client.ts +110 -110
- package/src/clients/third-party/shopify-client.ts +128 -128
- package/src/helpers/input-validation-helper.ts +21 -21
- package/src/helpers/shopify-helper.ts +39 -39
- package/src/helpers/subscription-helper.ts +217 -217
- package/src/libs/http-error.ts +6 -6
- package/src/libs/index.ts +7 -7
- package/src/libs/url.ts +9 -9
- package/src/types/internal-events/event-detail-types.ts +9 -9
- package/tsconfig.json +2 -1
|
@@ -1,128 +1,128 @@
|
|
|
1
|
-
import { axiosHttpService } from '../generic/http-client';
|
|
2
|
-
import * as log from 'lambda-log';
|
|
3
|
-
|
|
4
|
-
export class ShopifyClient {
|
|
5
|
-
static _shopify_api_version = process.env.SHOPIFY_API_VERSION as string;
|
|
6
|
-
static getConfig = (shopifyDomain: string, accessToken: string) => {
|
|
7
|
-
const config = {
|
|
8
|
-
baseURL: `https://${shopifyDomain}/admin/api/${this._shopify_api_version}`,
|
|
9
|
-
headers: {
|
|
10
|
-
common: {
|
|
11
|
-
'X-Shopify-Access-Token': accessToken,
|
|
12
|
-
},
|
|
13
|
-
},
|
|
14
|
-
};
|
|
15
|
-
return config;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
static getClient = (shopifyDomain: string, accessToken: string) => {
|
|
19
|
-
return axiosHttpService(
|
|
20
|
-
this.getConfig(shopifyDomain, accessToken)
|
|
21
|
-
);
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
static registerApp = async (shop: string, code: string, appKey: string, appSecret: string) => {
|
|
25
|
-
const client = axiosHttpService();
|
|
26
|
-
const url = 'https://' + shop + '/admin/oauth/access_token';
|
|
27
|
-
const payload = {
|
|
28
|
-
client_id: appKey,
|
|
29
|
-
client_secret: appSecret,
|
|
30
|
-
code
|
|
31
|
-
};
|
|
32
|
-
const res = await client.post(url, payload);
|
|
33
|
-
return res;
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
static registerWebhookTopic = async (shop: string, accessToken: string, eventBridgeArn: string, topic: string) => {
|
|
37
|
-
const client = axiosHttpService();
|
|
38
|
-
const url = `https://${shop}/admin/api/${this._shopify_api_version}/webhooks.json`;
|
|
39
|
-
const payload = {
|
|
40
|
-
webhook: {
|
|
41
|
-
topic,
|
|
42
|
-
address: eventBridgeArn,
|
|
43
|
-
format: 'json'
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
const res = await client.post(url, payload, { headers: { 'X-Shopify-Access-Token': accessToken } });
|
|
47
|
-
if (res.status >= 400) {
|
|
48
|
-
log.error('Failed to register Webhook Topic', { shop, accessToken, eventBridgeArn, topic, url, payload });
|
|
49
|
-
}
|
|
50
|
-
log.debug('Shopify Client Webhook Registration Response', { registrationResponse: res });
|
|
51
|
-
return res;
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
static updateShopifyAppMetafield = async (shop: string, accessToken: string, pixelId: string) => {
|
|
55
|
-
const url = `https://${shop}/admin/api/${this._shopify_api_version}/metafields.json`;
|
|
56
|
-
const payload = {
|
|
57
|
-
metafield: {
|
|
58
|
-
namespace: 'adtr',
|
|
59
|
-
key: 'adtr.config',
|
|
60
|
-
value: pixelId,
|
|
61
|
-
type: 'single_line_text_field'
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
const res = await this.genericShopifyPost(url, accessToken, payload);
|
|
65
|
-
|
|
66
|
-
if (res.status >= 400) {
|
|
67
|
-
log.error('Failed to register Webhook Topic', { shop, accessToken, url, payload });
|
|
68
|
-
}
|
|
69
|
-
return res;
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
static getShopifyStoreProperties = async (shop: string, accessToken: string) => {
|
|
73
|
-
const url = `https://${shop}/admin/api/${this._shopify_api_version}/shop.json`;
|
|
74
|
-
const res = await this.genericShopifyGet(url, accessToken);
|
|
75
|
-
|
|
76
|
-
if (res.status >= 400) {
|
|
77
|
-
log.error('Failed to get Shopify Store Properties', { shop, accessToken, url });
|
|
78
|
-
}
|
|
79
|
-
return res;
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
static createAppSubscription = async (shop: string, accessToken: string,
|
|
83
|
-
planName: string, price: number, returnUrl: string, trialDays: number, test?: boolean) => {
|
|
84
|
-
const url = `https://${shop}/admin/api/${this._shopify_api_version}/recurring_application_charges.json`;
|
|
85
|
-
const recurring_application_charge = {
|
|
86
|
-
name: planName,
|
|
87
|
-
price,
|
|
88
|
-
return_url: returnUrl,
|
|
89
|
-
trial_days: trialDays,
|
|
90
|
-
test
|
|
91
|
-
};
|
|
92
|
-
const res = await this.genericShopifyPost(url, accessToken, { recurring_application_charge });
|
|
93
|
-
if (res.status >= 400) {
|
|
94
|
-
log.error('Failed to create App Subscription', { shop, accessToken, url });
|
|
95
|
-
}
|
|
96
|
-
return res;
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
static listAppSubscriptions = async (shop: string, accessToken: string) => {
|
|
100
|
-
const url = `https://${shop}/admin/api/${this._shopify_api_version}/recurring_application_charges.json`;
|
|
101
|
-
const res = await this.genericShopifyGet(url, accessToken);
|
|
102
|
-
if (res.status >= 400) {
|
|
103
|
-
log.error('Failed to get App Subscriptions', { shop, accessToken, url });
|
|
104
|
-
}
|
|
105
|
-
return res;
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
static genericShopifyPost = async (url: string, accessToken: string, payload: any) => {
|
|
109
|
-
const client = axiosHttpService();
|
|
110
|
-
const res = await client.post(url, payload, { headers: { 'X-Shopify-Access-Token': accessToken } });
|
|
111
|
-
log.debug('Shopify Client Response', { res });
|
|
112
|
-
return res;
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
static genericShopifyGet = async (url: string, accessToken: string) => {
|
|
116
|
-
const client = axiosHttpService();
|
|
117
|
-
const res = await client.get(url, { headers: { 'X-Shopify-Access-Token': accessToken } });
|
|
118
|
-
log.debug('Shopify Client Response', { res });
|
|
119
|
-
return res;
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
static genericShopifyPut = async (url: string, accessToken: string, payload: any) => {
|
|
123
|
-
const client = axiosHttpService();
|
|
124
|
-
const res = await client.put(url, payload, { headers: { 'X-Shopify-Access-Token': accessToken } });
|
|
125
|
-
log.debug('Shopify Client Response', { res });
|
|
126
|
-
return res;
|
|
127
|
-
};
|
|
128
|
-
}
|
|
1
|
+
import { axiosHttpService } from '../generic/http-client';
|
|
2
|
+
import * as log from 'lambda-log';
|
|
3
|
+
|
|
4
|
+
export class ShopifyClient {
|
|
5
|
+
static _shopify_api_version = process.env.SHOPIFY_API_VERSION as string;
|
|
6
|
+
static getConfig = (shopifyDomain: string, accessToken: string) => {
|
|
7
|
+
const config = {
|
|
8
|
+
baseURL: `https://${shopifyDomain}/admin/api/${this._shopify_api_version}`,
|
|
9
|
+
headers: {
|
|
10
|
+
common: {
|
|
11
|
+
'X-Shopify-Access-Token': accessToken,
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
return config;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
static getClient = (shopifyDomain: string, accessToken: string) => {
|
|
19
|
+
return axiosHttpService(
|
|
20
|
+
this.getConfig(shopifyDomain, accessToken)
|
|
21
|
+
);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
static registerApp = async (shop: string, code: string, appKey: string, appSecret: string) => {
|
|
25
|
+
const client = axiosHttpService();
|
|
26
|
+
const url = 'https://' + shop + '/admin/oauth/access_token';
|
|
27
|
+
const payload = {
|
|
28
|
+
client_id: appKey,
|
|
29
|
+
client_secret: appSecret,
|
|
30
|
+
code
|
|
31
|
+
};
|
|
32
|
+
const res = await client.post(url, payload);
|
|
33
|
+
return res;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
static registerWebhookTopic = async (shop: string, accessToken: string, eventBridgeArn: string, topic: string) => {
|
|
37
|
+
const client = axiosHttpService();
|
|
38
|
+
const url = `https://${shop}/admin/api/${this._shopify_api_version}/webhooks.json`;
|
|
39
|
+
const payload = {
|
|
40
|
+
webhook: {
|
|
41
|
+
topic,
|
|
42
|
+
address: eventBridgeArn,
|
|
43
|
+
format: 'json'
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
const res = await client.post(url, payload, { headers: { 'X-Shopify-Access-Token': accessToken } });
|
|
47
|
+
if (res.status >= 400) {
|
|
48
|
+
log.error('Failed to register Webhook Topic', { shop, accessToken, eventBridgeArn, topic, url, payload });
|
|
49
|
+
}
|
|
50
|
+
log.debug('Shopify Client Webhook Registration Response', { registrationResponse: res });
|
|
51
|
+
return res;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
static updateShopifyAppMetafield = async (shop: string, accessToken: string, pixelId: string) => {
|
|
55
|
+
const url = `https://${shop}/admin/api/${this._shopify_api_version}/metafields.json`;
|
|
56
|
+
const payload = {
|
|
57
|
+
metafield: {
|
|
58
|
+
namespace: 'adtr',
|
|
59
|
+
key: 'adtr.config',
|
|
60
|
+
value: pixelId,
|
|
61
|
+
type: 'single_line_text_field'
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
const res = await this.genericShopifyPost(url, accessToken, payload);
|
|
65
|
+
|
|
66
|
+
if (res.status >= 400) {
|
|
67
|
+
log.error('Failed to register Webhook Topic', { shop, accessToken, url, payload });
|
|
68
|
+
}
|
|
69
|
+
return res;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
static getShopifyStoreProperties = async (shop: string, accessToken: string) => {
|
|
73
|
+
const url = `https://${shop}/admin/api/${this._shopify_api_version}/shop.json`;
|
|
74
|
+
const res = await this.genericShopifyGet(url, accessToken);
|
|
75
|
+
|
|
76
|
+
if (res.status >= 400) {
|
|
77
|
+
log.error('Failed to get Shopify Store Properties', { shop, accessToken, url });
|
|
78
|
+
}
|
|
79
|
+
return res;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
static createAppSubscription = async (shop: string, accessToken: string,
|
|
83
|
+
planName: string, price: number, returnUrl: string, trialDays: number, test?: boolean) => {
|
|
84
|
+
const url = `https://${shop}/admin/api/${this._shopify_api_version}/recurring_application_charges.json`;
|
|
85
|
+
const recurring_application_charge = {
|
|
86
|
+
name: planName,
|
|
87
|
+
price,
|
|
88
|
+
return_url: returnUrl,
|
|
89
|
+
trial_days: trialDays,
|
|
90
|
+
test
|
|
91
|
+
};
|
|
92
|
+
const res = await this.genericShopifyPost(url, accessToken, { recurring_application_charge });
|
|
93
|
+
if (res.status >= 400) {
|
|
94
|
+
log.error('Failed to create App Subscription', { shop, accessToken, url });
|
|
95
|
+
}
|
|
96
|
+
return res;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
static listAppSubscriptions = async (shop: string, accessToken: string) => {
|
|
100
|
+
const url = `https://${shop}/admin/api/${this._shopify_api_version}/recurring_application_charges.json`;
|
|
101
|
+
const res = await this.genericShopifyGet(url, accessToken);
|
|
102
|
+
if (res.status >= 400) {
|
|
103
|
+
log.error('Failed to get App Subscriptions', { shop, accessToken, url });
|
|
104
|
+
}
|
|
105
|
+
return res;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
static genericShopifyPost = async (url: string, accessToken: string, payload: any) => {
|
|
109
|
+
const client = axiosHttpService();
|
|
110
|
+
const res = await client.post(url, payload, { headers: { 'X-Shopify-Access-Token': accessToken } });
|
|
111
|
+
log.debug('Shopify Client Response', { res });
|
|
112
|
+
return res;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
static genericShopifyGet = async (url: string, accessToken: string) => {
|
|
116
|
+
const client = axiosHttpService();
|
|
117
|
+
const res = await client.get(url, { headers: { 'X-Shopify-Access-Token': accessToken } });
|
|
118
|
+
log.debug('Shopify Client Response', { res });
|
|
119
|
+
return res;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
static genericShopifyPut = async (url: string, accessToken: string, payload: any) => {
|
|
123
|
+
const client = axiosHttpService();
|
|
124
|
+
const res = await client.put(url, payload, { headers: { 'X-Shopify-Access-Token': accessToken } });
|
|
125
|
+
log.debug('Shopify Client Response', { res });
|
|
126
|
+
return res;
|
|
127
|
+
};
|
|
128
|
+
}
|
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
import Joi from 'joi';
|
|
2
|
-
import * as log from 'lambda-log';
|
|
3
|
-
import { HttpError } from '../libs/http-error';
|
|
4
|
-
|
|
5
|
-
export const validateInput = (schema: Joi.ObjectSchema<any>, input: any) => {
|
|
6
|
-
const { error, value } = schema.validate(input);
|
|
7
|
-
if (error) {
|
|
8
|
-
log.info('', { error });
|
|
9
|
-
|
|
10
|
-
const httperr = HttpError.badRequest('Bad Request', {
|
|
11
|
-
errors: error.details.map(detail => ({
|
|
12
|
-
message: detail?.message,
|
|
13
|
-
key: detail?.context?.key,
|
|
14
|
-
path: detail?.path,
|
|
15
|
-
}))
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
log.info('', { httperr });
|
|
19
|
-
throw httperr;
|
|
20
|
-
}
|
|
21
|
-
return value;
|
|
1
|
+
import Joi from 'joi';
|
|
2
|
+
import * as log from 'lambda-log';
|
|
3
|
+
import { HttpError } from '../libs/http-error';
|
|
4
|
+
|
|
5
|
+
export const validateInput = (schema: Joi.ObjectSchema<any>, input: any) => {
|
|
6
|
+
const { error, value } = schema.validate(input);
|
|
7
|
+
if (error) {
|
|
8
|
+
log.info('', { error });
|
|
9
|
+
|
|
10
|
+
const httperr = HttpError.badRequest('Bad Request', {
|
|
11
|
+
errors: error.details.map(detail => ({
|
|
12
|
+
message: detail?.message,
|
|
13
|
+
key: detail?.context?.key,
|
|
14
|
+
path: detail?.path,
|
|
15
|
+
}))
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
log.info('', { httperr });
|
|
19
|
+
throw httperr;
|
|
20
|
+
}
|
|
21
|
+
return value;
|
|
22
22
|
};
|
|
@@ -1,39 +1,39 @@
|
|
|
1
|
-
import { createHmac } from 'crypto';
|
|
2
|
-
import * as log from 'lambda-log';
|
|
3
|
-
import { HttpError } from '../libs';
|
|
4
|
-
import { mapObjectToQueryString } from '../libs/url';
|
|
5
|
-
export interface ShopifyRequestValidationParameters {
|
|
6
|
-
code: string,
|
|
7
|
-
hmac?: string,
|
|
8
|
-
shop: string,
|
|
9
|
-
state: string,
|
|
10
|
-
timestamp: string;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export const isShopifyRequestValid = (validationParams: ShopifyRequestValidationParameters, validationHmac: string, shopifyAppApiSecret: string): boolean => {
|
|
14
|
-
// remove hmac if it exists
|
|
15
|
-
// map input to query string
|
|
16
|
-
// generate hash using api secret key and validate it matches hmac
|
|
17
|
-
delete validationParams.hmac;
|
|
18
|
-
const hmacString = mapObjectToQueryString(validationParams);
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const generatedHash = createHmac('sha256', shopifyAppApiSecret)
|
|
22
|
-
.update(hmacString)
|
|
23
|
-
.digest('hex');
|
|
24
|
-
|
|
25
|
-
return generatedHash === validationHmac;
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
export const validateShopifyRequest = (validationParams: ShopifyRequestValidationParameters, validationHmac: string, shopifyAppApiSecret: string) => {
|
|
29
|
-
log.info('Validating shopify request is authentic', { validationParams });
|
|
30
|
-
const isValid = isShopifyRequestValid(validationParams, validationHmac as string, shopifyAppApiSecret);
|
|
31
|
-
if (!isValid) {
|
|
32
|
-
const message = 'Failed: Shopify Request hmac validation';
|
|
33
|
-
log.error(message);
|
|
34
|
-
throw HttpError.badRequest(message);
|
|
35
|
-
}
|
|
36
|
-
log.info('Sucess: Shopify Request hmac validation');
|
|
37
|
-
return true;
|
|
38
|
-
}
|
|
39
|
-
|
|
1
|
+
import { createHmac } from 'crypto';
|
|
2
|
+
import * as log from 'lambda-log';
|
|
3
|
+
import { HttpError } from '../libs';
|
|
4
|
+
import { mapObjectToQueryString } from '../libs/url';
|
|
5
|
+
export interface ShopifyRequestValidationParameters {
|
|
6
|
+
code: string,
|
|
7
|
+
hmac?: string,
|
|
8
|
+
shop: string,
|
|
9
|
+
state: string,
|
|
10
|
+
timestamp: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const isShopifyRequestValid = (validationParams: ShopifyRequestValidationParameters, validationHmac: string, shopifyAppApiSecret: string): boolean => {
|
|
14
|
+
// remove hmac if it exists
|
|
15
|
+
// map input to query string
|
|
16
|
+
// generate hash using api secret key and validate it matches hmac
|
|
17
|
+
delete validationParams.hmac;
|
|
18
|
+
const hmacString = mapObjectToQueryString(validationParams);
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
const generatedHash = createHmac('sha256', shopifyAppApiSecret)
|
|
22
|
+
.update(hmacString)
|
|
23
|
+
.digest('hex');
|
|
24
|
+
|
|
25
|
+
return generatedHash === validationHmac;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export const validateShopifyRequest = (validationParams: ShopifyRequestValidationParameters, validationHmac: string, shopifyAppApiSecret: string) => {
|
|
29
|
+
log.info('Validating shopify request is authentic', { validationParams });
|
|
30
|
+
const isValid = isShopifyRequestValid(validationParams, validationHmac as string, shopifyAppApiSecret);
|
|
31
|
+
if (!isValid) {
|
|
32
|
+
const message = 'Failed: Shopify Request hmac validation';
|
|
33
|
+
log.error(message);
|
|
34
|
+
throw HttpError.badRequest(message);
|
|
35
|
+
}
|
|
36
|
+
log.info('Sucess: Shopify Request hmac validation');
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
|