From e37f27315c4bd0c6a95dd640231536114f7bfdde Mon Sep 17 00:00:00 2001 From: Nikita Galaiko Date: Tue, 21 Feb 2023 09:17:12 +0100 Subject: [PATCH] log fetch requests --- src/lib/api.ts | 131 ++++++++++++++++++++++++++++--------------------- 1 file changed, 74 insertions(+), 57 deletions(-) diff --git a/src/lib/api.ts b/src/lib/api.ts index 1e1be00ab..a778cdfd2 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -43,69 +43,86 @@ const parseResponseJSON = async (response: Response) => { } }; +type FetchMiddleware = (f: typeof fetch) => typeof fetch; + +const fetchWithLog: FetchMiddleware = (fetch) => async (url, options) => { + log.info("fetch", url, options); + try { + return await fetch(url, options); + } catch (e) { + log.error("fetch", e); + throw e; + } +}; + export default ( - { fetch }: { fetch: typeof window.fetch } = { fetch: window.fetch } -) => ({ - login: { - token: { - create: (params: {} = {}): Promise => - fetch(getUrl("login/token.json"), { + { fetch: realFetch }: { fetch: typeof window.fetch } = { + fetch: window.fetch, + } +) => { + const fetch = fetchWithLog(realFetch); + return { + login: { + token: { + create: (params: {} = {}): Promise => + fetch(getUrl("login/token.json"), { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(params), + }) + .then(parseResponseJSON) + .then((token) => { + const url = new URL(token.url); + url.host = apiUrl.host; + return { + ...token, + url: url.toString(), + }; + }), + }, + user: { + get: (token: string): Promise => + fetch(getUrl(`login/user/${token}.json`), { + method: "GET", + }).then(parseResponseJSON), + }, + }, + projects: { + create: ( + token: string, + params: { name: string; uid?: string } + ): Promise => + fetch(getUrl("projects.json"), { method: "POST", headers: { "Content-Type": "application/json", + "X-Auth-Token": token, }, body: JSON.stringify(params), - }) - .then(parseResponseJSON) - .then((token) => { - const url = new URL(token.url); - url.host = apiUrl.host; - return { - ...token, - url: url.toString(), - }; - }), - }, - user: { - get: (token: string): Promise => - fetch(getUrl(`login/user/${token}.json`), { + }).then(parseResponseJSON), + list: (token: string): Promise => + fetch(getUrl("projects.json"), { method: "GET", + headers: { + "X-Auth-Token": token, + }, + }).then(parseResponseJSON), + get: (token: string, repositoryId: string): Promise => + fetch(getUrl(`projects/${repositoryId}.json`), { + method: "GET", + headers: { + "X-Auth-Token": token, + }, + }).then(parseResponseJSON), + delete: (token: string, repositoryId: string): Promise => + fetch(getUrl(`projects/${repositoryId}.json`), { + method: "DELETE", + headers: { + "X-Auth-Token": token, + }, }).then(parseResponseJSON), }, - }, - projects: { - create: ( - token: string, - params: { name: string; uid?: string } - ): Promise => - fetch(getUrl("projects.json"), { - method: "POST", - headers: { - "Content-Type": "application/json", - "X-Auth-Token": token, - }, - body: JSON.stringify(params), - }).then(parseResponseJSON), - list: (token: string): Promise => - fetch(getUrl("projects.json"), { - method: "GET", - headers: { - "X-Auth-Token": token, - }, - }).then(parseResponseJSON), - get: (token: string, repositoryId: string): Promise => - fetch(getUrl(`projects/${repositoryId}.json`), { - method: "GET", - headers: { - "X-Auth-Token": token, - }, - }).then(parseResponseJSON), - delete: (token: string, repositoryId: string): Promise => - fetch(getUrl(`projects/${repositoryId}.json`), { - method: "DELETE", - headers: { - "X-Auth-Token": token, - }, - }).then(parseResponseJSON), - }, -}); + }; +};