-
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.
Merge pull request #1 from Diaszano/feature/creationAndImplementation
Feature/creation and implementation
- Loading branch information
Showing
6 changed files
with
229 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 @@ | ||
export default class LinketrackError extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
this.name = 'LinketrackError'; | ||
} | ||
} |
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,17 @@ | ||
interface Evento { | ||
data: string; | ||
hora: string; | ||
local: string; | ||
status: string; | ||
subStatus: Array<string>; | ||
} | ||
|
||
export default interface LinketrackResponse { | ||
codigo: string; | ||
servico: string; | ||
host: string; | ||
quantidade: number; | ||
eventos: Array<Evento>; | ||
time: number; | ||
ultimo?: string; | ||
} |
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,52 @@ | ||
import 'dotenv/config'; | ||
import { describe, expect, it } from 'vitest'; | ||
import Linketrack from './linketrack'; | ||
import LinketrackResponse from './interface/LinketrackResponse'; | ||
|
||
describe('linketrack', async (): Promise<void> => { | ||
it('Deve dar erro de autorização', async (): Promise<void> => { | ||
const linketrack = new Linketrack('user', 'token'); | ||
expect(linketrack.track('LX002249507BR')).rejects.toThrow( | ||
'Usuário não autorizado.', | ||
); | ||
}); | ||
it('Deve rastrear uma encomenda', async (): Promise<void> => { | ||
const linketrack = new Linketrack( | ||
process.env.LINKETRACK_USER || '', | ||
process.env.LINKETRACK_TOKEN || '', | ||
); | ||
|
||
const expectedResponse: Partial<LinketrackResponse> = { | ||
codigo: 'LX002249507BR', | ||
servico: 'PAC - Encomenda Econômica', | ||
quantidade: 0, | ||
eventos: [], | ||
}; | ||
|
||
expect(linketrack.track('LX002249507BR')).resolves.toMatchObject( | ||
expectedResponse, | ||
); | ||
}); | ||
it('Deve rastrear duas encomendas', async (): Promise<void> => { | ||
const linketrack = new Linketrack( | ||
process.env.LINKETRACK_USER || '', | ||
process.env.LINKETRACK_TOKEN || '', | ||
); | ||
|
||
const response: Partial<LinketrackResponse> = { | ||
codigo: 'LX002249507BR', | ||
servico: 'PAC - Encomenda Econômica', | ||
quantidade: 0, | ||
eventos: [], | ||
}; | ||
|
||
const expectedResponse = new Array<Partial<LinketrackResponse>>( | ||
response, | ||
response, | ||
); | ||
|
||
expect( | ||
linketrack.trackAll('LX002249507BR', 'LX002249507BR'), | ||
).resolves.toMatchObject(expectedResponse); | ||
}); | ||
}); |
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,2 +1,46 @@ | ||
const oi: string = "Oi pessoal!" | ||
import axios, { AxiosError } from 'axios'; | ||
import LinketrackResponse from './interface/LinketrackResponse'; | ||
import LinketrackError from './errors/LinketrackError'; | ||
|
||
export default class linketrack { | ||
constructor(private readonly user: string, private readonly token: string) {} | ||
|
||
async track(code: string): Promise<LinketrackResponse> { | ||
return this.request(code); | ||
} | ||
|
||
async trackAll(...codes: Array<string>): Promise<Array<LinketrackResponse>> { | ||
const array_track = new Array<LinketrackResponse>(); | ||
for (const code of codes) { | ||
array_track.push(await this.request(code)); | ||
} | ||
return array_track; | ||
} | ||
|
||
private async request(code: string): Promise<LinketrackResponse> { | ||
try { | ||
const response = await axios.get(this.url(code)); | ||
return response.data; | ||
} catch (error) { | ||
if (error instanceof AxiosError) { | ||
if (error.code === 'ERR_BAD_REQUEST') { | ||
if (error.response?.statusText === 'Unauthorized') { | ||
throw new LinketrackError('Usuário não autorizado.'); | ||
} | ||
if (error.response?.statusText === 'Too Many Requests') { | ||
throw new LinketrackError( | ||
'Muitas solicitações estão sendo feitas.', | ||
); | ||
} | ||
} | ||
} | ||
console.error(error); | ||
throw new LinketrackError((error as Error).message); | ||
} | ||
} | ||
|
||
private url(code: string): string { | ||
const url_base = 'https://api.linketrack.com/track/json'; | ||
return `${url_base}?user=${this.user}&token=${this.token}&codigo=${code}`; | ||
} | ||
} |