@emmett-community/emmett-expressjs-with-openapi 0.1.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.
@@ -0,0 +1,440 @@
1
+ import express, { Request, Response, Router, Application, NextFunction } from 'express';
2
+ import http from 'http';
3
+ import { ProblemDocument } from 'http-problem-details';
4
+ import * as _event_driven_io_emmett from '@event-driven-io/emmett';
5
+ import { Brand, Event, TestEventStream, EventStore } from '@event-driven-io/emmett';
6
+ import supertest, { Test, Response as Response$1 } from 'supertest';
7
+ import TestAgent from 'supertest/lib/agent';
8
+
9
+ /**
10
+ * OpenAPI v3 Document type (to avoid requiring express-openapi-validator types directly)
11
+ */
12
+ type OpenAPIV3Document = any;
13
+ /**
14
+ * Imported handler modules, keyed by module name.
15
+ * Automatically populated by the framework when operationHandlers is configured.
16
+ */
17
+ type ImportedHandlerModules = Record<string, any>;
18
+ /**
19
+ * Security handlers for custom authentication/authorization logic.
20
+ * Maps security scheme names to handler functions.
21
+ *
22
+ * @see https://cdimascio.github.io/express-openapi-validator-documentation/usage-validate-security/
23
+ */
24
+ type SecurityHandlers = Record<string, (req: any, scopes: string[], schema: any) => boolean | Promise<boolean>>;
25
+ /**
26
+ * Configuration options for express-openapi-validator middleware.
27
+ * This allows optional validation of API requests and responses against an OpenAPI specification.
28
+ *
29
+ * @see https://cdimascio.github.io/express-openapi-validator-documentation/
30
+ */
31
+ type OpenApiValidatorOptions = {
32
+ /**
33
+ * Path to the OpenAPI specification file (JSON or YAML)
34
+ * or an OpenAPI specification object.
35
+ */
36
+ apiSpec: string | OpenAPIV3Document;
37
+ /**
38
+ * Determines whether the validator should validate requests.
39
+ * Can be a boolean or an object with detailed request validation options.
40
+ * @default true
41
+ * @see https://cdimascio.github.io/express-openapi-validator-documentation/usage-validate-requests/
42
+ */
43
+ validateRequests?: boolean | {
44
+ /**
45
+ * Allow unknown query parameters (not defined in the spec).
46
+ * @default false
47
+ */
48
+ allowUnknownQueryParameters?: boolean;
49
+ /**
50
+ * Coerce types in request parameters.
51
+ * @default true
52
+ */
53
+ coerceTypes?: boolean | 'array';
54
+ /**
55
+ * Remove additional properties not defined in the spec.
56
+ * @default false
57
+ */
58
+ removeAdditional?: boolean | 'all' | 'failing';
59
+ };
60
+ /**
61
+ * Determines whether the validator should validate responses.
62
+ * Can be a boolean or an object with detailed response validation options.
63
+ * @default false
64
+ * @see https://cdimascio.github.io/express-openapi-validator-documentation/usage-validate-responses/
65
+ */
66
+ validateResponses?: boolean | {
67
+ /**
68
+ * Remove additional properties from responses not defined in the spec.
69
+ * @default false
70
+ */
71
+ removeAdditional?: boolean | 'all' | 'failing';
72
+ /**
73
+ * Coerce types in responses.
74
+ * @default true
75
+ */
76
+ coerceTypes?: boolean;
77
+ /**
78
+ * Callback to handle response validation errors.
79
+ */
80
+ onError?: (error: any, body: any, req: any) => void;
81
+ };
82
+ /**
83
+ * Determines whether the validator should validate security.
84
+ * Can be a boolean or an object with security handlers.
85
+ * @default true
86
+ * @see https://cdimascio.github.io/express-openapi-validator-documentation/usage-validate-security/
87
+ */
88
+ validateSecurity?: boolean | {
89
+ /**
90
+ * Custom security handlers for authentication/authorization.
91
+ */
92
+ handlers?: SecurityHandlers;
93
+ };
94
+ /**
95
+ * Defines how the validator should validate formats.
96
+ * When true, uses ajv-formats for format validation.
97
+ * When false, format validation is disabled.
98
+ * Can also be 'fast' or 'full' for different validation modes.
99
+ * @default true
100
+ */
101
+ validateFormats?: boolean | 'fast' | 'full';
102
+ /**
103
+ * The base path to the operation handlers directory.
104
+ * When set to a path, automatically wires OpenAPI operations to handler functions
105
+ * based on operationId or x-eov-operation-id.
106
+ * When false, operation handlers are disabled (manual routing required).
107
+ * @default false
108
+ * @see https://cdimascio.github.io/express-openapi-validator-documentation/guide-operation-handlers/
109
+ */
110
+ operationHandlers?: string | false | {
111
+ /**
112
+ * Base path to operation handlers directory.
113
+ */
114
+ basePath?: string;
115
+ /**
116
+ * Resolver function to map operationId to handler module path.
117
+ */
118
+ resolver?: (handlersPath: string, route: string, apiDoc: OpenAPIV3Document) => string;
119
+ };
120
+ /**
121
+ * Paths or pattern to ignore during validation.
122
+ * @default undefined
123
+ */
124
+ ignorePaths?: RegExp | ((path: string) => boolean);
125
+ /**
126
+ * Validate the OpenAPI specification itself.
127
+ * @default true
128
+ */
129
+ validateApiSpec?: boolean;
130
+ /**
131
+ * $ref parser configuration for handling OpenAPI references.
132
+ * @default undefined
133
+ */
134
+ $refParser?: {
135
+ mode: 'bundle' | 'dereference';
136
+ };
137
+ /**
138
+ * Serve the OpenAPI specification at a specific path.
139
+ * When set to a string, the spec will be served at that path.
140
+ * When false, the spec will not be served.
141
+ * @default false
142
+ * @example '/api-docs/openapi.json'
143
+ */
144
+ serveSpec?: string | false;
145
+ /**
146
+ * File upload configuration options.
147
+ * @see https://cdimascio.github.io/express-openapi-validator-documentation/usage-file-uploads/
148
+ */
149
+ fileUploader?: boolean | {
150
+ /**
151
+ * Destination directory for uploaded files.
152
+ */
153
+ dest?: string;
154
+ /**
155
+ * File size limit in bytes.
156
+ */
157
+ limits?: {
158
+ fileSize?: number;
159
+ files?: number;
160
+ };
161
+ };
162
+ /**
163
+ * Optional callback to initialize operation handlers with dependencies.
164
+ * Called before the OpenAPI validator middleware is configured.
165
+ *
166
+ * The framework automatically imports handler modules referenced in your
167
+ * OpenAPI spec (via x-eov-operation-handler) and passes them as the first parameter.
168
+ *
169
+ * @param handlers - Auto-imported handler modules, keyed by module name
170
+ * @returns void or a Promise that resolves when initialization is complete
171
+ *
172
+ * @example
173
+ * ```typescript
174
+ * // With automatic import (recommended)
175
+ * initializeHandlers: async (handlers) => {
176
+ * handlers.shoppingCarts.initializeHandlers(eventStore, messageBus, getUnitPrice, getCurrentTime);
177
+ * }
178
+ *
179
+ * // Manual import (still supported for backward compatibility)
180
+ * import * as handlersModule from './handlers/shoppingCarts';
181
+ * import { registerHandlerModule } from '@emmett-community/emmett-expressjs-with-openapi';
182
+ * initializeHandlers: () => {
183
+ * const handlersPath = path.join(__dirname, './handlers/shoppingCarts');
184
+ * registerHandlerModule(handlersPath, handlersModule);
185
+ * handlersModule.initializeHandlers(eventStore, messageBus, getUnitPrice, getCurrentTime);
186
+ * }
187
+ * ```
188
+ */
189
+ initializeHandlers?: (handlers?: ImportedHandlerModules) => void | Promise<void>;
190
+ };
191
+ /**
192
+ * Helper function to create OpenAPI validator configuration with sensible defaults.
193
+ *
194
+ * @param apiSpec - Path to OpenAPI spec file or OpenAPI document object
195
+ * @param options - Additional validator options
196
+ * @returns Complete OpenApiValidatorOptions configuration
197
+ *
198
+ * @example
199
+ * ```typescript
200
+ * // Basic usage with default options
201
+ * const validatorOptions = createOpenApiValidatorOptions('./openapi.yaml');
202
+ *
203
+ * // With response validation enabled
204
+ * const validatorOptions = createOpenApiValidatorOptions('./openapi.yaml', {
205
+ * validateResponses: true
206
+ * });
207
+ *
208
+ * // With custom security handlers
209
+ * const validatorOptions = createOpenApiValidatorOptions('./openapi.yaml', {
210
+ * validateSecurity: {
211
+ * handlers: {
212
+ * bearerAuth: async (req, scopes) => {
213
+ * // Custom authentication logic
214
+ * return true;
215
+ * }
216
+ * }
217
+ * }
218
+ * });
219
+ *
220
+ * // Serving the spec at /api-docs
221
+ * const validatorOptions = createOpenApiValidatorOptions('./openapi.yaml', {
222
+ * serveSpec: '/api-docs/openapi.json'
223
+ * });
224
+ *
225
+ * // With dependency injection for operation handlers
226
+ * type ShoppingCartDeps = {
227
+ * eventStore: EventStore;
228
+ * messageBus: EventsPublisher;
229
+ * getUnitPrice: (productId: string) => Promise<number>;
230
+ * getCurrentTime: () => Date;
231
+ * };
232
+ *
233
+ * const validatorOptions = createOpenApiValidatorOptions<ShoppingCartDeps>(
234
+ * './openapi.yaml',
235
+ * {
236
+ * operationHandlers: './handlers',
237
+ * initializeHandlers: (deps) => {
238
+ * initializeHandlers(
239
+ * deps.eventStore,
240
+ * deps.messageBus,
241
+ * deps.getUnitPrice,
242
+ * deps.getCurrentTime
243
+ * );
244
+ * }
245
+ * }
246
+ * );
247
+ *
248
+ * const app = getApplication({
249
+ * apis: [myApi],
250
+ * openApiValidator: validatorOptions
251
+ * });
252
+ * ```
253
+ */
254
+ declare const createOpenApiValidatorOptions: (apiSpec: string | OpenAPIV3Document, options?: Partial<Omit<OpenApiValidatorOptions, "apiSpec">>) => OpenApiValidatorOptions;
255
+ /**
256
+ * Type guard to check if express-openapi-validator is available
257
+ */
258
+ declare const isOpenApiValidatorAvailable: () => Promise<boolean>;
259
+
260
+ declare const HeaderNames: {
261
+ IF_MATCH: string;
262
+ IF_NOT_MATCH: string;
263
+ ETag: string;
264
+ };
265
+ type WeakETag = Brand<`W/${string}`, 'ETag'>;
266
+ type ETag = Brand<string, 'ETag'>;
267
+ declare const WeakETagRegex: RegExp;
268
+ declare const enum ETagErrors {
269
+ WRONG_WEAK_ETAG_FORMAT = "WRONG_WEAK_ETAG_FORMAT",
270
+ MISSING_IF_MATCH_HEADER = "MISSING_IF_MATCH_HEADER",
271
+ MISSING_IF_NOT_MATCH_HEADER = "MISSING_IF_NOT_MATCH_HEADER"
272
+ }
273
+ declare const isWeakETag: (etag: ETag) => etag is WeakETag;
274
+ declare const getWeakETagValue: (etag: ETag) => string;
275
+ declare const toWeakETag: (value: number | bigint | string) => WeakETag;
276
+ declare const getETagFromIfMatch: (request: Request) => ETag;
277
+ declare const getETagFromIfNotMatch: (request: Request) => ETag;
278
+ declare const setETag: (response: Response, etag: ETag) => void;
279
+ declare const getETagValueFromIfMatch: (request: Request) => string;
280
+
281
+ type ErrorToProblemDetailsMapping = (error: Error, request: Request) => ProblemDocument | undefined;
282
+ type HttpResponseOptions = {
283
+ body?: unknown;
284
+ location?: string;
285
+ eTag?: ETag;
286
+ };
287
+ declare const DefaultHttpResponseOptions: HttpResponseOptions;
288
+ type HttpProblemResponseOptions = {
289
+ location?: string;
290
+ eTag?: ETag;
291
+ } & Omit<HttpResponseOptions, 'body'> & ({
292
+ problem: ProblemDocument;
293
+ } | {
294
+ problemDetails: string;
295
+ });
296
+ declare const DefaultHttpProblemResponseOptions: HttpProblemResponseOptions;
297
+ type CreatedHttpResponseOptions = ({
298
+ createdId: string;
299
+ } | {
300
+ createdId?: string;
301
+ url: string;
302
+ }) & HttpResponseOptions;
303
+ declare const sendCreated: (response: Response, { eTag, ...options }: CreatedHttpResponseOptions) => void;
304
+ type AcceptedHttpResponseOptions = {
305
+ location: string;
306
+ } & HttpResponseOptions;
307
+ declare const sendAccepted: (response: Response, options: AcceptedHttpResponseOptions) => void;
308
+ type NoContentHttpResponseOptions = Omit<HttpResponseOptions, 'body'>;
309
+ declare const send: (response: Response, statusCode: number, options?: HttpResponseOptions) => void;
310
+ declare const sendProblem: (response: Response, statusCode: number, options?: HttpProblemResponseOptions) => void;
311
+
312
+ type WebApiSetup = (router: Router) => void;
313
+ type ApplicationOptions = {
314
+ apis?: WebApiSetup[];
315
+ mapError?: ErrorToProblemDetailsMapping;
316
+ enableDefaultExpressEtag?: boolean;
317
+ disableJsonMiddleware?: boolean;
318
+ disableUrlEncodingMiddleware?: boolean;
319
+ disableProblemDetailsMiddleware?: boolean;
320
+ /**
321
+ * Optional OpenAPI validator configuration.
322
+ * When provided, enables request/response validation against an OpenAPI specification.
323
+ * Requires the 'express-openapi-validator' package to be installed.
324
+ *
325
+ * @see https://github.com/cdimascio/express-openapi-validator
326
+ * @example
327
+ * ```typescript
328
+ * import { getApplication, createOpenApiValidatorOptions } from '@event-driven-io/emmett-expressjs';
329
+ *
330
+ * type AppDeps = {
331
+ * eventStore: EventStore;
332
+ * messageBus: EventsPublisher;
333
+ * };
334
+ *
335
+ * const app = await getApplication({
336
+ * openApiValidator: createOpenApiValidatorOptions<AppDeps>('./openapi.yaml', {
337
+ * validateResponses: true,
338
+ * operationHandlers: './handlers',
339
+ * initializeHandlers: (deps) => {
340
+ * initializeHandlers(deps.eventStore, deps.messageBus);
341
+ * }
342
+ * })
343
+ * });
344
+ * ```
345
+ */
346
+ openApiValidator?: OpenApiValidatorOptions;
347
+ };
348
+ declare const getApplication: (options: ApplicationOptions) => Promise<express.Application>;
349
+ type StartApiOptions = {
350
+ port?: number;
351
+ };
352
+ declare const startAPI: (app: Application, options?: StartApiOptions) => http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
353
+
354
+ type HttpHandler<RequestType extends Request> = (request: RequestType) => Promise<HttpResponse> | HttpResponse;
355
+ declare const on: <RequestType extends Request>(handle: HttpHandler<RequestType>) => (request: RequestType, response: Response, _next: NextFunction) => Promise<void>;
356
+ declare const OK: (options?: HttpResponseOptions) => HttpResponse;
357
+ declare const Created: (options: CreatedHttpResponseOptions) => HttpResponse;
358
+ declare const Accepted: (options: AcceptedHttpResponseOptions) => HttpResponse;
359
+ declare const NoContent: (options?: NoContentHttpResponseOptions) => HttpResponse;
360
+ type HttpResponse = (response: Response) => void;
361
+ declare const HttpResponse: (statusCode: number, options?: HttpResponseOptions) => HttpResponse;
362
+ declare const BadRequest: (options?: HttpProblemResponseOptions) => HttpResponse;
363
+ declare const Forbidden: (options?: HttpProblemResponseOptions) => HttpResponse;
364
+ declare const NotFound: (options?: HttpProblemResponseOptions) => HttpResponse;
365
+ declare const Conflict: (options?: HttpProblemResponseOptions) => HttpResponse;
366
+ declare const PreconditionFailed: (options: HttpProblemResponseOptions) => HttpResponse;
367
+ declare const HttpProblem: (statusCode: number, options?: HttpProblemResponseOptions) => HttpResponse;
368
+
369
+ type TestRequest = (request: TestAgent<supertest.Test>) => Test;
370
+ declare const existingStream: <EventType extends Event = Event>(streamId: string, events: EventType[]) => TestEventStream<EventType>;
371
+ type ResponseAssert = (response: Response$1) => boolean | void;
372
+ type ApiSpecificationAssert<EventType extends Event = Event> = TestEventStream<EventType>[] | ResponseAssert | [ResponseAssert, ...TestEventStream<EventType>[]];
373
+ declare const expect: <EventType extends Event = Event>(streamId: string, events: EventType[]) => TestEventStream<EventType>;
374
+ declare const expectNewEvents: <EventType extends Event = Event>(streamId: string, events: EventType[]) => TestEventStream<EventType>;
375
+ declare const expectResponse: <Body = unknown>(statusCode: number, options?: {
376
+ body?: Body;
377
+ headers?: {
378
+ [index: string]: string;
379
+ };
380
+ }) => (response: Response$1) => void;
381
+ declare const expectError: (errorCode: number, problemDetails?: Partial<ProblemDocument>) => (response: Response$1) => void;
382
+ type ApiSpecification<EventType extends Event = Event> = (...givenStreams: TestEventStream<EventType>[]) => {
383
+ when: (setupRequest: TestRequest) => {
384
+ then: (verify: ApiSpecificationAssert<EventType>) => Promise<void>;
385
+ };
386
+ };
387
+ declare const ApiSpecification: {
388
+ for: <EventType extends Event = Event, Store extends EventStore<_event_driven_io_emmett.ReadEventMetadataWithGlobalPosition> = EventStore<_event_driven_io_emmett.ReadEventMetadataWithGlobalPosition<bigint>>>(getEventStore: () => Store, getApplication: (eventStore: Store) => Application | Promise<Application>) => ApiSpecification<EventType>;
389
+ };
390
+
391
+ type E2EResponseAssert = (response: Response$1) => boolean | void;
392
+ type ApiE2ESpecificationAssert = [E2EResponseAssert];
393
+ type ApiE2ESpecification = (...givenRequests: TestRequest[]) => {
394
+ when: (setupRequest: TestRequest) => {
395
+ then: (verify: ApiE2ESpecificationAssert) => Promise<void>;
396
+ };
397
+ };
398
+ declare const ApiE2ESpecification: {
399
+ for: <Store extends EventStore = EventStore<_event_driven_io_emmett.AnyRecordedMessageMetadata>>(getEventStore: () => Store, getApplication: (eventStore: Store) => Application | Promise<Application>) => ApiE2ESpecification;
400
+ };
401
+
402
+ /**
403
+ * ESM Resolver for express-openapi-validator operation handlers.
404
+ *
405
+ * INTERNAL MODULE - Not part of public API.
406
+ *
407
+ * PROBLEM:
408
+ * When using TypeScript runtime (tsx) with express-openapi-validator's operationHandlers:
409
+ * - Our code uses `import()` to load handlers (ESM)
410
+ * - express-openapi-validator uses `require()` to load handlers (CJS)
411
+ * - Node.js maintains separate module caches for ESM and CJS
412
+ * - This creates TWO separate instances of the same handler module
413
+ * - Dependencies injected via module-level variables in one instance don't reach the other
414
+ *
415
+ * SOLUTION:
416
+ * This module monkey-patches Module.prototype.require to intercept when
417
+ * express-openapi-validator loads operation handlers and forces it to use
418
+ * dynamic import() instead of require(). This ensures both sides share the
419
+ * same ESM module instance, allowing module-level variables to work correctly.
420
+ *
421
+ * USAGE:
422
+ * This module is automatically activated by getApplication() when operationHandlers
423
+ * are configured. Applications don't need to import or configure anything.
424
+ *
425
+ * LIMITATIONS:
426
+ * - Relies on heuristic detection (caller path includes 'express-openapi-validator')
427
+ * - May break if express-openapi-validator changes its internal loading mechanism
428
+ * - Adds "magic" behavior that may not be immediately obvious to developers
429
+ *
430
+ * FUTURE:
431
+ * If express-openapi-validator migrates to native ESM, this resolver becomes
432
+ * unnecessary and will safely become a no-op.
433
+ */
434
+ /**
435
+ * Registers a pre-loaded ESM module so it can be returned synchronously
436
+ * when express-openapi-validator tries to require() it.
437
+ */
438
+ declare const registerHandlerModule: (modulePath: string, moduleExports: any) => void;
439
+
440
+ export { Accepted, type AcceptedHttpResponseOptions, ApiE2ESpecification, type ApiE2ESpecificationAssert, ApiSpecification, type ApiSpecificationAssert, type ApplicationOptions, BadRequest, Conflict, Created, type CreatedHttpResponseOptions, DefaultHttpProblemResponseOptions, DefaultHttpResponseOptions, type E2EResponseAssert, type ETag, ETagErrors, type ErrorToProblemDetailsMapping, Forbidden, HeaderNames, type HttpHandler, HttpProblem, type HttpProblemResponseOptions, HttpResponse, type HttpResponseOptions, type ImportedHandlerModules, NoContent, type NoContentHttpResponseOptions, NotFound, OK, type OpenAPIV3Document, type OpenApiValidatorOptions, PreconditionFailed, type ResponseAssert, type SecurityHandlers, type StartApiOptions, type TestRequest, type WeakETag, WeakETagRegex, type WebApiSetup, createOpenApiValidatorOptions, existingStream, expect, expectError, expectNewEvents, expectResponse, getApplication, getETagFromIfMatch, getETagFromIfNotMatch, getETagValueFromIfMatch, getWeakETagValue, isOpenApiValidatorAvailable, isWeakETag, on, registerHandlerModule, send, sendAccepted, sendCreated, sendProblem, setETag, startAPI, toWeakETag };