├── .eslintrc.js
├── .gitignore
├── .travis.yml
├── .vscode
└── launch.json
├── LICENSE
├── README.md
├── index.js
├── package-lock.json
├── package.json
├── test
├── main.js
└── results.json
└── tools.js
/.eslintrc.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | extends: 'airbnb-base',
3 | };
4 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Logs
2 | logs
3 | *.log
4 | npm-debug.log*
5 | yarn-debug.log*
6 | yarn-error.log*
7 |
8 | # Runtime data
9 | pids
10 | *.pid
11 | *.seed
12 | *.pid.lock
13 |
14 | # Directory for instrumented libs generated by jscoverage/JSCover
15 | lib-cov
16 |
17 | # Coverage directory used by tools like istanbul
18 | coverage
19 |
20 | # nyc test coverage
21 | .nyc_output
22 |
23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24 | .grunt
25 |
26 | # Bower dependency directory (https://bower.io/)
27 | bower_components
28 |
29 | # node-waf configuration
30 | .lock-wscript
31 |
32 | # Compiled binary addons (http://nodejs.org/api/addons.html)
33 | build/Release
34 |
35 | # Dependency directories
36 | node_modules/
37 | jspm_packages/
38 |
39 | # Typescript v1 declaration files
40 | typings/
41 |
42 | # Optional npm cache directory
43 | .npm
44 |
45 | # Optional eslint cache
46 | .eslintcache
47 |
48 | # Optional REPL history
49 | .node_repl_history
50 |
51 | # Output of 'npm pack'
52 | *.tgz
53 |
54 | # Yarn Integrity file
55 | .yarn-integrity
56 |
57 | # dotenv environment variables file
58 | .env
59 |
60 | /.idea
61 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: node_js
2 | node_js:
3 | - 'lts/*'
4 | - 'node'
5 | - '10'
6 |
7 | before_install:
8 | # package-lock.json was introduced in npm@5
9 | - '[[ $(node -v) =~ ^v9.*$ ]] || npm install -g npm@latest' # skipped when using node 9
10 | - npm install -g greenkeeper-lockfile@1
11 | install: npm install
12 | before_script: greenkeeper-lockfile-update
13 | after_script: greenkeeper-lockfile-upload
14 | after_success: npm run coverage
15 | deploy:
16 | provider: npm
17 | email: sorackb@gmail.com
18 | api_key: $NPM_TOKEN
19 | on:
20 | tags: true
21 |
--------------------------------------------------------------------------------
/.vscode/launch.json:
--------------------------------------------------------------------------------
1 | {
2 | // Use IntelliSense to learn about possible attributes.
3 | // Hover to view descriptions of existing attributes.
4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5 | "version": "0.2.0",
6 | "configurations": [
7 | {
8 | "type": "node",
9 | "request": "launch",
10 | "name": "Mocha Tests",
11 | "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
12 | "args": [
13 | "--timeout",
14 | "999999",
15 | "--colors",
16 | "${workspaceFolder}/test"
17 | ],
18 | "internalConsoleOptions": "openOnSessionStart",
19 | "envFile": "${workspaceFolder}/.env",
20 | },
21 | {
22 | "type": "node",
23 | "request": "launch",
24 | "name": "Launch Program",
25 | "program": "${workspaceFolder}/index.js"
26 | }
27 | ]
28 | }
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 Lucas Bernardo de Souza Santos
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # sinesp-api
2 |
3 | [![Build Status][ci-img]][ci]
4 | [![Coverage Status][coveralls-img]][coveralls]
5 | [![snyk badge][snyk-img]][snyk]
6 |
7 | [![Dependency Status][dep-img]][dep]
8 | [![devDependency Status][devDep-img]][devDep]
9 |
10 | [![NPM version][npm-img]][npm]
11 | [![node badge][node-img]][node]
12 |
13 | [npm-img]: https://img.shields.io/npm/v/sinesp-api.svg
14 | [npm]: https://www.npmjs.com/package/sinesp-api
15 | [ci-img]: https://travis-ci.org/Sorackb/sinesp-api.svg
16 | [ci]: https://travis-ci.org/Sorackb/sinesp-api
17 | [coveralls-img]: https://coveralls.io/repos/github/Sorackb/sinesp-api/badge.svg?branch=master
18 | [coveralls]: https://coveralls.io/github/Sorackb/sinesp-api?branch=master
19 | [dep-img]: https://david-dm.org/Sorackb/sinesp-api.svg
20 | [dep]: https://david-dm.org/Sorackb/sinesp-api
21 | [devDep-img]: https://david-dm.org/Sorackb/sinesp-api/dev-status.svg
22 | [devDep]: https://david-dm.org/Sorackb/sinesp-api#info=devDependencies
23 | [snyk-img]: https://snyk.io/test/github/Sorackb/sinesp-api/badge.svg
24 | [snyk]: https://snyk.io/test/github/Sorackb/sinesp-api
25 | [node-img]: https://img.shields.io/node/v/mocha.svg
26 | [node]: https://nodejs.org/en/
27 |
28 | | PagSeguro | PayPal |
29 | | :-------------: | :-------------: |
30 | [](https://pag.ae/bhmK2Xf) | [](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=LKDGCQBKYBW5E)
31 |
32 | Módulo do Node.js que permite a consulta de placa no território nacional utilizando a base de dados da [API-Carros](https://github.com/100n0m3/API-Carros).
33 |
34 | ## Pré-requisitos
35 |
36 | - Versões do Node.js anteriores a 8 não são compatíveis;
37 |
38 | ## Instalação
39 |
40 | ```
41 | $ npm install sinesp-api --save
42 | ```
43 |
44 | ## API
45 |
46 |
47 | - search(plate) ⇒
Promise.<object>
48 | Busca o veículo pela placa
49 |
50 | - configure([host], [endpoint], [serviceVersion], [proxy]) ⇒
51 | Configura o módulo
52 |
53 |
54 |
55 | ## search(plate) ⇒ Promise.<object>
56 | Busca o veículo pela placa
57 |
58 | **Retorna**: Promise.<object>
- A representação do veículo identificado pela placa
59 |
60 | | Parâmetro | Tipo | Descrição |
61 | | --- | --- | --- |
62 | | plate | string
| A placa do veículo a ser consultada |
63 |
64 | **Exemplo**
65 | ```js
66 | const sinespApi = require('sinesp-api');
67 |
68 | let vehicle = await sinespApi.search('AAA111');
69 | ```
70 |
71 | ### Saída
72 |
73 | ```json
74 | {
75 | "codigoRetorno": "0",
76 | "mensagemRetorno": "Sem erros.",
77 | "codigoSituacao": "0",
78 | "situacao": "Sem restrição",
79 | "modelo": "FIAT/UNO MILLE EP",
80 | "marca": "FIAT/UNO MILLE EP",
81 | "cor": "BRANCA",
82 | "ano": "1996",
83 | "anoModelo": "1996",
84 | "placa": "ABC1234",
85 | "data": "02/08/2018 às 02:52:34",
86 | "uf": "DF",
87 | "municipio": "BRASILIA",
88 | "chassi": "99092",
89 | "dataAtualizacaoCaracteristicasVeiculo": "13/04/2018",
90 | "dataAtualizacaoRouboFurto": "01/08/2018",
91 | "dataAtualizacaoAlarme": "01/08/2018"
92 | }
93 | ```
94 |
95 |
96 |
97 | ## configure([host], [endpoint], [serviceVersion], [timeout], [maximumRetry], [proxy]) ⇒
98 | Configura o módulo
99 |
100 | **Retorna**: O próprio módulo
101 |
102 | | Parâmetro | Tipo | Default | Descrição |
103 | | --- | --- | --- | --- |
104 | | [host] | string
| "apicarros.com"
| Host do serviço SINESP |
105 | | [endpoint] | string
| "consulta"
| Endpoint do serviço SINESP |
106 | | [serviceVersion] | string
| "v1"
| Versão do serviço SINESP |
107 | | [timeout] | number
| 0
| req/res timeout em ms, reseta ao seguir redirecionamentos. 0 para desabilitar (Limite do SO aplicado) |
108 | | [maximumRetry] | number
| 0
| Número máximo de tentativas se a requisição falhar |
109 | | [proxy] | object
| {}
| O objeto com configurações de proxy, caso exista |
110 |
111 | ---
112 |
113 | ## Atenção
114 |
115 | Esta implementação não possui nenhum vínculo oficial com o Sistema Nacional de Informações de Segurança Pública (SINESP). Utilizamos a [API-Carros](https://github.com/100n0m3/API-Carros) para a obtenção dos dados e deixamos aqui nosso agradecimento pela disponilização do serviço.
116 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | /**
2 | * @file Manages the wrapper of SINESP's search for plates
3 | *
4 | * @author Lucas Bernardo
5 | */
6 |
7 | const { retry } = require('./tools');
8 |
9 | /**
10 | * The accepted format: AAA0000, AAA0AA0, AAA00A0
11 | *
12 | * @constant
13 | *
14 | * @type {RegExp}
15 | */
16 | const PLATE_FORMATS = [
17 | /^[a-zA-Z]{3}[0-9]{4}$/im,
18 | /^[a-zA-Z]{3}[0-9]{1}[a-zA-Z]{1}[0-9]{2}$/im,
19 | /^[a-zA-Z]{3}[0-9]{2}[a-zA-Z]{1}[0-9]{1}$/im,
20 | ];
21 | const SPECIAL = /[^a-zA-Z0-9]/i;
22 |
23 | const DEFAULT = {
24 | host: 'apicarros.com',
25 | endpoint: 'consulta',
26 | serviceVersion: 'v1',
27 | timeout: 0,
28 | maximumRetry: 0,
29 | proxy: {},
30 | };
31 |
32 | let opts = {};
33 |
34 | /**
35 | * Validate the format of the plate informed
36 | *
37 | * @param {string} plate - The informed plate
38 | *
39 | * @returns {Promise} Represents the plate without special characters
40 | *
41 | * @private
42 | */
43 | const validate = async (plate) => {
44 | const plateToUse = plate.replace(SPECIAL, '');
45 |
46 | const valid = PLATE_FORMATS.reduce((res, format) => res || format.test(plateToUse), false);
47 |
48 | if (!valid) {
49 | throw new Error('Formato de placa inválido! Utilize o formato "LLLNLNN", "LLLNNLN" ou "LLLNNNN" (em que L é letra e N, número).');
50 | }
51 |
52 | return plateToUse;
53 | };
54 |
55 | /**
56 | * Send the request to SINESP's 'search by plate' service
57 | *
58 | * @param {string} plate - The plate of the vehicle to be searched
59 | *
60 | * @returns {Promise