This repository has been archived by the owner on Dec 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(export): nova isntancia padrão e error handling
- Loading branch information
1 parent
08bd8be
commit 8dc3c82
Showing
16 changed files
with
515 additions
and
349 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"cSpell.words": [ | ||
"fwpr", | ||
"fwrp" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import type { FetchWrapperProps } from './lib/create-instance'; | ||
import { createInstance } from './lib/create-instance'; | ||
import type { ErrorHandlingResponse } from './lib/error-handling'; | ||
import { fwprErrorHandling } from './lib/error-handling'; | ||
import type { FetchWrapper } from './lib/fetch-wrapper'; | ||
|
||
const fwpr = createInstance() as FetchWrapper & { | ||
create: (defaultConfig?: FetchWrapperProps) => FetchWrapper; | ||
errorHandling: (error: unknown) => ErrorHandlingResponse; | ||
}; | ||
|
||
fwpr.create = createInstance; | ||
fwpr.errorHandling = fwprErrorHandling; | ||
|
||
export default fwpr; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
import { create } from './create-instance'; | ||
|
||
const wrapper = { create }; | ||
|
||
export { wrapper }; | ||
export default wrapper; | ||
|
||
export * from './http-error'; | ||
import fwpr from './fwpr'; | ||
|
||
/** | ||
* exports types | ||
*/ | ||
export type { FetchWrapperProps } from './create-instance'; | ||
export type { FetchWrapperProps } from './lib/create-instance'; | ||
|
||
/** | ||
* export lib | ||
*/ | ||
export * from './errors/http-request-error'; | ||
export * from './lib/error-handling'; | ||
|
||
export { fwpr as default, fwpr }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { fwpr } from '../index'; | ||
import { createInstance } from './create-instance'; | ||
import { FetchWrapper } from './fetch-wrapper'; | ||
|
||
describe('create-instance', () => { | ||
it('should create an instance of FetchWrapper', () => { | ||
const api = createInstance({ | ||
baseUrl: 'http://localhost:3000', | ||
}); | ||
|
||
expect(api).toBeInstanceOf(FetchWrapper); | ||
}); | ||
|
||
it('should create an instance of FetchWrapper using index.js', () => { | ||
const api = fwpr.create({ | ||
baseUrl: 'http://localhost:3000', | ||
}); | ||
|
||
expect(api).toBeInstanceOf(FetchWrapper); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import type { FetchWrapperDefaults } from './fetch-wrapper'; | ||
import { FetchWrapper } from './fetch-wrapper'; | ||
|
||
export type FetchWrapperProps = { | ||
baseUrl?: string; | ||
defaultConfig?: FetchWrapperDefaults; | ||
}; | ||
|
||
export function createInstance(defaultConfig?: FetchWrapperProps) { | ||
const instance = new FetchWrapper(defaultConfig); | ||
|
||
return instance; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { HttpRequestError } from '../errors/http-request-error'; | ||
import fwpr from '../fwpr'; | ||
import { fwprErrorHandling } from './error-handling'; | ||
|
||
describe('error-handling', () => { | ||
it('should handle HttpRequestError', async () => { | ||
const error = fwprErrorHandling( | ||
new HttpRequestError({ | ||
status: 404, | ||
statusText: 'Not Found', | ||
} as Response), | ||
); | ||
|
||
expect(error).toHaveProperty('error'); | ||
expect(error.error).toEqual('HTTP_REQUEST_ERROR'); | ||
}); | ||
|
||
it('should handle ECONNREFUSED error', async () => { | ||
try { | ||
await fwpr.get('http://localhost'); | ||
} catch (error) { | ||
const result = fwprErrorHandling(error); | ||
|
||
console.log(error); | ||
|
||
expect(result).toHaveProperty('error'); | ||
expect(result.error).toEqual('CONNECTION_REFUSED'); | ||
} | ||
}); | ||
|
||
it('should handle unknown error', () => { | ||
const error = { code: 'UNKNOWN_ERROR' }; | ||
const result = fwprErrorHandling(error); | ||
|
||
expect(result).toHaveProperty('error'); | ||
expect(result.error).toEqual('UNEXPECTED_ERROR'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { HttpRequestError } from '../errors/http-request-error'; | ||
|
||
type FetchTypeError = TypeError & { | ||
cause?: { code: 'ECONNREFUSED'; address: string; port: number }; | ||
}; | ||
|
||
export type ErrorHandlingResponse = { | ||
status?: number; | ||
message: string; | ||
error: 'HTTP_REQUEST_ERROR' | 'CONNECTION_REFUSED' | 'UNEXPECTED_ERROR'; | ||
throw?: unknown; | ||
}; | ||
|
||
export function fwprErrorHandling(error: unknown): ErrorHandlingResponse { | ||
console.log(error); | ||
if (error instanceof HttpRequestError) { | ||
return { | ||
status: error.status, | ||
message: error.message, | ||
error: 'HTTP_REQUEST_ERROR', | ||
}; | ||
} | ||
|
||
if (error instanceof TypeError) { | ||
const err = error as FetchTypeError; | ||
|
||
if (err?.cause && err?.cause.code === 'ECONNREFUSED') { | ||
return { | ||
message: `connection refused ${err.cause.address} on port ${err.cause.port}`, | ||
error: 'CONNECTION_REFUSED', | ||
}; | ||
} | ||
} | ||
|
||
return { | ||
message: 'an unexpected error occurred', | ||
error: 'UNEXPECTED_ERROR', | ||
throw: error, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { HttpRequestError } from '../errors/http-request-error'; | ||
import { fwprErrorHandling } from '../lib/error-handling'; | ||
import { FetchWrapper } from './fetch-wrapper'; | ||
|
||
describe('fetch-wrapper', () => { | ||
let api: FetchWrapper; | ||
|
||
beforeEach(() => { | ||
api = new FetchWrapper(); | ||
}); | ||
|
||
it('should be able to make a GET request', async () => { | ||
const response = await api.get('https://api.github.com/orgs/brainylab'); | ||
|
||
expect(response.status).toBe(200); | ||
expect(response.statusText).toBe('OK'); | ||
expect(typeof response.data).toBe('object'); | ||
expect(response.data).toHaveProperty('id'); | ||
}); | ||
|
||
it('should be able to return an HttpRequestError error on a request', async () => { | ||
try { | ||
await api.get('https://api.github.com/orgs'); | ||
} catch (err) { | ||
expect(err).toBeInstanceOf(HttpRequestError); | ||
} | ||
}); | ||
|
||
it('should be able to return an HttpRequestError error on a request connection refused', async () => { | ||
try { | ||
await api.get('http://localhost'); | ||
} catch (err) { | ||
const { error } = fwprErrorHandling(err); | ||
|
||
expect(error).toBe('CONNECTION_REFUSED'); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters