@agnostack/next-shopify 1.6.0-alpha.36 → 1.6.0-alpha.37

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/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # [1.6.0-alpha.37](https://github.com/agnostack/next-shopify/compare/v1.6.0-alpha.36...v1.6.0-alpha.37) (2023-05-19)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * add logging ([90f2f93](https://github.com/agnostack/next-shopify/commit/90f2f9319a2d7e59e756ea556fa6dc9526fa8ac1))
7
+
1
8
  # [1.6.0-alpha.36](https://github.com/agnostack/next-shopify/compare/v1.6.0-alpha.35...v1.6.0-alpha.36) (2023-05-19)
2
9
 
3
10
 
@@ -3,4 +3,6 @@ export * from "./constants";
3
3
  export * from "./display";
4
4
  export * from "./errors";
5
5
  export * from "./firebase";
6
+ export * from "./session";
6
7
  export * from "./shopify";
8
+ export * from "./webhooks";
@@ -0,0 +1,13 @@
1
+ export function getSessionStorage(serverRuntimeConfig: any, _shop: any): {
2
+ store: (session: any) => Promise<boolean>;
3
+ load: (id: any) => Promise<any>;
4
+ delete: (id: any) => Promise<void>;
5
+ deleteMany: (ids: any) => Promise<void>;
6
+ find: (__shop: any) => Promise<any[]>;
7
+ };
8
+ export function getVerifiedSession({ req, res, shop, serverRuntimeConfig }: {
9
+ req: any;
10
+ res: any;
11
+ shop: any;
12
+ serverRuntimeConfig: any;
13
+ }): Promise<any>;
@@ -0,0 +1,86 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getVerifiedSession = exports.getSessionStorage = void 0;
4
+ const shopify_api_1 = require("@shopify/shopify-api");
5
+ const display_1 = require("./display");
6
+ const shopify_1 = require("./shopify");
7
+ const billing_1 = require("./billing");
8
+ const errors_1 = require("./errors");
9
+ const constants_1 = require("./constants");
10
+ const firebase_1 = require("./firebase");
11
+ const getSessionStorage = (serverRuntimeConfig, _shop) => {
12
+ const { FB_CONFIG, FB_ROOT_COLLECTION, APP_ID } = serverRuntimeConfig;
13
+ const shopify = shopify_1.getInitializedShopify(serverRuntimeConfig);
14
+ const data = { app: APP_ID, shop: shopify.utils.sanitizeShop(_shop) };
15
+ const sessionHandler = firebase_1.FirebaseSessionHandler.getInstance(FB_CONFIG, FB_ROOT_COLLECTION);
16
+ return {
17
+ store: async (session) => firebase_1.storeCallback(session, data, sessionHandler),
18
+ load: async (id) => firebase_1.loadCallback(id, data, sessionHandler),
19
+ delete: async (id) => firebase_1.deleteSessionsCallback(id, data, sessionHandler),
20
+ deleteMany: async (ids) => firebase_1.deleteSessionsCallback(ids, data, sessionHandler),
21
+ find: async (__shop) => firebase_1.findSessionsByShop(shopify.utils.sanitizeShop(__shop), data, sessionHandler),
22
+ };
23
+ };
24
+ exports.getSessionStorage = getSessionStorage;
25
+ // TODO re-enable (check scopes and expiration)
26
+ const isSessionActive = (session, config) => {
27
+ console.log('isSessionActive:', { session, config });
28
+ return true;
29
+ // // TODO check scopes
30
+ // const scopesUnchanged = true
31
+ // if (
32
+ // scopesUnchanged &&
33
+ // (!session.expires || session.expires >= new Date())
34
+ // ) {
35
+ // return true
36
+ // }
37
+ // return false
38
+ };
39
+ const getVerifiedSession = async ({ req, res, shop, serverRuntimeConfig }) => {
40
+ try {
41
+ const shopify = shopify_1.getInitializedShopify(serverRuntimeConfig, shop);
42
+ const sessionStorage = exports.getSessionStorage(serverRuntimeConfig, shop);
43
+ if (sessionStorage) {
44
+ console.log('have sessionStorage');
45
+ }
46
+ else {
47
+ console.log('have NO sessionStorage');
48
+ }
49
+ const { PAYMENT_CONFIG } = serverRuntimeConfig !== null && serverRuntimeConfig !== void 0 ? serverRuntimeConfig : {};
50
+ const sessionId = await shopify.session.getCurrentId({
51
+ isOnline: true,
52
+ rawRequest: req,
53
+ rawResponse: res,
54
+ });
55
+ const session = await sessionStorage.load(sessionId);
56
+ if (!isSessionActive(session, shopify.config)) {
57
+ throw new errors_1.ShopifyAuthenticationError('Inactive session');
58
+ }
59
+ if (shop && (session === null || session === void 0 ? void 0 : session.shop) !== shop) {
60
+ throw new errors_1.ShopifyAuthenticationError('Invalid shop');
61
+ }
62
+ if (PAYMENT_CONFIG === null || PAYMENT_CONFIG === void 0 ? void 0 : PAYMENT_CONFIG.required) {
63
+ const billingManager = new billing_1.BillingManager(shopify, session, serverRuntimeConfig);
64
+ const confirmationUrl = await billingManager.ensureBilling();
65
+ console.log('confirmationUrl:', confirmationUrl);
66
+ if (display_1.stringNotEmpty(confirmationUrl)) {
67
+ throw new errors_1.ShopifyAuthenticationError('Invalid payment status', { redirectUrl: confirmationUrl });
68
+ }
69
+ }
70
+ else {
71
+ // NOTE making a test call to ensure a valid session for free apps
72
+ const client = new shopify.clients.Graphql({ session });
73
+ await client.query({ data: constants_1.SHOP_QUERY });
74
+ }
75
+ return session;
76
+ }
77
+ catch (error) {
78
+ if (error instanceof shopify_api_1.HttpResponseError &&
79
+ error.response.code === 401) {
80
+ throw new errors_1.ShopifyAuthenticationError('Not authenticated');
81
+ }
82
+ throw error;
83
+ }
84
+ };
85
+ exports.getVerifiedSession = getVerifiedSession;
86
+ //# sourceMappingURL=session.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session.js","sourceRoot":"","sources":["../../src/utils/session.js"],"names":[],"mappings":";;;AAAA,sDAAwD;AAExD,uCAA0C;AAC1C,uCAAiD;AACjD,uCAA0C;AAC1C,qCAAqD;AACrD,2CAAwC;AACxC,yCAMmB;AAEZ,MAAM,iBAAiB,GAAG,CAAC,mBAAmB,EAAE,KAAK,EAAE,EAAE;IAC9D,MAAM,EAAE,SAAS,EAAE,kBAAkB,EAAE,MAAM,EAAE,GAAG,mBAAmB,CAAA;IAErE,MAAM,OAAO,GAAG,+BAAqB,CAAC,mBAAmB,CAAC,CAAA;IAE1D,MAAM,IAAI,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,CAAC,KAAK,CAAC,YAAY,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AAe,EAAE,CACjC,CAAA;aACF;SACF;aAAM;YACL,kEAAkE;YAClE,MAAM,MAAM,GAAG,IAAI,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,OAAO,EAAE,CAAC,CAAA;YACvD,MAAM,MAAM,CAAC,KAAK,CAAC,EAAE,IAAI,EAAE,sBAAU,EAAE,CAAC,CAAA;SACzC;QAED,OAAO,OAAO,CAAA;KACf;IAAC,OAAO,KAAK,EAAE;QACd,IACE,KAAK,YAAY,+BAAiB;YAClC,KAAK,CAAC,QAAQ,CAAC,IAAI,KAAK,GAAG,EAC3B;YACA,MAAM,IAAI,mCAA0B,CAAC,mBAAmB,CAAC,CAAA;SAC1D;QAED,MAAM,KAAK,CAAA;KACZ;AACH,CAAC,CAAA;AAzDY,QAAA,kBAAkB,sBAyD9B"}
@@ -0,0 +1 @@
1
+ export function addWebhookHandlers(serverRuntimeConfig: any, shopify: any): Promise<void>;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.addWebhookHandlers = void 0;
4
+ const shopify_api_1 = require("@shopify/shopify-api");
5
+ const session_1 = require("./session");
6
+ const handleWebhook = async (topic, shop, webhookRequestBody) => {
7
+ const sessionStorage = session_1.getSessionStorage(JSON.parse(webhookRequestBody), shop);
8
+ console.log('sessionStorage:', sessionStorage);
9
+ switch (topic) {
10
+ // TODO pull from constants
11
+ case 'APP_UNINSTALLED': {
12
+ const sessionsIds = await sessionStorage.find(shop);
13
+ if (sessionsIds.length) {
14
+ return sessionStorage.deleteMany(sessionsIds);
15
+ }
16
+ break;
17
+ }
18
+ default: {
19
+ return;
20
+ }
21
+ }
22
+ };
23
+ const addWebhookHandlers = async (serverRuntimeConfig, shopify) => {
24
+ const { APP_WEBHOOKS } = serverRuntimeConfig;
25
+ const handlers = Object.entries(APP_WEBHOOKS).reduce((__payload, [topic, webhooks]) => (Object.assign(Object.assign({}, __payload), { [topic]: webhooks.map((webhook) => {
26
+ console.log('webhook:', webhook);
27
+ return {
28
+ deliveryMethod: shopify_api_1.DeliveryMethod.Http,
29
+ callbackUrl: webhook.path,
30
+ callback: handleWebhook,
31
+ };
32
+ }) })), {});
33
+ console.log('handlers:', handlers);
34
+ await shopify.webhooks.addHandlers(handlers);
35
+ const bar = await shopify.webhooks.getHandlers('APP_UNINSTALLED');
36
+ console.log('shopify.webhooks.getHandlers1:', bar);
37
+ };
38
+ exports.addWebhookHandlers = addWebhookHandlers;
39
+ //# sourceMappingURL=webhooks.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webhooks.js","sourceRoot":"","sources":["../../src/utils/webhooks.js"],"names":[],"mappings":";;;AAAA,sDAAqD;AAErD,uCAA6C;AAE7C,MAAM,aAAa,GAAG,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,kBAAkB,EAAE,EAAE;IAC9D,MAAM,cAAc,GAAG,2BAAiB,CAAC,IAAI,CAAC,KAAK,CAAC,kBAAkB,CAAC,EAAE,IAAI,CAAC,CAAA;IAC9E,OAAO,CAAC,GAAG,CAAC,iBAAiB,EAAE,cAAc,CAAC,CAAA;IAE9C,QAAQ,KAAK,EAAE;QACb,2BAA2B;QAC3B,KAAK,iBAAiB,CAAC,CAAC;YACtB,MAAM,WAAW,GAAG,MAAM,cAAc,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;YACnD,IAAI,WAAW,CAAC,MAAM,EAAE;gBACtB,OAAO,cAAc,CAAC,UAAU,CAAC,WAAW,CAAC,CAAA;aAC9C;YACD,MAAK;SACN;QAED,OAAO,CAAC,CAAC;YACP,OAAM;SACP;KACF;AACH,CAAC,CAAA;AAEM,MAAM,kBAAkB,GAAG,KAAK,EAAE,mBAAmB,EAAE,OAAO,EAAE,EAAE;IACvE,MAAM,EAAE,YAAY,EAAE,GAAG,mBAAmB,CAAA;IAE5C,MAAM,QAAQ,GAAG,MAAM,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC,MAAM,CAAC,CAAC,SAAS,EAAE,CAAC,KAAK,EAAE,QAAQ,CAAC,EAAE,EAAE,CAAC,iCAClF,SAAS,KACZ,CAAC,KAAK,CAAC,EAAE,QAAQ,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE;YAChC,OAAO,CAAC,GAAG,CAAC,UAAU,EAAE,OAAO,CAAC,CAAA;YAChC,OAAO;gBACL,cAAc,EAAE,4BAAc,CAAC,IAAI;gBACnC,WAAW,EAAE,OAAO,CAAC,IAAI;gBACzB,QAAQ,EAAE,aAAa;aACxB,CAAA;QACH,CAAC,CAAC,IACF,EAAE,EAAE,CAAC,CAAA;IACP,OAAO,CAAC,GAAG,CAAC,WAAW,EAAE,QAAQ,CAAC,CAAA;IAElC,MAAM,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAA;IAE5C,MAAM,GAAG,GAAG,MAAM,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,iBAAiB,CAAC,CAAA;IACjE,OAAO,CAAC,GAAG,CAAC,gCAAgC,EAAE,GAAG,CAAC,CAAA;AACpD,CAAC,CAAA;AApBY,QAAA,kBAAkB,sBAoB9B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agnostack/next-shopify",
3
- "version": "1.6.0-alpha.36",
3
+ "version": "1.6.0-alpha.37",
4
4
  "author": "agnoStack Dev <developers@agnostack.com> (https://agnostack.com)",
5
5
  "owner": "agnoStack",
6
6
  "description": "Please contact agnoStack via info@agnostack.com for any questions",