From 5674d5335147502fd701ecaad1584ef158762031 Mon Sep 17 00:00:00 2001 From: purocean Date: Sun, 4 Aug 2024 13:44:05 +0800 Subject: [PATCH] refactor: update proxyFetch function to handle URL objects and Request objects --- src/renderer/support/api.ts | 49 +++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/src/renderer/support/api.ts b/src/renderer/support/api.ts index c7edca68..22d97987 100644 --- a/src/renderer/support/api.ts +++ b/src/renderer/support/api.ts @@ -44,38 +44,57 @@ export async function fetchHttp (input: RequestInfo, init?: RequestInit) { /** * Proxy fetch. - * @param url string + * @param url RequestInfo | URL * @param init RequestInit * @returns */ -export async function proxyFetch (url: string, init?: Omit & { +export async function proxyFetch (url: RequestInfo | URL, init?: Omit & { body?: any, timeout?: number, proxy?: string, jsonBody?: boolean, }) { - init ??= {} - - if (typeof init.timeout === 'number') { - init.headers = { ...init.headers, 'x-proxy-timeout': String(init.timeout) } + if (!url) { + throw new Error('url is required') } - if (init.proxy) { - init.headers = { ...init.headers, 'x-proxy-url': init.proxy } + const _init: typeof init = { ...init } + let _headers = init?.headers + let _url: RequestInfo | URL + + const prefix = '/api/proxy-fetch/' + + if (typeof url === 'string') { + _url = `${prefix}${url}` + } else if ('href' in url) { // same as URL. we do not use instanceof URL because of the compatibility with cross iframe + _url = new URL(`${prefix}${url.href}`) + } else { // Request + _url = new Request(`${prefix}${(url as Request).url}`, url) + _headers = _url.headers } - if (init.redirect === 'error' || init.redirect === 'manual') { - init.headers = { ...init.headers, 'x-proxy-max-redirections': '0' } + const headers = new Headers(_headers) + + if (typeof _init.timeout === 'number') { + headers.set('x-proxy-timeout', String(_init.timeout)) } - if (init.jsonBody) { - init.headers = { ...init.headers, 'Content-Type': 'application/json' } - init.body = JSON.stringify(init.body) + if (_init.proxy) { + headers.set('x-proxy-url', _init.proxy) } - init.headers = { ...init.headers, 'x-yn-authorization': 'Bearer ' + JWT_TOKEN } + if (_init.redirect === 'error' || _init.redirect === 'manual') { + headers.set('x-proxy-max-redirections', '0') + } - const res: Response = await fetch(`/api/proxy-fetch/${url}`, init) + if (_init.jsonBody) { + headers.set('Content-Type', 'application/json') + _init.body = JSON.stringify(_init.body) + } + + headers.set('x-yn-authorization', 'Bearer ' + JWT_TOKEN) + + const res: Response = await fetch(_url, { ..._init, headers }) if (res.headers.get('x-yank-note-api-status') === 'error') { const msg = res.headers.get('x-yank-note-api-message') || 'error'