@adtrackify/at-service-common 1.1.13 → 1.1.16

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.
@@ -1,139 +1,139 @@
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 update Shopify app Metafield ', { 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 cancelAppSubscription = async (shop: string, accessToken: string, chargeId: string) => {
100
- const url = `https://${shop}/admin/api/${this._shopify_api_version}/recurring_application_charges/${chargeId}.json`;
101
- const client = axiosHttpService();
102
- const res = await client.delete(url, { headers: { 'X-Shopify-Access-Token': accessToken } });
103
-
104
- if (res.status !== 200) {
105
- log.error('Failed to cancel recurring App billing', { shop, accessToken, url });
106
- }
107
- return res;
108
- };
109
-
110
- static listAppSubscriptions = async (shop: string, accessToken: string) => {
111
- const url = `https://${shop}/admin/api/${this._shopify_api_version}/recurring_application_charges.json`;
112
- const res = await this.genericShopifyGet(url, accessToken);
113
- if (res.status >= 400) {
114
- log.error('Failed to get App Subscriptions', { shop, accessToken, url });
115
- }
116
- return res;
117
- };
118
-
119
- static genericShopifyPost = async (url: string, accessToken: string, payload: any) => {
120
- const client = axiosHttpService();
121
- const res = await client.post(url, payload, { headers: { 'X-Shopify-Access-Token': accessToken, 'Content-Type': 'application/json' } });
122
- log.debug('Shopify Client Response', { res });
123
- return res;
124
- };
125
-
126
- static genericShopifyGet = async (url: string, accessToken: string) => {
127
- const client = axiosHttpService();
128
- const res = await client.get(url, { headers: { 'X-Shopify-Access-Token': accessToken } });
129
- log.debug('Shopify Client Response', { res });
130
- return res;
131
- };
132
-
133
- static genericShopifyPut = async (url: string, accessToken: string, payload: any) => {
134
- const client = axiosHttpService();
135
- const res = await client.put(url, payload, { headers: { 'X-Shopify-Access-Token': accessToken } });
136
- log.debug('Shopify Client Response', { res });
137
- return res;
138
- };
139
- }
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 update Shopify app Metafield ', { 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 cancelAppSubscription = async (shop: string, accessToken: string, chargeId: string) => {
100
+ const url = `https://${shop}/admin/api/${this._shopify_api_version}/recurring_application_charges/${chargeId}.json`;
101
+ const client = axiosHttpService();
102
+ const res = await client.delete(url, { headers: { 'X-Shopify-Access-Token': accessToken } });
103
+
104
+ if (res.status !== 200) {
105
+ log.error('Failed to cancel recurring App billing', { shop, accessToken, url });
106
+ }
107
+ return res;
108
+ };
109
+
110
+ static listAppSubscriptions = async (shop: string, accessToken: string) => {
111
+ const url = `https://${shop}/admin/api/${this._shopify_api_version}/recurring_application_charges.json`;
112
+ const res = await this.genericShopifyGet(url, accessToken);
113
+ if (res.status >= 400) {
114
+ log.error('Failed to get App Subscriptions', { shop, accessToken, url });
115
+ }
116
+ return res;
117
+ };
118
+
119
+ static genericShopifyPost = async (url: string, accessToken: string, payload: any) => {
120
+ const client = axiosHttpService();
121
+ const res = await client.post(url, payload, { headers: { 'X-Shopify-Access-Token': accessToken, 'Content-Type': 'application/json' } });
122
+ log.debug('Shopify Client Response', { res });
123
+ return res;
124
+ };
125
+
126
+ static genericShopifyGet = async (url: string, accessToken: string) => {
127
+ const client = axiosHttpService();
128
+ const res = await client.get(url, { headers: { 'X-Shopify-Access-Token': accessToken } });
129
+ log.debug('Shopify Client Response', { res });
130
+ return res;
131
+ };
132
+
133
+ static genericShopifyPut = async (url: string, accessToken: string, payload: any) => {
134
+ const client = axiosHttpService();
135
+ const res = await client.put(url, payload, { headers: { 'X-Shopify-Access-Token': accessToken } });
136
+ log.debug('Shopify Client Response', { res });
137
+ return res;
138
+ };
139
+ }
@@ -1,5 +1,5 @@
1
- export * from './input-validation-helper';
2
- export * from './logging-helper';
3
- export * from './response-helper';
4
- export * from './shopify-helper';
5
- export * from './subscription-helper';
1
+ export * from './input-validation-helper';
2
+ export * from './logging-helper';
3
+ export * from './response-helper';
4
+ export * from './shopify-helper';
5
+ export * from './subscription-helper';
@@ -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
+
package/src/index.ts CHANGED
@@ -1,5 +1,5 @@
1
- export * from './clients';
2
- export * from './helpers';
3
- export * from './libs';
4
- export * from './types';
1
+ export * from './clients';
2
+ export * from './helpers';
3
+ export * from './libs';
4
+ export * from './types';
5
5
  export * from './services';
package/src/libs/index.ts CHANGED
@@ -1,7 +1,7 @@
1
- export * from '../helpers/shopify-helper';
2
- export * from './crypto';
3
- export * from './dates';
4
- export * from './http-error';
5
- export * from './http-status-codes';
6
- export * from './url';
7
-
1
+ export * from '../helpers/shopify-helper';
2
+ export * from './crypto';
3
+ export * from './dates';
4
+ export * from './http-error';
5
+ export * from './http-status-codes';
6
+ export * from './url';
7
+
package/src/libs/url.ts CHANGED
@@ -1,10 +1,10 @@
1
- // Record<string, string> is any object
2
- export const mapObjectToQueryString = (inputObj: any): string => {
3
- const qsp = Object.entries(inputObj).sort((a, b) => a[ 0 ] < b[ 0 ] ? -1 : 1);
4
- const urlParams = new URLSearchParams();
5
- qsp.map(p => {
6
- urlParams.append(p[ 0 ], p[ 1 ] as string);
7
- });
8
- const qs = urlParams.toString();
9
- return qs;
1
+ // Record<string, string> is any object
2
+ export const mapObjectToQueryString = (inputObj: any): string => {
3
+ const qsp = Object.entries(inputObj).sort((a, b) => a[ 0 ] < b[ 0 ] ? -1 : 1);
4
+ const urlParams = new URLSearchParams();
5
+ qsp.map(p => {
6
+ urlParams.append(p[ 0 ], p[ 1 ] as string);
7
+ });
8
+ const qs = urlParams.toString();
9
+ return qs;
10
10
  };
@@ -1,44 +1,44 @@
1
- import { EventBridgeClient } from '../clients';
2
- import { Message, TemplatedMessage } from 'postmark';
3
-
4
- export const enum PostmarkRequestType {
5
- SINGLE_EMAIL = 'single_email',
6
- TEMPLATE_EMAIL = 'template_email'
7
- }
8
-
9
- export enum ADTRACKIFY_EVENT_BRIDGE_EVENTS {
10
- SEND_POSTMARK_EMAIL = 'integration.sendPostmarkEmail',
11
- }
12
-
13
- export class EventBridgeIntegrationService {
14
- public eventBridgeClient: EventBridgeClient;
15
- public EVENT_BUS_NAME: string;
16
-
17
- constructor (eventBusName: string) {
18
- this.eventBridgeClient = new EventBridgeClient(eventBusName);
19
- this.EVENT_BUS_NAME = eventBusName;
20
- }
21
-
22
- public sendPostmarkEmailEvent = async (eventSource: string, postmarkMessage: Message, postmarkServerToken: string) => {
23
- return await this.eventBridgeClient.buildAndSendEvent(
24
- eventSource,
25
- ADTRACKIFY_EVENT_BRIDGE_EVENTS.SEND_POSTMARK_EMAIL,
26
- {
27
- postmarkMessage,
28
- postmarkRequestType: PostmarkRequestType.SINGLE_EMAIL,
29
- postmarkServerToken
30
- });
31
- };
32
-
33
- public sendPostmarkTemplatedEmailEvent = async (eventSource: string, postmarkMessage: TemplatedMessage, postmarkServerToken: string) => {
34
- return await this.eventBridgeClient.buildAndSendEvent(
35
- eventSource,
36
- ADTRACKIFY_EVENT_BRIDGE_EVENTS.SEND_POSTMARK_EMAIL,
37
- {
38
- postmarkMessage,
39
- postmarkRequestType: PostmarkRequestType.TEMPLATE_EMAIL,
40
- postmarkServerToken
41
- });
42
- };
43
-
44
- }
1
+ import { EventBridgeClient } from '../clients';
2
+ import { Message, TemplatedMessage } from 'postmark';
3
+
4
+ export const enum PostmarkRequestType {
5
+ SINGLE_EMAIL = 'single_email',
6
+ TEMPLATE_EMAIL = 'template_email'
7
+ }
8
+
9
+ export enum ADTRACKIFY_EVENT_BRIDGE_EVENTS {
10
+ SEND_POSTMARK_EMAIL = 'integration.sendPostmarkEmail',
11
+ }
12
+
13
+ export class EventBridgeIntegrationService {
14
+ public eventBridgeClient: EventBridgeClient;
15
+ public EVENT_BUS_NAME: string;
16
+
17
+ constructor (eventBusName: string) {
18
+ this.eventBridgeClient = new EventBridgeClient(eventBusName);
19
+ this.EVENT_BUS_NAME = eventBusName;
20
+ }
21
+
22
+ public sendPostmarkEmailEvent = async (eventSource: string, postmarkMessage: Message, postmarkServerToken: string) => {
23
+ return await this.eventBridgeClient.buildAndSendEvent(
24
+ eventSource,
25
+ ADTRACKIFY_EVENT_BRIDGE_EVENTS.SEND_POSTMARK_EMAIL,
26
+ {
27
+ postmarkMessage,
28
+ postmarkRequestType: PostmarkRequestType.SINGLE_EMAIL,
29
+ postmarkServerToken
30
+ });
31
+ };
32
+
33
+ public sendPostmarkTemplatedEmailEvent = async (eventSource: string, postmarkMessage: TemplatedMessage, postmarkServerToken: string) => {
34
+ return await this.eventBridgeClient.buildAndSendEvent(
35
+ eventSource,
36
+ ADTRACKIFY_EVENT_BRIDGE_EVENTS.SEND_POSTMARK_EMAIL,
37
+ {
38
+ postmarkMessage,
39
+ postmarkRequestType: PostmarkRequestType.TEMPLATE_EMAIL,
40
+ postmarkServerToken
41
+ });
42
+ };
43
+
44
+ }
@@ -1,10 +1,10 @@
1
- export enum ADTRACKIFY_EVENT_TYPES {
2
- NOTIFY_SHOPIFY_SUBSCRIPTION_CREATED = 'shopifySubscriptionCreated',
3
- NOTIFY_SUBSCRIPTION_SIGNUP_COMPLETED = 'subscription.signupCompleted',
4
- REQUEST_SET_ACCOUNT_OWNER = 'setAccountOwner',
5
- REQUEST_SET_ACCOUNT_SUBSCRIPTION_ID = 'setAccountSubscriptionId',
6
- }
7
-
8
- export enum ADTRACKIFY_EVENT_SOURCES {
9
- SUBSCRIPTIONS = 'subscriptions',
1
+ export enum ADTRACKIFY_EVENT_TYPES {
2
+ NOTIFY_SHOPIFY_SUBSCRIPTION_CREATED = 'shopifySubscriptionCreated',
3
+ NOTIFY_SUBSCRIPTION_SIGNUP_COMPLETED = 'subscription.signupCompleted',
4
+ REQUEST_SET_ACCOUNT_OWNER = 'setAccountOwner',
5
+ REQUEST_SET_ACCOUNT_SUBSCRIPTION_ID = 'setAccountSubscriptionId',
6
+ }
7
+
8
+ export enum ADTRACKIFY_EVENT_SOURCES {
9
+ SUBSCRIPTIONS = 'subscriptions',
10
10
  }