effortless-aws 0.7.1 → 0.8.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/dist/{chunk-5L76NICW.js → chunk-B4P7ZKNM.js} +11 -7
- package/dist/cli/index.js +18 -12
- package/dist/cli/index.js.map +1 -1
- package/dist/index.d.ts +444 -437
- package/dist/index.js +15 -1526
- package/dist/index.js.map +1 -1
- package/dist/runtime/wrap-app.js +3 -3
- package/dist/runtime/wrap-fifo-queue.js +2 -2
- package/dist/runtime/wrap-http.js +2 -2
- package/dist/runtime/wrap-table-stream.js +2 -2
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -88,6 +88,118 @@ type EffortlessConfig = {
|
|
|
88
88
|
*/
|
|
89
89
|
declare const defineConfig: (config: EffortlessConfig) => EffortlessConfig;
|
|
90
90
|
|
|
91
|
+
type AwsService = "dynamodb" | "s3" | "sqs" | "sns" | "ses" | "ssm" | "lambda" | "events" | "secretsmanager" | "cognito-idp" | "logs";
|
|
92
|
+
type Permission = `${AwsService}:${string}` | (string & {});
|
|
93
|
+
/** Logging verbosity level for Lambda handlers */
|
|
94
|
+
type LogLevel = "error" | "info" | "debug";
|
|
95
|
+
/**
|
|
96
|
+
* Common Lambda configuration shared by all handler types.
|
|
97
|
+
*/
|
|
98
|
+
type LambdaConfig = {
|
|
99
|
+
/** Handler name. Defaults to export name if not specified */
|
|
100
|
+
name?: string;
|
|
101
|
+
/** Lambda memory in MB (default: 256) */
|
|
102
|
+
memory?: number;
|
|
103
|
+
/** Lambda timeout in seconds (default: 30) */
|
|
104
|
+
timeout?: number;
|
|
105
|
+
/** Logging verbosity: "error" (errors only), "info" (+ execution summary), "debug" (+ input/output). Default: "info" */
|
|
106
|
+
logLevel?: LogLevel;
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* Lambda configuration with additional IAM permissions.
|
|
110
|
+
* Used by handler types that support custom permissions (http, table, fifo-queue).
|
|
111
|
+
*/
|
|
112
|
+
type LambdaWithPermissions = LambdaConfig & {
|
|
113
|
+
/** Additional IAM permissions for the Lambda */
|
|
114
|
+
permissions?: Permission[];
|
|
115
|
+
};
|
|
116
|
+
type AnyParamRef = ParamRef<any> | string;
|
|
117
|
+
/**
|
|
118
|
+
* Reference to an SSM Parameter Store parameter.
|
|
119
|
+
*
|
|
120
|
+
* @typeParam T - The resolved type after optional transform (default: string)
|
|
121
|
+
*/
|
|
122
|
+
type ParamRef<T = string> = {
|
|
123
|
+
readonly __brand: "effortless-param";
|
|
124
|
+
readonly key: string;
|
|
125
|
+
readonly transform?: (raw: string) => T;
|
|
126
|
+
};
|
|
127
|
+
/**
|
|
128
|
+
* Maps a config declaration to resolved value types.
|
|
129
|
+
* Plain strings resolve to `string`, `ParamRef<T>` resolves to `T`.
|
|
130
|
+
*
|
|
131
|
+
* @typeParam P - Record of config keys to string or ParamRef instances
|
|
132
|
+
*/
|
|
133
|
+
type ResolveConfig<P> = {
|
|
134
|
+
[K in keyof P]: P[K] extends ParamRef<infer T> ? T : string;
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* Declare an SSM Parameter Store parameter.
|
|
138
|
+
*
|
|
139
|
+
* The key is combined with project and stage at deploy time to form the full
|
|
140
|
+
* SSM path: `/${project}/${stage}/${key}`.
|
|
141
|
+
*
|
|
142
|
+
* @param key - Parameter key (e.g., "database-url")
|
|
143
|
+
* @param transform - Optional function to transform the raw string value
|
|
144
|
+
* @returns A ParamRef used by the deployment and runtime systems
|
|
145
|
+
*
|
|
146
|
+
* @example Simple string parameter
|
|
147
|
+
* ```typescript
|
|
148
|
+
* config: {
|
|
149
|
+
* dbUrl: param("database-url"),
|
|
150
|
+
* }
|
|
151
|
+
* ```
|
|
152
|
+
*
|
|
153
|
+
* @example With transform (e.g., TOML parsing)
|
|
154
|
+
* ```typescript
|
|
155
|
+
* import TOML from "smol-toml";
|
|
156
|
+
*
|
|
157
|
+
* config: {
|
|
158
|
+
* appConfig: param("app-config", TOML.parse),
|
|
159
|
+
* }
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
declare function param(key: string): ParamRef<string>;
|
|
163
|
+
declare function param<T>(key: string, transform: (raw: string) => T): ParamRef<T>;
|
|
164
|
+
/**
|
|
165
|
+
* Type-only schema helper for handlers.
|
|
166
|
+
*
|
|
167
|
+
* Use this instead of explicit generic parameters like `defineTable<Order>(...)`.
|
|
168
|
+
* It enables TypeScript to infer all generic types from the options object,
|
|
169
|
+
* avoiding the partial-inference problem where specifying one generic
|
|
170
|
+
* forces all others to their defaults.
|
|
171
|
+
*
|
|
172
|
+
* At runtime this is a no-op identity function — it simply returns the input unchanged.
|
|
173
|
+
* The type narrowing happens entirely at the TypeScript level.
|
|
174
|
+
*
|
|
175
|
+
* @example Resource-only table
|
|
176
|
+
* ```typescript
|
|
177
|
+
* type User = { id: string; email: string };
|
|
178
|
+
*
|
|
179
|
+
* // Before (breaks inference for setup, deps, config):
|
|
180
|
+
* export const users = defineTable<User>({ pk: { name: "id", type: "string" } });
|
|
181
|
+
*
|
|
182
|
+
* // After (all generics inferred correctly):
|
|
183
|
+
* export const users = defineTable({
|
|
184
|
+
* pk: { name: "id", type: "string" },
|
|
185
|
+
* schema: typed<User>(),
|
|
186
|
+
* });
|
|
187
|
+
* ```
|
|
188
|
+
*
|
|
189
|
+
* @example Table with stream handler
|
|
190
|
+
* ```typescript
|
|
191
|
+
* export const orders = defineTable({
|
|
192
|
+
* pk: { name: "id", type: "string" },
|
|
193
|
+
* schema: typed<Order>(),
|
|
194
|
+
* setup: async () => ({ db: createClient() }),
|
|
195
|
+
* onRecord: async ({ record, ctx }) => {
|
|
196
|
+
* // record.new is Order, ctx is { db: Client } — all inferred
|
|
197
|
+
* },
|
|
198
|
+
* });
|
|
199
|
+
* ```
|
|
200
|
+
*/
|
|
201
|
+
declare function typed<T>(): (input: unknown) => T;
|
|
202
|
+
|
|
91
203
|
/**
|
|
92
204
|
* Query parameters for TableClient.query()
|
|
93
205
|
*/
|
|
@@ -164,7 +276,7 @@ type TableKey = {
|
|
|
164
276
|
/** DynamoDB Streams view type - determines what data is captured in stream records */
|
|
165
277
|
type StreamView = "NEW_AND_OLD_IMAGES" | "NEW_IMAGE" | "OLD_IMAGE" | "KEYS_ONLY";
|
|
166
278
|
/**
|
|
167
|
-
* Configuration options extracted from DefineTableOptions (without onRecord/
|
|
279
|
+
* Configuration options extracted from DefineTableOptions (without onRecord/setup)
|
|
168
280
|
*/
|
|
169
281
|
type TableConfig = LambdaWithPermissions & {
|
|
170
282
|
/** Partition key definition */
|
|
@@ -215,13 +327,15 @@ type FailedRecord<T = Record<string, unknown>> = {
|
|
|
215
327
|
error: unknown;
|
|
216
328
|
};
|
|
217
329
|
/**
|
|
218
|
-
*
|
|
219
|
-
*
|
|
220
|
-
* With
|
|
330
|
+
* Setup factory type — conditional on whether deps/config are declared.
|
|
331
|
+
* No deps/config: `() => C | Promise<C>`
|
|
332
|
+
* With deps/config: `(args: { deps?, config? }) => C | Promise<C>`
|
|
221
333
|
*/
|
|
222
|
-
type
|
|
223
|
-
|
|
224
|
-
})
|
|
334
|
+
type SetupFactory$2<C, D, P> = [D | P] extends [undefined] ? () => C | Promise<C> : (args: ([D] extends [undefined] ? {} : {
|
|
335
|
+
deps: ResolveDeps$2<D>;
|
|
336
|
+
}) & ([P] extends [undefined] ? {} : {
|
|
337
|
+
config: ResolveConfig<P & {}>;
|
|
338
|
+
})) => C | Promise<C>;
|
|
225
339
|
/**
|
|
226
340
|
* Callback function type for processing a single DynamoDB stream record
|
|
227
341
|
*/
|
|
@@ -233,7 +347,7 @@ type TableRecordFn<T = Record<string, unknown>, C = undefined, R = void, D = und
|
|
|
233
347
|
}) & ([D] extends [undefined] ? {} : {
|
|
234
348
|
deps: ResolveDeps$2<D>;
|
|
235
349
|
}) & ([P] extends [undefined] ? {} : {
|
|
236
|
-
|
|
350
|
+
config: ResolveConfig<P>;
|
|
237
351
|
}) & ([S] extends [undefined] ? {} : {
|
|
238
352
|
readStatic: (path: string) => string;
|
|
239
353
|
})) => Promise<R>;
|
|
@@ -249,7 +363,7 @@ type TableBatchCompleteFn<T = Record<string, unknown>, C = undefined, R = void,
|
|
|
249
363
|
}) & ([D] extends [undefined] ? {} : {
|
|
250
364
|
deps: ResolveDeps$2<D>;
|
|
251
365
|
}) & ([P] extends [undefined] ? {} : {
|
|
252
|
-
|
|
366
|
+
config: ResolveConfig<P>;
|
|
253
367
|
}) & ([S] extends [undefined] ? {} : {
|
|
254
368
|
readStatic: (path: string) => string;
|
|
255
369
|
})) => Promise<void>;
|
|
@@ -264,7 +378,7 @@ type TableBatchFn<T = Record<string, unknown>, C = undefined, D = undefined, P =
|
|
|
264
378
|
}) & ([D] extends [undefined] ? {} : {
|
|
265
379
|
deps: ResolveDeps$2<D>;
|
|
266
380
|
}) & ([P] extends [undefined] ? {} : {
|
|
267
|
-
|
|
381
|
+
config: ResolveConfig<P>;
|
|
268
382
|
}) & ([S] extends [undefined] ? {} : {
|
|
269
383
|
readStatic: (path: string) => string;
|
|
270
384
|
})) => Promise<void>;
|
|
@@ -282,12 +396,12 @@ type DefineTableBase<T = Record<string, unknown>, C = undefined, D = undefined,
|
|
|
282
396
|
*/
|
|
283
397
|
onError?: (error: unknown) => void;
|
|
284
398
|
/**
|
|
285
|
-
* Factory function to
|
|
399
|
+
* Factory function to initialize shared state for callbacks.
|
|
286
400
|
* Called once on cold start, result is cached and reused across invocations.
|
|
287
|
-
* When params are declared, receives
|
|
401
|
+
* When deps/params are declared, receives them as argument.
|
|
288
402
|
* Supports both sync and async return values.
|
|
289
403
|
*/
|
|
290
|
-
|
|
404
|
+
setup?: SetupFactory$2<C, D, P>;
|
|
291
405
|
/**
|
|
292
406
|
* Dependencies on other handlers (tables, queues, etc.).
|
|
293
407
|
* Typed clients are injected into the handler via the `deps` argument.
|
|
@@ -296,9 +410,9 @@ type DefineTableBase<T = Record<string, unknown>, C = undefined, D = undefined,
|
|
|
296
410
|
/**
|
|
297
411
|
* SSM Parameter Store parameters.
|
|
298
412
|
* Declare with `param()` helper. Values are fetched and cached at cold start.
|
|
299
|
-
* Typed values are injected into the handler via the `
|
|
413
|
+
* Typed values are injected into the handler via the `config` argument.
|
|
300
414
|
*/
|
|
301
|
-
|
|
415
|
+
config?: P;
|
|
302
416
|
/**
|
|
303
417
|
* Static file glob patterns to bundle into the Lambda ZIP.
|
|
304
418
|
* Files are accessible at runtime via the `readStatic` callback argument.
|
|
@@ -330,12 +444,12 @@ type DefineTableOptions<T = Record<string, unknown>, C = undefined, R = void, D
|
|
|
330
444
|
*/
|
|
331
445
|
type TableHandler<T = Record<string, unknown>, C = undefined, R = void, D = undefined, P = undefined, S extends string[] | undefined = undefined> = {
|
|
332
446
|
readonly __brand: "effortless-table";
|
|
333
|
-
readonly
|
|
447
|
+
readonly __spec: TableConfig;
|
|
334
448
|
readonly schema?: (input: unknown) => T;
|
|
335
449
|
readonly onError?: (error: unknown) => void;
|
|
336
|
-
readonly
|
|
450
|
+
readonly setup?: (...args: any[]) => C | Promise<C>;
|
|
337
451
|
readonly deps?: D;
|
|
338
|
-
readonly
|
|
452
|
+
readonly config?: P;
|
|
339
453
|
readonly static?: string[];
|
|
340
454
|
readonly onRecord?: TableRecordFn<T, C, R, D, P, S>;
|
|
341
455
|
readonly onBatchComplete?: TableBatchCompleteFn<T, C, R, D, P, S>;
|
|
@@ -374,110 +488,311 @@ type TableHandler<T = Record<string, unknown>, C = undefined, R = void, D = unde
|
|
|
374
488
|
*/
|
|
375
489
|
declare const defineTable: <T = Record<string, unknown>, C = undefined, R = void, D extends Record<string, AnyTableHandler$2> | undefined = undefined, P extends Record<string, AnyParamRef> | undefined = undefined, S extends string[] | undefined = undefined>(options: DefineTableOptions<T, C, R, D, P, S>) => TableHandler<T, C, R, D, P, S>;
|
|
376
490
|
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
/** Base URL path the site is served under (e.g., "/app") */
|
|
382
|
-
path?: string;
|
|
383
|
-
/** Directory containing the static site files, relative to project root */
|
|
384
|
-
dir: string;
|
|
385
|
-
/** Default file for directory requests (default: "index.html") */
|
|
386
|
-
index?: string;
|
|
387
|
-
/** SPA mode: serve index.html for all paths that don't match a file (default: false) */
|
|
388
|
-
spa?: boolean;
|
|
389
|
-
/** Shell command to run before deploy to generate site content (e.g., "npx astro build") */
|
|
390
|
-
build?: string;
|
|
491
|
+
type AnyTableHandler$1 = TableHandler<any, any, any, any, any, any>;
|
|
492
|
+
/** Maps a deps declaration to resolved runtime client types */
|
|
493
|
+
type ResolveDeps$1<D> = {
|
|
494
|
+
[K in keyof D]: D[K] extends TableHandler<infer T, any, any, any, any> ? TableClient<T> : never;
|
|
391
495
|
};
|
|
496
|
+
/** HTTP methods supported by API Gateway */
|
|
497
|
+
type HttpMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
|
|
498
|
+
/** Short content-type aliases for common response formats */
|
|
499
|
+
type ContentType = "json" | "html" | "text" | "css" | "js" | "xml" | "csv" | "svg";
|
|
392
500
|
/**
|
|
393
|
-
*
|
|
394
|
-
* @internal
|
|
501
|
+
* Incoming HTTP request object passed to the handler
|
|
395
502
|
*/
|
|
396
|
-
type
|
|
397
|
-
|
|
398
|
-
|
|
503
|
+
type HttpRequest = {
|
|
504
|
+
/** HTTP method (GET, POST, etc.) */
|
|
505
|
+
method: string;
|
|
506
|
+
/** Request path (e.g., "/users/123") */
|
|
507
|
+
path: string;
|
|
508
|
+
/** Request headers */
|
|
509
|
+
headers: Record<string, string | undefined>;
|
|
510
|
+
/** Query string parameters */
|
|
511
|
+
query: Record<string, string | undefined>;
|
|
512
|
+
/** Path parameters extracted from route (e.g., {id} -> params.id) */
|
|
513
|
+
params: Record<string, string | undefined>;
|
|
514
|
+
/** Parsed request body (JSON parsed if Content-Type is application/json) */
|
|
515
|
+
body: unknown;
|
|
516
|
+
/** Raw unparsed request body */
|
|
517
|
+
rawBody?: string;
|
|
399
518
|
};
|
|
400
519
|
/**
|
|
401
|
-
*
|
|
402
|
-
* Files are bundled into the Lambda ZIP and served with auto-detected content types.
|
|
403
|
-
*
|
|
404
|
-
* For CDN-backed sites (S3 + CloudFront), use {@link defineStaticSite} instead.
|
|
405
|
-
*
|
|
406
|
-
* @param options - Site configuration: path, directory, optional SPA mode
|
|
407
|
-
* @returns Handler object used by the deployment system
|
|
408
|
-
*
|
|
409
|
-
* @example Basic static site
|
|
410
|
-
* ```typescript
|
|
411
|
-
* export const app = defineApp({
|
|
412
|
-
* path: "/app",
|
|
413
|
-
* dir: "src/webapp",
|
|
414
|
-
* });
|
|
415
|
-
* ```
|
|
416
|
-
*/
|
|
417
|
-
declare const defineApp: (options: AppConfig) => AppHandler;
|
|
418
|
-
|
|
419
|
-
/**
|
|
420
|
-
* Configuration for a static site handler (S3 + CloudFront)
|
|
520
|
+
* HTTP response returned from the handler
|
|
421
521
|
*/
|
|
422
|
-
type
|
|
423
|
-
/**
|
|
424
|
-
|
|
425
|
-
/**
|
|
426
|
-
|
|
427
|
-
/**
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
522
|
+
type HttpResponse = {
|
|
523
|
+
/** HTTP status code (e.g., 200, 404, 500) */
|
|
524
|
+
status: number;
|
|
525
|
+
/** Response body — JSON-serialized by default, or sent as string when contentType is set */
|
|
526
|
+
body?: unknown;
|
|
527
|
+
/**
|
|
528
|
+
* Short content-type alias. Resolves to full MIME type automatically:
|
|
529
|
+
* - `"json"` → `application/json` (default if omitted)
|
|
530
|
+
* - `"html"` → `text/html; charset=utf-8`
|
|
531
|
+
* - `"text"` → `text/plain; charset=utf-8`
|
|
532
|
+
* - `"css"` → `text/css; charset=utf-8`
|
|
533
|
+
* - `"js"` → `application/javascript; charset=utf-8`
|
|
534
|
+
* - `"xml"` → `application/xml; charset=utf-8`
|
|
535
|
+
* - `"csv"` → `text/csv; charset=utf-8`
|
|
536
|
+
* - `"svg"` → `image/svg+xml; charset=utf-8`
|
|
537
|
+
*/
|
|
538
|
+
contentType?: ContentType;
|
|
539
|
+
/** Response headers (use for custom content-types not covered by contentType) */
|
|
540
|
+
headers?: Record<string, string>;
|
|
433
541
|
};
|
|
434
542
|
/**
|
|
435
|
-
*
|
|
436
|
-
* @internal
|
|
543
|
+
* Configuration options extracted from DefineHttpOptions (without onRequest callback)
|
|
437
544
|
*/
|
|
438
|
-
type
|
|
439
|
-
|
|
440
|
-
|
|
545
|
+
type HttpConfig = LambdaWithPermissions & {
|
|
546
|
+
/** HTTP method for the route */
|
|
547
|
+
method: HttpMethod;
|
|
548
|
+
/** Route path (e.g., "/api/users", "/api/users/{id}") */
|
|
549
|
+
path: string;
|
|
441
550
|
};
|
|
442
551
|
/**
|
|
443
|
-
*
|
|
444
|
-
*
|
|
445
|
-
* @param options - Static site configuration: directory, optional SPA mode, build command
|
|
446
|
-
* @returns Handler object used by the deployment system
|
|
447
|
-
*
|
|
448
|
-
* @example Documentation site
|
|
449
|
-
* ```typescript
|
|
450
|
-
* export const docs = defineStaticSite({
|
|
451
|
-
* dir: "dist",
|
|
452
|
-
* build: "npx astro build",
|
|
453
|
-
* });
|
|
454
|
-
* ```
|
|
552
|
+
* Handler function type for HTTP endpoints
|
|
455
553
|
*
|
|
456
|
-
* @
|
|
457
|
-
*
|
|
458
|
-
*
|
|
459
|
-
*
|
|
460
|
-
* spa: true,
|
|
461
|
-
* build: "npm run build",
|
|
462
|
-
* });
|
|
463
|
-
* ```
|
|
554
|
+
* @typeParam T - Type of the validated request body (from schema function)
|
|
555
|
+
* @typeParam C - Type of the setup result (from setup function)
|
|
556
|
+
* @typeParam D - Type of the deps (from deps declaration)
|
|
557
|
+
* @typeParam P - Type of the params (from params declaration)
|
|
464
558
|
*/
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
}
|
|
559
|
+
type HttpHandlerFn<T = undefined, C = undefined, D = undefined, P = undefined, S extends string[] | undefined = undefined> = (args: {
|
|
560
|
+
req: HttpRequest;
|
|
561
|
+
} & ([T] extends [undefined] ? {} : {
|
|
562
|
+
data: T;
|
|
563
|
+
}) & ([C] extends [undefined] ? {} : {
|
|
564
|
+
ctx: C;
|
|
565
|
+
}) & ([D] extends [undefined] ? {} : {
|
|
566
|
+
deps: ResolveDeps$1<D>;
|
|
567
|
+
}) & ([P] extends [undefined] ? {} : {
|
|
568
|
+
config: ResolveConfig<P>;
|
|
569
|
+
}) & ([S] extends [undefined] ? {} : {
|
|
570
|
+
readStatic: (path: string) => string;
|
|
571
|
+
})) => Promise<HttpResponse>;
|
|
472
572
|
/**
|
|
473
|
-
*
|
|
474
|
-
*
|
|
475
|
-
*
|
|
573
|
+
* Setup factory type — conditional on whether deps/config are declared.
|
|
574
|
+
* No deps/config: `() => C | Promise<C>`
|
|
575
|
+
* With deps/config: `(args: { deps?, config? }) => C | Promise<C>`
|
|
476
576
|
*/
|
|
477
|
-
type
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
577
|
+
type SetupFactory$1<C, D, P> = [D | P] extends [undefined] ? () => C | Promise<C> : (args: ([D] extends [undefined] ? {} : {
|
|
578
|
+
deps: ResolveDeps$1<D>;
|
|
579
|
+
}) & ([P] extends [undefined] ? {} : {
|
|
580
|
+
config: ResolveConfig<P & {}>;
|
|
581
|
+
})) => C | Promise<C>;
|
|
582
|
+
/**
|
|
583
|
+
* Options for defining an HTTP endpoint
|
|
584
|
+
*
|
|
585
|
+
* @typeParam T - Type of the validated request body (inferred from schema function)
|
|
586
|
+
* @typeParam C - Type of the setup result returned by setup function
|
|
587
|
+
* @typeParam D - Type of the deps (from deps declaration)
|
|
588
|
+
* @typeParam P - Type of the params (from params declaration)
|
|
589
|
+
*/
|
|
590
|
+
type DefineHttpOptions<T = undefined, C = undefined, D extends Record<string, AnyTableHandler$1> | undefined = undefined, P extends Record<string, AnyParamRef> | undefined = undefined, S extends string[] | undefined = undefined> = HttpConfig & {
|
|
591
|
+
/**
|
|
592
|
+
* Decode/validate function for the request body.
|
|
593
|
+
* Called with the parsed body; should return typed data or throw on validation failure.
|
|
594
|
+
* When provided, the handler receives validated `data` and invalid requests get a 400 response.
|
|
595
|
+
*
|
|
596
|
+
* Works with any validation library:
|
|
597
|
+
* - Effect: `S.decodeUnknownSync(MySchema)`
|
|
598
|
+
* - Zod: `(body) => myZodSchema.parse(body)`
|
|
599
|
+
*/
|
|
600
|
+
schema?: (input: unknown) => T;
|
|
601
|
+
/**
|
|
602
|
+
* Error handler called when schema validation or onRequest throws.
|
|
603
|
+
* Receives the error and request, returns an HttpResponse.
|
|
604
|
+
* If not provided, defaults to 400 for validation errors and 500 for handler errors.
|
|
605
|
+
*/
|
|
606
|
+
onError?: (error: unknown, req: HttpRequest) => HttpResponse;
|
|
607
|
+
/**
|
|
608
|
+
* Factory function to initialize shared state for the request handler.
|
|
609
|
+
* Called once on cold start, result is cached and reused across invocations.
|
|
610
|
+
* When deps/params are declared, receives them as argument.
|
|
611
|
+
* Supports both sync and async return values.
|
|
612
|
+
*/
|
|
613
|
+
setup?: SetupFactory$1<C, D, P>;
|
|
614
|
+
/**
|
|
615
|
+
* Dependencies on other handlers (tables, queues, etc.).
|
|
616
|
+
* Typed clients are injected into the handler via the `deps` argument.
|
|
617
|
+
*/
|
|
618
|
+
deps?: D;
|
|
619
|
+
/**
|
|
620
|
+
* SSM Parameter Store parameters.
|
|
621
|
+
* Declare with `param()` helper. Values are fetched and cached at cold start.
|
|
622
|
+
* Typed values are injected into the handler via the `config` argument.
|
|
623
|
+
*/
|
|
624
|
+
config?: P;
|
|
625
|
+
/**
|
|
626
|
+
* Static file glob patterns to bundle into the Lambda ZIP.
|
|
627
|
+
* Files are accessible at runtime via the `readStatic` callback argument.
|
|
628
|
+
*/
|
|
629
|
+
static?: S;
|
|
630
|
+
/** HTTP request handler function */
|
|
631
|
+
onRequest: HttpHandlerFn<T, C, D, P, S>;
|
|
632
|
+
};
|
|
633
|
+
/**
|
|
634
|
+
* Internal handler object created by defineHttp
|
|
635
|
+
* @internal
|
|
636
|
+
*/
|
|
637
|
+
type HttpHandler<T = undefined, C = undefined, D = undefined, P = undefined, S extends string[] | undefined = undefined> = {
|
|
638
|
+
readonly __brand: "effortless-http";
|
|
639
|
+
readonly __spec: HttpConfig;
|
|
640
|
+
readonly schema?: (input: unknown) => T;
|
|
641
|
+
readonly onError?: (error: unknown, req: HttpRequest) => HttpResponse;
|
|
642
|
+
readonly setup?: (...args: any[]) => C | Promise<C>;
|
|
643
|
+
readonly deps?: D;
|
|
644
|
+
readonly config?: P;
|
|
645
|
+
readonly static?: string[];
|
|
646
|
+
readonly onRequest: HttpHandlerFn<T, C, D, P, S>;
|
|
647
|
+
};
|
|
648
|
+
/**
|
|
649
|
+
* Define an HTTP endpoint that creates an API Gateway route + Lambda function
|
|
650
|
+
*
|
|
651
|
+
* @typeParam T - Type of the validated request body (inferred from schema function)
|
|
652
|
+
* @typeParam C - Type of the setup result (inferred from setup function)
|
|
653
|
+
* @typeParam D - Type of the deps (from deps declaration)
|
|
654
|
+
* @typeParam P - Type of the params (from params declaration)
|
|
655
|
+
* @param options - Configuration, optional schema, optional setup factory, and request handler
|
|
656
|
+
* @returns Handler object used by the deployment system
|
|
657
|
+
*
|
|
658
|
+
* @example Basic GET endpoint
|
|
659
|
+
* ```typescript
|
|
660
|
+
* export const hello = defineHttp({
|
|
661
|
+
* method: "GET",
|
|
662
|
+
* path: "/hello",
|
|
663
|
+
* onRequest: async ({ req }) => ({
|
|
664
|
+
* status: 200,
|
|
665
|
+
* body: { message: "Hello World!" }
|
|
666
|
+
* })
|
|
667
|
+
* });
|
|
668
|
+
* ```
|
|
669
|
+
*
|
|
670
|
+
* @example With SSM parameters
|
|
671
|
+
* ```typescript
|
|
672
|
+
* import { param } from "effortless-aws";
|
|
673
|
+
*
|
|
674
|
+
* export const api = defineHttp({
|
|
675
|
+
* method: "GET",
|
|
676
|
+
* path: "/orders",
|
|
677
|
+
* config: {
|
|
678
|
+
* dbUrl: param("database-url"),
|
|
679
|
+
* },
|
|
680
|
+
* setup: async ({ config }) => ({
|
|
681
|
+
* pool: createPool(config.dbUrl),
|
|
682
|
+
* }),
|
|
683
|
+
* onRequest: async ({ req, ctx, config }) => ({
|
|
684
|
+
* status: 200,
|
|
685
|
+
* body: { dbUrl: config.dbUrl }
|
|
686
|
+
* })
|
|
687
|
+
* });
|
|
688
|
+
* ```
|
|
689
|
+
*/
|
|
690
|
+
declare const defineHttp: <T = undefined, C = undefined, D extends Record<string, AnyTableHandler$1> | undefined = undefined, P extends Record<string, AnyParamRef> | undefined = undefined, S extends string[] | undefined = undefined>(options: DefineHttpOptions<T, C, D, P, S>) => HttpHandler<T, C, D, P, S>;
|
|
691
|
+
|
|
692
|
+
/**
|
|
693
|
+
* Configuration for a Lambda-served static site (API Gateway + Lambda)
|
|
694
|
+
*/
|
|
695
|
+
type AppConfig = LambdaConfig & {
|
|
696
|
+
/** Base URL path the site is served under (e.g., "/app") */
|
|
697
|
+
path?: string;
|
|
698
|
+
/** Directory containing the static site files, relative to project root */
|
|
699
|
+
dir: string;
|
|
700
|
+
/** Default file for directory requests (default: "index.html") */
|
|
701
|
+
index?: string;
|
|
702
|
+
/** SPA mode: serve index.html for all paths that don't match a file (default: false) */
|
|
703
|
+
spa?: boolean;
|
|
704
|
+
/** Shell command to run before deploy to generate site content (e.g., "npx astro build") */
|
|
705
|
+
build?: string;
|
|
706
|
+
};
|
|
707
|
+
/**
|
|
708
|
+
* Internal handler object created by defineApp
|
|
709
|
+
* @internal
|
|
710
|
+
*/
|
|
711
|
+
type AppHandler = {
|
|
712
|
+
readonly __brand: "effortless-app";
|
|
713
|
+
readonly __spec: AppConfig;
|
|
714
|
+
};
|
|
715
|
+
/**
|
|
716
|
+
* Deploy a static site via Lambda + API Gateway.
|
|
717
|
+
* Files are bundled into the Lambda ZIP and served with auto-detected content types.
|
|
718
|
+
*
|
|
719
|
+
* For CDN-backed sites (S3 + CloudFront), use {@link defineStaticSite} instead.
|
|
720
|
+
*
|
|
721
|
+
* @param options - Site configuration: path, directory, optional SPA mode
|
|
722
|
+
* @returns Handler object used by the deployment system
|
|
723
|
+
*
|
|
724
|
+
* @example Basic static site
|
|
725
|
+
* ```typescript
|
|
726
|
+
* export const app = defineApp({
|
|
727
|
+
* path: "/app",
|
|
728
|
+
* dir: "src/webapp",
|
|
729
|
+
* });
|
|
730
|
+
* ```
|
|
731
|
+
*/
|
|
732
|
+
declare const defineApp: (options: AppConfig) => AppHandler;
|
|
733
|
+
|
|
734
|
+
/**
|
|
735
|
+
* Configuration for a static site handler (S3 + CloudFront)
|
|
736
|
+
*/
|
|
737
|
+
type StaticSiteConfig = {
|
|
738
|
+
/** Handler name. Defaults to export name if not specified */
|
|
739
|
+
name?: string;
|
|
740
|
+
/** Directory containing the static site files, relative to project root */
|
|
741
|
+
dir: string;
|
|
742
|
+
/** Default file for directory requests (default: "index.html") */
|
|
743
|
+
index?: string;
|
|
744
|
+
/** SPA mode: serve index.html for all paths that don't match a file (default: false) */
|
|
745
|
+
spa?: boolean;
|
|
746
|
+
/** Shell command to run before deploy to generate site content (e.g., "npx astro build") */
|
|
747
|
+
build?: string;
|
|
748
|
+
};
|
|
749
|
+
/**
|
|
750
|
+
* Internal handler object created by defineStaticSite
|
|
751
|
+
* @internal
|
|
752
|
+
*/
|
|
753
|
+
type StaticSiteHandler = {
|
|
754
|
+
readonly __brand: "effortless-static-site";
|
|
755
|
+
readonly __spec: StaticSiteConfig;
|
|
756
|
+
};
|
|
757
|
+
/**
|
|
758
|
+
* Deploy a static site via S3 + CloudFront CDN.
|
|
759
|
+
*
|
|
760
|
+
* @param options - Static site configuration: directory, optional SPA mode, build command
|
|
761
|
+
* @returns Handler object used by the deployment system
|
|
762
|
+
*
|
|
763
|
+
* @example Documentation site
|
|
764
|
+
* ```typescript
|
|
765
|
+
* export const docs = defineStaticSite({
|
|
766
|
+
* dir: "dist",
|
|
767
|
+
* build: "npx astro build",
|
|
768
|
+
* });
|
|
769
|
+
* ```
|
|
770
|
+
*
|
|
771
|
+
* @example SPA with client-side routing
|
|
772
|
+
* ```typescript
|
|
773
|
+
* export const app = defineStaticSite({
|
|
774
|
+
* dir: "dist",
|
|
775
|
+
* spa: true,
|
|
776
|
+
* build: "npm run build",
|
|
777
|
+
* });
|
|
778
|
+
* ```
|
|
779
|
+
*/
|
|
780
|
+
declare const defineStaticSite: (options: StaticSiteConfig) => StaticSiteHandler;
|
|
781
|
+
|
|
782
|
+
type AnyTableHandler = TableHandler<any, any, any, any, any, any>;
|
|
783
|
+
/** Maps a deps declaration to resolved runtime client types */
|
|
784
|
+
type ResolveDeps<D> = {
|
|
785
|
+
[K in keyof D]: D[K] extends TableHandler<infer T, any, any, any, any> ? TableClient<T> : never;
|
|
786
|
+
};
|
|
787
|
+
/**
|
|
788
|
+
* Parsed SQS FIFO message passed to the handler callbacks.
|
|
789
|
+
*
|
|
790
|
+
* @typeParam T - Type of the decoded message body (from schema function)
|
|
791
|
+
*/
|
|
792
|
+
type FifoQueueMessage<T = unknown> = {
|
|
793
|
+
/** Unique message identifier */
|
|
794
|
+
messageId: string;
|
|
795
|
+
/** Receipt handle for acknowledgement */
|
|
481
796
|
receiptHandle: string;
|
|
482
797
|
/** Parsed message body (JSON-decoded, then optionally schema-validated) */
|
|
483
798
|
body: T;
|
|
@@ -515,13 +830,15 @@ type FifoQueueConfig = LambdaWithPermissions & {
|
|
|
515
830
|
contentBasedDeduplication?: boolean;
|
|
516
831
|
};
|
|
517
832
|
/**
|
|
518
|
-
*
|
|
519
|
-
*
|
|
520
|
-
* With
|
|
833
|
+
* Setup factory type — conditional on whether deps/config are declared.
|
|
834
|
+
* No deps/config: `() => C | Promise<C>`
|
|
835
|
+
* With deps/config: `(args: { deps?, config? }) => C | Promise<C>`
|
|
521
836
|
*/
|
|
522
|
-
type
|
|
523
|
-
|
|
524
|
-
})
|
|
837
|
+
type SetupFactory<C, D, P> = [D | P] extends [undefined] ? () => C | Promise<C> : (args: ([D] extends [undefined] ? {} : {
|
|
838
|
+
deps: ResolveDeps<D>;
|
|
839
|
+
}) & ([P] extends [undefined] ? {} : {
|
|
840
|
+
config: ResolveConfig<P & {}>;
|
|
841
|
+
})) => C | Promise<C>;
|
|
525
842
|
/**
|
|
526
843
|
* Per-message handler function.
|
|
527
844
|
* Called once per message in the batch. Failures are reported individually.
|
|
@@ -531,9 +848,9 @@ type FifoQueueMessageFn<T = unknown, C = undefined, D = undefined, P = undefined
|
|
|
531
848
|
} & ([C] extends [undefined] ? {} : {
|
|
532
849
|
ctx: C;
|
|
533
850
|
}) & ([D] extends [undefined] ? {} : {
|
|
534
|
-
deps: ResolveDeps
|
|
851
|
+
deps: ResolveDeps<D>;
|
|
535
852
|
}) & ([P] extends [undefined] ? {} : {
|
|
536
|
-
|
|
853
|
+
config: ResolveConfig<P>;
|
|
537
854
|
}) & ([S] extends [undefined] ? {} : {
|
|
538
855
|
readStatic: (path: string) => string;
|
|
539
856
|
})) => Promise<void>;
|
|
@@ -546,9 +863,9 @@ type FifoQueueBatchFn<T = unknown, C = undefined, D = undefined, P = undefined,
|
|
|
546
863
|
} & ([C] extends [undefined] ? {} : {
|
|
547
864
|
ctx: C;
|
|
548
865
|
}) & ([D] extends [undefined] ? {} : {
|
|
549
|
-
deps: ResolveDeps
|
|
866
|
+
deps: ResolveDeps<D>;
|
|
550
867
|
}) & ([P] extends [undefined] ? {} : {
|
|
551
|
-
|
|
868
|
+
config: ResolveConfig<P>;
|
|
552
869
|
}) & ([S] extends [undefined] ? {} : {
|
|
553
870
|
readStatic: (path: string) => string;
|
|
554
871
|
})) => Promise<void>;
|
|
@@ -565,11 +882,11 @@ type DefineFifoQueueBase<T = unknown, C = undefined, D = undefined, P = undefine
|
|
|
565
882
|
*/
|
|
566
883
|
onError?: (error: unknown) => void;
|
|
567
884
|
/**
|
|
568
|
-
* Factory function to
|
|
885
|
+
* Factory function to initialize shared state for the handler.
|
|
569
886
|
* Called once on cold start, result is cached and reused across invocations.
|
|
570
|
-
* When params are declared, receives
|
|
887
|
+
* When deps/params are declared, receives them as argument.
|
|
571
888
|
*/
|
|
572
|
-
|
|
889
|
+
setup?: SetupFactory<C, D, P>;
|
|
573
890
|
/**
|
|
574
891
|
* Dependencies on other handlers (tables, queues, etc.).
|
|
575
892
|
* Typed clients are injected into the handler via the `deps` argument.
|
|
@@ -579,7 +896,7 @@ type DefineFifoQueueBase<T = unknown, C = undefined, D = undefined, P = undefine
|
|
|
579
896
|
* SSM Parameter Store parameters.
|
|
580
897
|
* Declare with `param()` helper. Values are fetched and cached at cold start.
|
|
581
898
|
*/
|
|
582
|
-
|
|
899
|
+
config?: P;
|
|
583
900
|
/**
|
|
584
901
|
* Static file glob patterns to bundle into the Lambda ZIP.
|
|
585
902
|
* Files are accessible at runtime via the `readStatic` callback argument.
|
|
@@ -596,19 +913,19 @@ type DefineFifoQueueWithOnBatch<T = unknown, C = undefined, D = undefined, P = u
|
|
|
596
913
|
onBatch: FifoQueueBatchFn<T, C, D, P, S>;
|
|
597
914
|
onMessage?: never;
|
|
598
915
|
};
|
|
599
|
-
type DefineFifoQueueOptions<T = unknown, C = undefined, D extends Record<string, AnyTableHandler
|
|
916
|
+
type DefineFifoQueueOptions<T = unknown, C = undefined, D extends Record<string, AnyTableHandler> | undefined = undefined, P extends Record<string, AnyParamRef> | undefined = undefined, S extends string[] | undefined = undefined> = DefineFifoQueueWithOnMessage<T, C, D, P, S> | DefineFifoQueueWithOnBatch<T, C, D, P, S>;
|
|
600
917
|
/**
|
|
601
918
|
* Internal handler object created by defineFifoQueue
|
|
602
919
|
* @internal
|
|
603
920
|
*/
|
|
604
921
|
type FifoQueueHandler<T = unknown, C = undefined, D = undefined, P = undefined, S extends string[] | undefined = undefined> = {
|
|
605
922
|
readonly __brand: "effortless-fifo-queue";
|
|
606
|
-
readonly
|
|
923
|
+
readonly __spec: FifoQueueConfig;
|
|
607
924
|
readonly schema?: (input: unknown) => T;
|
|
608
925
|
readonly onError?: (error: unknown) => void;
|
|
609
|
-
readonly
|
|
926
|
+
readonly setup?: (...args: any[]) => C | Promise<C>;
|
|
610
927
|
readonly deps?: D;
|
|
611
|
-
readonly
|
|
928
|
+
readonly config?: P;
|
|
612
929
|
readonly static?: string[];
|
|
613
930
|
readonly onMessage?: FifoQueueMessageFn<T, C, D, P, S>;
|
|
614
931
|
readonly onBatch?: FifoQueueBatchFn<T, C, D, P, S>;
|
|
@@ -643,316 +960,6 @@ type FifoQueueHandler<T = unknown, C = undefined, D = undefined, P = undefined,
|
|
|
643
960
|
* });
|
|
644
961
|
* ```
|
|
645
962
|
*/
|
|
646
|
-
declare const defineFifoQueue: <T = unknown, C = undefined, D extends Record<string, AnyTableHandler
|
|
647
|
-
|
|
648
|
-
type AwsService = "dynamodb" | "s3" | "sqs" | "sns" | "ses" | "ssm" | "lambda" | "events" | "secretsmanager" | "cognito-idp" | "logs";
|
|
649
|
-
type Permission = `${AwsService}:${string}` | (string & {});
|
|
650
|
-
/** Logging verbosity level for Lambda handlers */
|
|
651
|
-
type LogLevel = "error" | "info" | "debug";
|
|
652
|
-
/**
|
|
653
|
-
* Common Lambda configuration shared by all handler types.
|
|
654
|
-
*/
|
|
655
|
-
type LambdaConfig = {
|
|
656
|
-
/** Handler name. Defaults to export name if not specified */
|
|
657
|
-
name?: string;
|
|
658
|
-
/** Lambda memory in MB (default: 256) */
|
|
659
|
-
memory?: number;
|
|
660
|
-
/** Lambda timeout in seconds (default: 30) */
|
|
661
|
-
timeout?: number;
|
|
662
|
-
/** Logging verbosity: "error" (errors only), "info" (+ execution summary), "debug" (+ input/output). Default: "info" */
|
|
663
|
-
logLevel?: LogLevel;
|
|
664
|
-
};
|
|
665
|
-
/**
|
|
666
|
-
* Lambda configuration with additional IAM permissions.
|
|
667
|
-
* Used by handler types that support custom permissions (http, table, fifo-queue).
|
|
668
|
-
*/
|
|
669
|
-
type LambdaWithPermissions = LambdaConfig & {
|
|
670
|
-
/** Additional IAM permissions for the Lambda */
|
|
671
|
-
permissions?: Permission[];
|
|
672
|
-
};
|
|
673
|
-
type AnyParamRef = ParamRef<any>;
|
|
674
|
-
/**
|
|
675
|
-
* Reference to an SSM Parameter Store parameter.
|
|
676
|
-
*
|
|
677
|
-
* @typeParam T - The resolved type after optional transform (default: string)
|
|
678
|
-
*/
|
|
679
|
-
type ParamRef<T = string> = {
|
|
680
|
-
readonly __brand: "effortless-param";
|
|
681
|
-
readonly key: string;
|
|
682
|
-
readonly transform?: (raw: string) => T;
|
|
683
|
-
};
|
|
684
|
-
/**
|
|
685
|
-
* Maps a params declaration to resolved value types.
|
|
686
|
-
*
|
|
687
|
-
* @typeParam P - Record of param names to ParamRef instances
|
|
688
|
-
*/
|
|
689
|
-
type ResolveParams<P> = {
|
|
690
|
-
[K in keyof P]: P[K] extends ParamRef<infer T> ? T : never;
|
|
691
|
-
};
|
|
692
|
-
/**
|
|
693
|
-
* Declare an SSM Parameter Store parameter.
|
|
694
|
-
*
|
|
695
|
-
* The key is combined with project and stage at deploy time to form the full
|
|
696
|
-
* SSM path: `/${project}/${stage}/${key}`.
|
|
697
|
-
*
|
|
698
|
-
* @param key - Parameter key (e.g., "database-url")
|
|
699
|
-
* @param transform - Optional function to transform the raw string value
|
|
700
|
-
* @returns A ParamRef used by the deployment and runtime systems
|
|
701
|
-
*
|
|
702
|
-
* @example Simple string parameter
|
|
703
|
-
* ```typescript
|
|
704
|
-
* params: {
|
|
705
|
-
* dbUrl: param("database-url"),
|
|
706
|
-
* }
|
|
707
|
-
* ```
|
|
708
|
-
*
|
|
709
|
-
* @example With transform (e.g., TOML parsing)
|
|
710
|
-
* ```typescript
|
|
711
|
-
* import TOML from "smol-toml";
|
|
712
|
-
*
|
|
713
|
-
* params: {
|
|
714
|
-
* config: param("app-config", TOML.parse),
|
|
715
|
-
* }
|
|
716
|
-
* ```
|
|
717
|
-
*/
|
|
718
|
-
declare function param(key: string): ParamRef<string>;
|
|
719
|
-
declare function param<T>(key: string, transform: (raw: string) => T): ParamRef<T>;
|
|
720
|
-
/**
|
|
721
|
-
* Type-only schema helper for handlers.
|
|
722
|
-
*
|
|
723
|
-
* Use this instead of explicit generic parameters like `defineTable<Order>(...)`.
|
|
724
|
-
* It enables TypeScript to infer all generic types from the options object,
|
|
725
|
-
* avoiding the partial-inference problem where specifying one generic
|
|
726
|
-
* forces all others to their defaults.
|
|
727
|
-
*
|
|
728
|
-
* At runtime this is a no-op identity function — it simply returns the input unchanged.
|
|
729
|
-
* The type narrowing happens entirely at the TypeScript level.
|
|
730
|
-
*
|
|
731
|
-
* @example Resource-only table
|
|
732
|
-
* ```typescript
|
|
733
|
-
* type User = { id: string; email: string };
|
|
734
|
-
*
|
|
735
|
-
* // Before (breaks inference for context, deps, params):
|
|
736
|
-
* export const users = defineTable<User>({ pk: { name: "id", type: "string" } });
|
|
737
|
-
*
|
|
738
|
-
* // After (all generics inferred correctly):
|
|
739
|
-
* export const users = defineTable({
|
|
740
|
-
* pk: { name: "id", type: "string" },
|
|
741
|
-
* schema: typed<User>(),
|
|
742
|
-
* });
|
|
743
|
-
* ```
|
|
744
|
-
*
|
|
745
|
-
* @example Table with stream handler
|
|
746
|
-
* ```typescript
|
|
747
|
-
* export const orders = defineTable({
|
|
748
|
-
* pk: { name: "id", type: "string" },
|
|
749
|
-
* schema: typed<Order>(),
|
|
750
|
-
* context: async () => ({ db: createClient() }),
|
|
751
|
-
* onRecord: async ({ record, ctx }) => {
|
|
752
|
-
* // record.new is Order, ctx is { db: Client } — all inferred
|
|
753
|
-
* },
|
|
754
|
-
* });
|
|
755
|
-
* ```
|
|
756
|
-
*/
|
|
757
|
-
declare function typed<T>(): (input: unknown) => T;
|
|
758
|
-
|
|
759
|
-
type AnyTableHandler = TableHandler<any, any, any, any, any, any>;
|
|
760
|
-
/** Maps a deps declaration to resolved runtime client types */
|
|
761
|
-
type ResolveDeps<D> = {
|
|
762
|
-
[K in keyof D]: D[K] extends TableHandler<infer T, any, any, any, any> ? TableClient<T> : never;
|
|
763
|
-
};
|
|
764
|
-
/** HTTP methods supported by API Gateway */
|
|
765
|
-
type HttpMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
|
|
766
|
-
/** Short content-type aliases for common response formats */
|
|
767
|
-
type ContentType = "json" | "html" | "text" | "css" | "js" | "xml" | "csv" | "svg";
|
|
768
|
-
/**
|
|
769
|
-
* Incoming HTTP request object passed to the handler
|
|
770
|
-
*/
|
|
771
|
-
type HttpRequest = {
|
|
772
|
-
/** HTTP method (GET, POST, etc.) */
|
|
773
|
-
method: string;
|
|
774
|
-
/** Request path (e.g., "/users/123") */
|
|
775
|
-
path: string;
|
|
776
|
-
/** Request headers */
|
|
777
|
-
headers: Record<string, string | undefined>;
|
|
778
|
-
/** Query string parameters */
|
|
779
|
-
query: Record<string, string | undefined>;
|
|
780
|
-
/** Path parameters extracted from route (e.g., {id} -> params.id) */
|
|
781
|
-
params: Record<string, string | undefined>;
|
|
782
|
-
/** Parsed request body (JSON parsed if Content-Type is application/json) */
|
|
783
|
-
body: unknown;
|
|
784
|
-
/** Raw unparsed request body */
|
|
785
|
-
rawBody?: string;
|
|
786
|
-
};
|
|
787
|
-
/**
|
|
788
|
-
* HTTP response returned from the handler
|
|
789
|
-
*/
|
|
790
|
-
type HttpResponse = {
|
|
791
|
-
/** HTTP status code (e.g., 200, 404, 500) */
|
|
792
|
-
status: number;
|
|
793
|
-
/** Response body — JSON-serialized by default, or sent as string when contentType is set */
|
|
794
|
-
body?: unknown;
|
|
795
|
-
/**
|
|
796
|
-
* Short content-type alias. Resolves to full MIME type automatically:
|
|
797
|
-
* - `"json"` → `application/json` (default if omitted)
|
|
798
|
-
* - `"html"` → `text/html; charset=utf-8`
|
|
799
|
-
* - `"text"` → `text/plain; charset=utf-8`
|
|
800
|
-
* - `"css"` → `text/css; charset=utf-8`
|
|
801
|
-
* - `"js"` → `application/javascript; charset=utf-8`
|
|
802
|
-
* - `"xml"` → `application/xml; charset=utf-8`
|
|
803
|
-
* - `"csv"` → `text/csv; charset=utf-8`
|
|
804
|
-
* - `"svg"` → `image/svg+xml; charset=utf-8`
|
|
805
|
-
*/
|
|
806
|
-
contentType?: ContentType;
|
|
807
|
-
/** Response headers (use for custom content-types not covered by contentType) */
|
|
808
|
-
headers?: Record<string, string>;
|
|
809
|
-
};
|
|
810
|
-
/**
|
|
811
|
-
* Configuration options extracted from DefineHttpOptions (without onRequest callback)
|
|
812
|
-
*/
|
|
813
|
-
type HttpConfig = LambdaWithPermissions & {
|
|
814
|
-
/** HTTP method for the route */
|
|
815
|
-
method: HttpMethod;
|
|
816
|
-
/** Route path (e.g., "/api/users", "/api/users/{id}") */
|
|
817
|
-
path: string;
|
|
818
|
-
};
|
|
819
|
-
/**
|
|
820
|
-
* Handler function type for HTTP endpoints
|
|
821
|
-
*
|
|
822
|
-
* @typeParam T - Type of the validated request body (from schema function)
|
|
823
|
-
* @typeParam C - Type of the context/dependencies (from context function)
|
|
824
|
-
* @typeParam D - Type of the deps (from deps declaration)
|
|
825
|
-
* @typeParam P - Type of the params (from params declaration)
|
|
826
|
-
*/
|
|
827
|
-
type HttpHandlerFn<T = undefined, C = undefined, D = undefined, P = undefined, S extends string[] | undefined = undefined> = (args: {
|
|
828
|
-
req: HttpRequest;
|
|
829
|
-
} & ([T] extends [undefined] ? {} : {
|
|
830
|
-
data: T;
|
|
831
|
-
}) & ([C] extends [undefined] ? {} : {
|
|
832
|
-
ctx: C;
|
|
833
|
-
}) & ([D] extends [undefined] ? {} : {
|
|
834
|
-
deps: ResolveDeps<D>;
|
|
835
|
-
}) & ([P] extends [undefined] ? {} : {
|
|
836
|
-
params: ResolveParams<P>;
|
|
837
|
-
}) & ([S] extends [undefined] ? {} : {
|
|
838
|
-
readStatic: (path: string) => string;
|
|
839
|
-
})) => Promise<HttpResponse>;
|
|
840
|
-
/**
|
|
841
|
-
* Context factory type — conditional on whether params are declared.
|
|
842
|
-
* Without params: `() => C | Promise<C>`
|
|
843
|
-
* With params: `(args: { params: ResolveParams<P> }) => C | Promise<C>`
|
|
844
|
-
*/
|
|
845
|
-
type ContextFactory<C, P> = [P] extends [undefined] ? () => C | Promise<C> : (args: {
|
|
846
|
-
params: ResolveParams<P & {}>;
|
|
847
|
-
}) => C | Promise<C>;
|
|
848
|
-
/**
|
|
849
|
-
* Options for defining an HTTP endpoint
|
|
850
|
-
*
|
|
851
|
-
* @typeParam T - Type of the validated request body (inferred from schema function)
|
|
852
|
-
* @typeParam C - Type of the context/dependencies returned by context function
|
|
853
|
-
* @typeParam D - Type of the deps (from deps declaration)
|
|
854
|
-
* @typeParam P - Type of the params (from params declaration)
|
|
855
|
-
*/
|
|
856
|
-
type DefineHttpOptions<T = undefined, C = undefined, D extends Record<string, AnyTableHandler> | undefined = undefined, P extends Record<string, AnyParamRef> | undefined = undefined, S extends string[] | undefined = undefined> = HttpConfig & {
|
|
857
|
-
/**
|
|
858
|
-
* Decode/validate function for the request body.
|
|
859
|
-
* Called with the parsed body; should return typed data or throw on validation failure.
|
|
860
|
-
* When provided, the handler receives validated `data` and invalid requests get a 400 response.
|
|
861
|
-
*
|
|
862
|
-
* Works with any validation library:
|
|
863
|
-
* - Effect: `S.decodeUnknownSync(MySchema)`
|
|
864
|
-
* - Zod: `(body) => myZodSchema.parse(body)`
|
|
865
|
-
*/
|
|
866
|
-
schema?: (input: unknown) => T;
|
|
867
|
-
/**
|
|
868
|
-
* Error handler called when schema validation or onRequest throws.
|
|
869
|
-
* Receives the error and request, returns an HttpResponse.
|
|
870
|
-
* If not provided, defaults to 400 for validation errors and 500 for handler errors.
|
|
871
|
-
*/
|
|
872
|
-
onError?: (error: unknown, req: HttpRequest) => HttpResponse;
|
|
873
|
-
/**
|
|
874
|
-
* Factory function to create context/dependencies for the request handler.
|
|
875
|
-
* Called once on cold start, result is cached and reused across invocations.
|
|
876
|
-
* When params are declared, receives resolved params as argument.
|
|
877
|
-
* Supports both sync and async return values.
|
|
878
|
-
*/
|
|
879
|
-
context?: ContextFactory<C, P>;
|
|
880
|
-
/**
|
|
881
|
-
* Dependencies on other handlers (tables, queues, etc.).
|
|
882
|
-
* Typed clients are injected into the handler via the `deps` argument.
|
|
883
|
-
*/
|
|
884
|
-
deps?: D;
|
|
885
|
-
/**
|
|
886
|
-
* SSM Parameter Store parameters.
|
|
887
|
-
* Declare with `param()` helper. Values are fetched and cached at cold start.
|
|
888
|
-
* Typed values are injected into the handler via the `params` argument.
|
|
889
|
-
*/
|
|
890
|
-
params?: P;
|
|
891
|
-
/**
|
|
892
|
-
* Static file glob patterns to bundle into the Lambda ZIP.
|
|
893
|
-
* Files are accessible at runtime via the `readStatic` callback argument.
|
|
894
|
-
*/
|
|
895
|
-
static?: S;
|
|
896
|
-
/** HTTP request handler function */
|
|
897
|
-
onRequest: HttpHandlerFn<T, C, D, P, S>;
|
|
898
|
-
};
|
|
899
|
-
/**
|
|
900
|
-
* Internal handler object created by defineHttp
|
|
901
|
-
* @internal
|
|
902
|
-
*/
|
|
903
|
-
type HttpHandler<T = undefined, C = undefined, D = undefined, P = undefined, S extends string[] | undefined = undefined> = {
|
|
904
|
-
readonly __brand: "effortless-http";
|
|
905
|
-
readonly config: HttpConfig;
|
|
906
|
-
readonly schema?: (input: unknown) => T;
|
|
907
|
-
readonly onError?: (error: unknown, req: HttpRequest) => HttpResponse;
|
|
908
|
-
readonly context?: (...args: any[]) => C | Promise<C>;
|
|
909
|
-
readonly deps?: D;
|
|
910
|
-
readonly params?: P;
|
|
911
|
-
readonly static?: string[];
|
|
912
|
-
readonly onRequest: HttpHandlerFn<T, C, D, P, S>;
|
|
913
|
-
};
|
|
914
|
-
/**
|
|
915
|
-
* Define an HTTP endpoint that creates an API Gateway route + Lambda function
|
|
916
|
-
*
|
|
917
|
-
* @typeParam T - Type of the validated request body (inferred from schema function)
|
|
918
|
-
* @typeParam C - Type of the context/dependencies (inferred from context function)
|
|
919
|
-
* @typeParam D - Type of the deps (from deps declaration)
|
|
920
|
-
* @typeParam P - Type of the params (from params declaration)
|
|
921
|
-
* @param options - Configuration, optional schema, optional context factory, and request handler
|
|
922
|
-
* @returns Handler object used by the deployment system
|
|
923
|
-
*
|
|
924
|
-
* @example Basic GET endpoint
|
|
925
|
-
* ```typescript
|
|
926
|
-
* export const hello = defineHttp({
|
|
927
|
-
* method: "GET",
|
|
928
|
-
* path: "/hello",
|
|
929
|
-
* onRequest: async ({ req }) => ({
|
|
930
|
-
* status: 200,
|
|
931
|
-
* body: { message: "Hello World!" }
|
|
932
|
-
* })
|
|
933
|
-
* });
|
|
934
|
-
* ```
|
|
935
|
-
*
|
|
936
|
-
* @example With SSM parameters
|
|
937
|
-
* ```typescript
|
|
938
|
-
* import { param } from "effortless-aws";
|
|
939
|
-
*
|
|
940
|
-
* export const api = defineHttp({
|
|
941
|
-
* method: "GET",
|
|
942
|
-
* path: "/orders",
|
|
943
|
-
* params: {
|
|
944
|
-
* dbUrl: param("database-url"),
|
|
945
|
-
* },
|
|
946
|
-
* context: async ({ params }) => ({
|
|
947
|
-
* pool: createPool(params.dbUrl),
|
|
948
|
-
* }),
|
|
949
|
-
* onRequest: async ({ req, ctx, params }) => ({
|
|
950
|
-
* status: 200,
|
|
951
|
-
* body: { dbUrl: params.dbUrl }
|
|
952
|
-
* })
|
|
953
|
-
* });
|
|
954
|
-
* ```
|
|
955
|
-
*/
|
|
956
|
-
declare const defineHttp: <T = undefined, C = undefined, D extends Record<string, AnyTableHandler> | undefined = undefined, P extends Record<string, AnyParamRef> | undefined = undefined, S extends string[] | undefined = undefined>(options: DefineHttpOptions<T, C, D, P, S>) => HttpHandler<T, C, D, P, S>;
|
|
963
|
+
declare const defineFifoQueue: <T = unknown, C = undefined, D extends Record<string, AnyTableHandler> | undefined = undefined, P extends Record<string, AnyParamRef> | undefined = undefined, S extends string[] | undefined = undefined>(options: DefineFifoQueueOptions<T, C, D, P, S>) => FifoQueueHandler<T, C, D, P, S>;
|
|
957
964
|
|
|
958
|
-
export { type AppConfig, type AppHandler, type ContentType, type DefineFifoQueueOptions, type DefineHttpOptions, type DefineTableOptions, type EffortlessConfig, type FailedRecord, type FifoQueueBatchFn, type FifoQueueConfig, type FifoQueueHandler, type FifoQueueMessage, type FifoQueueMessageFn, type HttpConfig, type HttpHandler, type HttpHandlerFn, type HttpMethod, type HttpRequest, type HttpResponse, type KeyType, type LambdaConfig, type LambdaWithPermissions, type LogLevel, type ParamRef, type QueryParams, type
|
|
965
|
+
export { type AppConfig, type AppHandler, type ContentType, type DefineFifoQueueOptions, type DefineHttpOptions, type DefineTableOptions, type EffortlessConfig, type FailedRecord, type FifoQueueBatchFn, type FifoQueueConfig, type FifoQueueHandler, type FifoQueueMessage, type FifoQueueMessageFn, type HttpConfig, type HttpHandler, type HttpHandlerFn, type HttpMethod, type HttpRequest, type HttpResponse, type KeyType, type LambdaConfig, type LambdaWithPermissions, type LogLevel, type ParamRef, type QueryParams, type ResolveConfig, type ResolveDeps$1 as ResolveDeps, type StaticSiteConfig, type StaticSiteHandler, type StreamView, type TableBatchCompleteFn, type TableBatchFn, type TableClient, type TableConfig, type TableHandler, type TableKey, type TableRecord, type TableRecordFn, defineApp, defineConfig, defineFifoQueue, defineHttp, defineStaticSite, defineTable, param, typed };
|