@btps/sdk 1.2.0-rc.1 → 1.2.0-rc.3

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.
Files changed (30) hide show
  1. package/CHANGELOG.md +108 -0
  2. package/dist/client/index.d.ts +1 -0
  3. package/dist/client/index.js +7 -1
  4. package/dist/core/crypto/decryptVerify.js +6 -2
  5. package/dist/core/crypto/types.d.ts +3 -0
  6. package/dist/core/server/constants/btps-protocol.js +4 -2
  7. package/dist/core/server/constants/currency.d.ts +6 -0
  8. package/dist/core/server/constants/currency.js +27 -2
  9. package/dist/core/server/constants/limits.d.ts +25 -0
  10. package/dist/core/server/constants/limits.js +27 -0
  11. package/dist/core/server/constants/permissions.d.ts +1 -1
  12. package/dist/core/server/schemas/artifacts/transporterSchema.js +16 -9
  13. package/dist/core/server/schemas/boundView.d.ts +9 -0
  14. package/dist/core/server/schemas/boundView.js +28 -0
  15. package/dist/core/server/schemas/boundViewBindings.d.ts +40 -0
  16. package/dist/core/server/schemas/boundViewBindings.js +128 -0
  17. package/dist/core/server/schemas/boundViewChecks.d.ts +49 -0
  18. package/dist/core/server/schemas/boundViewChecks.js +344 -0
  19. package/dist/core/server/schemas/boundViewSchema.d.ts +1637 -0
  20. package/dist/core/server/schemas/boundViewSchema.js +186 -0
  21. package/dist/core/server/schemas/btpsDocsSchema.d.ts +37094 -8000
  22. package/dist/core/server/schemas/btpsDocsSchema.js +62 -44
  23. package/dist/core/server/schemas/helpers.d.ts +11604 -2312
  24. package/dist/core/trust/types.d.ts +11 -1
  25. package/dist/server/btpsServer.d.ts +11 -1
  26. package/dist/server/btpsServer.js +45 -5
  27. package/dist/server/index.d.ts +1 -0
  28. package/dist/server/index.js +9 -1
  29. package/dist/server/types.d.ts +31 -26
  30. package/package.json +14 -1
@@ -11,6 +11,8 @@ import { BTPS_FIELD_LIMITS } from "../constants/limits.js";
11
11
  import { BtpSignatureSchema, identitySchema } from "./shared.js";
12
12
  import { boundedArray, boundedRecord } from "./bounded.js";
13
13
  import { BtpKeyChainSchema } from "./keyChainSchema.js";
14
+ import { BtpBoundViewSchema } from "./boundViewSchema.js";
15
+ import { bindingProblem } from "./boundViewBindings.js";
14
16
  const {
15
17
  trustRequest,
16
18
  trustResponse,
@@ -135,7 +137,7 @@ const {
135
137
  }, BtpReceiptDocV1_1Schema = BtpInvoiceDocV1_1Schema.omit({
136
138
  dueAt: !0,
137
139
  paymentLink: !0
138
- }).extend(receiptFields).strict(), httpsUrl = z.string().max(BTPS_FIELD_LIMITS.url).url().refine((url) => url.startsWith("https://"), "must be an https URL"), semver = z.string().regex(/^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/, "must be a semantic version"), { signature } = BTPS_FIELD_LIMITS, HIDDEN = new Set([
140
+ }).extend(receiptFields).strict(), httpsUrl = z.string().max(BTPS_FIELD_LIMITS.url).url().refine((url) => url.startsWith("https://"), "must be an https URL"), { signature } = BTPS_FIELD_LIMITS, HIDDEN = new Set([
139
141
  8203,
140
142
  8204,
141
143
  8205,
@@ -154,15 +156,13 @@ const {
154
156
  ]), isPlainText = (text) => [...text].every((char) => {
155
157
  const code = char.codePointAt(0) ?? 0;
156
158
  return code >= 32 && code !== 127 && char !== "<" && char !== ">" && !HIDDEN.has(code);
157
- }), signatureText = z.string().min(1).max(signature.text).refine(isPlainText, "must be plain text"), drawnSignature = z.object({
158
- strokes: boundedArray(
159
- boundedArray(z.number().int().min(0), signature.strokePoints * 2, 2),
160
- signature.strokes,
161
- 1
162
- )
163
- }).strict().superRefine(({ strokes }, ctx) => {
159
+ }), signatureText = z.string().min(1).max(signature.text).refine(isPlainText, "must be plain text"), strokes = boundedArray(
160
+ boundedArray(z.number().int().min(0), signature.strokePoints * 2, 2),
161
+ signature.strokes,
162
+ 1
163
+ ), strokesOnCanvas = ({ strokes: strokes2 }, ctx) => {
164
164
  let points = 0;
165
- strokes.forEach((stroke, index) => {
165
+ strokes2.forEach((stroke, index) => {
166
166
  points += stroke.length / 2;
167
167
  const onCanvas = stroke.every(
168
168
  (value, at) => at % 2 === 0 ? value <= signature.width : value <= signature.height
@@ -177,39 +177,41 @@ const {
177
177
  message: `a drawn signature has at most ${signature.points} points`,
178
178
  path: ["strokes"]
179
179
  });
180
- }), BtpTemplateValuesSchema = z.object({
181
- "signature.typed": signatureText.optional(),
182
- "signature.name": signatureText.optional(),
183
- "signature.title": signatureText.optional(),
184
- "signature.drawn": drawnSignature.optional()
185
- }).strict(), templateSchema = (withValues) => z.object({
186
- name: z.string(),
187
- data: boundedRecord(z.unknown(), recordKeys).pipe(
188
- z.object({
189
- version: semver.optional(),
190
- sha256: sha256Hex.optional(),
191
- source: httpsUrl.optional(),
192
- logo: z.object({ sha256: sha256Hex, source: httpsUrl }).strict().optional(),
193
- values: withValues ? BtpTemplateValuesSchema.optional() : z.undefined({
194
- invalid_type_error: "values are only on a letter's or an agreement's template"
195
- })
196
- }).catchall(z.unknown()).refine((data) => data.source === void 0 || data.sha256 !== void 0, {
197
- message: "a source comes with the sha256 of what it serves",
198
- path: ["sha256"]
199
- })
200
- )
201
- }), BtpTemplateSchema = templateSchema(!1), BtpLetterTemplateSchema = templateSchema(!0), BtpDiscountSchema = z.union([
180
+ }, BtpVisualSignatureSchema = z.union([
181
+ z.object({ type: z.literal("typed"), text: signatureText }).strict(),
182
+ z.object({ type: z.literal("drawn"), strokes }).strict().superRefine(strokesOnCanvas)
183
+ ]), BtpSignatorySchema = z.object({
184
+ name: signatureText,
185
+ title: signatureText.optional(),
186
+ visualSignature: BtpVisualSignatureSchema.optional()
187
+ }).strict(), BtpRecipientSchema = z.object({ name: z.string().min(1).max(BTPS_FIELD_LIMITS.recipientName) }).strict(), BtpDiscountSchema = z.union([
202
188
  z.object({ amount: z.number().int().safe().positive() }).strict(),
203
189
  z.object({ percent: z.number().int().min(1).max(1e4) }).strict()
204
- ]), BtpDiscountedLineItemsSchema = BtpLineItemsSchema.refine(
190
+ ]), LINE_ROLES = ["description", "quantity", "unitPrice", "discount", "tax", "lineTotal"], BtpDiscountedLineItemsSchema = BtpLineItemsSchema.extend({
191
+ roles: boundedRecord(z.enum(LINE_ROLES), lineItems.columns).optional()
192
+ }).refine(
205
193
  ({ rows }) => rows.every(
206
194
  (row) => row.discount === void 0 || typeof row.discount == "number" && row.discount >= 0
207
195
  ),
208
196
  { message: "a line discount is an integer of at least 0", path: ["rows"] }
209
- ), documentFields1_2 = {
197
+ ).refine(
198
+ ({ columns, roles }) => roles === void 0 || Object.keys(roles).every((column) => columns.includes(column)) && new Set(Object.values(roles)).size === Object.keys(roles).length,
199
+ { message: "each role names one of columns, and appears at most once", path: ["roles"] }
200
+ ), viewFields1_2 = {
201
+ recipient: BtpRecipientSchema.optional(),
202
+ boundView: BtpBoundViewSchema.optional()
203
+ }, documentFields1_2 = {
204
+ ...viewFields1_2,
210
205
  description: BtpNoteSchema(note).optional(),
211
206
  discount: BtpDiscountSchema.optional(),
212
- template: BtpTemplateSchema.optional()
207
+ subtotalAmount: BtpMoneySchema.optional(),
208
+ taxAmount: BtpMoneySchema.optional(),
209
+
210
+ template: z.never({
211
+ errorMap: () => ({
212
+ message: "template is 1.1.0 only; in 1.2.0 a document's look is its boundView"
213
+ })
214
+ }).optional()
213
215
  }, BtpInvoiceDocSchema = BtpInvoiceDocV1_1Schema.extend({
214
216
  ...documentFields1_2,
215
217
  lineItems: BtpDiscountedLineItemsSchema
@@ -295,9 +297,10 @@ const {
295
297
  references: boundedArray(BtpDocReferenceSchema, letter.references).optional(),
296
298
  metadata: metadataSchema.optional()
297
299
  }).strict(), BtpPayslipDocSchema = BtpPayslipDocV1_1Schema.extend({
298
- template: BtpTemplateSchema.optional()
300
+ boundView: BtpBoundViewSchema.optional()
299
301
  }).strict(), BtpLetterDocSchema = BtpLetterDocV1_1Schema.extend({
300
- template: BtpLetterTemplateSchema.optional()
302
+ ...viewFields1_2,
303
+ signatory: BtpSignatorySchema.optional()
301
304
  }).strict(), BtpDocumentV1_1Schema = z.discriminatedUnion("type", [
302
305
  BtpInvoiceDocV1_1Schema,
303
306
  BtpReceiptDocV1_1Schema,
@@ -324,12 +327,23 @@ const {
324
327
  BtpLetterDocSchema,
325
328
  BtpAgreementDocSchema
326
329
  ]).superRefine((document, ctx) => {
327
- if (document.type !== "agreement") return;
328
- const attached = new Set(document.attachments?.map(({ sha256 }) => sha256));
329
- document.acceptance.terms.some((term) => !attached.has(term)) && ctx.addIssue({
330
- code: z.ZodIssueCode.custom,
331
- message: "each term is the sha256 of one of its attachments",
332
- path: ["acceptance", "terms"]
330
+ const issue = (message2, path) => ctx.addIssue({ code: z.ZodIssueCode.custom, message: message2, path });
331
+ if (document.type === "agreement") {
332
+ const attached = new Set(document.attachments?.map(({ sha256 }) => sha256));
333
+ document.acceptance.terms.some((term) => !attached.has(term)) && issue("each term is the sha256 of one of its attachments", ["acceptance", "terms"]);
334
+ }
335
+ if (document.type === "invoice" || document.type === "receipt")
336
+ for (const field of ["subtotalAmount", "taxAmount"]) {
337
+ const currency = document[field]?.currency;
338
+ currency !== void 0 && currency !== document.totalAmount.currency && issue("is in the currency of totalAmount", [field, "currency"]);
339
+ }
340
+ document.boundView?.elements.forEach((element, index) => {
341
+ if (element.type === "text") {
342
+ const problem = bindingProblem(document, element.bind);
343
+ problem !== void 0 && issue(problem, ["boundView", "elements", index, "bind"]);
344
+ }
345
+ const signatory = "signatory" in document ? document.signatory : void 0;
346
+ element.type === "signature" && signatory?.visualSignature === void 0 && issue("a signature is drawn from the signatory's visualSignature", ["boundView", "elements", index]);
333
347
  });
334
348
  }), lifecycleEventFields = {
335
349
  type: z.literal("lifecycle_event"),
@@ -399,8 +413,12 @@ const {
399
413
  signer: identitySchema,
400
414
  decision: z.enum(["accepted", "declined"]),
401
415
  signedAt: z.string().datetime(),
402
- previous: sha256Hex.optional()
403
- }).strict(), BtpStatementRecordSchema = z.object({
416
+ previous: sha256Hex.optional(),
417
+ visualSignature: BtpVisualSignatureSchema.optional()
418
+ }).strict().refine((s) => s.visualSignature === void 0 || s.decision === "accepted", {
419
+ message: "only an acceptance carries a visualSignature",
420
+ path: ["visualSignature"]
421
+ }), BtpStatementRecordSchema = z.object({
404
422
  statement: BtpAgreementStatementSchema,
405
423
  signature: BtpSignatureSchema,
406
424
  selector: z.string().min(1).optional(),