swell-js 4.0.0 → 4.0.1

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 (43) hide show
  1. package/dist/account.mjs +100 -1
  2. package/dist/api.mjs +324 -18
  3. package/dist/attributes.mjs +18 -5
  4. package/dist/cache.mjs +167 -4
  5. package/dist/card.mjs +143 -4
  6. package/dist/cart.mjs +190 -7
  7. package/dist/categories.mjs +18 -5
  8. package/dist/content.mjs +21 -5
  9. package/dist/cookie.mjs +52 -4
  10. package/dist/currency.mjs +194 -3
  11. package/dist/{_baseClone.c22e5907.mjs → find.fd7de00e.mjs} +236 -1319
  12. package/dist/index.78b6af02.mjs +1796 -0
  13. package/dist/index.mjs +18 -18
  14. package/dist/locale.mjs +47 -2
  15. package/dist/payment.mjs +3486 -9
  16. package/dist/products.mjs +420 -6
  17. package/dist/round.a606b844.mjs +57 -0
  18. package/dist/settings.mjs +296 -4
  19. package/dist/subscriptions.mjs +72 -7
  20. package/dist/swell.cjs +222 -131
  21. package/dist/swell.cjs.map +1 -1
  22. package/dist/swell.umd.min.js +560 -3027
  23. package/dist/swell.umd.min.js.map +1 -1
  24. package/dist/utils/index.mjs +3 -3
  25. package/package.json +31 -23
  26. package/types/index.d.ts +1 -0
  27. package/dist/account.d991b136.mjs +0 -100
  28. package/dist/api.1e92f86d.mjs +0 -322
  29. package/dist/attributes.b332bfad.mjs +0 -16
  30. package/dist/cache.70033487.mjs +0 -166
  31. package/dist/card.982ad378.mjs +0 -141
  32. package/dist/cart.dd00d6d6.mjs +0 -190
  33. package/dist/categories.f6027058.mjs +0 -16
  34. package/dist/content.5b4bea62.mjs +0 -18
  35. package/dist/cookie.40ff798c.mjs +0 -50
  36. package/dist/currency.e2cd8985.mjs +0 -197
  37. package/dist/index.a60f77a7.mjs +0 -3452
  38. package/dist/index.b29eadc6.mjs +0 -682
  39. package/dist/locale.9d7241ac.mjs +0 -50
  40. package/dist/products.c20a4350.mjs +0 -419
  41. package/dist/round.10a34c5f.mjs +0 -26
  42. package/dist/settings.6c0d2563.mjs +0 -295
  43. package/dist/subscriptions.fd44cccc.mjs +0 -69
package/dist/account.mjs CHANGED
@@ -1 +1,100 @@
1
- export { m as default } from './account.d991b136.mjs';
1
+ function methods(request) {
2
+ return {
3
+ state: null,
4
+
5
+ async requestStateChange(method, url, id, data) {
6
+ const result = await request(method, url, id, data);
7
+ if (result && result.errors) {
8
+ return result;
9
+ }
10
+ return (this.state = result);
11
+ },
12
+
13
+ get(query) {
14
+ return this.requestStateChange('get', '/account', query);
15
+ },
16
+
17
+ create(data) {
18
+ return this.requestStateChange('post', '/account', data);
19
+ },
20
+
21
+ update(data) {
22
+ return this.requestStateChange('put', '/account', data);
23
+ },
24
+
25
+ login(email, password) {
26
+ if (password && password.password_token) {
27
+ return this.requestStateChange('post', '/account/login', {
28
+ email,
29
+ password_token: password.password_token,
30
+ });
31
+ }
32
+ return this.requestStateChange('post', '/account/login', {
33
+ email,
34
+ password,
35
+ });
36
+ },
37
+
38
+ logout() {
39
+ this.state = null;
40
+ return request('post', '/account/logout');
41
+ },
42
+
43
+ recover(data) {
44
+ return request('post', '/account/recover', data);
45
+ },
46
+
47
+ listAddresses(query) {
48
+ return request('get', '/account/addresses', query);
49
+ },
50
+
51
+ createAddress(data) {
52
+ return request('post', '/account/addresses', data);
53
+ },
54
+
55
+ updateAddress(id, data) {
56
+ return request('put', `/account/addresses/${id}`, data);
57
+ },
58
+
59
+ deleteAddress(id) {
60
+ return request('delete', `/account/addresses/${id}`);
61
+ },
62
+
63
+ listCards(query) {
64
+ return request('get', '/account/cards', query);
65
+ },
66
+
67
+ createCard(data) {
68
+ return request('post', '/account/cards', data);
69
+ },
70
+
71
+ updateCard(id, data) {
72
+ return request('put', `/account/cards/${id}`, data);
73
+ },
74
+
75
+ deleteCard(id) {
76
+ return request('delete', `/account/cards/${id}`);
77
+ },
78
+
79
+ listOrders(query) {
80
+ return request('get', `/account/orders`, query);
81
+ },
82
+
83
+ getOrder(id) {
84
+ return request('get', `/account/orders/${id}`);
85
+ },
86
+
87
+ // Deprecated methods
88
+ getAddresses(query) {
89
+ return request('get', '/account/addresses', query);
90
+ },
91
+ getCards(query) {
92
+ return request('get', '/account/cards', query);
93
+ },
94
+ getOrders(query) {
95
+ return request('get', `/account/orders`, query);
96
+ },
97
+ };
98
+ }
99
+
100
+ export { methods as default };
package/dist/api.mjs CHANGED
@@ -1,21 +1,327 @@
1
- export { a as default } from './api.1e92f86d.mjs';
2
- import './card.982ad378.mjs';
3
- import './index.b29eadc6.mjs';
1
+ import cardApi from './card.mjs';
2
+ import { getCookie, setCookie } from './cookie.mjs';
3
+ import cacheApi from './cache.mjs';
4
+ import methods$3 from './cart.mjs';
5
+ import methods$4 from './account.mjs';
6
+ import methods$5 from './products.mjs';
7
+ import methods$6 from './categories.mjs';
8
+ import methods$7 from './attributes.mjs';
9
+ import methods$8 from './subscriptions.mjs';
10
+ import { d as defaultMethods, z as trimEnd, A as utils, B as trimStart, C as trimBoth, t as toSnake, D as stringifyQuery, E as base64Encode, a as toCamel, F as setOptions } from './index.78b6af02.mjs';
11
+ import methods$9 from './content.mjs';
12
+ import methods$a from './settings.mjs';
13
+ import PaymentController from './payment.mjs';
14
+ import methods$b from './locale.mjs';
15
+ import methods$c from './currency.mjs';
4
16
  import 'qs';
5
- import './_baseClone.c22e5907.mjs';
6
- import './round.10a34c5f.mjs';
17
+ import './find.fd7de00e.mjs';
18
+ import './round.a606b844.mjs';
7
19
  import 'deepmerge';
8
20
  import 'fast-case';
9
- import './cookie.40ff798c.mjs';
10
- import './cache.70033487.mjs';
11
- import './cart.dd00d6d6.mjs';
12
- import './products.c20a4350.mjs';
13
- import './attributes.b332bfad.mjs';
14
- import './account.d991b136.mjs';
15
- import './categories.f6027058.mjs';
16
- import './subscriptions.fd44cccc.mjs';
17
- import './content.5b4bea62.mjs';
18
- import './settings.6c0d2563.mjs';
19
- import './index.a60f77a7.mjs';
20
- import './locale.9d7241ac.mjs';
21
- import './currency.e2cd8985.mjs';
21
+
22
+ function methods$2(request) {
23
+ const { get, list } = defaultMethods(request, '/invoices', ['list', 'get']);
24
+ return {
25
+ get: (id, ...args) => {
26
+ return cacheApi.getFetch('invoices', id, () => get(id, ...args));
27
+ },
28
+
29
+ list,
30
+ };
31
+ }
32
+
33
+ function methods$1(request, opt) {
34
+ return {
35
+ /**
36
+ * Get the decoded session as an object of session values
37
+ */
38
+ get() {
39
+ return request('get', '/session');
40
+ },
41
+
42
+ /**
43
+ * Get the encoded session cookie
44
+ * This simplifies storing or passing the session to another system
45
+ */
46
+ getCookie() {
47
+ return opt.getCookie('swell-session');
48
+ },
49
+
50
+ /**
51
+ * Set the encoded session cookie
52
+ * This simplifies restoring the session from another system
53
+ */
54
+ setCookie(value) {
55
+ opt.setCookie('swell-session', value);
56
+ },
57
+ };
58
+ }
59
+
60
+ function methods(request, _opt) {
61
+ return {
62
+ /**
63
+ * Make a request to an app function and greceiveet a response
64
+ * @param {string} method
65
+ * @param {string} appId
66
+ * @param {string} functionName
67
+ * @param {any} data
68
+ * @param {object?} options
69
+ * @returns {any}
70
+ */
71
+ request(method, appId, functionName, data, options = undefined) {
72
+ return request(method, functionName, undefined, data, {
73
+ ...options,
74
+ path: `/functions/${appId}`,
75
+ useCamelCase: false, // avoid mutating data
76
+ });
77
+ },
78
+
79
+ /**
80
+ * Helper to make a GET request to an app function and receive a response
81
+ * @param {string} appId
82
+ * @param {string} functionName
83
+ * @param {any} data
84
+ * @param {object?} options
85
+ * @returns {any}
86
+ */
87
+ get(appId, functionName, data, options = undefined) {
88
+ return this.request('get', appId, functionName, data, options);
89
+ },
90
+
91
+ /**
92
+ * Helper to make a PUT request to an app function and receive a response
93
+ * @param {string} appId
94
+ * @param {string} functionName
95
+ * @param {any} data
96
+ * @param {object?} options
97
+ * @returns {any}
98
+ */
99
+ put(appId, functionName, data, options = undefined) {
100
+ return this.request('put', appId, functionName, data, options);
101
+ },
102
+
103
+ /**
104
+ * Helper to make a POST request to an app function and receive a response
105
+ * @param {string} appId
106
+ * @param {string} functionName
107
+ * @param {any} data
108
+ * @param {object?} options
109
+ * @returns {any}
110
+ */
111
+ post(appId, functionName, data, options = undefined) {
112
+ return this.request('post', appId, functionName, data, options);
113
+ },
114
+
115
+ /**
116
+ * Helper to make a DELETE request to an app function and receive a response
117
+ * @param {string} appId
118
+ * @param {string} functionName
119
+ * @param {any} data
120
+ * @param {object?} options
121
+ * @returns {any}
122
+ */
123
+ delete(appId, functionName, data, options = undefined) {
124
+ return this.request('delete', appId, functionName, data, options);
125
+ },
126
+ };
127
+ }
128
+
129
+ const options = {
130
+ store: null,
131
+ key: null,
132
+ url: null,
133
+ useCamelCase: null,
134
+ previewContent: null,
135
+ };
136
+
137
+ const api = {
138
+ version: '4.0.1',
139
+ options,
140
+ request,
141
+
142
+ init(store, key, opt = {}) {
143
+ options.key = key;
144
+ options.store = store;
145
+ options.url = opt.url
146
+ ? trimEnd(opt.url)
147
+ : `https://${store}.swell.store`;
148
+ options.vaultUrl = opt.vaultUrl
149
+ ? trimEnd(opt.vaultUrl)
150
+ : `https://vault.schema.io`;
151
+ options.timeout = (opt.timeout && parseInt(opt.timeout, 10)) || 20000;
152
+ options.useCamelCase = opt.useCamelCase || false;
153
+ options.previewContent = opt.previewContent || false;
154
+ options.session = opt.session;
155
+ options.locale = opt.locale;
156
+ options.currency = opt.currency;
157
+ options.api = api;
158
+ options.getCookie = opt.getCookie || getCookie;
159
+ options.setCookie = opt.setCookie || setCookie;
160
+ options.getCart = opt.getCart;
161
+ options.updateCart = opt.updateCart;
162
+ setOptions(options);
163
+ },
164
+
165
+ // Backward compatibility
166
+ auth(...args) {
167
+ return this.init(...args);
168
+ },
169
+
170
+ get(url, query) {
171
+ return request('get', url, query);
172
+ },
173
+
174
+ put(url, data) {
175
+ return request('put', url, data);
176
+ },
177
+
178
+ post(url, data) {
179
+ return request('post', url, data);
180
+ },
181
+
182
+ delete(url, data) {
183
+ return request('delete', url, data);
184
+ },
185
+
186
+ cache: cacheApi,
187
+
188
+ card: cardApi,
189
+
190
+ cart: methods$3(request, options),
191
+
192
+ account: methods$4(request),
193
+
194
+ products: methods$5(request, options),
195
+
196
+ categories: methods$6(request),
197
+
198
+ attributes: methods$7(request),
199
+
200
+ subscriptions: methods$8(request),
201
+
202
+ invoices: methods$2(request),
203
+
204
+ content: methods$9(request, options),
205
+
206
+ settings: methods$a(request, options),
207
+
208
+ payment: new PaymentController(request, options),
209
+
210
+ locale: methods$b(request, options),
211
+
212
+ currency: methods$c(request, options),
213
+
214
+ session: methods$1(request, options),
215
+
216
+ functions: methods(request),
217
+
218
+ utils,
219
+ };
220
+
221
+ async function request(
222
+ method,
223
+ url,
224
+ id = undefined,
225
+ data = undefined,
226
+ opt = undefined,
227
+ ) {
228
+ const allOptions = {
229
+ ...options,
230
+ ...opt,
231
+ };
232
+
233
+ const session = allOptions.session || allOptions.getCookie('swell-session');
234
+ const locale = allOptions.locale || allOptions.getCookie('swell-locale');
235
+ const currency =
236
+ allOptions.currency || allOptions.getCookie('swell-currency');
237
+ const path = allOptions.path || '/api';
238
+
239
+ const baseUrl = `${allOptions.url}${allOptions.base || ''}${path}`;
240
+ const reqMethod = String(method).toLowerCase();
241
+
242
+ let reqUrl = url;
243
+ let reqData = id;
244
+
245
+ if (data !== undefined || typeof id === 'string') {
246
+ reqUrl = [trimEnd(url), trimStart(id)].join('/');
247
+ reqData = data;
248
+ }
249
+
250
+ reqUrl = allOptions.fullUrl || `${baseUrl}/${trimBoth(reqUrl)}`;
251
+ reqData = allOptions.useCamelCase ? toSnake(reqData) : reqData;
252
+
253
+ let reqBody;
254
+ if (reqMethod === 'get') {
255
+ let exQuery;
256
+ [reqUrl, exQuery] = reqUrl.split('?');
257
+ const fullQuery = [exQuery, stringifyQuery(reqData)]
258
+ .join('&')
259
+ .replace(/^&/, '');
260
+ reqUrl = `${reqUrl}${fullQuery ? `?${fullQuery}` : ''}`;
261
+ } else {
262
+ reqBody = JSON.stringify(reqData);
263
+ }
264
+
265
+ const reqHeaders = {
266
+ Accept: 'application/json',
267
+ 'Content-Type': 'application/json',
268
+ Authorization: `Basic ${base64Encode(String(allOptions.key))}`,
269
+ };
270
+
271
+ if (session) {
272
+ reqHeaders['X-Session'] = session;
273
+ }
274
+
275
+ if (locale) {
276
+ reqHeaders['X-Locale'] = locale;
277
+ }
278
+
279
+ if (currency) {
280
+ reqHeaders['X-Currency'] = currency;
281
+ }
282
+
283
+ const response = await fetch(reqUrl, {
284
+ method: reqMethod,
285
+ headers: reqHeaders,
286
+ body: reqBody,
287
+ credentials: 'include',
288
+ mode: 'cors',
289
+ });
290
+
291
+ const responseSession = response.headers.get('X-Session');
292
+
293
+ if (typeof responseSession === 'string' && session !== responseSession) {
294
+ allOptions.setCookie('swell-session', responseSession);
295
+ }
296
+
297
+ // Response could be text, json, or empty
298
+ let result = null;
299
+ try {
300
+ result = await response.text();
301
+ try {
302
+ result = JSON.parse(result);
303
+ } catch (err) {
304
+ // noop
305
+ }
306
+ } catch (err) {
307
+ // noop
308
+ }
309
+
310
+ if (result && result.error) {
311
+ const err = new Error(result.error.message || result.error);
312
+ err.status = response.status;
313
+ err.code = result.error.code;
314
+ err.param = result.error.param;
315
+ throw err;
316
+ } else if (!response.ok) {
317
+ const err = new Error(
318
+ 'A connection error occurred while making the request',
319
+ );
320
+ err.code = 'connection_error';
321
+ throw err;
322
+ }
323
+
324
+ return allOptions.useCamelCase ? toCamel(result) : result;
325
+ }
326
+
327
+ export { api as default };
@@ -1,8 +1,21 @@
1
- export { m as default } from './attributes.b332bfad.mjs';
2
- import './index.b29eadc6.mjs';
1
+ import { d as defaultMethods } from './index.78b6af02.mjs';
2
+ import cacheApi from './cache.mjs';
3
3
  import 'qs';
4
- import './_baseClone.c22e5907.mjs';
5
- import './round.10a34c5f.mjs';
4
+ import './find.fd7de00e.mjs';
5
+ import './round.a606b844.mjs';
6
6
  import 'deepmerge';
7
7
  import 'fast-case';
8
- import './cache.70033487.mjs';
8
+
9
+ function methods(request) {
10
+ const { get, list } = defaultMethods(request, '/attributes', ['list', 'get']);
11
+
12
+ return {
13
+ get: (id, ...args) => {
14
+ return cacheApi.getFetch('attributes', id, () => get(id, ...args));
15
+ },
16
+
17
+ list,
18
+ };
19
+ }
20
+
21
+ export { methods as default };
package/dist/cache.mjs CHANGED
@@ -1,7 +1,170 @@
1
- export { c as default } from './cache.70033487.mjs';
2
- import './index.b29eadc6.mjs';
1
+ import { s as set, m as merge, a as toCamel, h as toCamelPath, j as getOptions } from './index.78b6af02.mjs';
2
+ import { g as get } from './find.fd7de00e.mjs';
3
3
  import 'qs';
4
- import './_baseClone.c22e5907.mjs';
5
- import './round.10a34c5f.mjs';
4
+ import './round.a606b844.mjs';
6
5
  import 'deepmerge';
7
6
  import 'fast-case';
7
+
8
+ const RECORD_TIMEOUT = 5000;
9
+
10
+ let VALUES = {
11
+ /*
12
+ [model]: {
13
+ [id]: {
14
+ data,
15
+ record,
16
+ recordTimer,
17
+ presets,
18
+ }
19
+ }
20
+ */
21
+ };
22
+
23
+ const cacheApi = {
24
+ options: {
25
+ enabled: true,
26
+ debug: false,
27
+ },
28
+
29
+ debug(...args) {
30
+ if (this.options.debug) {
31
+ console.log(...args);
32
+ }
33
+ },
34
+
35
+ values({ model, id }, setValues = undefined) {
36
+ this.debug('cache.values', ...arguments);
37
+ if (setValues !== undefined) {
38
+ for (let key in setValues) {
39
+ set(VALUES, `${model}.${id}.${key}`, setValues[key]);
40
+ }
41
+ return;
42
+ }
43
+ return get(VALUES, `${model}.${id}`, {});
44
+ },
45
+
46
+ preset(details) {
47
+ this.debug('cache.preset', ...arguments);
48
+ const { presets = [] } = this.values(details);
49
+ presets.push(details);
50
+ this.values(details, { presets });
51
+ },
52
+
53
+ set(details) {
54
+ this.debug('cache.set', ...arguments);
55
+ let { model, id, path, value } = details;
56
+ let { data = {}, record, presets } = this.values(details);
57
+
58
+ if (id === null) {
59
+ return;
60
+ }
61
+
62
+ if (record === undefined) {
63
+ return this.preset(details);
64
+ }
65
+
66
+ data = merge(record || {}, data);
67
+
68
+ const { useCamelCase } = getOptions();
69
+ if (useCamelCase && value && typeof value === 'object') {
70
+ value = toCamel(value);
71
+ }
72
+
73
+ if (path || value instanceof Array) {
74
+ let upData = { ...(data || {}) };
75
+ let upPath = useCamelCase ? toCamelPath(path) : path;
76
+ set(upData, upPath || '', value);
77
+ data = upData;
78
+ } else if (value && typeof value === 'object') {
79
+ data = data || {};
80
+ data = merge(data, value);
81
+ } else {
82
+ data = value;
83
+ }
84
+
85
+ this.values(details, { data });
86
+
87
+ try {
88
+ // Make sure values have clean refs
89
+ const cache = VALUES[model][id];
90
+ if (cache !== undefined) {
91
+ if (cache.data !== undefined) {
92
+ cache.data = JSON.parse(JSON.stringify(cache.data));
93
+ }
94
+ if (cache.record !== undefined) {
95
+ cache.record = JSON.parse(JSON.stringify(cache.record));
96
+ }
97
+ }
98
+ } catch (err) {
99
+ // noop
100
+ }
101
+ },
102
+
103
+ get(model, id) {
104
+ this.debug('cache.get', ...arguments);
105
+ const { data, recordTimer } = this.values({ model, id });
106
+ this.debug('cache.get:data+recordTimer', ...arguments);
107
+ if (recordTimer) {
108
+ return data;
109
+ }
110
+ },
111
+
112
+ setRecord(record, details) {
113
+ this.debug('cache.setRecord', ...arguments);
114
+ let { recordTimer, presets } = this.values(details);
115
+
116
+ if (recordTimer) {
117
+ clearTimeout(recordTimer);
118
+ }
119
+
120
+ recordTimer = setTimeout(() => {
121
+ this.values(details, { record: undefined, recordTimer: undefined });
122
+ }, RECORD_TIMEOUT);
123
+
124
+ // Record has to be null at minimum, not undefined
125
+ this.values(details, {
126
+ record: record !== undefined ? record : null,
127
+ recordTimer,
128
+ });
129
+
130
+ if (presets) {
131
+ for (let preset of presets) {
132
+ this.set(preset);
133
+ }
134
+ this.values(details, { presets: undefined });
135
+ }
136
+
137
+ const result = this.get(details.model, details.id);
138
+
139
+ return result !== undefined ? result : record;
140
+ },
141
+
142
+ async getFetch(model, id, fetch) {
143
+ if (this.options.enabled) {
144
+ this.debug('cache.getFetch', ...arguments);
145
+ const value = this.get(model, id);
146
+
147
+ if (value !== undefined) {
148
+ return value;
149
+ }
150
+ }
151
+
152
+ const record = await fetch();
153
+ return this.setRecord(record, { model, id });
154
+ },
155
+
156
+ clear(model = undefined, id = undefined) {
157
+ this.debug('cache.clear', ...arguments);
158
+ if (model) {
159
+ if (id) {
160
+ set(VALUES, `${model}.${id}`, undefined);
161
+ } else {
162
+ set(VALUES, model, undefined);
163
+ }
164
+ } else {
165
+ VALUES = {};
166
+ }
167
+ },
168
+ };
169
+
170
+ export { cacheApi as default };