@shopify/shopify-api 3.0.1 → 3.1.3

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.
Files changed (71) hide show
  1. package/CHANGELOG.md +25 -1
  2. package/dist/__tests__/fake-resource-with-custom-prefix.d.ts.map +1 -1
  3. package/dist/__tests__/fake-resource-with-custom-prefix.js +7 -5
  4. package/dist/__tests__/fake-resource.d.ts.map +1 -1
  5. package/dist/__tests__/fake-resource.js +15 -13
  6. package/dist/auth/oauth/oauth.d.ts.map +1 -1
  7. package/dist/auth/oauth/oauth.js +11 -11
  8. package/dist/auth/scopes/index.js +5 -5
  9. package/dist/auth/session/index.d.ts +11 -1
  10. package/dist/auth/session/index.d.ts.map +1 -1
  11. package/dist/auth/session/index.js +16 -1
  12. package/dist/auth/session/session-utils.d.ts +8 -0
  13. package/dist/auth/session/session-utils.d.ts.map +1 -0
  14. package/dist/auth/session/session-utils.js +116 -0
  15. package/dist/auth/session/storage/__tests__/battery-of-tests.d.ts +3 -0
  16. package/dist/auth/session/storage/__tests__/battery-of-tests.d.ts.map +1 -0
  17. package/dist/auth/session/storage/__tests__/battery-of-tests.js +191 -0
  18. package/dist/auth/session/storage/__tests__/utils.d.ts +10 -0
  19. package/dist/auth/session/storage/__tests__/utils.d.ts.map +1 -0
  20. package/dist/auth/session/storage/__tests__/utils.js +87 -0
  21. package/dist/auth/session/storage/custom.js +4 -4
  22. package/dist/auth/session/storage/mongodb.d.ts +23 -0
  23. package/dist/auth/session/storage/mongodb.d.ts.map +1 -0
  24. package/dist/auth/session/storage/mongodb.js +150 -0
  25. package/dist/auth/session/storage/mysql.d.ts +22 -0
  26. package/dist/auth/session/storage/mysql.d.ts.map +1 -0
  27. package/dist/auth/session/storage/mysql.js +154 -0
  28. package/dist/auth/session/storage/postgresql.d.ts +24 -0
  29. package/dist/auth/session/storage/postgresql.d.ts.map +1 -0
  30. package/dist/auth/session/storage/postgresql.js +172 -0
  31. package/dist/auth/session/storage/redis.d.ts +20 -0
  32. package/dist/auth/session/storage/redis.d.ts.map +1 -0
  33. package/dist/auth/session/storage/redis.js +105 -0
  34. package/dist/auth/session/storage/sqlite.d.ts +19 -0
  35. package/dist/auth/session/storage/sqlite.d.ts.map +1 -0
  36. package/dist/auth/session/storage/sqlite.js +128 -0
  37. package/dist/base-rest-resource.d.ts +2 -1
  38. package/dist/base-rest-resource.d.ts.map +1 -1
  39. package/dist/base-rest-resource.js +29 -19
  40. package/dist/base-types.d.ts +1 -0
  41. package/dist/base-types.d.ts.map +1 -1
  42. package/dist/clients/graphql/graphql_client.js +1 -1
  43. package/dist/clients/http_client/http_client.d.ts +1 -2
  44. package/dist/clients/http_client/http_client.d.ts.map +1 -1
  45. package/dist/clients/http_client/http_client.js +100 -99
  46. package/dist/clients/rest/rest_client.js +5 -3
  47. package/dist/context.d.ts +1 -0
  48. package/dist/context.d.ts.map +1 -1
  49. package/dist/context.js +13 -5
  50. package/dist/error.js +4 -3
  51. package/dist/index.d.ts +5 -0
  52. package/dist/index.d.ts.map +1 -1
  53. package/dist/rest-resources/2021-07/index.js +2 -0
  54. package/dist/rest-resources/2021-10/index.js +2 -0
  55. package/dist/rest-resources/2022-01/index.js +2 -0
  56. package/dist/rest-resources/2022-04/index.js +2 -0
  57. package/dist/utils/decode-session-token.js +1 -1
  58. package/dist/utils/graphql_proxy.js +1 -1
  59. package/dist/utils/hmac-validator.js +2 -5
  60. package/dist/utils/processed-query.js +3 -3
  61. package/dist/utils/safe-compare.d.ts.map +1 -1
  62. package/dist/utils/safe-compare.js +17 -15
  63. package/dist/utils/setup-jest.d.ts.map +1 -1
  64. package/dist/utils/setup-jest.js +4 -3
  65. package/dist/utils/with-session.js +3 -3
  66. package/dist/version.d.ts +1 -1
  67. package/dist/version.js +1 -1
  68. package/dist/webhooks/registry.d.ts +2 -1
  69. package/dist/webhooks/registry.d.ts.map +1 -1
  70. package/dist/webhooks/registry.js +39 -13
  71. package/package.json +12 -4
@@ -0,0 +1,191 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.batteryOfTests = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var context_1 = require("../../../../context");
6
+ var session_1 = require("../../session");
7
+ var session_utils_1 = require("../../session-utils");
8
+ function batteryOfTests(storageFactory) {
9
+ var _this = this;
10
+ it('can store and delete all kinds of sessions', function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
11
+ var sessionFactories, sessionId, storage, sessionFactories_1, sessionFactories_1_1, factory, session, _a, _b, loadedSession, e_1_1;
12
+ var e_1, _c;
13
+ var _this = this;
14
+ return tslib_1.__generator(this, function (_d) {
15
+ switch (_d.label) {
16
+ case 0:
17
+ sessionFactories = [
18
+ function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
19
+ var session;
20
+ return tslib_1.__generator(this, function (_a) {
21
+ session = new session_1.Session(sessionId, 'shop', 'state', false);
22
+ session.scope = context_1.Context.SCOPES.toString();
23
+ session.accessToken = '123';
24
+ return [2 /*return*/, session];
25
+ });
26
+ }); },
27
+ function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
28
+ var session, expiryDate;
29
+ return tslib_1.__generator(this, function (_a) {
30
+ session = new session_1.Session(sessionId, 'shop', 'state', false);
31
+ expiryDate = new Date();
32
+ expiryDate.setMinutes(expiryDate.getMinutes() + 60);
33
+ session.expires = expiryDate;
34
+ session.accessToken = '123';
35
+ session.scope = context_1.Context.SCOPES.toString();
36
+ return [2 /*return*/, session];
37
+ });
38
+ }); },
39
+ function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
40
+ var session;
41
+ return tslib_1.__generator(this, function (_a) {
42
+ session = new session_1.Session(sessionId, 'shop', 'state', false);
43
+ session.expires = null;
44
+ session.scope = context_1.Context.SCOPES.toString();
45
+ session.accessToken = '123';
46
+ return [2 /*return*/, session];
47
+ });
48
+ }); },
49
+ function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
50
+ var session;
51
+ return tslib_1.__generator(this, function (_a) {
52
+ session = new session_1.Session(sessionId, 'shop', 'state', false);
53
+ session.expires = undefined;
54
+ session.scope = context_1.Context.SCOPES.toString();
55
+ session.accessToken = '123';
56
+ return [2 /*return*/, session];
57
+ });
58
+ }); },
59
+ function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
60
+ var session;
61
+ return tslib_1.__generator(this, function (_a) {
62
+ session = new session_1.Session(sessionId, 'shop', 'state', false);
63
+ // eslint-disable-next-line @typescript-eslint/naming-convention
64
+ session.onlineAccessInfo = { associated_user: {} };
65
+ session.onlineAccessInfo.associated_user.id = 123;
66
+ session.scope = context_1.Context.SCOPES.toString();
67
+ session.accessToken = '123';
68
+ return [2 /*return*/, session];
69
+ });
70
+ }); },
71
+ ];
72
+ sessionId = 'test_session';
73
+ return [4 /*yield*/, storageFactory()];
74
+ case 1:
75
+ storage = _d.sent();
76
+ _d.label = 2;
77
+ case 2:
78
+ _d.trys.push([2, 14, 15, 16]);
79
+ sessionFactories_1 = tslib_1.__values(sessionFactories), sessionFactories_1_1 = sessionFactories_1.next();
80
+ _d.label = 3;
81
+ case 3:
82
+ if (!!sessionFactories_1_1.done) return [3 /*break*/, 13];
83
+ factory = sessionFactories_1_1.value;
84
+ return [4 /*yield*/, factory()];
85
+ case 4:
86
+ session = _d.sent();
87
+ return [4 /*yield*/, expect(storage.storeSession(session)).resolves.toBe(true)];
88
+ case 5:
89
+ _d.sent();
90
+ _a = expect;
91
+ _b = session_utils_1.sessionEqual;
92
+ return [4 /*yield*/, storage.loadSession(sessionId)];
93
+ case 6:
94
+ _a.apply(void 0, [_b.apply(void 0, [_d.sent(), session])]).toBe(true);
95
+ return [4 /*yield*/, expect(storage.storeSession(session)).resolves.toBe(true)];
96
+ case 7:
97
+ _d.sent();
98
+ return [4 /*yield*/, storage.loadSession(sessionId)];
99
+ case 8:
100
+ loadedSession = _d.sent();
101
+ expect((0, session_utils_1.sessionEqual)(loadedSession, session)).toBe(true);
102
+ expect(loadedSession === null || loadedSession === void 0 ? void 0 : loadedSession.isActive()).toBe(true);
103
+ return [4 /*yield*/, expect(storage.deleteSession(sessionId)).resolves.toBe(true)];
104
+ case 9:
105
+ _d.sent();
106
+ return [4 /*yield*/, expect(storage.loadSession(sessionId)).resolves.toBeUndefined()];
107
+ case 10:
108
+ _d.sent();
109
+ // Deleting a non-existing session should work
110
+ return [4 /*yield*/, expect(storage.deleteSession(sessionId)).resolves.toBe(true)];
111
+ case 11:
112
+ // Deleting a non-existing session should work
113
+ _d.sent();
114
+ _d.label = 12;
115
+ case 12:
116
+ sessionFactories_1_1 = sessionFactories_1.next();
117
+ return [3 /*break*/, 3];
118
+ case 13: return [3 /*break*/, 16];
119
+ case 14:
120
+ e_1_1 = _d.sent();
121
+ e_1 = { error: e_1_1 };
122
+ return [3 /*break*/, 16];
123
+ case 15:
124
+ try {
125
+ if (sessionFactories_1_1 && !sessionFactories_1_1.done && (_c = sessionFactories_1.return)) _c.call(sessionFactories_1);
126
+ }
127
+ finally { if (e_1) throw e_1.error; }
128
+ return [7 /*endfinally*/];
129
+ case 16: return [2 /*return*/];
130
+ }
131
+ });
132
+ }); });
133
+ it('can store sessions with unexpected fields', function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
134
+ var storage, sessionId, session, _a, _b;
135
+ return tslib_1.__generator(this, function (_c) {
136
+ switch (_c.label) {
137
+ case 0: return [4 /*yield*/, storageFactory()];
138
+ case 1:
139
+ storage = _c.sent();
140
+ sessionId = 'test_session';
141
+ session = new session_1.Session(sessionId, 'shop', 'state', true);
142
+ session.someField = 'lol';
143
+ return [4 /*yield*/, expect(storage.storeSession(session)).resolves.toBe(true)];
144
+ case 2:
145
+ _c.sent();
146
+ _a = expect;
147
+ _b = session_utils_1.sessionEqual;
148
+ return [4 /*yield*/, storage.loadSession(sessionId)];
149
+ case 3:
150
+ _a.apply(void 0, [_b.apply(void 0, [_c.sent(), session])]).toBe(true);
151
+ return [2 /*return*/];
152
+ }
153
+ });
154
+ }); });
155
+ it('can store and delete sessions with online tokens', function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
156
+ var storage, sessionId, session, _a, _b;
157
+ return tslib_1.__generator(this, function (_c) {
158
+ switch (_c.label) {
159
+ case 0: return [4 /*yield*/, storageFactory()];
160
+ case 1:
161
+ storage = _c.sent();
162
+ sessionId = 'test_session';
163
+ session = new session_1.Session(sessionId, 'shop', 'state', true);
164
+ return [4 /*yield*/, expect(storage.storeSession(session)).resolves.toBe(true)];
165
+ case 2:
166
+ _c.sent();
167
+ _a = expect;
168
+ _b = session_utils_1.sessionEqual;
169
+ return [4 /*yield*/, storage.loadSession(sessionId)];
170
+ case 3:
171
+ _a.apply(void 0, [_b.apply(void 0, [_c.sent(), session])]).toBe(true);
172
+ return [2 /*return*/];
173
+ }
174
+ });
175
+ }); });
176
+ it('wrong ids return null sessions', function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
177
+ var storage;
178
+ return tslib_1.__generator(this, function (_a) {
179
+ switch (_a.label) {
180
+ case 0: return [4 /*yield*/, storageFactory()];
181
+ case 1:
182
+ storage = _a.sent();
183
+ return [4 /*yield*/, expect(storage.loadSession('not_a_session_id')).resolves.toBeUndefined()];
184
+ case 2:
185
+ _a.sent();
186
+ return [2 /*return*/];
187
+ }
188
+ });
189
+ }); });
190
+ }
191
+ exports.batteryOfTests = batteryOfTests;
@@ -0,0 +1,10 @@
1
+ /// <reference types="node" />
2
+ import { Socket, SocketConnectOpts } from 'net';
3
+ export declare function wait(ms: number): Promise<void>;
4
+ export declare function poll(func: () => Promise<boolean>, { interval, timeout }?: {
5
+ interval?: number | undefined;
6
+ timeout?: number | undefined;
7
+ }): Promise<void>;
8
+ export declare function waitForData(socket: Socket): Promise<string>;
9
+ export declare function connectSocket(socket: Socket, opts: SocketConnectOpts): Promise<void>;
10
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../../../src/auth/session/storage/__tests__/utils.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,MAAM,EAAE,iBAAiB,EAAC,MAAM,KAAK,CAAC;AAE9C,wBAAgB,IAAI,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAE9C;AAED,wBAAsB,IAAI,CACxB,IAAI,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,EAC5B,EAAC,QAAc,EAAE,OAAc,EAAC;;;CAAK,iBAiBtC;AAED,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAuB3D;AAED,wBAAgB,aAAa,CAC3B,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,iBAAiB,GACtB,OAAO,CAAC,IAAI,CAAC,CAef"}
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.connectSocket = exports.waitForData = exports.poll = exports.wait = void 0;
4
+ var tslib_1 = require("tslib");
5
+ function wait(ms) {
6
+ return new Promise(function (resolve) { return setTimeout(function () { return resolve(); }, ms); });
7
+ }
8
+ exports.wait = wait;
9
+ function poll(func, _a) {
10
+ var _b = _a === void 0 ? {} : _a, _c = _b.interval, interval = _c === void 0 ? 100 : _c, _d = _b.timeout, timeout = _d === void 0 ? 5000 : _d;
11
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
12
+ var start, elapsed, success, _e;
13
+ return tslib_1.__generator(this, function (_f) {
14
+ switch (_f.label) {
15
+ case 0:
16
+ start = Date.now();
17
+ _f.label = 1;
18
+ case 1:
19
+ if (!true) return [3 /*break*/, 7];
20
+ elapsed = Date.now() - start;
21
+ if (elapsed > timeout) {
22
+ throw Error('Timeout');
23
+ }
24
+ _f.label = 2;
25
+ case 2:
26
+ _f.trys.push([2, 4, , 5]);
27
+ return [4 /*yield*/, func()];
28
+ case 3:
29
+ success = _f.sent();
30
+ if (success)
31
+ return [2 /*return*/];
32
+ return [3 /*break*/, 5];
33
+ case 4:
34
+ _e = _f.sent();
35
+ return [3 /*break*/, 5];
36
+ case 5: return [4 /*yield*/, wait(interval)];
37
+ case 6:
38
+ _f.sent();
39
+ return [3 /*break*/, 1];
40
+ case 7: return [2 /*return*/];
41
+ }
42
+ });
43
+ });
44
+ }
45
+ exports.poll = poll;
46
+ function waitForData(socket) {
47
+ return new Promise(function (resolve, reject) {
48
+ function data() {
49
+ resolve('data');
50
+ cleanup();
51
+ }
52
+ function error(err) {
53
+ reject(err);
54
+ cleanup();
55
+ }
56
+ function close() {
57
+ resolve('no data');
58
+ cleanup();
59
+ }
60
+ function cleanup() {
61
+ socket.off('data', data);
62
+ socket.off('error', error);
63
+ socket.off('close', close);
64
+ }
65
+ socket.on('data', data);
66
+ socket.on('error', error);
67
+ socket.on('close', close);
68
+ });
69
+ }
70
+ exports.waitForData = waitForData;
71
+ function connectSocket(socket, opts) {
72
+ return new Promise(function (resolve, reject) {
73
+ function error(err) {
74
+ reject(err);
75
+ cleanup();
76
+ }
77
+ function cleanup() {
78
+ socket.off('error', error);
79
+ }
80
+ socket.on('error', error);
81
+ socket.connect(opts, function () {
82
+ resolve();
83
+ cleanup();
84
+ });
85
+ });
86
+ }
87
+ exports.connectSocket = connectSocket;
@@ -24,7 +24,7 @@ var CustomSessionStorage = /** @class */ (function () {
24
24
  case 1: return [2 /*return*/, _a.sent()];
25
25
  case 2:
26
26
  error_1 = _a.sent();
27
- throw new ShopifyErrors.SessionStorageError("CustomSessionStorage failed to store a session. Error Details: " + error_1);
27
+ throw new ShopifyErrors.SessionStorageError("CustomSessionStorage failed to store a session. Error Details: ".concat(error_1));
28
28
  case 3: return [2 /*return*/];
29
29
  }
30
30
  });
@@ -43,7 +43,7 @@ var CustomSessionStorage = /** @class */ (function () {
43
43
  return [3 /*break*/, 3];
44
44
  case 2:
45
45
  error_2 = _a.sent();
46
- throw new ShopifyErrors.SessionStorageError("CustomSessionStorage failed to load a session. Error Details: " + error_2);
46
+ throw new ShopifyErrors.SessionStorageError("CustomSessionStorage failed to load a session. Error Details: ".concat(error_2));
47
47
  case 3:
48
48
  if (result) {
49
49
  if (result instanceof session_1.Session) {
@@ -61,7 +61,7 @@ var CustomSessionStorage = /** @class */ (function () {
61
61
  return [2 /*return*/, session];
62
62
  }
63
63
  else {
64
- throw new ShopifyErrors.SessionStorageError("Expected return to be instanceof Session, but received instanceof " + result.constructor.name + ".");
64
+ throw new ShopifyErrors.SessionStorageError("Expected return to be instanceof Session, but received instanceof ".concat(result.constructor.name, "."));
65
65
  }
66
66
  }
67
67
  else {
@@ -83,7 +83,7 @@ var CustomSessionStorage = /** @class */ (function () {
83
83
  case 1: return [2 /*return*/, _a.sent()];
84
84
  case 2:
85
85
  error_3 = _a.sent();
86
- throw new ShopifyErrors.SessionStorageError("CustomSessionStorage failed to delete a session. Error Details: " + error_3);
86
+ throw new ShopifyErrors.SessionStorageError("CustomSessionStorage failed to delete a session. Error Details: ".concat(error_3));
87
87
  case 3: return [2 /*return*/];
88
88
  }
89
89
  });
@@ -0,0 +1,23 @@
1
+ import { SessionInterface } from '../types';
2
+ import { SessionStorage } from '../session_storage';
3
+ export interface MongoDBSessionStorageOptions {
4
+ sessionCollectionName: string;
5
+ }
6
+ export declare class MongoDBSessionStorage implements SessionStorage {
7
+ private dbUrl;
8
+ private dbName;
9
+ static withCredentials(host: string, dbName: string, username: string, password: string, opts: Partial<MongoDBSessionStorageOptions>): MongoDBSessionStorage;
10
+ readonly ready: Promise<void>;
11
+ private options;
12
+ private client;
13
+ constructor(dbUrl: URL, dbName: string, opts?: Partial<MongoDBSessionStorageOptions>);
14
+ storeSession(session: SessionInterface): Promise<boolean>;
15
+ loadSession(id: string): Promise<SessionInterface | undefined>;
16
+ deleteSession(id: string): Promise<boolean>;
17
+ disconnect(): Promise<void>;
18
+ private get collection();
19
+ private init;
20
+ private hasSessionCollection;
21
+ private createCollection;
22
+ }
23
+ //# sourceMappingURL=mongodb.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mongodb.d.ts","sourceRoot":"","sources":["../../../../src/auth/session/storage/mongodb.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,gBAAgB,EAAC,MAAM,UAAU,CAAC;AAC1C,OAAO,EAAC,cAAc,EAAC,MAAM,oBAAoB,CAAC;AAGlD,MAAM,WAAW,4BAA4B;IAC3C,qBAAqB,EAAE,MAAM,CAAC;CAC/B;AAKD,qBAAa,qBAAsB,YAAW,cAAc;IAyBxD,OAAO,CAAC,KAAK;IACb,OAAO,CAAC,MAAM;IAzBhB,MAAM,CAAC,eAAe,CACpB,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,OAAO,CAAC,4BAA4B,CAAC;IAa7C,SAAgB,KAAK,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;IACrC,OAAO,CAAC,OAAO,CAA+B;IAE9C,OAAO,CAAC,MAAM,CAAM;gBAGV,KAAK,EAAE,GAAG,EACV,MAAM,EAAE,MAAM,EACtB,IAAI,GAAE,OAAO,CAAC,4BAA4B,CAAM;IASrC,YAAY,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,OAAO,CAAC;IAazD,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,GAAG,SAAS,CAAC;IAS9D,aAAa,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAM3C,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAIxC,OAAO,KAAK,UAAU,GAIrB;YAEa,IAAI;YAOJ,oBAAoB;YAOpB,gBAAgB;CAM/B"}
@@ -0,0 +1,150 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MongoDBSessionStorage = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var mongodb = tslib_1.__importStar(require("mongodb"));
6
+ var session_utils_1 = require("../session-utils");
7
+ var defaultMongoDBSessionStorageOptions = {
8
+ sessionCollectionName: 'shopify_sessions',
9
+ };
10
+ var MongoDBSessionStorage = /** @class */ (function () {
11
+ function MongoDBSessionStorage(dbUrl, dbName, opts) {
12
+ if (opts === void 0) { opts = {}; }
13
+ this.dbUrl = dbUrl;
14
+ this.dbName = dbName;
15
+ if (typeof this.dbUrl === 'string') {
16
+ this.dbUrl = new URL(this.dbUrl);
17
+ }
18
+ this.options = tslib_1.__assign(tslib_1.__assign({}, defaultMongoDBSessionStorageOptions), opts);
19
+ this.ready = this.init();
20
+ }
21
+ MongoDBSessionStorage.withCredentials = function (host, dbName, username, password, opts) {
22
+ return new MongoDBSessionStorage(new URL("mongodb://".concat(encodeURIComponent(username), ":").concat(encodeURIComponent(password), "@").concat(host, "/")), dbName, opts);
23
+ };
24
+ MongoDBSessionStorage.prototype.storeSession = function (session) {
25
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
26
+ return tslib_1.__generator(this, function (_a) {
27
+ switch (_a.label) {
28
+ case 0: return [4 /*yield*/, this.ready];
29
+ case 1:
30
+ _a.sent();
31
+ return [4 /*yield*/, this.collection.findOneAndReplace({ id: session.id }, { id: session.id, entries: (0, session_utils_1.sessionEntries)(session) }, {
32
+ upsert: true,
33
+ })];
34
+ case 2:
35
+ _a.sent();
36
+ return [2 /*return*/, true];
37
+ }
38
+ });
39
+ });
40
+ };
41
+ MongoDBSessionStorage.prototype.loadSession = function (id) {
42
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
43
+ var rawResult;
44
+ return tslib_1.__generator(this, function (_a) {
45
+ switch (_a.label) {
46
+ case 0: return [4 /*yield*/, this.ready];
47
+ case 1:
48
+ _a.sent();
49
+ return [4 /*yield*/, this.collection.findOne({ id: id })];
50
+ case 2:
51
+ rawResult = _a.sent();
52
+ if (!rawResult)
53
+ return [2 /*return*/, undefined];
54
+ return [2 /*return*/, (0, session_utils_1.sessionFromEntries)(rawResult.entries)];
55
+ }
56
+ });
57
+ });
58
+ };
59
+ MongoDBSessionStorage.prototype.deleteSession = function (id) {
60
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
61
+ return tslib_1.__generator(this, function (_a) {
62
+ switch (_a.label) {
63
+ case 0: return [4 /*yield*/, this.ready];
64
+ case 1:
65
+ _a.sent();
66
+ return [4 /*yield*/, this.collection.deleteOne({ id: id })];
67
+ case 2:
68
+ _a.sent();
69
+ return [2 /*return*/, true];
70
+ }
71
+ });
72
+ });
73
+ };
74
+ MongoDBSessionStorage.prototype.disconnect = function () {
75
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
76
+ return tslib_1.__generator(this, function (_a) {
77
+ switch (_a.label) {
78
+ case 0: return [4 /*yield*/, this.client.close()];
79
+ case 1:
80
+ _a.sent();
81
+ return [2 /*return*/];
82
+ }
83
+ });
84
+ });
85
+ };
86
+ Object.defineProperty(MongoDBSessionStorage.prototype, "collection", {
87
+ get: function () {
88
+ return this.client
89
+ .db(this.dbName)
90
+ .collection(this.options.sessionCollectionName);
91
+ },
92
+ enumerable: false,
93
+ configurable: true
94
+ });
95
+ MongoDBSessionStorage.prototype.init = function () {
96
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
97
+ return tslib_1.__generator(this, function (_a) {
98
+ switch (_a.label) {
99
+ case 0:
100
+ this.client = new mongodb.MongoClient(this.dbUrl.toString());
101
+ return [4 /*yield*/, this.client.connect()];
102
+ case 1:
103
+ _a.sent();
104
+ return [4 /*yield*/, this.client.db().command({ ping: 1 })];
105
+ case 2:
106
+ _a.sent();
107
+ return [4 /*yield*/, this.createCollection()];
108
+ case 3:
109
+ _a.sent();
110
+ return [2 /*return*/];
111
+ }
112
+ });
113
+ });
114
+ };
115
+ MongoDBSessionStorage.prototype.hasSessionCollection = function () {
116
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
117
+ var collections;
118
+ return tslib_1.__generator(this, function (_a) {
119
+ switch (_a.label) {
120
+ case 0: return [4 /*yield*/, this.client.db().collections()];
121
+ case 1:
122
+ collections = _a.sent();
123
+ return [2 /*return*/, collections
124
+ .map(function (collection) { return collection.collectionName; })
125
+ .includes(this.options.sessionCollectionName)];
126
+ }
127
+ });
128
+ });
129
+ };
130
+ MongoDBSessionStorage.prototype.createCollection = function () {
131
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
132
+ var hasSessionCollection;
133
+ return tslib_1.__generator(this, function (_a) {
134
+ switch (_a.label) {
135
+ case 0: return [4 /*yield*/, this.hasSessionCollection()];
136
+ case 1:
137
+ hasSessionCollection = _a.sent();
138
+ if (!!hasSessionCollection) return [3 /*break*/, 3];
139
+ return [4 /*yield*/, this.client.db().collection(this.options.sessionCollectionName)];
140
+ case 2:
141
+ _a.sent();
142
+ _a.label = 3;
143
+ case 3: return [2 /*return*/];
144
+ }
145
+ });
146
+ });
147
+ };
148
+ return MongoDBSessionStorage;
149
+ }());
150
+ exports.MongoDBSessionStorage = MongoDBSessionStorage;
@@ -0,0 +1,22 @@
1
+ import { SessionInterface } from '../types';
2
+ import { SessionStorage } from '../session_storage';
3
+ export interface MySQLSessionStorageOptions {
4
+ sessionTableName: string;
5
+ }
6
+ export declare class MySQLSessionStorage implements SessionStorage {
7
+ private dbUrl;
8
+ static withCredentials(host: string, dbName: string, username: string, password: string, opts: Partial<MySQLSessionStorageOptions>): MySQLSessionStorage;
9
+ readonly ready: Promise<void>;
10
+ private options;
11
+ private connection;
12
+ constructor(dbUrl: URL, opts?: Partial<MySQLSessionStorageOptions>);
13
+ storeSession(session: SessionInterface): Promise<boolean>;
14
+ loadSession(id: string): Promise<SessionInterface | undefined>;
15
+ deleteSession(id: string): Promise<boolean>;
16
+ disconnect(): Promise<void>;
17
+ private init;
18
+ private hasSessionTable;
19
+ private createTable;
20
+ private query;
21
+ }
22
+ //# sourceMappingURL=mysql.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mysql.d.ts","sourceRoot":"","sources":["../../../../src/auth/session/storage/mysql.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,gBAAgB,EAAC,MAAM,UAAU,CAAC;AAC1C,OAAO,EAAC,cAAc,EAAC,MAAM,oBAAoB,CAAC;AAGlD,MAAM,WAAW,0BAA0B;IACzC,gBAAgB,EAAE,MAAM,CAAC;CAC1B;AAKD,qBAAa,mBAAoB,YAAW,cAAc;IAuBtD,OAAO,CAAC,KAAK;IAtBf,MAAM,CAAC,eAAe,CACpB,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,OAAO,CAAC,0BAA0B,CAAC;IAY3C,SAAgB,KAAK,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;IACrC,OAAO,CAAC,OAAO,CAA6B;IAC5C,OAAO,CAAC,UAAU,CAAmB;gBAG3B,KAAK,EAAE,GAAG,EAClB,IAAI,GAAE,OAAO,CAAC,0BAA0B,CAAM;IASnC,YAAY,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,OAAO,CAAC;IAgBzD,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,GAAG,SAAS,CAAC;IAY9D,aAAa,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAU3C,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;YAI1B,IAAI;YAKJ,eAAe;YAQf,WAAW;IAmBzB,OAAO,CAAC,KAAK;CAGd"}