From 36a516d58473fd10927cd8cf3d21c77e4315969a Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Tue, 28 Jan 2020 16:27:11 +0800 Subject: [PATCH] add generics at fetch --- resources/assets/src/scripts/net.ts | 48 ++++++++++++++++++----------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/resources/assets/src/scripts/net.ts b/resources/assets/src/scripts/net.ts index c0af72f2..2c70b8a0 100644 --- a/resources/assets/src/scripts/net.ts +++ b/resources/assets/src/scripts/net.ts @@ -28,8 +28,9 @@ export const init: RequestInit = { } function retrieveToken() { - const csrfField = - document.querySelector('meta[name="csrf-token"]') + const csrfField = document.querySelector( + 'meta[name="csrf-token"]', + ) // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition return csrfField?.content || '' } @@ -40,9 +41,10 @@ export async function walkFetch(request: Request): Promise { try { const response = await fetch(request) const cloned = response.clone() - const body = response.headers.get('Content-Type') === 'application/json' - ? await response.json() - : await response.text() + const body = + response.headers.get('Content-Type') === 'application/json' + ? await response.json() + : await response.text() if (response.ok) { return body } @@ -50,7 +52,9 @@ export async function walkFetch(request: Request): Promise { if (response.status === 422) { // Process validation errors from Laravel. - const { errors }: { + const { + errors, + }: { message: string errors: { [field: string]: string[] } } = body @@ -68,7 +72,7 @@ export async function walkFetch(request: Request): Promise { } if (body.exception && Array.isArray(body.trace)) { - const trace = (body.trace as Array<{ file: string, line: number }>) + const trace = (body.trace as Array<{ file: string; line: number }>) .map((t, i) => `[${i + 1}] ${t.file}#L${t.line}`) .join('
') message = `${message}
${trace}
` @@ -87,7 +91,7 @@ export async function walkFetch(request: Request): Promise { } } -export function get(url: string, params = empty): Promise { +export function get(url: string, params = empty): Promise { emit('beforeFetch', { method: 'GET', url, @@ -96,10 +100,12 @@ export function get(url: string, params = empty): Promise { const qs = queryStringify(params) - return walkFetch(new Request(`${blessing.base_url}${url}${qs && `?${qs}`}`, init)) + return walkFetch( + new Request(`${blessing.base_url}${url}${qs && `?${qs}`}`, init), + ) } -function nonGet(method: string, url: string, data: any): Promise { +function nonGet(method: string, url: string, data: any): Promise { emit('beforeFetch', { method: method.toUpperCase(), url, @@ -119,26 +125,32 @@ function nonGet(method: string, url: string, data: any): Promise { return walkFetch(request) } -export function post(url: string, data = empty): Promise { - return nonGet('POST', url, data) +export function post(url: string, data = empty): Promise { + return nonGet('POST', url, data) } -export function put(url: string, data = empty): Promise { - return nonGet('PUT', url, data) +export function put(url: string, data = empty): Promise { + return nonGet('PUT', url, data) } -export function del(url: string, data = empty): Promise { - return nonGet('DELETE', url, data) +export function del(url: string, data = empty): Promise { + return nonGet('DELETE', url, data) } Vue.use(_Vue => { Object.defineProperty(_Vue.prototype, '$http', { get: () => ({ - get, post, put, del, + get, + post, + put, + del, }), }) }) blessing.fetch = { - get, post, put, del, + get, + post, + put, + del, }