From 49b70b7b040a608fcc92776a6323665a29499053 Mon Sep 17 00:00:00 2001 From: Pavel Mineev Date: Sun, 31 Jan 2021 18:27:34 +0300 Subject: [PATCH] Add comments to fetcher --- frontend/app/common/fetcher.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/frontend/app/common/fetcher.ts b/frontend/app/common/fetcher.ts index 40dc06c4..9ba1799d 100644 --- a/frontend/app/common/fetcher.ts +++ b/frontend/app/common/fetcher.ts @@ -22,6 +22,7 @@ type FetcherMethods = Record<'get' | 'delete', (url: string, query?: QueryPar export function stringifyUrl(uri: string, query: QueryParams) { const queryEntries = Object.entries(query); const siteIdParam = `site=${encodeURIComponent(siteId)}`; + // we use `indexOf` instead of `startsWidth` because we don't want to have another one polyfill for no reason const baseUrl = uri.indexOf('/auth') === 0 ? BASE_URL : `${BASE_URL}${API_BASE}`; const url = `${baseUrl}${uri}`; @@ -43,9 +44,15 @@ export function stringifyUrl(uri: string, query: QueryParams) { let activeJwtToken: string | undefined; const fetcher = METHODS.reduce((acc, method) => { + /** + * Fetcher is abstraction on top of fetch + * + * @uri – uri to API endpoint. BASE_URL and API_BASE will be added automatically. For auth requests API_BASE will be dropped. + * @query - collection of query params. They will be concatenated to URL. `siteId` will be added automatically. + * @body - data for sending to the server. If you pass object it will be stringified. If you pass form data it will be sent as is. Content type headers will be added automatically. + */ acc[method] = async (uri: string, query: QueryParams = {}, body?: Payload) => { // add api base if it's not auth request - // we use `indexOf` instead of `startsWidth` because we don't want to have another one polyfill for no reason const url = stringifyUrl(uri, query); const headers: Record = {}; const params: RequestInit = { method };