feat(requests): add delete and post function
This commit is contained in:
@@ -15,10 +15,18 @@ interface PostProps<T> extends RequestProps<T> {
|
|||||||
data: Record<string, any>;
|
data: Record<string, any>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface PutProps<T> extends RequestProps<T> {
|
||||||
|
data: Record<string, any>;
|
||||||
|
}
|
||||||
|
|
||||||
interface GetProps<T> extends RequestProps<T> {
|
interface GetProps<T> extends RequestProps<T> {
|
||||||
params?: Record<string, any>;
|
params?: Record<string, any>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface DeleteProps<T> extends RequestProps<T> {
|
||||||
|
params?: Record<string, any>;
|
||||||
|
}
|
||||||
|
|
||||||
class Requests {
|
class Requests {
|
||||||
constructor() {}
|
constructor() {}
|
||||||
|
|
||||||
@@ -54,7 +62,9 @@ class Requests {
|
|||||||
props.before?.();
|
props.before?.();
|
||||||
|
|
||||||
// Get url parameters
|
// Get url parameters
|
||||||
const urlParams = props.params ? new URLSearchParams(props.params).toString() : "";
|
const urlParams = props.params
|
||||||
|
? new URLSearchParams(props.params).toString()
|
||||||
|
: "";
|
||||||
// Normalize url
|
// Normalize url
|
||||||
const finalUrl = normalizeLink(`${API_BASE}/${url}${urlParams}`);
|
const finalUrl = normalizeLink(`${API_BASE}/${url}${urlParams}`);
|
||||||
|
|
||||||
@@ -115,6 +125,74 @@ class Requests {
|
|||||||
props.finally?.();
|
props.finally?.();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async put<T>(url: string, props: PutProps<T>): Promise<T | void> {
|
||||||
|
props.before?.();
|
||||||
|
|
||||||
|
// Normalize url
|
||||||
|
const finalUrl = normalizeLink(`${API_BASE}/${url}`);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Do request
|
||||||
|
const res = await fetch(finalUrl, {
|
||||||
|
method: "PUT",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify(props.data),
|
||||||
|
});
|
||||||
|
|
||||||
|
// Verify data
|
||||||
|
const responseData = await this.verifyData<T>(res);
|
||||||
|
|
||||||
|
// Otherwise return response data
|
||||||
|
props.success?.(responseData);
|
||||||
|
return responseData;
|
||||||
|
} catch (error) {
|
||||||
|
const err = error as Error;
|
||||||
|
// Show notification, and call error callback
|
||||||
|
toast.error(err.message);
|
||||||
|
props.error?.(err);
|
||||||
|
} finally {
|
||||||
|
props.finally?.();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async delete<T>(url: string, props: DeleteProps<T>): Promise<T | void> {
|
||||||
|
// Call before
|
||||||
|
props.before?.();
|
||||||
|
|
||||||
|
// Get url parameters
|
||||||
|
const urlParams = props.params
|
||||||
|
? new URLSearchParams(props.params).toString()
|
||||||
|
: "";
|
||||||
|
// Normalize url
|
||||||
|
const finalUrl = normalizeLink(`${API_BASE}/${url}${urlParams}`);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Do request
|
||||||
|
const res = await fetch(finalUrl, {
|
||||||
|
method: "DELETE",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Verify data
|
||||||
|
const responseData = await this.verifyData<T>(res);
|
||||||
|
|
||||||
|
// Otherwise return response data
|
||||||
|
props.success?.(responseData);
|
||||||
|
return responseData;
|
||||||
|
} catch (error) {
|
||||||
|
const err = error as Error;
|
||||||
|
// Show notification, and call error callback
|
||||||
|
toast.error(err.message);
|
||||||
|
props.error?.(err);
|
||||||
|
} finally {
|
||||||
|
props.finally?.();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default new Requests();
|
export default new Requests();
|
||||||
|
|||||||
Reference in New Issue
Block a user