@cloudcommerce/api 2.0.3 → 2.0.5
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/.env +3 -0
- package/.turbo/turbo-build.log +1 -1
- package/.turbo/turbo-test.log +9 -4
- package/lib/api.d.ts +8 -2
- package/lib/api.js +15 -15
- package/lib/api.js.map +1 -1
- package/package.json +3 -2
- package/src/api.ts +20 -15
- package/tests/index.test.ts +23 -2
- package/types.d.ts +35 -26
package/.env
ADDED
package/.turbo/turbo-build.log
CHANGED
package/.turbo/turbo-test.log
CHANGED
|
@@ -1,18 +1,23 @@
|
|
|
1
1
|
|
|
2
|
-
> @cloudcommerce/api@2.0.
|
|
2
|
+
> @cloudcommerce/api@2.0.4 test /home/leo/code/ecomplus/cloud-commerce/packages/api
|
|
3
3
|
> tsc -p ../../tsconfig.test.json && vitest run
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
RUN v1.1.3 /home/leo/code/ecomplus/cloud-commerce/packages/api
|
|
7
7
|
|
|
8
|
+
stdout | tests/index.test.ts > Read product and typecheck SKU
|
|
9
|
+
GFJ4714
|
|
10
|
+
|
|
8
11
|
✓ tests/index.test.ts > Read product and typecheck SKU
|
|
12
|
+
✓ tests/index.test.ts > Find and read product by SKU
|
|
9
13
|
✓ tests/index.test.ts > 404 with different Store ID from env
|
|
10
14
|
✓ tests/index.test.ts > List categories and typecheck result
|
|
11
15
|
✓ tests/index.test.ts > 401 trying to list API events
|
|
12
16
|
✓ tests/index.test.ts > 401 to create category and body typecheck
|
|
17
|
+
✓ tests/index.test.ts > 204 to update products views
|
|
13
18
|
|
|
14
19
|
Test Files 1 passed (1)
|
|
15
|
-
Tests
|
|
16
|
-
Start at 16:
|
|
17
|
-
Duration 1.
|
|
20
|
+
Tests 7 passed (7)
|
|
21
|
+
Start at 16:50:25
|
|
22
|
+
Duration 1.32s (transform 138ms, setup 1ms, collect 135ms, tests 681ms, environment 0ms, prepare 152ms)
|
|
18
23
|
|
package/lib/api.d.ts
CHANGED
|
@@ -66,7 +66,10 @@ declare const api: {
|
|
|
66
66
|
config: Config;
|
|
67
67
|
data: null;
|
|
68
68
|
}>;
|
|
69
|
-
patch: (endpoint:
|
|
69
|
+
patch: <E_2 extends Endpoint, C_2 extends AbstractedConfig>(endpoint: E_2, body: RequestBody<{
|
|
70
|
+
endpoint: E_2;
|
|
71
|
+
method: 'patch';
|
|
72
|
+
}>, config?: C_2 | undefined) => Promise<Response & {
|
|
70
73
|
config: Config;
|
|
71
74
|
data: null;
|
|
72
75
|
}>;
|
|
@@ -110,7 +113,10 @@ declare const put: <E extends import("../types.d").ResourceAndFind | `products/$
|
|
|
110
113
|
config: Config;
|
|
111
114
|
data: null;
|
|
112
115
|
}>;
|
|
113
|
-
declare const patch: (endpoint:
|
|
116
|
+
declare const patch: <E extends Endpoint, C extends AbstractedConfig>(endpoint: E, body: RequestBody<{
|
|
117
|
+
endpoint: E;
|
|
118
|
+
method: 'patch';
|
|
119
|
+
}>, config?: C | undefined) => Promise<Response & {
|
|
114
120
|
config: Config;
|
|
115
121
|
data: null;
|
|
116
122
|
}>;
|
package/lib/api.js
CHANGED
|
@@ -98,10 +98,9 @@ const api = async (requestConfig, _retries = 0) => {
|
|
|
98
98
|
? { ...globalThis.$apiMergeConfig, ...requestConfig }
|
|
99
99
|
: requestConfig;
|
|
100
100
|
const { url, headers } = def.middleware(config);
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
const canCache = method === 'get' && config.canCache;
|
|
101
|
+
const method = config.method?.toUpperCase() || 'GET';
|
|
102
|
+
const { timeout = 20000, maxRetries = 3, cacheMaxAge = 600000 /* 10 minutes */ } = config;
|
|
103
|
+
const canCache = method === 'GET' && config.canCache;
|
|
105
104
|
let cacheKey;
|
|
106
105
|
if (canCache) {
|
|
107
106
|
cacheKey = `${url}${JSON.stringify(headers)}`;
|
|
@@ -140,17 +139,16 @@ const api = async (requestConfig, _retries = 0) => {
|
|
|
140
139
|
clearTimeout(timer);
|
|
141
140
|
if (response) {
|
|
142
141
|
if (response.ok) {
|
|
143
|
-
const res =
|
|
144
|
-
|
|
145
|
-
data: await response.json(),
|
|
146
|
-
};
|
|
142
|
+
const res = response;
|
|
143
|
+
res.data = response.status !== 204 ? await response.json() : null;
|
|
147
144
|
if (canCache && cacheKey) {
|
|
148
145
|
globalThis.__apiCache[cacheKey] = {
|
|
149
146
|
timestamp: Date.now(),
|
|
150
147
|
res,
|
|
151
148
|
};
|
|
152
149
|
}
|
|
153
|
-
|
|
150
|
+
res.config = config;
|
|
151
|
+
return res;
|
|
154
152
|
}
|
|
155
153
|
const { status } = response;
|
|
156
154
|
if (maxRetries < _retries && (status === 429 || status >= 500)) {
|
|
@@ -189,12 +187,14 @@ const put = (endpoint, body, config) => {
|
|
|
189
187
|
body,
|
|
190
188
|
});
|
|
191
189
|
};
|
|
192
|
-
const patch = (endpoint, body, config) =>
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
190
|
+
const patch = (endpoint, body, config) => {
|
|
191
|
+
return api({
|
|
192
|
+
...config,
|
|
193
|
+
method: 'patch',
|
|
194
|
+
endpoint,
|
|
195
|
+
body,
|
|
196
|
+
});
|
|
197
|
+
};
|
|
198
198
|
const del = (endpoint, config) => api({
|
|
199
199
|
...config,
|
|
200
200
|
method: 'delete',
|
package/lib/api.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api.js","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAoBA,IAAI,CAAC,UAAU,CAAC,UAAU,EAAE;IAC1B,UAAU,CAAC,UAAU,GAAG,EAAE,CAAC;CAC5B;AAED,MAAM,IAAI,GAAG,CACX,CAAC,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO,EAAE,GAAG,CAAC;OAC1C,UAAU,CACS,CAAC;AAEzB,MAAM,QAAS,SAAQ,KAAK;IAM1B,YACE,MAAc,EACd,Q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Q,GACT,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudcommerce/api",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.0.
|
|
4
|
+
"version": "2.0.5",
|
|
5
5
|
"description": "E-Com Plus Cloud Commerce APIs client/adapter",
|
|
6
6
|
"main": "lib/api.js",
|
|
7
7
|
"types": "lib/api.d.ts",
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
"homepage": "https://github.com/ecomplus/cloud-commerce/tree/main/packages/api#readme",
|
|
23
23
|
"scripts": {
|
|
24
24
|
"build": "bash ../../scripts/build-lib.sh",
|
|
25
|
-
"test": "tsc -p ../../tsconfig.test.json && vitest run"
|
|
25
|
+
"test": "tsc -p ../../tsconfig.test.json && vitest run",
|
|
26
|
+
"test:echo": "tsc -p ../../tsconfig.test.json && vitest run --silent=false"
|
|
26
27
|
}
|
|
27
28
|
}
|
package/src/api.ts
CHANGED
|
@@ -143,13 +143,13 @@ const api = async <T extends Config & { body?: any, data?: any }>(
|
|
|
143
143
|
? { ...globalThis.$apiMergeConfig, ...requestConfig }
|
|
144
144
|
: requestConfig;
|
|
145
145
|
const { url, headers } = def.middleware(config);
|
|
146
|
+
const method = config.method?.toUpperCase() || 'GET';
|
|
146
147
|
const {
|
|
147
|
-
method = 'get',
|
|
148
148
|
timeout = 20000,
|
|
149
149
|
maxRetries = 3,
|
|
150
|
-
cacheMaxAge = 600000,
|
|
150
|
+
cacheMaxAge = 600000, /* 10 minutes */
|
|
151
151
|
} = config;
|
|
152
|
-
const canCache = method === '
|
|
152
|
+
const canCache = method === 'GET' && config.canCache;
|
|
153
153
|
let cacheKey: string | undefined;
|
|
154
154
|
if (canCache) {
|
|
155
155
|
cacheKey = `${url}${JSON.stringify(headers)}`;
|
|
@@ -190,17 +190,16 @@ const api = async <T extends Config & { body?: any, data?: any }>(
|
|
|
190
190
|
|
|
191
191
|
if (response) {
|
|
192
192
|
if (response.ok) {
|
|
193
|
-
const res = {
|
|
194
|
-
|
|
195
|
-
data: await response.json(),
|
|
196
|
-
};
|
|
193
|
+
const res = response as Response & { data: any, config: Config };
|
|
194
|
+
res.data = response.status !== 204 ? await response.json() : null;
|
|
197
195
|
if (canCache && cacheKey) {
|
|
198
196
|
globalThis.__apiCache[cacheKey] = {
|
|
199
197
|
timestamp: Date.now(),
|
|
200
|
-
res,
|
|
198
|
+
res: res as Response & { data: any },
|
|
201
199
|
};
|
|
202
200
|
}
|
|
203
|
-
|
|
201
|
+
res.config = config;
|
|
202
|
+
return res;
|
|
204
203
|
}
|
|
205
204
|
const { status } = response;
|
|
206
205
|
if (maxRetries < _retries && (status === 429 || status >= 500)) {
|
|
@@ -259,12 +258,18 @@ const put = <E extends Exclude<Endpoint, ResourceOpQuery>, C extends AbstractedC
|
|
|
259
258
|
});
|
|
260
259
|
};
|
|
261
260
|
|
|
262
|
-
const patch =
|
|
263
|
-
|
|
264
|
-
method: 'patch'
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
261
|
+
const patch = <E extends Endpoint, C extends AbstractedConfig>(
|
|
262
|
+
endpoint: E,
|
|
263
|
+
body: RequestBody<{ endpoint: E, method: 'patch' }>,
|
|
264
|
+
config?: C,
|
|
265
|
+
) => {
|
|
266
|
+
return api({
|
|
267
|
+
...config,
|
|
268
|
+
method: 'patch',
|
|
269
|
+
endpoint,
|
|
270
|
+
body,
|
|
271
|
+
});
|
|
272
|
+
};
|
|
268
273
|
|
|
269
274
|
const del = (endpoint: Endpoint, config?: AbstractedConfig) => api({
|
|
270
275
|
...config,
|
package/tests/index.test.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import * as dotenv from 'dotenv';
|
|
1
2
|
import { test, expect } from 'vitest';
|
|
2
3
|
import api, { type ApiError } from '../src/api';
|
|
3
4
|
|
|
@@ -10,14 +11,24 @@ test('Read product and typecheck SKU', async () => {
|
|
|
10
11
|
if (data.sku === '123') {
|
|
11
12
|
console.log('\\o/');
|
|
12
13
|
}
|
|
14
|
+
console.log(data.sku);
|
|
13
15
|
expect(data.sku).toBeTypeOf('string');
|
|
14
16
|
expect(data._id).toBe(productId);
|
|
15
17
|
});
|
|
16
18
|
|
|
19
|
+
test('Find and read product by SKU', async () => {
|
|
20
|
+
const { data } = await api({
|
|
21
|
+
storeId: 1056,
|
|
22
|
+
endpoint: 'products/sku:GFJ4714',
|
|
23
|
+
});
|
|
24
|
+
expect(data.sku).toBe('GFJ4714');
|
|
25
|
+
});
|
|
26
|
+
|
|
17
27
|
test('404 with different Store ID from env', async () => {
|
|
18
|
-
process.env.ECOM_STORE_ID = '1011';
|
|
19
28
|
try {
|
|
20
|
-
const { data } = await api.get(`products/${productId}
|
|
29
|
+
const { data } = await api.get(`products/${productId}`, {
|
|
30
|
+
storeId: 1011,
|
|
31
|
+
});
|
|
21
32
|
console.log(data);
|
|
22
33
|
throw new Error('Should have thrown not found');
|
|
23
34
|
} catch (err: any) {
|
|
@@ -28,6 +39,7 @@ test('404 with different Store ID from env', async () => {
|
|
|
28
39
|
});
|
|
29
40
|
|
|
30
41
|
test('List categories and typecheck result', async () => {
|
|
42
|
+
process.env.ECOM_STORE_ID = '1056';
|
|
31
43
|
const { data } = await api.get('categories', {
|
|
32
44
|
fields: ['name'] as const,
|
|
33
45
|
});
|
|
@@ -80,3 +92,12 @@ test('401 to create category and body typecheck', async () => {
|
|
|
80
92
|
expect(error.response?.status).toBe(401);
|
|
81
93
|
}
|
|
82
94
|
});
|
|
95
|
+
|
|
96
|
+
test('204 to update products views', async () => {
|
|
97
|
+
dotenv.config();
|
|
98
|
+
const isAuthenticating = !!process.env.ECOM_API_KEY;
|
|
99
|
+
const { status } = await api.patch(`products/${productId}`, {
|
|
100
|
+
views: 100,
|
|
101
|
+
});
|
|
102
|
+
expect(status).toBe(isAuthenticating ? 204 : 401);
|
|
103
|
+
});
|
package/types.d.ts
CHANGED
|
@@ -394,19 +394,19 @@ type ResponseBody<
|
|
|
394
394
|
TConfig['method'] extends 'put' | 'patch' | 'delete' ? null :
|
|
395
395
|
// method?: 'get'
|
|
396
396
|
TConfig['endpoint'] extends `${string}/${ResourceId}/${string}` ? any :
|
|
397
|
-
TConfig['endpoint'] extends
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
397
|
+
TConfig['endpoint'] extends ResourceAndFind ?
|
|
398
|
+
TConfig['endpoint'] extends `products/${string}` ? Products :
|
|
399
|
+
TConfig['endpoint'] extends `categories/${string}` ? Categories :
|
|
400
|
+
TConfig['endpoint'] extends `brands/${string}` ? Brands :
|
|
401
|
+
TConfig['endpoint'] extends `collections/${string}` ? Collections :
|
|
402
|
+
TConfig['endpoint'] extends `grids/${string}` ? Grids :
|
|
403
|
+
TConfig['endpoint'] extends `carts/${string}` ? Carts :
|
|
404
|
+
TConfig['endpoint'] extends `orders/${string}` ? Orders :
|
|
405
|
+
TConfig['endpoint'] extends `customers/${string}` ? Customers :
|
|
406
|
+
TConfig['endpoint'] extends `applications/${string}` ? Applications :
|
|
407
|
+
TConfig['endpoint'] extends `authentications/${string}` ? Authentications :
|
|
408
|
+
TConfig['endpoint'] extends `stores/${string}` ? Stores :
|
|
409
|
+
any :
|
|
410
410
|
TConfig['endpoint'] extends ResourceOpQuery ? ResourceListResult<TConfig['endpoint'], ListFields> :
|
|
411
411
|
TConfig['endpoint'] extends SearchOpQuery ? SearchResult<TConfig['endpoint'], ListFields> :
|
|
412
412
|
TConfig['endpoint'] extends SearchHistoryOpQuery ? SearchHistoryResult :
|
|
@@ -431,19 +431,28 @@ type SetDocEndpoint<TResource extends Resource> = TResource | `${TResource}/${Re
|
|
|
431
431
|
|
|
432
432
|
type RequestBody<TConfig extends Config> =
|
|
433
433
|
TConfig['method'] extends undefined | 'get' | 'delete' ? undefined :
|
|
434
|
-
TConfig['
|
|
435
|
-
|
|
436
|
-
TConfig['endpoint'] extends SetDocEndpoint<'
|
|
437
|
-
|
|
438
|
-
TConfig['endpoint'] extends SetDocEndpoint<'brands'>
|
|
439
|
-
|
|
440
|
-
TConfig['endpoint'] extends SetDocEndpoint<'
|
|
441
|
-
|
|
442
|
-
TConfig['endpoint'] extends SetDocEndpoint<'
|
|
443
|
-
|
|
444
|
-
TConfig['endpoint'] extends SetDocEndpoint<'
|
|
445
|
-
|
|
446
|
-
TConfig['endpoint'] extends SetDocEndpoint<'
|
|
434
|
+
TConfig['endpoint'] extends SetDocEndpoint<'products'>
|
|
435
|
+
? TConfig['method'] extends 'patch' ? Partial<ProductSet> : ProductSet :
|
|
436
|
+
TConfig['endpoint'] extends SetDocEndpoint<'categories'>
|
|
437
|
+
? TConfig['method'] extends 'patch' ? Partial<CategorySet> : CategorySet :
|
|
438
|
+
TConfig['endpoint'] extends SetDocEndpoint<'brands'>
|
|
439
|
+
? TConfig['method'] extends 'patch' ? Partial<BrandSet> : BrandSet :
|
|
440
|
+
TConfig['endpoint'] extends SetDocEndpoint<'collections'>
|
|
441
|
+
? TConfig['method'] extends 'patch' ? Partial<CollectionSet> : CollectionSet :
|
|
442
|
+
TConfig['endpoint'] extends SetDocEndpoint<'grids'>
|
|
443
|
+
? TConfig['method'] extends 'patch' ? Partial<GridSet> : GridSet :
|
|
444
|
+
TConfig['endpoint'] extends SetDocEndpoint<'carts'>
|
|
445
|
+
? TConfig['method'] extends 'patch' ? Partial<CartSet> : CartSet :
|
|
446
|
+
TConfig['endpoint'] extends SetDocEndpoint<'orders'>
|
|
447
|
+
? TConfig['method'] extends 'patch' ? Partial<OrderSet> : OrderSet :
|
|
448
|
+
TConfig['endpoint'] extends SetDocEndpoint<'customers'>
|
|
449
|
+
? TConfig['method'] extends 'patch' ? Partial<CustomerSet> : CustomerSet :
|
|
450
|
+
TConfig['endpoint'] extends SetDocEndpoint<'stores'>
|
|
451
|
+
? TConfig['method'] extends 'patch' ? Partial<StoreSet> : StoreSet :
|
|
452
|
+
TConfig['endpoint'] extends SetDocEndpoint<'applications'>
|
|
453
|
+
? TConfig['method'] extends 'patch' ? Partial<ApplicationSet> : ApplicationSet :
|
|
454
|
+
TConfig['endpoint'] extends SetDocEndpoint<'authentications'>
|
|
455
|
+
? TConfig['method'] extends 'patch' ? Partial<AuthenticationSet> : AuthenticationSet :
|
|
447
456
|
any;
|
|
448
457
|
|
|
449
458
|
type ErrorBody = {
|