@emitti/node 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +43 -0
- package/dist/index.d.ts +74 -0
- package/dist/index.js +87 -0
- package/package.json +30 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Emitti
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# @emitti/node
|
|
2
|
+
|
|
3
|
+
SDK oficial do Emitti para Node.js (>= 18). Envelopa a API REST em métodos nativos.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install @emitti/node
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { Emitti, webhooks } from "@emitti/node";
|
|
11
|
+
|
|
12
|
+
const emitti = new Emitti({ apiKey: process.env.EMITTI_API_KEY! });
|
|
13
|
+
|
|
14
|
+
const nota = await emitti.emitir({
|
|
15
|
+
prestador: { cnpj: "12345678000190", inscricao_municipal: "1122334" },
|
|
16
|
+
tomador: { razao_social: "Cliente X", cnpj: "98765432000110" },
|
|
17
|
+
servico: {
|
|
18
|
+
codigo_municipio: "3550308", codigo_servico: "01.05",
|
|
19
|
+
discriminacao: "Plano SaaS", valor_servicos: 499.9, aliquota_iss: 2,
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
// nota.emissao_id, nota.status === "QUEUED"
|
|
23
|
+
|
|
24
|
+
await emitti.consultar(nota.emissao_id);
|
|
25
|
+
await emitti.cancelar(nota.emissao_id);
|
|
26
|
+
await emitti.substituir(nota.emissao_id, { /* nova nota */ });
|
|
27
|
+
const xml = await emitti.baixarXml(nota.emissao_id);
|
|
28
|
+
const pdf = await emitti.baixarPdf(nota.emissao_id); // Uint8Array
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Webhooks
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
// no seu handler (use o corpo CRU)
|
|
35
|
+
const ok = webhooks.verificar(rawBody, req.headers["x-emitti-signature"], process.env.EMITTI_WEBHOOK_SECRET!);
|
|
36
|
+
if (!ok) return res.status(400).end();
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Build/test (deste repo)
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
npm install && npm run build && npm test
|
|
43
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
export interface EmittiOptions {
|
|
2
|
+
apiKey: string;
|
|
3
|
+
/** Default: https://api.emitti.com.br */
|
|
4
|
+
baseUrl?: string;
|
|
5
|
+
}
|
|
6
|
+
export interface Endereco {
|
|
7
|
+
tipo_logradouro?: string;
|
|
8
|
+
logradouro?: string;
|
|
9
|
+
numero?: string;
|
|
10
|
+
complemento?: string;
|
|
11
|
+
bairro?: string;
|
|
12
|
+
cidade_ibge?: string;
|
|
13
|
+
uf?: string;
|
|
14
|
+
cep?: string;
|
|
15
|
+
}
|
|
16
|
+
export interface NfseInput {
|
|
17
|
+
referencia_externa?: string;
|
|
18
|
+
prestador: {
|
|
19
|
+
cnpj: string;
|
|
20
|
+
inscricao_municipal: string;
|
|
21
|
+
};
|
|
22
|
+
tomador: {
|
|
23
|
+
razao_social?: string;
|
|
24
|
+
cnpj?: string;
|
|
25
|
+
cpf?: string;
|
|
26
|
+
email?: string;
|
|
27
|
+
endereco?: Endereco;
|
|
28
|
+
};
|
|
29
|
+
servico: {
|
|
30
|
+
codigo_municipio: string;
|
|
31
|
+
codigo_servico: string;
|
|
32
|
+
discriminacao: string;
|
|
33
|
+
valor_servicos: number;
|
|
34
|
+
aliquota_iss: number;
|
|
35
|
+
iss_retido?: boolean;
|
|
36
|
+
valor_deducoes?: number;
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
export interface Emissao {
|
|
40
|
+
emissao_id: string;
|
|
41
|
+
status: string;
|
|
42
|
+
referencia_externa?: string | null;
|
|
43
|
+
resultado?: Record<string, unknown> | null;
|
|
44
|
+
}
|
|
45
|
+
export declare class EmittiError extends Error {
|
|
46
|
+
status: number;
|
|
47
|
+
body: unknown;
|
|
48
|
+
constructor(status: number, body: unknown);
|
|
49
|
+
}
|
|
50
|
+
export declare class Emitti {
|
|
51
|
+
private readonly opts;
|
|
52
|
+
private readonly base;
|
|
53
|
+
constructor(opts: EmittiOptions);
|
|
54
|
+
private req;
|
|
55
|
+
/** Emite uma NFS-e (assíncrono). Resposta 202 com o emissao_id. */
|
|
56
|
+
emitir(input: NfseInput, opts?: {
|
|
57
|
+
idempotencyKey?: string;
|
|
58
|
+
}): Promise<Emissao>;
|
|
59
|
+
/** Consulta o status de uma emissão. */
|
|
60
|
+
consultar(emissaoId: string): Promise<Emissao>;
|
|
61
|
+
/** Cancela uma NFS-e autorizada (assíncrono). */
|
|
62
|
+
cancelar(emissaoId: string): Promise<Emissao>;
|
|
63
|
+
/** Substitui uma NFS-e: emite a nova e cancela a antiga. */
|
|
64
|
+
substituir(emissaoId: string, input: NfseInput): Promise<Emissao>;
|
|
65
|
+
/** Baixa o XML autorizado (string). */
|
|
66
|
+
baixarXml(emissaoId: string): Promise<string>;
|
|
67
|
+
/** Baixa o DANFE/RPS em PDF (bytes). */
|
|
68
|
+
baixarPdf(emissaoId: string): Promise<Uint8Array>;
|
|
69
|
+
}
|
|
70
|
+
/** Helpers de webhook: valide a assinatura antes de confiar no payload. */
|
|
71
|
+
export declare const webhooks: {
|
|
72
|
+
/** Confere o header X-Emitti-Signature (HMAC-SHA256 do corpo cru). */
|
|
73
|
+
verificar(rawBody: string | Uint8Array, signature: string, secret: string): boolean;
|
|
74
|
+
};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SDK oficial do Emitti para Node.js (>= 18).
|
|
3
|
+
*
|
|
4
|
+
* import { Emitti } from "@emitti/node";
|
|
5
|
+
* const emitti = new Emitti({ apiKey: process.env.EMITTI_API_KEY! });
|
|
6
|
+
* const { emissao_id } = await emitti.emitir({ ... });
|
|
7
|
+
*/
|
|
8
|
+
import { createHmac, timingSafeEqual } from "node:crypto";
|
|
9
|
+
export class EmittiError extends Error {
|
|
10
|
+
status;
|
|
11
|
+
body;
|
|
12
|
+
constructor(status, body) {
|
|
13
|
+
super(`Emitti API respondeu ${status}`);
|
|
14
|
+
this.status = status;
|
|
15
|
+
this.body = body;
|
|
16
|
+
this.name = "EmittiError";
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export class Emitti {
|
|
20
|
+
opts;
|
|
21
|
+
base;
|
|
22
|
+
constructor(opts) {
|
|
23
|
+
this.opts = opts;
|
|
24
|
+
this.base = (opts.baseUrl ?? "https://api.emitti.com.br").replace(/\/$/, "");
|
|
25
|
+
}
|
|
26
|
+
async req(method, path, body, idempotencyKey) {
|
|
27
|
+
const res = await fetch(`${this.base}${path}`, {
|
|
28
|
+
method,
|
|
29
|
+
headers: {
|
|
30
|
+
Authorization: `Bearer ${this.opts.apiKey}`,
|
|
31
|
+
...(body ? { "Content-Type": "application/json" } : {}),
|
|
32
|
+
...(idempotencyKey ? { "Idempotency-Key": idempotencyKey } : {}),
|
|
33
|
+
},
|
|
34
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
35
|
+
});
|
|
36
|
+
if (!res.ok) {
|
|
37
|
+
let parsed = undefined;
|
|
38
|
+
try {
|
|
39
|
+
parsed = await res.json();
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
/* corpo não-JSON */
|
|
43
|
+
}
|
|
44
|
+
throw new EmittiError(res.status, parsed);
|
|
45
|
+
}
|
|
46
|
+
return res;
|
|
47
|
+
}
|
|
48
|
+
/** Emite uma NFS-e (assíncrono). Resposta 202 com o emissao_id. */
|
|
49
|
+
async emitir(input, opts) {
|
|
50
|
+
return (await this.req("POST", "/v1/nfse", input, opts?.idempotencyKey)).json();
|
|
51
|
+
}
|
|
52
|
+
/** Consulta o status de uma emissão. */
|
|
53
|
+
async consultar(emissaoId) {
|
|
54
|
+
return (await this.req("GET", `/v1/nfse/${emissaoId}`)).json();
|
|
55
|
+
}
|
|
56
|
+
/** Cancela uma NFS-e autorizada (assíncrono). */
|
|
57
|
+
async cancelar(emissaoId) {
|
|
58
|
+
return (await this.req("DELETE", `/v1/nfse/${emissaoId}`)).json();
|
|
59
|
+
}
|
|
60
|
+
/** Substitui uma NFS-e: emite a nova e cancela a antiga. */
|
|
61
|
+
async substituir(emissaoId, input) {
|
|
62
|
+
return (await this.req("POST", `/v1/nfse/${emissaoId}/substituicao`, input)).json();
|
|
63
|
+
}
|
|
64
|
+
/** Baixa o XML autorizado (string). */
|
|
65
|
+
async baixarXml(emissaoId) {
|
|
66
|
+
return (await this.req("GET", `/v1/nfse/${emissaoId}/xml`)).text();
|
|
67
|
+
}
|
|
68
|
+
/** Baixa o DANFE/RPS em PDF (bytes). */
|
|
69
|
+
async baixarPdf(emissaoId) {
|
|
70
|
+
const buf = await (await this.req("GET", `/v1/nfse/${emissaoId}/pdf`)).arrayBuffer();
|
|
71
|
+
return new Uint8Array(buf);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/** Helpers de webhook: valide a assinatura antes de confiar no payload. */
|
|
75
|
+
export const webhooks = {
|
|
76
|
+
/** Confere o header X-Emitti-Signature (HMAC-SHA256 do corpo cru). */
|
|
77
|
+
verificar(rawBody, signature, secret) {
|
|
78
|
+
const corpo = typeof rawBody === "string" ? Buffer.from(rawBody) : Buffer.from(rawBody);
|
|
79
|
+
const esperado = createHmac("sha256", secret).update(corpo).digest("hex");
|
|
80
|
+
try {
|
|
81
|
+
return timingSafeEqual(Buffer.from(signature), Buffer.from(esperado));
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@emitti/node",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "SDK oficial do Emitti para Node.js — emissão de NFS-e",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": ["dist", "README.md", "LICENSE"],
|
|
9
|
+
"keywords": ["emitti", "nfse", "nota-fiscal", "fiscal", "api", "sefaz"],
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"author": "Emitti",
|
|
12
|
+
"homepage": "https://docs.emitti.com.br/sdks#node",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/emittibr/emitti-docs.git",
|
|
16
|
+
"directory": "sdks/node"
|
|
17
|
+
},
|
|
18
|
+
"bugs": { "url": "https://github.com/emittibr/emitti-docs/issues" },
|
|
19
|
+
"publishConfig": { "access": "public" },
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsc",
|
|
22
|
+
"test": "node --test",
|
|
23
|
+
"prepublishOnly": "npm run build"
|
|
24
|
+
},
|
|
25
|
+
"engines": { "node": ">=18" },
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/node": "^22.7.4",
|
|
28
|
+
"typescript": "^5.6.2"
|
|
29
|
+
}
|
|
30
|
+
}
|