cloudcommerce 0.0.26 → 0.0.29
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/CHANGELOG.md +30 -0
- package/CONTRIBUTING.md +1 -0
- package/package.json +4 -4
- package/packages/api/lib/index.d.ts +9988 -29
- package/packages/api/lib/index.js +47 -20
- package/packages/api/lib/index.js.map +1 -1
- package/packages/api/lib/types/applications.d.ts +1 -1
- package/packages/api/lib/types/authentications.d.ts +1 -1
- package/packages/api/lib/types/brands.d.ts +1 -1
- package/packages/api/lib/types/carts.d.ts +3 -3
- package/packages/api/lib/types/categories.d.ts +1 -1
- package/packages/api/lib/types/collections.d.ts +1 -1
- package/packages/api/lib/types/customers.d.ts +2 -2
- package/packages/api/lib/types/grids.d.ts +1 -1
- package/packages/api/lib/types/orders.d.ts +5 -5
- package/packages/api/lib/types/products.d.ts +2 -2
- package/packages/api/lib/types/stores.d.ts +1 -1
- package/packages/api/lib/types.d.ts +25 -8
- package/packages/api/package.json +1 -1
- package/packages/api/src/index.ts +78 -29
- package/packages/api/src/types/applications.d.ts +1 -1
- package/packages/api/src/types/authentications.d.ts +1 -1
- package/packages/api/src/types/brands.d.ts +1 -1
- package/packages/api/src/types/carts.d.ts +3 -3
- package/packages/api/src/types/categories.d.ts +1 -1
- package/packages/api/src/types/collections.d.ts +1 -1
- package/packages/api/src/types/customers.d.ts +2 -2
- package/packages/api/src/types/grids.d.ts +1 -1
- package/packages/api/src/types/orders.d.ts +5 -5
- package/packages/api/src/types/products.d.ts +2 -2
- package/packages/api/src/types/stores.d.ts +1 -1
- package/packages/api/src/types.ts +56 -17
- package/packages/api/tests/index.test.ts +16 -0
- package/packages/apps/discounts/package.json +1 -1
- package/packages/cli/create-auth.js +5 -0
- package/packages/cli/lib/create-auth.js +48 -0
- package/packages/cli/lib/index.js +39 -2
- package/packages/cli/lib/login.js +41 -0
- package/packages/cli/package.json +7 -1
- package/packages/cli/src/create-auth.ts +53 -0
- package/packages/cli/src/index.ts +52 -2
- package/packages/cli/src/login.ts +48 -0
- package/packages/firebase/lib/index.js +7 -2
- package/packages/firebase/lib/index.js.map +1 -1
- package/packages/firebase/package.json +1 -1
- package/packages/firebase/src/index.ts +8 -2
- package/packages/storefront/package.json +1 -1
- package/pnpm-lock.yaml +99 -63
|
@@ -13,35 +13,58 @@ class ApiError extends Error {
|
|
|
13
13
|
}
|
|
14
14
|
const def = {
|
|
15
15
|
middleware(config) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
const headers = { ...config.headers };
|
|
17
|
+
if (config.accessToken) {
|
|
18
|
+
// eslint-disable-next-line dot-notation
|
|
19
|
+
headers['Authorization'] = `Bearer ${config.accessToken}`;
|
|
20
|
+
} else if (config.authenticationId && config.apiKey) {
|
|
21
|
+
const rawAuth = `${config.authenticationId}:${config.apiKey}`;
|
|
22
|
+
const base64Auth = typeof Buffer === 'function'
|
|
23
|
+
? Buffer.from(rawAuth).toString('base64') : btoa(rawAuth);
|
|
24
|
+
// eslint-disable-next-line dot-notation
|
|
25
|
+
headers['Authorization'] = `Basic ${base64Auth}`;
|
|
20
26
|
}
|
|
21
|
-
url
|
|
22
|
-
const
|
|
23
|
-
if (
|
|
24
|
-
|
|
27
|
+
let url = config.baseUrl || env.API_BASE_URL || 'https://ecomplus.io/v2';
|
|
28
|
+
const { endpoint, params } = config;
|
|
29
|
+
if (endpoint !== 'login'
|
|
30
|
+
&& endpoint !== 'authenticate'
|
|
31
|
+
&& endpoint !== 'ask-auth-callback'
|
|
32
|
+
&& endpoint !== 'check-username') {
|
|
33
|
+
const storeId = config.storeId || env.ECOM_STORE_ID;
|
|
34
|
+
if (!storeId) {
|
|
35
|
+
throw new Error('`storeId` must be set in config or `ECOM_STORE_ID` env var');
|
|
36
|
+
}
|
|
37
|
+
url += `/:${storeId}`;
|
|
38
|
+
const lang = config.lang || env.ECOM_LANG;
|
|
39
|
+
if (lang) {
|
|
40
|
+
url += `,lang:${lang}`;
|
|
41
|
+
}
|
|
25
42
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
43
|
+
url += `/${endpoint}`;
|
|
44
|
+
if (params) {
|
|
45
|
+
if (typeof params === 'string') {
|
|
46
|
+
url += `?${params}`;
|
|
29
47
|
} else {
|
|
30
48
|
// https://github.com/microsoft/TypeScript/issues/32951
|
|
31
|
-
url += `?${new URLSearchParams(
|
|
49
|
+
url += `?${new URLSearchParams(params)}`;
|
|
32
50
|
}
|
|
33
51
|
}
|
|
34
|
-
return
|
|
52
|
+
return { url, headers };
|
|
35
53
|
},
|
|
36
54
|
};
|
|
37
55
|
const setMiddleware = (middleware) => {
|
|
38
56
|
def.middleware = middleware;
|
|
39
57
|
};
|
|
40
58
|
const api = async (config, retries = 0) => {
|
|
41
|
-
const url = def.middleware(config);
|
|
42
|
-
const {
|
|
43
|
-
|
|
44
|
-
|
|
59
|
+
const { url, headers } = def.middleware(config);
|
|
60
|
+
const { method, timeout = 20000, maxRetries = 3 } = config;
|
|
61
|
+
const bodyObject = config.body || config.data;
|
|
62
|
+
let body;
|
|
63
|
+
if (bodyObject) {
|
|
64
|
+
body = JSON.stringify(bodyObject);
|
|
65
|
+
headers['Content-Type'] = 'application/json';
|
|
66
|
+
headers['Content-Length'] = body.length.toString();
|
|
67
|
+
}
|
|
45
68
|
const abortController = new AbortController();
|
|
46
69
|
let isTimeout = false;
|
|
47
70
|
const timer = setTimeout(() => {
|
|
@@ -53,6 +76,7 @@ const api = async (config, retries = 0) => {
|
|
|
53
76
|
response = await (config.fetch || fetch)(url, {
|
|
54
77
|
method,
|
|
55
78
|
headers,
|
|
79
|
+
body,
|
|
56
80
|
signal: abortController.signal,
|
|
57
81
|
});
|
|
58
82
|
} catch (err) {
|
|
@@ -80,20 +104,23 @@ const api = async (config, retries = 0) => {
|
|
|
80
104
|
throw new ApiError(config, response);
|
|
81
105
|
};
|
|
82
106
|
const get = (endpoint, config) => api({ ...config, endpoint });
|
|
83
|
-
const post = (endpoint, config) => api({
|
|
107
|
+
const post = (endpoint, body, config) => api({
|
|
84
108
|
...config,
|
|
85
109
|
method: 'post',
|
|
86
110
|
endpoint,
|
|
111
|
+
body,
|
|
87
112
|
});
|
|
88
|
-
const put = (endpoint, config) => api({
|
|
113
|
+
const put = (endpoint, body, config) => api({
|
|
89
114
|
...config,
|
|
90
115
|
method: 'put',
|
|
91
116
|
endpoint,
|
|
117
|
+
body,
|
|
92
118
|
});
|
|
93
|
-
const patch = (endpoint, config) => api({
|
|
119
|
+
const patch = (endpoint, body, config) => api({
|
|
94
120
|
...config,
|
|
95
121
|
method: 'patch',
|
|
96
122
|
endpoint,
|
|
123
|
+
body,
|
|
97
124
|
});
|
|
98
125
|
const del = (endpoint, config) => api({
|
|
99
126
|
...config,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,aAAa;AACb,MAAM,GAAG,GAA8B,CAAC,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,CAAC;OACxE,CAAC,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO,IAAI,OAAO,CAAC,GAAG,CAAC;OACvD,EAAE,CAAC;AAER,MAAM,QAAS,SAAQ,KAAK;IAK1B,YAAY,MAAc,EAAE,QAAmB,EAAE,GAAY,EAAE,YAAqB,KAAK;QACvF,KAAK,CAAC,QAAQ,EAAE,UAAU,IAAI,GAAG,IAAI,eAAe,CAAC,CAAC;QACtD,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,UAAU,GAAG,QAAQ,EAAE,MAAM,CAAC;QACnC,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC7B,CAAC;CACF;AAED,MAAM,GAAG,GAAG;IACV,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|
9
9
|
* Shopping cart object model
|
|
10
10
|
*/
|
|
11
11
|
export interface Carts {
|
|
12
|
-
_id: string;
|
|
12
|
+
_id: string & { length: 24 };
|
|
13
13
|
created_at: string;
|
|
14
14
|
updated_at: string;
|
|
15
15
|
store_id: number;
|
|
@@ -175,7 +175,7 @@ export interface Carts {
|
|
|
175
175
|
/**
|
|
176
176
|
* Kit product ID (ObjectID)
|
|
177
177
|
*/
|
|
178
|
-
_id: string;
|
|
178
|
+
_id: string & { length: 24 };
|
|
179
179
|
/**
|
|
180
180
|
* Kit product full name
|
|
181
181
|
*/
|
|
@@ -197,7 +197,7 @@ export interface Carts {
|
|
|
197
197
|
/**
|
|
198
198
|
* Product ID (ObjectID)
|
|
199
199
|
*/
|
|
200
|
-
_id: string;
|
|
200
|
+
_id: string & { length: 24 };
|
|
201
201
|
/**
|
|
202
202
|
* Selected variation ID (ObjectID) if any
|
|
203
203
|
*/
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* Customer object model
|
|
10
10
|
*/
|
|
11
11
|
export interface Customers {
|
|
12
|
-
_id: string;
|
|
12
|
+
_id: string & { length: 24 };
|
|
13
13
|
created_at: string;
|
|
14
14
|
updated_at: string;
|
|
15
15
|
store_id: number;
|
|
@@ -365,7 +365,7 @@ export interface Customers {
|
|
|
365
365
|
/**
|
|
366
366
|
* Order ID (ObjectID)
|
|
367
367
|
*/
|
|
368
|
-
_id: string;
|
|
368
|
+
_id: string & { length: 24 };
|
|
369
369
|
/**
|
|
370
370
|
* When order was saved, date and time in ISO 8601 standard representation
|
|
371
371
|
*/
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* Order object model
|
|
10
10
|
*/
|
|
11
11
|
export interface Orders {
|
|
12
|
-
_id: string;
|
|
12
|
+
_id: string & { length: 24 };
|
|
13
13
|
created_at: string;
|
|
14
14
|
updated_at: string;
|
|
15
15
|
store_id: number;
|
|
@@ -229,7 +229,7 @@ export interface Orders {
|
|
|
229
229
|
/**
|
|
230
230
|
* Customer ID (ObjectID)
|
|
231
231
|
*/
|
|
232
|
-
_id: string;
|
|
232
|
+
_id: string & { length: 24 };
|
|
233
233
|
/**
|
|
234
234
|
* Customer language two letter code, sometimes with region, e.g. 'pt_br', 'fr', 'en_us'
|
|
235
235
|
*/
|
|
@@ -1229,7 +1229,7 @@ export interface Orders {
|
|
|
1229
1229
|
/**
|
|
1230
1230
|
* Kit product ID (ObjectID)
|
|
1231
1231
|
*/
|
|
1232
|
-
_id: string;
|
|
1232
|
+
_id: string & { length: 24 };
|
|
1233
1233
|
/**
|
|
1234
1234
|
* Kit product full name
|
|
1235
1235
|
*/
|
|
@@ -1251,7 +1251,7 @@ export interface Orders {
|
|
|
1251
1251
|
/**
|
|
1252
1252
|
* Product ID (ObjectID)
|
|
1253
1253
|
*/
|
|
1254
|
-
_id: string;
|
|
1254
|
+
_id: string & { length: 24 };
|
|
1255
1255
|
/**
|
|
1256
1256
|
* Selected variation ID (ObjectID) if any
|
|
1257
1257
|
*/
|
|
@@ -1345,7 +1345,7 @@ export interface Orders {
|
|
|
1345
1345
|
/**
|
|
1346
1346
|
* Subscription order ID (ObjectID)
|
|
1347
1347
|
*/
|
|
1348
|
-
_id: string;
|
|
1348
|
+
_id: string & { length: 24 };
|
|
1349
1349
|
/**
|
|
1350
1350
|
* Subscription order number
|
|
1351
1351
|
*/
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
export interface Products {
|
|
9
|
-
_id: string;
|
|
9
|
+
_id: string & { length: 24 };
|
|
10
10
|
created_at: string;
|
|
11
11
|
updated_at: string;
|
|
12
12
|
store_id: number;
|
|
@@ -1179,7 +1179,7 @@ export interface Products {
|
|
|
1179
1179
|
/**
|
|
1180
1180
|
* Product ID (ObjectID)
|
|
1181
1181
|
*/
|
|
1182
|
-
_id: string;
|
|
1182
|
+
_id: string & { length: 24 };
|
|
1183
1183
|
/**
|
|
1184
1184
|
* Product quantity
|
|
1185
1185
|
*/
|
|
@@ -8,22 +8,27 @@ import type { Orders } from './types/orders';
|
|
|
8
8
|
import type { Customers } from './types/customers';
|
|
9
9
|
import type { Stores } from './types/stores';
|
|
10
10
|
import type { Applications } from './types/applications';
|
|
11
|
-
|
|
11
|
+
import type { Authentications } from './types/authentications';
|
|
12
|
+
declare type Resource = 'products' | 'categories' | 'brands' | 'collections' | 'grids' | 'carts' | 'orders' | 'customers' | 'stores' | 'applications' | 'authentications';
|
|
12
13
|
declare type ResourceId = string & {
|
|
13
14
|
length: 24;
|
|
14
15
|
};
|
|
15
16
|
declare type ResourceAndId = `${Resource}/${ResourceId}`;
|
|
17
|
+
declare type ResourceOpQuery = Resource | `${Resource}?${string}`;
|
|
16
18
|
declare type EventsEndpoint = `events/${Resource}` | `events/${ResourceAndId}` | 'events/me';
|
|
17
|
-
declare type Endpoint =
|
|
19
|
+
declare type Endpoint = ResourceOpQuery | ResourceAndId | `${ResourceAndId}/${string}` | `slugs/${string}` | 'search/v1' | EventsEndpoint | 'login' | 'authenticate' | 'ask-auth-callback' | 'check-username' | `$aggregate/${Exclude<Resource, 'stores' | 'applications' | 'authentications'>}` | `schemas/${Resource}`;
|
|
18
20
|
declare type Method = 'get' | 'post' | 'put' | 'patch' | 'delete';
|
|
19
21
|
declare type Config = {
|
|
20
22
|
baseUrl?: string;
|
|
21
23
|
storeId?: number;
|
|
24
|
+
accessToken?: string;
|
|
25
|
+
authenticationId?: string;
|
|
26
|
+
apiKey?: string;
|
|
22
27
|
lang?: string;
|
|
23
28
|
method?: Method;
|
|
24
29
|
endpoint: Endpoint;
|
|
25
30
|
params?: Record<string, string | number>;
|
|
26
|
-
headers?: Record<string, string>;
|
|
31
|
+
headers?: Headers | Record<string, string>;
|
|
27
32
|
timeout?: number;
|
|
28
33
|
maxRetries?: number;
|
|
29
34
|
fetch?: typeof fetch;
|
|
@@ -33,8 +38,9 @@ declare type BaseListResultMeta = {
|
|
|
33
38
|
limit: number;
|
|
34
39
|
fields: Array<string>;
|
|
35
40
|
};
|
|
36
|
-
declare type
|
|
37
|
-
|
|
41
|
+
declare type ListEndpoint<TResource extends Resource> = TResource | `${TResource}?${string}`;
|
|
42
|
+
declare type ResourceListResult<TEndpoint extends ResourceOpQuery> = {
|
|
43
|
+
result: TEndpoint extends ListEndpoint<'products'> ? Products[] : TEndpoint extends ListEndpoint<'categories'> ? Categories[] : TEndpoint extends ListEndpoint<'brands'> ? Brands[] : TEndpoint extends ListEndpoint<'collections'> ? Collections[] : TEndpoint extends ListEndpoint<'grids'> ? Grids[] : TEndpoint extends ListEndpoint<'carts'> ? Carts[] : TEndpoint extends ListEndpoint<'orders'> ? Orders[] : TEndpoint extends ListEndpoint<'customers'> ? Customers[] : TEndpoint extends ListEndpoint<'stores'> ? Stores[] : TEndpoint extends ListEndpoint<'applications'> ? Applications[] : TEndpoint extends ListEndpoint<'authentications'> ? Authentications[] : never;
|
|
38
44
|
meta: BaseListResultMeta & {
|
|
39
45
|
count?: number;
|
|
40
46
|
sort: Array<{
|
|
@@ -71,7 +77,18 @@ declare type EventsResult<TEndpoint extends EventsEndpoint> = {
|
|
|
71
77
|
} & EventFieldsByEndpoint<TEndpoint>>;
|
|
72
78
|
meta: BaseListResultMeta;
|
|
73
79
|
};
|
|
74
|
-
declare type ResponseBody<TConfig extends Config> = TConfig['method'] extends 'post' ? {
|
|
80
|
+
declare type ResponseBody<TConfig extends Config> = TConfig['method'] extends 'post' ? TConfig['endpoint'] extends 'login' ? {
|
|
75
81
|
_id: ResourceId;
|
|
76
|
-
|
|
77
|
-
|
|
82
|
+
store_ids: number[];
|
|
83
|
+
api_key: string;
|
|
84
|
+
} : TConfig['endpoint'] extends 'authenticate' ? {
|
|
85
|
+
my_id: string;
|
|
86
|
+
access_token: string;
|
|
87
|
+
expires: string;
|
|
88
|
+
} : {
|
|
89
|
+
_id: ResourceId;
|
|
90
|
+
} : TConfig['method'] extends 'put' | 'patch' | 'delete' ? null : TConfig['endpoint'] extends `${string}/${ResourceId}/${string}` ? any : TConfig['endpoint'] extends `products/${ResourceId}` ? Products : TConfig['endpoint'] extends `categories/${ResourceId}` ? Categories : TConfig['endpoint'] extends `brands/${ResourceId}` ? Brands : TConfig['endpoint'] extends `collections/${ResourceId}` ? Collections : TConfig['endpoint'] extends `grids/${ResourceId}` ? Grids : TConfig['endpoint'] extends `carts/${ResourceId}` ? Carts : TConfig['endpoint'] extends `orders/${ResourceId}` ? Orders : TConfig['endpoint'] extends `customers/${ResourceId}` ? Customers : TConfig['endpoint'] extends `stores/${ResourceId}` ? Stores : TConfig['endpoint'] extends `applications/${ResourceId}` ? Applications : TConfig['endpoint'] extends `authentications/${ResourceId}` ? Authentications : TConfig['endpoint'] extends ResourceOpQuery ? ResourceListResult<TConfig['endpoint']> : TConfig['endpoint'] extends EventsEndpoint ? EventsResult<TConfig['endpoint']> : any;
|
|
91
|
+
declare type DocSchema<Document extends any> = Omit<Document, '_id' | 'store_id' | 'store_ids' | 'created_at' | 'updated_at'>;
|
|
92
|
+
declare type SetDocEndpoint<TResource extends Resource> = TResource | `${TResource}/${ResourceId}`;
|
|
93
|
+
declare type RequestBody<TConfig extends Config> = TConfig['method'] extends undefined | 'get' | 'delete' ? undefined : TConfig['method'] extends 'patch' ? any : TConfig['endpoint'] extends SetDocEndpoint<'products'> ? DocSchema<Products> : TConfig['endpoint'] extends SetDocEndpoint<'categories'> ? DocSchema<Categories> : TConfig['endpoint'] extends SetDocEndpoint<'brands'> ? DocSchema<Brands> : TConfig['endpoint'] extends SetDocEndpoint<'collections'> ? DocSchema<Collections> : TConfig['endpoint'] extends SetDocEndpoint<'grids'> ? DocSchema<Grids> : TConfig['endpoint'] extends SetDocEndpoint<'carts'> ? DocSchema<Carts> : TConfig['endpoint'] extends SetDocEndpoint<'orders'> ? DocSchema<Orders> : TConfig['endpoint'] extends SetDocEndpoint<'customers'> ? DocSchema<Customers> : TConfig['endpoint'] extends SetDocEndpoint<'stores'> ? DocSchema<Stores> : TConfig['endpoint'] extends SetDocEndpoint<'applications'> ? DocSchema<Applications> : TConfig['endpoint'] extends SetDocEndpoint<'authentications'> ? DocSchema<Authentications> : any;
|
|
94
|
+
export type { Products, Categories, Brands, Collections, Grids, Carts, Orders, Customers, Stores, Applications, Resource, ResourceAndId, ResourceOpQuery, Endpoint, Method, Config, ResponseBody, RequestBody, };
|
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
ResourceOpQuery,
|
|
3
|
+
Endpoint,
|
|
4
|
+
Config,
|
|
5
|
+
ResponseBody,
|
|
6
|
+
RequestBody,
|
|
7
|
+
} from './types';
|
|
2
8
|
|
|
3
9
|
// @ts-ignore
|
|
4
10
|
const env: { [key: string]: string } = (typeof window === 'object' && window)
|
|
@@ -21,25 +27,45 @@ class ApiError extends Error {
|
|
|
21
27
|
|
|
22
28
|
const def = {
|
|
23
29
|
middleware(config: Config) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
30
|
+
const headers: Headers | Record<string, string> = { ...config.headers };
|
|
31
|
+
if (config.accessToken) {
|
|
32
|
+
// eslint-disable-next-line dot-notation
|
|
33
|
+
headers['Authorization'] = `Bearer ${config.accessToken}`;
|
|
34
|
+
} else if (config.authenticationId && config.apiKey) {
|
|
35
|
+
const rawAuth = `${config.authenticationId}:${config.apiKey}`;
|
|
36
|
+
const base64Auth = typeof Buffer === 'function'
|
|
37
|
+
? Buffer.from(rawAuth).toString('base64') : btoa(rawAuth);
|
|
38
|
+
// eslint-disable-next-line dot-notation
|
|
39
|
+
headers['Authorization'] = `Basic ${base64Auth}`;
|
|
28
40
|
}
|
|
29
|
-
url
|
|
30
|
-
const
|
|
31
|
-
if (
|
|
32
|
-
|
|
41
|
+
let url = config.baseUrl || env.API_BASE_URL || 'https://ecomplus.io/v2';
|
|
42
|
+
const { endpoint, params } = config;
|
|
43
|
+
if (
|
|
44
|
+
endpoint !== 'login'
|
|
45
|
+
&& endpoint !== 'authenticate'
|
|
46
|
+
&& endpoint !== 'ask-auth-callback'
|
|
47
|
+
&& endpoint !== 'check-username'
|
|
48
|
+
) {
|
|
49
|
+
const storeId = config.storeId || env.ECOM_STORE_ID;
|
|
50
|
+
if (!storeId) {
|
|
51
|
+
throw new Error('`storeId` must be set in config or `ECOM_STORE_ID` env var');
|
|
52
|
+
}
|
|
53
|
+
url += `/:${storeId}`;
|
|
54
|
+
const lang = config.lang || env.ECOM_LANG;
|
|
55
|
+
if (lang) {
|
|
56
|
+
url += `,lang:${lang}`;
|
|
57
|
+
}
|
|
33
58
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
59
|
+
url += `/${endpoint}`;
|
|
60
|
+
if (params) {
|
|
61
|
+
if (typeof params === 'string') {
|
|
62
|
+
url += `?${params}`;
|
|
37
63
|
} else {
|
|
38
64
|
// https://github.com/microsoft/TypeScript/issues/32951
|
|
39
|
-
url += `?${new URLSearchParams(
|
|
65
|
+
url += `?${new URLSearchParams(params as Record<string, string>)}`;
|
|
40
66
|
}
|
|
41
67
|
}
|
|
42
|
-
return
|
|
68
|
+
return { url, headers };
|
|
43
69
|
},
|
|
44
70
|
};
|
|
45
71
|
|
|
@@ -47,17 +73,25 @@ const setMiddleware = (middleware: typeof def.middleware) => {
|
|
|
47
73
|
def.middleware = middleware;
|
|
48
74
|
};
|
|
49
75
|
|
|
50
|
-
const api = async <T extends Config>(config: T, retries = 0):
|
|
76
|
+
const api = async <T extends Config & { body?: any, data?: any }>(config: T, retries = 0):
|
|
77
|
+
Promise<Response & {
|
|
51
78
|
config: Config,
|
|
52
79
|
data: ResponseBody<T>,
|
|
53
80
|
}> => {
|
|
54
|
-
const url = def.middleware(config);
|
|
81
|
+
const { url, headers } = def.middleware(config);
|
|
55
82
|
const {
|
|
56
83
|
method,
|
|
57
|
-
headers,
|
|
58
84
|
timeout = 20000,
|
|
59
85
|
maxRetries = 3,
|
|
60
86
|
} = config;
|
|
87
|
+
const bodyObject = config.body || config.data;
|
|
88
|
+
let body: string | undefined;
|
|
89
|
+
if (bodyObject) {
|
|
90
|
+
body = JSON.stringify(bodyObject);
|
|
91
|
+
headers['Content-Type'] = 'application/json';
|
|
92
|
+
headers['Content-Length'] = body.length.toString();
|
|
93
|
+
}
|
|
94
|
+
|
|
61
95
|
const abortController = new AbortController();
|
|
62
96
|
let isTimeout = false;
|
|
63
97
|
const timer = setTimeout(() => {
|
|
@@ -69,12 +103,14 @@ const api = async <T extends Config>(config: T, retries = 0): Promise<Response &
|
|
|
69
103
|
response = await (config.fetch || fetch)(url, {
|
|
70
104
|
method,
|
|
71
105
|
headers,
|
|
106
|
+
body,
|
|
72
107
|
signal: abortController.signal,
|
|
73
108
|
});
|
|
74
109
|
} catch (err: any) {
|
|
75
110
|
throw new ApiError(config, response, err.message, isTimeout);
|
|
76
111
|
}
|
|
77
112
|
clearTimeout(timer);
|
|
113
|
+
|
|
78
114
|
if (response) {
|
|
79
115
|
if (response.ok) {
|
|
80
116
|
return {
|
|
@@ -97,6 +133,8 @@ const api = async <T extends Config>(config: T, retries = 0): Promise<Response &
|
|
|
97
133
|
};
|
|
98
134
|
|
|
99
135
|
type AbstractedConfig = Omit<Config, 'endpoint' | 'method'>;
|
|
136
|
+
type AbstractedAuthConfig = AbstractedConfig & { accessToken: string }
|
|
137
|
+
| AbstractedConfig & { authenticationId: string, apiKey: string };
|
|
100
138
|
|
|
101
139
|
const get = <E extends Endpoint, C extends AbstractedConfig>(
|
|
102
140
|
endpoint: E,
|
|
@@ -106,25 +144,36 @@ const get = <E extends Endpoint, C extends AbstractedConfig>(
|
|
|
106
144
|
data: ResponseBody<{ endpoint: E }>,
|
|
107
145
|
}> => api({ ...config, endpoint });
|
|
108
146
|
|
|
109
|
-
const post =
|
|
110
|
-
|
|
111
|
-
method: 'post'
|
|
112
|
-
|
|
113
|
-
|
|
147
|
+
const post = <E extends Endpoint, C extends AbstractedAuthConfig>(
|
|
148
|
+
endpoint: E,
|
|
149
|
+
body: RequestBody<{ endpoint: E, method: 'post' }>,
|
|
150
|
+
config: E extends 'login' | 'authenticate' ? AbstractedConfig : C,
|
|
151
|
+
) => api({
|
|
152
|
+
...config,
|
|
153
|
+
method: 'post',
|
|
154
|
+
endpoint,
|
|
155
|
+
body,
|
|
156
|
+
});
|
|
114
157
|
|
|
115
|
-
const put =
|
|
116
|
-
|
|
117
|
-
method: 'put'
|
|
118
|
-
|
|
119
|
-
|
|
158
|
+
const put = <E extends Exclude<Endpoint, ResourceOpQuery>, C extends AbstractedAuthConfig>(
|
|
159
|
+
endpoint: E,
|
|
160
|
+
body: RequestBody<{ endpoint: E, method: 'put' }>,
|
|
161
|
+
config: C,
|
|
162
|
+
) => api({
|
|
163
|
+
...config,
|
|
164
|
+
method: 'put',
|
|
165
|
+
endpoint,
|
|
166
|
+
body,
|
|
167
|
+
});
|
|
120
168
|
|
|
121
|
-
const patch = (endpoint: Endpoint, config
|
|
169
|
+
const patch = (endpoint: Endpoint, body: any, config: AbstractedAuthConfig) => api({
|
|
122
170
|
...config,
|
|
123
171
|
method: 'patch',
|
|
124
172
|
endpoint,
|
|
173
|
+
body,
|
|
125
174
|
});
|
|
126
175
|
|
|
127
|
-
const del = (endpoint: Endpoint, config
|
|
176
|
+
const del = (endpoint: Endpoint, config: AbstractedAuthConfig) => api({
|
|
128
177
|
...config,
|
|
129
178
|
method: 'delete',
|
|
130
179
|
endpoint,
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* Shopping cart object model
|
|
10
10
|
*/
|
|
11
11
|
export interface Carts {
|
|
12
|
-
_id: string;
|
|
12
|
+
_id: string & { length: 24 };
|
|
13
13
|
created_at: string;
|
|
14
14
|
updated_at: string;
|
|
15
15
|
store_id: number;
|
|
@@ -175,7 +175,7 @@ export interface Carts {
|
|
|
175
175
|
/**
|
|
176
176
|
* Kit product ID (ObjectID)
|
|
177
177
|
*/
|
|
178
|
-
_id: string;
|
|
178
|
+
_id: string & { length: 24 };
|
|
179
179
|
/**
|
|
180
180
|
* Kit product full name
|
|
181
181
|
*/
|
|
@@ -197,7 +197,7 @@ export interface Carts {
|
|
|
197
197
|
/**
|
|
198
198
|
* Product ID (ObjectID)
|
|
199
199
|
*/
|
|
200
|
-
_id: string;
|
|
200
|
+
_id: string & { length: 24 };
|
|
201
201
|
/**
|
|
202
202
|
* Selected variation ID (ObjectID) if any
|
|
203
203
|
*/
|