@btps/sdk 1.2.0-rc.2 → 1.2.0-rc.3.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.
@@ -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,
@@ -175,42 +177,41 @@ const {
175
177
  message: `a drawn signature has at most ${signature.points} points`,
176
178
  path: ["strokes"]
177
179
  });
178
- }, drawnSignature = z.object({ strokes }).strict().superRefine(strokesOnCanvas), BtpVisualSignatureSchema = z.union([
180
+ }, BtpVisualSignatureSchema = z.union([
179
181
  z.object({ type: z.literal("typed"), text: signatureText }).strict(),
180
182
  z.object({ type: z.literal("drawn"), strokes }).strict().superRefine(strokesOnCanvas)
181
- ]), BtpTemplateValuesSchema = z.object({
182
- "signature.typed": signatureText.optional(),
183
- "signature.name": signatureText.optional(),
184
- "signature.title": signatureText.optional(),
185
- "signature.drawn": drawnSignature.optional()
186
- }).strict(), templateSchema = (withValues) => z.object({
187
- name: z.string(),
188
- data: boundedRecord(z.unknown(), recordKeys).pipe(
189
- z.object({
190
- version: semver.optional(),
191
- sha256: sha256Hex.optional(),
192
- source: httpsUrl.optional(),
193
- logo: z.object({ sha256: sha256Hex, source: httpsUrl }).strict().optional(),
194
- values: withValues ? BtpTemplateValuesSchema.optional() : z.undefined({
195
- invalid_type_error: "values are only on a letter's or an agreement's template"
196
- })
197
- }).catchall(z.unknown()).refine((data) => data.source === void 0 || data.sha256 !== void 0, {
198
- message: "a source comes with the sha256 of what it serves",
199
- path: ["sha256"]
200
- })
201
- )
202
- }), BtpTemplateSchema = templateSchema(!1), BtpLetterTemplateSchema = templateSchema(!0), BtpDiscountSchema = z.union([
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([
203
188
  z.object({ amount: z.number().int().safe().positive() }).strict(),
204
189
  z.object({ percent: z.number().int().min(1).max(1e4) }).strict()
205
- ]), 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(
206
193
  ({ rows }) => rows.every(
207
194
  (row) => row.discount === void 0 || typeof row.discount == "number" && row.discount >= 0
208
195
  ),
209
196
  { message: "a line discount is an integer of at least 0", path: ["rows"] }
210
- ), 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,
211
205
  description: BtpNoteSchema(note).optional(),
212
206
  discount: BtpDiscountSchema.optional(),
213
- 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()
214
215
  }, BtpInvoiceDocSchema = BtpInvoiceDocV1_1Schema.extend({
215
216
  ...documentFields1_2,
216
217
  lineItems: BtpDiscountedLineItemsSchema
@@ -296,9 +297,10 @@ const {
296
297
  references: boundedArray(BtpDocReferenceSchema, letter.references).optional(),
297
298
  metadata: metadataSchema.optional()
298
299
  }).strict(), BtpPayslipDocSchema = BtpPayslipDocV1_1Schema.extend({
299
- template: BtpTemplateSchema.optional()
300
+ boundView: BtpBoundViewSchema.optional()
300
301
  }).strict(), BtpLetterDocSchema = BtpLetterDocV1_1Schema.extend({
301
- template: BtpLetterTemplateSchema.optional()
302
+ ...viewFields1_2,
303
+ signatory: BtpSignatorySchema.optional()
302
304
  }).strict(), BtpDocumentV1_1Schema = z.discriminatedUnion("type", [
303
305
  BtpInvoiceDocV1_1Schema,
304
306
  BtpReceiptDocV1_1Schema,
@@ -325,12 +327,23 @@ const {
325
327
  BtpLetterDocSchema,
326
328
  BtpAgreementDocSchema
327
329
  ]).superRefine((document, ctx) => {
328
- if (document.type !== "agreement") return;
329
- const attached = new Set(document.attachments?.map(({ sha256 }) => sha256));
330
- document.acceptance.terms.some((term) => !attached.has(term)) && ctx.addIssue({
331
- code: z.ZodIssueCode.custom,
332
- message: "each term is the sha256 of one of its attachments",
333
- 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]);
334
347
  });
335
348
  }), lifecycleEventFields = {
336
349
  type: z.literal("lifecycle_event"),