swell-js 3.21.9 → 3.22.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.
@@ -0,0 +1,207 @@
1
+ import { c as cardApi } from './card-4257bc68.js';
2
+ import { g as getCookie, s as setCookie } from './cookie-4cde18fb.js';
3
+ import { c as cacheApi } from './cache-b92f4460.js';
4
+ import { m as methods$1 } from './cart-35e7bdb4.js';
5
+ import { m as methods$2 } from './account-328cc590.js';
6
+ import { m as methods$3 } from './products-d194c3c6.js';
7
+ import { m as methods$4 } from './categories-b1d04223.js';
8
+ import { m as methods$5 } from './attributes-bfef7db7.js';
9
+ import { m as methods$6 } from './subscriptions-f077cac6.js';
10
+ import { d as defaultMethods, n as trimEnd, K as utils, l as trimStart, k as trimBoth, j as toSnake, o as stringifyQuery, E as base64Encode, t as toCamel, e as setOptions } from './index-ca9cb73c.js';
11
+ import { m as methods$7 } from './content-c366a9f8.js';
12
+ import { m as methods$8 } from './settings-34395c72.js';
13
+ import { P as PaymentController } from './index-ae8e1259.js';
14
+ import { m as methods$9 } from './locale-62622809.js';
15
+ import { m as methods$a } from './currency-004374be.js';
16
+
17
+ function methods(request) {
18
+ const { get, list } = defaultMethods(request, '/invoices', ['list', 'get']);
19
+ return {
20
+ get: (id, ...args) => {
21
+ return cacheApi.getFetch('invoices', id, () => get(id, ...args));
22
+ },
23
+
24
+ list,
25
+ };
26
+ }
27
+
28
+ const options = {
29
+ store: null,
30
+ key: null,
31
+ url: null,
32
+ useCamelCase: null,
33
+ previewContent: null,
34
+ };
35
+
36
+ const api = {
37
+ version: '3.22.0',
38
+ options,
39
+ request,
40
+
41
+ init(store, key, opt = {}) {
42
+ options.key = key;
43
+ options.store = store;
44
+ options.url = opt.url
45
+ ? trimEnd(opt.url)
46
+ : `https://${store}.swell.store`;
47
+ options.vaultUrl = opt.vaultUrl
48
+ ? trimEnd(opt.vaultUrl)
49
+ : `https://vault.schema.io`;
50
+ options.timeout = (opt.timeout && parseInt(opt.timeout, 10)) || 20000;
51
+ options.useCamelCase = opt.useCamelCase || false;
52
+ options.previewContent = opt.previewContent || false;
53
+ options.session = opt.session;
54
+ options.locale = opt.locale;
55
+ options.currency = opt.currency;
56
+ options.api = api;
57
+ options.getCart = opt.getCart;
58
+ options.updateCart = opt.updateCart;
59
+ setOptions(options);
60
+ },
61
+
62
+ // Backward compatibility
63
+ auth(...args) {
64
+ return this.init(...args);
65
+ },
66
+
67
+ get(url, query) {
68
+ return request('get', url, query);
69
+ },
70
+
71
+ put(url, data) {
72
+ return request('put', url, data);
73
+ },
74
+
75
+ post(url, data) {
76
+ return request('post', url, data);
77
+ },
78
+
79
+ delete(url, data) {
80
+ return request('delete', url, data);
81
+ },
82
+
83
+ cache: cacheApi,
84
+
85
+ card: cardApi,
86
+
87
+ cart: methods$1(request, options),
88
+
89
+ account: methods$2(request),
90
+
91
+ products: methods$3(request, options),
92
+
93
+ categories: methods$4(request),
94
+
95
+ attributes: methods$5(request),
96
+
97
+ subscriptions: methods$6(request),
98
+
99
+ invoices: methods(request),
100
+
101
+ content: methods$7(request, options),
102
+
103
+ settings: methods$8(request, options),
104
+
105
+ payment: new PaymentController(request, options),
106
+
107
+ locale: methods$9(request, options),
108
+
109
+ currency: methods$a(request, options),
110
+
111
+ utils,
112
+ };
113
+
114
+ async function request(
115
+ method,
116
+ url,
117
+ id = undefined,
118
+ data = undefined,
119
+ opt = undefined,
120
+ ) {
121
+ const allOptions = {
122
+ ...options,
123
+ ...opt,
124
+ };
125
+
126
+ const session = allOptions.session || getCookie('swell-session');
127
+ const locale = allOptions.locale || getCookie('swell-locale');
128
+ const currency = allOptions.currency || getCookie('swell-currency');
129
+
130
+ const baseUrl = `${allOptions.url}${allOptions.base || ''}/api`;
131
+ const reqMethod = String(method).toLowerCase();
132
+
133
+ let reqUrl = url;
134
+ let reqData = id;
135
+
136
+ if (data !== undefined || typeof id === 'string') {
137
+ reqUrl = [trimEnd(url), trimStart(id)].join('/');
138
+ reqData = data;
139
+ }
140
+
141
+ reqUrl = allOptions.fullUrl || `${baseUrl}/${trimBoth(reqUrl)}`;
142
+ reqData = allOptions.useCamelCase ? toSnake(reqData) : reqData;
143
+
144
+ let reqBody;
145
+ if (reqMethod === 'get') {
146
+ let exQuery;
147
+ [reqUrl, exQuery] = reqUrl.split('?');
148
+ const fullQuery = [exQuery, stringifyQuery(reqData)]
149
+ .join('&')
150
+ .replace(/^&/, '');
151
+ reqUrl = `${reqUrl}${fullQuery ? `?${fullQuery}` : ''}`;
152
+ } else {
153
+ reqBody = JSON.stringify(reqData);
154
+ }
155
+
156
+ const reqHeaders = {
157
+ Accept: 'application/json',
158
+ 'Content-Type': 'application/json',
159
+ Authorization: `Basic ${base64Encode(String(allOptions.key))}`,
160
+ };
161
+
162
+ if (session) {
163
+ reqHeaders['X-Session'] = session;
164
+ }
165
+
166
+ if (locale) {
167
+ reqHeaders['X-Locale'] = locale;
168
+ }
169
+
170
+ if (currency) {
171
+ reqHeaders['X-Currency'] = currency;
172
+ }
173
+
174
+ const response = await fetch(reqUrl, {
175
+ method: reqMethod,
176
+ headers: reqHeaders,
177
+ body: reqBody,
178
+ credentials: 'include',
179
+ mode: 'cors',
180
+ });
181
+
182
+ const responseSession = response.headers.get('X-Session');
183
+
184
+ if (typeof responseSession === 'string' && session !== responseSession) {
185
+ setCookie('swell-session', responseSession);
186
+ }
187
+
188
+ const result = await response.json();
189
+
190
+ if (result && result.error) {
191
+ const err = new Error(result.error.message);
192
+ err.status = response.status;
193
+ err.code = result.error.code;
194
+ err.param = result.error.param;
195
+ throw err;
196
+ } else if (!response.ok) {
197
+ const err = new Error(
198
+ 'A connection error occurred while making the request',
199
+ );
200
+ err.code = 'connection_error';
201
+ throw err;
202
+ }
203
+
204
+ return options.useCamelCase ? toCamel(result) : result;
205
+ }
206
+
207
+ export { api as a };
package/dist/api.js CHANGED
@@ -1,19 +1,19 @@
1
- export { a as default } from './api-6ad3ed1e.js';
2
- import './card-91071403.js';
3
- import './index-512fc30d.js';
1
+ export { a as default } from './api-35708af6.js';
2
+ import './card-4257bc68.js';
3
+ import './index-ca9cb73c.js';
4
4
  import 'qs';
5
5
  import 'deepmerge';
6
6
  import 'fast-case';
7
- import './cookie-b78058af.js';
8
- import './cache-751d89b1.js';
9
- import './cart-ff3e3ef6.js';
10
- import './products-7f2fbc3e.js';
11
- import './attributes-7a214d6b.js';
7
+ import './cookie-4cde18fb.js';
8
+ import './cache-b92f4460.js';
9
+ import './cart-35e7bdb4.js';
10
+ import './products-d194c3c6.js';
11
+ import './attributes-bfef7db7.js';
12
12
  import './account-328cc590.js';
13
- import './categories-8f6a4584.js';
14
- import './subscriptions-8044a530.js';
15
- import './content-0afdcb05.js';
16
- import './settings-c5569614.js';
17
- import './index-abe4ebcd.js';
18
- import './locale-4391bcf3.js';
19
- import './currency-42145ec0.js';
13
+ import './categories-b1d04223.js';
14
+ import './subscriptions-f077cac6.js';
15
+ import './content-c366a9f8.js';
16
+ import './settings-34395c72.js';
17
+ import './index-ae8e1259.js';
18
+ import './locale-62622809.js';
19
+ import './currency-004374be.js';
@@ -0,0 +1,16 @@
1
+ import { d as defaultMethods } from './index-ca9cb73c.js';
2
+ import { c as cacheApi } from './cache-b92f4460.js';
3
+
4
+ function methods(request) {
5
+ const { get, list } = defaultMethods(request, '/attributes', ['list', 'get']);
6
+
7
+ return {
8
+ get: (id, ...args) => {
9
+ return cacheApi.getFetch('attributes', id, () => get(id, ...args));
10
+ },
11
+
12
+ list,
13
+ };
14
+ }
15
+
16
+ export { methods as m };
@@ -1,6 +1,6 @@
1
- export { m as default } from './attributes-7a214d6b.js';
2
- import './index-512fc30d.js';
1
+ export { m as default } from './attributes-bfef7db7.js';
2
+ import './index-ca9cb73c.js';
3
3
  import 'qs';
4
4
  import 'deepmerge';
5
5
  import 'fast-case';
6
- import './cache-751d89b1.js';
6
+ import './cache-b92f4460.js';
@@ -0,0 +1,165 @@
1
+ import { s as set, g as get, m as merge, t as toCamel, i as toCamelPath, h as getOptions } from './index-ca9cb73c.js';
2
+
3
+ const RECORD_TIMEOUT = 5000;
4
+
5
+ let VALUES = {
6
+ /*
7
+ [model]: {
8
+ [id]: {
9
+ data,
10
+ record,
11
+ recordTimer,
12
+ presets,
13
+ }
14
+ }
15
+ */
16
+ };
17
+
18
+ const cacheApi = {
19
+ options: {
20
+ enabled: true,
21
+ debug: false,
22
+ },
23
+
24
+ debug(...args) {
25
+ if (this.options.debug) {
26
+ console.log(...args);
27
+ }
28
+ },
29
+
30
+ values({ model, id }, setValues = undefined) {
31
+ this.debug('cache.values', ...arguments);
32
+ if (setValues !== undefined) {
33
+ for (let key in setValues) {
34
+ set(VALUES, `${model}.${id}.${key}`, setValues[key]);
35
+ }
36
+ return;
37
+ }
38
+ return get(VALUES, `${model}.${id}`, {});
39
+ },
40
+
41
+ preset(details) {
42
+ this.debug('cache.preset', ...arguments);
43
+ const { presets = [] } = this.values(details);
44
+ presets.push(details);
45
+ this.values(details, { presets });
46
+ },
47
+
48
+ set(details) {
49
+ this.debug('cache.set', ...arguments);
50
+ let { model, id, path, value } = details;
51
+ let { data = {}, record, presets } = this.values(details);
52
+
53
+ if (id === null) {
54
+ return;
55
+ }
56
+
57
+ if (record === undefined) {
58
+ return this.preset(details);
59
+ }
60
+
61
+ data = merge(record || {}, data);
62
+
63
+ const { useCamelCase } = getOptions();
64
+ if (useCamelCase && value && typeof value === 'object') {
65
+ value = toCamel(value);
66
+ }
67
+
68
+ if (path || value instanceof Array) {
69
+ let upData = { ...(data || {}) };
70
+ let upPath = useCamelCase ? toCamelPath(path) : path;
71
+ set(upData, upPath || '', value);
72
+ data = upData;
73
+ } else if (value && typeof value === 'object') {
74
+ data = data || {};
75
+ data = merge(data, value);
76
+ } else {
77
+ data = value;
78
+ }
79
+
80
+ this.values(details, { data });
81
+
82
+ try {
83
+ // Make sure values have clean refs
84
+ const cache = VALUES[model][id];
85
+ if (cache !== undefined) {
86
+ if (cache.data !== undefined) {
87
+ cache.data = JSON.parse(JSON.stringify(cache.data));
88
+ }
89
+ if (cache.record !== undefined) {
90
+ cache.record = JSON.parse(JSON.stringify(cache.record));
91
+ }
92
+ }
93
+ } catch (err) {
94
+ // noop
95
+ }
96
+ },
97
+
98
+ get(model, id) {
99
+ this.debug('cache.get', ...arguments);
100
+ const { data, recordTimer } = this.values({ model, id });
101
+ this.debug('cache.get:data+recordTimer', ...arguments);
102
+ if (recordTimer) {
103
+ return data;
104
+ }
105
+ },
106
+
107
+ setRecord(record, details) {
108
+ this.debug('cache.setRecord', ...arguments);
109
+ let { recordTimer, presets } = this.values(details);
110
+
111
+ if (recordTimer) {
112
+ clearTimeout(recordTimer);
113
+ }
114
+
115
+ recordTimer = setTimeout(() => {
116
+ this.values(details, { record: undefined, recordTimer: undefined });
117
+ }, RECORD_TIMEOUT);
118
+
119
+ // Record has to be null at minimum, not undefined
120
+ this.values(details, {
121
+ record: record !== undefined ? record : null,
122
+ recordTimer,
123
+ });
124
+
125
+ if (presets) {
126
+ for (let preset of presets) {
127
+ this.set(preset);
128
+ }
129
+ this.values(details, { presets: undefined });
130
+ }
131
+
132
+ const result = this.get(details.model, details.id);
133
+
134
+ return result !== undefined ? result : record;
135
+ },
136
+
137
+ async getFetch(model, id, fetch) {
138
+ if (this.options.enabled) {
139
+ this.debug('cache.getFetch', ...arguments);
140
+ const value = this.get(model, id);
141
+
142
+ if (value !== undefined) {
143
+ return value;
144
+ }
145
+ }
146
+
147
+ const record = await fetch();
148
+ return this.setRecord(record, { model, id });
149
+ },
150
+
151
+ clear(model = undefined, id = undefined) {
152
+ this.debug('cache.clear', ...arguments);
153
+ if (model) {
154
+ if (id) {
155
+ set(VALUES, `${model}.${id}`, undefined);
156
+ } else {
157
+ set(VALUES, model, undefined);
158
+ }
159
+ } else {
160
+ VALUES = {};
161
+ }
162
+ },
163
+ };
164
+
165
+ export { cacheApi as c };
package/dist/cache.js CHANGED
@@ -1,5 +1,5 @@
1
- export { c as default } from './cache-751d89b1.js';
2
- import './index-512fc30d.js';
1
+ export { c as default } from './cache-b92f4460.js';
2
+ import './index-ca9cb73c.js';
3
3
  import 'qs';
4
4
  import 'deepmerge';
5
5
  import 'fast-case';
@@ -0,0 +1,141 @@
1
+ import { j as toSnake, F as vaultRequest } from './index-ca9cb73c.js';
2
+
3
+ const cardApi = {
4
+ async createToken(data) {
5
+ let error = null;
6
+ let code = null;
7
+ let param = null;
8
+ if (!data) {
9
+ error = 'Card details are missing in `swell.card.createToken(card)`';
10
+ param = '';
11
+ }
12
+ const card = toSnake(data);
13
+ if (!card.nonce) {
14
+ if (!this.validateNumber(card.number)) {
15
+ error = 'Card number appears to be invalid';
16
+ code = 'invalid_card_number';
17
+ param = 'number';
18
+ }
19
+ if (card.exp) {
20
+ const exp = this.expiry(card.exp);
21
+ card.exp_month = exp.month;
22
+ card.exp_year = exp.year;
23
+ }
24
+ if (!this.validateExpiry(card.exp_month, card.exp_year)) {
25
+ error = 'Card expiry appears to be invalid';
26
+ code = 'invalid_card_expiry';
27
+ param = 'exp_month';
28
+ }
29
+ if (!this.validateCVC(card.cvc)) {
30
+ error = 'Card CVC code appears to be invalid';
31
+ code = 'invalid_card_cvc';
32
+ param = 'exp_cvc';
33
+ }
34
+ }
35
+
36
+ if (error) {
37
+ const err = new Error(error);
38
+ err.code = code || 'invalid_card';
39
+ err.status = 402;
40
+ err.param = param;
41
+ throw err;
42
+ }
43
+
44
+ // Get a token from the vault
45
+ const result = await vaultRequest('post', '/tokens', card);
46
+ if (result.errors) {
47
+ const param = Object.keys(result.errors)[0];
48
+ const err = new Error(result.errors[param].message || 'Unknown error');
49
+ err.code = 'vault_error';
50
+ err.status = 402;
51
+ err.param = param;
52
+ throw err;
53
+ }
54
+ return result;
55
+ },
56
+
57
+ expiry(value) {
58
+ if (value && value.month && value.year) {
59
+ return value;
60
+ }
61
+
62
+ const parts = new String(value).split(/[\s\/\-]+/, 2);
63
+ const month = parts[0];
64
+ let year = parts[1];
65
+
66
+ // Convert 2 digit year
67
+ if (year && year.length === 2 && /^\d+$/.test(year)) {
68
+ const prefix = new Date().getFullYear().toString().substring(0, 2);
69
+ year = prefix + year;
70
+ }
71
+
72
+ return {
73
+ month: ~~month,
74
+ year: ~~year,
75
+ };
76
+ },
77
+
78
+ types() {
79
+ let e, t, n, r;
80
+ t = {};
81
+ for (e = n = 40; n <= 49; e = ++n) t[e] = 'Visa';
82
+ for (e = r = 50; r <= 59; e = ++r) t[e] = 'MasterCard';
83
+ return (
84
+ (t[34] = t[37] = 'American Express'),
85
+ (t[60] = t[62] = t[64] = t[65] = 'Discover'),
86
+ (t[35] = 'JCB'),
87
+ (t[30] = t[36] = t[38] = t[39] = 'Diners Club'),
88
+ t
89
+ );
90
+ },
91
+
92
+ type(num) {
93
+ return this.types()[num.slice(0, 2)] || 'Unknown';
94
+ },
95
+
96
+ luhnCheck(num) {
97
+ let t, n, r, i, s, o;
98
+ (r = !0), (i = 0), (n = (num + '').split('').reverse());
99
+ for (s = 0, o = n.length; s < o; s++) {
100
+ (t = n[s]), (t = parseInt(t, 10));
101
+ if ((r = !r)) t *= 2;
102
+ t > 9 && (t -= 9), (i += t);
103
+ }
104
+ return i % 10 === 0;
105
+ },
106
+
107
+ validateNumber(num) {
108
+ return (
109
+ (num = (num + '').replace(/\s+|-/g, '')),
110
+ num.length >= 10 && num.length <= 16 && this.luhnCheck(num)
111
+ );
112
+ },
113
+
114
+ validateExpiry(month, year) {
115
+ let r, i;
116
+ return (
117
+ (month = String(month).trim()),
118
+ (year = String(year).trim()),
119
+ /^\d+$/.test(month)
120
+ ? /^\d+$/.test(year)
121
+ ? parseInt(month, 10) <= 12
122
+ ? ((i = new Date(year, month)),
123
+ (r = new Date()),
124
+ i.setMonth(i.getMonth() - 1),
125
+ i.setMonth(i.getMonth() + 1, 1),
126
+ i > r)
127
+ : !1
128
+ : !1
129
+ : !1
130
+ );
131
+ },
132
+
133
+ validateCVC(val) {
134
+ return (
135
+ (val = String(val).trim()),
136
+ /^\d+$/.test(val) && val.length >= 3 && val.length <= 4
137
+ );
138
+ },
139
+ };
140
+
141
+ export { cardApi as c };
package/dist/card.js CHANGED
@@ -1,5 +1,5 @@
1
- export { c as default } from './card-91071403.js';
2
- import './index-512fc30d.js';
1
+ export { c as default } from './card-4257bc68.js';
2
+ import './index-ca9cb73c.js';
3
3
  import 'qs';
4
4
  import 'deepmerge';
5
5
  import 'fast-case';