tissue/resources/assets/js/fetch.ts

72 lines
2.5 KiB
TypeScript
Raw Normal View History

2020-08-06 02:26:51 +09:00
import { stringify } from 'qs';
const token = document.head.querySelector<HTMLMetaElement>('meta[name="csrf-token"]');
if (!token) {
console.error('CSRF token not found');
}
const headers = {
'X-CSRF-TOKEN': token?.content ?? '',
};
type QueryParams = { [key: string]: string };
const joinParamsToPath = (path: string, params: QueryParams) =>
Object.keys(params).length === 0 ? path : `${path}?${stringify(params)}`;
const fetchWrapper = (path: string, options: RequestInit = {}) =>
fetch(path, {
credentials: 'same-origin',
...options,
headers: { ...headers, ...options.headers },
2020-08-06 02:26:51 +09:00
});
2020-08-06 09:53:50 +09:00
const fetchWithJson = (path: string, body?: any, options: RequestInit = {}) =>
2020-08-06 02:26:51 +09:00
fetchWrapper(path, {
...options,
2020-08-06 09:53:50 +09:00
body: body && JSON.stringify(body),
headers: { 'Content-Type': 'application/json', ...options.headers },
2020-08-06 02:26:51 +09:00
});
2020-08-06 09:53:50 +09:00
const fetchWithForm = (path: string, body?: any, options: RequestInit = {}) =>
2020-08-06 02:26:51 +09:00
fetchWrapper(path, {
...options,
2020-08-06 09:53:50 +09:00
body: body && stringify(body),
headers: { 'Content-Type': 'application/x-www-form-urlencoded', ...options.headers },
2020-08-06 02:26:51 +09:00
});
export const fetchGet = (path: string, params: QueryParams = {}, options: RequestInit = {}) =>
fetchWrapper(joinParamsToPath(path, params), { method: 'GET', ...options });
2020-08-06 09:53:50 +09:00
export const fetchPostJson = (path: string, body?: any, options: RequestInit = {}) =>
2020-08-06 02:26:51 +09:00
fetchWithJson(path, body, { method: 'POST', ...options });
2020-08-06 09:53:50 +09:00
export const fetchPostForm = (path: string, body?: any, options: RequestInit = {}) =>
2020-08-06 02:26:51 +09:00
fetchWithForm(path, body, { method: 'POST', ...options });
2020-08-06 09:53:50 +09:00
export const fetchPutJson = (path: string, body?: any, options: RequestInit = {}) =>
2020-08-06 02:26:51 +09:00
fetchWithJson(path, body, { method: 'PUT', ...options });
2020-08-06 09:53:50 +09:00
export const fetchPutForm = (path: string, body?: any, options: RequestInit = {}) =>
2020-08-06 02:26:51 +09:00
fetchWithForm(path, body, { method: 'PUT', ...options });
2020-08-06 09:53:50 +09:00
export const fetchDeleteJson = (path: string, body?: any, options: RequestInit = {}) =>
2020-08-06 02:26:51 +09:00
fetchWithJson(path, body, { method: 'DELETE', ...options });
2020-08-06 09:53:50 +09:00
export const fetchDeleteForm = (path: string, body?: any, options: RequestInit = {}) =>
2020-08-06 02:26:51 +09:00
fetchWithForm(path, body, { method: 'DELETE', ...options });
2020-08-06 09:53:50 +09:00
export class ResponseError extends Error {
response: Response;
constructor(response: Response, ...rest: any) {
super(...rest);
if (Error.captureStackTrace) {
Error.captureStackTrace(this, ResponseError);
}
this.name = 'ResponseError';
this.response = response;
}
}