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

Commit

Permalink
feat(request): using new Request to prepare configs to fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
andrefelipeschulle committed Aug 22, 2024
1 parent d58e92a commit 690de94
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 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.6.0",
"version": "0.7.0",
"keywords": [
"fetch",
"wrapper",
Expand Down
21 changes: 12 additions & 9 deletions src/lib/fetch-wrapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,20 @@ describe('fetch-wrapper', () => {
});

it('should be able to using hook before request', async () => {
api.hooks.beforeRequest = async (config) => {
config.headers = { 'x-custom-header': 'custom-value' };
};
const apiInstance = new FetchWrapper({
baseUrl: 'https://brasilapi.com.br/',
hooks: {
beforeRequest: async (request) => {
request.headers.set('x-custom-header', 'custom-value');
},
},
});

const response = await api.get(
'https://brasilapi.com.br/api/cep/v2/89010025',
);
const response = await apiInstance.get('/api/cep/v2/89010025');

expect(response.raw.request.headers).toEqual({
'x-custom-header': 'custom-value',
});
expect(response.raw.request.headers.get('x-custom-header')).toEqual(
'custom-value',
);
});

it('should be able to intercept an HttpRequestError error on a request', async () => {
Expand Down
25 changes: 17 additions & 8 deletions src/lib/fetch-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type FetchWrapperInit = FetchWrapperConfig & {
};

export type FwprHooks = {
beforeRequest?: (configs: FetchWrapperConfig) => Promise<void>;
beforeRequest?: (request: Request) => Promise<void>;
beforeError?: (response: Response, data: any) => Promise<void>;
};

Expand All @@ -26,7 +26,7 @@ type FetchWrapperResponse<T> = {
data: T;
raw: {
response: Response;
request: FetchWrapperConfig & { url: URL };
request: Request;
};
};

Expand Down Expand Up @@ -54,12 +54,14 @@ type FetchMethods = {
export class FetchWrapper implements FetchMethods {
private url: string = 'http://localhost';
private data: any = null;
private request: Request | null = null;
private response: Response | null = null;
public defaults: FetchWrapperConfig = {
private defaults: FetchWrapperConfig = {
headers: {},
};
public hooks: FwprHooks = {
beforeRequest: undefined,
beforeError: undefined,
};

constructor(props?: FetchWrapperProps) {
Expand Down Expand Up @@ -91,16 +93,23 @@ export class FetchWrapper implements FetchMethods {
url.search = params;
}

const configs = init ? mergeConfigs(this.defaults, init) : this.defaults;

/**
* create a new request instance
*/
this.request = new Request(url, {
...configs,
});

/**
* implement before hook
*/
if (this.hooks?.beforeRequest) {
await this.hooks.beforeRequest(this.defaults);
await this.hooks.beforeRequest(this.request);
}

const configs = init ? mergeConfigs(this.defaults, init) : this.defaults;

this.response = await fetch(url, configs);
this.response = await fetch(this.request);

if (!this.response.ok) {
if (
Expand Down Expand Up @@ -129,7 +138,7 @@ export class FetchWrapper implements FetchMethods {
data,
raw: {
response: this.response,
request: { url: url, ...configs },
request: this.request,
},
};
}
Expand Down

0 comments on commit 690de94

Please sign in to comment.