@andco/protocol 0.0.11 → 0.0.13
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-errors.d.ts +5 -128
- package/dist/api-errors.d.ts.map +1 -1
- package/dist/api-errors.js +5 -131
- package/dist/authorization-preparation.d.ts +3 -3
- package/dist/authorization-preparation.d.ts.map +1 -1
- package/dist/authorization-preparation.js +4 -0
- package/dist/authorization.js +2 -2
- package/dist/errors.d.ts +111 -16
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +124 -12
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/intent-input.d.ts +1791 -725
- package/dist/intent-input.d.ts.map +1 -1
- package/dist/intent-input.js +325 -157
- package/dist/issues.d.ts +62 -0
- package/dist/issues.d.ts.map +1 -0
- package/dist/issues.js +61 -0
- package/dist/messages/index.d.ts +2 -2
- package/dist/messages/index.d.ts.map +1 -1
- package/dist/messages/index.js +10 -3
- package/dist/messages/set-preferences.d.ts +13 -1
- package/dist/messages/set-preferences.d.ts.map +1 -1
- package/dist/messages/set-preferences.js +31 -9
- package/dist/miniapp-manifest.d.ts +2 -2
- package/dist/miniapp-manifest.d.ts.map +1 -1
- package/dist/miniapp-manifest.js +1 -1
- package/dist/native-intent.d.ts +48 -28
- package/dist/native-intent.d.ts.map +1 -1
- package/dist/native-intent.js +76 -24
- package/dist/pagination.d.ts +37 -31
- package/dist/pagination.d.ts.map +1 -1
- package/dist/pagination.js +72 -24
- package/dist/rar/account-matchers.d.ts +47 -240
- package/dist/rar/account-matchers.d.ts.map +1 -1
- package/dist/rar/account-matchers.js +52 -131
- package/dist/rar/bank.d.ts +280 -392
- package/dist/rar/bank.d.ts.map +1 -1
- package/dist/rar/bank.js +68 -137
- package/dist/rar/catalog.d.ts +305 -288
- package/dist/rar/catalog.d.ts.map +1 -1
- package/dist/rar/catalog.js +69 -29
- package/dist/rar/detail.d.ts +15 -32
- package/dist/rar/detail.d.ts.map +1 -1
- package/dist/rar/detail.js +14 -34
- package/dist/rar/index.d.ts +2 -1
- package/dist/rar/index.d.ts.map +1 -1
- package/dist/rar/index.js +2 -1
- package/dist/rar/json-schemas.generated.d.ts +4 -0
- package/dist/rar/json-schemas.generated.d.ts.map +1 -0
- package/dist/rar/json-schemas.generated.js +3570 -0
- package/dist/rar/money.d.ts +5 -5
- package/dist/rar/money.d.ts.map +1 -1
- package/dist/rar/money.js +5 -5
- package/dist/rar/project-users.d.ts +40 -14
- package/dist/rar/project-users.d.ts.map +1 -1
- package/dist/rar/project-users.js +20 -23
- package/dist/rar/subject-data.d.ts +96 -48
- package/dist/rar/subject-data.d.ts.map +1 -1
- package/dist/rar/subject-data.js +61 -34
- package/dist/rar/subject.d.ts +18 -85
- package/dist/rar/subject.d.ts.map +1 -1
- package/dist/rar/subject.js +23 -51
- package/dist/rest/accounts.d.ts +347 -0
- package/dist/rest/accounts.d.ts.map +1 -0
- package/dist/rest/accounts.js +108 -0
- package/dist/rest/addresses.d.ts +581 -0
- package/dist/rest/addresses.d.ts.map +1 -0
- package/dist/rest/addresses.js +190 -0
- package/dist/rest/api-error.d.ts +104 -0
- package/dist/rest/api-error.d.ts.map +1 -0
- package/dist/rest/api-error.js +103 -0
- package/dist/rest/authorization.d.ts +123 -0
- package/dist/rest/authorization.d.ts.map +1 -0
- package/dist/rest/authorization.js +62 -0
- package/dist/rest/catalog.d.ts +199 -0
- package/dist/rest/catalog.d.ts.map +1 -0
- package/dist/rest/catalog.js +63 -0
- package/dist/rest/common.d.ts +637 -0
- package/dist/rest/common.d.ts.map +1 -0
- package/dist/rest/common.js +172 -0
- package/dist/rest/contract.d.ts +3480 -0
- package/dist/rest/contract.d.ts.map +1 -0
- package/dist/rest/contract.js +120 -0
- package/dist/rest/events.d.ts +145 -0
- package/dist/rest/events.d.ts.map +1 -0
- package/dist/rest/events.js +42 -0
- package/dist/rest/http.d.ts +49 -0
- package/dist/rest/http.d.ts.map +1 -0
- package/dist/rest/http.js +25 -0
- package/dist/rest/identity.d.ts +627 -0
- package/dist/rest/identity.d.ts.map +1 -0
- package/dist/rest/identity.js +128 -0
- package/dist/rest/index.d.ts +14 -0
- package/dist/rest/index.d.ts.map +1 -0
- package/dist/rest/index.js +13 -0
- package/dist/rest/intents.d.ts +4556 -0
- package/dist/rest/intents.d.ts.map +1 -0
- package/dist/rest/intents.js +551 -0
- package/dist/rest/signatures.d.ts +110 -0
- package/dist/rest/signatures.d.ts.map +1 -0
- package/dist/rest/signatures.js +113 -0
- package/dist/rest/subjects.d.ts +14 -0
- package/dist/rest/subjects.d.ts.map +1 -0
- package/dist/rest/subjects.js +22 -0
- package/dist/service-grants.d.ts +2 -2
- package/dist/service-grants.d.ts.map +1 -1
- package/dist/service-grants.js +1 -1
- package/dist/transport/generated/oauth-api.d.ts +5547 -1448
- package/dist/transport/generated/oauth-api.d.ts.map +1 -1
- package/dist/transport/intents.d.ts +9 -134
- package/dist/transport/intents.d.ts.map +1 -1
- package/dist/transport/intents.js +21 -405
- package/dist/transport/rest.d.ts +15 -5
- package/dist/transport/rest.d.ts.map +1 -1
- package/dist/userinfo.d.ts +22 -9
- package/dist/userinfo.d.ts.map +1 -1
- package/dist/userinfo.js +17 -12
- package/package.json +13 -2
package/dist/native-intent.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as v from "valibot";
|
|
2
|
-
import { TransferConfirmationSchema } from "./intent-input.js";
|
|
2
|
+
import { TransferConfirmationSchema, transferConfirmationIssues } from "./intent-input.js";
|
|
3
3
|
import { INTENT_STATUS_BASE, IS_INTENT_TERMINAL_STATUS } from "./intent-status.js";
|
|
4
|
+
import { duplicateIssues, issuesAt, parseWithRules } from "./issues.js";
|
|
4
5
|
import { MONEY_PATTERN, MoneyOrZeroSchema } from "./rar/money.js";
|
|
5
6
|
import { SafeURL } from "./urls.js";
|
|
6
7
|
const text = /* @__PURE__ */ v.string();
|
|
@@ -20,19 +21,18 @@ const accountType = /* @__PURE__ */ v.picklist(["checking", "debit", "savings"])
|
|
|
20
21
|
const accountKeyAccount = /* @__PURE__ */ v.object({ type: v.literal("account_key"), accountKey: text });
|
|
21
22
|
const idAccount = /* @__PURE__ */ v.object({ type: v.literal("account"), id: uuid });
|
|
22
23
|
/**
|
|
23
|
-
* An account at another bank, the
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
24
|
+
* An account at another bank, with every field the `external` matcher can fix. It is also what a
|
|
25
|
+
* person writes on the screen when the Intent left where the money lands to her (see
|
|
26
|
+
* {@link AndCoNativeIntentDestination}), so a transfer's answered destination and the command that
|
|
27
|
+
* completes it share one shape.
|
|
27
28
|
*/
|
|
28
|
-
const
|
|
29
|
+
const externalAccount = /* @__PURE__ */ v.object({
|
|
29
30
|
type: v.literal("external"),
|
|
30
31
|
bank_id: text,
|
|
31
32
|
account_type: accountType,
|
|
32
33
|
account_id: text,
|
|
33
34
|
holder_id: text,
|
|
34
35
|
holder_name: text,
|
|
35
|
-
editable: v.boolean(),
|
|
36
36
|
});
|
|
37
37
|
const base = {
|
|
38
38
|
id: uuid,
|
|
@@ -130,11 +130,14 @@ export const AndCoNativeIntentPreparationSchema = /* @__PURE__ */ v.object({
|
|
|
130
130
|
// command for the bank's challenge (web PIN and SMS code), so it sends the person to the hosted
|
|
131
131
|
// page, and `confirm_transfer` answers `409` with `error.code` `interaction_required` in that case.
|
|
132
132
|
interaction_reason: v.optional(v.picklist(["bank_verification_required", "source_account_required"])),
|
|
133
|
-
|
|
133
|
+
// Null while the person has yet to write where the money lands, when the Intent was created with
|
|
134
|
+
// `destination: null`; afterwards the account she wrote. Whatever it names is locked: a person
|
|
135
|
+
// completes a destination, never redirects one.
|
|
136
|
+
destination: v.nullable(v.variant("type", [
|
|
134
137
|
v.object({ ...accountKeyAccount.entries, editable: v.literal(false) }),
|
|
135
138
|
v.object({ ...idAccount.entries, editable: v.literal(false) }),
|
|
136
|
-
|
|
137
|
-
]),
|
|
139
|
+
v.object({ ...externalAccount.entries, editable: v.literal(false) }),
|
|
140
|
+
])),
|
|
138
141
|
source: v.nullable(v.variant("type", [accountKeyAccount, idAccount])),
|
|
139
142
|
}),
|
|
140
143
|
]),
|
|
@@ -178,7 +181,8 @@ export const AndCoNativeIntentCommandSchema = /* @__PURE__ */ v.variant("action"
|
|
|
178
181
|
// One command confirms money movement, whichever of the two forms carries it: what the person
|
|
179
182
|
// decides -- which account it leaves from, how much, where it lands and under what reference -- is
|
|
180
183
|
// the same question in both. Its members are the confirmation schema's own, so a value that is not
|
|
181
|
-
// editable has no separate shape here to be injected through.
|
|
184
|
+
// editable has no separate shape here to be injected through. `destination` is sent only for an
|
|
185
|
+
// Intent that left it to the screen (`destination: null`).
|
|
182
186
|
v.strictObject({
|
|
183
187
|
action: v.literal("confirm_transfer"),
|
|
184
188
|
sourceAccountId: TransferConfirmationSchema.entries.source_account_id,
|
|
@@ -195,9 +199,39 @@ export const AndCoNativeIntentCommandSchema = /* @__PURE__ */ v.variant("action"
|
|
|
195
199
|
v.strictObject({
|
|
196
200
|
action: v.literal("assign_deposit_credits"),
|
|
197
201
|
expectedVersion: v.pipe(v.number(), v.integer(), v.minValue(0), v.maxValue(Number.MAX_SAFE_INTEGER)),
|
|
198
|
-
|
|
202
|
+
// Distinct ids are `uniqueItems` in JSON Schema and the rule {@link nativeIntentCommandIssues} here.
|
|
203
|
+
selectedMovements: v.pipe(v.array(uuid), v.metadata({ uniqueItems: true })),
|
|
199
204
|
}),
|
|
200
205
|
]);
|
|
206
|
+
/**
|
|
207
|
+
* The rules of a command that its shape does not say: a confirmed transfer's note is at most
|
|
208
|
+
* `NOTE_MAX_LENGTH` code points (`note_too_long`) and its destination's holder name at most
|
|
209
|
+
* `HOLDER_NAME_MAX_LENGTH` (`holder_name_too_long`), and `assign_deposit_credits` selects each
|
|
210
|
+
* movement once (`duplicate_movement`, at the repeated entry).
|
|
211
|
+
*
|
|
212
|
+
* @example
|
|
213
|
+
* nativeIntentCommandIssues({ action: "assign_deposit_credits", expectedVersion: 1, selectedMovements: [id, id] });
|
|
214
|
+
* // [{ type: "duplicate_movement", path: [{ key: "selectedMovements" }, { key: 1 }] }]
|
|
215
|
+
*/
|
|
216
|
+
export function nativeIntentCommandIssues(command) {
|
|
217
|
+
if (command.action === "confirm_transfer") {
|
|
218
|
+
return transferConfirmationIssues(command);
|
|
219
|
+
}
|
|
220
|
+
if (command.action === "assign_deposit_credits") {
|
|
221
|
+
return issuesAt(["selectedMovements"], duplicateIssues(command.selectedMovements, "duplicate_movement"));
|
|
222
|
+
}
|
|
223
|
+
return [];
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Parses a command the native presentation sends: its shape, and then {@link nativeIntentCommandIssues}.
|
|
227
|
+
*
|
|
228
|
+
* @example
|
|
229
|
+
* const parsed = parseNativeIntentCommand(await request.json());
|
|
230
|
+
* if (!parsed.success) return invalid(parsed.issues);
|
|
231
|
+
*/
|
|
232
|
+
export function parseNativeIntentCommand(input) {
|
|
233
|
+
return parseWithRules(AndCoNativeIntentCommandSchema, nativeIntentCommandIssues, input);
|
|
234
|
+
}
|
|
201
235
|
class NativeIntentRequestError extends Error {
|
|
202
236
|
status;
|
|
203
237
|
constructor(message, status) {
|
|
@@ -249,7 +283,10 @@ export function ANDCO_NATIVE_INTENT_CALLBACK(presentation, result) {
|
|
|
249
283
|
url.searchParams.set("state", presentation.state);
|
|
250
284
|
return url.href;
|
|
251
285
|
}
|
|
252
|
-
/**
|
|
286
|
+
/**
|
|
287
|
+
* Reads the current state or submits one explicit command; commands are never retried automatically.
|
|
288
|
+
* The server validates the command, so a malformed one comes back as an error response.
|
|
289
|
+
*/
|
|
253
290
|
export async function requestAndCoNativeIntent(options) {
|
|
254
291
|
const url = new URL(`/api/host/intents/${options.presentation.id}`, options.bankOrigin);
|
|
255
292
|
url.searchParams.set("return_uri", options.presentation.returnUri);
|
|
@@ -261,7 +298,7 @@ export async function requestAndCoNativeIntent(options) {
|
|
|
261
298
|
"Content-Type": "application/json",
|
|
262
299
|
Authorization: `Bearer ${options.accessToken}`,
|
|
263
300
|
},
|
|
264
|
-
body: options.command ? JSON.stringify(
|
|
301
|
+
body: options.command ? JSON.stringify(options.command) : undefined,
|
|
265
302
|
});
|
|
266
303
|
const body = await response.json();
|
|
267
304
|
if (!response.ok) {
|
|
@@ -398,14 +435,19 @@ export function ANDCO_NATIVE_INTENT_VIEW(data, now = Date.now(), observedAt = no
|
|
|
398
435
|
(intent.status === "processing.confirmed" && data.retryable)),
|
|
399
436
|
};
|
|
400
437
|
}
|
|
438
|
+
const BLANK_DESTINATION = {
|
|
439
|
+
bank_id: "",
|
|
440
|
+
account_type: "",
|
|
441
|
+
account_id: "",
|
|
442
|
+
holder_id: "",
|
|
443
|
+
holder_name: "",
|
|
444
|
+
};
|
|
401
445
|
/**
|
|
402
|
-
*
|
|
403
|
-
*
|
|
446
|
+
* Whether the person still writes where the money lands: a transfer created with `destination: null`
|
|
447
|
+
* answers `destination: null` until she confirms, and then the account she wrote.
|
|
404
448
|
*/
|
|
405
|
-
function
|
|
406
|
-
|
|
407
|
-
return intent.destination;
|
|
408
|
-
return null;
|
|
449
|
+
function destinationIsOpen(intent) {
|
|
450
|
+
return intent.type === "transfer" && intent.destination === null;
|
|
409
451
|
}
|
|
410
452
|
/** Restores accepted command values; otherwise defaults only an unambiguous source. */
|
|
411
453
|
export function ANDCO_NATIVE_INTENT_FORM(data, retry) {
|
|
@@ -413,7 +455,7 @@ export function ANDCO_NATIVE_INTENT_FORM(data, retry) {
|
|
|
413
455
|
if (retry?.action === "fund_deposit")
|
|
414
456
|
return { source: retry.sourceAccountId, amount: String(retry.amount), note: retry.note ?? "", destination: null };
|
|
415
457
|
const transferRetry = retry?.action === "confirm_transfer" ? retry : undefined;
|
|
416
|
-
const
|
|
458
|
+
const retried = transferRetry?.destination;
|
|
417
459
|
return {
|
|
418
460
|
source: transferRetry?.sourceAccountId ??
|
|
419
461
|
data.funding?.pendingSourceId ??
|
|
@@ -429,7 +471,15 @@ export function ANDCO_NATIVE_INTENT_FORM(data, retry) {
|
|
|
429
471
|
? (intent.progress.remaining ?? "")
|
|
430
472
|
: (intent.amount.confirmed ?? intent.amount.value)),
|
|
431
473
|
note: transferRetry?.note ?? data.funding?.pendingNote ?? data.confirmedNote ?? intent.note?.value ?? "",
|
|
432
|
-
destination:
|
|
474
|
+
destination: destinationIsOpen(intent)
|
|
475
|
+
? {
|
|
476
|
+
bank_id: retried?.bank_id ?? BLANK_DESTINATION.bank_id,
|
|
477
|
+
account_type: retried?.account_type ?? BLANK_DESTINATION.account_type,
|
|
478
|
+
account_id: retried?.account_id ?? BLANK_DESTINATION.account_id,
|
|
479
|
+
holder_id: retried?.holder_id ?? BLANK_DESTINATION.holder_id,
|
|
480
|
+
holder_name: retried?.holder_name ?? BLANK_DESTINATION.holder_name,
|
|
481
|
+
}
|
|
482
|
+
: null,
|
|
433
483
|
};
|
|
434
484
|
}
|
|
435
485
|
/** Builds an explicit command without resending frozen or non-editable values of a money movement. */
|
|
@@ -438,7 +488,6 @@ export function ANDCO_NATIVE_INTENT_COMMAND(data, form) {
|
|
|
438
488
|
const frozen = INTENT_STATUS_BASE(intent.status) === "processing";
|
|
439
489
|
// Editability is read from the Intent the server sent and never from the form, so a screen that
|
|
440
490
|
// holds an edited value cannot turn a frozen amount, destination or note into a confirmed one.
|
|
441
|
-
const proposed = proposedDestination(intent);
|
|
442
491
|
const destination = form.destination;
|
|
443
492
|
return v.parse(AndCoNativeIntentCommandSchema, intent.type === "deposit"
|
|
444
493
|
? {
|
|
@@ -452,7 +501,7 @@ export function ANDCO_NATIVE_INTENT_COMMAND(data, form) {
|
|
|
452
501
|
sourceAccountId: form.source || undefined,
|
|
453
502
|
note: !frozen && intent.note?.editable ? form.note : undefined,
|
|
454
503
|
amount: !frozen && intent.amount.editable ? form.amount : undefined,
|
|
455
|
-
destination: !frozen &&
|
|
504
|
+
destination: !frozen && destinationIsOpen(intent) && destination
|
|
456
505
|
? {
|
|
457
506
|
type: "external",
|
|
458
507
|
bank_id: destination.bank_id,
|
|
@@ -468,6 +517,9 @@ export function ANDCO_NATIVE_INTENT_COMMAND(data, form) {
|
|
|
468
517
|
export function ANDCO_NATIVE_INTENT_FORM_VALID(data, form) {
|
|
469
518
|
if (data.intent.type !== "deposit" && data.sourceEditable && !form.source)
|
|
470
519
|
return false;
|
|
520
|
+
// A destination the Intent left to the person has to be written whole before there is a command.
|
|
521
|
+
if (destinationIsOpen(data.intent) && !form.destination)
|
|
522
|
+
return false;
|
|
471
523
|
try {
|
|
472
524
|
const command = ANDCO_NATIVE_INTENT_COMMAND(data, form);
|
|
473
525
|
return (command.action !== "fund_deposit" ||
|
package/dist/pagination.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import * as v from "valibot";
|
|
2
|
+
import { type Issue, type ParseResult } from "./issues.js";
|
|
2
3
|
/** Position and size of a cursor page; forward and backward arguments are exclusive. */
|
|
3
|
-
export type PaginationCursor = Pick<
|
|
4
|
+
export type PaginationCursor = Pick<PaginationArguments, "after" | "first" | "before" | "last">;
|
|
4
5
|
/** Position and size of an offset page. */
|
|
5
|
-
export type PaginationOffset = Pick<
|
|
6
|
+
export type PaginationOffset = Pick<PaginationArguments, "offset" | "limit">;
|
|
6
7
|
export type Pagination = PaginationCursor | PaginationOffset;
|
|
7
8
|
export type Edge<T> = {
|
|
8
9
|
cursor: string;
|
|
@@ -18,42 +19,39 @@ export type Connection<T> = {
|
|
|
18
19
|
edges: Edge<T>[];
|
|
19
20
|
pageInfo: PageInfo;
|
|
20
21
|
};
|
|
22
|
+
/** Every argument a page can name, from any of the three families. {@link paginationIssues} keeps them apart. */
|
|
23
|
+
export type PaginationArguments = v.InferInput<ReturnType<typeof paginationSchema>>;
|
|
21
24
|
/** Validated pagination suitable for SQL, GraphQL, and transport adapters. */
|
|
22
|
-
export type NormalizedPagination =
|
|
25
|
+
export type NormalizedPagination = ReturnType<typeof normalizePage>;
|
|
23
26
|
/**
|
|
24
|
-
*
|
|
25
|
-
*
|
|
27
|
+
* The shape of pagination arguments: a cursor and a size per direction, and an offset with a size.
|
|
28
|
+
* `maximum` bounds every size. That at most one family is present is a rule
|
|
29
|
+
* ({@link paginationIssues}), because JSON Schema cannot say it.
|
|
26
30
|
*/
|
|
27
|
-
export declare function paginationSchema(maximum?: number): v.
|
|
31
|
+
export declare function paginationSchema(maximum?: number): v.ObjectSchema<{
|
|
28
32
|
readonly after: v.OptionalSchema<v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.NonEmptyAction<string, undefined>, v.MaxLengthAction<string, 2048, undefined>]>, undefined>;
|
|
29
33
|
readonly first: v.OptionalSchema<v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 1, undefined>, v.MaxValueAction<number, number, undefined>]>, undefined>;
|
|
30
34
|
readonly before: v.OptionalSchema<v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.NonEmptyAction<string, undefined>, v.MaxLengthAction<string, 2048, undefined>]>, undefined>;
|
|
31
35
|
readonly last: v.OptionalSchema<v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 1, undefined>, v.MaxValueAction<number, number, undefined>]>, undefined>;
|
|
32
36
|
readonly offset: v.OptionalSchema<v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 0, undefined>, v.MaxValueAction<number, 2147483647, undefined>]>, undefined>;
|
|
33
37
|
readonly limit: v.OptionalSchema<v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.SafeIntegerAction<number, undefined>, v.MinValueAction<number, 1, undefined>, v.MaxValueAction<number, number, undefined>]>, undefined>;
|
|
34
|
-
}, undefined
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
first: number | undefined;
|
|
52
|
-
before: string | undefined;
|
|
53
|
-
last: number | undefined;
|
|
54
|
-
offset: number | undefined;
|
|
55
|
-
limit: number | undefined;
|
|
56
|
-
}>, v.CheckAction<{
|
|
38
|
+
}, undefined>;
|
|
39
|
+
/**
|
|
40
|
+
* The rule of pagination that the shape does not say: a page asks for one family of arguments, so
|
|
41
|
+
* `after`/`first` (forward), `before`/`last` (backward) and `offset`/`limit` are never mixed. It
|
|
42
|
+
* answers `mixed_pagination_families`, with no path, when two families are present.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* paginationIssues({ first: 10, offset: 0 }); // [{ type: "mixed_pagination_families", path: undefined }]
|
|
46
|
+
* paginationIssues({ after: "cursor", first: 10 }); // []
|
|
47
|
+
*/
|
|
48
|
+
export declare function paginationIssues(fields: PaginationArguments): Issue[];
|
|
49
|
+
/**
|
|
50
|
+
* Supplies the position and size a page leaves out, over arguments whose shape and family already
|
|
51
|
+
* hold: forward unless a backward argument is present, an offset of `0` for an offset page, and
|
|
52
|
+
* `maximum` or 100, whichever is smaller, as the default size.
|
|
53
|
+
*/
|
|
54
|
+
export declare function normalizePage(fields: PaginationArguments, maximum?: number): {
|
|
57
55
|
size: number;
|
|
58
56
|
after: string | undefined;
|
|
59
57
|
first: number | undefined;
|
|
@@ -61,8 +59,16 @@ export declare function paginationSchema(maximum?: number): v.SchemaWithPipe<rea
|
|
|
61
59
|
last: number | undefined;
|
|
62
60
|
offset: number | undefined;
|
|
63
61
|
limit: number | undefined;
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Validates one pagination family, shape then rule, and supplies its default position and size.
|
|
65
|
+
* Answers every {@link Issue} found, so a handler can report all of them.
|
|
66
|
+
*/
|
|
67
|
+
export declare function parsePagination(input: unknown, maximum?: number): ParseResult<NormalizedPagination>;
|
|
68
|
+
/**
|
|
69
|
+
* Validates the argument family and sizes before a request is made.
|
|
70
|
+
* Throws an {@link AndcoError} `invalid_request` with the {@link Issue}s in `details.issues`.
|
|
71
|
+
*/
|
|
66
72
|
export declare function normalizePagination(input?: Pagination, maximum?: number): NormalizedPagination;
|
|
67
73
|
/** Returns only the public arguments; internal normalization fields never cross the wire. */
|
|
68
74
|
export declare function paginationParameters(input?: Pagination, maximum?: number): PaginationCursor & PaginationOffset;
|
package/dist/pagination.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pagination.d.ts","sourceRoot":"","sources":["../src/pagination.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,SAAS,CAAC;AAE7B,
|
|
1
|
+
{"version":3,"file":"pagination.d.ts","sourceRoot":"","sources":["../src/pagination.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,SAAS,CAAC;AAE7B,OAAO,EAAE,KAAK,KAAK,EAAY,KAAK,WAAW,EAAE,MAAM,aAAa,CAAC;AAErE,wFAAwF;AACxF,MAAM,MAAM,gBAAgB,GAAG,IAAI,CAAC,mBAAmB,EAAE,OAAO,GAAG,OAAO,GAAG,QAAQ,GAAG,MAAM,CAAC,CAAC;AAEhG,2CAA2C;AAC3C,MAAM,MAAM,gBAAgB,GAAG,IAAI,CAAC,mBAAmB,EAAE,QAAQ,GAAG,OAAO,CAAC,CAAC;AAC7E,MAAM,MAAM,UAAU,GAAG,gBAAgB,GAAG,gBAAgB,CAAC;AAC7D,MAAM,MAAM,IAAI,CAAC,CAAC,IAAI;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,CAAC,CAAA;CAAE,CAAC;AAClD,MAAM,MAAM,QAAQ,GAAG;IACrB,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,eAAe,EAAE,OAAO,CAAC;IACzB,WAAW,EAAE,OAAO,CAAC;CACtB,CAAC;AACF,MAAM,MAAM,UAAU,CAAC,CAAC,IAAI;IAAE,KAAK,EAAE,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;IAAC,QAAQ,EAAE,QAAQ,CAAA;CAAE,CAAC;AAErE,iHAAiH;AACjH,MAAM,MAAM,mBAAmB,GAAG,CAAC,CAAC,UAAU,CAAC,UAAU,CAAC,OAAO,gBAAgB,CAAC,CAAC,CAAC;AAEpF,8EAA8E;AAC9E,MAAM,MAAM,oBAAoB,GAAG,UAAU,CAAC,OAAO,aAAa,CAAC,CAAC;AAEpE;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,SAAM;;;;;;;cAW7C;AAED;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,mBAAmB,GAAG,KAAK,EAAE,CAQrE;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,mBAAmB,EAAE,OAAO,SAAM;;;;;;;;EAavE;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,SAAM,GAAG,WAAW,CAAC,oBAAoB,CAAC,CAUhG;AAED;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,GAAE,UAAe,EAAE,OAAO,SAAM,GAAG,oBAAoB,CAU/F;AAED,6FAA6F;AAC7F,wBAAgB,oBAAoB,CAAC,KAAK,GAAE,UAAe,EAAE,OAAO,SAAM,GAAG,gBAAgB,GAAG,gBAAgB,CAU/G;AAED,uFAAuF;AACvF,wBAAgB,UAAU,CAAC,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,eAAe,UAAQ,EAAE,WAAW,UAAQ,GAAG,UAAU,CAAC,CAAC,CAAC,CAU3G"}
|
package/dist/pagination.js
CHANGED
|
@@ -1,41 +1,89 @@
|
|
|
1
1
|
import * as v from "valibot";
|
|
2
|
+
import { ANDCO_ERROR_CODES, AndcoError } from "./errors.js";
|
|
3
|
+
import { issuesOf } from "./issues.js";
|
|
2
4
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
+
* The shape of pagination arguments: a cursor and a size per direction, and an offset with a size.
|
|
6
|
+
* `maximum` bounds every size. That at most one family is present is a rule
|
|
7
|
+
* ({@link paginationIssues}), because JSON Schema cannot say it.
|
|
5
8
|
*/
|
|
6
9
|
export function paginationSchema(maximum = 100) {
|
|
7
10
|
const pageSize = v.pipe(v.number(), v.safeInteger(), v.minValue(1), v.maxValue(maximum));
|
|
8
11
|
const cursor = v.pipe(v.string(), v.nonEmpty(), v.maxLength(2048));
|
|
9
|
-
return v.
|
|
12
|
+
return v.object({
|
|
10
13
|
after: v.optional(cursor),
|
|
11
14
|
first: v.optional(pageSize),
|
|
12
15
|
before: v.optional(cursor),
|
|
13
16
|
last: v.optional(pageSize),
|
|
14
17
|
offset: v.optional(v.pipe(v.number(), v.safeInteger(), v.minValue(0), v.maxValue(2147483647))),
|
|
15
18
|
limit: v.optional(pageSize),
|
|
16
|
-
})
|
|
17
|
-
const forward = fields.after !== undefined || fields.first !== undefined;
|
|
18
|
-
const backward = fields.before !== undefined || fields.last !== undefined;
|
|
19
|
-
const offset = fields.offset !== undefined || fields.limit !== undefined;
|
|
20
|
-
return Number(forward) + Number(backward) + Number(offset) <= 1;
|
|
21
|
-
}, "Pagination argument families cannot be mixed"), v.transform((fields) => {
|
|
22
|
-
const backward = fields.before !== undefined || fields.last !== undefined;
|
|
23
|
-
const offset = fields.offset !== undefined || fields.limit !== undefined;
|
|
24
|
-
const size = fields.first ?? fields.last ?? fields.limit ?? Math.min(100, maximum);
|
|
25
|
-
return {
|
|
26
|
-
size,
|
|
27
|
-
after: fields.after,
|
|
28
|
-
first: !backward ? size : undefined,
|
|
29
|
-
before: fields.before,
|
|
30
|
-
last: backward ? size : undefined,
|
|
31
|
-
offset: offset ? (fields.offset ?? 0) : undefined,
|
|
32
|
-
limit: offset ? size : undefined,
|
|
33
|
-
};
|
|
34
|
-
}), v.check((page) => v.is(pageSize, page.size), "Invalid default page size"));
|
|
19
|
+
});
|
|
35
20
|
}
|
|
36
|
-
/**
|
|
21
|
+
/**
|
|
22
|
+
* The rule of pagination that the shape does not say: a page asks for one family of arguments, so
|
|
23
|
+
* `after`/`first` (forward), `before`/`last` (backward) and `offset`/`limit` are never mixed. It
|
|
24
|
+
* answers `mixed_pagination_families`, with no path, when two families are present.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* paginationIssues({ first: 10, offset: 0 }); // [{ type: "mixed_pagination_families", path: undefined }]
|
|
28
|
+
* paginationIssues({ after: "cursor", first: 10 }); // []
|
|
29
|
+
*/
|
|
30
|
+
export function paginationIssues(fields) {
|
|
31
|
+
const forward = fields.after !== undefined || fields.first !== undefined;
|
|
32
|
+
const backward = fields.before !== undefined || fields.last !== undefined;
|
|
33
|
+
const offset = fields.offset !== undefined || fields.limit !== undefined;
|
|
34
|
+
if (Number(forward) + Number(backward) + Number(offset) > 1) {
|
|
35
|
+
return [{ type: "mixed_pagination_families", path: undefined }];
|
|
36
|
+
}
|
|
37
|
+
return [];
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Supplies the position and size a page leaves out, over arguments whose shape and family already
|
|
41
|
+
* hold: forward unless a backward argument is present, an offset of `0` for an offset page, and
|
|
42
|
+
* `maximum` or 100, whichever is smaller, as the default size.
|
|
43
|
+
*/
|
|
44
|
+
export function normalizePage(fields, maximum = 100) {
|
|
45
|
+
const backward = fields.before !== undefined || fields.last !== undefined;
|
|
46
|
+
const offset = fields.offset !== undefined || fields.limit !== undefined;
|
|
47
|
+
const size = fields.first ?? fields.last ?? fields.limit ?? Math.min(100, maximum);
|
|
48
|
+
return {
|
|
49
|
+
size,
|
|
50
|
+
after: fields.after,
|
|
51
|
+
first: !backward ? size : undefined,
|
|
52
|
+
before: fields.before,
|
|
53
|
+
last: backward ? size : undefined,
|
|
54
|
+
offset: offset ? (fields.offset ?? 0) : undefined,
|
|
55
|
+
limit: offset ? size : undefined,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Validates one pagination family, shape then rule, and supplies its default position and size.
|
|
60
|
+
* Answers every {@link Issue} found, so a handler can report all of them.
|
|
61
|
+
*/
|
|
62
|
+
export function parsePagination(input, maximum = 100) {
|
|
63
|
+
const parsed = v.safeParse(paginationSchema(maximum), input);
|
|
64
|
+
if (!parsed.success) {
|
|
65
|
+
return { success: false, issues: issuesOf(parsed.issues) };
|
|
66
|
+
}
|
|
67
|
+
const issues = paginationIssues(parsed.output);
|
|
68
|
+
if (issues.length > 0) {
|
|
69
|
+
return { success: false, issues };
|
|
70
|
+
}
|
|
71
|
+
return { success: true, output: normalizePage(parsed.output, maximum) };
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Validates the argument family and sizes before a request is made.
|
|
75
|
+
* Throws an {@link AndcoError} `invalid_request` with the {@link Issue}s in `details.issues`.
|
|
76
|
+
*/
|
|
37
77
|
export function normalizePagination(input = {}, maximum = 100) {
|
|
38
|
-
|
|
78
|
+
const parsed = parsePagination(input, maximum);
|
|
79
|
+
if (!parsed.success) {
|
|
80
|
+
const types = parsed.issues.map((issue) => issue.type).join(", ");
|
|
81
|
+
throw new AndcoError(ANDCO_ERROR_CODES.INVALID_REQUEST, {
|
|
82
|
+
message: `Invalid pagination: ${types}.`,
|
|
83
|
+
details: { issues: parsed.issues },
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
return parsed.output;
|
|
39
87
|
}
|
|
40
88
|
/** Returns only the public arguments; internal normalization fields never cross the wire. */
|
|
41
89
|
export function paginationParameters(input = {}, maximum = 100) {
|