lambder 3.6.1 → 3.7.1

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 CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Lambder is a highly opinionated dynamic serverless framework designed to facilitate the management and implementation of routes and APIs within AWS Lambda functions, specifically tailored for TypeScript projects. It provides a streamlined approach to handling HTTP requests, managing sessions, and defining API routes, making serverless application development more intuitive and structured.
4
4
 
5
- **New in v3:** Public file serving with `servePublicFiles()` + `serveIndexHtml()`, unified `addAction()` for non-HTTP triggers, automatic gzip + ETag, thrown responses with a real `die`, the comment-based `LambderTemplatingEngine`, type-safe `html`/`xml` tagged templates, API Gateway HTTP API (payload v2) / Lambda Function URL support, the `LambderDdbCache` DynamoDB cache (3.1), typed translations with `createLambderI18n` (3.2), and in 3.5: typed API refusals with `LambderApiError`, caller outcomes/timeouts with `apiOutcome()`, plus declarative per-API rate limits, guards, and idempotency.
5
+ **New in v3:** Public file serving with `servePublicFiles()` + `serveIndexHtml()`, unified `addAction()` for non-HTTP triggers, automatic gzip + ETag, thrown responses with a real `die`, the comment-based `LambderTemplatingEngine`, type-safe `html`/`xml` tagged templates, API Gateway HTTP API (payload v2) / Lambda Function URL support, the `LambderDdbCache` DynamoDB cache (3.1), typed translations with `createLambderI18n` (3.2), and in 3.5: typed API refusals with `LambderApiError`, caller outcomes/timeouts with `apiOutcome()`, plus declarative per-API rate limits, guards, and idempotency; 3.7 makes guards and custom rate-limit keys payload-sliced ({ input, handler }): the slice is validated pre-run, typed in the handler, and force-merged into the contract input.
6
6
 
7
7
  ## Features
8
8
 
@@ -466,7 +466,7 @@ Related: when an API call crashes with no `setGlobalErrorHandler` (or the handle
466
466
  Declare named building blocks once; reference them from API definitions with full type inference (unknown names are compile errors, and everything is re-asserted at registration time for plain-JS safety). Each piece is independent and optional.
467
467
 
468
468
  ```typescript
469
- import Lambder, { LambderDdbRateLimiter, LambderDdbIdempotency, LambderApiError } from "lambder";
469
+ import Lambder, { LambderDdbRateLimiter, LambderDdbIdempotency, lambderGuard, lambderRateLimitKey, refuse } from "lambder";
470
470
 
471
471
  const lambder = new Lambder<SessionData>({ apiPath: "/api" })
472
472
  // 1. Rate limiting: your limiter instance + named policies. Each policy
@@ -476,8 +476,17 @@ const lambder = new Lambder<SessionData>({ apiPath: "/api" })
476
476
  policies: {
477
477
  authPerIp: { perMin: 5, perHour: 30, per: "ip" },
478
478
  writePerUser: { perMin: 30, per: "session" }, // only referable from addSessionApi (also enforced at compile time)
479
- codePerEmail: { perMin: 3, per: (ctx) => String(ctx.post?.payload?.email ?? "").toLowerCase(),
480
- errorMessage: { type: "warning", content: "Too many attempts for this address." } },
479
+ codePerEmail: {
480
+ perMin: 3,
481
+ // The key declares the payload slice it needs: validated before
482
+ // it runs, handed to the handler typed, and merged into the
483
+ // contract input of every API referencing this policy.
484
+ per: lambderRateLimitKey({
485
+ input: z.object({ email: z.string() }),
486
+ handler: (_ctx, { email }) => email.trim().toLowerCase(),
487
+ }),
488
+ errorMessage: { type: "warning", content: "Too many attempts for this address." },
489
+ },
481
490
  },
482
491
  })
483
492
  // 2. Idempotency: a store instance + replay defaults. May share the rate
@@ -487,18 +496,25 @@ const lambder = new Lambder<SessionData>({ apiPath: "/api" })
487
496
  defaultTtlSeconds: 24 * 3600,
488
497
  failOpen: true, // DynamoDB down => execute without dedupe instead of failing
489
498
  })
490
- // 3. Named guards: run before input validation, refuse by throwing.
491
- // Callable multiple times; domain modules can contribute their own.
499
+ // 3. Named guards: { input?, handler } definitions run before input
500
+ // validation and refuse by throwing. A guard's `input` slice is
501
+ // validated against the raw payload, handed to the handler typed, and
502
+ // merged into the contract input of every API declaring the guard, so
503
+ // forgetting to send captchaToken is a compile error at the call site.
492
504
  .defineApiGuards({
493
- captcha: async (ctx) => {
494
- if (!await verifyCaptcha(ctx.post?.payload?.captchaToken, ctx.ip)) {
495
- throw new LambderApiError("Captcha failed", { errorMessage: "Verification failed, please retry." });
496
- }
497
- },
505
+ captcha: lambderGuard({
506
+ input: z.object({ captchaToken: z.string() }),
507
+ handler: async (ctx, { captchaToken }) => {
508
+ if (!await verifyCaptcha(captchaToken, ctx.ip)) refuse("Verification failed, please retry.");
509
+ },
510
+ }),
498
511
  });
499
512
 
500
513
  lambder.addApi("public.resetPassword", {
501
- input: z.object({ email: z.string().email(), captchaToken: z.string() }),
514
+ // captchaToken is NOT declared here: the guard contributes it to the
515
+ // contract, the guard validates and consumes it, and the handler never
516
+ // sees it.
517
+ input: z.object({ email: z.string().email() }),
502
518
  output: z.object({ ok: z.boolean() }),
503
519
  rateLimit: ["authPerIp", "codePerEmail"], // stacked: checked in order, first exceeded refuses (429 envelope)
504
520
  guards: "captcha",
package/dist/Lambder.d.ts CHANGED
@@ -8,7 +8,7 @@ import { type LambderCorsConfig } from "./LambderCors.js";
8
8
  import { type LambderSessionDataRefreshConfig } from "./LambderSessionManager.js";
9
9
  import LambderSessionController, { type LambderSessionCookieOptions } from "./LambderSessionController.js";
10
10
  import { type LambderPublicFilesOptions } from "./LambderPublicFiles.js";
11
- import { type LambderApiRateLimitPolicyConfig, type LambderApiRateLimitsConfig, type LambderApiIdempotencyConfig, type LambderApiGuardFunction, type LambderApiRegistrationOptions, type LambderPublicRateLimitNames } from "./LambderApiPolicies.js";
11
+ import { type LambderApiRateLimitPolicyConfig, type LambderApiRateLimitsConfig, type LambderApiIdempotencyConfig, type LambderApiGuard, type LambderGuardPayloadMap, type LambderGuardsRequirement, type LambderPoliciesRequirement, type LambderMergedInput, type LambderPublicRateLimitNames } from "./LambderApiPolicies.js";
12
12
  import type { MergeContract } from "./LambderApiContract.js";
13
13
  import { type LambderHttpEvent, type LambderRenderContext, type LambderSessionRenderContext } from "./LambderContext.js";
14
14
  export type { PathParamsOf, RouteCondition, ConditionFunction, LambderRouteMatcher } from "./LambderRouting.js";
@@ -81,7 +81,7 @@ export type LambderConstructorOptions = {
81
81
  * @typeParam TSessionData - Type of session data stored in DynamoDB
82
82
  * @typeParam _TContract - @internal Accumulates API contract during chaining (do not pass manually)
83
83
  * @typeParam _TRateLimitPolicies - @internal Accumulated by enableApiRateLimits (do not pass manually)
84
- * @typeParam _TGuardName - @internal Accumulated by defineApiGuards (do not pass manually)
84
+ * @typeParam _TGuards - @internal Guard name to required-payload map, accumulated by defineApiGuards (do not pass manually)
85
85
  * @typeParam _TIdempotencyEnabled - @internal Flipped by enableApiIdempotency (do not pass manually)
86
86
  *
87
87
  * @example
@@ -93,7 +93,7 @@ export type LambderConstructorOptions = {
93
93
  * .addApi('createUser', { input: z.object({...}), output: z.object({...}) }, handler);
94
94
  * ```
95
95
  */
96
- export default class Lambder<TSessionData = any, _TContract extends Record<string, any> = {}, _TRateLimitPolicies extends Record<string, LambderApiRateLimitPolicyConfig> = {}, _TGuardName extends string = never, _TIdempotencyEnabled extends boolean = false> {
96
+ export default class Lambder<TSessionData = any, _TContract extends Record<string, any> = {}, _TRateLimitPolicies extends Record<string, LambderApiRateLimitPolicyConfig> = {}, _TGuards extends Record<string, any> = {}, _TIdempotencyEnabled extends boolean = false> {
97
97
  apiPath: string;
98
98
  apiVersion: null | string;
99
99
  publicPath: string;
@@ -188,7 +188,7 @@ export default class Lambder<TSessionData = any, _TContract extends Record<strin
188
188
  * policies keyed per "session" are only referable from addSessionApi.
189
189
  * Callable once; call it before the API registrations that use it.
190
190
  */
191
- enableApiRateLimits<const TPolicies extends Record<string, LambderApiRateLimitPolicyConfig>>(config: LambderApiRateLimitsConfig<TPolicies>): Lambder<TSessionData, _TContract, TPolicies, _TGuardName, _TIdempotencyEnabled>;
191
+ enableApiRateLimits<const TPolicies extends Record<string, LambderApiRateLimitPolicyConfig>>(config: LambderApiRateLimitsConfig<TPolicies>): Lambder<TSessionData, _TContract, TPolicies, _TGuards, _TIdempotencyEnabled>;
192
192
  /**
193
193
  * Wire declarative idempotency: your LambderDdbIdempotency instance plus
194
194
  * replay defaults. APIs opt in via `idempotency: true | { ttlSeconds }`;
@@ -198,15 +198,19 @@ export default class Lambder<TSessionData = any, _TContract extends Record<strin
198
198
  * 409, replays of a completed request return the stored response, and a
199
199
  * crashed original releases its claim. Callable once.
200
200
  */
201
- enableApiIdempotency(config: LambderApiIdempotencyConfig): Lambder<TSessionData, _TContract, _TRateLimitPolicies, _TGuardName, true>;
201
+ enableApiIdempotency(config: LambderApiIdempotencyConfig): Lambder<TSessionData, _TContract, _TRateLimitPolicies, _TGuards, true>;
202
202
  /**
203
203
  * Define named guards that APIs reference (typed) via the `guards`
204
- * option. Guards run before input validation, in the order the API
205
- * declares them; a guard refuses by throwing (typically LambderApiError).
204
+ * option. Each guard is a { input?, handler } definition (build with
205
+ * lambderGuard()): the input slice is validated against the raw payload
206
+ * before the handler runs, the handler receives it typed, and the
207
+ * requirement merges into the contract input of every API declaring the
208
+ * guard. Guards run before input validation, in the order the API
209
+ * declares them; a handler refuses by throwing (typically refuse()).
206
210
  * Callable multiple times so domain modules can contribute their own;
207
211
  * names must not collide.
208
212
  */
209
- defineApiGuards<TGuards extends Record<string, LambderApiGuardFunction>>(guards: TGuards): Lambder<TSessionData, _TContract, _TRateLimitPolicies, _TGuardName | Extract<keyof TGuards, string>, _TIdempotencyEnabled>;
213
+ defineApiGuards<TGuards extends Record<string, LambderApiGuard<any>>>(guards: TGuards): Lambder<TSessionData, _TContract, _TRateLimitPolicies, _TGuards & LambderGuardPayloadMap<TGuards>, _TIdempotencyEnabled>;
210
214
  private getOrCreatePolicyEngine;
211
215
  /** Registration-time checks shared by addApi/addSessionApi. */
212
216
  private assertApiRegistration;
@@ -214,15 +218,33 @@ export default class Lambder<TSessionData = any, _TContract extends Record<strin
214
218
  addRoute(condition: RegExp | ConditionFunction | LambderRouteMatcher, actionFn: ActionFunction): this;
215
219
  addSessionRoute<TPath extends Path>(condition: TPath, actionFn: (ctx: LambderSessionRenderContext<any, TSessionData, PathParamsOf<TPath>>, resolver: LambderResolver) => MaybePromise<LambderResponse>): this;
216
220
  addSessionRoute(condition: RegExp | ConditionFunction | LambderRouteMatcher, actionFn: SessionActionFunction<TSessionData>): this;
217
- use<_TNewContract extends Record<string, any>>(plugin: (lambder: Lambder<TSessionData, _TContract, any, any, any>) => Lambder<TSessionData, _TNewContract, any, any, any>): Lambder<TSessionData, _TNewContract extends _TContract ? _TNewContract : (_TContract & _TNewContract), _TRateLimitPolicies, _TGuardName, _TIdempotencyEnabled>;
218
- addApi<TName extends string, TInput extends z.ZodTypeAny, TOutput extends z.ZodTypeAny>(name: TName, schema: {
221
+ use<_TNewContract extends Record<string, any>>(plugin: (lambder: Lambder<TSessionData, _TContract, any, any, any>) => Lambder<TSessionData, _TNewContract, any, any, any>): Lambder<TSessionData, _TNewContract extends _TContract ? _TNewContract : (_TContract & _TNewContract), _TRateLimitPolicies, _TGuards, _TIdempotencyEnabled>;
222
+ addApi<TName extends string, TInput extends z.ZodTypeAny, TOutput extends z.ZodTypeAny, const TRateOpt extends LambderPublicRateLimitNames<_TRateLimitPolicies> | readonly LambderPublicRateLimitNames<_TRateLimitPolicies>[] = never, const TGuardsOpt extends Extract<keyof _TGuards, string> | readonly Extract<keyof _TGuards, string>[] = never>(name: TName, schema: {
219
223
  input: TInput;
220
224
  output: TOutput;
221
- } & LambderApiRegistrationOptions<LambderPublicRateLimitNames<_TRateLimitPolicies>, _TGuardName, _TIdempotencyEnabled>, handler: (ctx: LambderRenderContext<z.infer<TInput>>, resolver: LambderResolver<z.infer<TOutput>>) => MaybePromise<LambderResponse>): Lambder<TSessionData, MergeContract<_TContract, TName, z.infer<TInput>, z.infer<TOutput>>, _TRateLimitPolicies, _TGuardName, _TIdempotencyEnabled>;
222
- addSessionApi<TName extends string, TInput extends z.ZodTypeAny, TOutput extends z.ZodTypeAny>(name: TName, schema: {
225
+ } & {
226
+ /** Named rate limits, checked in declared order before guards and validation; the first exceeded one refuses (429 envelope). */
227
+ rateLimit?: TRateOpt;
228
+ /** Named guards, run in declared order before input validation; their input requirements merge into this API's contract input. */
229
+ guards?: TGuardsOpt;
230
+ /** Replay-protect this API per client idempotencyKey. Requires enableApiIdempotency() first. */
231
+ idempotency?: _TIdempotencyEnabled extends true ? (boolean | {
232
+ ttlSeconds?: number;
233
+ }) : never;
234
+ }, handler: (ctx: LambderRenderContext<z.infer<TInput>>, resolver: LambderResolver<z.infer<TOutput>>) => MaybePromise<LambderResponse>): Lambder<TSessionData, MergeContract<_TContract, TName, LambderMergedInput<z.infer<TInput>, LambderPoliciesRequirement<_TRateLimitPolicies, TRateOpt>, LambderGuardsRequirement<_TGuards, TGuardsOpt>>, z.infer<TOutput>>, _TRateLimitPolicies, _TGuards, _TIdempotencyEnabled>;
235
+ addSessionApi<TName extends string, TInput extends z.ZodTypeAny, TOutput extends z.ZodTypeAny, const TRateOpt extends Extract<keyof _TRateLimitPolicies, string> | readonly Extract<keyof _TRateLimitPolicies, string>[] = never, const TGuardsOpt extends Extract<keyof _TGuards, string> | readonly Extract<keyof _TGuards, string>[] = never>(name: TName, schema: {
223
236
  input: TInput;
224
237
  output: TOutput;
225
- } & LambderApiRegistrationOptions<Extract<keyof _TRateLimitPolicies, string>, _TGuardName, _TIdempotencyEnabled>, handler: (ctx: LambderSessionRenderContext<z.infer<TInput>, TSessionData>, resolver: LambderResolver<z.infer<TOutput>>) => MaybePromise<LambderResponse>): Lambder<TSessionData, MergeContract<_TContract, TName, z.infer<TInput>, z.infer<TOutput>>, _TRateLimitPolicies, _TGuardName, _TIdempotencyEnabled>;
238
+ } & {
239
+ /** Named rate limits, checked in declared order before guards and validation; the first exceeded one refuses (429 envelope). */
240
+ rateLimit?: TRateOpt;
241
+ /** Named guards, run in declared order before input validation; their input requirements merge into this API's contract input. */
242
+ guards?: TGuardsOpt;
243
+ /** Replay-protect this API per client idempotencyKey. Requires enableApiIdempotency() first. */
244
+ idempotency?: _TIdempotencyEnabled extends true ? (boolean | {
245
+ ttlSeconds?: number;
246
+ }) : never;
247
+ }, handler: (ctx: LambderSessionRenderContext<z.infer<TInput>, TSessionData>, resolver: LambderResolver<z.infer<TOutput>>) => MaybePromise<LambderResponse>): Lambder<TSessionData, MergeContract<_TContract, TName, LambderMergedInput<z.infer<TInput>, LambderPoliciesRequirement<_TRateLimitPolicies, TRateOpt>, LambderGuardsRequirement<_TGuards, TGuardsOpt>>, z.infer<TOutput>>, _TRateLimitPolicies, _TGuards, _TIdempotencyEnabled>;
226
248
  /**
227
249
  * Fetch the session or short-circuit the request: API calls get the
228
250
  * protocol's { sessionExpired: true } response (handled by LambderCaller),
package/dist/Lambder.js CHANGED
@@ -15,7 +15,7 @@ import { createContext, isV2HttpEvent } from "./LambderContext.js";
15
15
  * @typeParam TSessionData - Type of session data stored in DynamoDB
16
16
  * @typeParam _TContract - @internal Accumulates API contract during chaining (do not pass manually)
17
17
  * @typeParam _TRateLimitPolicies - @internal Accumulated by enableApiRateLimits (do not pass manually)
18
- * @typeParam _TGuardName - @internal Accumulated by defineApiGuards (do not pass manually)
18
+ * @typeParam _TGuards - @internal Guard name to required-payload map, accumulated by defineApiGuards (do not pass manually)
19
19
  * @typeParam _TIdempotencyEnabled - @internal Flipped by enableApiIdempotency (do not pass manually)
20
20
  *
21
21
  * @example
@@ -193,8 +193,12 @@ export default class Lambder {
193
193
  }
194
194
  /**
195
195
  * Define named guards that APIs reference (typed) via the `guards`
196
- * option. Guards run before input validation, in the order the API
197
- * declares them; a guard refuses by throwing (typically LambderApiError).
196
+ * option. Each guard is a { input?, handler } definition (build with
197
+ * lambderGuard()): the input slice is validated against the raw payload
198
+ * before the handler runs, the handler receives it typed, and the
199
+ * requirement merges into the contract input of every API declaring the
200
+ * guard. Guards run before input validation, in the order the API
201
+ * declares them; a handler refuses by throwing (typically refuse()).
198
202
  * Callable multiple times so domain modules can contribute their own;
199
203
  * names must not collide.
200
204
  */
@@ -1,14 +1,44 @@
1
+ import type { z } from "zod";
1
2
  import type { LambderRenderContext } from "./LambderContext.js";
2
3
  import type LambderResolver from "./LambderResolver.js";
3
4
  import type { LambderRateLimitPolicy, LambderDdbRateLimiter } from "./LambderDdbRateLimiter.js";
4
5
  import type { LambderDdbIdempotency } from "./LambderDdbIdempotency.js";
5
6
  import { LambderResponse } from "./LambderResponse.js";
6
7
  /**
7
- * What one rate-limit counter tracks: the client IP, the session identity, or
8
- * a custom key derived from the request (e.g. a normalized email). Custom
9
- * functions run before input validation, so they read the raw payload.
8
+ * A custom rate-limit key: `input` names the payload fields the key needs.
9
+ * The slice is validated against the raw payload before `handler` runs (a
10
+ * failure answers the standard 422 validation shape), and the requirement is
11
+ * merged into the contract input of every API that references the policy, so
12
+ * clients are forced by the compiler to send those fields. Build with
13
+ * lambderRateLimitKey() so the handler's payload type follows `input`.
10
14
  */
11
- export type LambderRateLimitPer = "ip" | "session" | ((ctx: LambderRenderContext) => string | Promise<string>);
15
+ export type LambderRateLimitKeyFn<TInput extends z.ZodTypeAny = z.ZodTypeAny> = {
16
+ input: TInput;
17
+ handler: (ctx: LambderRenderContext, payload: z.output<TInput>) => string | Promise<string>;
18
+ } | {
19
+ input?: undefined;
20
+ handler: (ctx: LambderRenderContext, payload: undefined) => string | Promise<string>;
21
+ };
22
+ /**
23
+ * Builder that ties the handler's payload type to the `input` schema inside
24
+ * one literal. Returns the exact union member (not the union), so requirement
25
+ * extraction can see the `input` type.
26
+ */
27
+ export declare function lambderRateLimitKey<TInput extends z.ZodTypeAny>(key: {
28
+ input: TInput;
29
+ handler: (ctx: LambderRenderContext, payload: z.output<TInput>) => string | Promise<string>;
30
+ }): {
31
+ input: TInput;
32
+ handler: (ctx: LambderRenderContext, payload: z.output<TInput>) => string | Promise<string>;
33
+ };
34
+ export declare function lambderRateLimitKey(key: {
35
+ handler: (ctx: LambderRenderContext, payload: undefined) => string | Promise<string>;
36
+ }): {
37
+ input?: undefined;
38
+ handler: (ctx: LambderRenderContext, payload: undefined) => string | Promise<string>;
39
+ };
40
+ /** What one rate-limit counter tracks: the client IP, the session identity, or a custom payload-derived key. */
41
+ export type LambderRateLimitPer = "ip" | "session" | LambderRateLimitKeyFn<any>;
12
42
  /** A named rate-limit policy: fixed windows plus the key one counter tracks. */
13
43
  export type LambderApiRateLimitPolicyConfig = LambderRateLimitPolicy & {
14
44
  per: LambderRateLimitPer;
@@ -30,28 +60,72 @@ export type LambderApiIdempotencyConfig = {
30
60
  failOpen?: boolean;
31
61
  };
32
62
  /**
33
- * A named guard, run before input validation. Refuse by throwing (typically a
34
- * LambderApiError, or res.die.*); return normally to let the request through.
35
- * ctx.apiPayload is unvalidated at this point.
63
+ * A named guard, run before the API's own input validation. `input` names the
64
+ * payload fields the guard requires: the slice is validated against the raw
65
+ * payload before `handler` runs (a failure answers the standard 422
66
+ * validation shape), the handler receives it typed, and the requirement is
67
+ * merged into the contract input of every API that declares the guard, so
68
+ * clients are forced by the compiler to send those fields. The handler
69
+ * refuses by throwing (typically refuse()/LambderApiError). Build with
70
+ * lambderGuard() so the handler's payload type follows `input`.
36
71
  */
37
- export type LambderApiGuardFunction = (ctx: LambderRenderContext, res: LambderResolver) => void | Promise<void>;
72
+ export type LambderApiGuard<TInput extends z.ZodTypeAny = z.ZodTypeAny> = {
73
+ input: TInput;
74
+ handler: (ctx: LambderRenderContext, payload: z.output<TInput>, res: LambderResolver) => void | Promise<void>;
75
+ } | {
76
+ input?: undefined;
77
+ handler: (ctx: LambderRenderContext, payload: undefined, res: LambderResolver) => void | Promise<void>;
78
+ };
79
+ /**
80
+ * Builder that ties the handler's payload type to the `input` schema inside
81
+ * one literal. Returns the exact union member (not the union), so requirement
82
+ * extraction can see the `input` type.
83
+ */
84
+ export declare function lambderGuard<TInput extends z.ZodTypeAny>(guard: {
85
+ input: TInput;
86
+ handler: (ctx: LambderRenderContext, payload: z.output<TInput>, res: LambderResolver) => void | Promise<void>;
87
+ }): {
88
+ input: TInput;
89
+ handler: (ctx: LambderRenderContext, payload: z.output<TInput>, res: LambderResolver) => void | Promise<void>;
90
+ };
91
+ export declare function lambderGuard(guard: {
92
+ handler: (ctx: LambderRenderContext, payload: undefined, res: LambderResolver) => void | Promise<void>;
93
+ }): {
94
+ input?: undefined;
95
+ handler: (ctx: LambderRenderContext, payload: undefined, res: LambderResolver) => void | Promise<void>;
96
+ };
38
97
  /** Names of policies usable on public APIs: everything not keyed per "session". */
39
98
  export type LambderPublicRateLimitNames<TPolicies> = {
40
99
  [K in keyof TPolicies]: TPolicies[K] extends {
41
100
  per: "session";
42
101
  } ? never : K;
43
102
  }[keyof TPolicies] & string;
44
- /** Declarative per-API options carried in the addApi/addSessionApi schema object. */
45
- export type LambderApiRegistrationOptions<TRateLimitName extends string, TGuardName extends string, TIdempotencyEnabled extends boolean> = {
46
- /** Named rate limits, checked in declared order before guards and validation; the first exceeded one refuses (429 envelope). */
47
- rateLimit?: TRateLimitName | readonly TRateLimitName[];
48
- /** Named guards, run in declared order before input validation; refuse by throwing. */
49
- guards?: TGuardName | readonly TGuardName[];
50
- /** Replay-protect this API per client idempotencyKey. Requires enableApiIdempotency() first. */
51
- idempotency?: TIdempotencyEnabled extends true ? (boolean | {
52
- ttlSeconds?: number;
53
- }) : never;
103
+ /** Payload fields a guard requires; {} when it declares no input. */
104
+ export type LambderGuardPayload<G> = G extends {
105
+ input: infer S extends z.ZodTypeAny;
106
+ } ? z.output<S> : {};
107
+ /** Guard name to required-payload map, accumulated on the Lambder instance by defineApiGuards. */
108
+ export type LambderGuardPayloadMap<TGuards> = {
109
+ [K in keyof TGuards]: LambderGuardPayload<TGuards[K]>;
54
110
  };
111
+ /** Payload fields a policy's custom key requires; {} for "ip"/"session" or keys with no input. */
112
+ export type LambderPolicyPayload<P> = P extends {
113
+ per: {
114
+ input: infer S extends z.ZodTypeAny;
115
+ };
116
+ } ? z.output<S> : {};
117
+ type UnionToIntersection<U> = (U extends any ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
118
+ type NamesIn<TOpt> = TOpt extends readonly (infer N extends string)[] ? N : TOpt extends string ? TOpt : never;
119
+ /** Intersection of the payload requirements of the referenced guards; never when none are declared. */
120
+ export type LambderGuardsRequirement<TGuardPayloads, TOpt> = [
121
+ NamesIn<TOpt>
122
+ ] extends [never] ? never : UnionToIntersection<TGuardPayloads[Extract<NamesIn<TOpt>, keyof TGuardPayloads>]>;
123
+ /** Intersection of the payload requirements of the referenced rate-limit policies; never when none are declared. */
124
+ export type LambderPoliciesRequirement<TPolicies, TOpt> = [
125
+ NamesIn<TOpt>
126
+ ] extends [never] ? never : UnionToIntersection<LambderPolicyPayload<TPolicies[Extract<NamesIn<TOpt>, keyof TPolicies>]>>;
127
+ /** Contract-input merge: the API's own input plus everything its rate limits and guards force clients to send. */
128
+ export type LambderMergedInput<TIn, TReqA, TReqB> = ([TReqA] extends [never] ? TIn : TIn & TReqA) extends infer TMid ? ([TReqB] extends [never] ? TMid : TMid & TReqB) : never;
55
129
  /**
56
130
  * Runtime side of the declarative API options: holds what the enable/define
57
131
  * calls declared, asserts registrations against it at startup, and executes
@@ -67,7 +141,7 @@ export declare class LambderApiPolicyEngine {
67
141
  private idempotencyDefaultTtlSeconds;
68
142
  private idempotencyFailOpen;
69
143
  setRateLimits(config: LambderApiRateLimitsConfig<Record<string, LambderApiRateLimitPolicyConfig>>): void;
70
- addGuards(guards: Record<string, LambderApiGuardFunction>): void;
144
+ addGuards(guards: Record<string, LambderApiGuard<any>>): void;
71
145
  setIdempotency(config: LambderApiIdempotencyConfig): void;
72
146
  /** Startup validation of one API registration's declarative options. */
73
147
  assertRegistration(apiName: string, mode: "public" | "session", options: {
@@ -80,6 +154,13 @@ export declare class LambderApiPolicyEngine {
80
154
  rateLimit?: string | readonly string[];
81
155
  guards?: string | readonly string[];
82
156
  }): Promise<void>;
157
+ /**
158
+ * Validate a preflight input slice against the raw payload. Runs before
159
+ * the API's own validation, so guard/key requirements hold even when the
160
+ * API schema does not declare (and would strip) those fields. Failures
161
+ * answer the same 422 shape as regular input validation.
162
+ */
163
+ private parseSlice;
83
164
  private resolveRateLimitKey;
84
165
  /**
85
166
  * Idempotency wrapper around validation-passed handler execution. Without
@@ -93,3 +174,4 @@ export declare class LambderApiPolicyEngine {
93
174
  ttlSeconds?: number;
94
175
  }, exec: () => Promise<LambderResponse>): Promise<LambderResponse>;
95
176
  }
177
+ export {};
@@ -5,6 +5,8 @@ const IDEMPOTENCY_PENDING_TTL_SECONDS = 300;
5
5
  /** Responses above this size skip replay storage (DynamoDB item limit is 400KB). */
6
6
  const IDEMPOTENCY_MAX_STORED_BODY_BYTES = 350_000;
7
7
  const RATE_LIMIT_WINDOW_KEYS = ["perMin", "per10Min", "perHour", "perDay", "perWeek", "perMonth"];
8
+ export function lambderRateLimitKey(key) { return key; }
9
+ export function lambderGuard(guard) { return guard; }
8
10
  const toList = (value) => value === undefined ? [] : typeof value === "string" ? [value] : value;
9
11
  /**
10
12
  * Runtime side of the declarative API options: holds what the enable/define
@@ -24,8 +26,10 @@ export class LambderApiPolicyEngine {
24
26
  if (this.limiter)
25
27
  throw new Error("Lambder: enableApiRateLimits() was already called.");
26
28
  for (const [name, policy] of Object.entries(config.policies)) {
27
- if (!policy.per)
28
- throw new Error(`Lambder: rate-limit policy "${name}" is missing its "per" key source.`);
29
+ const per = policy.per;
30
+ if (!per || (per !== "ip" && per !== "session" && typeof per.handler !== "function")) {
31
+ throw new Error(`Lambder: rate-limit policy "${name}" needs per: "ip", "session", or a { input?, handler } key.`);
32
+ }
29
33
  if (!RATE_LIMIT_WINDOW_KEYS.some((key) => policy[key])) {
30
34
  throw new Error(`Lambder: rate-limit policy "${name}" declares no window (${RATE_LIMIT_WINDOW_KEYS.join("/")}).`);
31
35
  }
@@ -34,10 +38,12 @@ export class LambderApiPolicyEngine {
34
38
  this.rateLimitPolicies = { ...config.policies };
35
39
  }
36
40
  addGuards(guards) {
37
- for (const [name, guardFn] of Object.entries(guards)) {
41
+ for (const [name, guardDef] of Object.entries(guards)) {
38
42
  if (this.guards[name])
39
43
  throw new Error(`Lambder: guard "${name}" is already defined.`);
40
- this.guards[name] = guardFn;
44
+ if (typeof guardDef?.handler !== "function")
45
+ throw new Error(`Lambder: guard "${name}" has no handler function.`);
46
+ this.guards[name] = guardDef;
41
47
  }
42
48
  }
43
49
  setIdempotency(config) {
@@ -73,7 +79,7 @@ export class LambderApiPolicyEngine {
73
79
  const policy = this.rateLimitPolicies[name];
74
80
  if (!policy || !this.limiter)
75
81
  throw new Error(`Lambder: rate-limit policy "${name}" is not configured.`);
76
- const key = await this.resolveRateLimitKey(ctx, policy.per);
82
+ const key = await this.resolveRateLimitKey(ctx, resolver, policy.per);
77
83
  const limited = await this.limiter.isRateLimited(`api|${apiName}|${name}|${key}`, policy);
78
84
  if (limited) {
79
85
  throw new LambderApiError(`Rate limited: "${apiName}" exceeded policy "${name}".`, {
@@ -83,13 +89,29 @@ export class LambderApiPolicyEngine {
83
89
  }
84
90
  }
85
91
  for (const name of toList(options.guards)) {
86
- const guardFn = this.guards[name];
87
- if (!guardFn)
92
+ const guardDef = this.guards[name];
93
+ if (!guardDef)
88
94
  throw new Error(`Lambder: guard "${name}" is not configured.`);
89
- await guardFn(ctx, resolver);
95
+ const payload = this.parseSlice(guardDef.input, ctx, resolver);
96
+ await guardDef.handler(ctx, payload, resolver);
97
+ }
98
+ }
99
+ /**
100
+ * Validate a preflight input slice against the raw payload. Runs before
101
+ * the API's own validation, so guard/key requirements hold even when the
102
+ * API schema does not declare (and would strip) those fields. Failures
103
+ * answer the same 422 shape as regular input validation.
104
+ */
105
+ parseSlice(input, ctx, resolver) {
106
+ if (!input)
107
+ return undefined;
108
+ const parsed = input.safeParse(ctx.post?.payload);
109
+ if (!parsed.success) {
110
+ throw resolver.json({ error: "Input validation failed", zodError: parsed.error }, { statusCode: 422 });
90
111
  }
112
+ return parsed.data;
91
113
  }
92
- async resolveRateLimitKey(ctx, per) {
114
+ async resolveRateLimitKey(ctx, resolver, per) {
93
115
  if (per === "ip")
94
116
  return `ip:${ctx.ip}`;
95
117
  if (per === "session") {
@@ -98,7 +120,8 @@ export class LambderApiPolicyEngine {
98
120
  throw new Error('Lambder: rate-limit per "session" evaluated without a session on the context.');
99
121
  return `session:${sessionKey}`;
100
122
  }
101
- return `custom:${await per(ctx)}`;
123
+ const payload = this.parseSlice(per.input, ctx, resolver);
124
+ return `custom:${await per.handler(ctx, payload)}`;
102
125
  }
103
126
  /**
104
127
  * Idempotency wrapper around validation-passed handler execution. Without
package/dist/index.d.ts CHANGED
@@ -27,7 +27,8 @@ export { LambderDdbRateLimiter } from "./LambderDdbRateLimiter.js";
27
27
  export type { LambderDdbRateLimiterOptions, LambderRateLimitPolicy, LambderRateLimitExceededMap, LambderRateLimitResult, } from "./LambderDdbRateLimiter.js";
28
28
  export { LambderDdbIdempotency } from "./LambderDdbIdempotency.js";
29
29
  export type { LambderDdbIdempotencyOptions, LambderIdempotencyBeginResult, } from "./LambderDdbIdempotency.js";
30
- export type { LambderApiGuardFunction, LambderRateLimitPer, LambderApiRateLimitPolicyConfig, LambderApiRateLimitsConfig, LambderApiIdempotencyConfig, LambderApiRegistrationOptions, LambderPublicRateLimitNames, } from "./LambderApiPolicies.js";
30
+ export { lambderGuard, lambderRateLimitKey } from "./LambderApiPolicies.js";
31
+ export type { LambderApiGuard, LambderRateLimitKeyFn, LambderRateLimitPer, LambderApiRateLimitPolicyConfig, LambderApiRateLimitsConfig, LambderApiIdempotencyConfig, LambderPublicRateLimitNames, LambderGuardPayload, LambderGuardPayloadMap, LambderPolicyPayload, LambderGuardsRequirement, LambderPoliciesRequirement, LambderMergedInput, } from "./LambderApiPolicies.js";
31
32
  export { createLambderI18n } from "./LambderI18n.js";
32
33
  export type { LambderLanguageMeta, LambderI18nConfig, LambderI18nInstance, LambderI18nTranslator, LambderI18nExtractParams, LambderI18nCodes, LambderI18nKeys, LambderI18nTranslatorFor, } from "./LambderI18n.js";
33
34
  export { type ApiContractShape, } from "./LambderApiContract.js";
package/dist/index.js CHANGED
@@ -23,6 +23,8 @@ export { LambderDdbCache } from "./LambderDdbCache.js";
23
23
  export { LambderDdbRateLimiter } from "./LambderDdbRateLimiter.js";
24
24
  // DynamoDB-backed idempotency records (standalone, server-only)
25
25
  export { LambderDdbIdempotency } from "./LambderDdbIdempotency.js";
26
+ // Declarative per-API policies (rate limits, guards, idempotency)
27
+ export { lambderGuard, lambderRateLimitKey } from "./LambderApiPolicies.js";
26
28
  // Typed translations (standalone, isomorphic)
27
29
  export { createLambderI18n } from "./LambderI18n.js";
28
30
  export { createContext, isV2HttpEvent } from "./LambderContext.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lambder",
3
- "version": "3.6.1",
3
+ "version": "3.7.1",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",