cloudcommerce 0.0.4 → 0.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/CHANGELOG.md +13 -0
- package/package.json +4 -3
- package/packages/api/lib/index.d.ts +134 -0
- package/packages/api/lib/index.js +89 -0
- package/packages/api/{dist → lib}/index.js.map +1 -1
- package/packages/api/{dist → lib}/types/applications.d.ts +0 -0
- package/packages/api/{dist → lib}/types/authentications.d.ts +0 -0
- package/packages/api/{dist → lib}/types/brands.d.ts +0 -0
- package/packages/api/{dist → lib}/types/carts.d.ts +0 -0
- package/packages/api/{dist → lib}/types/categories.d.ts +0 -0
- package/packages/api/{dist → lib}/types/collections.d.ts +0 -0
- package/packages/api/{dist → lib}/types/customers.d.ts +0 -0
- package/packages/api/{dist → lib}/types/grids.d.ts +0 -0
- package/packages/api/{dist → lib}/types/orders.d.ts +0 -0
- package/packages/api/{dist → lib}/types/products.d.ts +0 -0
- package/packages/api/{dist → lib}/types/stores.d.ts +0 -0
- package/packages/api/{dist → lib}/types.d.ts +6 -32
- package/packages/api/lib/types.js +2 -0
- package/packages/api/{dist → lib}/types.js.map +0 -0
- package/packages/api/package.json +5 -5
- package/packages/api/scripts/build.sh +6 -0
- package/packages/api/src/index.ts +22 -17
- package/packages/api/src/types.ts +13 -11
- package/packages/api/tests/index.test.ts +25 -9
- package/packages/api/tsconfig.json +6 -0
- package/packages/apps/discounts/package.json +1 -1
- package/packages/storefront/package.json +3 -2
- package/tsconfig.json +2 -1
- package/tsconfig.test.json +5 -100
- package/turbo.json +6 -1
- package/vite.config.ts +12 -0
- package/packages/api/dist/index.d.ts +0 -65
- package/packages/api/dist/index.js +0 -89
- package/packages/api/dist/types.js +0 -2
- package/packages/api/scripts/build.mjs +0 -5
- package/packages/api/scripts/test.mjs +0 -4
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
### [0.0.5](https://github.com/ecomplus/cloud-commerce/compare/v0.0.4...v0.0.5) (2022-06-16)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* **api:** Error object (not 2xx) includes `statusCode` property (as Fastify errors) ([a9681f2](https://github.com/ecomplus/cloud-commerce/commit/a9681f23ceb9b007f90e95351ca133fdac865f1b))
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Bug Fixes
|
|
14
|
+
|
|
15
|
+
* **api:** Config object is optional on abstracted method functions ([2eebbe7](https://github.com/ecomplus/cloud-commerce/commit/2eebbe771971b5ac87b85f6068a36d7e1f647aba))
|
|
16
|
+
* **api:** Resolve with resource type on read without method (get is default) ([52b1a9b](https://github.com/ecomplus/cloud-commerce/commit/52b1a9b7cb48210b155ac620eaa6b3c8d95d681e))
|
|
17
|
+
|
|
5
18
|
### [0.0.4](https://github.com/ecomplus/cloud-commerce/compare/v0.0.3...v0.0.4) (2022-06-15)
|
|
6
19
|
|
|
7
20
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cloudcommerce",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "Open fair-code headless commerce platform: API-first, microservices based, event driven and cloud native",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"author": "E-Com Club Softwares para E-commerce <ti@e-com.club>",
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"postinstall": "husky install",
|
|
14
14
|
"build": "turbo run build",
|
|
15
15
|
"test": "turbo run test",
|
|
16
|
-
"release": "pnpm build && standard-version --commit-all",
|
|
16
|
+
"release": "pnpm build && pnpm test && standard-version --commit-all",
|
|
17
17
|
"postpublish": "zx scripts/release.mjs --publish"
|
|
18
18
|
},
|
|
19
19
|
"standard-version": {
|
|
@@ -36,9 +36,10 @@
|
|
|
36
36
|
"husky": "^8.0.1",
|
|
37
37
|
"node-fetch": "^3.2.6",
|
|
38
38
|
"standard-version": "^9.5.0",
|
|
39
|
-
"tsx": "^3.4.2",
|
|
40
39
|
"turbo": "^1.2.16",
|
|
41
40
|
"typescript": "^4.7.3",
|
|
41
|
+
"vite": "^2.9.12",
|
|
42
|
+
"vitest": "^0.15.1",
|
|
42
43
|
"zx": "^6.2.5"
|
|
43
44
|
}
|
|
44
45
|
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import type { Endpoint, Config, ResponseBody } from './types';
|
|
2
|
+
declare const def: {
|
|
3
|
+
middleware(config: Config): string;
|
|
4
|
+
};
|
|
5
|
+
declare const setMiddleware: (middleware: (config: Config) => string) => void;
|
|
6
|
+
declare const api: {
|
|
7
|
+
<T extends Config>(config: T): Promise<Response & {
|
|
8
|
+
config: Config;
|
|
9
|
+
data: ResponseBody<T>;
|
|
10
|
+
}>;
|
|
11
|
+
get: <E extends Endpoint, C extends AbstractedConfig>(endpoint: E, config?: C | undefined) => Promise<Response & {
|
|
12
|
+
config: Config;
|
|
13
|
+
data: {
|
|
14
|
+
endpoint: E;
|
|
15
|
+
} extends infer T_1 ? T_1 extends {
|
|
16
|
+
endpoint: E;
|
|
17
|
+
} ? T_1 extends Config & {
|
|
18
|
+
method?: "get" | undefined;
|
|
19
|
+
endpoint: `products/${string}`;
|
|
20
|
+
} ? import("./types").Products : T_1 extends Config & {
|
|
21
|
+
method?: "get" | undefined;
|
|
22
|
+
endpoint: `categories/${string}`;
|
|
23
|
+
} ? import("./types").Categories : T_1 extends Config & {
|
|
24
|
+
method?: "get" | undefined;
|
|
25
|
+
endpoint: `brands/${string}`;
|
|
26
|
+
} ? import("./types").Brands : T_1 extends Config & {
|
|
27
|
+
method?: "get" | undefined;
|
|
28
|
+
endpoint: `collections/${string}`;
|
|
29
|
+
} ? import("./types").Collections : T_1 extends Config & {
|
|
30
|
+
method?: "get" | undefined;
|
|
31
|
+
endpoint: `grids/${string}`;
|
|
32
|
+
} ? import("./types").Grids : T_1 extends Config & {
|
|
33
|
+
method?: "get" | undefined;
|
|
34
|
+
endpoint: `carts/${string}`;
|
|
35
|
+
} ? import("./types").Carts : T_1 extends Config & {
|
|
36
|
+
method?: "get" | undefined;
|
|
37
|
+
endpoint: `orders/${string}`;
|
|
38
|
+
} ? import("./types").Orders : T_1 extends Config & {
|
|
39
|
+
method?: "get" | undefined;
|
|
40
|
+
endpoint: `customers/${string}`;
|
|
41
|
+
} ? import("./types").Customers : T_1 extends Config & {
|
|
42
|
+
method?: "get" | undefined;
|
|
43
|
+
endpoint: `stores/${string}`;
|
|
44
|
+
} ? import("./types").Stores : T_1 extends Config & {
|
|
45
|
+
method?: "get" | undefined;
|
|
46
|
+
endpoint: `applications/${string}`;
|
|
47
|
+
} ? import("./types").Applications : any : never : never;
|
|
48
|
+
}>;
|
|
49
|
+
post: (endpoint: Endpoint, config?: AbstractedConfig) => Promise<Response & {
|
|
50
|
+
config: Config;
|
|
51
|
+
data: {
|
|
52
|
+
_id: string & {
|
|
53
|
+
length: 24;
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
}>;
|
|
57
|
+
put: (endpoint: Endpoint, config?: AbstractedConfig) => Promise<Response & {
|
|
58
|
+
config: Config;
|
|
59
|
+
data: null;
|
|
60
|
+
}>;
|
|
61
|
+
patch: (endpoint: Endpoint, config?: AbstractedConfig) => Promise<Response & {
|
|
62
|
+
config: Config;
|
|
63
|
+
data: null;
|
|
64
|
+
}>;
|
|
65
|
+
del: (endpoint: Endpoint, config?: AbstractedConfig) => Promise<Response & {
|
|
66
|
+
config: Config;
|
|
67
|
+
data: null;
|
|
68
|
+
}>;
|
|
69
|
+
delete: (endpoint: Endpoint, config?: AbstractedConfig) => Promise<Response & {
|
|
70
|
+
config: Config;
|
|
71
|
+
data: null;
|
|
72
|
+
}>;
|
|
73
|
+
};
|
|
74
|
+
declare type AbstractedConfig = Omit<Config, 'endpoint' | 'method'>;
|
|
75
|
+
declare const get: <E extends Endpoint, C extends AbstractedConfig>(endpoint: E, config?: C | undefined) => Promise<Response & {
|
|
76
|
+
config: Config;
|
|
77
|
+
data: {
|
|
78
|
+
endpoint: E;
|
|
79
|
+
} extends infer T_1 ? T_1 extends {
|
|
80
|
+
endpoint: E;
|
|
81
|
+
} ? T_1 extends Config & {
|
|
82
|
+
method?: "get" | undefined;
|
|
83
|
+
endpoint: `products/${string}`;
|
|
84
|
+
} ? import("./types").Products : T_1 extends Config & {
|
|
85
|
+
method?: "get" | undefined;
|
|
86
|
+
endpoint: `categories/${string}`;
|
|
87
|
+
} ? import("./types").Categories : T_1 extends Config & {
|
|
88
|
+
method?: "get" | undefined;
|
|
89
|
+
endpoint: `brands/${string}`;
|
|
90
|
+
} ? import("./types").Brands : T_1 extends Config & {
|
|
91
|
+
method?: "get" | undefined;
|
|
92
|
+
endpoint: `collections/${string}`;
|
|
93
|
+
} ? import("./types").Collections : T_1 extends Config & {
|
|
94
|
+
method?: "get" | undefined;
|
|
95
|
+
endpoint: `grids/${string}`;
|
|
96
|
+
} ? import("./types").Grids : T_1 extends Config & {
|
|
97
|
+
method?: "get" | undefined;
|
|
98
|
+
endpoint: `carts/${string}`;
|
|
99
|
+
} ? import("./types").Carts : T_1 extends Config & {
|
|
100
|
+
method?: "get" | undefined;
|
|
101
|
+
endpoint: `orders/${string}`;
|
|
102
|
+
} ? import("./types").Orders : T_1 extends Config & {
|
|
103
|
+
method?: "get" | undefined;
|
|
104
|
+
endpoint: `customers/${string}`;
|
|
105
|
+
} ? import("./types").Customers : T_1 extends Config & {
|
|
106
|
+
method?: "get" | undefined;
|
|
107
|
+
endpoint: `stores/${string}`;
|
|
108
|
+
} ? import("./types").Stores : T_1 extends Config & {
|
|
109
|
+
method?: "get" | undefined;
|
|
110
|
+
endpoint: `applications/${string}`;
|
|
111
|
+
} ? import("./types").Applications : any : never : never;
|
|
112
|
+
}>;
|
|
113
|
+
declare const post: (endpoint: Endpoint, config?: AbstractedConfig) => Promise<Response & {
|
|
114
|
+
config: Config;
|
|
115
|
+
data: {
|
|
116
|
+
_id: string & {
|
|
117
|
+
length: 24;
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
}>;
|
|
121
|
+
declare const put: (endpoint: Endpoint, config?: AbstractedConfig) => Promise<Response & {
|
|
122
|
+
config: Config;
|
|
123
|
+
data: null;
|
|
124
|
+
}>;
|
|
125
|
+
declare const patch: (endpoint: Endpoint, config?: AbstractedConfig) => Promise<Response & {
|
|
126
|
+
config: Config;
|
|
127
|
+
data: null;
|
|
128
|
+
}>;
|
|
129
|
+
declare const del: (endpoint: Endpoint, config?: AbstractedConfig) => Promise<Response & {
|
|
130
|
+
config: Config;
|
|
131
|
+
data: null;
|
|
132
|
+
}>;
|
|
133
|
+
export default api;
|
|
134
|
+
export { setMiddleware, get, post, put, patch, del, };
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// @ts-ignore
|
|
2
|
+
const env = (typeof window === 'object' && window)
|
|
3
|
+
|| (typeof process === 'object' && process && process.env)
|
|
4
|
+
|| {};
|
|
5
|
+
const def = {
|
|
6
|
+
middleware(config) {
|
|
7
|
+
let url = config.baseUrl || env.API_BASE_URL || 'https://ecomplus.io/v2';
|
|
8
|
+
const storeId = config.storeId || env.ECOM_STORE_ID;
|
|
9
|
+
if (!storeId) {
|
|
10
|
+
throw new Error('`storeId` must be set in config or `ECOM_STORE_ID` env var');
|
|
11
|
+
}
|
|
12
|
+
url += `/:${storeId}`;
|
|
13
|
+
const lang = config.lang || env.ECOM_LANG;
|
|
14
|
+
if (lang) {
|
|
15
|
+
url += `,lang:${lang}`;
|
|
16
|
+
}
|
|
17
|
+
if (config.params) {
|
|
18
|
+
if (typeof config.params === 'string') {
|
|
19
|
+
url += `?${config.params}`;
|
|
20
|
+
} else {
|
|
21
|
+
// https://github.com/microsoft/TypeScript/issues/32951
|
|
22
|
+
url += `?${new URLSearchParams(config.params)}`;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return `${url}/${config.endpoint}`;
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
// eslint-disable-next-line no-unused-vars
|
|
29
|
+
const setMiddleware = (middleware) => {
|
|
30
|
+
def.middleware = middleware;
|
|
31
|
+
};
|
|
32
|
+
const api = async (config) => {
|
|
33
|
+
const url = def.middleware(config);
|
|
34
|
+
const { method, headers, timeout = 20000 } = config;
|
|
35
|
+
const abortController = new AbortController();
|
|
36
|
+
const timer = setTimeout(() => abortController.abort(), timeout);
|
|
37
|
+
const response = await fetch(url, {
|
|
38
|
+
method,
|
|
39
|
+
headers,
|
|
40
|
+
signal: abortController.signal,
|
|
41
|
+
});
|
|
42
|
+
clearTimeout(timer);
|
|
43
|
+
if (response.ok) {
|
|
44
|
+
return {
|
|
45
|
+
...response,
|
|
46
|
+
config,
|
|
47
|
+
data: await response.json(),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
const error = new Error(response.statusText);
|
|
51
|
+
error.config = config;
|
|
52
|
+
error.response = response;
|
|
53
|
+
error.statusCode = response.status;
|
|
54
|
+
throw error;
|
|
55
|
+
};
|
|
56
|
+
const get = (endpoint, config) => api({ ...config, endpoint });
|
|
57
|
+
const post = (endpoint, config) => api({
|
|
58
|
+
...config,
|
|
59
|
+
method: 'post',
|
|
60
|
+
endpoint,
|
|
61
|
+
});
|
|
62
|
+
const put = (endpoint, config) => api({
|
|
63
|
+
...config,
|
|
64
|
+
method: 'put',
|
|
65
|
+
endpoint,
|
|
66
|
+
});
|
|
67
|
+
const patch = (endpoint, config) => api({
|
|
68
|
+
...config,
|
|
69
|
+
method: 'patch',
|
|
70
|
+
endpoint,
|
|
71
|
+
});
|
|
72
|
+
const del = (endpoint, config) => api({
|
|
73
|
+
...config,
|
|
74
|
+
method: 'delete',
|
|
75
|
+
endpoint,
|
|
76
|
+
});
|
|
77
|
+
api.get = get;
|
|
78
|
+
api.post = post;
|
|
79
|
+
api.put = put;
|
|
80
|
+
api.patch = patch;
|
|
81
|
+
api.del = del;
|
|
82
|
+
api.delete = del;
|
|
83
|
+
|
|
84
|
+
export default api;
|
|
85
|
+
|
|
86
|
+
export {
|
|
87
|
+
setMiddleware, get, post, put, patch, del,
|
|
88
|
+
};
|
|
89
|
+
// # sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,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,GAAG,GAAG;IACV,UAAU,CAAC,MAAc;QACvB,IAAI,GAAG,GAAG,MAAM,CAAC,OAAO,IAAI,GAAG,CAAC,YAAY,IAAI,wBAAwB,CAAC;QACzE,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,IAAI,GAAG,CAAC,aAAa,CAAC;QACpD,IAAI,CAAC,OAAO,EAAE;YACZ,MAAM,IAAI,KAAK,CAAC,4DAA4D,CAAC,CAAC;SAC/E;QACD,GAAG,IAAI,KAAK,OAAO,EAAE,CAAC;QACtB,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,IAAI,GAAG,CAAC,SAAS,CAAC;QAC1C,IAAI,IAAI,EAAE;YACR,GAAG,IAAI,SAAS,IAAI,EAAE,CAAC;SACxB;QACD,IAAI,MAAM,CAAC,MAAM,EAAE;YACjB,IAAI,OAAO,MAAM,CAAC,MAAM,KAAK,QAAQ,EAAE;gBACrC,GAAG,IAAI,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC;aAC5B;iBAAM;gBACL,uDAAuD;gBACvD,GAAG,IAAI,IAAI,IAAI,eAAe,CAAC,MAAM,CAAC,MAAgC,CAAC,EAAE,CAAC;aAC3E;SACF;QACD,OAAO,GAAG,GAAG,IAAI,MAAM,CAAC,QAAQ,EAAE,CAAC;IACrC,CAAC;CACF,CAAC;AAEF,0CAA0C;AAC1C,MAAM,aAAa,GAAG,CAAC,UAAiC,EAAE,EAAE;IAC1D,GAAG,CAAC,UAAU,GAAG,UAAU,CAAC;AAC9B,CAAC,CAAC;AAEF,MAAM,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"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"}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -19,47 +19,21 @@ declare type Config = {
|
|
|
19
19
|
baseUrl?: string;
|
|
20
20
|
storeId?: number;
|
|
21
21
|
lang?: string;
|
|
22
|
-
method
|
|
22
|
+
method?: Method;
|
|
23
23
|
endpoint: Endpoint;
|
|
24
24
|
params?: Record<string, string | number>;
|
|
25
25
|
headers?: Record<string, string>;
|
|
26
26
|
timeout?: number;
|
|
27
27
|
};
|
|
28
|
+
declare type ReadConfig<endpoint> = Config & {
|
|
29
|
+
method?: 'get';
|
|
30
|
+
endpoint: endpoint;
|
|
31
|
+
};
|
|
28
32
|
declare type ResponseBody<T> = T extends Config & {
|
|
29
33
|
method: 'post';
|
|
30
34
|
} ? {
|
|
31
35
|
_id: ResourceId;
|
|
32
36
|
} : T extends Config & {
|
|
33
37
|
method: 'put' | 'patch' | 'delete';
|
|
34
|
-
} ? null : T extends
|
|
35
|
-
method: 'get';
|
|
36
|
-
endpoint: `products/${ResourceId}`;
|
|
37
|
-
} ? Products : T extends Config & {
|
|
38
|
-
method: 'get';
|
|
39
|
-
endpoint: `categories/${ResourceId}`;
|
|
40
|
-
} ? Categories : T extends Config & {
|
|
41
|
-
method: 'get';
|
|
42
|
-
endpoint: `brands/${ResourceId}`;
|
|
43
|
-
} ? Brands : T extends Config & {
|
|
44
|
-
method: 'get';
|
|
45
|
-
endpoint: `collections/${ResourceId}`;
|
|
46
|
-
} ? Collections : T extends Config & {
|
|
47
|
-
method: 'get';
|
|
48
|
-
endpoint: `grids/${ResourceId}`;
|
|
49
|
-
} ? Grids : T extends Config & {
|
|
50
|
-
method: 'get';
|
|
51
|
-
endpoint: `carts/${ResourceId}`;
|
|
52
|
-
} ? Carts : T extends Config & {
|
|
53
|
-
method: 'get';
|
|
54
|
-
endpoint: `orders/${ResourceId}`;
|
|
55
|
-
} ? Orders : T extends Config & {
|
|
56
|
-
method: 'get';
|
|
57
|
-
endpoint: `customers/${ResourceId}`;
|
|
58
|
-
} ? Customers : T extends Config & {
|
|
59
|
-
method: 'get';
|
|
60
|
-
endpoint: `stores/${ResourceId}`;
|
|
61
|
-
} ? Stores : T extends Config & {
|
|
62
|
-
method: 'get';
|
|
63
|
-
endpoint: `applications/${ResourceId}`;
|
|
64
|
-
} ? Applications : any;
|
|
38
|
+
} ? null : T extends ReadConfig<`products/${ResourceId}`> ? Products : T extends ReadConfig<`categories/${ResourceId}`> ? Categories : T extends ReadConfig<`brands/${ResourceId}`> ? Brands : T extends ReadConfig<`collections/${ResourceId}`> ? Collections : T extends ReadConfig<`grids/${ResourceId}`> ? Grids : T extends ReadConfig<`carts/${ResourceId}`> ? Carts : T extends ReadConfig<`orders/${ResourceId}`> ? Orders : T extends ReadConfig<`customers/${ResourceId}`> ? Customers : T extends ReadConfig<`stores/${ResourceId}`> ? Stores : T extends ReadConfig<`applications/${ResourceId}`> ? Applications : any;
|
|
65
39
|
export type { Products, Categories, Brands, Collections, Grids, Carts, Orders, Customers, Stores, Applications, Resource, ResourceAndId, Endpoint, Method, Config, ResponseBody, };
|
|
File without changes
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudcommerce/api",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "E-Com Plus Cloud Commerce APIs client/adapter",
|
|
5
|
-
"main": "
|
|
6
|
-
"types": "
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"types": "lib/index.d.ts",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
9
|
"url": "git+https://github.com/ecomplus/cloud-commerce.git",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
},
|
|
17
17
|
"homepage": "https://github.com/ecomplus/cloud-commerce/tree/main/packages/api#readme",
|
|
18
18
|
"scripts": {
|
|
19
|
-
"build": "
|
|
20
|
-
"test": "
|
|
19
|
+
"build": "sh scripts/build.sh",
|
|
20
|
+
"test": "tsc -p ../../tsconfig.test.json && vitest run"
|
|
21
21
|
}
|
|
22
22
|
}
|
|
@@ -34,7 +34,7 @@ const setMiddleware = (middleware: typeof def.middleware) => {
|
|
|
34
34
|
def.middleware = middleware;
|
|
35
35
|
};
|
|
36
36
|
|
|
37
|
-
const
|
|
37
|
+
const api = async <T extends Config>(config: T): Promise<Response & {
|
|
38
38
|
config: Config,
|
|
39
39
|
data: ResponseBody<T>,
|
|
40
40
|
}> => {
|
|
@@ -58,47 +58,52 @@ const callApi = async <T extends Config>(config: T): Promise<Response & {
|
|
|
58
58
|
const error: any = new Error(response.statusText);
|
|
59
59
|
error.config = config;
|
|
60
60
|
error.response = response;
|
|
61
|
+
error.statusCode = response.status;
|
|
61
62
|
throw error;
|
|
62
63
|
};
|
|
63
64
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
endpoint,
|
|
68
|
-
|
|
65
|
+
type AbstractedConfig = Omit<Config, 'endpoint' | 'method'>;
|
|
66
|
+
|
|
67
|
+
const get = <E extends Endpoint, C extends AbstractedConfig>(
|
|
68
|
+
endpoint: E,
|
|
69
|
+
config?: C,
|
|
70
|
+
): Promise<Response & {
|
|
71
|
+
config: Config,
|
|
72
|
+
data: ResponseBody<{ endpoint: E }>,
|
|
73
|
+
}> => api({ ...config, endpoint });
|
|
69
74
|
|
|
70
|
-
const post = (endpoint: Endpoint, config
|
|
75
|
+
const post = (endpoint: Endpoint, config?: AbstractedConfig) => api({
|
|
71
76
|
...config,
|
|
72
77
|
method: 'post',
|
|
73
78
|
endpoint,
|
|
74
79
|
});
|
|
75
80
|
|
|
76
|
-
const put = (endpoint: Endpoint, config
|
|
81
|
+
const put = (endpoint: Endpoint, config?: AbstractedConfig) => api({
|
|
77
82
|
...config,
|
|
78
83
|
method: 'put',
|
|
79
84
|
endpoint,
|
|
80
85
|
});
|
|
81
86
|
|
|
82
|
-
const patch = (endpoint: Endpoint, config
|
|
87
|
+
const patch = (endpoint: Endpoint, config?: AbstractedConfig) => api({
|
|
83
88
|
...config,
|
|
84
89
|
method: 'patch',
|
|
85
90
|
endpoint,
|
|
86
91
|
});
|
|
87
92
|
|
|
88
|
-
const del = (endpoint: Endpoint, config
|
|
93
|
+
const del = (endpoint: Endpoint, config?: AbstractedConfig) => api({
|
|
89
94
|
...config,
|
|
90
95
|
method: 'delete',
|
|
91
96
|
endpoint,
|
|
92
97
|
});
|
|
93
98
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
99
|
+
api.get = get;
|
|
100
|
+
api.post = post;
|
|
101
|
+
api.put = put;
|
|
102
|
+
api.patch = patch;
|
|
103
|
+
api.del = del;
|
|
104
|
+
api.delete = del;
|
|
100
105
|
|
|
101
|
-
export default
|
|
106
|
+
export default api;
|
|
102
107
|
|
|
103
108
|
export {
|
|
104
109
|
setMiddleware,
|
|
@@ -32,26 +32,28 @@ type Config = {
|
|
|
32
32
|
baseUrl?: string,
|
|
33
33
|
storeId?: number,
|
|
34
34
|
lang?: string,
|
|
35
|
-
method
|
|
35
|
+
method?: Method,
|
|
36
36
|
endpoint: Endpoint,
|
|
37
37
|
params?: Record<string, string | number>,
|
|
38
38
|
headers?: Record<string, string>,
|
|
39
39
|
timeout?: number,
|
|
40
40
|
};
|
|
41
41
|
|
|
42
|
+
type ReadConfig<endpoint> = Config & { method?: 'get', endpoint: endpoint };
|
|
43
|
+
|
|
42
44
|
type ResponseBody<T> =
|
|
43
45
|
T extends Config & { method: 'post' } ? { _id: ResourceId } :
|
|
44
46
|
T extends Config & { method: 'put' | 'patch' | 'delete' } ? null :
|
|
45
|
-
T extends
|
|
46
|
-
T extends
|
|
47
|
-
T extends
|
|
48
|
-
T extends
|
|
49
|
-
T extends
|
|
50
|
-
T extends
|
|
51
|
-
T extends
|
|
52
|
-
T extends
|
|
53
|
-
T extends
|
|
54
|
-
T extends
|
|
47
|
+
T extends ReadConfig<`products/${ResourceId}`> ? Products :
|
|
48
|
+
T extends ReadConfig<`categories/${ResourceId}`> ? Categories :
|
|
49
|
+
T extends ReadConfig<`brands/${ResourceId}`> ? Brands :
|
|
50
|
+
T extends ReadConfig<`collections/${ResourceId}`> ? Collections :
|
|
51
|
+
T extends ReadConfig<`grids/${ResourceId}`> ? Grids :
|
|
52
|
+
T extends ReadConfig<`carts/${ResourceId}`> ? Carts :
|
|
53
|
+
T extends ReadConfig<`orders/${ResourceId}`> ? Orders :
|
|
54
|
+
T extends ReadConfig<`customers/${ResourceId}`> ? Customers :
|
|
55
|
+
T extends ReadConfig<`stores/${ResourceId}`> ? Stores :
|
|
56
|
+
T extends ReadConfig<`applications/${ResourceId}`> ? Applications :
|
|
55
57
|
any
|
|
56
58
|
|
|
57
59
|
export type {
|
|
@@ -1,14 +1,30 @@
|
|
|
1
|
-
/* eslint-disable no-console */
|
|
1
|
+
/* eslint-disable no-console, import/no-extraneous-dependencies */
|
|
2
|
+
|
|
3
|
+
import { test, expect } from 'vitest';
|
|
2
4
|
import './fetch-polyfill';
|
|
3
|
-
import
|
|
5
|
+
import api from '../src/index';
|
|
4
6
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
const productId = '618041aa239b7206d3fc06de';
|
|
8
|
+
test('Read product and typecheck SKU', async () => {
|
|
9
|
+
const { data } = await api({
|
|
10
|
+
storeId: 1056,
|
|
11
|
+
endpoint: `products/${productId}`,
|
|
12
|
+
});
|
|
13
|
+
if (data.sku === '123') {
|
|
11
14
|
console.log('\\o/');
|
|
12
15
|
}
|
|
13
|
-
|
|
16
|
+
expect(data.sku).toBeTypeOf('string');
|
|
17
|
+
expect(data._id).toBe(productId);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test('404 with different Store ID from env', async () => {
|
|
21
|
+
process.env.ECOM_STORE_ID = '1011';
|
|
22
|
+
try {
|
|
23
|
+
const { data } = await api.get(`products/${productId}`);
|
|
24
|
+
console.log(data);
|
|
25
|
+
throw new Error('Should have thrown not found');
|
|
26
|
+
} catch (error: any) {
|
|
27
|
+
expect(error.statusCode).toBe(404);
|
|
28
|
+
expect(error.response?.status).toBe(404);
|
|
29
|
+
}
|
|
14
30
|
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudcommerce/storefront",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "E-Com Plus Cloud Commerce storefront with Astro",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"repository": {
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
"build": "echo '@ecomplus/storefront'"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@cloudcommerce/api": "workspace:*"
|
|
21
|
+
"@cloudcommerce/api": "workspace:*",
|
|
22
|
+
"astro": "1.0.0-beta.46"
|
|
22
23
|
}
|
|
23
24
|
}
|
package/tsconfig.json
CHANGED
package/tsconfig.test.json
CHANGED
|
@@ -1,106 +1,11 @@
|
|
|
1
1
|
{
|
|
2
|
+
"extends": "./tsconfig.json",
|
|
2
3
|
"compilerOptions": {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
/* Projects */
|
|
6
|
-
// "incremental": true, /* Enable incremental compilation */
|
|
7
|
-
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
|
|
8
|
-
// "tsBuildInfoFile": "./", /* Specify the folder for .tsbuildinfo incremental compilation files. */
|
|
9
|
-
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects */
|
|
10
|
-
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
|
|
11
|
-
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
|
12
|
-
|
|
13
|
-
/* Language and Environment */
|
|
14
|
-
"target": "es2020", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
|
|
15
|
-
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
|
16
|
-
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
|
17
|
-
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
|
|
18
|
-
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
|
|
19
|
-
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h' */
|
|
20
|
-
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
|
|
21
|
-
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using `jsx: react-jsx*`.` */
|
|
22
|
-
// "reactNamespace": "", /* Specify the object invoked for `createElement`. This only applies when targeting `react` JSX emit. */
|
|
23
|
-
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
|
|
24
|
-
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
|
25
|
-
|
|
26
|
-
/* Modules */
|
|
27
|
-
"module": "es2020", /* Specify what module code is generated. */
|
|
28
|
-
// "rootDir": "src", /* Specify the root folder within your source files. */
|
|
29
|
-
"moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
|
|
30
|
-
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
|
31
|
-
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
|
32
|
-
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
|
33
|
-
// "typeRoots": ["lib/app/src/@types"], /* Specify multiple folders that act like `./node_modules/@types`. */
|
|
34
|
-
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
|
|
35
|
-
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
|
36
|
-
"resolveJsonModule": true, /* Enable importing .json files */
|
|
37
|
-
// "noResolve": true, /* Disallow `import`s, `require`s or `<reference>`s from expanding the number of files TypeScript should add to a project. */
|
|
38
|
-
|
|
39
|
-
/* JavaScript Support */
|
|
40
|
-
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
|
|
41
|
-
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
|
|
42
|
-
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */
|
|
43
|
-
|
|
44
|
-
/* Emit */
|
|
45
|
-
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
|
46
|
-
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
|
47
|
-
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
|
48
|
-
"sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
|
49
|
-
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
|
|
50
|
-
// "outDir": "dist", /* Specify an output folder for all emitted files. */
|
|
51
|
-
"removeComments": false, /* Disable emitting comments. */
|
|
52
|
-
"noEmit": true, /* Disable emitting files from a compilation. */
|
|
53
|
-
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
|
54
|
-
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */
|
|
55
|
-
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
|
56
|
-
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
|
57
|
-
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
|
58
|
-
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
|
59
|
-
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
|
|
60
|
-
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
|
|
61
|
-
"newLine": "lf", /* Set the newline character for emitting files. */
|
|
62
|
-
// "stripInternal": true, /* Disable emitting declarations that have `@internal` in their JSDoc comments. */
|
|
63
|
-
// "noEmitHelpers": true, /* Disable generating custom helper functions like `__extends` in compiled output. */
|
|
64
|
-
"noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
|
65
|
-
// "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */
|
|
66
|
-
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
|
67
|
-
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
|
|
68
|
-
|
|
69
|
-
/* Interop Constraints */
|
|
70
|
-
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
|
71
|
-
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
|
72
|
-
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */
|
|
73
|
-
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
|
74
|
-
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
|
|
75
|
-
|
|
76
|
-
/* Type Checking */
|
|
77
|
-
"strict": true, /* Enable all strict type-checking options. */
|
|
78
|
-
"noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */
|
|
79
|
-
// "strictNullChecks": true, /* When type checking, take into account `null` and `undefined`. */
|
|
80
|
-
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
|
81
|
-
// "strictBindCallApply": true, /* Check that the arguments for `bind`, `call`, and `apply` methods match the original function. */
|
|
82
|
-
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
|
|
83
|
-
// "noImplicitThis": true, /* Enable error reporting when `this` is given the type `any`. */
|
|
84
|
-
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
|
|
85
|
-
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|
|
86
|
-
// "noUnusedLocals": true, /* Enable error reporting when a local variables aren't read. */
|
|
87
|
-
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read */
|
|
88
|
-
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
|
89
|
-
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
|
|
90
|
-
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
|
|
91
|
-
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
|
|
92
|
-
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
|
|
93
|
-
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type */
|
|
94
|
-
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
|
|
95
|
-
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
|
|
96
|
-
|
|
97
|
-
/* Completeness */
|
|
98
|
-
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
|
99
|
-
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
|
4
|
+
"noEmit": true
|
|
100
5
|
},
|
|
6
|
+
"include": [
|
|
7
|
+
"packages/*/tests/**/*.ts"
|
|
8
|
+
],
|
|
101
9
|
"exclude": [
|
|
102
|
-
"**/dist/**",
|
|
103
|
-
"**/node_modules/**",
|
|
104
|
-
"**/src/**"
|
|
105
10
|
]
|
|
106
11
|
}
|
package/turbo.json
CHANGED
package/vite.config.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/* eslint-disable import/no-extraneous-dependencies */
|
|
2
|
+
/// <reference types="vitest" />
|
|
3
|
+
// Configure Vitest (https://vitest.dev/config/)
|
|
4
|
+
|
|
5
|
+
import { defineConfig } from 'vite';
|
|
6
|
+
|
|
7
|
+
export default defineConfig({
|
|
8
|
+
test: {
|
|
9
|
+
globals: true,
|
|
10
|
+
reporters: 'verbose',
|
|
11
|
+
},
|
|
12
|
+
});
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import type { Endpoint, Config, ResponseBody } from './types';
|
|
2
|
-
declare const def: {
|
|
3
|
-
middleware(config: Config): string;
|
|
4
|
-
};
|
|
5
|
-
declare const setMiddleware: (middleware: (config: Config) => string) => void;
|
|
6
|
-
declare const callApi: {
|
|
7
|
-
<T extends Config>(config: T): Promise<Response & {
|
|
8
|
-
config: Config;
|
|
9
|
-
data: ResponseBody<T>;
|
|
10
|
-
}>;
|
|
11
|
-
get: (endpoint: Endpoint, config: Exclude<Config, 'method'>) => Promise<Response & {
|
|
12
|
-
config: Config;
|
|
13
|
-
data: any;
|
|
14
|
-
}>;
|
|
15
|
-
post: (endpoint: Endpoint, config: Exclude<Config, 'method'>) => Promise<Response & {
|
|
16
|
-
config: Config;
|
|
17
|
-
data: {
|
|
18
|
-
_id: string & {
|
|
19
|
-
length: 24;
|
|
20
|
-
};
|
|
21
|
-
};
|
|
22
|
-
}>;
|
|
23
|
-
put: (endpoint: Endpoint, config: Exclude<Config, 'method'>) => Promise<Response & {
|
|
24
|
-
config: Config;
|
|
25
|
-
data: null;
|
|
26
|
-
}>;
|
|
27
|
-
patch: (endpoint: Endpoint, config: Exclude<Config, 'method'>) => Promise<Response & {
|
|
28
|
-
config: Config;
|
|
29
|
-
data: null;
|
|
30
|
-
}>;
|
|
31
|
-
del: (endpoint: Endpoint, config: Exclude<Config, 'method'>) => Promise<Response & {
|
|
32
|
-
config: Config;
|
|
33
|
-
data: null;
|
|
34
|
-
}>;
|
|
35
|
-
delete: (endpoint: Endpoint, config: Exclude<Config, 'method'>) => Promise<Response & {
|
|
36
|
-
config: Config;
|
|
37
|
-
data: null;
|
|
38
|
-
}>;
|
|
39
|
-
};
|
|
40
|
-
declare const get: (endpoint: Endpoint, config: Exclude<Config, 'method'>) => Promise<Response & {
|
|
41
|
-
config: Config;
|
|
42
|
-
data: any;
|
|
43
|
-
}>;
|
|
44
|
-
declare const post: (endpoint: Endpoint, config: Exclude<Config, 'method'>) => Promise<Response & {
|
|
45
|
-
config: Config;
|
|
46
|
-
data: {
|
|
47
|
-
_id: string & {
|
|
48
|
-
length: 24;
|
|
49
|
-
};
|
|
50
|
-
};
|
|
51
|
-
}>;
|
|
52
|
-
declare const put: (endpoint: Endpoint, config: Exclude<Config, 'method'>) => Promise<Response & {
|
|
53
|
-
config: Config;
|
|
54
|
-
data: null;
|
|
55
|
-
}>;
|
|
56
|
-
declare const patch: (endpoint: Endpoint, config: Exclude<Config, 'method'>) => Promise<Response & {
|
|
57
|
-
config: Config;
|
|
58
|
-
data: null;
|
|
59
|
-
}>;
|
|
60
|
-
declare const del: (endpoint: Endpoint, config: Exclude<Config, 'method'>) => Promise<Response & {
|
|
61
|
-
config: Config;
|
|
62
|
-
data: null;
|
|
63
|
-
}>;
|
|
64
|
-
export default callApi;
|
|
65
|
-
export { setMiddleware, get, post, put, patch, del, };
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
// @ts-ignore
|
|
2
|
-
const env = (typeof window === 'object' && window)
|
|
3
|
-
|| (typeof process === 'object' && process && process.env)
|
|
4
|
-
|| {};
|
|
5
|
-
const def = {
|
|
6
|
-
middleware(config) {
|
|
7
|
-
let url = config.baseUrl || env.API_BASE_URL || 'https://ecomplus.io/v2';
|
|
8
|
-
const storeId = config.storeId || env.ECOM_STORE_ID;
|
|
9
|
-
if (!storeId) {
|
|
10
|
-
throw new Error('`storeId` must be set in config or `ECOM_STORE_ID` env var');
|
|
11
|
-
}
|
|
12
|
-
url += `/:${storeId}`;
|
|
13
|
-
const lang = config.lang || env.ECOM_LANG;
|
|
14
|
-
if (lang) {
|
|
15
|
-
url += `,lang:${lang}`;
|
|
16
|
-
}
|
|
17
|
-
if (config.params) {
|
|
18
|
-
if (typeof config.params === 'string') {
|
|
19
|
-
url += `?${config.params}`;
|
|
20
|
-
}
|
|
21
|
-
else {
|
|
22
|
-
// https://github.com/microsoft/TypeScript/issues/32951
|
|
23
|
-
url += `?${new URLSearchParams(config.params)}`;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
return `${url}/${config.endpoint}`;
|
|
27
|
-
},
|
|
28
|
-
};
|
|
29
|
-
// eslint-disable-next-line no-unused-vars
|
|
30
|
-
const setMiddleware = (middleware) => {
|
|
31
|
-
def.middleware = middleware;
|
|
32
|
-
};
|
|
33
|
-
const callApi = async (config) => {
|
|
34
|
-
const url = def.middleware(config);
|
|
35
|
-
const { method, headers, timeout = 20000 } = config;
|
|
36
|
-
const abortController = new AbortController();
|
|
37
|
-
const timer = setTimeout(() => abortController.abort(), timeout);
|
|
38
|
-
const response = await fetch(url, {
|
|
39
|
-
method,
|
|
40
|
-
headers,
|
|
41
|
-
signal: abortController.signal,
|
|
42
|
-
});
|
|
43
|
-
clearTimeout(timer);
|
|
44
|
-
if (response.ok) {
|
|
45
|
-
return {
|
|
46
|
-
...response,
|
|
47
|
-
config,
|
|
48
|
-
data: await response.json(),
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
const error = new Error(response.statusText);
|
|
52
|
-
error.config = config;
|
|
53
|
-
error.response = response;
|
|
54
|
-
throw error;
|
|
55
|
-
};
|
|
56
|
-
const get = (endpoint, config) => callApi({
|
|
57
|
-
...config,
|
|
58
|
-
method: 'get',
|
|
59
|
-
endpoint,
|
|
60
|
-
});
|
|
61
|
-
const post = (endpoint, config) => callApi({
|
|
62
|
-
...config,
|
|
63
|
-
method: 'post',
|
|
64
|
-
endpoint,
|
|
65
|
-
});
|
|
66
|
-
const put = (endpoint, config) => callApi({
|
|
67
|
-
...config,
|
|
68
|
-
method: 'put',
|
|
69
|
-
endpoint,
|
|
70
|
-
});
|
|
71
|
-
const patch = (endpoint, config) => callApi({
|
|
72
|
-
...config,
|
|
73
|
-
method: 'patch',
|
|
74
|
-
endpoint,
|
|
75
|
-
});
|
|
76
|
-
const del = (endpoint, config) => callApi({
|
|
77
|
-
...config,
|
|
78
|
-
method: 'delete',
|
|
79
|
-
endpoint,
|
|
80
|
-
});
|
|
81
|
-
callApi.get = get;
|
|
82
|
-
callApi.post = post;
|
|
83
|
-
callApi.put = put;
|
|
84
|
-
callApi.patch = patch;
|
|
85
|
-
callApi.del = del;
|
|
86
|
-
callApi.delete = del;
|
|
87
|
-
export default callApi;
|
|
88
|
-
export { setMiddleware, get, post, put, patch, del, };
|
|
89
|
-
//# sourceMappingURL=index.js.map
|