@geekmidas/constructs 9.2.0 → 9.3.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/CHANGELOG.md +62 -0
- package/README.md +1 -1
- package/dist/{AWSScheduledFunction-B0thZRJU.d.cts → AWSScheduledFunction-DAGP_vz5.d.cts} +43 -11
- package/dist/AWSScheduledFunction-DAGP_vz5.d.cts.map +1 -0
- package/dist/{AWSScheduledFunction-a6hRp3nk.d.mts → AWSScheduledFunction-Dg6TPIxn.d.mts} +42 -10
- package/dist/AWSScheduledFunction-Dg6TPIxn.d.mts.map +1 -0
- package/dist/{Endpoint-BmnuNbF1.d.cts → Endpoint-CxBdXNZm.d.cts} +2 -2
- package/dist/{Endpoint-BmnuNbF1.d.cts.map → Endpoint-CxBdXNZm.d.cts.map} +1 -1
- package/dist/Endpoint-DEydLuBs.d.mts.map +1 -1
- package/dist/{HonoEndpointAdaptor-Dd-S7gV_.mjs → HonoEndpointAdaptor-CJfFQ5KR.mjs} +2 -2
- package/dist/{HonoEndpointAdaptor-Dd-S7gV_.mjs.map → HonoEndpointAdaptor-CJfFQ5KR.mjs.map} +1 -1
- package/dist/{HonoEndpointAdaptor-INisd3XZ.cjs → HonoEndpointAdaptor-DCD0rhFg.cjs} +2 -2
- package/dist/{HonoEndpointAdaptor-INisd3XZ.cjs.map → HonoEndpointAdaptor-DCD0rhFg.cjs.map} +1 -1
- package/dist/adaptors/aws.cjs +8 -7
- package/dist/adaptors/aws.cjs.map +1 -1
- package/dist/adaptors/aws.d.cts +3 -3
- package/dist/adaptors/aws.d.cts.map +1 -1
- package/dist/adaptors/aws.d.mts +1 -1
- package/dist/adaptors/aws.d.mts.map +1 -1
- package/dist/adaptors/aws.mjs +9 -8
- package/dist/adaptors/aws.mjs.map +1 -1
- package/dist/adaptors/hono.cjs +2 -2
- package/dist/adaptors/hono.d.cts +2 -2
- package/dist/adaptors/hono.mjs +2 -2
- package/dist/adaptors/testing.cjs +2 -2
- package/dist/adaptors/testing.d.cts +2 -2
- package/dist/adaptors/testing.mjs +2 -2
- package/dist/crons/index.cjs +4 -0
- package/dist/crons/index.cjs.map +1 -1
- package/dist/crons/index.d.cts +8 -4
- package/dist/crons/index.d.cts.map +1 -1
- package/dist/crons/index.d.mts +7 -3
- package/dist/crons/index.d.mts.map +1 -1
- package/dist/crons/index.mjs +4 -0
- package/dist/crons/index.mjs.map +1 -1
- package/dist/endpoints/index.cjs +1 -1
- package/dist/endpoints/index.d.cts +4 -4
- package/dist/endpoints/index.mjs +1 -1
- package/dist/functions/index.d.cts +1 -1
- package/dist/{index-HWHPZv8u.d.cts → index-CeoLMr7c.d.cts} +5 -5
- package/dist/{index-HWHPZv8u.d.cts.map → index-CeoLMr7c.d.cts.map} +1 -1
- package/dist/lazyAccessors-C1n1QfEj.cjs +653 -0
- package/dist/lazyAccessors-C1n1QfEj.cjs.map +1 -0
- package/dist/lazyAccessors-fFIQrblM.mjs +594 -0
- package/dist/lazyAccessors-fFIQrblM.mjs.map +1 -0
- package/dist/monitors/index.cjs +1 -1
- package/dist/monitors/index.cjs.map +1 -1
- package/dist/monitors/index.d.cts +1 -1
- package/dist/monitors/index.d.mts +1 -1
- package/dist/monitors/index.mjs +1 -1
- package/dist/monitors/index.mjs.map +1 -1
- package/package.json +23 -27
- package/src/__tests__/Construct.environment.spec.ts +2 -2
- package/src/__tests__/builder-reuse.spec.ts +2 -2
- package/src/__tests__/monitor.spec.ts +16 -16
- package/src/crons/Cron.ts +187 -60
- package/src/crons/CronBuilder.ts +10 -4
- package/src/crons/__tests__/AWSScheduledFunction.spec.ts +8 -6
- package/src/crons/__tests__/Cron.spec.ts +53 -91
- package/src/crons/__tests__/CronBuilder.state-isolation.spec.ts +28 -28
- package/src/crons/index.ts +2 -1
- package/src/endpoints/AmazonApiGatewayEndpointAdaptor.ts +7 -15
- package/src/endpoints/Endpoint.ts +12 -15
- package/src/monitors/heartbeat.ts +1 -1
- package/tsdown.config.ts +1 -7
- package/dist/AWSScheduledFunction-B0thZRJU.d.cts.map +0 -1
- package/dist/AWSScheduledFunction-a6hRp3nk.d.mts.map +0 -1
- package/dist/lazyAccessors-DzxZVtPR.mjs +0 -1855
- package/dist/lazyAccessors-DzxZVtPR.mjs.map +0 -1
- package/dist/lazyAccessors-iwvg1Pv4.cjs +0 -1920
- package/dist/lazyAccessors-iwvg1Pv4.cjs.map +0 -1
|
@@ -0,0 +1,594 @@
|
|
|
1
|
+
import { ConstructType } from "./Construct-D0uZvbr3.mjs";
|
|
2
|
+
import { Function } from "./functions-DhkDodl0.mjs";
|
|
3
|
+
import { convertSchemaWithComponents, convertStandardSchemaToJsonSchema } from "@geekmidas/schema/conversion";
|
|
4
|
+
import { buildOpenApiSchema } from "@geekmidas/schema/openapi";
|
|
5
|
+
import { getCookie } from "hono/cookie";
|
|
6
|
+
|
|
7
|
+
//#region src/endpoints/Endpoint.ts
|
|
8
|
+
/**
|
|
9
|
+
* Represents an HTTP endpoint that can handle requests with type-safe input/output validation,
|
|
10
|
+
* dependency injection, session management, and authorization.
|
|
11
|
+
*
|
|
12
|
+
* @template TRoute - The route path string with parameter placeholders (e.g., '/users/:id')
|
|
13
|
+
* @template TMethod - The HTTP method (GET, POST, PUT, DELETE, PATCH)
|
|
14
|
+
* @template TInput - The input schema definition for body, query, and path parameters
|
|
15
|
+
* @template OutSchema - The output schema for response validation
|
|
16
|
+
* @template TServices - Array of service dependencies to inject
|
|
17
|
+
* @template TLogger - The logger instance type
|
|
18
|
+
* @template TSession - The session data type
|
|
19
|
+
*
|
|
20
|
+
* @extends Function - Base function construct for handler execution
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* const endpoint = new Endpoint({
|
|
25
|
+
* route: '/users/:id',
|
|
26
|
+
* method: 'GET',
|
|
27
|
+
* input: { params: userIdSchema },
|
|
28
|
+
* output: userSchema,
|
|
29
|
+
* fn: async ({ params }) => getUserById(params.id)
|
|
30
|
+
* });
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
var Endpoint = class Endpoint extends Function {
|
|
34
|
+
operationId;
|
|
35
|
+
/** The route path pattern with parameter placeholders */
|
|
36
|
+
route;
|
|
37
|
+
/** The HTTP method for this endpoint */
|
|
38
|
+
method;
|
|
39
|
+
/** Optional description for OpenAPI documentation */
|
|
40
|
+
description;
|
|
41
|
+
/** Optional tags for OpenAPI documentation */
|
|
42
|
+
tags;
|
|
43
|
+
/** The HTTP success status code to return (default: 200) */
|
|
44
|
+
status;
|
|
45
|
+
/**
|
|
46
|
+
* Response content type for this endpoint. Defaults to `'application/json'`.
|
|
47
|
+
* Non-JSON types cause adaptors to emit the body as-is with the declared
|
|
48
|
+
* Content-Type header instead of JSON-serializing.
|
|
49
|
+
*/
|
|
50
|
+
responseType;
|
|
51
|
+
/** Default headers to apply to all responses */
|
|
52
|
+
defaultHeaders = {};
|
|
53
|
+
/** Function to extract session data from the request context */
|
|
54
|
+
getSession = () => ({});
|
|
55
|
+
/** Function to determine if the request is authorized */
|
|
56
|
+
authorize = () => true;
|
|
57
|
+
/** Optional rate limiting configuration */
|
|
58
|
+
rateLimit;
|
|
59
|
+
/** Optional authorizer for this endpoint */
|
|
60
|
+
authorizer;
|
|
61
|
+
/** Optional actor extractor for audit records */
|
|
62
|
+
actorExtractor;
|
|
63
|
+
/** Declarative audit definitions */
|
|
64
|
+
audits = [];
|
|
65
|
+
/** RLS configuration for this endpoint */
|
|
66
|
+
rlsConfig;
|
|
67
|
+
/** Whether to bypass RLS for this endpoint */
|
|
68
|
+
rlsBypass;
|
|
69
|
+
/** The endpoint handler function */
|
|
70
|
+
endpointFn;
|
|
71
|
+
/**
|
|
72
|
+
* Builds a complete OpenAPI 3.1 schema from an array of endpoints.
|
|
73
|
+
*
|
|
74
|
+
* @param endpoints - Array of endpoint instances to document
|
|
75
|
+
* @param options - Optional configuration for OpenAPI generation
|
|
76
|
+
* @returns OpenAPI 3.1 specification object
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```typescript
|
|
80
|
+
* const schema = await Endpoint.buildOpenApiSchema([
|
|
81
|
+
* getUserEndpoint,
|
|
82
|
+
* createUserEndpoint
|
|
83
|
+
* ], {
|
|
84
|
+
* title: 'User API',
|
|
85
|
+
* version: '1.0.0'
|
|
86
|
+
* });
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
static async buildOpenApiSchema(endpoints, options) {
|
|
90
|
+
return buildOpenApiSchema(endpoints, options);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Gets the full path including HTTP method and route.
|
|
94
|
+
* @returns Formatted string like 'GET /users/{id}'
|
|
95
|
+
*/
|
|
96
|
+
get fullPath() {
|
|
97
|
+
return `${this.method} ${this._path}`;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Parses and validates input data for a specific input type (body, query, params).
|
|
101
|
+
*
|
|
102
|
+
* @param input - The raw input data to validate
|
|
103
|
+
* @param key - The input type key ('body', 'query', or 'params')
|
|
104
|
+
* @returns The validated input data for the specified key
|
|
105
|
+
* @throws {UnprocessableEntityError} When validation fails
|
|
106
|
+
*/
|
|
107
|
+
async parseInput(input, key) {
|
|
108
|
+
const schema = this.input?.[key];
|
|
109
|
+
return Endpoint.parseSchema(schema, input);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Parses and validates the request body against the body schema.
|
|
113
|
+
*
|
|
114
|
+
* @param body - The raw request body to validate
|
|
115
|
+
* @returns The validated body data
|
|
116
|
+
* @throws {UnprocessableEntityError} When body validation fails
|
|
117
|
+
*/
|
|
118
|
+
async parseBody(body) {
|
|
119
|
+
return this.parseInput(body, "body");
|
|
120
|
+
}
|
|
121
|
+
static isSuccessStatus(status) {
|
|
122
|
+
return status >= 200 && status < 300;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Creates a case-insensitive header lookup function from a headers object.
|
|
126
|
+
*
|
|
127
|
+
* @param headers - Object containing header key-value pairs
|
|
128
|
+
* @returns Function to retrieve header values by case-insensitive key, or all headers
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* ```typescript
|
|
132
|
+
* const headerFn = Endpoint.createHeaders({ 'Content-Type': 'application/json' });
|
|
133
|
+
* headerFn('content-type'); // Returns 'application/json'
|
|
134
|
+
* headerFn(); // Returns { 'content-type': 'application/json' }
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
static createHeaders(headers) {
|
|
138
|
+
const headerMap = /* @__PURE__ */ new Map();
|
|
139
|
+
for (const [k, v] of Object.entries(headers)) {
|
|
140
|
+
const key = k.toLowerCase();
|
|
141
|
+
headerMap.set(key, v);
|
|
142
|
+
}
|
|
143
|
+
function get(key) {
|
|
144
|
+
if (key === void 0) return Object.fromEntries(headerMap.entries());
|
|
145
|
+
return headerMap.get(key.toLowerCase());
|
|
146
|
+
}
|
|
147
|
+
return get;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Parses cookie string and creates a cookie lookup function.
|
|
151
|
+
*
|
|
152
|
+
* @param cookieHeader - The Cookie header value
|
|
153
|
+
* @returns Function to retrieve cookie values by name, or all cookies
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```typescript
|
|
157
|
+
* const cookieFn = Endpoint.createCookies('session=abc123; theme=dark');
|
|
158
|
+
* cookieFn('session'); // Returns 'abc123'
|
|
159
|
+
* cookieFn(); // Returns { session: 'abc123', theme: 'dark' }
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
static createCookies(cookieHeader) {
|
|
163
|
+
const cookieMap = /* @__PURE__ */ new Map();
|
|
164
|
+
if (cookieHeader) {
|
|
165
|
+
const cookies = cookieHeader.split(";");
|
|
166
|
+
for (const cookie of cookies) {
|
|
167
|
+
const [name, ...valueParts] = cookie.trim().split("=");
|
|
168
|
+
if (name) {
|
|
169
|
+
const value = valueParts.join("=");
|
|
170
|
+
cookieMap.set(name, decodeURIComponent(value));
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
function get(name) {
|
|
175
|
+
if (name === void 0) return Object.fromEntries(cookieMap.entries());
|
|
176
|
+
return cookieMap.get(name);
|
|
177
|
+
}
|
|
178
|
+
return get;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Formats a cookie as a Set-Cookie header string.
|
|
182
|
+
*
|
|
183
|
+
* @param name - Cookie name
|
|
184
|
+
* @param value - Cookie value
|
|
185
|
+
* @param options - Cookie options (httpOnly, secure, sameSite, etc.)
|
|
186
|
+
* @returns Formatted Set-Cookie header string
|
|
187
|
+
*
|
|
188
|
+
* @example
|
|
189
|
+
* ```typescript
|
|
190
|
+
* const header = Endpoint.formatCookieHeader('session', 'abc123', {
|
|
191
|
+
* httpOnly: true,
|
|
192
|
+
* secure: true,
|
|
193
|
+
* sameSite: 'strict',
|
|
194
|
+
* maxAge: 3600
|
|
195
|
+
* });
|
|
196
|
+
* // Returns: "session=abc123; Max-Age=3600; HttpOnly; Secure; SameSite=Strict"
|
|
197
|
+
* ```
|
|
198
|
+
*/
|
|
199
|
+
static formatCookieHeader(name, value, options) {
|
|
200
|
+
let cookie = `${name}=${value}`;
|
|
201
|
+
if (options) {
|
|
202
|
+
if (options.domain) cookie += `; Domain=${options.domain}`;
|
|
203
|
+
if (options.path) cookie += `; Path=${options.path}`;
|
|
204
|
+
if (options.expires) cookie += `; Expires=${options.expires.toUTCString()}`;
|
|
205
|
+
if (options.maxAge !== void 0) cookie += `; Max-Age=${options.maxAge}`;
|
|
206
|
+
if (options.httpOnly) cookie += "; HttpOnly";
|
|
207
|
+
if (options.secure) cookie += "; Secure";
|
|
208
|
+
if (options.sameSite) cookie += `; SameSite=${options.sameSite.charAt(0).toUpperCase() + options.sameSite.slice(1)}`;
|
|
209
|
+
}
|
|
210
|
+
return cookie;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Extracts and refines input data from the endpoint context.
|
|
214
|
+
*
|
|
215
|
+
* @param ctx - The endpoint execution context
|
|
216
|
+
* @returns Object containing only the input data (body, query, params)
|
|
217
|
+
* @internal
|
|
218
|
+
*/
|
|
219
|
+
refineInput(ctx) {
|
|
220
|
+
const input = {};
|
|
221
|
+
for (const key of [
|
|
222
|
+
"body",
|
|
223
|
+
"query",
|
|
224
|
+
"params"
|
|
225
|
+
]) if (key in ctx) input[key] = ctx[key];
|
|
226
|
+
return input;
|
|
227
|
+
}
|
|
228
|
+
handler = (ctx, response) => {
|
|
229
|
+
for (const [key, value] of Object.entries(this.defaultHeaders)) response.header(key, value);
|
|
230
|
+
const handlerCtx = {
|
|
231
|
+
...this.refineInput(ctx),
|
|
232
|
+
services: ctx.services,
|
|
233
|
+
logger: ctx.logger,
|
|
234
|
+
header: ctx.header,
|
|
235
|
+
cookie: ctx.cookie,
|
|
236
|
+
session: ctx.session,
|
|
237
|
+
..."auditor" in ctx && { auditor: ctx.auditor },
|
|
238
|
+
..."db" in ctx && { db: ctx.db }
|
|
239
|
+
};
|
|
240
|
+
return this.endpointFn(handlerCtx, response);
|
|
241
|
+
};
|
|
242
|
+
/**
|
|
243
|
+
* Type guard to check if an object is an Endpoint instance.
|
|
244
|
+
*
|
|
245
|
+
* @param obj - The object to check
|
|
246
|
+
* @returns True if the object is an Endpoint
|
|
247
|
+
*/
|
|
248
|
+
static isEndpoint(obj) {
|
|
249
|
+
return Boolean(obj && obj.__IS_FUNCTION__ === true && obj.type === ConstructType.Endpoint);
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Helper to check if response has metadata
|
|
253
|
+
*/
|
|
254
|
+
static hasMetadata(response) {
|
|
255
|
+
return response !== null && typeof response === "object" && "data" in response && "metadata" in response;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Converts Express-style route params to OpenAPI format.
|
|
259
|
+
* @returns Route with ':param' converted to '{param}'
|
|
260
|
+
* @internal
|
|
261
|
+
*/
|
|
262
|
+
get _path() {
|
|
263
|
+
return this.route.replace(/:(\w+)/g, "{$1}");
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Generates OpenAPI 3.1 schema for this endpoint.
|
|
267
|
+
*
|
|
268
|
+
* @returns OpenAPI route definition with operation details
|
|
269
|
+
*/
|
|
270
|
+
async toOpenApi3Route(componentCollector) {
|
|
271
|
+
const operation = {
|
|
272
|
+
operationId: this.operationId,
|
|
273
|
+
...this.description && { description: this.description },
|
|
274
|
+
...this.tags && this.tags.length > 0 && { tags: this.tags },
|
|
275
|
+
responses: { "200": { description: "Successful response" } }
|
|
276
|
+
};
|
|
277
|
+
if (this.outputSchema) {
|
|
278
|
+
const responseSchema = await convertSchemaWithComponents(this.outputSchema, componentCollector);
|
|
279
|
+
if (responseSchema) operation.responses["200"].content = { [this.responseType]: { schema: responseSchema } };
|
|
280
|
+
}
|
|
281
|
+
const pathParameters = [];
|
|
282
|
+
const queryParameters = [];
|
|
283
|
+
if (this.input && typeof this.input === "object") {
|
|
284
|
+
if ([
|
|
285
|
+
"POST",
|
|
286
|
+
"PUT",
|
|
287
|
+
"PATCH"
|
|
288
|
+
].includes(this.method) && "body" in this.input && this.input.body) {
|
|
289
|
+
const bodySchema = await convertSchemaWithComponents(this.input.body, componentCollector);
|
|
290
|
+
if (bodySchema) operation.requestBody = {
|
|
291
|
+
required: true,
|
|
292
|
+
content: { "application/json": { schema: bodySchema } }
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
if ("params" in this.input && this.input.params) {
|
|
296
|
+
const paramsSchema = await convertStandardSchemaToJsonSchema(this.input.params);
|
|
297
|
+
if (paramsSchema && paramsSchema.type === "object" && paramsSchema.properties) for (const [name, schema] of Object.entries(paramsSchema.properties)) pathParameters.push({
|
|
298
|
+
name,
|
|
299
|
+
in: "path",
|
|
300
|
+
required: paramsSchema.required?.includes(name) ?? true,
|
|
301
|
+
schema
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
if ("query" in this.input && this.input.query) {
|
|
305
|
+
const querySchema = await convertStandardSchemaToJsonSchema(this.input.query);
|
|
306
|
+
if (querySchema && querySchema.type === "object" && querySchema.properties) for (const [name, schema] of Object.entries(querySchema.properties)) queryParameters.push({
|
|
307
|
+
name,
|
|
308
|
+
in: "query",
|
|
309
|
+
required: querySchema.required?.includes(name) ?? false,
|
|
310
|
+
schema
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
if (queryParameters.length > 0) operation.parameters = queryParameters;
|
|
315
|
+
const routeObject = {};
|
|
316
|
+
if (pathParameters.length > 0) routeObject.parameters = pathParameters;
|
|
317
|
+
routeObject[this.method.toLowerCase()] = operation;
|
|
318
|
+
return { [this._path]: routeObject };
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Creates a new Endpoint instance.
|
|
322
|
+
*
|
|
323
|
+
* @param options - Configuration options for the endpoint
|
|
324
|
+
* @param options.fn - The handler function to execute
|
|
325
|
+
* @param options.method - HTTP method
|
|
326
|
+
* @param options.route - Route path with parameter placeholders
|
|
327
|
+
* @param options.description - Optional description for documentation
|
|
328
|
+
* @param options.input - Input schemas for validation
|
|
329
|
+
* @param options.logger - Logger instance
|
|
330
|
+
* @param options.output - Output schema for response validation
|
|
331
|
+
* @param options.services - Service dependencies
|
|
332
|
+
* @param options.timeout - Execution timeout in milliseconds
|
|
333
|
+
* @param options.getSession - Session extraction function
|
|
334
|
+
* @param options.authorize - Authorization check function
|
|
335
|
+
* @param options.status - Success HTTP status code (default: 200)
|
|
336
|
+
* @param options.authorizer - Optional authorizer configuration
|
|
337
|
+
*/
|
|
338
|
+
constructor({ fn, method, route, description, tags, input, logger, output: outputSchema, services, timeout, memorySize, getSession, authorize, rateLimit, status = SuccessStatus.OK, publisherService, events, authorizer, auditorStorageService, actorExtractor, audits, databaseService, rlsConfig, rlsBypass, responseType }) {
|
|
339
|
+
super(fn, timeout, ConstructType.Endpoint, input, outputSchema, services, logger, publisherService, events, memorySize, auditorStorageService);
|
|
340
|
+
this.route = route;
|
|
341
|
+
this.method = method;
|
|
342
|
+
this.description = description;
|
|
343
|
+
this.tags = tags;
|
|
344
|
+
this.status = status;
|
|
345
|
+
this.responseType = responseType ?? "application/json";
|
|
346
|
+
this.endpointFn = fn;
|
|
347
|
+
if (getSession) this.getSession = getSession;
|
|
348
|
+
if (authorize) this.authorize = authorize;
|
|
349
|
+
if (rateLimit) this.rateLimit = rateLimit;
|
|
350
|
+
if (authorizer) this.authorizer = authorizer;
|
|
351
|
+
if (actorExtractor) this.actorExtractor = actorExtractor;
|
|
352
|
+
if (audits) this.audits = audits;
|
|
353
|
+
if (databaseService) this.databaseService = databaseService;
|
|
354
|
+
if (rlsConfig) this.rlsConfig = rlsConfig;
|
|
355
|
+
if (rlsBypass) this.rlsBypass = rlsBypass;
|
|
356
|
+
}
|
|
357
|
+
};
|
|
358
|
+
/**
|
|
359
|
+
* Response builder for fluent API in handlers
|
|
360
|
+
*/
|
|
361
|
+
var ResponseBuilder = class {
|
|
362
|
+
metadata = {
|
|
363
|
+
headers: {},
|
|
364
|
+
cookies: /* @__PURE__ */ new Map()
|
|
365
|
+
};
|
|
366
|
+
header(key, value) {
|
|
367
|
+
this.metadata.headers[key] = value;
|
|
368
|
+
return this;
|
|
369
|
+
}
|
|
370
|
+
cookie(name, value, options) {
|
|
371
|
+
this.metadata.cookies?.set(name, {
|
|
372
|
+
value,
|
|
373
|
+
options
|
|
374
|
+
});
|
|
375
|
+
return this;
|
|
376
|
+
}
|
|
377
|
+
deleteCookie(name, options) {
|
|
378
|
+
this.metadata.cookies?.set(name, {
|
|
379
|
+
value: "",
|
|
380
|
+
options: {
|
|
381
|
+
...options,
|
|
382
|
+
maxAge: 0,
|
|
383
|
+
expires: /* @__PURE__ */ new Date(0)
|
|
384
|
+
}
|
|
385
|
+
});
|
|
386
|
+
return this;
|
|
387
|
+
}
|
|
388
|
+
status(code) {
|
|
389
|
+
this.metadata.status = code;
|
|
390
|
+
return this;
|
|
391
|
+
}
|
|
392
|
+
send(data) {
|
|
393
|
+
return {
|
|
394
|
+
data,
|
|
395
|
+
metadata: this.metadata
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
getMetadata() {
|
|
399
|
+
return this.metadata;
|
|
400
|
+
}
|
|
401
|
+
};
|
|
402
|
+
/**
|
|
403
|
+
* HTTP success status codes that can be returned by endpoints.
|
|
404
|
+
*/
|
|
405
|
+
let SuccessStatus = /* @__PURE__ */ function(SuccessStatus$1) {
|
|
406
|
+
/** Standard response for successful HTTP requests */
|
|
407
|
+
SuccessStatus$1[SuccessStatus$1["OK"] = 200] = "OK";
|
|
408
|
+
/** Request has been fulfilled and resulted in a new resource being created */
|
|
409
|
+
SuccessStatus$1[SuccessStatus$1["Created"] = 201] = "Created";
|
|
410
|
+
/** Request has been accepted for processing, but processing is not complete */
|
|
411
|
+
SuccessStatus$1[SuccessStatus$1["Accepted"] = 202] = "Accepted";
|
|
412
|
+
/** Server successfully processed the request but is not returning any content */
|
|
413
|
+
SuccessStatus$1[SuccessStatus$1["NoContent"] = 204] = "NoContent";
|
|
414
|
+
/** Server successfully processed the request and is not returning any content, client should reset the document view */
|
|
415
|
+
SuccessStatus$1[SuccessStatus$1["ResetContent"] = 205] = "ResetContent";
|
|
416
|
+
/** Server is delivering only part of the resource due to a range header */
|
|
417
|
+
SuccessStatus$1[SuccessStatus$1["PartialContent"] = 206] = "PartialContent";
|
|
418
|
+
return SuccessStatus$1;
|
|
419
|
+
}({});
|
|
420
|
+
|
|
421
|
+
//#endregion
|
|
422
|
+
//#region src/endpoints/lazyAccessors.ts
|
|
423
|
+
/**
|
|
424
|
+
* Create a lazy header accessor for Hono.
|
|
425
|
+
* Uses `c.req.header(name)` for single lookups (native, case-insensitive).
|
|
426
|
+
* Only calls `c.req.header()` to get all headers when needed.
|
|
427
|
+
*/
|
|
428
|
+
function createHonoHeaders(c) {
|
|
429
|
+
let allHeaders = null;
|
|
430
|
+
return (key) => {
|
|
431
|
+
if (key !== void 0) return c.req.header(key);
|
|
432
|
+
if (!allHeaders) allHeaders = c.req.header();
|
|
433
|
+
return allHeaders;
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* Create a lazy cookie accessor for Hono.
|
|
438
|
+
* Uses `getCookie(c, name)` for single lookups (native).
|
|
439
|
+
* Only parses the cookie header when all cookies are requested.
|
|
440
|
+
*/
|
|
441
|
+
function createHonoCookies(c) {
|
|
442
|
+
let allCookies = null;
|
|
443
|
+
return (name) => {
|
|
444
|
+
if (name !== void 0) return getCookie(c, name);
|
|
445
|
+
if (!allCookies) {
|
|
446
|
+
allCookies = {};
|
|
447
|
+
const cookieHeader = c.req.header("cookie");
|
|
448
|
+
if (cookieHeader) for (const part of cookieHeader.split(";")) {
|
|
449
|
+
const trimmed = part.trim();
|
|
450
|
+
const eqIndex = trimmed.indexOf("=");
|
|
451
|
+
if (eqIndex > 0) {
|
|
452
|
+
const key = trimmed.slice(0, eqIndex);
|
|
453
|
+
const value = trimmed.slice(eqIndex + 1);
|
|
454
|
+
allCookies[key] = value;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
return allCookies;
|
|
459
|
+
};
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* Create a lazy header accessor for API Gateway events.
|
|
463
|
+
* Handles case-insensitive lookups.
|
|
464
|
+
*/
|
|
465
|
+
function createApiGatewayHeaders(headers) {
|
|
466
|
+
let normalizedHeaders = null;
|
|
467
|
+
const normalize = () => {
|
|
468
|
+
if (!normalizedHeaders) {
|
|
469
|
+
normalizedHeaders = {};
|
|
470
|
+
if (headers) {
|
|
471
|
+
for (const [k, v] of Object.entries(headers)) if (v !== void 0) normalizedHeaders[k.toLowerCase()] = v;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
return normalizedHeaders;
|
|
475
|
+
};
|
|
476
|
+
return (key) => {
|
|
477
|
+
if (key !== void 0) {
|
|
478
|
+
if (headers) {
|
|
479
|
+
const direct = headers[key] ?? headers[key.toLowerCase()];
|
|
480
|
+
if (direct !== void 0) return direct;
|
|
481
|
+
return normalize()[key.toLowerCase()];
|
|
482
|
+
}
|
|
483
|
+
return void 0;
|
|
484
|
+
}
|
|
485
|
+
return normalize();
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* Create a lazy cookie accessor for API Gateway events.
|
|
490
|
+
* Decodes URL-encoded values to match browser behavior.
|
|
491
|
+
*/
|
|
492
|
+
function createApiGatewayCookies(cookies, cookieHeader) {
|
|
493
|
+
let parsed = null;
|
|
494
|
+
const parse = () => {
|
|
495
|
+
if (!parsed) {
|
|
496
|
+
parsed = {};
|
|
497
|
+
if (cookies) for (const cookie of cookies) {
|
|
498
|
+
const eqIndex = cookie.indexOf("=");
|
|
499
|
+
if (eqIndex > 0) {
|
|
500
|
+
const key = cookie.slice(0, eqIndex);
|
|
501
|
+
const value = cookie.slice(eqIndex + 1);
|
|
502
|
+
parsed[key] = decodeURIComponent(value);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
else if (cookieHeader) for (const part of cookieHeader.split(";")) {
|
|
506
|
+
const trimmed = part.trim();
|
|
507
|
+
const eqIndex = trimmed.indexOf("=");
|
|
508
|
+
if (eqIndex > 0) {
|
|
509
|
+
const key = trimmed.slice(0, eqIndex);
|
|
510
|
+
const value = trimmed.slice(eqIndex + 1);
|
|
511
|
+
parsed[key] = decodeURIComponent(value);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
return parsed;
|
|
516
|
+
};
|
|
517
|
+
return (name) => {
|
|
518
|
+
if (name !== void 0) return parse()[name];
|
|
519
|
+
return parse();
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
/**
|
|
523
|
+
* Create a lazy header accessor for plain object headers (test adaptor, etc).
|
|
524
|
+
* Headers are already available as an object, just provide lazy access.
|
|
525
|
+
*/
|
|
526
|
+
function createObjectHeaders(headers) {
|
|
527
|
+
let normalized = null;
|
|
528
|
+
return (key) => {
|
|
529
|
+
if (key !== void 0) {
|
|
530
|
+
if (headers) {
|
|
531
|
+
const direct = headers[key] ?? headers[key.toLowerCase()];
|
|
532
|
+
if (direct !== void 0) return direct;
|
|
533
|
+
}
|
|
534
|
+
if (!normalized && headers) {
|
|
535
|
+
normalized = {};
|
|
536
|
+
for (const [k, v] of Object.entries(headers)) normalized[k.toLowerCase()] = v;
|
|
537
|
+
}
|
|
538
|
+
return normalized?.[key.toLowerCase()];
|
|
539
|
+
}
|
|
540
|
+
if (!normalized && headers) {
|
|
541
|
+
normalized = {};
|
|
542
|
+
for (const [k, v] of Object.entries(headers)) normalized[k.toLowerCase()] = v;
|
|
543
|
+
}
|
|
544
|
+
return normalized ?? {};
|
|
545
|
+
};
|
|
546
|
+
}
|
|
547
|
+
/**
|
|
548
|
+
* Create a lazy cookie accessor from a cookie header string.
|
|
549
|
+
* Decodes URL-encoded values to match browser behavior.
|
|
550
|
+
*/
|
|
551
|
+
function createCookieHeaderAccessor(cookieHeader) {
|
|
552
|
+
let parsed = null;
|
|
553
|
+
const parse = () => {
|
|
554
|
+
if (!parsed) {
|
|
555
|
+
parsed = {};
|
|
556
|
+
if (cookieHeader) for (const part of cookieHeader.split(";")) {
|
|
557
|
+
const trimmed = part.trim();
|
|
558
|
+
const eqIndex = trimmed.indexOf("=");
|
|
559
|
+
if (eqIndex > 0) {
|
|
560
|
+
const key = trimmed.slice(0, eqIndex);
|
|
561
|
+
const value = trimmed.slice(eqIndex + 1);
|
|
562
|
+
parsed[key] = decodeURIComponent(value);
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
return parsed;
|
|
567
|
+
};
|
|
568
|
+
return (name) => {
|
|
569
|
+
if (name !== void 0) return parse()[name];
|
|
570
|
+
return parse();
|
|
571
|
+
};
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* Create a no-op header accessor (for minimal endpoints that don't use headers).
|
|
575
|
+
*/
|
|
576
|
+
function createNoopHeaders() {
|
|
577
|
+
return (key) => {
|
|
578
|
+
if (key !== void 0) return void 0;
|
|
579
|
+
return {};
|
|
580
|
+
};
|
|
581
|
+
}
|
|
582
|
+
/**
|
|
583
|
+
* Create a no-op cookie accessor (for minimal endpoints that don't use cookies).
|
|
584
|
+
*/
|
|
585
|
+
function createNoopCookies() {
|
|
586
|
+
return (name) => {
|
|
587
|
+
if (name !== void 0) return void 0;
|
|
588
|
+
return {};
|
|
589
|
+
};
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
//#endregion
|
|
593
|
+
export { Endpoint, ResponseBuilder, createApiGatewayCookies, createApiGatewayHeaders, createCookieHeaderAccessor, createHonoCookies, createHonoHeaders, createNoopCookies, createNoopHeaders, createObjectHeaders };
|
|
594
|
+
//# sourceMappingURL=lazyAccessors-fFIQrblM.mjs.map
|