@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.
Files changed (120) hide show
  1. package/dist/api-errors.d.ts +5 -128
  2. package/dist/api-errors.d.ts.map +1 -1
  3. package/dist/api-errors.js +5 -131
  4. package/dist/authorization-preparation.d.ts +3 -3
  5. package/dist/authorization-preparation.d.ts.map +1 -1
  6. package/dist/authorization-preparation.js +4 -0
  7. package/dist/authorization.js +2 -2
  8. package/dist/errors.d.ts +111 -16
  9. package/dist/errors.d.ts.map +1 -1
  10. package/dist/errors.js +124 -12
  11. package/dist/index.d.ts +1 -0
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +1 -0
  14. package/dist/intent-input.d.ts +1791 -725
  15. package/dist/intent-input.d.ts.map +1 -1
  16. package/dist/intent-input.js +325 -157
  17. package/dist/issues.d.ts +62 -0
  18. package/dist/issues.d.ts.map +1 -0
  19. package/dist/issues.js +61 -0
  20. package/dist/messages/index.d.ts +2 -2
  21. package/dist/messages/index.d.ts.map +1 -1
  22. package/dist/messages/index.js +10 -3
  23. package/dist/messages/set-preferences.d.ts +13 -1
  24. package/dist/messages/set-preferences.d.ts.map +1 -1
  25. package/dist/messages/set-preferences.js +31 -9
  26. package/dist/miniapp-manifest.d.ts +2 -2
  27. package/dist/miniapp-manifest.d.ts.map +1 -1
  28. package/dist/miniapp-manifest.js +1 -1
  29. package/dist/native-intent.d.ts +48 -28
  30. package/dist/native-intent.d.ts.map +1 -1
  31. package/dist/native-intent.js +76 -24
  32. package/dist/pagination.d.ts +37 -31
  33. package/dist/pagination.d.ts.map +1 -1
  34. package/dist/pagination.js +72 -24
  35. package/dist/rar/account-matchers.d.ts +47 -240
  36. package/dist/rar/account-matchers.d.ts.map +1 -1
  37. package/dist/rar/account-matchers.js +52 -131
  38. package/dist/rar/bank.d.ts +280 -392
  39. package/dist/rar/bank.d.ts.map +1 -1
  40. package/dist/rar/bank.js +68 -137
  41. package/dist/rar/catalog.d.ts +305 -288
  42. package/dist/rar/catalog.d.ts.map +1 -1
  43. package/dist/rar/catalog.js +69 -29
  44. package/dist/rar/detail.d.ts +15 -32
  45. package/dist/rar/detail.d.ts.map +1 -1
  46. package/dist/rar/detail.js +14 -34
  47. package/dist/rar/index.d.ts +2 -1
  48. package/dist/rar/index.d.ts.map +1 -1
  49. package/dist/rar/index.js +2 -1
  50. package/dist/rar/json-schemas.generated.d.ts +4 -0
  51. package/dist/rar/json-schemas.generated.d.ts.map +1 -0
  52. package/dist/rar/json-schemas.generated.js +3570 -0
  53. package/dist/rar/money.d.ts +5 -5
  54. package/dist/rar/money.d.ts.map +1 -1
  55. package/dist/rar/money.js +5 -5
  56. package/dist/rar/project-users.d.ts +40 -14
  57. package/dist/rar/project-users.d.ts.map +1 -1
  58. package/dist/rar/project-users.js +20 -23
  59. package/dist/rar/subject-data.d.ts +96 -48
  60. package/dist/rar/subject-data.d.ts.map +1 -1
  61. package/dist/rar/subject-data.js +61 -34
  62. package/dist/rar/subject.d.ts +18 -85
  63. package/dist/rar/subject.d.ts.map +1 -1
  64. package/dist/rar/subject.js +23 -51
  65. package/dist/rest/accounts.d.ts +347 -0
  66. package/dist/rest/accounts.d.ts.map +1 -0
  67. package/dist/rest/accounts.js +108 -0
  68. package/dist/rest/addresses.d.ts +581 -0
  69. package/dist/rest/addresses.d.ts.map +1 -0
  70. package/dist/rest/addresses.js +190 -0
  71. package/dist/rest/api-error.d.ts +104 -0
  72. package/dist/rest/api-error.d.ts.map +1 -0
  73. package/dist/rest/api-error.js +103 -0
  74. package/dist/rest/authorization.d.ts +123 -0
  75. package/dist/rest/authorization.d.ts.map +1 -0
  76. package/dist/rest/authorization.js +62 -0
  77. package/dist/rest/catalog.d.ts +199 -0
  78. package/dist/rest/catalog.d.ts.map +1 -0
  79. package/dist/rest/catalog.js +63 -0
  80. package/dist/rest/common.d.ts +637 -0
  81. package/dist/rest/common.d.ts.map +1 -0
  82. package/dist/rest/common.js +172 -0
  83. package/dist/rest/contract.d.ts +3480 -0
  84. package/dist/rest/contract.d.ts.map +1 -0
  85. package/dist/rest/contract.js +120 -0
  86. package/dist/rest/events.d.ts +145 -0
  87. package/dist/rest/events.d.ts.map +1 -0
  88. package/dist/rest/events.js +42 -0
  89. package/dist/rest/http.d.ts +49 -0
  90. package/dist/rest/http.d.ts.map +1 -0
  91. package/dist/rest/http.js +25 -0
  92. package/dist/rest/identity.d.ts +627 -0
  93. package/dist/rest/identity.d.ts.map +1 -0
  94. package/dist/rest/identity.js +128 -0
  95. package/dist/rest/index.d.ts +14 -0
  96. package/dist/rest/index.d.ts.map +1 -0
  97. package/dist/rest/index.js +13 -0
  98. package/dist/rest/intents.d.ts +4556 -0
  99. package/dist/rest/intents.d.ts.map +1 -0
  100. package/dist/rest/intents.js +551 -0
  101. package/dist/rest/signatures.d.ts +110 -0
  102. package/dist/rest/signatures.d.ts.map +1 -0
  103. package/dist/rest/signatures.js +113 -0
  104. package/dist/rest/subjects.d.ts +14 -0
  105. package/dist/rest/subjects.d.ts.map +1 -0
  106. package/dist/rest/subjects.js +22 -0
  107. package/dist/service-grants.d.ts +2 -2
  108. package/dist/service-grants.d.ts.map +1 -1
  109. package/dist/service-grants.js +1 -1
  110. package/dist/transport/generated/oauth-api.d.ts +5547 -1448
  111. package/dist/transport/generated/oauth-api.d.ts.map +1 -1
  112. package/dist/transport/intents.d.ts +9 -134
  113. package/dist/transport/intents.d.ts.map +1 -1
  114. package/dist/transport/intents.js +21 -405
  115. package/dist/transport/rest.d.ts +15 -5
  116. package/dist/transport/rest.d.ts.map +1 -1
  117. package/dist/userinfo.d.ts +22 -9
  118. package/dist/userinfo.d.ts.map +1 -1
  119. package/dist/userinfo.js +17 -12
  120. package/package.json +13 -2
package/dist/errors.js CHANGED
@@ -3,8 +3,10 @@
3
3
  * it sends: lower snake_case, no prefix. Both vocabularies end up in the same `error.code` field, so
4
4
  * an integrator should not have to remember which half of a failure produced the string in hand.
5
5
  *
6
- * The backend never reports any of these -- it cannot see a blocked popup or a browser without Web
7
- * Crypto -- which is why they live apart from {@link ANDCO_API_ERROR_CODES} rather than in one list.
6
+ * The backend never reports most of these -- it cannot see a blocked popup or a browser without Web
7
+ * Crypto -- which is why they live apart from the codes the API answers, which its OpenAPI document
8
+ * declares, each with its status and data. The two it shares, `invalid_identifier` and
9
+ * `invalid_request`, are here for the times the SDK refuses an argument before sending it.
8
10
  *
9
11
  * Compare against a member rather than a literal, and always leave a branch for a code you do not
10
12
  * recognise. This list grows, `AndcoError.code` is a plain `string` for that reason, and an
@@ -99,6 +101,14 @@ export const ANDCO_ERROR_CODES = {
99
101
  // -- Authorization lifecycle -----------------------------------------------------------------
100
102
  /** The person closed or rejected the authorization before it completed. Not a failure: a decision. */
101
103
  AUTHORIZATION_CANCELLED: "authorization_cancelled",
104
+ /**
105
+ * The factory handed to `andco.auth.present(factory, ...)` threw, so there was no authorization
106
+ * URL to present. What failed is the application's own server (or whatever the factory called),
107
+ * not the SDK's configuration: `cause` holds the original error, and an `AndcoError` the factory
108
+ * throws itself is passed through with its own code. The window is already open when the factory
109
+ * runs, so it closes without navigating.
110
+ */
111
+ AUTHORIZATION_FACTORY_FAILED: "authorization_factory_failed",
102
112
  /**
103
113
  * The authorization needs a bank account that is not open yet, because the bank is still
104
114
  * reviewing its opening. The Authorization Server ends the authorization with
@@ -116,6 +126,15 @@ export const ANDCO_ERROR_CODES = {
116
126
  * Not a cancellation. Start a new authorization; the progress made on the bank side is kept.
117
127
  */
118
128
  AUTHORIZATION_EXPIRED: "authorization_expired",
129
+ /**
130
+ * The authorization asked for an Organization, and the person who signed in belongs to none where
131
+ * they may authorize applications. The Authorization Server ends it with
132
+ * `error=access_denied&error_description=organization_required` when they choose to go back.
133
+ *
134
+ * Not a cancellation. The consent screen offers to create an Empresa first; a new authorization
135
+ * after that lets them pick it.
136
+ */
137
+ ORGANIZATION_REQUIRED: "organization_required",
119
138
  /** The authorization did not complete before its timeout elapsed. */
120
139
  AUTHORIZATION_TIMEOUT: "authorization_timeout",
121
140
  /** Building or pushing (RFC 9126 PAR) an authorization request failed. */
@@ -156,6 +175,18 @@ export const ANDCO_ERROR_CODES = {
156
175
  /** The pushed authorization request (RFC 9126) answered without a usable `request_uri`. */
157
176
  INVALID_PAR_RESPONSE: "invalid_par_response",
158
177
  // -- Operations ------------------------------------------------------------------------------
178
+ /**
179
+ * An identifier the caller passed (an Intent's, an account's) is not well formed, so no request was
180
+ * made. The API answers the same code when the malformed identifier reaches it; here the SDK caught
181
+ * it first. Not retryable: fix the identifier.
182
+ */
183
+ INVALID_IDENTIFIER: "invalid_identifier",
184
+ /**
185
+ * An argument the caller passed, such as pagination that mixes cursor and offset families or asks
186
+ * for zero items, is not one the operation takes, so no request was made. `details.issues` names
187
+ * each problem as `{ type, path }`. The API answers the same code for the same request.
188
+ */
189
+ INVALID_REQUEST: "invalid_request",
159
190
  /**
160
191
  * An Intent watch was stopped, by `stop()` or its signal, before the Intent reached a terminal
161
192
  * status. A decision of the caller, not a failure; the watch's `intent` still holds the last read.
@@ -187,6 +218,34 @@ export const ANDCO_ERROR_CODES = {
187
218
  /** A failure the SDK could not attribute to any condition above. Its cause carries what is known. */
188
219
  UNKNOWN_ERROR: "unknown_error",
189
220
  };
221
+ /**
222
+ * The codes of the failures the SDK's own credentials throw instead of a token, which stop a call
223
+ * before it goes out:
224
+ *
225
+ * - `session_missing`, `storage_failed`, `browser_required`, `refresh_failed` and
226
+ * `invalid_token_response`, from the credentials of a session.
227
+ * - `ambiguous_grant`, `invalid_grant` and `invalid_response`, which `AndcoService`'s token exchange
228
+ * relays from the Authorization Server (or raises when the issuer answers something unusable).
229
+ * - `invalid_configuration`, from a `Bank` built without credentials.
230
+ *
231
+ * `authorization_required` is one more, thrown as an {@link AndcoAuthorizationRequiredError}. Any
232
+ * other code the Authorization Server relays (its vocabulary is open) and any code a resolver an
233
+ * integrator wrote throws reach the caller as `request_failed` with the error as `cause`.
234
+ *
235
+ * `no_token_for_resource` is not here: the credentials turn it into a call without an
236
+ * `Authorization` header, so the API answers it instead.
237
+ */
238
+ export const ANDCO_CREDENTIALS_ERROR_CODES = [
239
+ ANDCO_ERROR_CODES.SESSION_MISSING,
240
+ ANDCO_ERROR_CODES.STORAGE_FAILED,
241
+ ANDCO_ERROR_CODES.BROWSER_REQUIRED,
242
+ ANDCO_ERROR_CODES.REFRESH_FAILED,
243
+ ANDCO_ERROR_CODES.INVALID_TOKEN_RESPONSE,
244
+ "ambiguous_grant",
245
+ "invalid_grant",
246
+ ANDCO_ERROR_CODES.INVALID_RESPONSE,
247
+ ANDCO_ERROR_CODES.INVALID_CONFIGURATION,
248
+ ];
190
249
  /**
191
250
  * The subset a widget may report to its host page over postMessage.
192
251
  *
@@ -221,6 +280,7 @@ export const ANDCO_BRIDGE_ERROR_CODES = [
221
280
  export const ANDCO_ACCESS_DENIED_WITHOUT_DECISION = [
222
281
  ANDCO_ERROR_CODES.BANK_ACCOUNT_PENDING_REVIEW,
223
282
  ANDCO_ERROR_CODES.AUTHORIZATION_EXPIRED,
283
+ ANDCO_ERROR_CODES.ORGANIZATION_REQUIRED,
224
284
  ];
225
285
  /**
226
286
  * Whether an authorization redirect's error is `access_denied` with a description that names a
@@ -259,15 +319,20 @@ export class ErrorExtendable extends Error {
259
319
  * The single error type every Andco operation reports.
260
320
  *
261
321
  * An integrator writes one `catch`-equivalent branch and compares one `code`, whether the failure
262
- * came from authentication, an Intent, the resource API or the SDK itself. `code` is a plain string
263
- * because it also carries codes this package does not own, such as the Authorization Server's OAuth
264
- * error identifiers, and the same string identifies the same condition in the Python SDK.
322
+ * came from authentication, an Intent, the resource API or the SDK itself. A failure the API answered
323
+ * is an `AndcoResponseError` (`@andco/sdk`), a subclass whose `code` is the API's `error` and whose
324
+ * `data` carries the error's own fields.
325
+ *
326
+ * `Code` narrows `code` where the set is known, so a union of errors is discriminated by it: an
327
+ * operation's error is one member per code it can answer. It defaults to a plain string because
328
+ * `code` also carries codes this package does not own, such as the Authorization Server's OAuth error
329
+ * identifiers, and the same string identifies the same condition in the Python SDK.
265
330
  *
266
331
  * @example
267
332
  * ```ts
268
333
  * const { data, error } = await andco.intents.get(intentId);
269
334
  * if (error) {
270
- * if (error.code === "insufficient_authorization") return reconnect();
335
+ * if (error.code === "not_found") return showUnknownOrder();
271
336
  * throw error;
272
337
  * }
273
338
  * ```
@@ -278,16 +343,15 @@ export class AndcoError extends Error {
278
343
  /** HTTP status when the error came from a response. */
279
344
  status;
280
345
  /**
281
- * The remaining fields of the error body.
282
- *
283
- * Andco reports more than a code — `required_scope` on an authorization failure, per-field notes
284
- * on a rejected input. Branch on `code`, but surface these: without them an integrator can only
285
- * tell a user that something failed, not which permission is missing.
346
+ * What the SDK learned about a failure it detected itself, beyond its code: the Intent of
347
+ * `intent_ended`, the problems of a refused argument (`issues`).
286
348
  *
287
349
  * When the Authorization Server refused an exchange (a refresh, a code or device exchange, a
288
350
  * revocation) with an RFC 6749 error body, it carries two keys of its own: `oauth_error`, the
289
351
  * answer's `error` (`"invalid_grant"`, `"temporarily_unavailable"`, ...), and, when the server sent
290
352
  * one, `oauth_error_description`. They are absent when the server answered nothing OAuth-shaped.
353
+ *
354
+ * The fields of an error the API answered are not here: they are an `AndcoResponseError`'s `data`.
291
355
  */
292
356
  details;
293
357
  constructor(code, options = {}) {
@@ -308,11 +372,34 @@ export class AndcoError extends Error {
308
372
  static is(cause) {
309
373
  return cause instanceof AndcoError;
310
374
  }
375
+ /**
376
+ * Whether a value is one of the failures the SDK's own credentials throw instead of a token: an
377
+ * `AndcoError` with a code of {@link ANDCO_CREDENTIALS_ERROR_CODES}, or an
378
+ * {@link AndcoAuthorizationRequiredError}. Any other `AndcoError` is not, whatever its code.
379
+ *
380
+ * @example
381
+ * ```ts
382
+ * try {
383
+ * return await credentials.accessTokenFor(resource);
384
+ * } catch (cause) {
385
+ * if (AndcoError.isCredentialsError(cause)) return signOut(cause.code);
386
+ * throw cause;
387
+ * }
388
+ * ```
389
+ */
390
+ static isCredentialsError(cause) {
391
+ if (cause instanceof AndcoAuthorizationRequiredError)
392
+ return true;
393
+ if (!AndcoError.is(cause))
394
+ return false;
395
+ return ANDCO_CREDENTIALS_ERROR_CODES.includes(cause.code);
396
+ }
311
397
  /**
312
398
  * Whether the Grant or session behind a failure no longer works, so the only way out is for the
313
399
  * person to authorize again. It is `true` for:
314
400
  *
315
- * - a `401` from the Resource API,
401
+ * - a `401` from the Resource API (`invalid_token`, the only error the API answers with 401 for a
402
+ * bad token), as an `AndcoError` or as the raw error body `rest.raw` hands through,
316
403
  * - `session_missing`,
317
404
  * - `no_token_for_resource`,
318
405
  * - `refresh_failed` whose `details.oauth_error` is `invalid_grant` or `invalid_token`.
@@ -331,6 +418,9 @@ export class AndcoError extends Error {
331
418
  * ```
332
419
  */
333
420
  static isAuthorizationEnded(cause) {
421
+ if (typeof cause === "object" && cause !== null && !AndcoError.is(cause)) {
422
+ return cause["error"] === "invalid_token";
423
+ }
334
424
  if (!AndcoError.is(cause))
335
425
  return false;
336
426
  if (cause.status === 401)
@@ -348,3 +438,25 @@ export class AndcoError extends Error {
348
438
  }
349
439
  }
350
440
  }
441
+ /**
442
+ * No approved Grant covers a Service's subject and resource. Version one never opens a login flow:
443
+ * a person approves the Grant in Platform.
444
+ *
445
+ * @example
446
+ * ```ts
447
+ * const { error } = await bank.client.http.GET("/accounts");
448
+ * if (error?.code === "authorization_required") console.error(error.subject, error.resource);
449
+ * ```
450
+ */
451
+ export class AndcoAuthorizationRequiredError extends AndcoError {
452
+ subject;
453
+ resource;
454
+ constructor(subject, resource) {
455
+ super("authorization_required", {
456
+ message: "No approved Grant covers this subject and resource; approve one in Platform",
457
+ details: { subject, resource },
458
+ });
459
+ this.subject = subject;
460
+ this.resource = resource;
461
+ }
462
+ }
package/dist/index.d.ts CHANGED
@@ -8,6 +8,7 @@ export * from "./errors.js";
8
8
  export * from "./format-money.js";
9
9
  export * from "./intent-input.js";
10
10
  export * from "./intent-status.js";
11
+ export * from "./issues.js";
11
12
  export * from "./messages/base.js";
12
13
  export * from "./messages/click.js";
13
14
  export * from "./messages/error.js";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,oBAAoB,CAAC;AACnC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,qBAAqB,CAAC;AACpC,cAAc,cAAc,CAAC;AAC7B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,qBAAqB,CAAC;AACpC,cAAc,qBAAqB,CAAC;AACpC,cAAc,qBAAqB,CAAC;AACpC,cAAc,oBAAoB,CAAC;AACnC,cAAc,qBAAqB,CAAC;AACpC,cAAc,qCAAqC,CAAC;AACpD,cAAc,qBAAqB,CAAC;AACpC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,mBAAmB,CAAC;AAClC,cAAc,uBAAuB,CAAC;AACtC,cAAc,aAAa,CAAC;AAC5B,cAAc,oBAAoB,CAAC;AACnC,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,qBAAqB,CAAC;AACpC,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC;AAC1B,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC;AAC3B,OAAO,EACL,uBAAuB,EACvB,KAAK,kBAAkB,EACvB,KAAK,kBAAkB,EACvB,SAAS,EACT,kBAAkB,GACnB,MAAM,sBAAsB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,oBAAoB,CAAC;AACnC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,qBAAqB,CAAC;AACpC,cAAc,cAAc,CAAC;AAC7B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,aAAa,CAAC;AAC5B,cAAc,oBAAoB,CAAC;AACnC,cAAc,qBAAqB,CAAC;AACpC,cAAc,qBAAqB,CAAC;AACpC,cAAc,qBAAqB,CAAC;AACpC,cAAc,oBAAoB,CAAC;AACnC,cAAc,qBAAqB,CAAC;AACpC,cAAc,qCAAqC,CAAC;AACpD,cAAc,qBAAqB,CAAC;AACpC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,mBAAmB,CAAC;AAClC,cAAc,uBAAuB,CAAC;AACtC,cAAc,aAAa,CAAC;AAC5B,cAAc,oBAAoB,CAAC;AACnC,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,qBAAqB,CAAC;AACpC,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC;AAC1B,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC;AAC3B,OAAO,EACL,uBAAuB,EACvB,KAAK,kBAAkB,EACvB,KAAK,kBAAkB,EACvB,SAAS,EACT,kBAAkB,GACnB,MAAM,sBAAsB,CAAC"}
package/dist/index.js CHANGED
@@ -8,6 +8,7 @@ export * from "./errors.js";
8
8
  export * from "./format-money.js";
9
9
  export * from "./intent-input.js";
10
10
  export * from "./intent-status.js";
11
+ export * from "./issues.js";
11
12
  export * from "./messages/base.js";
12
13
  export * from "./messages/click.js";
13
14
  export * from "./messages/error.js";