cloudcommerce 0.0.6 → 0.0.7

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/.eslintrc.cjs CHANGED
@@ -26,6 +26,8 @@ module.exports = {
26
26
  'no-param-reassign': 'warn',
27
27
  'no-underscore-dangle': 'off',
28
28
  'no-continue': 'off',
29
+ 'lines-between-class-members': 'off',
30
+ 'arrow-body-style': 'off',
29
31
  'import/extensions': [
30
32
  'error',
31
33
  'ignorePackages',
@@ -42,7 +44,6 @@ module.exports = {
42
44
  { blankLine: 'never', prev: 'directive', next: 'directive' },
43
45
  { blankLine: 'never', prev: 'import', next: 'import' },
44
46
  ],
45
- 'arrow-body-style': 'off',
46
47
  'import/order': [
47
48
  'error',
48
49
  { groups: ['type', 'builtin', 'external', 'parent', 'sibling', 'index'] },
package/CHANGELOG.md CHANGED
@@ -2,6 +2,21 @@
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.7](https://github.com/ecomplus/cloud-commerce/compare/v0.0.6...v0.0.7) (2022-06-21)
6
+
7
+
8
+ ### Features
9
+
10
+ * **api:** Add `fetch` option to config ([9ca0f68](https://github.com/ecomplus/cloud-commerce/commit/9ca0f681b003132574a6f147d406c8dddab93261))
11
+ * **api:** Add events, slug, search and auth endpoints to type ([9d4f712](https://github.com/ecomplus/cloud-commerce/commit/9d4f71217e73e22b941cfea855416a86e7bbc9a2))
12
+ * **api:** New (exported) `ApiError` class ([8e88494](https://github.com/ecomplus/cloud-commerce/commit/8e884944505caf3861aef4bce440f7fd0ed937a4))
13
+ * **api:** Type definiftion for list requests (`/${resource}`) ([21d71ff](https://github.com/ecomplus/cloud-commerce/commit/21d71ff56ac1220d16324f1018a9804b7f5cf3c3))
14
+
15
+
16
+ ### Bug Fixes
17
+
18
+ * **api:** Event `timestamp` retrieved as date iso string (not object) ([aa18cc7](https://github.com/ecomplus/cloud-commerce/commit/aa18cc7e8cf0d8fb3345da7e48ddc15f4bb2209e))
19
+
5
20
  ### [0.0.6](https://github.com/ecomplus/cloud-commerce/compare/v0.0.5...v0.0.6) (2022-06-16)
6
21
 
7
22
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cloudcommerce",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
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>",
@@ -25,11 +25,11 @@
25
25
  "@commitlint/cli": "^17.0.2",
26
26
  "@commitlint/config-conventional": "^17.0.2",
27
27
  "@commitlint/rules": "^17.0.0",
28
- "@types/node": "^17.0.42",
29
- "@typescript-eslint/eslint-plugin": "^5.28.0",
30
- "@typescript-eslint/parser": "^5.28.0",
31
- "esbuild": "^0.14.43",
32
- "eslint": "^8.17.0",
28
+ "@types/node": "^17.0.45",
29
+ "@typescript-eslint/eslint-plugin": "^5.29.0",
30
+ "@typescript-eslint/parser": "^5.29.0",
31
+ "esbuild": "^0.14.47",
32
+ "eslint": "^8.18.0",
33
33
  "eslint-config-airbnb-base": "^15.0.0",
34
34
  "eslint-plugin-import": "^2.26.0",
35
35
  "eslint-plugin-vue": "^9.1.1",
@@ -37,9 +37,9 @@
37
37
  "node-fetch": "^3.2.6",
38
38
  "standard-version": "^9.5.0",
39
39
  "turbo": "^1.2.16",
40
- "typescript": "^4.7.3",
40
+ "typescript": "^4.7.4",
41
41
  "vite": "^2.9.12",
42
- "vitest": "^0.15.1",
42
+ "vitest": "^0.15.2",
43
43
  "zx": "^6.2.5"
44
44
  }
45
45
  }
@@ -1,4 +1,11 @@
1
1
  import type { Endpoint, Config, ResponseBody } from './types';
2
+ declare class ApiError extends Error {
3
+ config: Config;
4
+ response?: Response;
5
+ statusCode?: number;
6
+ isTimeout: boolean;
7
+ constructor(config: Config, response?: Response, msg?: string, isTimeout?: boolean);
8
+ }
2
9
  declare const def: {
3
10
  middleware(config: Config): string;
4
11
  };
@@ -10,41 +17,62 @@ declare const api: {
10
17
  }>;
11
18
  get: <E extends Endpoint, C extends AbstractedConfig>(endpoint: E, config?: C | undefined) => Promise<Response & {
12
19
  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;
20
+ data: E extends `products/${string}` ? import("./types").Products : E extends `categories/${string}` ? import("./types").Categories : E extends `brands/${string}` ? import("./types").Brands : E extends `collections/${string}` ? import("./types").Collections : E extends `grids/${string}` ? import("./types").Grids : E extends `carts/${string}` ? import("./types").Carts : E extends `orders/${string}` ? import("./types").Orders : E extends `customers/${string}` ? import("./types").Customers : E extends `stores/${string}` ? import("./types").Stores : E extends `applications/${string}` ? import("./types").Applications : E extends import("./types").Resource ? {
21
+ result: E extends infer T_1 ? T_1 extends E ? T_1 extends "products" ? import("./types").Products[] : T_1 extends "categories" ? import("./types").Categories[] : T_1 extends "brands" ? import("./types").Brands[] : T_1 extends "collections" ? import("./types").Collections[] : T_1 extends "grids" ? import("./types").Grids[] : T_1 extends "carts" ? import("./types").Carts[] : T_1 extends "orders" ? import("./types").Orders[] : T_1 extends "customers" ? import("./types").Customers[] : T_1 extends "stores" ? import("./types").Stores[] : T_1 extends "applications" ? import("./types").Applications[] : never : never : never;
22
+ meta: {
23
+ offset: number;
24
+ limit: number;
25
+ fields: string[];
26
+ } & {
27
+ count?: number | undefined;
28
+ sort: {
29
+ field: string;
30
+ order: 1 | -1;
31
+ }[];
32
+ query: {
33
+ [key: string]: any;
34
+ };
35
+ };
36
+ } : E extends "events/products" | "events/categories" | "events/brands" | "events/collections" | "events/grids" | "events/carts" | "events/orders" | "events/customers" | "events/stores" | "events/applications" | `events/products/${string}` | `events/categories/${string}` | `events/brands/${string}` | `events/collections/${string}` | `events/grids/${string}` | `events/carts/${string}` | `events/orders/${string}` | `events/customers/${string}` | `events/stores/${string}` | `events/applications/${string}` | "events/me" ? {
37
+ result: ({
38
+ timestamp: string;
39
+ store_id?: number | undefined;
40
+ resource?: string | undefined;
41
+ authentication_id?: (string & {
42
+ length: 24;
43
+ }) | null | undefined;
44
+ resource_id?: (string & {
45
+ length: 24;
46
+ }) | undefined;
47
+ action: string;
48
+ modified_fields: string[];
49
+ method?: number | undefined;
50
+ endpoint?: string | undefined;
51
+ body?: any;
52
+ ip?: string | undefined;
53
+ } & (E extends infer T_2 ? T_2 extends E ? T_2 extends "events/products" | "events/categories" | "events/brands" | "events/collections" | "events/grids" | "events/carts" | "events/orders" | "events/customers" | "events/stores" | "events/applications" ? {
54
+ resource_id: string & {
55
+ length: 24;
56
+ };
57
+ authentication_id: (string & {
58
+ length: 24;
59
+ }) | null;
60
+ } : T_2 extends `events/products/${string}` | `events/categories/${string}` | `events/brands/${string}` | `events/collections/${string}` | `events/grids/${string}` | `events/carts/${string}` | `events/orders/${string}` | `events/customers/${string}` | `events/stores/${string}` | `events/applications/${string}` ? {
61
+ authentication_id: (string & {
62
+ length: 24;
63
+ }) | null;
64
+ } : T_2 extends "events/me" ? {
65
+ resource: import("./types").Resource;
66
+ resource_id: string & {
67
+ length: 24;
68
+ };
69
+ } : never : never : never))[];
70
+ meta: {
71
+ offset: number;
72
+ limit: number;
73
+ fields: string[];
74
+ };
75
+ } : any;
48
76
  }>;
49
77
  post: (endpoint: Endpoint, config?: AbstractedConfig) => Promise<Response & {
50
78
  config: Config;
@@ -74,41 +102,62 @@ declare const api: {
74
102
  declare type AbstractedConfig = Omit<Config, 'endpoint' | 'method'>;
75
103
  declare const get: <E extends Endpoint, C extends AbstractedConfig>(endpoint: E, config?: C | undefined) => Promise<Response & {
76
104
  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;
105
+ data: E extends `products/${string}` ? import("./types").Products : E extends `categories/${string}` ? import("./types").Categories : E extends `brands/${string}` ? import("./types").Brands : E extends `collections/${string}` ? import("./types").Collections : E extends `grids/${string}` ? import("./types").Grids : E extends `carts/${string}` ? import("./types").Carts : E extends `orders/${string}` ? import("./types").Orders : E extends `customers/${string}` ? import("./types").Customers : E extends `stores/${string}` ? import("./types").Stores : E extends `applications/${string}` ? import("./types").Applications : E extends import("./types").Resource ? {
106
+ result: E extends infer T_1 ? T_1 extends E ? T_1 extends "products" ? import("./types").Products[] : T_1 extends "categories" ? import("./types").Categories[] : T_1 extends "brands" ? import("./types").Brands[] : T_1 extends "collections" ? import("./types").Collections[] : T_1 extends "grids" ? import("./types").Grids[] : T_1 extends "carts" ? import("./types").Carts[] : T_1 extends "orders" ? import("./types").Orders[] : T_1 extends "customers" ? import("./types").Customers[] : T_1 extends "stores" ? import("./types").Stores[] : T_1 extends "applications" ? import("./types").Applications[] : never : never : never;
107
+ meta: {
108
+ offset: number;
109
+ limit: number;
110
+ fields: string[];
111
+ } & {
112
+ count?: number | undefined;
113
+ sort: {
114
+ field: string;
115
+ order: 1 | -1;
116
+ }[];
117
+ query: {
118
+ [key: string]: any;
119
+ };
120
+ };
121
+ } : E extends "events/products" | "events/categories" | "events/brands" | "events/collections" | "events/grids" | "events/carts" | "events/orders" | "events/customers" | "events/stores" | "events/applications" | `events/products/${string}` | `events/categories/${string}` | `events/brands/${string}` | `events/collections/${string}` | `events/grids/${string}` | `events/carts/${string}` | `events/orders/${string}` | `events/customers/${string}` | `events/stores/${string}` | `events/applications/${string}` | "events/me" ? {
122
+ result: ({
123
+ timestamp: string;
124
+ store_id?: number | undefined;
125
+ resource?: string | undefined;
126
+ authentication_id?: (string & {
127
+ length: 24;
128
+ }) | null | undefined;
129
+ resource_id?: (string & {
130
+ length: 24;
131
+ }) | undefined;
132
+ action: string;
133
+ modified_fields: string[];
134
+ method?: number | undefined;
135
+ endpoint?: string | undefined;
136
+ body?: any;
137
+ ip?: string | undefined;
138
+ } & (E extends infer T_2 ? T_2 extends E ? T_2 extends "events/products" | "events/categories" | "events/brands" | "events/collections" | "events/grids" | "events/carts" | "events/orders" | "events/customers" | "events/stores" | "events/applications" ? {
139
+ resource_id: string & {
140
+ length: 24;
141
+ };
142
+ authentication_id: (string & {
143
+ length: 24;
144
+ }) | null;
145
+ } : T_2 extends `events/products/${string}` | `events/categories/${string}` | `events/brands/${string}` | `events/collections/${string}` | `events/grids/${string}` | `events/carts/${string}` | `events/orders/${string}` | `events/customers/${string}` | `events/stores/${string}` | `events/applications/${string}` ? {
146
+ authentication_id: (string & {
147
+ length: 24;
148
+ }) | null;
149
+ } : T_2 extends "events/me" ? {
150
+ resource: import("./types").Resource;
151
+ resource_id: string & {
152
+ length: 24;
153
+ };
154
+ } : never : never : never))[];
155
+ meta: {
156
+ offset: number;
157
+ limit: number;
158
+ fields: string[];
159
+ };
160
+ } : any;
112
161
  }>;
113
162
  declare const post: (endpoint: Endpoint, config?: AbstractedConfig) => Promise<Response & {
114
163
  config: Config;
@@ -131,4 +180,4 @@ declare const del: (endpoint: Endpoint, config?: AbstractedConfig) => Promise<Re
131
180
  data: null;
132
181
  }>;
133
182
  export default api;
134
- export { setMiddleware, get, post, put, patch, del, };
183
+ export { setMiddleware, get, post, put, patch, del, ApiError, };
@@ -2,6 +2,15 @@
2
2
  const env = (typeof window === 'object' && window)
3
3
  || (typeof process === 'object' && process && process.env)
4
4
  || {};
5
+ class ApiError extends Error {
6
+ constructor(config, response, msg, isTimeout = false) {
7
+ super(response?.statusText || msg || 'Request error');
8
+ this.config = config;
9
+ this.response = response;
10
+ this.statusCode = response?.status;
11
+ this.isTimeout = isTimeout;
12
+ }
13
+ }
5
14
  const def = {
6
15
  middleware(config) {
7
16
  let url = config.baseUrl || env.API_BASE_URL || 'https://ecomplus.io/v2';
@@ -25,7 +34,6 @@ const def = {
25
34
  return `${url}/${config.endpoint}`;
26
35
  },
27
36
  };
28
- // eslint-disable-next-line no-unused-vars
29
37
  const setMiddleware = (middleware) => {
30
38
  def.middleware = middleware;
31
39
  };
@@ -35,34 +43,41 @@ const api = async (config, retries = 0) => {
35
43
  method, headers, timeout = 20000, maxRetries = 3,
36
44
  } = config;
37
45
  const abortController = new AbortController();
38
- const timer = setTimeout(() => abortController.abort(), timeout);
39
- const response = await fetch(url, {
40
- method,
41
- headers,
42
- signal: abortController.signal,
43
- });
44
- clearTimeout(timer);
45
- if (response.ok) {
46
- return {
47
- ...response,
48
- config,
49
- data: await response.json(),
50
- };
51
- }
52
- const { status } = response;
53
- if (maxRetries < retries && (status === 429 || status >= 500)) {
54
- const retryAfter = response.headers.get('retry-after');
55
- return new Promise((resolve, reject) => {
56
- setTimeout(() => {
57
- api(config, retries + 1).then(resolve).catch(reject);
58
- }, (retryAfter && parseInt(retryAfter, 10)) || 5000);
46
+ let isTimeout = false;
47
+ const timer = setTimeout(() => {
48
+ abortController.abort();
49
+ isTimeout = true;
50
+ }, timeout);
51
+ let response;
52
+ try {
53
+ response = await (config.fetch || fetch)(url, {
54
+ method,
55
+ headers,
56
+ signal: abortController.signal,
59
57
  });
58
+ } catch (err) {
59
+ throw new ApiError(config, response, err.message, isTimeout);
60
+ }
61
+ clearTimeout(timer);
62
+ if (response) {
63
+ if (response.ok) {
64
+ return {
65
+ ...response,
66
+ config,
67
+ data: await response.json(),
68
+ };
69
+ }
70
+ const { status } = response;
71
+ if (maxRetries < retries && (status === 429 || status >= 500)) {
72
+ const retryAfter = response.headers.get('retry-after');
73
+ return new Promise((resolve, reject) => {
74
+ setTimeout(() => {
75
+ api(config, retries + 1).then(resolve).catch(reject);
76
+ }, (retryAfter && parseInt(retryAfter, 10)) || 5000);
77
+ });
78
+ }
60
79
  }
61
- const error = new Error(response.statusText);
62
- error.config = config;
63
- error.response = response;
64
- error.statusCode = status;
65
- throw error;
80
+ throw new ApiError(config, response);
66
81
  };
67
82
  const get = (endpoint, config) => api({ ...config, endpoint });
68
83
  const post = (endpoint, config) => api({
@@ -95,6 +110,6 @@ api.delete = del;
95
110
  export default api;
96
111
 
97
112
  export {
98
- setMiddleware, get, post, put, patch, del,
113
+ setMiddleware, get, post, put, patch, del, ApiError,
99
114
  };
100
115
  // # 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,C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CAAC,GAAG,CAAC,EAAE,GAAG,MAAM,EAAE,QAAQ,EAAE,CAAC,CAAC;AAEnC,MAAM,IAAI,GAAG,CAAC,QAAkB,EAAE,MAAyB,EAAE,EAAE,CAAC,GAAG,CAAC;IAClE,GAAG,MAAM;IACT,MAAM,EAAE,MAAM;IACd,QAAQ;CACT,CAAC,CAAC;AAEH,MAAM,GAAG,GAAG,CAAC,QAAkB,EAAE,MAAyB,EAAE,EAAE,CAAC,GAAG,CAAC;IACjE,GAAG,MAAM;IACT,MAAM,EAAE,KAAK;IACb,QAAQ;CACT,CAAC,CAAC;AAEH,MAAM,KAAK,GAAG,CAAC,QAAkB,EAAE,MAAyB,EAAE,EAAE,CAAC,GAAG,CAAC;IACnE,GAAG,MAAM;IACT,MAAM,EAAE,OAAO;IACf,QAAQ;CACT,CAAC,CAAC;AAEH,MAAM,GAAG,GAAG,CAAC,QAAkB,EAAE,MAAyB,EAAE,EAAE,CAAC,GAAG,CAAC;IACjE,GAAG,MAAM;IACT,MAAM,EAAE,QAAQ;IAChB,QAAQ;CACT,CAAC,CAAC;AAEH,GAAG,CAAC,GAAG,GAAG,GAAG,CAAC;AACd,GAAG,CAAC,IAAI,GAAG,IAAI,CAAC;AAChB,GAAG,CAAC,GAAG,GAAG,GAAG,CAAC;AACd,GAAG,CAAC,KAAK,GAAG,KAAK,CAAC;AAClB,GAAG,CAAC,GAAG,GAAG,GAAG,CAAC;AACd,GAAG,CAAC,MAAM,GAAG,GAAG,CAAC;AAEjB,eAAe,GAAG,CAAC;AAEnB,OAAO,EACL,aAAa,EACb,GAAG,EACH,IAAI,EACJ,GAAG,EACH,KAAK,EACL,GAAG,EACH,QAAQ,GACT,CAAC"}
@@ -13,7 +13,8 @@ declare type ResourceId = string & {
13
13
  length: 24;
14
14
  };
15
15
  declare type ResourceAndId = `${Resource}/${ResourceId}`;
16
- declare type Endpoint = Resource | ResourceAndId | `${ResourceAndId}/${string}`;
16
+ declare type EventsEndpoint = `events/${Resource}` | `events/${ResourceAndId}` | 'events/me';
17
+ declare type Endpoint = Resource | ResourceAndId | `${ResourceAndId}/${string}` | `slugs/${string}` | 'search/v1' | EventsEndpoint | 'login' | 'authenticate' | 'ask-auth-callback' | 'check-username' | `$aggregate/${Exclude<Resource, 'stores' | 'applications'>}` | `schemas/${Resource}`;
17
18
  declare type Method = 'get' | 'post' | 'put' | 'patch' | 'delete';
18
19
  declare type Config = {
19
20
  baseUrl?: string;
@@ -25,16 +26,52 @@ declare type Config = {
25
26
  headers?: Record<string, string>;
26
27
  timeout?: number;
27
28
  maxRetries?: number;
29
+ fetch?: typeof fetch;
28
30
  };
29
- declare type ReadConfig<endpoint> = Config & {
30
- method?: 'get';
31
- endpoint: endpoint;
31
+ declare type BaseListResultMeta = {
32
+ offset: number;
33
+ limit: number;
34
+ fields: Array<string>;
32
35
  };
33
- declare type ResponseBody<T> = T extends Config & {
34
- method: 'post';
35
- } ? {
36
+ declare type ResourceListResult<TResource extends Resource> = {
37
+ result: TResource extends 'products' ? Products[] : TResource extends 'categories' ? Categories[] : TResource extends 'brands' ? Brands[] : TResource extends 'collections' ? Collections[] : TResource extends 'grids' ? Grids[] : TResource extends 'carts' ? Carts[] : TResource extends 'orders' ? Orders[] : TResource extends 'customers' ? Customers[] : TResource extends 'stores' ? Stores[] : TResource extends 'applications' ? Applications[] : never;
38
+ meta: BaseListResultMeta & {
39
+ count?: number;
40
+ sort: Array<{
41
+ field: string;
42
+ order: 1 | -1;
43
+ }>;
44
+ query: {
45
+ [key: string]: any;
46
+ };
47
+ };
48
+ };
49
+ declare type EventFieldsByEndpoint<TEndpoint extends EventsEndpoint> = TEndpoint extends `events/${Resource}` ? {
50
+ resource_id: ResourceId;
51
+ authentication_id: ResourceId | null;
52
+ } : TEndpoint extends `events/${ResourceAndId}` ? {
53
+ authentication_id: ResourceId | null;
54
+ } : TEndpoint extends 'events/me' ? {
55
+ resource: Resource;
56
+ resource_id: ResourceId;
57
+ } : never;
58
+ declare type EventsResult<TEndpoint extends EventsEndpoint> = {
59
+ result: Array<{
60
+ timestamp: string;
61
+ store_id?: number;
62
+ resource?: string;
63
+ authentication_id?: ResourceId | null;
64
+ resource_id?: ResourceId;
65
+ action: string;
66
+ modified_fields: string[];
67
+ method?: number | undefined;
68
+ endpoint?: string;
69
+ body?: any;
70
+ ip?: string;
71
+ } & EventFieldsByEndpoint<TEndpoint>>;
72
+ meta: BaseListResultMeta;
73
+ };
74
+ declare type ResponseBody<TConfig extends Config> = TConfig['method'] extends 'post' ? {
36
75
  _id: ResourceId;
37
- } : T extends Config & {
38
- method: 'put' | 'patch' | 'delete';
39
- } ? 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;
76
+ } : TConfig['method'] extends 'put' | 'patch' | 'delete' ? null : 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 Resource ? ResourceListResult<TConfig['endpoint']> : TConfig['endpoint'] extends EventsEndpoint ? EventsResult<TConfig['endpoint']> : any;
40
77
  export type { Products, Categories, Brands, Collections, Grids, Carts, Orders, Customers, Stores, Applications, Resource, ResourceAndId, Endpoint, Method, Config, ResponseBody, };
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudcommerce/api",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "E-Com Plus Cloud Commerce APIs client/adapter",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -5,6 +5,20 @@ const env: { [key: string]: string } = (typeof window === 'object' && window)
5
5
  || (typeof process === 'object' && process && process.env)
6
6
  || {};
7
7
 
8
+ class ApiError extends Error {
9
+ config: Config;
10
+ response?: Response;
11
+ statusCode?: number;
12
+ isTimeout: boolean;
13
+ constructor(config: Config, response?: Response, msg?: string, isTimeout: boolean = false) {
14
+ super(response?.statusText || msg || 'Request error');
15
+ this.config = config;
16
+ this.response = response;
17
+ this.statusCode = response?.status;
18
+ this.isTimeout = isTimeout;
19
+ }
20
+ }
21
+
8
22
  const def = {
9
23
  middleware(config: Config) {
10
24
  let url = config.baseUrl || env.API_BASE_URL || 'https://ecomplus.io/v2';
@@ -29,7 +43,6 @@ const def = {
29
43
  },
30
44
  };
31
45
 
32
- // eslint-disable-next-line no-unused-vars
33
46
  const setMiddleware = (middleware: typeof def.middleware) => {
34
47
  def.middleware = middleware;
35
48
  };
@@ -46,34 +59,41 @@ const api = async <T extends Config>(config: T, retries = 0): Promise<Response &
46
59
  maxRetries = 3,
47
60
  } = config;
48
61
  const abortController = new AbortController();
49
- const timer = setTimeout(() => abortController.abort(), timeout);
50
- const response = await fetch(url, {
51
- method,
52
- headers,
53
- signal: abortController.signal,
54
- });
55
- clearTimeout(timer);
56
- if (response.ok) {
57
- return {
58
- ...response,
59
- config,
60
- data: await response.json(),
61
- };
62
- }
63
- const { status } = response;
64
- if (maxRetries < retries && (status === 429 || status >= 500)) {
65
- const retryAfter = response.headers.get('retry-after');
66
- return new Promise((resolve, reject) => {
67
- setTimeout(() => {
68
- api(config, retries + 1).then(resolve).catch(reject);
69
- }, (retryAfter && parseInt(retryAfter, 10)) || 5000);
62
+ let isTimeout = false;
63
+ const timer = setTimeout(() => {
64
+ abortController.abort();
65
+ isTimeout = true;
66
+ }, timeout);
67
+ let response: Response | undefined;
68
+ try {
69
+ response = await (config.fetch || fetch)(url, {
70
+ method,
71
+ headers,
72
+ signal: abortController.signal,
70
73
  });
74
+ } catch (err: any) {
75
+ throw new ApiError(config, response, err.message, isTimeout);
76
+ }
77
+ clearTimeout(timer);
78
+ if (response) {
79
+ if (response.ok) {
80
+ return {
81
+ ...response,
82
+ config,
83
+ data: await response.json(),
84
+ };
85
+ }
86
+ const { status } = response;
87
+ if (maxRetries < retries && (status === 429 || status >= 500)) {
88
+ const retryAfter = response.headers.get('retry-after');
89
+ return new Promise((resolve, reject) => {
90
+ setTimeout(() => {
91
+ api(config, retries + 1).then(resolve).catch(reject);
92
+ }, (retryAfter && parseInt(retryAfter, 10)) || 5000);
93
+ });
94
+ }
71
95
  }
72
- const error: any = new Error(response.statusText);
73
- error.config = config;
74
- error.response = response;
75
- error.statusCode = status;
76
- throw error;
96
+ throw new ApiError(config, response);
77
97
  };
78
98
 
79
99
  type AbstractedConfig = Omit<Config, 'endpoint' | 'method'>;
@@ -126,4 +146,5 @@ export {
126
146
  put,
127
147
  patch,
128
148
  del,
149
+ ApiError,
129
150
  };
@@ -24,7 +24,22 @@ type ResourceId = string & { length: 24 };
24
24
 
25
25
  type ResourceAndId = `${Resource}/${ResourceId}`;
26
26
 
27
- type Endpoint = Resource | ResourceAndId | `${ResourceAndId}/${string}`;
27
+ type EventsEndpoint = `events/${Resource}`
28
+ | `events/${ResourceAndId}`
29
+ | 'events/me';
30
+
31
+ type Endpoint = Resource
32
+ | ResourceAndId
33
+ | `${ResourceAndId}/${string}`
34
+ | `slugs/${string}`
35
+ | 'search/v1'
36
+ | EventsEndpoint
37
+ | 'login'
38
+ | 'authenticate'
39
+ | 'ask-auth-callback'
40
+ | 'check-username'
41
+ | `$aggregate/${Exclude<Resource, 'stores' | 'applications'>}`
42
+ | `schemas/${Resource}`;
28
43
 
29
44
  type Method = 'get' | 'post' | 'put' | 'patch' | 'delete';
30
45
 
@@ -38,24 +53,86 @@ type Config = {
38
53
  headers?: Record<string, string>,
39
54
  timeout?: number,
40
55
  maxRetries?: number,
56
+ fetch?: typeof fetch,
57
+ };
58
+
59
+ type BaseListResultMeta = {
60
+ offset: number,
61
+ limit: number,
62
+ fields: Array<string>,
41
63
  };
42
64
 
43
- type ReadConfig<endpoint> = Config & { method?: 'get', endpoint: endpoint };
65
+ type ResourceListResult<TResource extends Resource> = {
66
+ result:
67
+ TResource extends 'products' ? Products[] :
68
+ TResource extends 'categories' ? Categories[] :
69
+ TResource extends 'brands' ? Brands[] :
70
+ TResource extends 'collections' ? Collections[] :
71
+ TResource extends 'grids' ? Grids[] :
72
+ TResource extends 'carts' ? Carts[] :
73
+ TResource extends 'orders' ? Orders[] :
74
+ TResource extends 'customers' ? Customers[] :
75
+ TResource extends 'stores' ? Stores[] :
76
+ TResource extends 'applications' ? Applications[] :
77
+ never,
78
+ meta: BaseListResultMeta & {
79
+ count?: number,
80
+ sort: Array<{
81
+ field: string,
82
+ order: 1 | -1,
83
+ }>,
84
+ query: { [key: string]: any },
85
+ },
86
+ };
87
+
88
+ type EventFieldsByEndpoint<TEndpoint extends EventsEndpoint> =
89
+ TEndpoint extends `events/${Resource}` ? {
90
+ resource_id: ResourceId,
91
+ authentication_id: ResourceId | null,
92
+ } :
93
+ TEndpoint extends `events/${ResourceAndId}` ? {
94
+ authentication_id: ResourceId | null,
95
+ } :
96
+ TEndpoint extends 'events/me' ? {
97
+ resource: Resource,
98
+ resource_id: ResourceId,
99
+ } :
100
+ never;
101
+
102
+ type EventsResult<TEndpoint extends EventsEndpoint> = {
103
+ result: Array<{
104
+ timestamp: string,
105
+ store_id?: number,
106
+ resource?: string,
107
+ authentication_id?: ResourceId | null,
108
+ resource_id?: ResourceId,
109
+ action: string,
110
+ modified_fields: string[],
111
+ method?: number | undefined,
112
+ endpoint?: string,
113
+ body?: any,
114
+ ip?: string,
115
+ } & EventFieldsByEndpoint<TEndpoint>>,
116
+ meta: BaseListResultMeta,
117
+ };
44
118
 
45
- type ResponseBody<T> =
46
- T extends Config & { method: 'post' } ? { _id: ResourceId } :
47
- T extends Config & { method: 'put' | 'patch' | 'delete' } ? null :
48
- T extends ReadConfig<`products/${ResourceId}`> ? Products :
49
- T extends ReadConfig<`categories/${ResourceId}`> ? Categories :
50
- T extends ReadConfig<`brands/${ResourceId}`> ? Brands :
51
- T extends ReadConfig<`collections/${ResourceId}`> ? Collections :
52
- T extends ReadConfig<`grids/${ResourceId}`> ? Grids :
53
- T extends ReadConfig<`carts/${ResourceId}`> ? Carts :
54
- T extends ReadConfig<`orders/${ResourceId}`> ? Orders :
55
- T extends ReadConfig<`customers/${ResourceId}`> ? Customers :
56
- T extends ReadConfig<`stores/${ResourceId}`> ? Stores :
57
- T extends ReadConfig<`applications/${ResourceId}`> ? Applications :
58
- any
119
+ type ResponseBody<TConfig extends Config> =
120
+ TConfig['method'] extends 'post' ? { _id: ResourceId } :
121
+ TConfig['method'] extends 'put' | 'patch' | 'delete' ? null :
122
+ // method?: 'get'
123
+ TConfig['endpoint'] extends `products/${ResourceId}` ? Products :
124
+ TConfig['endpoint'] extends `categories/${ResourceId}` ? Categories :
125
+ TConfig['endpoint'] extends `brands/${ResourceId}` ? Brands :
126
+ TConfig['endpoint'] extends `collections/${ResourceId}` ? Collections :
127
+ TConfig['endpoint'] extends `grids/${ResourceId}` ? Grids :
128
+ TConfig['endpoint'] extends `carts/${ResourceId}` ? Carts :
129
+ TConfig['endpoint'] extends `orders/${ResourceId}` ? Orders :
130
+ TConfig['endpoint'] extends `customers/${ResourceId}` ? Customers :
131
+ TConfig['endpoint'] extends `stores/${ResourceId}` ? Stores :
132
+ TConfig['endpoint'] extends `applications/${ResourceId}` ? Applications :
133
+ TConfig['endpoint'] extends Resource ? ResourceListResult<TConfig['endpoint']> :
134
+ TConfig['endpoint'] extends EventsEndpoint ? EventsResult<TConfig['endpoint']> :
135
+ any;
59
136
 
60
137
  export type {
61
138
  Products,
@@ -2,7 +2,7 @@
2
2
 
3
3
  import { test, expect } from 'vitest';
4
4
  import './fetch-polyfill';
5
- import api from '../src/index';
5
+ import api, { ApiError } from '../src/index';
6
6
 
7
7
  const productId = '618041aa239b7206d3fc06de';
8
8
  test('Read product and typecheck SKU', async () => {
@@ -23,8 +23,32 @@ test('404 with different Store ID from env', async () => {
23
23
  const { data } = await api.get(`products/${productId}`);
24
24
  console.log(data);
25
25
  throw new Error('Should have thrown not found');
26
- } catch (error: any) {
26
+ } catch (err: any) {
27
+ const error = err as ApiError;
27
28
  expect(error.statusCode).toBe(404);
28
29
  expect(error.response?.status).toBe(404);
29
30
  }
30
31
  });
32
+
33
+ test('List categories and typecheck result', async () => {
34
+ const { data } = await api.get('categories');
35
+ if (data.result === []) {
36
+ console.log('Any category found');
37
+ }
38
+ expect(Array.isArray(data.result)).toBe(true);
39
+ expect(data.meta).toBeTypeOf('object');
40
+ expect(data.meta.offset).toBeTypeOf('number');
41
+ });
42
+
43
+ test('401 trying to list API events', async () => {
44
+ try {
45
+ const { data } = await api.get('events/orders');
46
+ console.log(data);
47
+ console.log(data.result[0].modified_fields);
48
+ throw new Error('Should have thrown unauthorized');
49
+ } catch (err: any) {
50
+ const error = err as ApiError;
51
+ expect(error.statusCode).toBe(401);
52
+ expect(error.response?.status).toBe(401);
53
+ }
54
+ });
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudcommerce/app-discounts",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "E-Com Plus Cloud Commerce app for complex discount rules",
5
5
  "main": "functions/dist/index.js",
6
6
  "repository": {
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudcommerce/storefront",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "E-Com Plus Cloud Commerce storefront with Astro",
5
5
  "main": "src/index.js",
6
6
  "repository": {