swell-js 4.2.2 → 4.2.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.
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,235 @@ 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
+ /**
130
+ * Swell API client
131
+ * @param {string} initStore - Store name
132
+ * @param {string} initKey - API key
133
+ * @param {InitOptions} initOptions - Options
134
+ * @returns {SwellClient} API client
135
+ */
136
+ function swell(initStore = undefined, initKey, initOptions = {}) {
137
+ const options = {
138
+ store: null,
139
+ key: null,
140
+ url: null,
141
+ useCamelCase: null,
142
+ previewContent: null,
143
+ };
185
144
 
186
- cache: cacheApi,
145
+ const api = {};
146
+
147
+ Object.assign(api, {
148
+ version: '4.2.3',
149
+ options,
150
+ request,
151
+
152
+ init(store, key, opt = {}) {
153
+ options.key = key;
154
+ options.store = store;
155
+ options.url = opt.url
156
+ ? trimEnd(opt.url)
157
+ : `https://${store}.swell.store`;
158
+ options.vaultUrl = opt.vaultUrl
159
+ ? trimEnd(opt.vaultUrl)
160
+ : `https://vault.schema.io`;
161
+ options.timeout = (opt.timeout && parseInt(opt.timeout, 10)) || 20000;
162
+ options.useCamelCase = opt.useCamelCase || false;
163
+ options.previewContent = opt.previewContent || false;
164
+ options.session = opt.session;
165
+ options.locale = opt.locale;
166
+ options.currency = opt.currency;
167
+ options.api = api;
168
+ options.getCookie = opt.getCookie || getCookie;
169
+ options.setCookie = opt.setCookie || setCookie;
170
+ options.getCart = opt.getCart;
171
+ options.updateCart = opt.updateCart;
172
+ options.headers = opt.headers || {};
173
+
174
+ setOptions(options);
175
+ },
187
176
 
188
- card: cardApi,
177
+ // Backward compatibility
178
+ auth(...args) {
179
+ return this.init(...args);
180
+ },
189
181
 
190
- cart: methods$3(request, options),
182
+ get(url, query) {
183
+ return api.request('get', url, query);
184
+ },
191
185
 
192
- account: methods$4(request),
186
+ put(url, data) {
187
+ return api.request('put', url, data);
188
+ },
193
189
 
194
- products: methods$5(request, options),
190
+ post(url, data) {
191
+ return api.request('post', url, data);
192
+ },
195
193
 
196
- categories: methods$6(request),
194
+ delete(url, data) {
195
+ return api.request('delete', url, data);
196
+ },
197
197
 
198
- attributes: methods$7(request),
198
+ cache: cacheApi,
199
199
 
200
- subscriptions: methods$8(request),
200
+ card: cardApi,
201
201
 
202
- invoices: methods$2(request),
202
+ cart: methods$3(api, options),
203
203
 
204
- content: methods$9(request, options),
204
+ account: methods$4(api),
205
205
 
206
- settings: methods$a(request, options),
206
+ products: methods$5(api, options),
207
207
 
208
- payment: new PaymentController(request, options),
208
+ categories: methods$6(api),
209
209
 
210
- locale: methods$b(request, options),
210
+ attributes: methods$7(api),
211
211
 
212
- currency: methods$c(request, options),
212
+ subscriptions: methods$8(api),
213
213
 
214
- session: methods$1(request, options),
214
+ invoices: methods$2(api),
215
215
 
216
- functions: methods(request),
216
+ content: methods$9(api, options),
217
217
 
218
- utils,
219
- };
218
+ settings: methods$a(api, options),
220
219
 
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
- };
220
+ payment: new PaymentController(api, options),
232
221
 
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';
222
+ locale: methods$b(api, options),
238
223
 
239
- const baseUrl = `${allOptions.url}${allOptions.base || ''}${path}`;
240
- const reqMethod = String(method).toLowerCase();
224
+ currency: methods$c(api, options),
241
225
 
242
- let reqUrl = url;
243
- let reqData = id;
226
+ session: methods$1(api, options),
244
227
 
245
- if (data !== undefined || typeof id === 'string') {
246
- reqUrl = [trimEnd(url), trimStart(id)].join('/');
247
- reqData = data;
248
- }
228
+ functions: methods(api),
249
229
 
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
- }
230
+ utils,
231
+ });
264
232
 
265
- const reqHeaders = {
266
- Accept: 'application/json',
267
- 'Content-Type': 'application/json',
268
- Authorization: `Basic ${base64Encode(String(allOptions.key))}`,
269
- };
233
+ async function request(
234
+ method,
235
+ url,
236
+ id = undefined,
237
+ data = undefined,
238
+ opt = undefined,
239
+ ) {
240
+ const allOptions = {
241
+ ...options,
242
+ ...opt,
243
+ headers: {
244
+ ...options.headers,
245
+ ...(opt ? opt.headers : undefined),
246
+ },
247
+ };
248
+
249
+ const session = allOptions.session || allOptions.getCookie('swell-session');
250
+ const locale = allOptions.locale || allOptions.getCookie('swell-locale');
251
+ const currency =
252
+ allOptions.currency || allOptions.getCookie('swell-currency');
253
+ const path = allOptions.path || '/api';
254
+
255
+ const baseUrl = `${allOptions.url}${allOptions.base || ''}${path}`;
256
+ const reqMethod = String(method).toLowerCase();
257
+
258
+ let reqUrl = url;
259
+ let reqData = id;
260
+
261
+ if (data !== undefined || typeof id === 'string') {
262
+ reqUrl = [trimEnd(url), trimStart(id)].join('/');
263
+ reqData = data;
264
+ }
270
265
 
271
- if (session) {
272
- reqHeaders['X-Session'] = session;
273
- }
266
+ reqUrl = allOptions.fullUrl || `${baseUrl}/${trimBoth(reqUrl)}`;
267
+ reqData = allOptions.useCamelCase ? toSnake(reqData) : reqData;
268
+
269
+ let reqBody;
270
+ if (reqMethod === 'get') {
271
+ let exQuery;
272
+ [reqUrl, exQuery] = reqUrl.split('?');
273
+ const fullQuery = [exQuery, stringifyQuery(reqData)]
274
+ .join('&')
275
+ .replace(/^&/, '');
276
+ reqUrl = `${reqUrl}${fullQuery ? `?${fullQuery}` : ''}`;
277
+ } else {
278
+ reqBody = JSON.stringify(reqData);
279
+ }
274
280
 
275
- if (locale) {
276
- reqHeaders['X-Locale'] = locale;
277
- }
281
+ const reqHeaders = {
282
+ ...(allOptions.headers || undefined),
283
+ Accept: 'application/json',
284
+ 'Content-Type': 'application/json',
285
+ Authorization: `Basic ${base64Encode(String(allOptions.key))}`,
286
+ };
278
287
 
279
- if (currency) {
280
- reqHeaders['X-Currency'] = currency;
281
- }
288
+ if (session) {
289
+ reqHeaders['X-Session'] = session;
290
+ }
282
291
 
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
- });
292
+ if (locale) {
293
+ reqHeaders['X-Locale'] = locale;
294
+ }
295
295
 
296
- const responseSession = response.headers.get('X-Session');
296
+ if (currency) {
297
+ reqHeaders['X-Currency'] = currency;
298
+ }
297
299
 
298
- if (typeof responseSession === 'string' && session !== responseSession) {
299
- allOptions.setCookie('swell-session', responseSession);
300
- }
300
+ const response = await fetch(reqUrl, {
301
+ method: reqMethod,
302
+ headers: reqHeaders,
303
+ body: reqBody,
304
+ // Credentials and mode are only available in the browser
305
+ ...(!isServer()
306
+ ? {
307
+ credentials: 'include',
308
+ mode: 'cors',
309
+ }
310
+ : undefined),
311
+ });
312
+
313
+ const responseSession = response.headers.get('X-Session');
314
+
315
+ if (typeof responseSession === 'string' && session !== responseSession) {
316
+ allOptions.setCookie('swell-session', responseSession);
317
+ }
301
318
 
302
- // Response could be text, json, or empty
303
- let result = null;
304
- try {
305
- result = await response.text();
319
+ // Response could be text, json, or empty
320
+ let result = null;
306
321
  try {
307
- result = JSON.parse(result);
322
+ result = await response.text();
323
+ try {
324
+ result = JSON.parse(result);
325
+ } catch (err) {
326
+ // noop
327
+ }
308
328
  } catch (err) {
309
329
  // noop
310
330
  }
311
- } catch (err) {
312
- // noop
331
+
332
+ if (result && result.error) {
333
+ const err = new Error(result.error.message || result.error);
334
+ err.status = response.status;
335
+ err.code = result.error.code;
336
+ err.param = result.error.param;
337
+ throw err;
338
+ } else if (!response.ok) {
339
+ const err = new Error(
340
+ 'A connection error occurred while making the request',
341
+ );
342
+ err.code = 'connection_error';
343
+ throw err;
344
+ }
345
+
346
+ return allOptions.useCamelCase ? toCamel(result) : result;
313
347
  }
314
348
 
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;
349
+ if (initStore) {
350
+ api.init(initStore, initKey, initOptions);
327
351
  }
328
352
 
329
- return allOptions.useCamelCase ? toCamel(result) : result;
353
+ return api;
330
354
  }
331
355
 
332
- export { api as default };
356
+ const instance = swell();
357
+
358
+ instance.create = swell;
359
+
360
+ 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';