@ericminassian/platform-core 0.2.0 → 0.4.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 (89) hide show
  1. package/dist/api.d.ts +1847 -212
  2. package/dist/api.js +694 -1
  3. package/dist/api.js.map +1 -1
  4. package/dist/auth.d.ts +19 -0
  5. package/dist/auth.js +32 -0
  6. package/dist/auth.js.map +1 -1
  7. package/dist/base64url.d.ts +3 -0
  8. package/dist/base64url.js +15 -0
  9. package/dist/base64url.js.map +1 -0
  10. package/dist/bindings.d.ts +69 -13
  11. package/dist/bindings.js +1 -1
  12. package/dist/bindings.js.map +1 -1
  13. package/dist/cloudfront-function.d.ts +5 -1
  14. package/dist/cloudfront-function.js +70 -2
  15. package/dist/cloudfront-function.js.map +1 -1
  16. package/dist/compute.d.ts +52 -0
  17. package/dist/compute.js +65 -0
  18. package/dist/compute.js.map +1 -0
  19. package/dist/config.d.ts +21 -1
  20. package/dist/config.js +16 -0
  21. package/dist/config.js.map +1 -1
  22. package/dist/constants.d.ts +67 -1
  23. package/dist/constants.js +56 -1
  24. package/dist/constants.js.map +1 -1
  25. package/dist/email.d.ts +84 -0
  26. package/dist/email.js +111 -0
  27. package/dist/email.js.map +1 -0
  28. package/dist/fingerprint.d.ts +9 -0
  29. package/dist/fingerprint.js +19 -0
  30. package/dist/fingerprint.js.map +1 -0
  31. package/dist/index.d.ts +6 -0
  32. package/dist/index.js +6 -0
  33. package/dist/index.js.map +1 -1
  34. package/dist/jobs.d.ts +111 -0
  35. package/dist/jobs.js +214 -0
  36. package/dist/jobs.js.map +1 -0
  37. package/dist/manifest.d.ts +357 -112
  38. package/dist/manifest.js +270 -72
  39. package/dist/manifest.js.map +1 -1
  40. package/dist/naming.d.ts +93 -6
  41. package/dist/naming.js +98 -6
  42. package/dist/naming.js.map +1 -1
  43. package/dist/platform-api.d.ts +7792 -0
  44. package/dist/platform-api.js +455 -0
  45. package/dist/platform-api.js.map +1 -0
  46. package/dist/realtime.d.ts +139 -0
  47. package/dist/realtime.js +201 -0
  48. package/dist/realtime.js.map +1 -0
  49. package/dist/router/decide.d.ts +7 -1
  50. package/dist/router/decide.js +42 -5
  51. package/dist/router.d.ts +35 -13
  52. package/dist/router.js +36 -0
  53. package/dist/router.js.map +1 -1
  54. package/dist/runtime-config.d.ts +57 -0
  55. package/dist/runtime-config.js +12 -0
  56. package/dist/runtime-config.js.map +1 -1
  57. package/dist/storage.d.ts +55 -0
  58. package/dist/storage.js +112 -0
  59. package/dist/storage.js.map +1 -0
  60. package/dist/telemetry.d.ts +350 -83
  61. package/dist/telemetry.js +188 -15
  62. package/dist/telemetry.js.map +1 -1
  63. package/dist/web-compute.d.ts +191 -0
  64. package/dist/web-compute.js +257 -0
  65. package/dist/web-compute.js.map +1 -0
  66. package/package.json +28 -8
  67. package/src/api.ts +864 -238
  68. package/src/auth.ts +37 -0
  69. package/src/base64url.ts +13 -0
  70. package/src/bindings.ts +74 -7
  71. package/src/cloudfront-function.ts +70 -2
  72. package/src/compute.ts +86 -0
  73. package/src/config.ts +34 -2
  74. package/src/constants.ts +76 -1
  75. package/src/email.ts +168 -0
  76. package/src/fingerprint.ts +20 -0
  77. package/src/index.ts +6 -0
  78. package/src/jobs.ts +281 -0
  79. package/src/manifest.ts +542 -114
  80. package/src/naming.ts +103 -8
  81. package/src/platform-api.ts +630 -0
  82. package/src/realtime.ts +298 -0
  83. package/src/router/decide.d.ts +7 -1
  84. package/src/router/decide.js +42 -5
  85. package/src/router.ts +33 -13
  86. package/src/runtime-config.ts +70 -0
  87. package/src/storage.ts +120 -0
  88. package/src/telemetry.ts +198 -93
  89. package/src/web-compute.ts +342 -0
@@ -0,0 +1,630 @@
1
+ /**
2
+ * The control plane's HTTP API (`/v1`) as an Effect `HttpApi`: the control
3
+ * plane implements it, the console calls it through a client generated from
4
+ * it, and its schemas describe what the CLI sends and reads.
5
+ *
6
+ * Imported from `@ericminassian/platform-core/platform-api`, never from the
7
+ * core index, so app runtimes don't load it.
8
+ *
9
+ * Auth is one of:
10
+ * - `Authorization: Bearer <platform token>` (CLI, from `plat login`)
11
+ * - `Authorization: Bearer <GitHub Actions OIDC token>` (CI deploys only)
12
+ * - the console's session cookie, plus `x-platform-csrf: 1` on writes
13
+ *
14
+ * The wire format predates this definition and published CLIs depend on it:
15
+ * paths, bodies and the `{ error: { code, message } }` error body stay as
16
+ * they are.
17
+ */
18
+ import { Context, Schema, SchemaGetter } from 'effect';
19
+ import { HttpApi, HttpApiEndpoint, HttpApiGroup, HttpApiMiddleware, HttpApiSchema } from 'effect/http-api';
20
+ import {
21
+ AdminOverview,
22
+ ApiErrorBody,
23
+ AppRecord,
24
+ AppSummary,
25
+ CliLoginRequest,
26
+ CliLoginResponse,
27
+ CostResponse,
28
+ CreateAppRequest,
29
+ CreateDeploymentRequest,
30
+ CreateDeploymentResponse,
31
+ CronRunResponse,
32
+ DeadLetterAction,
33
+ DeadLetterActionResponse,
34
+ DeadLettersResponse,
35
+ CustomMetricsResponse,
36
+ DataCollections,
37
+ DataPage,
38
+ DeploymentRecord,
39
+ EmailSending,
40
+ EdgeFloorResponse,
41
+ EmailStatsResponse,
42
+ ErrorGroup,
43
+ ErrorStatus,
44
+ JobsResponse,
45
+ LimitsResponse,
46
+ LogLine,
47
+ LogsResponse,
48
+ MeResponse,
49
+ MetricsResponse,
50
+ PutSecretRequest,
51
+ QueueStats,
52
+ RealtimeStatsResponse,
53
+ RollbackRequest,
54
+ RollbackResponse,
55
+ RouteStats,
56
+ SecretsResponse,
57
+ ServiceActionResponse,
58
+ ServicesResponse,
59
+ StageDestroyed,
60
+ StageDestroying,
61
+ StageRecord,
62
+ StartServiceRequest,
63
+ StorageResponse,
64
+ SuppressedResponse,
65
+ TraceResponse,
66
+ UpdateAppRequest,
67
+ UpdateErrorRequest,
68
+ UptimePoint,
69
+ UrlResponse,
70
+ } from './api.ts';
71
+ import type { MemberRole } from './config.ts';
72
+
73
+ // ---------------------------------------------------------------------------
74
+ // Errors: one class per status, each encoded as `{ error: { code, message } }`
75
+ // ---------------------------------------------------------------------------
76
+
77
+ const errorFields = { code: Schema.String, message: Schema.String };
78
+ type ErrorFields = { readonly code: string; readonly message: string };
79
+
80
+ /** The wire schema for an error class: its `code`/`message` nested under `error`, answered with `status`. */
81
+ const onTheWire = <
82
+ Tag extends string,
83
+ S extends Schema.Codec<ErrorFields, ErrorFields & { readonly _tag: Tag }>,
84
+ >(
85
+ tag: Tag,
86
+ error: S,
87
+ status: number,
88
+ ) =>
89
+ ApiErrorBody.pipe(
90
+ Schema.decodeTo(error, {
91
+ decode: SchemaGetter.transform((body) => ({ _tag: tag, ...body.error })),
92
+ encode: SchemaGetter.transform(({ code, message }) => ({ error: { code, message } })),
93
+ }),
94
+ HttpApiSchema.status(status),
95
+ );
96
+
97
+ /** 400 `bad_request` (also every request that fails its schema). */
98
+ export class BadRequest extends Schema.TaggedError<BadRequest, { readonly brand: unique symbol }>()(
99
+ 'BadRequest',
100
+ errorFields,
101
+ ) {
102
+ static readonly of = (message: string) => new BadRequest({ code: 'bad_request', message });
103
+ }
104
+
105
+ /** 401 `unauthenticated`: no or invalid credentials. */
106
+ export class Unauthenticated extends Schema.TaggedError<Unauthenticated, { readonly brand: unique symbol }>()(
107
+ 'Unauthenticated',
108
+ errorFields,
109
+ ) {
110
+ static readonly of = (message: string) => new Unauthenticated({ code: 'unauthenticated', message });
111
+ }
112
+
113
+ /** 403: `forbidden`, `members_only`, `admins_only`, `not_a_member`, `csrf`. */
114
+ export class Forbidden extends Schema.TaggedError<Forbidden, { readonly brand: unique symbol }>()(
115
+ 'Forbidden',
116
+ errorFields,
117
+ ) {}
118
+
119
+ /** 404 `not_found`. */
120
+ export class NotFound extends Schema.TaggedError<NotFound, { readonly brand: unique symbol }>()(
121
+ 'NotFound',
122
+ errorFields,
123
+ ) {
124
+ static readonly of = (what: string) => new NotFound({ code: 'not_found', message: `${what} not found` });
125
+ }
126
+
127
+ /**
128
+ * 409: `exists`, `dns_conflict`, `has_stages`, `already_started`, `rollback_failed`,
129
+ * `redrive_failed`, `discard_failed`, `paused_by_aws`, `service_action_failed`.
130
+ */
131
+ export class Conflict extends Schema.TaggedError<Conflict, { readonly brand: unique symbol }>()(
132
+ 'Conflict',
133
+ errorFields,
134
+ ) {}
135
+
136
+ /** 422 `invalid_manifest`: the deployment's `plat.config.ts` doesn't validate. */
137
+ export class InvalidManifest extends Schema.TaggedError<InvalidManifest, { readonly brand: unique symbol }>()(
138
+ 'InvalidManifest',
139
+ errorFields,
140
+ ) {
141
+ static readonly of = (message: string) => new InvalidManifest({ code: 'invalid_manifest', message });
142
+ }
143
+
144
+ /** 429 `limit`: an app or stage limit from `platform.config.ts`. */
145
+ export class LimitReached extends Schema.TaggedError<LimitReached, { readonly brand: unique symbol }>()(
146
+ 'LimitReached',
147
+ errorFields,
148
+ ) {
149
+ static readonly of = (message: string) => new LimitReached({ code: 'limit', message });
150
+ }
151
+
152
+ export const BadRequestError = onTheWire('BadRequest', BadRequest, 400);
153
+ export const UnauthenticatedError = onTheWire('Unauthenticated', Unauthenticated, 401);
154
+ export const ForbiddenError = onTheWire('Forbidden', Forbidden, 403);
155
+ export const NotFoundError = onTheWire('NotFound', NotFound, 404);
156
+ export const ConflictError = onTheWire('Conflict', Conflict, 409);
157
+ export const InvalidManifestError = onTheWire('InvalidManifest', InvalidManifest, 422);
158
+ export const LimitReachedError = onTheWire('LimitReached', LimitReached, 429);
159
+
160
+ /** Every API error; a 500 is a defect, answered by the platform's server wrapper. */
161
+ export type ApiError =
162
+ | BadRequest
163
+ | Unauthenticated
164
+ | Forbidden
165
+ | NotFound
166
+ | Conflict
167
+ | InvalidManifest
168
+ | LimitReached;
169
+
170
+ /** Errors any endpoint may answer with. */
171
+ const errors = [
172
+ BadRequestError,
173
+ UnauthenticatedError,
174
+ ForbiddenError,
175
+ NotFoundError,
176
+ ConflictError,
177
+ InvalidManifestError,
178
+ LimitReachedError,
179
+ ] as const;
180
+
181
+ // ---------------------------------------------------------------------------
182
+ // Who is calling
183
+ // ---------------------------------------------------------------------------
184
+
185
+ export interface MemberPrincipal {
186
+ readonly kind: 'member';
187
+ readonly github: string;
188
+ readonly role: MemberRole;
189
+ }
190
+
191
+ /** A GitHub Actions run, identified by its OIDC token. */
192
+ export interface CiPrincipal {
193
+ readonly kind: 'ci';
194
+ readonly repository: string;
195
+ readonly repositoryOwner: string;
196
+ readonly ref: string;
197
+ readonly eventName: string;
198
+ readonly sha?: string | undefined;
199
+ readonly actor?: string | undefined;
200
+ readonly runId?: string | undefined;
201
+ }
202
+
203
+ export type Principal = MemberPrincipal | CiPrincipal;
204
+
205
+ /** The caller, provided by `Authenticated`. */
206
+ export class CurrentPrincipal extends Context.Service<CurrentPrincipal, Principal>()(
207
+ '@ericminassian/platform-core/CurrentPrincipal',
208
+ ) {}
209
+
210
+ /**
211
+ * Requires a platform token, a GitHub Actions OIDC token or the console
212
+ * cookie (with the CSRF header on writes), and provides `CurrentPrincipal`.
213
+ * Endpoints narrow to members or admins themselves.
214
+ */
215
+ export class Authenticated extends HttpApiMiddleware.Service<Authenticated, { provides: CurrentPrincipal }>()(
216
+ '@ericminassian/platform-core/Authenticated',
217
+ { error: [UnauthenticatedError, ForbiddenError] },
218
+ ) {}
219
+
220
+ /** Header the console sends on every request; cross-site forms can't set it (CSRF guard). */
221
+ export const CSRF_HEADER = 'x-platform-csrf';
222
+
223
+ /**
224
+ * Answers a request whose path, query or body fails its schema with a 400
225
+ * `bad_request` in the API's error body, where Effect would answer an empty
226
+ * 400. Applies to every endpoint.
227
+ */
228
+ export class RequestValidation extends HttpApiMiddleware.Service<RequestValidation>()(
229
+ '@ericminassian/platform-core/RequestValidation',
230
+ { error: BadRequestError },
231
+ ) {}
232
+
233
+ // ---------------------------------------------------------------------------
234
+ // Endpoints
235
+ // ---------------------------------------------------------------------------
236
+
237
+ const app = { app: Schema.String };
238
+ const appStage = { app: Schema.String, stage: Schema.String };
239
+ /** `?stage=` defaults to prod; the server validates the name. */
240
+ const stageQuery = { stage: Schema.optional(Schema.String) };
241
+ /** `?range=1h|24h|7d|30d` or `?from=&to=` (epoch ms or ISO), and `?res=1m|1h|1d`. */
242
+ const timeQuery = {
243
+ from: Schema.optional(Schema.String),
244
+ to: Schema.optional(Schema.String),
245
+ range: Schema.optional(Schema.String),
246
+ res: Schema.optional(Schema.String),
247
+ };
248
+ const rangeQuery = { ...stageQuery, ...timeQuery };
249
+ const limit = Schema.optional(Schema.Int);
250
+
251
+ export class SessionApi extends HttpApiGroup.make('session').add(
252
+ /** Exchanges a GitHub token for a platform token (`plat login`). */
253
+ HttpApiEndpoint.post('cliLogin', '/auth/cli', {
254
+ payload: CliLoginRequest,
255
+ success: CliLoginResponse,
256
+ error: errors,
257
+ }),
258
+ HttpApiEndpoint.get('me', '/me', { success: MeResponse, error: errors }).middleware(Authenticated),
259
+ ) {}
260
+
261
+ export class AppsApi extends HttpApiGroup.make('apps')
262
+ .add(
263
+ HttpApiEndpoint.get('list', '/apps', { success: Schema.Array(AppSummary), error: errors }),
264
+ HttpApiEndpoint.post('create', '/apps', {
265
+ payload: CreateAppRequest,
266
+ success: AppRecord.pipe(HttpApiSchema.status(201)),
267
+ error: errors,
268
+ }),
269
+ HttpApiEndpoint.get('get', '/apps/:app', { params: app, success: AppSummary, error: errors }),
270
+ HttpApiEndpoint.patch('update', '/apps/:app', {
271
+ params: app,
272
+ payload: UpdateAppRequest,
273
+ success: AppRecord,
274
+ error: errors,
275
+ }),
276
+ /** Refused (409 `has_stages`) while any stage exists. */
277
+ HttpApiEndpoint.delete('remove', '/apps/:app', {
278
+ params: app,
279
+ success: HttpApiSchema.NoContent,
280
+ error: errors,
281
+ }),
282
+ )
283
+ .middleware(Authenticated) {}
284
+
285
+ export class StagesApi extends HttpApiGroup.make('stages')
286
+ .add(
287
+ HttpApiEndpoint.get('list', '/apps/:app/stages', {
288
+ params: app,
289
+ success: Schema.Array(StageRecord),
290
+ error: errors,
291
+ }),
292
+ /** 202 while destroying; 200 when there was nothing left. Prod needs `?confirm=<app>`. */
293
+ HttpApiEndpoint.delete('destroy', '/apps/:app/stages/:stage', {
294
+ params: appStage,
295
+ query: { confirm: Schema.optional(Schema.String) },
296
+ success: [
297
+ StageDestroying.pipe(HttpApiSchema.status(202)),
298
+ StageDestroyed.pipe(HttpApiSchema.status(200)),
299
+ ],
300
+ error: errors,
301
+ }),
302
+ HttpApiEndpoint.post('rollback', '/apps/:app/stages/:stage/rollback', {
303
+ params: appStage,
304
+ /** No body rolls back to the previous deployment. */
305
+ payload: [RollbackRequest, Schema.Null],
306
+ success: RollbackResponse,
307
+ error: errors,
308
+ }),
309
+ )
310
+ .middleware(Authenticated) {}
311
+
312
+ export class DeploymentsApi extends HttpApiGroup.make('deployments')
313
+ .add(
314
+ /** Newest first, without event logs or manifests. */
315
+ HttpApiEndpoint.get('list', '/apps/:app/deployments', {
316
+ params: app,
317
+ query: { stage: Schema.optional(Schema.String), limit },
318
+ success: Schema.Array(DeploymentRecord),
319
+ error: errors,
320
+ }),
321
+ /** Records the deployment and returns presigned upload forms; `start` runs it. */
322
+ HttpApiEndpoint.post('create', '/apps/:app/deployments', {
323
+ params: app,
324
+ payload: CreateDeploymentRequest,
325
+ success: CreateDeploymentResponse.pipe(HttpApiSchema.status(201)),
326
+ error: errors,
327
+ }),
328
+ HttpApiEndpoint.get('get', '/deployments/:id', {
329
+ params: { id: Schema.String },
330
+ success: DeploymentRecord,
331
+ error: errors,
332
+ }),
333
+ HttpApiEndpoint.post('start', '/deployments/:id/start', {
334
+ params: { id: Schema.String },
335
+ success: DeploymentRecord.pipe(HttpApiSchema.status(202)),
336
+ error: errors,
337
+ }),
338
+ )
339
+ .middleware(Authenticated) {}
340
+
341
+ /** A live log line, or the error that ended the tail. */
342
+ export const TailEvent = Schema.Union([
343
+ Schema.Struct({ event: Schema.Literal('line'), data: Schema.fromJsonString(LogLine) }),
344
+ Schema.Struct({ event: Schema.Literal('error'), data: Schema.String }),
345
+ ]);
346
+
347
+ export class ObserveApi extends HttpApiGroup.make('observe')
348
+ .add(
349
+ HttpApiEndpoint.get('metrics', '/apps/:app/metrics', {
350
+ params: app,
351
+ query: rangeQuery,
352
+ success: MetricsResponse,
353
+ error: errors,
354
+ }),
355
+ HttpApiEndpoint.get('routes', '/apps/:app/routes', {
356
+ params: app,
357
+ query: rangeQuery,
358
+ success: Schema.Array(RouteStats),
359
+ error: errors,
360
+ }),
361
+ HttpApiEndpoint.get('jobs', '/apps/:app/jobs', {
362
+ params: app,
363
+ query: rangeQuery,
364
+ success: JobsResponse,
365
+ error: errors,
366
+ }),
367
+ HttpApiEndpoint.get('queues', '/apps/:app/queues', {
368
+ params: app,
369
+ query: stageQuery,
370
+ success: Schema.Array(QueueStats),
371
+ error: errors,
372
+ }),
373
+ /** The stage's dead-letter queue: every job that failed its last attempt. */
374
+ HttpApiEndpoint.get('deadLetters', '/apps/:app/dead-letters', {
375
+ params: app,
376
+ query: stageQuery,
377
+ success: DeadLettersResponse,
378
+ error: errors,
379
+ }),
380
+ /** AppSync Events activity of the stage's channel namespace, from CloudWatch. */
381
+ HttpApiEndpoint.get('realtime', '/apps/:app/realtime', {
382
+ params: app,
383
+ query: rangeQuery,
384
+ success: RealtimeStatsResponse,
385
+ error: errors,
386
+ }),
387
+ /** The stage's sender, SES sending status, today's usage, and SES counts for its tenant. */
388
+ HttpApiEndpoint.get('email', '/apps/:app/email', {
389
+ params: app,
390
+ query: rangeQuery,
391
+ success: EmailStatsResponse,
392
+ error: errors,
393
+ }),
394
+ /** `Metrics.count`/`value` series per name, at 1h or 1d. */
395
+ HttpApiEndpoint.get('customMetrics', '/apps/:app/custom-metrics', {
396
+ params: app,
397
+ query: rangeQuery,
398
+ success: CustomMetricsResponse,
399
+ error: errors,
400
+ }),
401
+ /** `RateLimits` checks per limit: allowed and denied, at 1h or 1d. */
402
+ HttpApiEndpoint.get('limits', '/apps/:app/limits', {
403
+ params: app,
404
+ query: rangeQuery,
405
+ success: LimitsResponse,
406
+ error: errors,
407
+ }),
408
+ /** The stage's long-running services: ECS state, CPU and memory, starts and failed runs. */
409
+ HttpApiEndpoint.get('services', '/apps/:app/services', {
410
+ params: app,
411
+ query: rangeQuery,
412
+ success: ServicesResponse,
413
+ error: errors,
414
+ }),
415
+ HttpApiEndpoint.get('errors', '/apps/:app/errors', {
416
+ params: app,
417
+ query: { ...stageQuery, status: Schema.optional(ErrorStatus) },
418
+ success: Schema.Array(ErrorGroup),
419
+ error: errors,
420
+ }),
421
+ HttpApiEndpoint.patch('updateError', '/apps/:app/errors/:fingerprint', {
422
+ params: { ...app, fingerprint: Schema.String },
423
+ query: stageQuery,
424
+ payload: UpdateErrorRequest,
425
+ success: UpdateErrorRequest,
426
+ error: errors,
427
+ }),
428
+ HttpApiEndpoint.get('uptime', '/apps/:app/uptime', {
429
+ params: app,
430
+ query: rangeQuery,
431
+ success: Schema.Array(UptimePoint),
432
+ error: errors,
433
+ }),
434
+ HttpApiEndpoint.get('cost', '/apps/:app/cost', {
435
+ params: app,
436
+ query: { days: Schema.optional(Schema.Int) },
437
+ success: CostResponse,
438
+ error: errors,
439
+ }),
440
+ /** Logs Insights search; `next` pages on. */
441
+ HttpApiEndpoint.get('logs', '/apps/:app/logs', {
442
+ params: app,
443
+ query: {
444
+ ...rangeQuery,
445
+ q: Schema.optional(Schema.String),
446
+ /** `web`, `worker` or `service:<name>`. */
447
+ stream: Schema.optional(Schema.String),
448
+ limit,
449
+ next: Schema.optional(Schema.String),
450
+ },
451
+ success: LogsResponse,
452
+ error: errors,
453
+ }),
454
+ /**
455
+ * Server-sent events: `line` (a `LogLine` as JSON) as logs arrive, `error` if
456
+ * the tail fails. A comment every 20 s keeps the connection open: CloudFront
457
+ * drops origin connections that stay idle.
458
+ */
459
+ HttpApiEndpoint.get('tail', '/apps/:app/logs/tail', {
460
+ params: app,
461
+ query: {
462
+ ...stageQuery,
463
+ q: Schema.optional(Schema.String),
464
+ /** `web`, `worker` or `service:<name>`. */
465
+ stream: Schema.optional(Schema.String),
466
+ },
467
+ success: HttpApiSchema.StreamSse({ events: TailEvent, keepAlive: '20 seconds' }),
468
+ error: errors,
469
+ }),
470
+ HttpApiEndpoint.get('trace', '/apps/:app/traces/:trace', {
471
+ params: { ...app, trace: Schema.String },
472
+ query: rangeQuery,
473
+ success: TraceResponse,
474
+ error: errors,
475
+ }),
476
+ )
477
+ .middleware(Authenticated) {}
478
+
479
+ /** `?scope=shared` targets the app-wide secret; otherwise the stage (`?stage=`, default prod). */
480
+ const secretQuery = { ...stageQuery, scope: Schema.optional(Schema.Literals(['shared', 'stage'])) };
481
+
482
+ export class ManageApi extends HttpApiGroup.make('manage')
483
+ .add(
484
+ HttpApiEndpoint.get('collections', '/apps/:app/data/collections', {
485
+ params: app,
486
+ query: stageQuery,
487
+ success: DataCollections,
488
+ error: errors,
489
+ }),
490
+ HttpApiEndpoint.get('items', '/apps/:app/data/items', {
491
+ params: app,
492
+ query: {
493
+ ...stageQuery,
494
+ collection: Schema.String,
495
+ partition: Schema.optional(Schema.String),
496
+ cursor: Schema.optional(Schema.String),
497
+ limit,
498
+ },
499
+ success: DataPage,
500
+ error: errors,
501
+ }),
502
+ HttpApiEndpoint.get('storage', '/apps/:app/storage', {
503
+ params: app,
504
+ query: {
505
+ ...stageQuery,
506
+ store: Schema.optional(Schema.String),
507
+ prefix: Schema.optional(Schema.String),
508
+ cursor: Schema.optional(Schema.String),
509
+ },
510
+ success: StorageResponse,
511
+ error: errors,
512
+ }),
513
+ HttpApiEndpoint.get('storageUrl', '/apps/:app/storage/url', {
514
+ params: app,
515
+ query: { ...stageQuery, store: Schema.String, key: Schema.String },
516
+ success: UrlResponse,
517
+ error: errors,
518
+ }),
519
+ HttpApiEndpoint.get('secrets', '/apps/:app/secrets', {
520
+ params: app,
521
+ query: stageQuery,
522
+ success: SecretsResponse,
523
+ error: errors,
524
+ }),
525
+ HttpApiEndpoint.put('putSecret', '/apps/:app/secrets/:name', {
526
+ params: { ...app, name: Schema.String },
527
+ query: secretQuery,
528
+ payload: PutSecretRequest,
529
+ success: HttpApiSchema.NoContent,
530
+ error: errors,
531
+ }),
532
+ HttpApiEndpoint.delete('deleteSecret', '/apps/:app/secrets/:name', {
533
+ params: { ...app, name: Schema.String },
534
+ query: secretQuery,
535
+ success: HttpApiSchema.NoContent,
536
+ error: errors,
537
+ }),
538
+ HttpApiEndpoint.post('runCron', '/apps/:app/crons/:name/run', {
539
+ params: { ...app, name: Schema.String },
540
+ query: stageQuery,
541
+ success: CronRunResponse.pipe(HttpApiSchema.status(202)),
542
+ error: errors,
543
+ }),
544
+ /** The stage's suppression list (its SES tenant's), in SES's order; `next` pages on. */
545
+ HttpApiEndpoint.get('suppressed', '/apps/:app/email/suppressed', {
546
+ params: app,
547
+ query: { ...stageQuery, next: Schema.optional(Schema.String) },
548
+ success: SuppressedResponse,
549
+ error: errors,
550
+ }),
551
+ /** Lets SES send to `address` from this stage again. */
552
+ HttpApiEndpoint.delete('unsuppress', '/apps/:app/email/suppressed/:address', {
553
+ params: { ...app, address: Schema.String },
554
+ query: stageQuery,
555
+ success: HttpApiSchema.NoContent,
556
+ error: errors,
557
+ }),
558
+ /** Resumes sending after a pause by the reputation policy or by hand (not one by AWS). */
559
+ HttpApiEndpoint.post('resumeEmail', '/apps/:app/email/resume', {
560
+ params: app,
561
+ query: stageQuery,
562
+ success: EmailSending,
563
+ error: errors,
564
+ }),
565
+ /** Replaces the service's task with a fresh one on the same build. */
566
+ HttpApiEndpoint.post('restartService', '/apps/:app/services/:name/restart', {
567
+ params: { ...app, name: Schema.String },
568
+ query: stageQuery,
569
+ success: ServiceActionResponse.pipe(HttpApiSchema.status(202)),
570
+ error: errors,
571
+ }),
572
+ /**
573
+ * Runs it (desired 1). It stops again after `minutes` when given; on
574
+ * previews always, by default after its `previewMinutes` (30 when that is 0).
575
+ */
576
+ HttpApiEndpoint.post('startService', '/apps/:app/services/:name/start', {
577
+ params: { ...app, name: Schema.String },
578
+ query: stageQuery,
579
+ payload: StartServiceRequest,
580
+ success: ServiceActionResponse.pipe(HttpApiSchema.status(202)),
581
+ error: errors,
582
+ }),
583
+ /** Scales it to zero until it is started again or the stage is redeployed. */
584
+ HttpApiEndpoint.post('stopService', '/apps/:app/services/:name/stop', {
585
+ params: { ...app, name: Schema.String },
586
+ query: stageQuery,
587
+ success: ServiceActionResponse.pipe(HttpApiSchema.status(202)),
588
+ error: errors,
589
+ }),
590
+ /** Runs dead letters again from their first attempt, on the function their handler runs in now. */
591
+ HttpApiEndpoint.post('redrive', '/apps/:app/dead-letters/redrive', {
592
+ params: app,
593
+ query: stageQuery,
594
+ payload: DeadLetterAction,
595
+ success: DeadLetterActionResponse,
596
+ error: errors,
597
+ }),
598
+ HttpApiEndpoint.post('discard', '/apps/:app/dead-letters/discard', {
599
+ params: app,
600
+ query: stageQuery,
601
+ payload: DeadLetterAction,
602
+ success: DeadLetterActionResponse,
603
+ error: errors,
604
+ }),
605
+ )
606
+ .middleware(Authenticated) {}
607
+
608
+ export class AdminApi extends HttpApiGroup.make('admin')
609
+ .add(HttpApiEndpoint.get('overview', '/admin/overview', { success: AdminOverview, error: errors }))
610
+ /** The edge's per-client request floor: on or off, blocked and allowed requests, recently blocked ones. */
611
+ .add(
612
+ HttpApiEndpoint.get('edge', '/admin/edge', {
613
+ query: timeQuery,
614
+ success: EdgeFloorResponse,
615
+ error: errors,
616
+ }),
617
+ )
618
+ .middleware(Authenticated) {}
619
+
620
+ export class PlatformApi extends HttpApi.make('platform')
621
+ .add(SessionApi)
622
+ .add(AppsApi)
623
+ .add(StagesApi)
624
+ .add(DeploymentsApi)
625
+ .add(ObserveApi)
626
+ .add(ManageApi)
627
+ .add(AdminApi)
628
+ .prefix('/v1')
629
+ // After every group: `middleware` only reaches the endpoints added so far.
630
+ .middleware(RequestValidation) {}