swell-js 3.22.1 → 3.22.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.
@@ -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-cec81203.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-ffb531dc.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.3',
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 };
@@ -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-cec81203.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-d4d59397.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.2',
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,4 +1,4 @@
1
- export { a as default } from './api-35708af6.js';
1
+ export { a as default } from './api-73c0aac4.js';
2
2
  import './card-4257bc68.js';
3
3
  import './index-ca9cb73c.js';
4
4
  import 'qs';
@@ -6,7 +6,7 @@ import 'deepmerge';
6
6
  import 'fast-case';
7
7
  import './cookie-4cde18fb.js';
8
8
  import './cache-b92f4460.js';
9
- import './cart-35e7bdb4.js';
9
+ import './cart-cec81203.js';
10
10
  import './products-d194c3c6.js';
11
11
  import './attributes-bfef7db7.js';
12
12
  import './account-328cc590.js';
@@ -14,6 +14,6 @@ import './categories-b1d04223.js';
14
14
  import './subscriptions-f077cac6.js';
15
15
  import './content-c366a9f8.js';
16
16
  import './settings-34395c72.js';
17
- import './index-ae8e1259.js';
17
+ import './index-ffb531dc.js';
18
18
  import './locale-62622809.js';
19
19
  import './currency-004374be.js';
@@ -0,0 +1,189 @@
1
+ import 'qs';
2
+ import { b as cloneDeep } from './index-ca9cb73c.js';
3
+ import 'deepmerge';
4
+ import 'fast-case';
5
+ import { c as cleanProductOptions } from './products-d194c3c6.js';
6
+
7
+ function methods(request, options) {
8
+ return {
9
+ state: null,
10
+ order: null,
11
+ settings: null,
12
+ requested: false,
13
+ pendingRequests: [],
14
+ cacheClear: false,
15
+
16
+ async requestStateChange(method, url, id, data) {
17
+ return this.requestStateSync(async () => {
18
+ const result = await request(method, url, id, data);
19
+
20
+ if (result && result.errors) {
21
+ return result;
22
+ }
23
+
24
+ this.state = result;
25
+ return result;
26
+ });
27
+ },
28
+
29
+ nextRequest() {
30
+ if (this.pendingRequests.length <= 0) {
31
+ this.requested = false;
32
+ return;
33
+ }
34
+
35
+ const { handler, resolve, reject } = this.pendingRequests.shift();
36
+
37
+ return Promise.resolve().then(handler).then(resolve, reject).finally(() => {
38
+ this.nextRequest();
39
+ });
40
+ },
41
+
42
+ async requestStateSync(handler) {
43
+ if (this.requested) {
44
+ return new Promise((resolve, reject) => {
45
+ this.pendingRequests.push({ handler, resolve, reject });
46
+ });
47
+ }
48
+
49
+ this.requested = true;
50
+
51
+ try {
52
+ const result = await handler();
53
+
54
+ return result;
55
+ } finally {
56
+ this.nextRequest();
57
+ }
58
+ },
59
+
60
+ get() {
61
+ if (options.getCart) {
62
+ return options.getCart();
63
+ }
64
+ let data;
65
+ if (this.cacheClear) {
66
+ this.cacheClear = false;
67
+ data = { $cache: false };
68
+ }
69
+ return this.requestStateChange('get', '/cart', undefined, data);
70
+ },
71
+
72
+ clearCache() {
73
+ this.cacheClear = true;
74
+ },
75
+
76
+ getItemData(item, data = {}) {
77
+ let result = cloneDeep(item);
78
+ if (typeof item === 'string') {
79
+ result = {
80
+ ...(data || {}),
81
+ product_id: item,
82
+ };
83
+ }
84
+ if (result && result.options) {
85
+ result.options = cleanProductOptions(result.options);
86
+ }
87
+ return result;
88
+ },
89
+
90
+ addItem(item, data) {
91
+ return this.requestStateChange(
92
+ 'post',
93
+ '/cart/items',
94
+ this.getItemData(item, data),
95
+ );
96
+ },
97
+
98
+ updateItem(id, item) {
99
+ return this.requestStateChange(
100
+ 'put',
101
+ `/cart/items/${id}`,
102
+ this.getItemData(item),
103
+ );
104
+ },
105
+
106
+ setItems(input) {
107
+ let items = input;
108
+ if (items && items.map) {
109
+ items = items.map(this.getItemData);
110
+ }
111
+ return this.requestStateChange('put', '/cart/items', items);
112
+ },
113
+
114
+ removeItem(id) {
115
+ return this.requestStateChange('delete', `/cart/items/${id}`);
116
+ },
117
+
118
+ recover(checkoutId) {
119
+ return this.requestStateChange('put', `/cart/recover/${checkoutId}`);
120
+ },
121
+
122
+ update(input) {
123
+ let data = input;
124
+ if (data.items && data.items.map) {
125
+ data = {
126
+ ...data,
127
+ items: data.items.map(this.getItemData),
128
+ };
129
+ }
130
+ if (options.updateCart) {
131
+ return options.updateCart(input);
132
+ }
133
+ return this.requestStateChange('put', `/cart`, data);
134
+ },
135
+
136
+ applyCoupon(code) {
137
+ return this.requestStateChange('put', '/cart/coupon', { code });
138
+ },
139
+
140
+ removeCoupon() {
141
+ return this.requestStateChange('delete', '/cart/coupon');
142
+ },
143
+
144
+ applyGiftcard(code) {
145
+ return this.requestStateChange('post', '/cart/giftcards', { code });
146
+ },
147
+
148
+ removeGiftcard(id) {
149
+ return this.requestStateChange('delete', `/cart/giftcards/${id}`);
150
+ },
151
+
152
+ async getShippingRates() {
153
+ await this.requestStateChange('get', '/cart/shipment-rating');
154
+ return this.state[
155
+ options.useCamelCase ? 'shipmentRating' : 'shipment_rating'
156
+ ];
157
+ },
158
+
159
+ async submitOrder() {
160
+ const result = await this.requestStateChange('post', '/cart/order');
161
+ if (result.errors) {
162
+ return result;
163
+ }
164
+ this.state = null;
165
+ this.order = result;
166
+ return result;
167
+ },
168
+
169
+ async getOrder(checkoutId = undefined) {
170
+ let result;
171
+ if (checkoutId) {
172
+ result = await request('get', `/cart/order`, {
173
+ checkout_id: checkoutId,
174
+ });
175
+ } else {
176
+ result = await request('get', `/cart/order`);
177
+ }
178
+ this.order = result;
179
+ return result;
180
+ },
181
+
182
+ async getSettings() {
183
+ this.settings = await request('get', '/cart/settings');
184
+ return this.settings;
185
+ },
186
+ };
187
+ }
188
+
189
+ export { methods as m };
package/dist/cart.js CHANGED
@@ -1,4 +1,4 @@
1
- export { m as default } from './cart-35e7bdb4.js';
1
+ export { m as default } from './cart-cec81203.js';
2
2
  import 'qs';
3
3
  import './index-ca9cb73c.js';
4
4
  import 'deepmerge';