Skip to content
This repository has been archived by the owner on Dec 11, 2024. It is now read-only.

Commit

Permalink
feat(types): fix types on post and put method
Browse files Browse the repository at this point in the history
  • Loading branch information
andrefelipeschulle committed Aug 27, 2024
1 parent 690de94 commit ac60b92
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@brainylab/fetch-wrapper",
"version": "0.7.0",
"version": "0.7.1",
"keywords": [
"fetch",
"wrapper",
Expand Down
2 changes: 1 addition & 1 deletion src/lib/fetch-wrapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);
});
Expand Down
32 changes: 12 additions & 20 deletions src/lib/fetch-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,29 @@ type FetchWrapperResponse<T> = {
status: number;
statusText: string;
data: T;
raw: {
response: Response;
request: Request;
};
response: Response;
request: Request;
};

type FetchMethods = {
get: <T>(
get: <U>(
path: string | string[],
init?: FetchWrapperInit,
) => Promise<FetchWrapperResponse<T>>;
post: <T, U>(
) => Promise<FetchWrapperResponse<U>>;
post: <U = unknown, T = unknown>(
path: string | string[],
body?: T,
init?: FetchWrapperInit,
) => Promise<FetchWrapperResponse<U>>;
put: <T, U>(
put: <U = unknown, T = unknown>(
path: string | string[],
body: T,
init?: FetchWrapperInit,
) => Promise<FetchWrapperResponse<U>>;
delete: <T>(
delete: <U>(
path: string | string[],
init?: FetchWrapperInit,
) => Promise<FetchWrapperResponse<T>>;
) => Promise<FetchWrapperResponse<U>>;
};

export class FetchWrapper implements FetchMethods {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
};
}

Expand Down

0 comments on commit ac60b92

Please sign in to comment.