@assinafy/cli 2.3.0 → 2.4.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
@@ -131,6 +131,19 @@ function cleanParams(params) {
131
131
  }
132
132
  return out;
133
133
  }
134
+ function stripEmpty(body) {
135
+ const out = {};
136
+ for (const [key, value] of Object.entries(body)) {
137
+ if (value !== void 0 && value !== null) {
138
+ out[key] = value;
139
+ }
140
+ }
141
+ return out;
142
+ }
143
+ function appendFilePart(form, data, contentType, fileName) {
144
+ const view = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
145
+ form.append("file", new Blob([view], { type: contentType }), fileName);
146
+ }
134
147
  function publicRequestConfig(config = {}) {
135
148
  return {
136
149
  ...config,
@@ -371,7 +384,7 @@ function buildAssignmentPayload(payload, options = {}) {
371
384
  if (!options.skipDigitalCertificateStepValidation) {
372
385
  validateSigningSteps(normalisedSigners);
373
386
  }
374
- return cleanParams({
387
+ return stripEmpty({
375
388
  method: payload.method ?? "virtual",
376
389
  signers: normalisedSigners,
377
390
  message: payload.message,
@@ -402,7 +415,7 @@ function normaliseSignerRef(ref, options) {
402
415
  const input = ref;
403
416
  const id = input.id ?? input.signer_id;
404
417
  validateSignerOptions(input);
405
- const normalised = cleanParams({
418
+ const normalised = stripEmpty({
406
419
  id,
407
420
  verification_method: input.verification_method,
408
421
  notification_methods: input.notification_methods,
@@ -601,6 +614,7 @@ var AuthenticationResource = class extends BaseResource {
601
614
  import { promises as fs } from "fs";
602
615
  import path from "path";
603
616
  var MAX_UPLOAD_BYTES = 25 * 1024 * 1024;
617
+ var DOCUMENT_SORTS = ["name", "-name", "updated_at", "-updated_at"];
604
618
  var READY_STATUSES = /* @__PURE__ */ new Set([
605
619
  "metadata_ready",
606
620
  "pending_signature",
@@ -648,7 +662,7 @@ var DocumentResource = class extends BaseResource {
648
662
  */
649
663
  async list(params = {}, accountId) {
650
664
  const id = this.accountId(accountId);
651
- requireSort(params.sort, ["name", "-name", "updated_at", "-updated_at"]);
665
+ requireSort(params.sort, DOCUMENT_SORTS);
652
666
  return this.callList(
653
667
  "Failed to list documents",
654
668
  () => this.http.get(`/accounts/${id}/documents`, {
@@ -666,7 +680,7 @@ var DocumentResource = class extends BaseResource {
666
680
  */
667
681
  async search(params = {}, accountId) {
668
682
  const id = this.accountId(accountId);
669
- requireSort(params.sort, ["name", "-name", "updated_at", "-updated_at"]);
683
+ requireSort(params.sort, DOCUMENT_SORTS);
670
684
  return this.callList(
671
685
  "Failed to search documents",
672
686
  () => this.http.get(`/accounts/${id}/documents/search`, {
@@ -1021,8 +1035,7 @@ function validateUpload(buffer, fileName) {
1021
1035
  }
1022
1036
  function buildUploadForm(buffer, fileName, name, metadata) {
1023
1037
  const form = new FormData();
1024
- const view = new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
1025
- form.append("file", new Blob([view], { type: "application/pdf" }), fileName);
1038
+ appendFilePart(form, buffer, "application/pdf", fileName);
1026
1039
  form.append("name", name);
1027
1040
  if (metadata) {
1028
1041
  form.append("metadata", JSON.stringify(metadata));
@@ -1137,18 +1150,26 @@ function validationConfig(signerAccessCode) {
1137
1150
 
1138
1151
  // src/api/resources/oauth.ts
1139
1152
  import { createHash, randomBytes, timingSafeEqual } from "crypto";
1153
+ function formRequestConfig() {
1154
+ return publicRequestConfig({
1155
+ headers: { "Content-Type": "application/x-www-form-urlencoded" }
1156
+ });
1157
+ }
1140
1158
  var OAuthResource = class extends BaseResource {
1141
1159
  /** `GET /.well-known/oauth-protected-resource` at the API origin, outside `/v1`. */
1142
1160
  async metadata() {
1143
- return this.call(
1144
- "Failed to discover OAuth resource",
1145
- () => this.http.get(
1161
+ return this.call("Failed to discover OAuth resource", () => {
1162
+ const baseURL = this.http.defaults.baseURL;
1163
+ if (!baseURL) {
1164
+ throw new ValidationError("A base URL is required to discover OAuth metadata");
1165
+ }
1166
+ return this.http.get(
1146
1167
  "/.well-known/oauth-protected-resource",
1147
1168
  publicRequestConfig({
1148
- baseURL: new URL(this.http.defaults.baseURL).origin
1169
+ baseURL: new URL(baseURL).origin
1149
1170
  })
1150
- )
1151
- );
1171
+ );
1172
+ });
1152
1173
  }
1153
1174
  /** Read RFC 8414 metadata from an HTTPS issuer and verify its issuer identity. */
1154
1175
  async discovery(issuer) {
@@ -1249,7 +1270,10 @@ var OAuthResource = class extends BaseResource {
1249
1270
  resource: request.resource
1250
1271
  });
1251
1272
  }
1252
- /** `POST /oauth/token` — code exchange or refresh; returns the flat OAuth JSON body. */
1273
+ /**
1274
+ * `POST /oauth/token` — code exchange or refresh; returns the flat OAuth JSON body.
1275
+ * A refresh must return a new `refresh_token`; otherwise this throws `ValidationError`.
1276
+ */
1253
1277
  async token(payload) {
1254
1278
  required(payload.client_id, "client_id");
1255
1279
  if (payload.client_secret !== void 0) required(payload.client_secret, "client_secret");
@@ -1267,10 +1291,18 @@ var OAuthResource = class extends BaseResource {
1267
1291
  } else {
1268
1292
  throw new ValidationError("grant_type must be authorization_code or refresh_token");
1269
1293
  }
1270
- return this.call(
1294
+ const tokens = await this.call(
1271
1295
  "OAuth token request failed",
1272
- () => this.http.post("/oauth/token", payload, publicRequestConfig())
1296
+ () => this.http.post("/oauth/token", payload, formRequestConfig())
1273
1297
  );
1298
+ const next = tokens?.refresh_token;
1299
+ if (payload.grant_type === "refresh_token" && (typeof next !== "string" || !next.trim() || next === payload.refresh_token)) {
1300
+ throw new ValidationError(
1301
+ "OAuth refresh response has no new refresh_token; the submitted refresh token may already be retired, so do not reuse it: reconnect instead",
1302
+ { field: "refresh_token" }
1303
+ );
1304
+ }
1305
+ return tokens;
1274
1306
  }
1275
1307
  /** `POST /oauth/revoke` — revoke an access/refresh token; success has no required body. */
1276
1308
  async revoke(payload) {
@@ -1282,7 +1314,7 @@ var OAuthResource = class extends BaseResource {
1282
1314
  }
1283
1315
  await this.call(
1284
1316
  "OAuth revocation failed",
1285
- () => this.http.post("/oauth/revoke", payload, publicRequestConfig())
1317
+ () => this.http.post("/oauth/revoke", payload, formRequestConfig())
1286
1318
  );
1287
1319
  }
1288
1320
  /** `GET /oauth/userinfo` — flat OIDC claims; requires a bearer token with `openid`. */
@@ -1680,12 +1712,18 @@ var SignerResource = class extends BaseResource {
1680
1712
  requireEmail(email);
1681
1713
  try {
1682
1714
  const lower = email.toLowerCase();
1683
- for (let page = 1; ; page++) {
1715
+ for (let page = 1; page <= MAX_SEARCH_PAGES; page++) {
1684
1716
  const { data, meta } = await this.list({ search: email, per_page: 100, page }, accountId);
1685
1717
  const match = data.find((s) => (s.email ?? "").toLowerCase() === lower);
1686
1718
  if (match) return match;
1687
- if (!data.length || !meta?.last_page || page >= meta.last_page) return null;
1719
+ if (!data.length) return null;
1720
+ if (meta?.last_page !== void 0) {
1721
+ if (page >= meta.last_page) return null;
1722
+ } else if (data.length < 100) {
1723
+ return null;
1724
+ }
1688
1725
  }
1726
+ throw new ValidationError("Signer email search exceeded the maximum page count");
1689
1727
  } catch (err) {
1690
1728
  if (err instanceof ApiError && err.statusCode === 404) {
1691
1729
  return null;
@@ -1694,6 +1732,7 @@ var SignerResource = class extends BaseResource {
1694
1732
  }
1695
1733
  }
1696
1734
  };
1735
+ var MAX_SEARCH_PAGES = 100;
1697
1736
  function normaliseSignerPayload(payload, useGovernmentId = false) {
1698
1737
  const normalised = {
1699
1738
  full_name: payload.full_name,
@@ -1708,7 +1747,7 @@ function normaliseSignerPayload(payload, useGovernmentId = false) {
1708
1747
  if ("metadata" in payload && payload.metadata !== void 0) {
1709
1748
  normalised.metadata = payload.metadata;
1710
1749
  }
1711
- return cleanParams(normalised);
1750
+ return stripEmpty(normalised);
1712
1751
  }
1713
1752
 
1714
1753
  // src/api/resources/tags.ts
@@ -1729,7 +1768,7 @@ var TagResource = class extends BaseResource {
1729
1768
  const id = this.accountId(accountId);
1730
1769
  return this.call(
1731
1770
  "Failed to create tag",
1732
- () => this.http.post(`/accounts/${id}/tags`, cleanParams({ ...payload }))
1771
+ () => this.http.post(`/accounts/${id}/tags`, stripEmpty({ ...payload }))
1733
1772
  );
1734
1773
  }
1735
1774
  /**
@@ -2002,9 +2041,8 @@ var WorkspaceResource = class extends BaseResource {
2002
2041
  throw new ValidationError("Logo buffer is required");
2003
2042
  }
2004
2043
  const contentType = options.contentType ?? "image/png";
2005
- const view = new Uint8Array(logo.buffer, logo.byteOffset, logo.byteLength);
2006
2044
  const form = new FormData();
2007
- form.append("file", new Blob([view], { type: contentType }), options.fileName ?? "logo.png");
2045
+ appendFilePart(form, logo, contentType, options.fileName ?? "logo.png");
2008
2046
  return this.call(
2009
2047
  "Failed to upload workspace logo",
2010
2048
  () => this.http.post(`/accounts/${id}/logo`, form, {
@@ -2340,6 +2378,8 @@ export {
2340
2378
  WebhookVerifier,
2341
2379
  WorkspaceResource,
2342
2380
  buildAssignmentPayload,
2343
- normalizeBaseUrl
2381
+ normalizeBaseUrl,
2382
+ requireDocumentArtifactName,
2383
+ requireSignerImageType
2344
2384
  };
2345
2385
  //# sourceMappingURL=api.js.map