@storecraft/sdk 1.0.14 → 1.0.16
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/index.js +24 -16
- package/package.json +1 -1
- package/src/ai.js +5 -5
- package/src/auth.js +56 -80
- package/src/checkout.js +7 -7
- package/src/collections.js +0 -7
- package/src/customers.js +0 -2
- package/src/discounts.js +1 -7
- package/src/email.js +67 -0
- package/src/images.js +0 -2
- package/src/notifications.js +3 -3
- package/src/orders.js +20 -3
- package/src/payments.js +49 -21
- package/src/posts.js +0 -1
- package/src/products.js +3 -8
- package/src/search.js +0 -3
- package/src/settings.js +1 -2
- package/src/shipping.js +0 -1
- package/src/statistics.js +19 -49
- package/src/storage.js +29 -38
- package/src/storefronts.js +23 -3
- package/src/tags.js +0 -1
- package/src/templates.js +0 -1
- package/src/utils.api.fetch.js +43 -50
- package/src/utils.functional.js +0 -6
- package/types.d.ts +17 -4
package/src/storefronts.js
CHANGED
@@ -13,7 +13,6 @@ import { collection_base, fetchApiWithAuth } from './utils.api.fetch.js';
|
|
13
13
|
export default class Storefronts extends collection_base {
|
14
14
|
|
15
15
|
/**
|
16
|
-
*
|
17
16
|
* @param {StorecraftSDK} sdk
|
18
17
|
*/
|
19
18
|
constructor(sdk) {
|
@@ -24,11 +23,9 @@ export default class Storefronts extends collection_base {
|
|
24
23
|
* @description Export a storefront into the `storage`. This is
|
25
24
|
* beneficial for `collections`, that hardly change and therefore can be
|
26
25
|
* efficiently stored in a cost-effective `storage` and **CDN** network.
|
27
|
-
*
|
28
26
|
* @param {HandleOrId} handle_or_id
|
29
27
|
*/
|
30
28
|
publish = async (handle_or_id) => {
|
31
|
-
|
32
29
|
const result = await fetchApiWithAuth(
|
33
30
|
this.sdk,
|
34
31
|
`storefronts/${handle_or_id}/export`,
|
@@ -36,6 +33,29 @@ export default class Storefronts extends collection_base {
|
|
36
33
|
method: 'post'
|
37
34
|
}
|
38
35
|
);
|
36
|
+
return result
|
37
|
+
}
|
38
|
+
|
39
|
+
/**
|
40
|
+
* @description You can fetch the default auto-generated storefront.
|
41
|
+
* This will fetch all active
|
42
|
+
* - collections
|
43
|
+
* - discounts
|
44
|
+
* - shipping methods
|
45
|
+
* - posts (latest 5)
|
46
|
+
* - products(latest 10)
|
47
|
+
* that are linked to the storefront. Also, all the products
|
48
|
+
* tags aggregated so you can build a filter system in the frontend
|
49
|
+
* @returns {Promise<StorefrontType>}
|
50
|
+
*/
|
51
|
+
get_default_auto_generated_storefront = async () => {
|
52
|
+
const result = await fetchApiWithAuth(
|
53
|
+
this.sdk,
|
54
|
+
`storefronts/auto-generated`,
|
55
|
+
{
|
56
|
+
method: 'get'
|
57
|
+
}
|
58
|
+
);
|
39
59
|
|
40
60
|
return result
|
41
61
|
}
|
package/src/tags.js
CHANGED
package/src/templates.js
CHANGED
package/src/utils.api.fetch.js
CHANGED
@@ -9,7 +9,6 @@ import {
|
|
9
9
|
|
10
10
|
|
11
11
|
/**
|
12
|
-
*
|
13
12
|
* @param {StorecraftSDKConfig} config
|
14
13
|
* @param {string} path
|
15
14
|
* @param {URLSearchParams} [query] url search params
|
@@ -34,12 +33,10 @@ export const url = (config, path, query) => {
|
|
34
33
|
* - Prepends `backend` endpoint.
|
35
34
|
* - Fetches with `authentication` middleware.
|
36
35
|
* - Refreshed `auth` if needed.
|
37
|
-
*
|
38
36
|
* @param {StorecraftSDK} sdk
|
39
37
|
* @param {string} path relative path in api
|
40
38
|
* @param {RequestInit} [init] request `init` type
|
41
39
|
* @param {URLSearchParams} [query] url search params
|
42
|
-
*
|
43
40
|
* @returns {Promise<Response>}
|
44
41
|
*/
|
45
42
|
export const fetchOnlyApiResponseWithAuth = async (
|
@@ -48,11 +45,11 @@ export const fetchOnlyApiResponseWithAuth = async (
|
|
48
45
|
|
49
46
|
const auth_token = await sdk.auth.working_auth_token();
|
50
47
|
const auth_header_value = (
|
51
|
-
sdk.auth.
|
48
|
+
sdk.auth.currentAuthStrategy==='apikey' ? 'Basic' : 'Bearer'
|
52
49
|
) + ` ${auth_token}`;
|
53
50
|
|
54
51
|
|
55
|
-
const response = await
|
52
|
+
const response = await sdk.fetcher(
|
56
53
|
url(sdk.config, path, query),
|
57
54
|
{
|
58
55
|
...init,
|
@@ -74,16 +71,14 @@ export const fetchOnlyApiResponseWithAuth = async (
|
|
74
71
|
* - Refreshed `auth` if needed.
|
75
72
|
* - Throws a `json` representation of the `error`,
|
76
73
|
* if the request is `bad`
|
77
|
-
*
|
74
|
+
* - returns the object according to the `content-type` header
|
75
|
+
* - json, text, html, blob
|
78
76
|
* @template {any} [R=any]
|
79
|
-
*
|
80
77
|
* @param {StorecraftSDK} sdk
|
81
78
|
* @param {string} path relative path in api
|
82
79
|
* @param {RequestInit} [init] request `init` type
|
83
80
|
* @param {URLSearchParams} [query] url search params
|
84
|
-
*
|
85
81
|
* @throws {error}
|
86
|
-
*
|
87
82
|
* @returns {Promise<R>}
|
88
83
|
*/
|
89
84
|
export const fetchApiWithAuth = async (
|
@@ -98,15 +93,24 @@ export const fetchApiWithAuth = async (
|
|
98
93
|
|
99
94
|
const isok = response.ok;
|
100
95
|
let payload = undefined;
|
96
|
+
const ct = response?.headers?.get('content-type');
|
101
97
|
|
102
98
|
try {
|
103
|
-
|
99
|
+
if(ct?.includes('application/json'))
|
100
|
+
payload = await response.json();
|
101
|
+
else if(ct?.includes('text/plain'))
|
102
|
+
payload = await response.text();
|
103
|
+
else if(ct?.includes('text/html'))
|
104
|
+
payload = await response.text();
|
105
|
+
else
|
106
|
+
payload = await response.blob();
|
104
107
|
} catch(e) {
|
105
108
|
console.log('fetchApiWithAuth.json()', e)
|
106
109
|
}
|
107
110
|
|
108
|
-
if(!isok)
|
111
|
+
if(!isok) {
|
109
112
|
throw payload;
|
113
|
+
}
|
110
114
|
|
111
115
|
return payload;
|
112
116
|
}
|
@@ -114,16 +118,14 @@ export const fetchApiWithAuth = async (
|
|
114
118
|
|
115
119
|
/**
|
116
120
|
* @template {any} G Get type
|
117
|
-
*
|
118
|
-
*
|
119
121
|
* @param {StorecraftSDK} sdk
|
120
122
|
* @param {string} handle_or_id `handle` or `id`
|
121
123
|
* @param {string} resource base path of resource
|
122
|
-
*
|
123
|
-
*
|
124
124
|
* @returns {Promise<G>}
|
125
125
|
*/
|
126
|
-
export async function get_from_collection_resource(
|
126
|
+
export async function get_from_collection_resource(
|
127
|
+
sdk, resource, handle_or_id
|
128
|
+
) {
|
127
129
|
return fetchApiWithAuth(
|
128
130
|
sdk,
|
129
131
|
`${resource}/${handle_or_id}`,
|
@@ -135,18 +137,15 @@ export async function get_from_collection_resource(sdk, resource, handle_or_id)
|
|
135
137
|
|
136
138
|
|
137
139
|
/**
|
138
|
-
*
|
139
140
|
* @template {any} U the upsert type
|
140
|
-
*
|
141
|
-
*
|
142
141
|
* @param {StorecraftSDK} sdk
|
143
142
|
* @param {string} resource base path of resource
|
144
143
|
* @param {U} item Item to upsert
|
145
|
-
*
|
146
|
-
*
|
147
144
|
* @returns {Promise<string>} id
|
148
145
|
*/
|
149
|
-
export async function upsert_to_collection_resource(
|
146
|
+
export async function upsert_to_collection_resource(
|
147
|
+
sdk, resource, item
|
148
|
+
) {
|
150
149
|
return fetchApiWithAuth(
|
151
150
|
sdk,
|
152
151
|
`${resource}`,
|
@@ -176,19 +175,18 @@ export async function count_query_of_resource(sdk, resource, query) {
|
|
176
175
|
{
|
177
176
|
method: 'get',
|
178
177
|
}
|
179
|
-
)
|
178
|
+
);
|
180
179
|
}
|
181
180
|
|
182
181
|
/**
|
183
|
-
*
|
184
182
|
* @param {StorecraftSDK} sdk
|
185
183
|
* @param {string} resource base path of resource
|
186
184
|
* @param {string} handle_or_id `handle` or `id`
|
187
|
-
*
|
188
|
-
*
|
189
185
|
* @returns {Promise<boolean>}
|
190
186
|
*/
|
191
|
-
export async function remove_from_collection_resource(
|
187
|
+
export async function remove_from_collection_resource(
|
188
|
+
sdk, resource, handle_or_id
|
189
|
+
) {
|
192
190
|
return fetchApiWithAuth(
|
193
191
|
sdk,
|
194
192
|
`${resource}/${handle_or_id}`,
|
@@ -202,19 +200,15 @@ export async function remove_from_collection_resource(sdk, resource, handle_or_i
|
|
202
200
|
/**
|
203
201
|
* @description Invoke `api` endpoint that requires use of `query`
|
204
202
|
* object. I named it `list` because it usually entails list op.
|
205
|
-
*
|
206
|
-
*
|
207
203
|
* @template {any} G Get type
|
208
|
-
*
|
209
|
-
*
|
210
204
|
* @param {StorecraftSDK} sdk
|
211
205
|
* @param {string} resource base path of resource
|
212
206
|
* @param {ApiQuery<G>} [query]
|
213
|
-
*
|
214
|
-
*
|
215
207
|
* @returns {Promise<G[]>}
|
216
208
|
*/
|
217
|
-
export async function list_from_collection_resource(
|
209
|
+
export async function list_from_collection_resource(
|
210
|
+
sdk, resource, query={}
|
211
|
+
) {
|
218
212
|
const sq = api_query_to_searchparams(query);
|
219
213
|
|
220
214
|
// console.log('sq', sq.toString())
|
@@ -230,7 +224,6 @@ export async function list_from_collection_resource(sdk, resource, query={}) {
|
|
230
224
|
|
231
225
|
/**
|
232
226
|
* @description A simple resource base `class` with `CRUD` helpers
|
233
|
-
*
|
234
227
|
* @template {any} U upsert type
|
235
228
|
* @template {any} G get type
|
236
229
|
*/
|
@@ -243,7 +236,6 @@ export class collection_base {
|
|
243
236
|
#base_name = undefined;
|
244
237
|
|
245
238
|
/**
|
246
|
-
*
|
247
239
|
* @param {StorecraftSDK} sdk storecraft sdk
|
248
240
|
* @param {string} base_name base path of resource type
|
249
241
|
*/
|
@@ -254,36 +246,33 @@ export class collection_base {
|
|
254
246
|
|
255
247
|
|
256
248
|
/**
|
257
|
-
*
|
258
249
|
* @param {string} handle_or_id `handle` or `id`
|
259
|
-
*
|
260
|
-
*
|
261
250
|
* @returns {Promise<G>}
|
262
251
|
*/
|
263
252
|
async get(handle_or_id) {
|
264
|
-
return get_from_collection_resource(
|
253
|
+
return get_from_collection_resource(
|
254
|
+
this.sdk, this.base_name, handle_or_id
|
255
|
+
);
|
265
256
|
}
|
266
257
|
|
267
258
|
/**
|
268
|
-
*
|
269
259
|
* @param {U} item Item to upsert
|
270
|
-
*
|
271
|
-
*
|
272
260
|
* @returns {Promise<string>} id
|
273
261
|
*/
|
274
262
|
async upsert(item) {
|
275
|
-
return upsert_to_collection_resource(
|
263
|
+
return upsert_to_collection_resource(
|
264
|
+
this.sdk, this.base_name, item
|
265
|
+
);
|
276
266
|
}
|
277
267
|
|
278
268
|
/**
|
279
|
-
*
|
280
269
|
* @param {string} handle_or_id `handle` or `id`
|
281
|
-
*
|
282
|
-
*
|
283
270
|
* @returns {Promise<boolean>}
|
284
271
|
*/
|
285
272
|
async remove(handle_or_id) {
|
286
|
-
return remove_from_collection_resource(
|
273
|
+
return remove_from_collection_resource(
|
274
|
+
this.sdk, this.base_name, handle_or_id
|
275
|
+
);
|
287
276
|
}
|
288
277
|
|
289
278
|
/**
|
@@ -291,7 +280,9 @@ export class collection_base {
|
|
291
280
|
* @returns {Promise<G[]>}
|
292
281
|
*/
|
293
282
|
async list(query) {
|
294
|
-
return list_from_collection_resource(
|
283
|
+
return list_from_collection_resource(
|
284
|
+
this.sdk, this.base_name, query
|
285
|
+
);
|
295
286
|
}
|
296
287
|
|
297
288
|
/**
|
@@ -299,7 +290,9 @@ export class collection_base {
|
|
299
290
|
* @param {ApiQuery<G>} query Query object
|
300
291
|
*/
|
301
292
|
async count_query(query) {
|
302
|
-
return count_query_of_resource(
|
293
|
+
return count_query_of_resource(
|
294
|
+
this.sdk, this.base_name, query
|
295
|
+
);
|
303
296
|
}
|
304
297
|
|
305
298
|
get base_name() {
|
package/src/utils.functional.js
CHANGED
@@ -9,7 +9,6 @@ export const select_fields = (...fields) => {
|
|
9
9
|
}
|
10
10
|
|
11
11
|
/**
|
12
|
-
*
|
13
12
|
* @param {...any} fields
|
14
13
|
*/
|
15
14
|
export const filter_fields = (...fields) => {
|
@@ -20,7 +19,6 @@ export const filter_fields = (...fields) => {
|
|
20
19
|
}
|
21
20
|
|
22
21
|
/**
|
23
|
-
*
|
24
22
|
* @param {object} o
|
25
23
|
*/
|
26
24
|
export const select_unused_fields = o => Object.keys(o).reduce((p, c) => {
|
@@ -33,7 +31,6 @@ export const select_unused_fields = o => Object.keys(o).reduce((p, c) => {
|
|
33
31
|
}, {});
|
34
32
|
|
35
33
|
/**
|
36
|
-
*
|
37
34
|
* @param {any[]} items
|
38
35
|
*/
|
39
36
|
export const filter_unused = items =>
|
@@ -55,7 +52,6 @@ export const delete_keys = (...keys) => {
|
|
55
52
|
|
56
53
|
|
57
54
|
/**
|
58
|
-
*
|
59
55
|
* @param {string} text
|
60
56
|
*/
|
61
57
|
export const text2tokens_unsafe = (text) => {
|
@@ -79,7 +75,6 @@ export const STOP_WORDS = [
|
|
79
75
|
]
|
80
76
|
|
81
77
|
/**
|
82
|
-
*
|
83
78
|
* @param {string} text
|
84
79
|
* @returns {string[] | undefined}
|
85
80
|
*/
|
@@ -101,7 +96,6 @@ export const text2tokens = (text) => {
|
|
101
96
|
|
102
97
|
|
103
98
|
/**
|
104
|
-
*
|
105
99
|
* @param {any[]} arrA
|
106
100
|
* @param {any[]} arrB
|
107
101
|
* @returns
|
package/types.d.ts
CHANGED
@@ -3,8 +3,8 @@ import type { ApiAuthResult, ApiKeyResult } from '@storecraft/core/api';
|
|
3
3
|
export * from './index.js';
|
4
4
|
|
5
5
|
/**
|
6
|
-
* @description The `storecraft` **SDK**
|
7
|
-
*
|
6
|
+
* @description The `storecraft` **SDK** `auth` config,
|
7
|
+
* represents either `apikey` or `jwt` authentication
|
8
8
|
*/
|
9
9
|
export type SdkConfigAuth = ApiAuthResult | ApiKeyResult;
|
10
10
|
|
@@ -13,9 +13,22 @@ export type SdkConfigAuth = ApiAuthResult | ApiKeyResult;
|
|
13
13
|
*/
|
14
14
|
export type StorecraftSDKConfig = {
|
15
15
|
|
16
|
-
/**
|
16
|
+
/**
|
17
|
+
* @description Endpoint of `backend`
|
18
|
+
*/
|
17
19
|
endpoint?: string;
|
18
20
|
|
19
|
-
/**
|
21
|
+
/**
|
22
|
+
* @description `auth` info, may be either `apikey` or `jwt` results.
|
23
|
+
* This will be setup automatically when performing `login` or
|
24
|
+
* `apikey` operations if
|
25
|
+
* - the {@link StorecraftSDKConfig.persist_auth} is set to `true`.
|
26
|
+
* - This us used by the sdk to re-authenticate the user when
|
27
|
+
* the token expires.
|
28
|
+
*/
|
20
29
|
auth?: SdkConfigAuth;
|
21
30
|
}
|
31
|
+
|
32
|
+
export type Fetcher = (
|
33
|
+
input: RequestInfo, init?: RequestInit
|
34
|
+
) => Promise<Response>
|