@cloudcommerce/api 0.0.27 → 0.0.28

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.
@@ -9,7 +9,7 @@
9
9
  * Customer object model
10
10
  */
11
11
  export interface Customers {
12
- _id: string;
12
+ _id: string & { length: 24 };
13
13
  created_at: string;
14
14
  updated_at: string;
15
15
  store_id: number;
@@ -365,7 +365,7 @@ export interface Customers {
365
365
  /**
366
366
  * Order ID (ObjectID)
367
367
  */
368
- _id: string;
368
+ _id: string & { length: 24 };
369
369
  /**
370
370
  * When order was saved, date and time in ISO 8601 standard representation
371
371
  */
@@ -6,7 +6,7 @@
6
6
  */
7
7
 
8
8
  export interface Grids {
9
- _id: string;
9
+ _id: string & { length: 24 };
10
10
  created_at: string;
11
11
  updated_at: string;
12
12
  store_id: number;
@@ -9,7 +9,7 @@
9
9
  * Order object model
10
10
  */
11
11
  export interface Orders {
12
- _id: string;
12
+ _id: string & { length: 24 };
13
13
  created_at: string;
14
14
  updated_at: string;
15
15
  store_id: number;
@@ -229,7 +229,7 @@ export interface Orders {
229
229
  /**
230
230
  * Customer ID (ObjectID)
231
231
  */
232
- _id: string;
232
+ _id: string & { length: 24 };
233
233
  /**
234
234
  * Customer language two letter code, sometimes with region, e.g. 'pt_br', 'fr', 'en_us'
235
235
  */
@@ -1229,7 +1229,7 @@ export interface Orders {
1229
1229
  /**
1230
1230
  * Kit product ID (ObjectID)
1231
1231
  */
1232
- _id: string;
1232
+ _id: string & { length: 24 };
1233
1233
  /**
1234
1234
  * Kit product full name
1235
1235
  */
@@ -1251,7 +1251,7 @@ export interface Orders {
1251
1251
  /**
1252
1252
  * Product ID (ObjectID)
1253
1253
  */
1254
- _id: string;
1254
+ _id: string & { length: 24 };
1255
1255
  /**
1256
1256
  * Selected variation ID (ObjectID) if any
1257
1257
  */
@@ -1345,7 +1345,7 @@ export interface Orders {
1345
1345
  /**
1346
1346
  * Subscription order ID (ObjectID)
1347
1347
  */
1348
- _id: string;
1348
+ _id: string & { length: 24 };
1349
1349
  /**
1350
1350
  * Subscription order number
1351
1351
  */
@@ -6,7 +6,7 @@
6
6
  */
7
7
 
8
8
  export interface Products {
9
- _id: string;
9
+ _id: string & { length: 24 };
10
10
  created_at: string;
11
11
  updated_at: string;
12
12
  store_id: number;
@@ -1179,7 +1179,7 @@ export interface Products {
1179
1179
  /**
1180
1180
  * Product ID (ObjectID)
1181
1181
  */
1182
- _id: string;
1182
+ _id: string & { length: 24 };
1183
1183
  /**
1184
1184
  * Product quantity
1185
1185
  */
@@ -6,7 +6,7 @@
6
6
  */
7
7
 
8
8
  export interface Stores {
9
- _id: string;
9
+ _id: string & { length: 24 };
10
10
  created_at: string;
11
11
  updated_at: string;
12
12
  store_id: number;
package/src/types.ts CHANGED
@@ -8,6 +8,7 @@ import type { Orders } from './types/orders';
8
8
  import type { Customers } from './types/customers';
9
9
  import type { Stores } from './types/stores';
10
10
  import type { Applications } from './types/applications';
11
+ import type { Authentications } from './types/authentications';
11
12
 
12
13
  type Resource = 'products'
13
14
  | 'categories'
@@ -18,17 +19,20 @@ type Resource = 'products'
18
19
  | 'orders'
19
20
  | 'customers'
20
21
  | 'stores'
21
- | 'applications';
22
+ | 'applications'
23
+ | 'authentications';
22
24
 
23
25
  type ResourceId = string & { length: 24 };
24
26
 
25
27
  type ResourceAndId = `${Resource}/${ResourceId}`;
26
28
 
29
+ type ResourceOpQuery = Resource | `${Resource}?${string}`;
30
+
27
31
  type EventsEndpoint = `events/${Resource}`
28
32
  | `events/${ResourceAndId}`
29
33
  | 'events/me';
30
34
 
31
- type Endpoint = Resource
35
+ type Endpoint = ResourceOpQuery
32
36
  | ResourceAndId
33
37
  | `${ResourceAndId}/${string}`
34
38
  | `slugs/${string}`
@@ -38,7 +42,7 @@ type Endpoint = Resource
38
42
  | 'authenticate'
39
43
  | 'ask-auth-callback'
40
44
  | 'check-username'
41
- | `$aggregate/${Exclude<Resource, 'stores' | 'applications'>}`
45
+ | `$aggregate/${Exclude<Resource, 'stores' | 'applications' | 'authentications'>}`
42
46
  | `schemas/${Resource}`;
43
47
 
44
48
  type Method = 'get' | 'post' | 'put' | 'patch' | 'delete';
@@ -46,11 +50,14 @@ type Method = 'get' | 'post' | 'put' | 'patch' | 'delete';
46
50
  type Config = {
47
51
  baseUrl?: string,
48
52
  storeId?: number,
53
+ accessToken?: string,
54
+ authenticationId?: string,
55
+ apiKey?: string,
49
56
  lang?: string,
50
57
  method?: Method,
51
58
  endpoint: Endpoint,
52
59
  params?: Record<string, string | number>,
53
- headers?: Record<string, string>,
60
+ headers?: Headers | Record<string, string>,
54
61
  timeout?: number,
55
62
  maxRetries?: number,
56
63
  fetch?: typeof fetch,
@@ -62,18 +69,21 @@ type BaseListResultMeta = {
62
69
  fields: Array<string>,
63
70
  };
64
71
 
65
- type ResourceListResult<TResource extends Resource> = {
72
+ type ListEndpoint<TResource extends Resource> = TResource | `${TResource}?${string}`;
73
+
74
+ type ResourceListResult<TEndpoint extends ResourceOpQuery> = {
66
75
  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[] :
76
+ TEndpoint extends ListEndpoint<'products'> ? Products[] :
77
+ TEndpoint extends ListEndpoint<'categories'> ? Categories[] :
78
+ TEndpoint extends ListEndpoint<'brands'> ? Brands[] :
79
+ TEndpoint extends ListEndpoint<'collections'> ? Collections[] :
80
+ TEndpoint extends ListEndpoint<'grids'> ? Grids[] :
81
+ TEndpoint extends ListEndpoint<'carts'> ? Carts[] :
82
+ TEndpoint extends ListEndpoint<'orders'> ? Orders[] :
83
+ TEndpoint extends ListEndpoint<'customers'> ? Customers[] :
84
+ TEndpoint extends ListEndpoint<'stores'> ? Stores[] :
85
+ TEndpoint extends ListEndpoint<'applications'> ? Applications[] :
86
+ TEndpoint extends ListEndpoint<'authentications'> ? Authentications[] :
77
87
  never,
78
88
  meta: BaseListResultMeta & {
79
89
  count?: number,
@@ -117,9 +127,13 @@ type EventsResult<TEndpoint extends EventsEndpoint> = {
117
127
  };
118
128
 
119
129
  type ResponseBody<TConfig extends Config> =
120
- TConfig['method'] extends 'post' ? { _id: ResourceId } :
130
+ TConfig['method'] extends 'post' ?
131
+ TConfig['endpoint'] extends 'login' ? { _id: ResourceId, store_ids: number[], api_key: string } :
132
+ TConfig['endpoint'] extends 'authenticate' ? { my_id: string, access_token: string, expires: string } :
133
+ { _id: ResourceId } :
121
134
  TConfig['method'] extends 'put' | 'patch' | 'delete' ? null :
122
135
  // method?: 'get'
136
+ TConfig['endpoint'] extends `${string}/${ResourceId}/${string}` ? any :
123
137
  TConfig['endpoint'] extends `products/${ResourceId}` ? Products :
124
138
  TConfig['endpoint'] extends `categories/${ResourceId}` ? Categories :
125
139
  TConfig['endpoint'] extends `brands/${ResourceId}` ? Brands :
@@ -130,10 +144,33 @@ type ResponseBody<TConfig extends Config> =
130
144
  TConfig['endpoint'] extends `customers/${ResourceId}` ? Customers :
131
145
  TConfig['endpoint'] extends `stores/${ResourceId}` ? Stores :
132
146
  TConfig['endpoint'] extends `applications/${ResourceId}` ? Applications :
133
- TConfig['endpoint'] extends Resource ? ResourceListResult<TConfig['endpoint']> :
147
+ TConfig['endpoint'] extends `authentications/${ResourceId}` ? Authentications :
148
+ TConfig['endpoint'] extends ResourceOpQuery ? ResourceListResult<TConfig['endpoint']> :
134
149
  TConfig['endpoint'] extends EventsEndpoint ? EventsResult<TConfig['endpoint']> :
135
150
  any;
136
151
 
152
+ type DocSchema<Document extends any> =
153
+ Omit<Document, '_id' | 'store_id' | 'store_ids' | 'created_at' | 'updated_at'>;
154
+
155
+ type SetDocEndpoint<TResource extends Resource> = TResource | `${TResource}/${ResourceId}`;
156
+
157
+ type RequestBody<TConfig extends Config> =
158
+ TConfig['method'] extends undefined | 'get' | 'delete' ? undefined :
159
+ TConfig['method'] extends 'patch' ? any : // TODO: partial body
160
+ // method: 'post' | 'put'
161
+ TConfig['endpoint'] extends SetDocEndpoint<'products'> ? DocSchema<Products> :
162
+ TConfig['endpoint'] extends SetDocEndpoint<'categories'> ? DocSchema<Categories> :
163
+ TConfig['endpoint'] extends SetDocEndpoint<'brands'> ? DocSchema<Brands> :
164
+ TConfig['endpoint'] extends SetDocEndpoint<'collections'> ? DocSchema<Collections> :
165
+ TConfig['endpoint'] extends SetDocEndpoint<'grids'> ? DocSchema<Grids> :
166
+ TConfig['endpoint'] extends SetDocEndpoint<'carts'> ? DocSchema<Carts> :
167
+ TConfig['endpoint'] extends SetDocEndpoint<'orders'> ? DocSchema<Orders> :
168
+ TConfig['endpoint'] extends SetDocEndpoint<'customers'> ? DocSchema<Customers> :
169
+ TConfig['endpoint'] extends SetDocEndpoint<'stores'> ? DocSchema<Stores> :
170
+ TConfig['endpoint'] extends SetDocEndpoint<'applications'> ? DocSchema<Applications> :
171
+ TConfig['endpoint'] extends SetDocEndpoint<'authentications'> ? DocSchema<Authentications> :
172
+ any;
173
+
137
174
  export type {
138
175
  Products,
139
176
  Categories,
@@ -147,8 +184,10 @@ export type {
147
184
  Applications,
148
185
  Resource,
149
186
  ResourceAndId,
187
+ ResourceOpQuery,
150
188
  Endpoint,
151
189
  Method,
152
190
  Config,
153
191
  ResponseBody,
192
+ RequestBody,
154
193
  };
@@ -52,3 +52,19 @@ test('401 trying to list API events', async () => {
52
52
  expect(error.response?.status).toBe(401);
53
53
  }
54
54
  });
55
+
56
+ test('401 to create category and body typecheck', async () => {
57
+ try {
58
+ const { data } = await api.post('categories', {
59
+ name: 'Test category',
60
+ }, {
61
+ accessToken: 'invalid',
62
+ });
63
+ console.log(data._id);
64
+ throw new Error('Should have thrown unauthorized');
65
+ } catch (err: any) {
66
+ const error = err as ApiError;
67
+ expect(error.statusCode).toBe(401);
68
+ expect(error.response?.status).toBe(401);
69
+ }
70
+ });