swell-js 4.2.2 → 4.2.3-alpha.0

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/dist/account.mjs CHANGED
@@ -1,9 +1,9 @@
1
- function methods(request) {
1
+ function methods(api) {
2
2
  return {
3
3
  state: null,
4
4
 
5
5
  async requestStateChange(method, url, id, data) {
6
- const result = await request(method, url, id, data);
6
+ const result = await api.request(method, url, id, data);
7
7
  if (result && result.errors) {
8
8
  return result;
9
9
  }
@@ -37,51 +37,51 @@ function methods(request) {
37
37
 
38
38
  logout() {
39
39
  this.state = null;
40
- return request('post', '/account/logout');
40
+ return api.request('post', '/account/logout');
41
41
  },
42
42
 
43
43
  recover(data) {
44
- return request('post', '/account/recover', data);
44
+ return api.request('post', '/account/recover', data);
45
45
  },
46
46
 
47
47
  listAddresses(query) {
48
- return request('get', '/account/addresses', query);
48
+ return api.request('get', '/account/addresses', query);
49
49
  },
50
50
 
51
51
  createAddress(data) {
52
- return request('post', '/account/addresses', data);
52
+ return api.request('post', '/account/addresses', data);
53
53
  },
54
54
 
55
55
  updateAddress(id, data) {
56
- return request('put', `/account/addresses/${id}`, data);
56
+ return api.request('put', `/account/addresses/${id}`, data);
57
57
  },
58
58
 
59
59
  deleteAddress(id) {
60
- return request('delete', `/account/addresses/${id}`);
60
+ return api.request('delete', `/account/addresses/${id}`);
61
61
  },
62
62
 
63
63
  listCards(query) {
64
- return request('get', '/account/cards', query);
64
+ return api.request('get', '/account/cards', query);
65
65
  },
66
66
 
67
67
  createCard(data) {
68
- return request('post', '/account/cards', data);
68
+ return api.request('post', '/account/cards', data);
69
69
  },
70
70
 
71
71
  updateCard(id, data) {
72
- return request('put', `/account/cards/${id}`, data);
72
+ return api.request('put', `/account/cards/${id}`, data);
73
73
  },
74
74
 
75
75
  deleteCard(id) {
76
- return request('delete', `/account/cards/${id}`);
76
+ return api.request('delete', `/account/cards/${id}`);
77
77
  },
78
78
 
79
79
  listOrders(query) {
80
- return request('get', '/account/orders', query);
80
+ return api.request('get', '/account/orders', query);
81
81
  },
82
82
 
83
83
  getOrder(id) {
84
- return request('get', `/account/orders/${id}`);
84
+ return api.request('get', `/account/orders/${id}`);
85
85
  },
86
86
 
87
87
  // Deprecated methods
@@ -90,21 +90,21 @@ function methods(request) {
90
90
  * @deprecated use `listAddresses` instead
91
91
  */
92
92
  getAddresses(query) {
93
- return request('get', '/account/addresses', query);
93
+ return api.request('get', '/account/addresses', query);
94
94
  },
95
95
 
96
96
  /**
97
97
  * @deprecated use `listCards` instead
98
98
  */
99
99
  getCards(query) {
100
- return request('get', '/account/cards', query);
100
+ return api.request('get', '/account/cards', query);
101
101
  },
102
102
 
103
103
  /**
104
104
  * @deprecated use `listOrders` instead
105
105
  */
106
106
  getOrders(query) {
107
- return request('get', '/account/orders', query);
107
+ return api.request('get', '/account/orders', query);
108
108
  },
109
109
  };
110
110
  }
package/dist/api.mjs CHANGED
@@ -7,7 +7,7 @@ import methods$5 from './products.mjs';
7
7
  import methods$6 from './categories.mjs';
8
8
  import methods$7 from './attributes.mjs';
9
9
  import methods$8 from './subscriptions.mjs';
10
- import { d as defaultMethods, y as trimEnd, z as utils, A as trimStart, B as trimBoth, t as toSnake, C as stringifyQuery, D as base64Encode, i as isServer, a as toCamel, E as setOptions } from './index.6497db06.mjs';
10
+ import { d as defaultMethods, y as trimEnd, z as setOptions, A as utils, B as trimStart, C as trimBoth, t as toSnake, D as stringifyQuery, E as base64Encode, i as isServer, a as toCamel } from './index.a911a674.mjs';
11
11
  import methods$9 from './content.mjs';
12
12
  import methods$a from './settings.mjs';
13
13
  import PaymentController from './payment.mjs';
@@ -19,8 +19,8 @@ import './round.577a8441.mjs';
19
19
  import 'deepmerge';
20
20
  import 'fast-case';
21
21
 
22
- function methods$2(request) {
23
- const { get, list } = defaultMethods(request, '/invoices', ['list', 'get']);
22
+ function methods$2(api) {
23
+ const { get, list } = defaultMethods(api, '/invoices', ['list', 'get']);
24
24
  return {
25
25
  get: (id, ...args) => {
26
26
  return cacheApi.getFetch('invoices', id, () => get(id, ...args));
@@ -30,13 +30,13 @@ function methods$2(request) {
30
30
  };
31
31
  }
32
32
 
33
- function methods$1(request, opt) {
33
+ function methods$1(api, opt) {
34
34
  return {
35
35
  /**
36
36
  * Get the decoded session as an object of session values
37
37
  */
38
38
  get() {
39
- return request('get', '/session');
39
+ return api.request('get', '/session');
40
40
  },
41
41
 
42
42
  /**
@@ -57,7 +57,7 @@ function methods$1(request, opt) {
57
57
  };
58
58
  }
59
59
 
60
- function methods(request, _opt) {
60
+ function methods(api, _opt) {
61
61
  return {
62
62
  /**
63
63
  * Make a request to an app function and greceiveet a response
@@ -69,7 +69,7 @@ function methods(request, _opt) {
69
69
  * @returns {any}
70
70
  */
71
71
  request(method, appId, functionName, data, options = undefined) {
72
- return request(method, functionName, undefined, data, {
72
+ return api.request(method, functionName, undefined, data, {
73
73
  ...options,
74
74
  path: `/functions/${appId}`,
75
75
  useCamelCase: false, // avoid mutating data
@@ -126,207 +126,221 @@ function methods(request, _opt) {
126
126
  };
127
127
  }
128
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.2.2',
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
- },
129
+ function swell(initStore = undefined, initKey, initOpt = {}) {
130
+ const options = {
131
+ store: null,
132
+ key: null,
133
+ url: null,
134
+ useCamelCase: null,
135
+ previewContent: null,
136
+ };
185
137
 
186
- cache: cacheApi,
138
+ const api = {};
139
+
140
+ Object.assign(api, {
141
+ version: '4.2.3-alpha.0',
142
+ options,
143
+ request,
144
+
145
+ init(store, key, opt = {}) {
146
+ options.key = key;
147
+ options.store = store;
148
+ options.url = opt.url
149
+ ? trimEnd(opt.url)
150
+ : `https://${store}.swell.store`;
151
+ options.vaultUrl = opt.vaultUrl
152
+ ? trimEnd(opt.vaultUrl)
153
+ : `https://vault.schema.io`;
154
+ options.timeout = (opt.timeout && parseInt(opt.timeout, 10)) || 20000;
155
+ options.useCamelCase = opt.useCamelCase || false;
156
+ options.previewContent = opt.previewContent || false;
157
+ options.session = opt.session;
158
+ options.locale = opt.locale;
159
+ options.currency = opt.currency;
160
+ options.api = api;
161
+ options.getCookie = opt.getCookie || getCookie;
162
+ options.setCookie = opt.setCookie || setCookie;
163
+ options.getCart = opt.getCart;
164
+ options.updateCart = opt.updateCart;
165
+ setOptions(options);
166
+ },
187
167
 
188
- card: cardApi,
168
+ // Backward compatibility
169
+ auth(...args) {
170
+ return this.init(...args);
171
+ },
189
172
 
190
- cart: methods$3(request, options),
173
+ get(url, query) {
174
+ return api.request('get', url, query);
175
+ },
191
176
 
192
- account: methods$4(request),
177
+ put(url, data) {
178
+ return api.request('put', url, data);
179
+ },
193
180
 
194
- products: methods$5(request, options),
181
+ post(url, data) {
182
+ return api.request('post', url, data);
183
+ },
195
184
 
196
- categories: methods$6(request),
185
+ delete(url, data) {
186
+ return api.request('delete', url, data);
187
+ },
197
188
 
198
- attributes: methods$7(request),
189
+ cache: cacheApi,
199
190
 
200
- subscriptions: methods$8(request),
191
+ card: cardApi,
201
192
 
202
- invoices: methods$2(request),
193
+ cart: methods$3(api, options),
203
194
 
204
- content: methods$9(request, options),
195
+ account: methods$4(api),
205
196
 
206
- settings: methods$a(request, options),
197
+ products: methods$5(api, options),
207
198
 
208
- payment: new PaymentController(request, options),
199
+ categories: methods$6(api),
209
200
 
210
- locale: methods$b(request, options),
201
+ attributes: methods$7(api),
211
202
 
212
- currency: methods$c(request, options),
203
+ subscriptions: methods$8(api),
213
204
 
214
- session: methods$1(request, options),
205
+ invoices: methods$2(api),
215
206
 
216
- functions: methods(request),
207
+ content: methods$9(api, options),
217
208
 
218
- utils,
219
- };
209
+ settings: methods$a(api, options),
220
210
 
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
- };
211
+ payment: new PaymentController(api, options),
232
212
 
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';
213
+ locale: methods$b(api, options),
238
214
 
239
- const baseUrl = `${allOptions.url}${allOptions.base || ''}${path}`;
240
- const reqMethod = String(method).toLowerCase();
215
+ currency: methods$c(api, options),
241
216
 
242
- let reqUrl = url;
243
- let reqData = id;
217
+ session: methods$1(api, options),
244
218
 
245
- if (data !== undefined || typeof id === 'string') {
246
- reqUrl = [trimEnd(url), trimStart(id)].join('/');
247
- reqData = data;
248
- }
219
+ functions: methods(api),
249
220
 
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
- }
221
+ utils,
222
+ });
264
223
 
265
- const reqHeaders = {
266
- Accept: 'application/json',
267
- 'Content-Type': 'application/json',
268
- Authorization: `Basic ${base64Encode(String(allOptions.key))}`,
269
- };
224
+ async function request(
225
+ method,
226
+ url,
227
+ id = undefined,
228
+ data = undefined,
229
+ opt = undefined,
230
+ ) {
231
+ const allOptions = {
232
+ ...options,
233
+ ...opt,
234
+ };
235
+
236
+ const session = allOptions.session || allOptions.getCookie('swell-session');
237
+ const locale = allOptions.locale || allOptions.getCookie('swell-locale');
238
+ const currency =
239
+ allOptions.currency || allOptions.getCookie('swell-currency');
240
+ const path = allOptions.path || '/api';
241
+
242
+ const baseUrl = `${allOptions.url}${allOptions.base || ''}${path}`;
243
+ const reqMethod = String(method).toLowerCase();
244
+
245
+ let reqUrl = url;
246
+ let reqData = id;
247
+
248
+ if (data !== undefined || typeof id === 'string') {
249
+ reqUrl = [trimEnd(url), trimStart(id)].join('/');
250
+ reqData = data;
251
+ }
270
252
 
271
- if (session) {
272
- reqHeaders['X-Session'] = session;
273
- }
253
+ reqUrl = allOptions.fullUrl || `${baseUrl}/${trimBoth(reqUrl)}`;
254
+ reqData = allOptions.useCamelCase ? toSnake(reqData) : reqData;
255
+
256
+ let reqBody;
257
+ if (reqMethod === 'get') {
258
+ let exQuery;
259
+ [reqUrl, exQuery] = reqUrl.split('?');
260
+ const fullQuery = [exQuery, stringifyQuery(reqData)]
261
+ .join('&')
262
+ .replace(/^&/, '');
263
+ reqUrl = `${reqUrl}${fullQuery ? `?${fullQuery}` : ''}`;
264
+ } else {
265
+ reqBody = JSON.stringify(reqData);
266
+ }
274
267
 
275
- if (locale) {
276
- reqHeaders['X-Locale'] = locale;
277
- }
268
+ const reqHeaders = {
269
+ Accept: 'application/json',
270
+ 'Content-Type': 'application/json',
271
+ Authorization: `Basic ${base64Encode(String(allOptions.key))}`,
272
+ };
278
273
 
279
- if (currency) {
280
- reqHeaders['X-Currency'] = currency;
281
- }
274
+ if (session) {
275
+ reqHeaders['X-Session'] = session;
276
+ }
282
277
 
283
- const response = await fetch(reqUrl, {
284
- method: reqMethod,
285
- headers: reqHeaders,
286
- body: reqBody,
287
- // Credentials and mode are only available in the browser
288
- ...(!isServer()
289
- ? {
290
- credentials: 'include',
291
- mode: 'cors',
292
- }
293
- : undefined),
294
- });
278
+ if (locale) {
279
+ reqHeaders['X-Locale'] = locale;
280
+ }
295
281
 
296
- const responseSession = response.headers.get('X-Session');
282
+ if (currency) {
283
+ reqHeaders['X-Currency'] = currency;
284
+ }
297
285
 
298
- if (typeof responseSession === 'string' && session !== responseSession) {
299
- allOptions.setCookie('swell-session', responseSession);
300
- }
286
+ const response = await fetch(reqUrl, {
287
+ method: reqMethod,
288
+ headers: reqHeaders,
289
+ body: reqBody,
290
+ // Credentials and mode are only available in the browser
291
+ ...(!isServer()
292
+ ? {
293
+ credentials: 'include',
294
+ mode: 'cors',
295
+ }
296
+ : undefined),
297
+ });
298
+
299
+ const responseSession = response.headers.get('X-Session');
300
+
301
+ if (typeof responseSession === 'string' && session !== responseSession) {
302
+ allOptions.setCookie('swell-session', responseSession);
303
+ }
301
304
 
302
- // Response could be text, json, or empty
303
- let result = null;
304
- try {
305
- result = await response.text();
305
+ // Response could be text, json, or empty
306
+ let result = null;
306
307
  try {
307
- result = JSON.parse(result);
308
+ result = await response.text();
309
+ try {
310
+ result = JSON.parse(result);
311
+ } catch (err) {
312
+ // noop
313
+ }
308
314
  } catch (err) {
309
315
  // noop
310
316
  }
311
- } catch (err) {
312
- // noop
317
+
318
+ if (result && result.error) {
319
+ const err = new Error(result.error.message || result.error);
320
+ err.status = response.status;
321
+ err.code = result.error.code;
322
+ err.param = result.error.param;
323
+ throw err;
324
+ } else if (!response.ok) {
325
+ const err = new Error(
326
+ 'A connection error occurred while making the request',
327
+ );
328
+ err.code = 'connection_error';
329
+ throw err;
330
+ }
331
+
332
+ return allOptions.useCamelCase ? toCamel(result) : result;
313
333
  }
314
334
 
315
- if (result && result.error) {
316
- const err = new Error(result.error.message || result.error);
317
- err.status = response.status;
318
- err.code = result.error.code;
319
- err.param = result.error.param;
320
- throw err;
321
- } else if (!response.ok) {
322
- const err = new Error(
323
- 'A connection error occurred while making the request',
324
- );
325
- err.code = 'connection_error';
326
- throw err;
335
+ if (initStore) {
336
+ api.init(initStore, initKey, initOpt);
327
337
  }
328
338
 
329
- return allOptions.useCamelCase ? toCamel(result) : result;
339
+ return api;
330
340
  }
331
341
 
332
- export { api as default };
342
+ const instance = swell();
343
+
344
+ instance.create = swell;
345
+
346
+ export { instance as default };
@@ -1,4 +1,4 @@
1
- import { d as defaultMethods } from './index.6497db06.mjs';
1
+ import { d as defaultMethods } from './index.a911a674.mjs';
2
2
  import cacheApi from './cache.mjs';
3
3
  import 'qs';
4
4
  import './find.18f1ac6d.mjs';
@@ -6,8 +6,8 @@ import './round.577a8441.mjs';
6
6
  import 'deepmerge';
7
7
  import 'fast-case';
8
8
 
9
- function methods(request) {
10
- const { get, list } = defaultMethods(request, '/attributes', ['list', 'get']);
9
+ function methods(api) {
10
+ const { get, list } = defaultMethods(api, '/attributes', ['list', 'get']);
11
11
 
12
12
  return {
13
13
  get: (id, ...args) => {
package/dist/cache.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { s as set, m as merge, a as toCamel, h as toCamelPath, j as getOptions } from './index.6497db06.mjs';
1
+ import { s as set, m as merge, a as toCamel, h as toCamelPath, j as getOptions } from './index.a911a674.mjs';
2
2
  import { g as get } from './find.18f1ac6d.mjs';
3
3
  import 'qs';
4
4
  import './round.577a8441.mjs';
package/dist/card.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { t as toSnake, v as vaultRequest } from './index.6497db06.mjs';
1
+ import { t as toSnake, v as vaultRequest } from './index.a911a674.mjs';
2
2
  import 'qs';
3
3
  import './find.18f1ac6d.mjs';
4
4
  import './round.577a8441.mjs';
package/dist/cart.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import 'qs';
2
- import { c as cloneDeep } from './index.6497db06.mjs';
2
+ import { c as cloneDeep } from './index.a911a674.mjs';
3
3
  import 'deepmerge';
4
4
  import 'fast-case';
5
5
  import { cleanProductOptions } from './products.mjs';
@@ -8,7 +8,7 @@ import './round.577a8441.mjs';
8
8
  import './cache.mjs';
9
9
  import './attributes.mjs';
10
10
 
11
- function methods(request, options) {
11
+ function methods(api, options) {
12
12
  return {
13
13
  state: null,
14
14
  order: null,
@@ -19,7 +19,7 @@ function methods(request, options) {
19
19
 
20
20
  async requestStateChange(method, url, id, data) {
21
21
  return this.requestStateSync(async () => {
22
- const result = await request(method, url, id, data);
22
+ const result = await api.request(method, url, id, data);
23
23
 
24
24
  if (result && result.errors) {
25
25
  return result;
@@ -176,18 +176,18 @@ function methods(request, options) {
176
176
  async getOrder(checkoutId = undefined) {
177
177
  let result;
178
178
  if (checkoutId) {
179
- result = await request('get', '/cart/order', {
179
+ result = await api.request('get', '/cart/order', {
180
180
  checkout_id: checkoutId,
181
181
  });
182
182
  } else {
183
- result = await request('get', '/cart/order');
183
+ result = await api.request('get', '/cart/order');
184
184
  }
185
185
  this.order = result;
186
186
  return result;
187
187
  },
188
188
 
189
189
  async getSettings() {
190
- this.settings = await request('get', '/cart/settings');
190
+ this.settings = await api.request('get', '/cart/settings');
191
191
  return this.settings;
192
192
  },
193
193
  };
@@ -1,4 +1,4 @@
1
- import { d as defaultMethods } from './index.6497db06.mjs';
1
+ import { d as defaultMethods } from './index.a911a674.mjs';
2
2
  import cacheApi from './cache.mjs';
3
3
  import 'qs';
4
4
  import './find.18f1ac6d.mjs';
@@ -6,8 +6,8 @@ import './round.577a8441.mjs';
6
6
  import 'deepmerge';
7
7
  import 'fast-case';
8
8
 
9
- function methods(request) {
10
- const { get, list } = defaultMethods(request, '/categories', ['list', 'get']);
9
+ function methods(api) {
10
+ const { get, list } = defaultMethods(api, '/categories', ['list', 'get']);
11
11
 
12
12
  return {
13
13
  get: (id, ...args) => {
package/dist/content.mjs CHANGED
@@ -1,23 +1,24 @@
1
1
  import cacheApi from './cache.mjs';
2
- import './index.6497db06.mjs';
2
+ import './index.a911a674.mjs';
3
3
  import 'qs';
4
4
  import './find.18f1ac6d.mjs';
5
5
  import './round.577a8441.mjs';
6
6
  import 'deepmerge';
7
7
  import 'fast-case';
8
8
 
9
- function methods(request, opt) {
9
+ function methods(api, opt) {
10
10
  return {
11
11
  get: (type, id, query) => {
12
12
  return cacheApi.getFetch(`content_${type}`, id, () =>
13
- request('get', `/content/${type}`, id, {
13
+ api.request('get', `/content/${type}`, id, {
14
14
  $preview: opt.previewContent,
15
15
  ...(query || {}),
16
16
  }),
17
17
  );
18
18
  },
19
19
 
20
- list: (type, query) => request('get', `/content/${type}`, undefined, query),
20
+ list: (type, query) =>
21
+ api.request('get', `/content/${type}`, undefined, query),
21
22
  };
22
23
  }
23
24
 
package/dist/cookie.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { i as isServer } from './index.6497db06.mjs';
1
+ import { i as isServer } from './index.a911a674.mjs';
2
2
  import 'qs';
3
3
  import './find.18f1ac6d.mjs';
4
4
  import './round.577a8441.mjs';