From cace92f9ec7d8e57b433ae6a4a1f98d39f6fd1e5 Mon Sep 17 00:00:00 2001 From: diaszano Date: Sat, 13 May 2023 21:47:51 -0300 Subject: [PATCH 1/8] feature/creationAndImplementation: Adicionado novos pacotes --- package-lock.json | 105 +++++++++++++++++++++++++++++++++++++++++++++- package.json | 4 ++ 2 files changed, 108 insertions(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index 7e0b64b..c1489b8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,11 +7,15 @@ "": { "name": "linketrack", "version": "1.0.0", - "license": "ISC", + "license": "MIT", + "dependencies": { + "axios": "^1.4.0" + }, "devDependencies": { "@types/node": "^20.1.4", "@typescript-eslint/eslint-plugin": "^5.59.5", "@typescript-eslint/parser": "^5.59.5", + "dotenv": "^16.0.3", "eslint": "^8.40.0", "eslint-config-prettier": "^8.8.0", "eslint-plugin-prettier": "^4.2.1", @@ -977,6 +981,21 @@ "node": "*" } }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" + }, + "node_modules/axios": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.4.0.tgz", + "integrity": "sha512-S4XCWMEmzvo64T9GfvQDOXgYRDJ/wsSZc7Jvdgx5u1sd0JwsuPLqb3SYmusag+edF6ziyMensPVqLTSc1PiSEA==", + "dependencies": { + "follow-redirects": "^1.15.0", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, "node_modules/balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", @@ -1090,6 +1109,17 @@ "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", "dev": true }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -1182,6 +1212,14 @@ "integrity": "sha512-oIPzksmTg4/MriiaYGO+okXDT7ztn/w3Eptv/+gSIdMdKsJo0u4CfYNFJPy+4SKMuCqGw2wxnA+URMg3t8a/bQ==", "dev": true }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/diff": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz", @@ -1215,6 +1253,15 @@ "node": ">=6.0.0" } }, + "node_modules/dotenv": { + "version": "16.0.3", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.3.tgz", + "integrity": "sha512-7GO6HghkA5fYG9TYnNxi14/7K9f5occMlp3zXAuSxn7CKCxt9xbNWG7yF8hTCSUchlfWSe3uLmlPfigevRItzQ==", + "dev": true, + "engines": { + "node": ">=12" + } + }, "node_modules/esbuild": { "version": "0.17.19", "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.17.19.tgz", @@ -1601,6 +1648,38 @@ "integrity": "sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ==", "dev": true }, + "node_modules/follow-redirects": { + "version": "1.15.2", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.2.tgz", + "integrity": "sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, + "node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -1987,6 +2066,25 @@ "node": ">=8.6" } }, + "node_modules/mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "dependencies": { + "mime-db": "1.52.0" + }, + "engines": { + "node": ">= 0.6" + } + }, "node_modules/minimatch": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", @@ -2285,6 +2383,11 @@ "url": "https://github.com/chalk/ansi-styles?sponsor=1" } }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + }, "node_modules/punycode": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.0.tgz", diff --git a/package.json b/package.json index 1c6ce01..2057e0c 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "@types/node": "^20.1.4", "@typescript-eslint/eslint-plugin": "^5.59.5", "@typescript-eslint/parser": "^5.59.5", + "dotenv": "^16.0.3", "eslint": "^8.40.0", "eslint-config-prettier": "^8.8.0", "eslint-plugin-prettier": "^4.2.1", @@ -33,5 +34,8 @@ "vite-tsconfig-paths": "^4.2.0", "vitest": "^0.31.0", "vitest-mock-extended": "^1.1.3" + }, + "dependencies": { + "axios": "^1.4.0" } } From c6b96a2c9c0107ba5fea1704c27b428ac4116448 Mon Sep 17 00:00:00 2001 From: diaszano Date: Sat, 13 May 2023 21:48:16 -0300 Subject: [PATCH 2/8] feature/creationAndImplementation: Criado os testes --- src/linketrack.spec.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/linketrack.spec.ts diff --git a/src/linketrack.spec.ts b/src/linketrack.spec.ts new file mode 100644 index 0000000..eb18831 --- /dev/null +++ b/src/linketrack.spec.ts @@ -0,0 +1,30 @@ +import 'dotenv/config'; +import { describe, expect, it } from 'vitest'; +import Linketrack from './linketrack'; +import LinketrackResponse from './interface/LinketrackResponse'; + +describe('linketrack', async (): Promise => { + it('Deve dar erro de autorização', async (): Promise => { + const linketrack = new Linketrack('user', 'token'); + expect(linketrack.track('LX002249507BR')).rejects.toThrow( + 'Usuário não autorizado.', + ); + }); + it('Deve rastrear uma encomenda', async (): Promise => { + const linketrack = new Linketrack( + process.env.LINKETRACK_USER || '', + process.env.LINKETRACK_TOKEN || '', + ); + + const expectedResponse: Partial = { + codigo: 'LX002249507BR', + servico: 'PAC - Encomenda Econômica', + quantidade: 0, + eventos: [], + }; + + expect(linketrack.track('LX002249507BR')).resolves.toMatchObject( + expectedResponse, + ); + }); +}); From d0f190cf2e1f19096d256f1d8fecdce0faa258c4 Mon Sep 17 00:00:00 2001 From: diaszano Date: Sat, 13 May 2023 21:49:36 -0300 Subject: [PATCH 3/8] feature/creationAndImplementation: Criado o erro do projeto --- src/interface/LinketrackError.ts | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 src/interface/LinketrackError.ts diff --git a/src/interface/LinketrackError.ts b/src/interface/LinketrackError.ts new file mode 100644 index 0000000..7a50afc --- /dev/null +++ b/src/interface/LinketrackError.ts @@ -0,0 +1,6 @@ +export default class LinketrackError extends Error { + constructor(message: string) { + super(message); + this.name = 'LinketrackError'; + } +} From 41fa6522b4ebe12e495296ae320a99c714956bdb Mon Sep 17 00:00:00 2001 From: diaszano Date: Sat, 13 May 2023 21:50:45 -0300 Subject: [PATCH 4/8] feature/creationAndImplementation: Criado tipos dos dados do Linketrack --- src/interface/LinketrackResponse.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/interface/LinketrackResponse.ts diff --git a/src/interface/LinketrackResponse.ts b/src/interface/LinketrackResponse.ts new file mode 100644 index 0000000..8ccc620 --- /dev/null +++ b/src/interface/LinketrackResponse.ts @@ -0,0 +1,17 @@ +interface Evento { + data: string; + hora: string; + local: string; + status: string; + subStatus: Array; +} + +export default interface LinketrackResponse { + codigo: string; + servico: string; + host: string; + quantidade: number; + eventos: Array; + time: number; + ultimo?: string; +} From d34c9159288facc2e8b5e63ebb32d319d7731e04 Mon Sep 17 00:00:00 2001 From: diaszano Date: Sat, 13 May 2023 21:55:13 -0300 Subject: [PATCH 5/8] feature/creationAndImplementation: Criado o teste para o trackAll --- src/linketrack.spec.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/linketrack.spec.ts b/src/linketrack.spec.ts index eb18831..603edfd 100644 --- a/src/linketrack.spec.ts +++ b/src/linketrack.spec.ts @@ -27,4 +27,26 @@ describe('linketrack', async (): Promise => { expectedResponse, ); }); + it('Deve rastrear duas encomendas', async (): Promise => { + const linketrack = new Linketrack( + process.env.LINKETRACK_USER || '', + process.env.LINKETRACK_TOKEN || '', + ); + + const response: Partial = { + codigo: 'LX002249507BR', + servico: 'PAC - Encomenda Econômica', + quantidade: 0, + eventos: [], + }; + + const expectedResponse = new Array>( + response, + response, + ); + + expect( + linketrack.trackAll('LX002249507BR', 'LX002249507BR'), + ).resolves.toMatchObject(expectedResponse); + }); }); From da25977a4b038478b8c24eaddc73da48396c2621 Mon Sep 17 00:00:00 2001 From: diaszano Date: Sat, 13 May 2023 22:04:47 -0300 Subject: [PATCH 6/8] feature/creationAndImplementation: Feito o linketrack --- src/linketrack.ts | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/src/linketrack.ts b/src/linketrack.ts index 654ad66..47e189e 100644 --- a/src/linketrack.ts +++ b/src/linketrack.ts @@ -1,2 +1,46 @@ -const oi: string = "Oi pessoal!" +import axios, { AxiosError } from 'axios'; +import LinketrackResponse from './interface/LinketrackResponse'; +import LinketrackError from './interface/LinketrackError'; +export default class linketrack { + constructor(private readonly user: string, private readonly token: string) {} + + async track(code: string): Promise { + return this.request(code); + } + + async trackAll(...codes: Array): Promise> { + const array_track = new Array(); + for (const code of codes) { + array_track.push(await this.request(code)); + } + return array_track; + } + + private async request(code: string): Promise { + 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}`; + } +} From 4e0d7159f207f117ebb3a014f0d43ac616499cd2 Mon Sep 17 00:00:00 2001 From: diaszano Date: Sat, 13 May 2023 22:06:56 -0300 Subject: [PATCH 7/8] feature/creationAndImplementation: Corrigido o local do error --- src/{interface => errors}/LinketrackError.ts | 0 src/linketrack.ts | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename src/{interface => errors}/LinketrackError.ts (100%) diff --git a/src/interface/LinketrackError.ts b/src/errors/LinketrackError.ts similarity index 100% rename from src/interface/LinketrackError.ts rename to src/errors/LinketrackError.ts diff --git a/src/linketrack.ts b/src/linketrack.ts index 47e189e..be6a921 100644 --- a/src/linketrack.ts +++ b/src/linketrack.ts @@ -1,6 +1,6 @@ import axios, { AxiosError } from 'axios'; import LinketrackResponse from './interface/LinketrackResponse'; -import LinketrackError from './interface/LinketrackError'; +import LinketrackError from './errors/LinketrackError'; export default class linketrack { constructor(private readonly user: string, private readonly token: string) {} From 84da4a03b5f8175f02b81f65f44fa2942816d262 Mon Sep 17 00:00:00 2001 From: diaszano Date: Sat, 13 May 2023 22:16:28 -0300 Subject: [PATCH 8/8] feature/creationAndImplementation: Trocado o nome do projeto. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2057e0c..b33dcb4 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "linketrack", + "name": "linketrackjs", "version": "1.0.0", "description": "Uma interface amigável para a API de rastreamento de encomendas dos Correios desenvolvida pela Link & Track.", "main": "dist/linketrack.js",