@v3finance/core-sdk 0.18.0 → 0.20.0

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 (58) hide show
  1. package/CHANGELOG.md +69 -0
  2. package/README.md +6 -3
  3. package/dist/cjs/core/gated.d.ts +11 -2
  4. package/dist/cjs/core/gated.d.ts.map +1 -1
  5. package/dist/cjs/core/gated.js +11 -2
  6. package/dist/cjs/core/gated.js.map +1 -1
  7. package/dist/cjs/core/pagination.d.ts +4 -3
  8. package/dist/cjs/core/pagination.d.ts.map +1 -1
  9. package/dist/cjs/core/pagination.js +4 -3
  10. package/dist/cjs/core/pagination.js.map +1 -1
  11. package/dist/cjs/generated/operations.d.ts +40 -0
  12. package/dist/cjs/generated/operations.d.ts.map +1 -1
  13. package/dist/cjs/generated/operations.js +42 -2
  14. package/dist/cjs/generated/operations.js.map +1 -1
  15. package/dist/cjs/generated/types.d.ts +160 -23
  16. package/dist/cjs/generated/types.d.ts.map +1 -1
  17. package/dist/cjs/index.d.ts +1 -1
  18. package/dist/cjs/index.d.ts.map +1 -1
  19. package/dist/cjs/index.js.map +1 -1
  20. package/dist/cjs/resources/aml.d.ts +175 -7
  21. package/dist/cjs/resources/aml.d.ts.map +1 -1
  22. package/dist/cjs/resources/aml.js +165 -10
  23. package/dist/cjs/resources/aml.js.map +1 -1
  24. package/dist/cjs/version.d.ts +1 -1
  25. package/dist/cjs/version.js +1 -1
  26. package/dist/esm/core/gated.d.ts +11 -2
  27. package/dist/esm/core/gated.d.ts.map +1 -1
  28. package/dist/esm/core/gated.js +11 -2
  29. package/dist/esm/core/gated.js.map +1 -1
  30. package/dist/esm/core/pagination.d.ts +4 -3
  31. package/dist/esm/core/pagination.d.ts.map +1 -1
  32. package/dist/esm/core/pagination.js +4 -3
  33. package/dist/esm/core/pagination.js.map +1 -1
  34. package/dist/esm/generated/operations.d.ts +40 -0
  35. package/dist/esm/generated/operations.d.ts.map +1 -1
  36. package/dist/esm/generated/operations.js +42 -2
  37. package/dist/esm/generated/operations.js.map +1 -1
  38. package/dist/esm/generated/types.d.ts +160 -23
  39. package/dist/esm/generated/types.d.ts.map +1 -1
  40. package/dist/esm/index.d.ts +1 -1
  41. package/dist/esm/index.d.ts.map +1 -1
  42. package/dist/esm/index.js.map +1 -1
  43. package/dist/esm/resources/aml.d.ts +175 -7
  44. package/dist/esm/resources/aml.d.ts.map +1 -1
  45. package/dist/esm/resources/aml.js +165 -10
  46. package/dist/esm/resources/aml.js.map +1 -1
  47. package/dist/esm/version.d.ts +1 -1
  48. package/dist/esm/version.js +1 -1
  49. package/docs/concepts.md +11 -0
  50. package/docs/errors.md +28 -7
  51. package/package.json +1 -1
  52. package/src/core/gated.ts +11 -2
  53. package/src/core/pagination.ts +4 -3
  54. package/src/generated/operations.ts +42 -2
  55. package/src/generated/types.ts +167 -26
  56. package/src/index.ts +6 -1
  57. package/src/resources/aml.ts +217 -10
  58. package/src/version.ts +1 -1
@@ -1,8 +1,12 @@
1
1
  import { seg, type HttpClient } from '../core/http.js'
2
2
  import { toGated, type Gated } from '../core/gated.js'
3
+ import type { PageParams } from '../core/pagination.js'
3
4
  import type {
4
5
  AddAmlStopListBody,
5
6
  AmlDepositDecisionResult,
7
+ AmlDraft,
8
+ AmlDraftResult,
9
+ AmlDraftsPage,
6
10
  AmlOperations,
7
11
  AmlProvenance,
8
12
  AmlRuleVersions,
@@ -22,6 +26,7 @@ import type {
22
26
  AmlVisibilityGrant,
23
27
  AmlVisibilityGrants,
24
28
  ApprovalRequestCreated,
29
+ CreateAmlDecisionDraftBody,
25
30
  CreateAmlVisibilityGrantBody,
26
31
  UpdateAmlSettingsBody,
27
32
  } from '../generated/types.js'
@@ -41,6 +46,38 @@ export interface AmlOperationsFilter {
41
46
  address?: string | undefined
42
47
  }
43
48
 
49
+ /**
50
+ * `draft` — waiting to be confirmed from the phone; `sent` — confirmed and in
51
+ * force (the outcome is in `result`); `discarded` — deleted; `stale` — can no
52
+ * longer be applied (`staleReason`); `expired` — 24 hours passed. Nothing was
53
+ * applied for the last three.
54
+ */
55
+ export type AmlDraftStatus = AmlDraft['status']
56
+
57
+ /** `deposit_decision` — a decision on a deposit; the other four — an AML edit that weakens a control. */
58
+ export type AmlDraftKind = AmlDraft['kind']
59
+
60
+ export interface ListAmlDraftsParams extends PageParams {
61
+ /**
62
+ * The status as it is NOW, worked out on reading — a `draft` past its
63
+ * `expiresAt` is `expired`. Because of that a filtered page can be shorter
64
+ * than `limit` and still not be the last: only `nextCursor: null` ends it.
65
+ */
66
+ status?: AmlDraftStatus | undefined
67
+ kind?: AmlDraftKind | undefined
68
+ /**
69
+ * Drafts of a decision on this deposit — how to find the draft after a lost
70
+ * answer. With a `kind` other than `deposit_decision` it is 400.
71
+ */
72
+ depositId?: string | undefined
73
+ /**
74
+ * Drafts of an edit of this target, with `kind`: a rule id
75
+ * (`rule_update`), a stop-list entry id (`stop_list_remove`), `user:<id>` or
76
+ * `group:<id>` (`visibility_grant`), `settings` (`settings_update`).
77
+ */
78
+ targetId?: string | undefined
79
+ }
80
+
44
81
  /**
45
82
  * AML checks of a vault, in the typed model (AML v2).
46
83
  *
@@ -60,6 +97,16 @@ export interface AmlOperationsFilter {
60
97
  * Not to be confused with `registries.amlScore()`: that one keeps the
61
98
  * earlier shape for the transfer form; this one is the model decisions are
62
99
  * made on, with a record id you can come back to.
100
+ *
101
+ * AN OFFICER'S DECISIONS TAKE EFFECT ONLY FROM THE PHONE. A session that may
102
+ * not sign — a web login (the web version, the Mac and Windows apps), or a
103
+ * browser of anyone but a platform administrator — prepares them as DRAFTS:
104
+ * a decision on a deposit with `createDecisionDraft`, and an edit that
105
+ * weakens AML (`updateRule`, `removeFromStopList`, `grantVisibility`,
106
+ * `updateSettings`) comes back as `outcome: 'pending'` with the draft instead
107
+ * of being applied. The author gets a push on their phones and confirms with
108
+ * `sendDraft` from there. A draft is seen and touched only by its author, an
109
+ * administrator included: anyone else's is 404 `AML_DRAFT_NOT_FOUND`.
63
110
  */
64
111
  export class AmlResource {
65
112
  readonly #http: HttpClient
@@ -88,7 +135,13 @@ export class AmlResource {
88
135
 
89
136
  /**
90
137
  * A fresh check, always calling the provider. Paid per call — only on an
91
- * explicit action of a person. Admins only (403 for a member).
138
+ * explicit action of a person. Admins and AML officers only (403 for a
139
+ * member).
140
+ *
141
+ * At most 60 paid checks an hour per person in a vault, this method and an
142
+ * immediate `checkInternal` together: beyond that 429
143
+ * `AML_CHECK_RATE_LIMITED`, with `details.retryAfterSeconds` (and the
144
+ * `Retry-After` header) — when to try again.
92
145
  */
93
146
  recheck(input: AmlRecheckBody): Promise<AmlCheck> {
94
147
  return this.#http.request<AmlCheck>('POST', `${this.#base}/checks`, { body: input })
@@ -143,9 +196,18 @@ export class AmlResource {
143
196
  * is validated: exactly one enabled default provider (400
144
197
  * `AML_DEFAULT_PROVIDER_REQUIRED`), `maxAgeSeconds` 0…30 days (0 — every
145
198
  * check calls the paid provider). Admins and officers only.
199
+ *
200
+ * `outcome: 'applied'` carries the settings. `'pending'` — the change
201
+ * weakens AML and came from a session that may not sign (see the class):
202
+ * NOTHING was applied, the answer is the draft to confirm from the phone.
203
+ * Weakening here: widening `memberDefaultView`, another default provider,
204
+ * disabling a provider, a larger `maxAgeSeconds` or 0. The server decides;
205
+ * do not guess in advance. Settings have no version — read them again after
206
+ * saving.
146
207
  */
147
- updateSettings(input: UpdateAmlSettingsBody): Promise<AmlSettings> {
148
- return this.#http.request<AmlSettings>('PUT', `${this.#base}/settings`, { body: input })
208
+ async updateSettings(input: UpdateAmlSettingsBody): Promise<Gated<AmlSettings, AmlDraftResult>> {
209
+ const res = await this.#http.requestRaw<unknown>('PUT', `${this.#base}/settings`, { body: input })
210
+ return toGated<AmlSettings, AmlDraftResult>(res.status, res.data)
149
211
  }
150
212
 
151
213
  /** Who sees more than the vault's default. Admins and officers only. */
@@ -160,11 +222,20 @@ export class AmlResource {
160
222
  * Idempotent like every grant: the same subject again changes the role or
161
223
  * reactivates a revoked grant. You must be able to see the member
162
224
  * (`NOT_VISIBLE`); not to yourself (`SELF_SHARE`). Admins and officers only.
225
+ *
226
+ * `outcome: 'applied'` carries the grant — status 201 for a new one, 200 for
227
+ * an existing one updated. `'pending'` — the grant weakens AML (a new or
228
+ * revoked grant, or `links` → `full`) and came from a session that may not
229
+ * sign (see the class): NOTHING was granted, the answer is the draft to
230
+ * confirm from the phone. The same or a narrower role applies at once.
163
231
  */
164
- grantVisibility(input: CreateAmlVisibilityGrantBody): Promise<AmlVisibilityGrant> {
165
- return this.#http.request<AmlVisibilityGrant>('POST', `${this.#base}/visibility-grants`, {
232
+ async grantVisibility(
233
+ input: CreateAmlVisibilityGrantBody,
234
+ ): Promise<Gated<AmlVisibilityGrant, AmlDraftResult>> {
235
+ const res = await this.#http.requestRaw<unknown>('POST', `${this.#base}/visibility-grants`, {
166
236
  body: input,
167
237
  })
238
+ return toGated<AmlVisibilityGrant, AmlDraftResult>(res.status, res.data)
168
239
  }
169
240
 
170
241
  /** Soft and immediate; repeating it is not an error. Admins and officers only. */
@@ -187,6 +258,9 @@ export class AmlResource {
187
258
  * is the request's `resolvedResultId`. No policy — 403. No view of the
188
259
  * address — 404. Always a fresh, paid provider call. `outcome: 'applied'`
189
260
  * carries the check; `'pending'` — the request awaiting approval.
261
+ *
262
+ * An immediate check counts towards the limit of `recheck` (429
263
+ * `AML_CHECK_RATE_LIMITED`); creating the request does not.
190
264
  */
191
265
  async checkInternal(addressId: string): Promise<Gated<AmlCheck, ApprovalRequestCreated>> {
192
266
  const res = await this.#http.requestRaw<unknown>('POST', `${this.#base}/internal-checks`, {
@@ -232,9 +306,25 @@ export class AmlResource {
232
306
  /**
233
307
  * Absent fields stay; the RESULTING rule is validated. Every change is a new
234
308
  * `version`. 409 `AML_RULE_CONFLICT` — someone changed it meanwhile: reload.
309
+ *
310
+ * `outcome: 'applied'` carries the rule. `'pending'` — the change weakens an
311
+ * ENABLED rule and came from a session that may not sign (see the class):
312
+ * NOTHING was saved, the answer is the draft to confirm from the phone, and
313
+ * it is checked against the version the rule had when the draft was made.
314
+ * Weakening: disabling, `block` → `officer`, allowing the initiator to
315
+ * approve, dropping a kind from `appliesTo`, a `scope` not proven at least
316
+ * as wide (removing it is not), a risk condition not proven to fire at
317
+ * least as often, another `subject` or
318
+ * provider, a smaller `provenance.depth`. One weakening field sends the
319
+ * whole change. Name and description, enabling a rule and any change of a
320
+ * disabled one apply at once. The checks above (400, 404, 409
321
+ * `AML_RULE_CONFLICT`) run before the draft is made.
235
322
  */
236
- updateRule(ruleId: string, patch: PatchAmlRuleBody): Promise<AmlRule> {
237
- return this.#http.request<AmlRule>('PATCH', `${this.#base}/rules/${seg(ruleId)}`, { body: patch })
323
+ async updateRule(ruleId: string, patch: PatchAmlRuleBody): Promise<Gated<AmlRule, AmlDraftResult>> {
324
+ const res = await this.#http.requestRaw<unknown>('PATCH', `${this.#base}/rules/${seg(ruleId)}`, {
325
+ body: patch,
326
+ })
327
+ return toGated<AmlRule, AmlDraftResult>(res.status, res.data)
238
328
  }
239
329
 
240
330
  /**
@@ -263,9 +353,20 @@ export class AmlResource {
263
353
  return this.#http.request<AmlStopListEntry>('POST', `${this.#base}/stop-list`, { body: input })
264
354
  }
265
355
 
266
- /** Soft; repeating it is not an error. */
267
- removeFromStopList(entryId: string): Promise<AmlStopListEntry> {
268
- return this.#http.request<AmlStopListEntry>('DELETE', `${this.#base}/stop-list/${seg(entryId)}`)
356
+ /**
357
+ * Soft; repeating it is not an error.
358
+ *
359
+ * `outcome: 'applied'` carries the removed entry. Removing an ACTIVE address
360
+ * always weakens AML, so from a session that may not sign (see the class)
361
+ * it is `'pending'`: the address STAYS on the stop list, and the answer is
362
+ * the draft to confirm from the phone.
363
+ */
364
+ async removeFromStopList(entryId: string): Promise<Gated<AmlStopListEntry, AmlDraftResult>> {
365
+ const res = await this.#http.requestRaw<unknown>(
366
+ 'DELETE',
367
+ `${this.#base}/stop-list/${seg(entryId)}`,
368
+ )
369
+ return toGated<AmlStopListEntry, AmlDraftResult>(res.status, res.data)
269
370
  }
270
371
 
271
372
  /**
@@ -294,6 +395,112 @@ export class AmlResource {
294
395
  })
295
396
  }
296
397
 
398
+ // ---------- Officer's drafts: prepare anywhere, confirm from the phone ----------
399
+
400
+ /**
401
+ * An AML officer's decision on a deposit, made in a session that may not
402
+ * sign — the Mac above all. It is a DRAFT: nothing happens to the deposit
403
+ * until the author confirms it from the phone with `sendDraft`. Allowed in
404
+ * any session. AML officers only — an administrator is not one (403
405
+ * `FORBIDDEN`): it is a decision on risk.
406
+ *
407
+ * `decision`: `release` / `block` — a held deposit (a vote on its request
408
+ * `amlrev_<depositId>`); `unblock` — release a rejected one; `reviewed` —
409
+ * mark a past deposit reviewed. The comment is required for every one.
410
+ *
411
+ * Checked at once by the rules of the direct actions, with their codes: 404
412
+ * `DEPOSIT_NOT_FOUND`; 409 `DEPOSIT_NOT_HELD` (release, block),
413
+ * `DEPOSIT_NOT_BLOCKED` (unblock), `ALREADY_VOTED`, `DEPOSIT_ALREADY_DECIDED`
414
+ * (`details.decidedById` — who did); 403 `NOT_APPROVER`, `AML_OWN_DEPOSIT`.
415
+ * Everything is checked again when the draft is confirmed.
416
+ *
417
+ * One waiting draft per deposit and author. The same decision and comment
418
+ * again answers 200 with that draft and no second push (a new one is 201 and
419
+ * pushes); a different one is 409 `AML_DRAFT_EXISTS` with `details.draftId` —
420
+ * confirm or discard that one first. Both successes mean the same thing — a
421
+ * draft exactly as asked is waiting — so the status is not surfaced. After a
422
+ * lost answer, find it with `drafts({ depositId })`. A draft expires after
423
+ * 24 hours.
424
+ *
425
+ * The push to the author's phones has `data.type: "aml_draft_created"`, with
426
+ * `draftId`, `kind` and `incomingId` (the deposit).
427
+ */
428
+ createDecisionDraft(depositId: string, input: CreateAmlDecisionDraftBody): Promise<AmlDraftResult> {
429
+ return this.#http.request<AmlDraftResult>(
430
+ 'POST',
431
+ `${this.#base}/deposits/${seg(depositId)}/decision-drafts`,
432
+ { body: input },
433
+ )
434
+ }
435
+
436
+ /**
437
+ * Your own AML drafts, newest first — decisions on deposits and edits that
438
+ * weaken AML. Nobody else's, an administrator's view included.
439
+ * Keyset-paginated: `{items, nextCursor}`, 20 by default, at most 50.
440
+ *
441
+ * `status` is worked out on reading: a `draft` past `expiresAt` reads as
442
+ * `expired`, one that can no longer be applied (another officer decided,
443
+ * you are no longer an officer, the rule changed…) as `stale` with
444
+ * `staleReason`, and one whose action is already in force as `sent`.
445
+ */
446
+ drafts(params: ListAmlDraftsParams = {}): Promise<AmlDraftsPage> {
447
+ return this.#http.request<AmlDraftsPage>('GET', `${this.#base}/drafts`, { query: { ...params } })
448
+ }
449
+
450
+ /**
451
+ * One of your drafts — where a tap on the `aml_draft_created` push leads.
452
+ * Everything to show before confirming is in it: for a decision, the
453
+ * deposit as it was when the draft was made (`deposit`), `decision` and
454
+ * `comment`; for an edit, `target` and `changes` (was → becomes, with
455
+ * `weakens` on what weakens); where it was made (`createdFrom`). Anyone
456
+ * else's, a missing one and one from another vault are all 404
457
+ * `AML_DRAFT_NOT_FOUND`.
458
+ */
459
+ draft(draftId: string): Promise<AmlDraftResult> {
460
+ return this.#http.request<AmlDraftResult>('GET', `${this.#base}/drafts/${seg(draftId)}`)
461
+ }
462
+
463
+ /**
464
+ * Deletes a draft — softly: `draft` becomes `discarded` and can no longer
465
+ * be confirmed. Allowed in any session. Discarding again is not an error,
466
+ * nor is discarding a draft already stale or expired: 200.
467
+ *
468
+ * A confirmed one is 409 `AML_DRAFT_ALREADY_SENT` (`details.draftId`,
469
+ * `details.result`): it is in force, and discarding does not undo it. The
470
+ * same 409 comes back when the action is in force but the draft is not
471
+ * marked `sent` yet — it is being confirmed from a phone right now, or that
472
+ * broke off — and the mark is written then.
473
+ */
474
+ discardDraft(draftId: string): Promise<AmlDraftResult> {
475
+ return this.#http.request<AmlDraftResult>('DELETE', `${this.#base}/drafts/${seg(draftId)}`)
476
+ }
477
+
478
+ /**
479
+ * Confirms a draft: applies it by the SAME path as the direct action — the
480
+ * vote, `unblockDeposit`, `markDepositReviewed`, the rule edit (against the
481
+ * version the draft was made on), the stop-list removal, the grant, the
482
+ * settings. There is no body; everything comes from the draft. Only the
483
+ * author, and only from a session that may sign — the mobile app: a web
484
+ * login, or a browser of anyone but a platform administrator, gets 403
485
+ * `WEB_SESSION_CANNOT_SIGN`.
486
+ *
487
+ * 200 — `{draft}` with `status: 'sent'` and the outcome in `result`. A
488
+ * repeat (a double tap, a second phone, a retry after a broken connection)
489
+ * answers 200 with the same draft and applies nothing a second time, so a
490
+ * timed-out confirmation is retried by calling this again.
491
+ *
492
+ * Everything is checked anew first. Not applied, nothing changed:
493
+ * 409 `AML_DRAFT_STALE` — out of date (`details.staleReason`, and
494
+ * `details.cause` — the code and message the direct action gave);
495
+ * 409 `AML_DRAFT_EXPIRED`; 409 `AML_DRAFT_DISCARDED`. The draft is recorded
496
+ * as such and cannot be confirmed later — make a new one. A temporary
497
+ * refusal of the path itself (409 `DEPOSIT_MONEY_MISSING` — the money is not
498
+ * on hold yet) leaves it a `draft`: retry later.
499
+ */
500
+ sendDraft(draftId: string): Promise<AmlDraftResult> {
501
+ return this.#http.request<AmlDraftResult>('POST', `${this.#base}/drafts/${seg(draftId)}/send`)
502
+ }
503
+
297
504
  // ---------- Officer's view ----------
298
505
 
299
506
  /**
package/src/version.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /** SDK version, sent in the `user-agent` header. */
2
- export const SDK_VERSION = '0.18.0'
2
+ export const SDK_VERSION = '0.20.0'
3
3
 
4
4
  /** Default API base URL. */
5
5
  export const DEFAULT_BASE_URL = 'https://policy-engine-rest.ezig.workers.dev'