@cloudcommerce/api 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.
@@ -1,8 +1,5 @@
1
- @cloudcommerce/api:build: cache hit, replaying output 95f3926a68635b20
1
+ @cloudcommerce/api:build: cache hit, replaying output 6f3e88d4d63cc69e
2
2
  @cloudcommerce/api:build: 
3
- @cloudcommerce/api:build: > @cloudcommerce/api@0.0.3 build /home/leo/code/ecomplus/cloud-commerce/packages/api
4
- @cloudcommerce/api:build: > zx scripts/build.mjs
3
+ @cloudcommerce/api:build: > @cloudcommerce/api@0.0.4 build /home/leo/code/ecomplus/cloud-commerce/packages/api
4
+ @cloudcommerce/api:build: > sh scripts/build.sh
5
5
  @cloudcommerce/api:build: 
6
- @cloudcommerce/api:build: $ rm -rf dist
7
- @cloudcommerce/api:build: $ tsc -p ../../tsconfig.json --outDir dist --declaration
8
- @cloudcommerce/api:build: $ cp -r src/types dist/
@@ -1,8 +1,15 @@
1
- @cloudcommerce/api:test: cache hit, replaying output b689b985522783d4
2
- @cloudcommerce/api:test: 
3
- @cloudcommerce/api:test: > @cloudcommerce/api@0.0.3 test /home/leo/code/ecomplus/cloud-commerce/packages/api
4
- @cloudcommerce/api:test: > zx scripts/test.mjs
5
- @cloudcommerce/api:test: 
6
- @cloudcommerce/api:test: $ tsc -p ../../tsconfig.test.json
7
- @cloudcommerce/api:test: $ tsx tests/index.test.ts
8
- @cloudcommerce/api:test: ✓ Read product GFJ4714 and checked SKU type string
1
+ @cloudcommerce/api:test: cache hit, replaying output 11fdf26ab00d55b5
2
+ @cloudcommerce/api:test: 
3
+ @cloudcommerce/api:test: > @cloudcommerce/api@0.0.4 test /home/leo/code/ecomplus/cloud-commerce/packages/api
4
+ @cloudcommerce/api:test: > tsc -p ../../tsconfig.test.json && vitest run
5
+ @cloudcommerce/api:test: 
6
+ @cloudcommerce/api:test: 
7
+ @cloudcommerce/api:test:  RUN v0.15.1 /home/leo/code/ecomplus/cloud-commerce/packages/api
8
+ @cloudcommerce/api:test: 
9
+ @cloudcommerce/api:test:  ✓ tests/index.test.ts > Read product and typecheck SKU
10
+ @cloudcommerce/api:test:  ✓ tests/index.test.ts > 404 with different Store ID from env
11
+ @cloudcommerce/api:test: 
12
+ @cloudcommerce/api:test: Test Files 1 passed (1)
13
+ @cloudcommerce/api:test:  Tests 2 passed (2)
14
+ @cloudcommerce/api:test:  Time 2.15s (in thread 804ms, 267.51%)
15
+ @cloudcommerce/api:test: 
package/lib/index.d.ts ADDED
@@ -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, };
package/lib/index.js ADDED
@@ -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,QA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M,GAAG,GAAG,CAAC;AAEjB,eAAe,GAAG,CAAC;AAEnB,OAAO,EACL,aAAa,EACb,GAAG,EACH,IAAI,EACJ,GAAG,EACH,KAAK,EACL,GAAG,GACJ,CAAC"}
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: 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 Config & {
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, };
package/lib/types.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ // # sourceMappingURL=types.js.map
File without changes
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@cloudcommerce/api",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "E-Com Plus Cloud Commerce APIs client/adapter",
5
- "main": "dist/index.js",
6
- "types": "dist/index.d.ts",
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": "zx scripts/build.mjs",
20
- "test": "zx scripts/test.mjs"
19
+ "build": "sh scripts/build.sh",
20
+ "test": "tsc -p ../../tsconfig.test.json && vitest run"
21
21
  }
22
22
  }
@@ -0,0 +1,6 @@
1
+ #!/bin/bash
2
+
3
+ rm -rf lib
4
+ tsc --outDir lib
5
+ cp -r src/types lib/
6
+ npx eslint --rule 'max-len: off' --ext .js lib --fix
package/src/index.ts CHANGED
@@ -34,7 +34,7 @@ const setMiddleware = (middleware: typeof def.middleware) => {
34
34
  def.middleware = middleware;
35
35
  };
36
36
 
37
- const callApi = async <T extends Config>(config: T): Promise<Response & {
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
- const get = (endpoint: Endpoint, config: Exclude<Config, 'method'>) => callApi({
65
- ...config,
66
- method: 'get',
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: Exclude<Config, 'method'>) => callApi({
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: Exclude<Config, 'method'>) => callApi({
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: Exclude<Config, 'method'>) => callApi({
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: Exclude<Config, 'method'>) => callApi({
93
+ const del = (endpoint: Endpoint, config?: AbstractedConfig) => api({
89
94
  ...config,
90
95
  method: 'delete',
91
96
  endpoint,
92
97
  });
93
98
 
94
- callApi.get = get;
95
- callApi.post = post;
96
- callApi.put = put;
97
- callApi.patch = patch;
98
- callApi.del = del;
99
- callApi.delete = del;
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 callApi;
106
+ export default api;
102
107
 
103
108
  export {
104
109
  setMiddleware,
package/src/types.ts CHANGED
@@ -32,26 +32,28 @@ type Config = {
32
32
  baseUrl?: string,
33
33
  storeId?: number,
34
34
  lang?: string,
35
- method: 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 Config & { method: 'get', endpoint: `products/${ResourceId}` } ? Products :
46
- T extends Config & { method: 'get', endpoint: `categories/${ResourceId}` } ? Categories :
47
- T extends Config & { method: 'get', endpoint: `brands/${ResourceId}` } ? Brands :
48
- T extends Config & { method: 'get', endpoint: `collections/${ResourceId}` } ? Collections :
49
- T extends Config & { method: 'get', endpoint: `grids/${ResourceId}` } ? Grids :
50
- T extends Config & { method: 'get', endpoint: `carts/${ResourceId}` } ? Carts :
51
- T extends Config & { method: 'get', endpoint: `orders/${ResourceId}` } ? Orders :
52
- T extends Config & { method: 'get', endpoint: `customers/${ResourceId}` } ? Customers :
53
- T extends Config & { method: 'get', endpoint: `stores/${ResourceId}` } ? Stores :
54
- T extends Config & { method: 'get', endpoint: `applications/${ResourceId}` } ? Applications :
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 callApi from '../src/index';
5
+ import api from '../src/index';
4
6
 
5
- callApi({
6
- storeId: 1056,
7
- method: 'get',
8
- endpoint: 'products/618041aa239b7206d3fc06de',
9
- }).then(({ data }) => {
10
- if (data.sku === 'string') {
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
- console.info(`✓ Read product ${data.sku} and checked SKU type string`);
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
  });
package/tsconfig.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "declaration": true
5
+ }
6
+ }
package/dist/index.d.ts DELETED
@@ -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, };
package/dist/index.js DELETED
@@ -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
package/dist/types.js DELETED
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=types.js.map
package/scripts/build.mjs DELETED
@@ -1,5 +0,0 @@
1
- /* global $ */
2
-
3
- await $`rm -rf dist`;
4
- await $`tsc -p ../../tsconfig.json --outDir dist --declaration`;
5
- await $`cp -r src/types dist/`;
package/scripts/test.mjs DELETED
@@ -1,4 +0,0 @@
1
- /* global $ */
2
-
3
- await $`tsc -p ../../tsconfig.test.json`;
4
- await $`tsx tests/index.test.ts`;