@assinafy/cli 2.5.0 → 2.5.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/dist/api.js CHANGED
@@ -17,6 +17,8 @@ var ApiError = class _ApiError extends AssinafyError {
17
17
  wwwAuthenticate;
18
18
  /** Server retry delay (seconds or HTTP date), when provided. */
19
19
  retryAfter;
20
+ /** Rate-limit window reported by the API, when the headers are present. */
21
+ rateLimit;
20
22
  constructor(message, statusCode, responseData = null, options) {
21
23
  super(message, { statusCode, responseData }, options);
22
24
  this.name = "ApiError";
@@ -24,6 +26,7 @@ var ApiError = class _ApiError extends AssinafyError {
24
26
  this.responseData = responseData;
25
27
  this.wwwAuthenticate = options?.wwwAuthenticate;
26
28
  this.retryAfter = options?.retryAfter;
29
+ this.rateLimit = options?.rateLimit;
27
30
  }
28
31
  static fromResponse(statusCode, responseData, headers = {}) {
29
32
  if (Buffer.isBuffer(responseData) || responseData instanceof ArrayBuffer) {
@@ -37,13 +40,23 @@ var ApiError = class _ApiError extends AssinafyError {
37
40
  const data = responseData ?? {};
38
41
  const rawMessage = data.message;
39
42
  const rawError = data.error;
40
- const message = typeof rawMessage === "string" && rawMessage.length > 0 ? rawMessage : typeof rawError === "string" ? rawError : "API request failed";
43
+ const description = data.error_description;
44
+ const message = typeof rawMessage === "string" && rawMessage.length > 0 ? rawMessage : typeof rawError === "string" ? typeof description === "string" && description.length > 0 ? `${rawError}: ${description}` : rawError : "API request failed";
41
45
  return new _ApiError(message, statusCode, responseData, {
42
46
  wwwAuthenticate: typeof headers["www-authenticate"] === "string" ? headers["www-authenticate"] : void 0,
43
- retryAfter: typeof headers["retry-after"] === "string" ? headers["retry-after"] : void 0
47
+ retryAfter: typeof headers["retry-after"] === "string" ? headers["retry-after"] : void 0,
48
+ rateLimit: parseRateLimit(headers)
44
49
  });
45
50
  }
46
51
  };
52
+ function parseRateLimit(headers) {
53
+ const info = {};
54
+ for (const key of ["limit", "remaining", "reset"]) {
55
+ const value = Number.parseInt(String(headers[`x-rate-limit-${key}`]), 10);
56
+ if (Number.isFinite(value)) info[key] = value;
57
+ }
58
+ return Object.keys(info).length > 0 ? info : void 0;
59
+ }
47
60
  var ValidationError = class extends AssinafyError {
48
61
  errors;
49
62
  constructor(message = "Validation failed", errors = {}) {
@@ -197,6 +210,16 @@ function validateSignerOptions(input) {
197
210
  `notification_methods must contain only: ${NOTIFICATION_METHODS.join(", ")}`
198
211
  );
199
212
  }
213
+ const notifications = input.notification_methods ?? [];
214
+ if (notifications.length > 1) {
215
+ throw new ValidationError("notification_methods accepts exactly one method per signer");
216
+ }
217
+ const [notification] = notifications;
218
+ if (notification !== void 0 && (input.verification_method === "Email" || input.verification_method === "Whatsapp") && notification !== input.verification_method) {
219
+ throw new ValidationError(
220
+ `${input.verification_method} verification requires the ${input.verification_method} notification method`
221
+ );
222
+ }
200
223
  if (input.step !== void 0 && !Number.isInteger(input.step)) {
201
224
  throw new ValidationError("step must be an integer");
202
225
  }
@@ -933,7 +956,7 @@ var DocumentResource = class extends BaseResource {
933
956
  async createFromTemplate(templateId, signers, options = {}, accountId) {
934
957
  const tmplId = this.requireId(templateId, "Template ID");
935
958
  const accId = this.accountId(accountId);
936
- validateTemplateSigners(signers, { requireId: true, singleNotification: true });
959
+ validateTemplateSigners(signers, { requireId: true });
937
960
  if (options.expires_at !== void 0) requireIso8601(options.expires_at, "expires_at");
938
961
  if (options.editor_fields !== void 0 && (!Array.isArray(options.editor_fields) || options.editor_fields.some((field) => !field?.field_id || typeof field.value !== "string"))) {
939
962
  throw new ValidationError("editor_fields must contain field_id and value");
@@ -952,7 +975,7 @@ var DocumentResource = class extends BaseResource {
952
975
  async estimateCostFromTemplate(templateId, signers, accountId) {
953
976
  const tmplId = this.requireId(templateId, "Template ID");
954
977
  const accId = this.accountId(accountId);
955
- validateTemplateSigners(signers, { requireId: false, singleNotification: false });
978
+ validateTemplateSigners(signers, { requireId: false });
956
979
  return this.call(
957
980
  "Failed to estimate cost from template",
958
981
  () => this.http.post(`/accounts/${accId}/templates/${tmplId}/documents/estimate-cost`, { signers })
@@ -992,33 +1015,21 @@ var DocumentResource = class extends BaseResource {
992
1015
  );
993
1016
  }
994
1017
  /**
995
- * `PUT /public/documents/{document_id}/send-token` — send the 6-digit
996
- * verification token to the signer's email / WhatsApp. Pass `{ email }` for
997
- * the published OpenAPI body or a string plus channel for the live legacy body.
1018
+ * `PUT /public/documents/{document_id}/send-token` — email a one-time access
1019
+ * token to a signer who has not signed a pending document yet. The API sends
1020
+ * `{ channel: 'email', recipient }`; only the `email` channel is accepted.
1021
+ * The `{ email }` form is kept for existing callers and sends the same body.
998
1022
  */
999
1023
  async sendToken(documentId, recipient, channel = "email") {
1000
1024
  const id = this.requireId(documentId, "Document ID");
1001
- if (typeof recipient === "object") {
1002
- requireEmail(recipient?.email);
1003
- return this.call(
1004
- "Failed to send signing token",
1005
- () => this.http.put(
1006
- `/public/documents/${id}/send-token`,
1007
- { email: recipient.email },
1008
- publicRequestConfig()
1009
- )
1010
- );
1011
- }
1012
- if (!recipient) throw new ValidationError("recipient is required");
1013
- if (channel !== "email" && channel !== "whatsapp") {
1014
- throw new ValidationError("channel must be email or whatsapp");
1015
- }
1016
- if (channel === "email") requireEmail(recipient);
1025
+ const email = typeof recipient === "object" ? recipient?.email : recipient;
1026
+ if (channel !== "email") throw new ValidationError("channel must be email");
1027
+ requireEmail(email);
1017
1028
  return this.call(
1018
1029
  "Failed to send signing token",
1019
1030
  () => this.http.put(
1020
1031
  `/public/documents/${id}/send-token`,
1021
- { recipient, channel },
1032
+ { channel, recipient: email },
1022
1033
  publicRequestConfig()
1023
1034
  )
1024
1035
  );
@@ -1061,9 +1072,6 @@ function validateTemplateSigners(signers, options) {
1061
1072
  if (options.requireId && signer.step !== void 0 && Number(signer.step) < 1) {
1062
1073
  throw new ValidationError("template signer step must be a positive integer");
1063
1074
  }
1064
- if (options.singleNotification && Array.isArray(signer.notification_methods) && signer.notification_methods.length > 1) {
1065
- throw new ValidationError("template signers accept at most one notification method");
1066
- }
1067
1075
  }
1068
1076
  if (options.requireId) {
1069
1077
  validateSigningSteps(signers);
@@ -1348,7 +1356,7 @@ var OAuthResource = class extends BaseResource {
1348
1356
  });
1349
1357
  }
1350
1358
  /**
1351
- * `POST /oauth/token` — code exchange or refresh; returns the flat OAuth JSON body.
1359
+ * `POST /oauth/token` — code exchange, refresh, or RFC 8693 token exchange; returns the flat OAuth JSON body.
1352
1360
  * A refresh must return a new `refresh_token`; otherwise this throws `ValidationError`.
1353
1361
  */
1354
1362
  async token(payload) {
@@ -1365,8 +1373,20 @@ var OAuthResource = class extends BaseResource {
1365
1373
  }
1366
1374
  } else if (payload.grant_type === "refresh_token") {
1367
1375
  required(payload.refresh_token, "refresh_token");
1376
+ } else if (payload.grant_type === TOKEN_EXCHANGE) {
1377
+ required(payload.client_secret, "client_secret");
1378
+ httpsUrl(required(payload.resource, "resource"), "resource");
1379
+ required(payload.subject_token, "subject_token");
1380
+ for (const field of ["subject_token_type", "requested_token_type"]) {
1381
+ const value = payload[field];
1382
+ if ((value !== void 0 || field === "subject_token_type") && value !== ACCESS_TOKEN_TYPE) {
1383
+ throw new ValidationError(`${field} must be ${ACCESS_TOKEN_TYPE}`);
1384
+ }
1385
+ }
1368
1386
  } else {
1369
- throw new ValidationError("grant_type must be authorization_code or refresh_token");
1387
+ throw new ValidationError(
1388
+ `grant_type must be authorization_code, refresh_token or ${TOKEN_EXCHANGE}`
1389
+ );
1370
1390
  }
1371
1391
  const tokens = await this.call(
1372
1392
  "OAuth token request failed",
@@ -1399,6 +1419,8 @@ var OAuthResource = class extends BaseResource {
1399
1419
  return this.call("Failed to fetch OAuth userinfo", () => this.http.get("/oauth/userinfo"));
1400
1420
  }
1401
1421
  };
1422
+ var TOKEN_EXCHANGE = "urn:ietf:params:oauth:grant-type:token-exchange";
1423
+ var ACCESS_TOKEN_TYPE = "urn:ietf:params:oauth:token-type:access_token";
1402
1424
  function required(value, name) {
1403
1425
  if (typeof value !== "string" || !value.trim()) throw new ValidationError(`${name} is required`);
1404
1426
  return value;
@@ -1427,27 +1449,32 @@ var SignerDocumentsResource = class extends BaseResource {
1427
1449
  () => this.http.get(`/signers/${sid}/document`, signerAccessConfig(code))
1428
1450
  );
1429
1451
  }
1430
- /** `GET /signers/{signer_id}/documents?signer-access-code=…` */
1452
+ /**
1453
+ * `GET /signers/{signer_id}/documents` — with a signer access code, or with
1454
+ * the client's owner credentials when the code is omitted (the signer must
1455
+ * belong to that workspace).
1456
+ */
1431
1457
  async list(signerId, signerAccessCode, params = {}) {
1432
1458
  const sid = this.requireId(signerId, "Signer ID");
1433
- const code = this.requireId(signerAccessCode, "signer-access-code");
1459
+ const config = this.signerOrOwnerConfig(signerAccessCode, { ...params });
1434
1460
  return this.callList(
1435
1461
  "Failed to list signer documents",
1436
- () => this.http.get(
1437
- `/signers/${sid}/documents`,
1438
- signerAccessConfig(code, params)
1439
- )
1462
+ () => this.http.get(`/signers/${sid}/documents`, config)
1440
1463
  );
1441
1464
  }
1442
- /** `GET /signers/{signer_id}/documents/search?signer-access-code=…&search=…` */
1465
+ /** `GET /signers/{signer_id}/documents/search?search=…` — access code or owner credentials. */
1443
1466
  async search(signerId, search, signerAccessCode) {
1444
1467
  const sid = this.requireId(signerId, "Signer ID");
1445
- const code = this.requireId(signerAccessCode, "signer-access-code");
1468
+ const config = this.signerOrOwnerConfig(signerAccessCode, { search });
1446
1469
  return this.callList(
1447
1470
  "Failed to search signer documents",
1448
- () => this.http.get(`/signers/${sid}/documents/search`, signerAccessConfig(code, { search }))
1471
+ () => this.http.get(`/signers/${sid}/documents/search`, config)
1449
1472
  );
1450
1473
  }
1474
+ signerOrOwnerConfig(signerAccessCode, params) {
1475
+ if (signerAccessCode === void 0) return { params: cleanParams(params) };
1476
+ return signerAccessConfig(this.requireId(signerAccessCode, "signer-access-code"), params);
1477
+ }
1451
1478
  /**
1452
1479
  * Public `GET /signers/{signer_id}/documents/{document_id}/download/{artifact}`.
1453
1480
  * When an access code is supplied, the SDK first verifies that it belongs to