rest-api-kit
Advanced tools
Comparing version 0.0.39 to 0.0.40
@@ -67,4 +67,16 @@ type MethodType = "GET" | "POST" | "PUT" | "PATCH" | "DELETE"; | ||
declare function createRequest(url?: string, headers?: {}, method?: MethodType, body?: {}, rest?: Partial<Omit<RequestType, "headers" | "url" | "method" | "body">>): Promise<any>; | ||
declare function makeRequest(payload: string | Partial<RequestType>, headers: Headers): Promise<any> | undefined; | ||
declare function createRequest(url?: string, headers?: {}, method?: MethodType, body?: {}, rest?: Partial<Omit<RequestType, "headers" | "url" | "method" | "body">>): Promise<{ | ||
type: string; | ||
data: string; | ||
} | { | ||
type: string; | ||
data: Promise<any>; | ||
}>; | ||
declare function makeRequest(payload: string | Partial<RequestType>, headers: Headers): Promise<{ | ||
type: string; | ||
data: string; | ||
} | { | ||
type: string; | ||
data: Promise<any>; | ||
}> | undefined; | ||
@@ -81,3 +93,3 @@ /** | ||
*/ | ||
declare function useRest<R = unknown, T = void>(url: string, paramsFromBase: IOptions<any, any>, options?: Partial<RestOptionsType>): TypedQueryHookReturnType<string | T>; | ||
declare function useRest<R = unknown, T = void>(url: string, paramsFromBase: IOptions<any, any>, options?: Partial<RestOptionsType>): TypedQueryHookReturnType<T>; | ||
@@ -84,0 +96,0 @@ declare const defaultOptions: IOptions<any, any>; |
@@ -63,5 +63,13 @@ "use strict"; | ||
const response = await fetch(url, params); | ||
return response.json(); | ||
if (!response.ok) { | ||
if (response.status > 299) { | ||
return { | ||
type: "error", | ||
data: `Request failed with status: ${response.status}` | ||
}; | ||
} | ||
} | ||
return { type: "success", data: response.json() }; | ||
} catch (error) { | ||
console.log(error, "rest-api-kit-error"); | ||
return { type: "error", data: "An error occured" }; | ||
} | ||
@@ -294,3 +302,3 @@ } | ||
} = useStore(); | ||
const trigger = async (body) => { | ||
const trigger = async (body, urlParams) => { | ||
try { | ||
@@ -306,3 +314,3 @@ const params = { ...paramsFromBase }; | ||
url = getBaseUrl(url, options?.baseUrl); | ||
const formattedUrl = params.method === "GET" || params.bodyAsQueryParams ? concatenateParamsWithUrl(url, body) : url; | ||
const formattedUrl = params.method === "GET" || params.bodyAsQueryParams ? concatenateParamsWithUrl(url, body) : !!body && !!urlParams ? concatenateParamsWithUrl(url, urlParams) : url; | ||
load(dispatch, formattedUrl, { ...params, headers }, body); | ||
@@ -341,8 +349,12 @@ let storeIdentifier = `${options.baseUrl || ""}&${params.endpointName}`; | ||
); | ||
if (response?.type === "error") { | ||
dispatch({ type: "data/error", payload: response?.data }); | ||
return; | ||
} | ||
if (params.saveToCache) { | ||
saveToStore(storeIdentifier, response, { ...params }); | ||
saveToStore(storeIdentifier, response?.data, { ...params }); | ||
} | ||
const { type: checkType, response: payload } = applyChecks( | ||
{ ...params, headers }, | ||
response | ||
response?.data | ||
); | ||
@@ -358,3 +370,3 @@ if (checkType === "error") { | ||
type: "response/save", | ||
payload: response | ||
payload: response?.data | ||
}); | ||
@@ -361,0 +373,0 @@ dispatch({ |
{ | ||
"name": "rest-api-kit", | ||
"version": "0.0.39", | ||
"version": "0.0.40", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "module": "dist/index.mjs", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
34176
959