From ac60b92f7270fd69095c6790037c275a18a7b734 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Felipe=20Schulle?= Date: Mon, 26 Aug 2024 22:25:38 -0300 Subject: [PATCH] feat(types): fix types on post and put method --- package.json | 2 +- src/lib/fetch-wrapper.spec.ts | 2 +- src/lib/fetch-wrapper.ts | 32 ++++++++++++-------------------- 3 files changed, 14 insertions(+), 22 deletions(-) diff --git a/package.json b/package.json index 22e4e73..eb7915d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@brainylab/fetch-wrapper", - "version": "0.7.0", + "version": "0.7.1", "keywords": [ "fetch", "wrapper", diff --git a/src/lib/fetch-wrapper.spec.ts b/src/lib/fetch-wrapper.spec.ts index 279f897..34fbfd6 100644 --- a/src/lib/fetch-wrapper.spec.ts +++ b/src/lib/fetch-wrapper.spec.ts @@ -42,7 +42,7 @@ describe('fetch-wrapper', () => { const response = await apiInstance.get('/api/cep/v2/89010025'); - expect(response.raw.request.headers.get('x-custom-header')).toEqual( + expect(response.request.headers.get('x-custom-header')).toEqual( 'custom-value', ); }); diff --git a/src/lib/fetch-wrapper.ts b/src/lib/fetch-wrapper.ts index ec9b31f..afd9f8f 100644 --- a/src/lib/fetch-wrapper.ts +++ b/src/lib/fetch-wrapper.ts @@ -24,31 +24,29 @@ type FetchWrapperResponse = { status: number; statusText: string; data: T; - raw: { - response: Response; - request: Request; - }; + response: Response; + request: Request; }; type FetchMethods = { - get: ( + get: ( path: string | string[], init?: FetchWrapperInit, - ) => Promise>; - post: ( + ) => Promise>; + post: ( path: string | string[], body?: T, init?: FetchWrapperInit, ) => Promise>; - put: ( + put: ( path: string | string[], body: T, init?: FetchWrapperInit, ) => Promise>; - delete: ( + delete: ( path: string | string[], init?: FetchWrapperInit, - ) => Promise>; + ) => Promise>; }; export class FetchWrapper implements FetchMethods { @@ -98,9 +96,7 @@ export class FetchWrapper implements FetchMethods { /** * create a new request instance */ - this.request = new Request(url, { - ...configs, - }); + this.request = new Request(url, configs); /** * implement before hook @@ -130,16 +126,12 @@ export class FetchWrapper implements FetchMethods { throw new HttpRequestError(this.response as Response, this.data); } - const data = await this.response.json(); - return { status: this.response.status, statusText: this.response.statusText, - data, - raw: { - response: this.response, - request: this.request, - }, + data: this.data as T, + response: this.response, + request: this.request, }; }