From 463ced66c4d12f8d380d0bca2c5ff2febf38af7e Mon Sep 17 00:00:00 2001 From: Christian Farris Date: Fri, 22 Oct 2021 10:11:08 -0500 Subject: [PATCH] feat: allow for custom response parser with `parseResponse` (#16) Co-authored-by: Pooya Parsa --- README.md | 10 ++++++++++ src/fetch.ts | 8 +++++--- test/index.test.ts | 8 +++++++- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e5ad6a9..127d120 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,16 @@ We use [conditional exports](https://nodejs.org/api/packages.html#packages_condi const { users } = await $fetch('/api/users') ``` +You can optionally provde a different parser than destr. + +```js +// Use JSON.parse +await $fetch('/movie?lang=en', { parseResponse: JSON.parse }) + +// Return text as is +await $fetch('/movie?lang=en', { parseResponse: txt => txt }) +``` + ## ✔️ JSON Body `$fetch` automatically stringifies request body (if an object is passed) and adds JSON `Content-Type` headers (for `put`, `patch` and `post` requests). diff --git a/src/fetch.ts b/src/fetch.ts index 93f6736..e7b61d2 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -15,6 +15,7 @@ export interface FetchOptions extends Omit { baseURL?: string body?: RequestInit['body'] | Record params?: SearchParams + parseResponse?: (responseText: string) => any response?: boolean } @@ -44,8 +45,8 @@ export function setHeader (options: FetchOptions, _key: string, value: string) { } export function createFetch ({ fetch }: CreateFetchOptions): $Fetch { - const raw: $Fetch['raw'] = async function (request, opts) { - if (opts && typeof request === 'string') { + const raw: $Fetch['raw'] = async function (request, opts = {}) { + if (typeof request === 'string') { if (opts.baseURL) { request = joinURL(opts.baseURL, request) } @@ -59,7 +60,8 @@ export function createFetch ({ fetch }: CreateFetchOptions): $Fetch { } const response: FetchResponse = await fetch(request, opts as RequestInit) const text = await response.text() - response.data = destr(text) + const parseFn = opts.parseResponse || destr + response.data = parseFn(text) if (!response.ok) { throw createFetchError(request, response) } diff --git a/test/index.test.ts b/test/index.test.ts index baccee0..1e9cc8e 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -8,7 +8,7 @@ describe('ohmyfetch', () => { let listener: Listener const getURL = (url: string) => joinURL(listener.url, url) - it('setup', async () => { + beforeAll(async () => { const app = createApp() .use('/ok', () => 'ok') .use('/params', req => (getQuery(req.url || ''))) @@ -25,6 +25,12 @@ describe('ohmyfetch', () => { expect(await $fetch(getURL('ok'))).toBe('ok') }) + it('custom parseResponse', async () => { + const parser = jest.fn().mockReturnValue('asdf') + await $fetch(getURL('ok'), { parseResponse: parser }) + expect(parser).toHaveBeenCalledTimes(1) + }) + it('baseURL', async () => { expect(await $fetch('/x?foo=123', { baseURL: getURL('url') })).toBe('/x?foo=123') })