@pplethai/cli 0.21.0 → 0.23.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.
- package/README.md +12 -14
- package/dist/bin.js +102 -102
- package/package.json +3 -6
- package/templates/golden-path/.dev.vars.example +0 -33
- package/templates/golden-path/.env.example +0 -10
- package/templates/golden-path/.github/workflows/ci.yml +0 -22
- package/templates/golden-path/AGENTS.md +0 -1
- package/templates/golden-path/CLAUDE.md +0 -187
- package/templates/golden-path/README.md +0 -118
- package/templates/golden-path/gitignore +0 -17
- package/templates/golden-path/index.html +0 -16
- package/templates/golden-path/migrations/0001_init.sql +0 -10
- package/templates/golden-path/overlay-mtime.mjs +0 -30
- package/templates/golden-path/package-lock.json +0 -4318
- package/templates/golden-path/package.json +0 -41
- package/templates/golden-path/scripts/assert-dev-compiled-out.mjs +0 -45
- package/templates/golden-path/shared/api.ts +0 -325
- package/templates/golden-path/src/auth.ts +0 -230
- package/templates/golden-path/src/call-service.ts +0 -147
- package/templates/golden-path/src/dataset-query.ts +0 -470
- package/templates/golden-path/src/dataset-schema.ts +0 -398
- package/templates/golden-path/src/db.ts +0 -32
- package/templates/golden-path/src/dev-call-service.ts +0 -392
- package/templates/golden-path/src/dev-personas.ts +0 -186
- package/templates/golden-path/src/dev-read-dataset.ts +0 -444
- package/templates/golden-path/src/dev-registry.ts +0 -294
- package/templates/golden-path/src/email.ts +0 -224
- package/templates/golden-path/src/entry-name.ts +0 -114
- package/templates/golden-path/src/env.ts +0 -38
- package/templates/golden-path/src/files.ts +0 -38
- package/templates/golden-path/src/identity-header.ts +0 -17
- package/templates/golden-path/src/index.ts +0 -276
- package/templates/golden-path/src/legal-identity.ts +0 -180
- package/templates/golden-path/src/levels.ts +0 -132
- package/templates/golden-path/src/notification-response.ts +0 -55
- package/templates/golden-path/src/notify.ts +0 -311
- package/templates/golden-path/src/payment.ts +0 -554
- package/templates/golden-path/src/read-dataset.ts +0 -490
- package/templates/golden-path/src/registry.ts +0 -128
- package/templates/golden-path/src/vite-env.d.ts +0 -27
- package/templates/golden-path/test/apply-migrations.ts +0 -4
- package/templates/golden-path/test/call-service.test.ts +0 -288
- package/templates/golden-path/test/dev-call-service.test.ts +0 -455
- package/templates/golden-path/test/dev-read-dataset.test.ts +0 -379
- package/templates/golden-path/test/dev-registry.test.ts +0 -404
- package/templates/golden-path/test/effective-sub.test.ts +0 -101
- package/templates/golden-path/test/email-offline.test.ts +0 -51
- package/templates/golden-path/test/email.test.ts +0 -117
- package/templates/golden-path/test/env.d.ts +0 -17
- package/templates/golden-path/test/fixtures/overlay/branch-list/entry.md +0 -85
- package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/audience.json +0 -3
- package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/named.json +0 -3
- package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/offices.json +0 -6
- package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/roster.json +0 -3
- package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/summary.json +0 -1
- package/templates/golden-path/test/fixtures/overlay/catalog/entry.md +0 -8
- package/templates/golden-path/test/fixtures/overlay/catalog/mocks/responses.json +0 -7
- package/templates/golden-path/test/fixtures/overlay/election/2027/score/entry.md +0 -11
- package/templates/golden-path/test/fixtures/overlay/election/2027/score/mocks/responses.json +0 -7
- package/templates/golden-path/test/fixtures/overlay/election/results/entry.md +0 -24
- package/templates/golden-path/test/fixtures/overlay/election/results/mocks/turnout.json +0 -1
- package/templates/golden-path/test/fixtures/overlay/orders/entry.md +0 -12
- package/templates/golden-path/test/fixtures/overlay/orders/mocks/responses.json +0 -36
- package/templates/golden-path/test/fixtures/overlay/platform/email/entry.md +0 -12
- package/templates/golden-path/test/fixtures/overlay/platform/email/mocks/responses.json +0 -11
- package/templates/golden-path/test/fixtures/overlay/platform/legal-identity/entry.md +0 -8
- package/templates/golden-path/test/fixtures/overlay/platform/legal-identity/mocks/responses.json +0 -13
- package/templates/golden-path/test/fixtures/overlay/platform/payments/entry.md +0 -12
- package/templates/golden-path/test/fixtures/overlay/platform/payments/mocks/responses.json +0 -71
- package/templates/golden-path/test/fixtures/overlay/platform/sms/entry.md +0 -10
- package/templates/golden-path/test/fixtures/overlay/platform/sms/mocks/responses.json +0 -7
- package/templates/golden-path/test/fixtures/overlay/score/entry.md +0 -10
- package/templates/golden-path/test/fixtures/overlay/score/mocks/responses.json +0 -7
- package/templates/golden-path/test/harness.ts +0 -153
- package/templates/golden-path/test/interaction.test.ts +0 -230
- package/templates/golden-path/test/legal-identity.test.ts +0 -323
- package/templates/golden-path/test/membership.test.ts +0 -105
- package/templates/golden-path/test/notification-response.test.ts +0 -79
- package/templates/golden-path/test/payment-app.test.ts +0 -210
- package/templates/golden-path/test/payment-offline.test.ts +0 -91
- package/templates/golden-path/test/payment.test.ts +0 -427
- package/templates/golden-path/test/payments-offline.ts +0 -33
- package/templates/golden-path/test/read-dataset.test.ts +0 -387
- package/templates/golden-path/test/service-call.test.ts +0 -166
- package/templates/golden-path/test/smoke.test.ts +0 -464
- package/templates/golden-path/tsconfig.json +0 -20
- package/templates/golden-path/tsconfig.worker.json +0 -21
- package/templates/golden-path/vite.config.ts +0 -12
- package/templates/golden-path/vitest.config.ts +0 -28
- package/templates/golden-path/web/App.tsx +0 -478
- package/templates/golden-path/web/app.css +0 -83
- package/templates/golden-path/web/main.tsx +0 -51
- package/templates/golden-path/web/pple.ts +0 -124
- package/templates/golden-path/web/vite-env.d.ts +0 -10
- package/templates/golden-path/wrangler.jsonc +0 -89
- package/templates/payment-bridge/.github/workflows/ci.yml +0 -21
- package/templates/payment-bridge/CLAUDE.md +0 -9
- package/templates/payment-bridge/README.md +0 -57
- package/templates/payment-bridge/docs/verify-webhook.md +0 -80
- package/templates/payment-bridge/docs/verify-webhook.ts +0 -29
- package/templates/payment-bridge/gitignore +0 -5
- package/templates/payment-bridge/package-lock.json +0 -4198
- package/templates/payment-bridge/package.json +0 -21
- package/templates/payment-bridge/src/bridge-key.ts +0 -18
- package/templates/payment-bridge/src/env.ts +0 -14
- package/templates/payment-bridge/src/index.ts +0 -39
- package/templates/payment-bridge/src/platform.ts +0 -49
- package/templates/payment-bridge/src/webhook.ts +0 -86
- package/templates/payment-bridge/test/docs.test.ts +0 -9
- package/templates/payment-bridge/test/fixtures/payments-responses.json +0 -71
- package/templates/payment-bridge/test/harness.ts +0 -78
- package/templates/payment-bridge/test/inbound.test.ts +0 -138
- package/templates/payment-bridge/test/webhook.test.ts +0 -174
- package/templates/payment-bridge/tsconfig.json +0 -22
- package/templates/payment-bridge/vitest.config.ts +0 -8
- package/templates/payment-bridge/wrangler.jsonc +0 -15
- package/templates/payment-bridge.version +0 -1
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
// A Notification Response: one recipient's answer to an Interactive Notification
|
|
2
|
-
// your app sent (docs/adr/0035). today-v2 delivers it through the platform to
|
|
3
|
-
// `notificationResponse(event, env, ctx)` on the Worker default export, beside
|
|
4
|
-
// `fetch` and `registry`. Define it on `worker` in `src/index.ts`.
|
|
5
|
-
//
|
|
6
|
-
// What your handler does is the status today-v2 retries on:
|
|
7
|
-
//
|
|
8
|
-
// - not defined: the platform answers 404, and answers keep coming for about
|
|
9
|
-
// a day and a half, so there is time to ship one
|
|
10
|
-
// - returns nothing: 204, delivered
|
|
11
|
-
// - returns a `Response`: that status. Return `410` to say "stop sending me
|
|
12
|
-
// these", which ends delivery for good
|
|
13
|
-
// - throws: 500, and today-v2 tries again
|
|
14
|
-
//
|
|
15
|
-
// Delivery is at-least-once and unordered. Dedupe on `event.id`. If only the
|
|
16
|
-
// first answer should count ("first approver wins"), decide it yourself by
|
|
17
|
-
// `respondedAt`, then `id`. An answer can arrive after you closed the
|
|
18
|
-
// interaction.
|
|
19
|
-
//
|
|
20
|
-
// The platform answers when the handler returns. It does not wait for work
|
|
21
|
-
// handed to `ctx.waitUntil`, so do the slow part there, after the answer is
|
|
22
|
-
// acknowledged. today-v2 gives up on a call after about 10 s.
|
|
23
|
-
//
|
|
24
|
-
// The handler gets the parsed event only: no request, no headers. The
|
|
25
|
-
// responder is a PPLE ID `sub` and nothing else. If you need more about that
|
|
26
|
-
// person, call a Service you have been granted.
|
|
27
|
-
|
|
28
|
-
import type { Env } from "./env";
|
|
29
|
-
|
|
30
|
-
export interface NotificationResponseEvent {
|
|
31
|
-
type: "notification.response";
|
|
32
|
-
/** The response's own id. The same on every retry, so dedupe on it. */
|
|
33
|
-
id: string;
|
|
34
|
-
/** The notification that was answered. `notify()` returned it when you sent. */
|
|
35
|
-
notificationId: string;
|
|
36
|
-
/** Your own id for the question, as you sent it. Absent if you sent none. */
|
|
37
|
-
ref?: string;
|
|
38
|
-
app: { slug: string };
|
|
39
|
-
/** Who answered: their PPLE ID `sub`. */
|
|
40
|
-
responder: { sub: string };
|
|
41
|
-
/** The action the recipient chose. */
|
|
42
|
-
action: string;
|
|
43
|
-
/** The form fields they filled in, by field id. A tick-list is an array. */
|
|
44
|
-
values?: Record<string, string | string[]>;
|
|
45
|
-
/** ISO 8601 time they answered. */
|
|
46
|
-
respondedAt: string;
|
|
47
|
-
/** Which delivery attempt this is, from 1. */
|
|
48
|
-
attempt: number;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export type NotificationResponseHandler = (
|
|
52
|
-
event: NotificationResponseEvent,
|
|
53
|
-
env: Env,
|
|
54
|
-
ctx: ExecutionContext,
|
|
55
|
-
) => void | Response | Promise<void | Response>;
|
|
@@ -1,311 +0,0 @@
|
|
|
1
|
-
// Push notifications: Broadcast and Direct.
|
|
2
|
-
//
|
|
3
|
-
// Every send states its audience, and the two kinds follow from that:
|
|
4
|
-
//
|
|
5
|
-
// • Broadcast (การแจ้งเตือนทุกคน) — `{ kind: "all" }`, to everyone this app
|
|
6
|
-
// can reach: its App Users ∩ its current publication-tier audience.
|
|
7
|
-
// • Direct (การแจ้งเตือนรายบุคคล) — `{ kind: "direct", recipients }`, to the
|
|
8
|
-
// people this app names, by PPLE ID `sub` or mobile number.
|
|
9
|
-
//
|
|
10
|
-
// Naming narrows the audience and never widens it — the rule, and what
|
|
11
|
-
// `not_reachable` may be read as, are in shared/api.ts. The privacy model in
|
|
12
|
-
// one line: an app can reach the people who use it, say which of them it
|
|
13
|
-
// means, and reach no one else; and it can deep-link into itself, not into
|
|
14
|
-
// another team's mini app.
|
|
15
|
-
//
|
|
16
|
-
// See docs/integration/direct-notifications.md for the contract and
|
|
17
|
-
// docs/adr/0017 for why naming recipients is safe but per-recipient *reasons*
|
|
18
|
-
// are not.
|
|
19
|
-
//
|
|
20
|
-
// Wiring: the platform provisions NOTIFY_API_BASE and this app's own
|
|
21
|
-
// NotificationApiKey as NOTIFY_API_KEY, so `POST {NOTIFY_API_BASE}/notifications`
|
|
22
|
-
// lands on PPLE Today's send route. The key is the whole address — PPLE Today
|
|
23
|
-
// resolves it to this app and derives the reachable set from that, so the
|
|
24
|
-
// request carries no app id.
|
|
25
|
-
//
|
|
26
|
-
// The wire shape below is PPLE Today's, not this app's: your code passes the
|
|
27
|
-
// friendlier `{ audience, title, body, linkPath? }` and this is where it
|
|
28
|
-
// becomes `{ audience, content: { header, message }, linkPath? }`. Keeping that
|
|
29
|
-
// translation in one place means the rest of the app never has to know the
|
|
30
|
-
// platform's vocabulary.
|
|
31
|
-
import type { Notification, NotificationDelivery, NotificationRecipient } from "../shared/api";
|
|
32
|
-
|
|
33
|
-
interface NotifyEnv {
|
|
34
|
-
NOTIFY_API_BASE: string;
|
|
35
|
-
NOTIFY_API_KEY?: string;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Every refusal this app can get back, named in terms a Builder can act on.
|
|
40
|
-
*
|
|
41
|
-
* - `notifications_not_configured` — no key bound (e.g. local dev).
|
|
42
|
-
* - `invalid_request` — the send itself was malformed: no audience, an
|
|
43
|
-
* unknown `kind`, a `direct` send with no `recipients` array.
|
|
44
|
-
* - `invalid_recipients` — the list was: empty, over the 200 cap, or an entry
|
|
45
|
-
* naming neither or both of `sub`/`phone`.
|
|
46
|
-
* - `idempotency_key_conflict` — that key was already used with a list of a
|
|
47
|
-
* different length, so its stored outcome cannot be replayed onto this one.
|
|
48
|
-
* - `quota_exceeded` — **nothing was delivered.** Quota is checked up front
|
|
49
|
-
* against the named count, so the call is rejected whole rather than
|
|
50
|
-
* half-sent.
|
|
51
|
-
* - `not_permitted` — this key may not use this route.
|
|
52
|
-
* - `invalid_interaction` — PPLE Today refused the `interaction`; `message`
|
|
53
|
-
* names the first failing path, e.g. `interaction.actions[1].requires[0]`.
|
|
54
|
-
* Nothing was sent.
|
|
55
|
-
* - `interaction_unavailable` — this app cannot send an interaction right now:
|
|
56
|
-
* no callback target yet, or the feature is switched off. Nothing was sent;
|
|
57
|
-
* retrying without `interaction` sends a plain notification.
|
|
58
|
-
* - `notification_not_found` — `closeInteraction` named an id that is not one
|
|
59
|
-
* of this app's notifications (PPLE Today answers the same for all of them).
|
|
60
|
-
* - `send_failed` — anything else.
|
|
61
|
-
*/
|
|
62
|
-
export type NotifyError =
|
|
63
|
-
| "notifications_not_configured"
|
|
64
|
-
| "invalid_request"
|
|
65
|
-
| "invalid_recipients"
|
|
66
|
-
| "idempotency_key_conflict"
|
|
67
|
-
| "quota_exceeded"
|
|
68
|
-
| "not_permitted"
|
|
69
|
-
| "invalid_interaction"
|
|
70
|
-
| "interaction_unavailable"
|
|
71
|
-
| "notification_not_found"
|
|
72
|
-
| "send_failed";
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* What the platform reports alongside a `quota_exceeded` refusal, so an app
|
|
76
|
-
* can back off rather than guess. Denominated in **deliveries**, not calls: a
|
|
77
|
-
* Direct send debits one unit per name it addressed (delivered or not), and a
|
|
78
|
-
* Broadcast debits its audience size.
|
|
79
|
-
*/
|
|
80
|
-
export interface NotifyQuota {
|
|
81
|
-
dailyQuota?: number;
|
|
82
|
-
remaining?: number;
|
|
83
|
-
/** When the budget resets (Asia/Bangkok midnight). */
|
|
84
|
-
resetAt?: string;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* `notificationId` is PPLE Today's id for the send, the one `closeInteraction`
|
|
89
|
-
* takes. A replay of an `idempotencyKey` returns the original id. It is absent
|
|
90
|
-
* only if PPLE Today did not give one (for instance an audience that reached
|
|
91
|
-
* no one).
|
|
92
|
-
*/
|
|
93
|
-
export type NotifyResult =
|
|
94
|
-
| { ok: true; audience: "all"; notificationId?: string }
|
|
95
|
-
| {
|
|
96
|
-
ok: true;
|
|
97
|
-
audience: "direct";
|
|
98
|
-
results: NotificationDelivery[];
|
|
99
|
-
notificationId?: string;
|
|
100
|
-
}
|
|
101
|
-
| {
|
|
102
|
-
ok: false;
|
|
103
|
-
status: number;
|
|
104
|
-
error: NotifyError;
|
|
105
|
-
/** PPLE Today's own words, kept for `invalid_interaction`. */
|
|
106
|
-
message?: string;
|
|
107
|
-
quota?: NotifyQuota;
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
export type CloseInteractionResult =
|
|
111
|
-
| { ok: true; closedAt?: string }
|
|
112
|
-
| { ok: false; status: number; error: NotifyError };
|
|
113
|
-
|
|
114
|
-
export async function sendNotification(
|
|
115
|
-
env: NotifyEnv,
|
|
116
|
-
input: Notification,
|
|
117
|
-
fetchImpl: typeof fetch = globalThis.fetch.bind(globalThis),
|
|
118
|
-
): Promise<NotifyResult> {
|
|
119
|
-
if (!env.NOTIFY_API_BASE || !env.NOTIFY_API_KEY) {
|
|
120
|
-
// Not wired up (e.g. local dev without a key). Fail loud, not silent.
|
|
121
|
-
return { ok: false, status: 503, error: "notifications_not_configured" };
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
const res = await fetchImpl(`${env.NOTIFY_API_BASE}/notifications`, {
|
|
125
|
-
method: "POST",
|
|
126
|
-
headers: {
|
|
127
|
-
authorization: `Bearer ${env.NOTIFY_API_KEY}`,
|
|
128
|
-
"content-type": "application/json",
|
|
129
|
-
},
|
|
130
|
-
body: JSON.stringify({
|
|
131
|
-
// Stated on every send, including a broadcast. There is no shape of this
|
|
132
|
-
// request that leaves the audience to be guessed.
|
|
133
|
-
audience: input.audience,
|
|
134
|
-
content: { header: input.title, message: input.body },
|
|
135
|
-
...(input.linkPath !== undefined ? { linkPath: input.linkPath } : {}),
|
|
136
|
-
...(input.idempotencyKey !== undefined
|
|
137
|
-
? { idempotencyKey: input.idempotencyKey }
|
|
138
|
-
: {}),
|
|
139
|
-
// Passed on unchanged: PPLE Today owns its validation.
|
|
140
|
-
...(input.interaction !== undefined ? { interaction: input.interaction } : {}),
|
|
141
|
-
}),
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
if (!res.ok) {
|
|
145
|
-
const body = await readJson(res);
|
|
146
|
-
const code = codeOf(body);
|
|
147
|
-
const error = interactionErrorFor(code) ?? errorFor(res.status);
|
|
148
|
-
const quota = error === "quota_exceeded" ? quotaOf(body) : undefined;
|
|
149
|
-
const message = error === "invalid_interaction" ? messageOf(body) : undefined;
|
|
150
|
-
return {
|
|
151
|
-
ok: false,
|
|
152
|
-
status: res.status,
|
|
153
|
-
error,
|
|
154
|
-
...(message ? { message } : {}),
|
|
155
|
-
...(quota ? { quota } : {}),
|
|
156
|
-
};
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
const body = await readJson(res);
|
|
160
|
-
const id = (body as { notificationId?: unknown } | null)?.notificationId;
|
|
161
|
-
const notificationId = typeof id === "string" ? { notificationId: id } : {};
|
|
162
|
-
if (input.audience.kind === "all") return { ok: true, audience: "all", ...notificationId };
|
|
163
|
-
return {
|
|
164
|
-
ok: true,
|
|
165
|
-
audience: "direct",
|
|
166
|
-
results: zipResults(input.audience.recipients, body),
|
|
167
|
-
...notificationId,
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* Stop accepting answers to one of this app's Interactive Notifications.
|
|
173
|
-
* Idempotent: closing again succeeds. Answers already in flight can still
|
|
174
|
-
* arrive after this returns, so an app that needs "first wins" settles it on
|
|
175
|
-
* its own side by `respondedAt`.
|
|
176
|
-
*/
|
|
177
|
-
export async function closeInteraction(
|
|
178
|
-
env: NotifyEnv,
|
|
179
|
-
notificationId: string,
|
|
180
|
-
fetchImpl: typeof fetch = globalThis.fetch.bind(globalThis),
|
|
181
|
-
): Promise<CloseInteractionResult> {
|
|
182
|
-
if (!env.NOTIFY_API_BASE || !env.NOTIFY_API_KEY) {
|
|
183
|
-
return { ok: false, status: 503, error: "notifications_not_configured" };
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
const res = await fetchImpl(
|
|
187
|
-
`${env.NOTIFY_API_BASE}/notifications/${encodeURIComponent(notificationId)}/interaction/close`,
|
|
188
|
-
{ method: "POST", headers: { authorization: `Bearer ${env.NOTIFY_API_KEY}` } },
|
|
189
|
-
);
|
|
190
|
-
|
|
191
|
-
if (!res.ok) {
|
|
192
|
-
const code = codeOf(await readJson(res));
|
|
193
|
-
const error =
|
|
194
|
-
interactionErrorFor(code) ??
|
|
195
|
-
(res.status === 404
|
|
196
|
-
? "notification_not_found"
|
|
197
|
-
: res.status === 401 || res.status === 403
|
|
198
|
-
? "not_permitted"
|
|
199
|
-
: "send_failed");
|
|
200
|
-
return { ok: false, status: res.status, error };
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
const closedAt = (await readJson(res) as { closedAt?: unknown } | null)?.closedAt;
|
|
204
|
-
return { ok: true, ...(typeof closedAt === "string" ? { closedAt } : {}) };
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* PPLE Today's own codes for interaction refusals. These share status 400 with
|
|
209
|
-
* recipient refusals, so the code is read before the status is.
|
|
210
|
-
*/
|
|
211
|
-
function interactionErrorFor(code: string): NotifyError | undefined {
|
|
212
|
-
switch (code) {
|
|
213
|
-
case "NOTIFICATION_INVALID_INTERACTION":
|
|
214
|
-
return "invalid_interaction";
|
|
215
|
-
case "NOTIFICATION_INTERACTION_NO_CALLBACK":
|
|
216
|
-
case "INTERACTIVE_NOTIFICATIONS_DISABLED":
|
|
217
|
-
return "interaction_unavailable";
|
|
218
|
-
default:
|
|
219
|
-
return undefined;
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
/** PPLE Today answers `{ error: { code, message } }`; tolerate a flat body too. */
|
|
224
|
-
function errorObject(body: unknown): Record<string, unknown> {
|
|
225
|
-
if (typeof body !== "object" || body === null) return {};
|
|
226
|
-
const { error } = body as { error?: unknown };
|
|
227
|
-
return typeof error === "object" && error !== null
|
|
228
|
-
? (error as Record<string, unknown>)
|
|
229
|
-
: (body as Record<string, unknown>);
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
function codeOf(body: unknown): string {
|
|
233
|
-
const { code } = errorObject(body);
|
|
234
|
-
return typeof code === "string" ? code : "";
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
function messageOf(body: unknown): string | undefined {
|
|
238
|
-
const { message } = errorObject(body);
|
|
239
|
-
return typeof message === "string" ? message : undefined;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
/**
|
|
243
|
-
* The platform splits its refusals: schema-level ones answer 422 (its whole
|
|
244
|
-
* API validates uniformly, and this route does not get to be the exception),
|
|
245
|
-
* recipient-level ones answer 400. Both are refusals — none is a broadcast,
|
|
246
|
-
* none is a silent no-op — but a client that branches on 400 alone is wrong.
|
|
247
|
-
* The status table lives in docs/integration/direct-notifications.md.
|
|
248
|
-
*/
|
|
249
|
-
function errorFor(status: number): NotifyError {
|
|
250
|
-
switch (status) {
|
|
251
|
-
case 422:
|
|
252
|
-
return "invalid_request";
|
|
253
|
-
case 400:
|
|
254
|
-
return "invalid_recipients";
|
|
255
|
-
case 403:
|
|
256
|
-
return "not_permitted";
|
|
257
|
-
case 409:
|
|
258
|
-
return "idempotency_key_conflict";
|
|
259
|
-
case 429:
|
|
260
|
-
return "quota_exceeded";
|
|
261
|
-
default:
|
|
262
|
-
return "send_failed";
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
async function readJson(res: Response): Promise<unknown> {
|
|
267
|
-
return res.json().catch(() => null);
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
/**
|
|
271
|
-
* The three figures the contract says a 429 carries, read from the top level
|
|
272
|
-
* of the refusal body. If the platform ever nests them under an envelope, this
|
|
273
|
-
* is the one place to adjust — the refusal itself is reported either way.
|
|
274
|
-
*/
|
|
275
|
-
function quotaOf(body: unknown): NotifyQuota | undefined {
|
|
276
|
-
if (typeof body !== "object" || body === null) return undefined;
|
|
277
|
-
const { dailyQuota, remaining, resetAt } = body as Record<string, unknown>;
|
|
278
|
-
const quota: NotifyQuota = {
|
|
279
|
-
...(typeof dailyQuota === "number" ? { dailyQuota } : {}),
|
|
280
|
-
...(typeof remaining === "number" ? { remaining } : {}),
|
|
281
|
-
...(typeof resetAt === "string" ? { resetAt } : {}),
|
|
282
|
-
};
|
|
283
|
-
return Object.keys(quota).length > 0 ? quota : undefined;
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
/**
|
|
287
|
-
* Pair each recipient this app named with what the platform said about it.
|
|
288
|
-
* The results come back in the order they were named, one per entry, so this
|
|
289
|
-
* pairs them by position and hands back the caller's *own* entry — ready to
|
|
290
|
-
* match onto the list the app holds.
|
|
291
|
-
*
|
|
292
|
-
* Two rules, both in the direction of not lying: a list of a different length
|
|
293
|
-
* is not the answer to this send, so none of it is trusted; and anything not
|
|
294
|
-
* affirmatively reported `delivered` is reported `not_reachable`. See
|
|
295
|
-
* shared/api.ts for what that status may and may not be read as.
|
|
296
|
-
*/
|
|
297
|
-
function zipResults(
|
|
298
|
-
recipients: readonly NotificationRecipient[],
|
|
299
|
-
body: unknown,
|
|
300
|
-
): NotificationDelivery[] {
|
|
301
|
-
const reported = (body as { results?: unknown } | null)?.results;
|
|
302
|
-
const results =
|
|
303
|
-
Array.isArray(reported) && reported.length === recipients.length ? reported : [];
|
|
304
|
-
return recipients.map((recipient, i) => ({
|
|
305
|
-
recipient,
|
|
306
|
-
status:
|
|
307
|
-
(results[i] as { status?: unknown } | undefined)?.status === "delivered"
|
|
308
|
-
? "delivered"
|
|
309
|
-
: "not_reachable",
|
|
310
|
-
}));
|
|
311
|
-
}
|