swell-js 4.0.0-next.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.
- package/LICENSE +3 -1
- package/README.md +2 -2
- package/dist/account.mjs +100 -0
- package/dist/api.mjs +327 -0
- package/dist/attributes.mjs +21 -0
- package/dist/cache.mjs +170 -0
- package/dist/card.mjs +146 -0
- package/dist/cart.mjs +193 -0
- package/dist/categories.mjs +21 -0
- package/dist/content.mjs +24 -0
- package/dist/cookie.mjs +55 -0
- package/dist/currency.mjs +197 -0
- package/dist/find.fd7de00e.mjs +2971 -0
- package/dist/index.78b6af02.mjs +1796 -0
- package/dist/index.mjs +21 -0
- package/dist/locale.mjs +50 -0
- package/dist/payment.mjs +3489 -0
- package/dist/products.mjs +423 -0
- package/dist/round.a606b844.mjs +57 -0
- package/dist/settings.mjs +299 -0
- package/dist/subscriptions.mjs +75 -0
- package/dist/swell.cjs +10254 -0
- package/dist/swell.cjs.map +1 -0
- package/dist/swell.umd.min.js +11987 -0
- package/dist/swell.umd.min.js.map +1 -0
- package/dist/utils/index.mjs +6 -0
- package/package.json +67 -40
- package/types/account/camel.d.ts +12 -0
- package/types/account/index.d.ts +8 -0
- package/types/account/snake.d.ts +53 -0
- package/types/attribute/camel.d.ts +6 -0
- package/types/attribute/index.d.ts +4 -0
- package/types/attribute/snake.d.ts +37 -0
- package/types/billing/camel.d.ts +36 -0
- package/types/billing/index.d.ts +33 -0
- package/types/billing/snake.d.ts +58 -0
- package/types/card/camel.d.ts +12 -0
- package/types/card/index.d.ts +8 -0
- package/types/card/snake.d.ts +41 -0
- package/types/cart/camel.d.ts +24 -0
- package/types/cart/index.d.ts +24 -0
- package/types/cart/snake.d.ts +163 -0
- package/types/category/camel.d.ts +6 -0
- package/types/category/index.d.ts +4 -0
- package/types/category/snake.d.ts +24 -0
- package/types/content/camel.d.ts +9 -0
- package/types/content/index.d.ts +7 -0
- package/types/content/snake.d.ts +16 -0
- package/types/coupon/camel.d.ts +6 -0
- package/types/coupon/index.d.ts +4 -0
- package/types/coupon/snake.d.ts +14 -0
- package/types/currency/camel.d.ts +16 -0
- package/types/currency/index.d.ts +18 -0
- package/types/currency/snake.d.ts +22 -0
- package/types/discount/camel.d.ts +6 -0
- package/types/discount/index.d.ts +4 -0
- package/types/discount/snake.d.ts +12 -0
- package/types/index.d.ts +365 -0
- package/types/invoice/camel.d.ts +6 -0
- package/types/invoice/index.d.ts +4 -0
- package/types/invoice/snake.d.ts +59 -0
- package/types/locale/index.d.ts +5 -0
- package/types/order/camel.d.ts +24 -0
- package/types/order/index.d.ts +19 -0
- package/types/order/snake.d.ts +211 -0
- package/types/payment/camel.d.ts +43 -0
- package/types/payment/index.d.ts +43 -0
- package/types/payment/snake.d.ts +117 -0
- package/types/product/camel.d.ts +47 -0
- package/types/product/index.d.ts +49 -0
- package/types/product/snake.d.ts +185 -0
- package/types/promotion/camel.d.ts +6 -0
- package/types/promotion/index.d.ts +4 -0
- package/types/promotion/snake.d.ts +20 -0
- package/types/purchase_link/camel.d.ts +10 -0
- package/types/purchase_link/index.d.ts +8 -0
- package/types/purchase_link/snake.d.ts +26 -0
- package/types/refund/camel.d.ts +6 -0
- package/types/refund/index.d.ts +4 -0
- package/types/refund/snake.d.ts +29 -0
- package/types/settings/index.d.ts +10 -0
- package/types/shipment_rating/camel.d.ts +10 -0
- package/types/shipment_rating/index.d.ts +9 -0
- package/types/shipment_rating/snake.d.ts +20 -0
- package/types/subscription/camel.d.ts +10 -0
- package/types/subscription/index.d.ts +7 -0
- package/types/subscription/snake.d.ts +142 -0
- package/dist/swell.cjs.js +0 -1
- package/dist/swell.esm.js +0 -1
- package/dist/swell.umd.js +0 -1
package/LICENSE
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Swell Commerce Corp.
|
|
2
4
|
|
|
3
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
6
|
|
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@ Universal JavaScript client for Swell's Frontend API, providing client-safe acce
|
|
|
9
9
|
- Resolve linked content to dynamically generate page URLs
|
|
10
10
|
- Format prices in the store's currency
|
|
11
11
|
|
|
12
|
-
> This SDK implements a subset of operations available in Swell's [Backend API](https://swell.
|
|
12
|
+
> This SDK implements a subset of operations available in Swell's [Backend API](https://developers.swell.is/backend-api/) and is authorized with a public key + session token, making it safe to use in any context. You should only use the Backend API server-side, and keep your secret keys stored as environment variables.
|
|
13
13
|
|
|
14
14
|
**About Swell**
|
|
15
15
|
|
|
@@ -17,4 +17,4 @@ Universal JavaScript client for Swell's Frontend API, providing client-safe acce
|
|
|
17
17
|
|
|
18
18
|
## Documentation
|
|
19
19
|
|
|
20
|
-
📖
|
|
20
|
+
📖 [**View Swell.js Documentation**](https://developers.swell.is/frontend-api/)
|
package/dist/account.mjs
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,327 @@
|
|
|
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';
|
|
16
|
+
import 'qs';
|
|
17
|
+
import './find.fd7de00e.mjs';
|
|
18
|
+
import './round.a606b844.mjs';
|
|
19
|
+
import 'deepmerge';
|
|
20
|
+
import 'fast-case';
|
|
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 };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { d as defaultMethods } from './index.78b6af02.mjs';
|
|
2
|
+
import cacheApi from './cache.mjs';
|
|
3
|
+
import 'qs';
|
|
4
|
+
import './find.fd7de00e.mjs';
|
|
5
|
+
import './round.a606b844.mjs';
|
|
6
|
+
import 'deepmerge';
|
|
7
|
+
import 'fast-case';
|
|
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
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
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
|
+
import 'qs';
|
|
4
|
+
import './round.a606b844.mjs';
|
|
5
|
+
import 'deepmerge';
|
|
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 };
|