@cloudcommerce/api
Advanced tools
Comparing version 0.0.42 to 0.0.43
@@ -7,6 +7,11 @@ // @ts-ignore | ||
constructor(config, response, msg, isTimeout = false) { | ||
super(response?.statusText || msg || 'Request error'); | ||
if (response) { | ||
super(response.statusText); | ||
this.data = response.data; | ||
this.statusCode = response.status; | ||
} else { | ||
super(msg || 'Request error'); | ||
} | ||
this.config = config; | ||
this.response = response; | ||
this.statusCode = response?.status; | ||
this.isTimeout = isTimeout; | ||
@@ -105,2 +110,7 @@ } | ||
} | ||
try { | ||
response.data = await response?.json(); | ||
} catch (e) { | ||
// | ||
} | ||
throw new ApiError(config, response); | ||
@@ -107,0 +117,0 @@ }; |
@@ -30,3 +30,3 @@ import type { Products } from './types/products'; | ||
endpoint: Endpoint; | ||
params?: Record<string, string | number>; | ||
params?: Record<string, string | number | boolean>; | ||
headers?: Headers | Record<string, string>; | ||
@@ -95,2 +95,12 @@ timeout?: number; | ||
declare type RequestBody<TConfig extends Config> = TConfig['method'] extends undefined | 'get' | 'delete' ? undefined : TConfig['method'] extends 'patch' ? any : TConfig['endpoint'] extends SetDocEndpoint<'products'> ? DocSchema<Products> : TConfig['endpoint'] extends SetDocEndpoint<'categories'> ? DocSchema<Categories> : TConfig['endpoint'] extends SetDocEndpoint<'brands'> ? DocSchema<Brands> : TConfig['endpoint'] extends SetDocEndpoint<'collections'> ? DocSchema<Collections> : TConfig['endpoint'] extends SetDocEndpoint<'grids'> ? DocSchema<Grids> : TConfig['endpoint'] extends SetDocEndpoint<'carts'> ? DocSchema<Carts> : TConfig['endpoint'] extends SetDocEndpoint<'orders'> ? DocSchema<Orders> : TConfig['endpoint'] extends SetDocEndpoint<'customers'> ? DocSchema<Customers> : TConfig['endpoint'] extends SetDocEndpoint<'stores'> ? DocSchema<Stores> : TConfig['endpoint'] extends SetDocEndpoint<'applications'> ? DocSchema<Applications> : TConfig['endpoint'] extends SetDocEndpoint<'authentications'> ? DocSchema<Authentications> : any; | ||
export type { Products, Categories, Brands, Collections, Grids, Carts, Orders, Customers, Stores, Applications, Authentications, Resource, ResourceId, ResourceAndId, ResourceOpQuery, Endpoint, Method, Config, ResourceListResult, EventsResult, ResponseBody, RequestBody, }; | ||
declare type ErrorBody = { | ||
status: number; | ||
error_code: number; | ||
message: string; | ||
user_message?: { | ||
en_us: string; | ||
pt_br: string; | ||
}; | ||
more_info?: string; | ||
}; | ||
export type { Products, Categories, Brands, Collections, Grids, Carts, Orders, Customers, Stores, Applications, Authentications, Resource, ResourceId, ResourceAndId, ResourceOpQuery, Endpoint, Method, Config, ResourceListResult, EventsResult, ResponseBody, RequestBody, ErrorBody, }; |
{ | ||
"name": "@cloudcommerce/api", | ||
"type": "module", | ||
"version": "0.0.42", | ||
"version": "0.0.43", | ||
"description": "E-Com Plus Cloud Commerce APIs client/adapter", | ||
@@ -6,0 +6,0 @@ "main": "lib/index.js", |
@@ -7,2 +7,3 @@ import type { | ||
RequestBody, | ||
ErrorBody, | ||
} from './types'; | ||
@@ -17,10 +18,21 @@ | ||
config: Config; | ||
response?: Response; | ||
response?: Response & { data?: ErrorBody }; | ||
statusCode?: number; | ||
data?: ErrorBody; | ||
isTimeout: boolean; | ||
constructor(config: Config, response?: Response, msg?: string, isTimeout: boolean = false) { | ||
super(response?.statusText || msg || 'Request error'); | ||
constructor( | ||
config: Config, | ||
response?: ApiError['response'], | ||
msg?: string, | ||
isTimeout: boolean = false, | ||
) { | ||
if (response) { | ||
super(response.statusText); | ||
this.data = response.data; | ||
this.statusCode = response.status; | ||
} else { | ||
super(msg || 'Request error'); | ||
} | ||
this.config = config; | ||
this.response = response; | ||
this.statusCode = response?.status; | ||
this.isTimeout = isTimeout; | ||
@@ -103,3 +115,3 @@ } | ||
}, timeout); | ||
let response: Response | undefined; | ||
let response: Response & { data?: any } | undefined; | ||
try { | ||
@@ -135,2 +147,7 @@ response = await (config.fetch || fetch)(url, { | ||
} | ||
try { | ||
response.data = await response?.json() as ErrorBody; | ||
} catch (e) { | ||
// | ||
} | ||
throw new ApiError(config, response); | ||
@@ -204,1 +221,7 @@ }; | ||
}; | ||
export type ApiEndpoint = Endpoint; | ||
export type ApiConfig = Config; | ||
export type ApiErrorBody = ErrorBody; |
@@ -59,3 +59,3 @@ import type { Products } from './types/products'; | ||
endpoint: Endpoint, | ||
params?: Record<string, string | number>, | ||
params?: Record<string, string | number | boolean>, | ||
headers?: Headers | Record<string, string>, | ||
@@ -175,2 +175,13 @@ timeout?: number, | ||
type ErrorBody = { | ||
status: number, | ||
error_code: number, | ||
message: string, | ||
user_message?: { | ||
en_us: string, | ||
pt_br: string, | ||
}, | ||
more_info?: string, | ||
}; | ||
export type { | ||
@@ -199,2 +210,3 @@ Products, | ||
RequestBody, | ||
ErrorBody, | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
677332
20698