ksef-client-ts 0.9.0 → 0.9.1

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/README.md CHANGED
@@ -6,7 +6,7 @@ TypeScript client for the Polish National e-Invoice System (KSeF) API v2.
6
6
 
7
7
  ## Features
8
8
 
9
- - **Complete API coverage** — KSeF API v2.6.0, types aligned with the official OpenAPI spec
9
+ - **Complete API coverage** — KSeF API v2.6.1, types aligned with the official OpenAPI spec
10
10
  - **Offline invoice mode** — full lifecycle for all 4 KSeF offline modes with QR KOD I + KOD II signing, deadline tracking, local storage, and technical correction
11
11
  - **Full-featured CLI** — `ksef` with 15 command groups for auth, sessions, invoices, offline, batch upload, export, and more
12
12
  - **High-level workflows** — auth, online/batch sessions, invoice export — full lifecycle in a single call
package/dist/cli.js CHANGED
@@ -238,10 +238,21 @@ var init_ksef_bad_request_error = __esm({
238
238
  });
239
239
 
240
240
  // src/errors/ksef-auth-status-error.ts
241
+ var KSeFAuthStatusError;
241
242
  var init_ksef_auth_status_error = __esm({
242
243
  "src/errors/ksef-auth-status-error.ts"() {
243
244
  "use strict";
244
245
  init_ksef_error();
246
+ KSeFAuthStatusError = class extends KSeFError {
247
+ referenceNumber;
248
+ statusDescription;
249
+ constructor(message, referenceNumber, statusDescription) {
250
+ super(message);
251
+ this.name = "KSeFAuthStatusError";
252
+ this.referenceNumber = referenceNumber;
253
+ this.statusDescription = statusDescription;
254
+ }
255
+ };
245
256
  }
246
257
  });
247
258
 
@@ -281,6 +292,14 @@ var init_ksef_validation_error = __esm({
281
292
  }
282
293
  });
283
294
 
295
+ // src/errors/ksef-metadata-pagination-error.ts
296
+ var init_ksef_metadata_pagination_error = __esm({
297
+ "src/errors/ksef-metadata-pagination-error.ts"() {
298
+ "use strict";
299
+ init_ksef_error();
300
+ }
301
+ });
302
+
284
303
  // src/errors/error-codes.ts
285
304
  function hasErrorCode(body, code) {
286
305
  return !!body?.exception?.exceptionDetailList?.some((d) => d.exceptionCode === code);
@@ -418,6 +437,7 @@ var init_errors = __esm({
418
437
  init_ksef_auth_status_error();
419
438
  init_ksef_session_expired_error();
420
439
  init_ksef_validation_error();
440
+ init_ksef_metadata_pagination_error();
421
441
  init_ksef_batch_timeout_error();
422
442
  init_ksef_unknown_public_key_error();
423
443
  init_ksef_circuit_open_error();
@@ -2095,6 +2115,44 @@ var init_tokens = __esm({
2095
2115
  }
2096
2116
  });
2097
2117
 
2118
+ // src/validation/patterns.ts
2119
+ function isValidNip(value) {
2120
+ if (!Nip.test(value)) return false;
2121
+ let sum = 0;
2122
+ for (let i = 0; i < 9; i++) {
2123
+ sum += Number(value[i]) * NIP_WEIGHTS[i];
2124
+ }
2125
+ const checksum = sum % 11;
2126
+ return checksum !== 10 && checksum === Number(value[9]);
2127
+ }
2128
+ function isValidPesel(value) {
2129
+ if (!Pesel.test(value)) return false;
2130
+ let sum = 0;
2131
+ for (let i = 0; i < 10; i++) {
2132
+ sum += Number(value[i]) * PESEL_WEIGHTS[i];
2133
+ }
2134
+ const checksum = (10 - sum % 10) % 10;
2135
+ return checksum === Number(value[10]);
2136
+ }
2137
+ function isValidCertificateSerialNumber(value) {
2138
+ return CertificateSerialNumber.test(value);
2139
+ }
2140
+ var NIP_PATTERN_CORE, VAT_UE_PATTERN_CORE, Nip, VatUe, NipVatUe, Pesel, CertificateSerialNumber, NIP_WEIGHTS, PESEL_WEIGHTS;
2141
+ var init_patterns = __esm({
2142
+ "src/validation/patterns.ts"() {
2143
+ "use strict";
2144
+ NIP_PATTERN_CORE = "[1-9]((\\d[1-9])|([1-9]\\d))\\d{7}";
2145
+ VAT_UE_PATTERN_CORE = "(ATU\\d{8}|BE[01]{1}\\d{9}|BG\\d{9,10}|CY\\d{8}[A-Z]|CZ\\d{8,10}|DE\\d{9}|DK\\d{8}|EE\\d{9}|EL\\d{9}|ES([A-Z]\\d{8}|\\d{8}[A-Z]|[A-Z]\\d{7}[A-Z])|FI\\d{8}|FR[A-Z0-9]{2}\\d{9}|HR\\d{11}|HU\\d{8}|IE(\\d{7}[A-Z]{2}|\\d[A-Z0-9+*]\\d{5}[A-Z])|IT\\d{11}|LT(\\d{9}|\\d{12})|LU\\d{8}|LV\\d{11}|MT\\d{8}|NL[A-Z0-9+*]{12}|PT\\d{9}|RO\\d{2,10}|SE\\d{12}|SI\\d{8}|SK\\d{10}|XI((\\d{9}|\\d{12})|(GD|HA)\\d{3}))";
2146
+ Nip = new RegExp(`^${NIP_PATTERN_CORE}$`);
2147
+ VatUe = new RegExp(`^${VAT_UE_PATTERN_CORE}$`);
2148
+ NipVatUe = new RegExp(`^${NIP_PATTERN_CORE}-${VAT_UE_PATTERN_CORE}$`);
2149
+ Pesel = /^\d{2}(?:0[1-9]|1[0-2]|2[1-9]|3[0-2]|4[1-9]|5[0-2]|6[1-9]|7[0-2]|8[1-9]|9[0-2])\d{7}$/;
2150
+ CertificateSerialNumber = /^[0-9A-F]{16}$/;
2151
+ NIP_WEIGHTS = [6, 5, 7, 2, 3, 4, 5, 6, 7];
2152
+ PESEL_WEIGHTS = [1, 3, 7, 9, 1, 3, 7, 9, 1, 3];
2153
+ }
2154
+ });
2155
+
2098
2156
  // src/services/certificates.ts
2099
2157
  var CertificateApiService;
2100
2158
  var init_certificates = __esm({
@@ -2102,6 +2160,8 @@ var init_certificates = __esm({
2102
2160
  "use strict";
2103
2161
  init_rest_request();
2104
2162
  init_routes();
2163
+ init_ksef_validation_error();
2164
+ init_patterns();
2105
2165
  CertificateApiService = class {
2106
2166
  restClient;
2107
2167
  constructor(restClient) {
@@ -2128,6 +2188,14 @@ var init_certificates = __esm({
2128
2188
  return response.body;
2129
2189
  }
2130
2190
  async retrieve(request) {
2191
+ for (const serial of request.certificateSerialNumbers ?? []) {
2192
+ if (!isValidCertificateSerialNumber(serial)) {
2193
+ throw KSeFValidationError.fromField(
2194
+ "certificateSerialNumbers",
2195
+ `Invalid certificate serial number "${serial}": must be exactly 16 uppercase hex characters (^[0-9A-F]{16}$)`
2196
+ );
2197
+ }
2198
+ }
2131
2199
  const req = RestRequest.post(Routes.Certificates.retrieve).body(request);
2132
2200
  const response = await this.restClient.execute(req);
2133
2201
  return response.body;
@@ -6334,40 +6402,6 @@ var init_char_validity = __esm({
6334
6402
  }
6335
6403
  });
6336
6404
 
6337
- // src/validation/patterns.ts
6338
- function isValidNip(value) {
6339
- if (!Nip.test(value)) return false;
6340
- let sum = 0;
6341
- for (let i = 0; i < 9; i++) {
6342
- sum += Number(value[i]) * NIP_WEIGHTS[i];
6343
- }
6344
- const checksum = sum % 11;
6345
- return checksum !== 10 && checksum === Number(value[9]);
6346
- }
6347
- function isValidPesel(value) {
6348
- if (!Pesel.test(value)) return false;
6349
- let sum = 0;
6350
- for (let i = 0; i < 10; i++) {
6351
- sum += Number(value[i]) * PESEL_WEIGHTS[i];
6352
- }
6353
- const checksum = (10 - sum % 10) % 10;
6354
- return checksum === Number(value[10]);
6355
- }
6356
- var NIP_PATTERN_CORE, VAT_UE_PATTERN_CORE, Nip, VatUe, NipVatUe, Pesel, NIP_WEIGHTS, PESEL_WEIGHTS;
6357
- var init_patterns = __esm({
6358
- "src/validation/patterns.ts"() {
6359
- "use strict";
6360
- NIP_PATTERN_CORE = "[1-9]((\\d[1-9])|([1-9]\\d))\\d{7}";
6361
- VAT_UE_PATTERN_CORE = "(ATU\\d{8}|BE[01]{1}\\d{9}|BG\\d{9,10}|CY\\d{8}[A-Z]|CZ\\d{8,10}|DE\\d{9}|DK\\d{8}|EE\\d{9}|EL\\d{9}|ES([A-Z]\\d{8}|\\d{8}[A-Z]|[A-Z]\\d{7}[A-Z])|FI\\d{8}|FR[A-Z0-9]{2}\\d{9}|HR\\d{11}|HU\\d{8}|IE(\\d{7}[A-Z]{2}|\\d[A-Z0-9+*]\\d{5}[A-Z])|IT\\d{11}|LT(\\d{9}|\\d{12})|LU\\d{8}|LV\\d{11}|MT\\d{8}|NL[A-Z0-9+*]{12}|PT\\d{9}|RO\\d{2,10}|SE\\d{12}|SI\\d{8}|SK\\d{10}|XI((\\d{9}|\\d{12})|(GD|HA)\\d{3}))";
6362
- Nip = new RegExp(`^${NIP_PATTERN_CORE}$`);
6363
- VatUe = new RegExp(`^${VAT_UE_PATTERN_CORE}$`);
6364
- NipVatUe = new RegExp(`^${NIP_PATTERN_CORE}-${VAT_UE_PATTERN_CORE}$`);
6365
- Pesel = /^\d{2}(?:0[1-9]|1[0-2]|2[1-9]|3[0-2]|4[1-9]|5[0-2]|6[1-9]|7[0-2]|8[1-9]|9[0-2])\d{7}$/;
6366
- NIP_WEIGHTS = [6, 5, 7, 2, 3, 4, 5, 6, 7];
6367
- PESEL_WEIGHTS = [1, 3, 7, 9, 1, 3, 7, 9, 1, 3];
6368
- }
6369
- });
6370
-
6371
6405
  // src/validation/invoice-validator.ts
6372
6406
  var invoice_validator_exports = {};
6373
6407
  __export(invoice_validator_exports, {
@@ -7288,6 +7322,33 @@ function clearCredentials() {
7288
7322
  init_polling();
7289
7323
  init_auth_xml_builder();
7290
7324
  init_with_key_rotation_retry();
7325
+ init_ksef_auth_status_error();
7326
+ var AUTH_STATUS_SUCCESS = 200;
7327
+ var AUTH_STATUS_IN_PROGRESS = 100;
7328
+ async function awaitAuthentication(client, referenceNumber, authToken, pollOptions) {
7329
+ const final = await pollUntil(
7330
+ () => client.auth.getAuthStatus(referenceNumber, authToken),
7331
+ (s) => s.status.code !== AUTH_STATUS_IN_PROGRESS,
7332
+ { ...pollOptions, description: `auth ${referenceNumber}` }
7333
+ );
7334
+ if (final.status.code !== AUTH_STATUS_SUCCESS) {
7335
+ const details = final.status.details?.length ? ` (${final.status.details.join("; ")})` : "";
7336
+ throw new KSeFAuthStatusError(
7337
+ `Authentication failed with status ${final.status.code}: ${final.status.description}${details}`,
7338
+ referenceNumber,
7339
+ final.status.description
7340
+ );
7341
+ }
7342
+ const tokens = await client.auth.getAccessToken(authToken);
7343
+ client.authManager.setAccessToken(tokens.accessToken.token);
7344
+ client.authManager.setRefreshToken(tokens.refreshToken.token);
7345
+ return {
7346
+ accessToken: tokens.accessToken.token,
7347
+ accessTokenValidUntil: tokens.accessToken.validUntil,
7348
+ refreshToken: tokens.refreshToken.token,
7349
+ refreshTokenValidUntil: tokens.refreshToken.validUntil
7350
+ };
7351
+ }
7291
7352
  async function authenticateWithToken(client, options) {
7292
7353
  const challenge2 = await client.auth.getChallenge();
7293
7354
  await client.crypto.init();
@@ -7302,20 +7363,7 @@ async function authenticateWithToken(client, options) {
7302
7363
  });
7303
7364
  });
7304
7365
  const authToken = submitResult.authenticationToken.token;
7305
- await pollUntil(
7306
- () => client.auth.getAuthStatus(submitResult.referenceNumber, authToken),
7307
- (s) => s.status.code !== 100,
7308
- { ...options.pollOptions, description: `auth ${submitResult.referenceNumber}` }
7309
- );
7310
- const tokens = await client.auth.getAccessToken(authToken);
7311
- client.authManager.setAccessToken(tokens.accessToken.token);
7312
- client.authManager.setRefreshToken(tokens.refreshToken.token);
7313
- return {
7314
- accessToken: tokens.accessToken.token,
7315
- accessTokenValidUntil: tokens.accessToken.validUntil,
7316
- refreshToken: tokens.refreshToken.token,
7317
- refreshTokenValidUntil: tokens.refreshToken.validUntil
7318
- };
7366
+ return awaitAuthentication(client, submitResult.referenceNumber, authToken, options.pollOptions);
7319
7367
  }
7320
7368
 
7321
7369
  // src/cli/session-recovery.ts