@useinvio/nfse-sdk 2.2.0 → 3.0.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.
@@ -1,43 +1,6 @@
1
- import { buildDpsFromJson, type DpsJsonRequest } from '@useinvio/nfse-sdk';
2
-
3
- const nota: DpsJsonRequest = {
4
- ambiente: 'restrita',
5
- prestador: {
6
- cnpj: '12345678000195',
7
- tpInsc: '2',
8
- cLocEmi: '4106902',
9
- serie: '1601',
10
- opSimpNac: '1',
11
- regEspTrib: '0',
12
- },
13
- servico: {
14
- cTribNac: '010201',
15
- xDescServ: 'Desenvolvimento de software',
16
- cLocPrestacao: '4106902',
17
- cNBS: '115022000',
18
- },
19
- emissao: {
20
- nDPS: '1',
21
- dhEmi: '2026-06-15T10:30:00-03:00',
22
- dCompet: '2026-06-01',
23
- valores: { vServ: '1500.00' },
24
- tributacaoMunicipal: {
25
- tribISSQN: '3',
26
- cPaisResult: 'BR',
27
- tpRetISSQN: '1',
28
- },
29
- tributacaoFederal: {
30
- piscofins: { CST: '07' },
31
- },
32
- totTrib: {
33
- pTotTribFed: '0.00',
34
- pTotTribEst: '0.00',
35
- pTotTribMun: '5.00',
36
- },
37
- },
38
- };
39
-
40
- const { id, xml } = buildDpsFromJson(nota);
1
+ import { buildDpsFromJson } from '@useinvio/nfse-sdk';
2
+ import { nationalInvoice } from './payloads.js';
41
3
 
4
+ const { id, xml } = buildDpsFromJson(nationalInvoice);
42
5
  console.log(id);
43
6
  console.log(xml);
@@ -1,4 +1,5 @@
1
1
  import { EmitirNotaError, emitirNfse, loadPfx, type DpsJsonRequest } from '@useinvio/nfse-sdk';
2
+ import { nationalInvoice } from './payloads.js';
2
3
 
3
4
  async function emitWithStructuredErrors(nota: DpsJsonRequest) {
4
5
  const pfx = loadPfx('./certificado.pfx', process.env.PFX_PASSWORD!);
@@ -9,7 +10,7 @@ async function emitWithStructuredErrors(nota: DpsJsonRequest) {
9
10
  if (!(error instanceof EmitirNotaError)) throw error;
10
11
 
11
12
  return {
12
- rejected: true,
13
+ failed: true, // Includes incomplete HTTP 2xx responses; reconcile before retrying.
13
14
  status: error.status,
14
15
  dpsId: error.dpsId,
15
16
  errors: error.erros.map((rejeicao) => ({
@@ -21,4 +22,4 @@ async function emitWithStructuredErrors(nota: DpsJsonRequest) {
21
22
  }
22
23
  }
23
24
 
24
- void emitWithStructuredErrors({} as DpsJsonRequest);
25
+ void emitWithStructuredErrors(nationalInvoice);
@@ -0,0 +1,36 @@
1
+ import type { DpsJsonRequest } from '@useinvio/nfse-sdk';
2
+
3
+ /** Illustrative declarations; the integrator chooses the actual tax treatment. */
4
+ export const nationalInvoice: DpsJsonRequest = {
5
+ ambiente: 'restrita',
6
+ prestador: { cnpj: '12345678000195', cLocEmi: '4106902', serie: '1601', opSimpNac: '1', regEspTrib: '0' },
7
+ servico: { cTribNac: '010201', xDescServ: 'Desenvolvimento de software', cLocPrestacao: '4106902', cNBS: '115022000' },
8
+ emissao: {
9
+ nDPS: '1', dhEmi: '2026-06-15T10:30:00-03:00', dCompet: '2026-06-01',
10
+ valores: { vServ: '1500.00' },
11
+ tomador: {
12
+ CPF: '12345678901', xNome: 'Cliente Exemplo',
13
+ end: { endNac: { cMun: '4106902', CEP: '80010000' }, xLgr: 'Rua Exemplo', nro: '100', xBairro: 'Centro' },
14
+ },
15
+ tributacaoMunicipal: { tribISSQN: '1', tpRetISSQN: '1' },
16
+ tributacaoFederal: { piscofins: { CST: '07' } },
17
+ totTrib: { pTotTribFed: '0.00', pTotTribEst: '0.00', pTotTribMun: '5.00' },
18
+ },
19
+ };
20
+
21
+ export const exportInvoice: DpsJsonRequest = {
22
+ ...nationalInvoice,
23
+ emissao: {
24
+ ...nationalInvoice.emissao,
25
+ valores: { vServMoeda: '9000.00', cotacao: 5.210621 },
26
+ tomador: {
27
+ NIF: 'FOREIGN123', xNome: 'Cliente Exterior',
28
+ end: {
29
+ endExt: { cPais: 'US', cEndPost: '10001', xCidade: 'New York', xEstProvReg: 'NY' },
30
+ xLgr: 'Example Street', nro: '100', xBairro: 'Manhattan',
31
+ },
32
+ },
33
+ comercioExterior: { mdPrestacao: '1', vincPrest: '0', tpMoeda: '220', mecAFComexP: '01', mecAFComexT: '01', movTempBens: '1', mdic: '0' },
34
+ tributacaoMunicipal: { tribISSQN: '3', cPaisResult: 'US', tpRetISSQN: '1' },
35
+ },
36
+ };
package/llms.txt CHANGED
@@ -55,9 +55,14 @@ Keep values as strings when they represent fiscal codes, identifiers, decimals,
55
55
  - `DEFAULT_AMBIENTE` is `restrita`; `NfseClient` also accepts `sandbox` and `production` aliases.
56
56
  - `buildDpsFromJson` validates first via `validateDpsJsonRequest`.
57
57
  - The SDK intentionally avoids silent fiscal defaults for required tax fields such as `tributacaoMunicipal` and `totTrib`.
58
- - `emissao.totTrib` follows an `xs:choice`: provide exactly one branch among value totals, percentage totals, `indTotTrib`, or `pTotTribSN`.
58
+ - The JSON validator restricts `totTrib` by regime: non-Simples requires all three percentage totals; MEI requires `indTotTrib="0"`; ME/EPP requires `pTotTribSN`. Value totals remain typed but are rejected by the current regime matrix.
59
59
  - `buildDpsId` must return a 45-character Id beginning with `DPS`.
60
- - `emitirNfse` signs the DPS before transport and throws `EmitirNotaError` when SEFIN rejects or no authorized XML is returned.
60
+ - `emitirNfse` checks certificate validity before signing and again before transmission. Holder matching is an explicit `assertCertificateForProvider` call, not an automatic requirement.
61
+ - JSON environment precedence is options > request > restricted; external XML derives its environment from `tpAmb` and rejects explicit conflicts.
62
+ - `client.invoices.get(key, { ambiente })` supports an explicit environment; otherwise defaults.ambiente > constructor. It does not remember the last emission.
63
+ - Currency discrepancies are warnings; explicit values retain priority. Service amounts must remain positive after rounding; all foreign amounts are serialized with two decimals.
64
+ - `validateDpsJsonRequest` returns independent normalized data, errors in `issues`, and non-blocking `warnings`.
65
+ - `EmitirNotaError` covers SEFIN rejection and incomplete/invalid HTTP 2xx authorization responses, including missing/invalid/conflicting 50-digit access keys. Reconcile before retrying; the SDK never retries automatically.
61
66
  - `sefinClient.ts` uses PEM key/cert material for mTLS, not the raw PFX in the Node TLS handshake.
62
67
  - SEFIN latency metrics are off by default, enabled through `NFSE_SEFIN_LATENCY_METRICS`, and emitted for the external request only; keep them separate from API route latency when integrating observability.
63
68
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@useinvio/nfse-sdk",
3
- "version": "2.2.0",
3
+ "version": "3.0.0",
4
4
  "description": "TypeScript SDK for Brazil's national NFS-e: JSON to DPS/XML, XMLDSIG, GZip/Base64, mTLS and SEFIN errors.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",