@forklaunch/core 0.7.4 → 0.8.2
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/lib/__test__/index.js +3 -5
- package/lib/__test__/index.js.map +1 -1
- package/lib/__test__/index.mjs +4 -6
- package/lib/__test__/index.mjs.map +1 -1
- package/lib/src/cache/index.js +30 -20
- package/lib/src/cache/index.js.map +1 -1
- package/lib/src/cache/index.mjs +37 -20
- package/lib/src/cache/index.mjs.map +1 -1
- package/lib/src/http/index.d.mts +201 -43
- package/lib/src/http/index.d.ts +201 -43
- package/lib/src/http/index.js +321 -37
- package/lib/src/http/index.js.map +1 -1
- package/lib/src/http/index.mjs +326 -37
- package/lib/src/http/index.mjs.map +1 -1
- package/lib/src/services/index.js +7 -1
- package/lib/src/services/index.js.map +1 -1
- package/lib/src/services/index.mjs +7 -1
- package/lib/src/services/index.mjs.map +1 -1
- package/package.json +31 -31
package/lib/src/http/index.d.ts
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
import { AnySchemaValidator, UnboxedObjectSchema, IdiomaticSchema, Schema } from '@forklaunch/validator';
|
2
2
|
import { O as OpenTelemetryCollector, M as MetricsDefinition, T as TelemetryOptions, L as LoggerMeta, a as LogFn } from '../../openTelemetryCollector-DQC2FfOu.js';
|
3
3
|
export { c as MetricType, h as httpRequestsTotalCounter, b as httpServerDurationHistogram } from '../../openTelemetryCollector-DQC2FfOu.js';
|
4
|
-
import { Prettify, RemoveTrailingSlash, MakePropertyOptionalIfChildrenOptional } from '@forklaunch/common';
|
4
|
+
import { UnionToIntersection, MimeType, Prettify, ExclusiveRecord, RemoveTrailingSlash, MakePropertyOptionalIfChildrenOptional } from '@forklaunch/common';
|
5
5
|
import { Span } from '@opentelemetry/api';
|
6
6
|
import { ParsedQs } from 'qs';
|
7
7
|
import { Readable } from 'stream';
|
@@ -78,20 +78,85 @@ type QueryObject<SV extends AnySchemaValidator> = StringOnlyObject<SV> & unknown
|
|
78
78
|
* @template SV - A type that extends AnySchemaValidator.
|
79
79
|
*/
|
80
80
|
type HeadersObject<SV extends AnySchemaValidator> = StringOnlyObject<SV> & unknown;
|
81
|
+
type RawTypedResponseBody<SV extends AnySchemaValidator> = TextBody<SV> | JsonBody<SV> | FileBody<SV> | ServerSentEventBody<SV> | UnknownResponseBody<SV>;
|
82
|
+
type ExclusiveResponseBodyBase<SV extends AnySchemaValidator> = {
|
83
|
+
[K in keyof UnionToIntersection<RawTypedResponseBody<SV>>]?: undefined;
|
84
|
+
};
|
85
|
+
type ExclusiveSchemaCatchall<SV extends AnySchemaValidator> = {
|
86
|
+
[K in keyof SV['_SchemaCatchall'] as string extends K ? never : number extends K ? never : symbol extends K ? never : K]?: undefined;
|
87
|
+
};
|
88
|
+
type TypedResponseBody<SV extends AnySchemaValidator> = {
|
89
|
+
[K in keyof (ExclusiveSchemaCatchall<SV> & ExclusiveResponseBodyBase<SV>)]?: K extends keyof TextBody<SV> ? TextBody<SV>[K] : undefined;
|
90
|
+
} | {
|
91
|
+
[K in keyof (ExclusiveSchemaCatchall<SV> & ExclusiveResponseBodyBase<SV>)]?: K extends keyof JsonBody<SV> ? JsonBody<SV>[K] : undefined;
|
92
|
+
} | {
|
93
|
+
[K in keyof (ExclusiveSchemaCatchall<SV> & ExclusiveResponseBodyBase<SV>)]?: K extends keyof FileBody<SV> ? FileBody<SV>[K] : undefined;
|
94
|
+
} | {
|
95
|
+
[K in keyof (ExclusiveSchemaCatchall<SV> & ExclusiveResponseBodyBase<SV>)]?: K extends keyof ServerSentEventBody<SV> ? ServerSentEventBody<SV>[K] : undefined;
|
96
|
+
} | {
|
97
|
+
[K in keyof (ExclusiveSchemaCatchall<SV> & ExclusiveResponseBodyBase<SV>)]?: K extends keyof UnknownResponseBody<SV> ? UnknownResponseBody<SV>[K] : undefined;
|
98
|
+
};
|
99
|
+
type ResponseBody<SV extends AnySchemaValidator> = TypedResponseBody<SV> | (ExclusiveResponseBodyBase<SV> & SV['_ValidSchemaObject']) | (ExclusiveResponseBodyBase<SV> & UnboxedObjectSchema<SV>) | (ExclusiveResponseBodyBase<SV> & SV['string']);
|
81
100
|
/**
|
82
101
|
* Type representing the responses object in a request.
|
83
102
|
*
|
84
103
|
* @template SV - A type that extends AnySchemaValidator.
|
85
104
|
*/
|
86
105
|
type ResponsesObject<SV extends AnySchemaValidator> = {
|
87
|
-
[
|
88
|
-
}
|
106
|
+
[K: number]: ResponseBody<SV>;
|
107
|
+
};
|
108
|
+
type JsonBody<SV extends AnySchemaValidator> = {
|
109
|
+
contentType?: 'application/json' | string;
|
110
|
+
json: BodyObject<SV> | SV['_ValidSchemaObject'] | SV['_SchemaCatchall'];
|
111
|
+
};
|
89
112
|
/**
|
90
113
|
* Type representing the body in a request.
|
91
114
|
*
|
92
115
|
* @template SV - A type that extends AnySchemaValidator.
|
93
116
|
*/
|
94
|
-
type
|
117
|
+
type TextBody<SV extends AnySchemaValidator> = {
|
118
|
+
contentType?: 'application/xml' | 'text/plain' | 'text/html' | 'text/css' | 'text/javascript' | 'text/csv' | 'text/markdown' | 'text/xml' | 'text/rtf' | 'text/x-yaml' | 'text/yaml' | string;
|
119
|
+
text: SV['string'];
|
120
|
+
};
|
121
|
+
type FileBody<SV extends AnySchemaValidator> = {
|
122
|
+
contentType?: 'application/octet-stream' | 'application/pdf' | 'application/vnd.ms-excel' | 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' | 'application/msword' | 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' | 'application/zip' | 'image/jpeg' | 'image/png' | 'image/gif' | 'audio/mpeg' | 'audio/wav' | 'video/mp4' | string;
|
123
|
+
file: SV['file'] extends (name: string, mimeType: MimeType) => infer R ? R : SV['file'];
|
124
|
+
};
|
125
|
+
/**
|
126
|
+
* Type representing the body in a request.
|
127
|
+
*
|
128
|
+
* @template SV - A type that extends AnySchemaValidator.
|
129
|
+
*/
|
130
|
+
type MultipartForm<SV extends AnySchemaValidator> = {
|
131
|
+
contentType?: 'multipart/form-data' | 'multipart/mixed' | 'multipart/alternative' | 'multipart/related' | 'multipart/signed' | 'multipart/encrypted' | string;
|
132
|
+
multipartForm: BodyObject<SV>;
|
133
|
+
};
|
134
|
+
/**
|
135
|
+
* Type representing the body in a request.
|
136
|
+
*
|
137
|
+
* @template SV - A type that extends AnySchemaValidator.
|
138
|
+
*/
|
139
|
+
type UrlEncodedForm<SV extends AnySchemaValidator> = {
|
140
|
+
contentType?: 'application/x-www-form-urlencoded' | 'application/x-url-encoded' | 'application/x-www-url-encoded' | 'application/x-urlencode' | string;
|
141
|
+
urlEncodedForm: BodyObject<SV>;
|
142
|
+
};
|
143
|
+
type ServerSentEventBody<SV extends AnySchemaValidator> = {
|
144
|
+
contentType?: 'text/event-stream' | string;
|
145
|
+
event: {
|
146
|
+
id: SV['string'];
|
147
|
+
data: SV['string'] | BodyObject<SV>;
|
148
|
+
};
|
149
|
+
};
|
150
|
+
type UnknownBody<SV extends AnySchemaValidator> = {
|
151
|
+
contentType?: string;
|
152
|
+
schema: BodyObject<SV> | SV['_ValidSchemaObject'] | SV['_SchemaCatchall'];
|
153
|
+
};
|
154
|
+
type UnknownResponseBody<SV extends AnySchemaValidator> = {
|
155
|
+
contentType?: string;
|
156
|
+
schema: BodyObject<SV> | SV['_ValidSchemaObject'] | SV['_SchemaCatchall'];
|
157
|
+
};
|
158
|
+
type TypedBody<SV extends AnySchemaValidator> = JsonBody<SV> | TextBody<SV> | FileBody<SV> | MultipartForm<SV> | UrlEncodedForm<SV> | UnknownBody<SV>;
|
159
|
+
type Body<SV extends AnySchemaValidator> = TypedBody<SV> | BodyObject<SV> | SV['_ValidSchemaObject'] | SV['_SchemaCatchall'];
|
95
160
|
type AuthMethodsBase = ({
|
96
161
|
readonly method: 'jwt';
|
97
162
|
} | {
|
@@ -217,14 +282,31 @@ type PathParamHttpContractDetails<SV extends AnySchemaValidator, Path extends `/
|
|
217
282
|
* @template BodySchema - A type for the body schema, defaulting to Body.
|
218
283
|
* @template QuerySchema - A type for query schemas, defaulting to QueryObject.
|
219
284
|
*/
|
220
|
-
type HttpContractDetails<SV extends AnySchemaValidator, Path extends `/${string}` = `/${string}`, ParamsSchema extends ParamsObject<SV> = ParamsObject<SV>, ResponseSchemas extends ResponsesObject<SV> = ResponsesObject<SV>, BodySchema extends Body<SV> = Body<SV>, QuerySchema extends QueryObject<SV> = QueryObject<SV>, ReqHeaders extends HeadersObject<SV> = HeadersObject<SV>, ResHeaders extends HeadersObject<SV> = HeadersObject<SV>, BaseRequest = unknown> = PathParamHttpContractDetails<SV, Path, ParamsSchema, ResponseSchemas, QuerySchema, ReqHeaders, ResHeaders, BaseRequest> & {
|
221
|
-
/** Required body schema for the contract */
|
285
|
+
type HttpContractDetails<SV extends AnySchemaValidator, Path extends `/${string}` = `/${string}`, ParamsSchema extends ParamsObject<SV> = ParamsObject<SV>, ResponseSchemas extends ResponsesObject<SV> = ResponsesObject<SV>, BodySchema extends Body<SV> = Body<SV>, QuerySchema extends QueryObject<SV> = QueryObject<SV>, ReqHeaders extends HeadersObject<SV> = HeadersObject<SV>, ResHeaders extends HeadersObject<SV> = HeadersObject<SV>, BaseRequest = unknown> = PathParamHttpContractDetails<SV, Path, ParamsSchema, ResponseSchemas, QuerySchema, ReqHeaders, ResHeaders, BaseRequest> & (BodySchema extends SV['_SchemaCatchall'] ? {
|
286
|
+
/** Required body schema for body-based methods for the contract */
|
287
|
+
readonly body: BodySchema;
|
288
|
+
} : BodySchema extends JsonBody<SV> ? {
|
289
|
+
/** Required body schema for body-based methods for the contract */
|
290
|
+
readonly body: ExclusiveRecord<BodySchema, TypedBody<SV>>;
|
291
|
+
} : BodySchema extends TextBody<SV> ? {
|
292
|
+
/** Required body schema for body-based methods for the contract */
|
293
|
+
readonly body: ExclusiveRecord<BodySchema, TypedBody<SV>>;
|
294
|
+
} : BodySchema extends MultipartForm<SV> ? {
|
295
|
+
/** Required body schema for body-based methods for the contract */
|
296
|
+
readonly body: ExclusiveRecord<BodySchema, TypedBody<SV>>;
|
297
|
+
} : BodySchema extends UrlEncodedForm<SV> ? {
|
298
|
+
/** Required body schema for body-based methods for the contract */
|
299
|
+
readonly body: ExclusiveRecord<BodySchema, TypedBody<SV>>;
|
300
|
+
} : BodySchema extends FileBody<SV> ? {
|
301
|
+
/** Required body schema for body-based methods for the contract */
|
302
|
+
readonly body: ExclusiveRecord<BodySchema, TypedBody<SV>>;
|
303
|
+
} : {
|
304
|
+
/** Required body schema for body-based methods for the contract */
|
222
305
|
readonly body: BodySchema;
|
223
|
-
|
224
|
-
readonly contentType?: 'application/json' | 'multipart/form-data' | 'application/x-www-form-urlencoded';
|
306
|
+
}) & {
|
225
307
|
readonly auth?: SchemaAuthMethods<SV, string | number | symbol extends ExtractedParamsObject<Path> ? {
|
226
308
|
[K in keyof ExtractedParamsObject<Path>]: ParamsSchema[K];
|
227
|
-
} : ParamsSchema, BodySchema, QuerySchema, ReqHeaders, BaseRequest
|
309
|
+
} : ParamsSchema, BodySchema, QuerySchema, ReqHeaders, BaseRequest>;
|
228
310
|
};
|
229
311
|
/**
|
230
312
|
* Interface representing HTTP contract details for middleware.
|
@@ -316,8 +398,8 @@ interface ForklaunchStatusResponse<ResBody> {
|
|
316
398
|
* @returns {T} - The sent response.
|
317
399
|
*/
|
318
400
|
send: {
|
319
|
-
|
320
|
-
<
|
401
|
+
(body?: ResBody extends AsyncGenerator<unknown> ? never : ResBody | null, close_connection?: boolean): boolean;
|
402
|
+
<U>(body?: ResBody extends AsyncGenerator<unknown> ? never : ResBody | null, close_connection?: boolean): U;
|
321
403
|
};
|
322
404
|
/**
|
323
405
|
* Sends a JSON response.
|
@@ -325,8 +407,8 @@ interface ForklaunchStatusResponse<ResBody> {
|
|
325
407
|
* @returns {boolean|T} - The JSON response.
|
326
408
|
*/
|
327
409
|
json: {
|
328
|
-
(body
|
329
|
-
<
|
410
|
+
(body: ResBody extends string | AsyncGenerator<unknown> ? never : ResBody | null): boolean;
|
411
|
+
<U>(body: ResBody extends string | AsyncGenerator<unknown> ? never : ResBody | null): U;
|
330
412
|
};
|
331
413
|
/**
|
332
414
|
* Sends a JSONP response.
|
@@ -334,17 +416,24 @@ interface ForklaunchStatusResponse<ResBody> {
|
|
334
416
|
* @returns {boolean|T} - The JSONP response.
|
335
417
|
*/
|
336
418
|
jsonp: {
|
337
|
-
(body
|
338
|
-
<
|
419
|
+
(body: ResBody extends string | AsyncGenerator<unknown> ? never : ResBody | null): boolean;
|
420
|
+
<U>(body: ResBody extends string | AsyncGenerator<unknown> ? never : ResBody | null): U;
|
339
421
|
};
|
422
|
+
/**
|
423
|
+
* Sends a Server-Sent Event (SSE) response.
|
424
|
+
* @param {ResBodyMap} [body] - The response body.
|
425
|
+
* @param {number} interval - The interval between events.
|
426
|
+
*/
|
427
|
+
sseEmitter: (generator: () => AsyncGenerator<ResBody extends AsyncGenerator<infer T> ? T : never, void, unknown>) => void;
|
340
428
|
}
|
429
|
+
type ToNumber<T extends string | number | symbol> = T extends number ? T : T extends `${infer N extends number}` ? N : never;
|
341
430
|
/**
|
342
431
|
* Interface representing a Forklaunch response.
|
343
432
|
*
|
344
433
|
* @template ResBodyMap - A type for the response body, defaulting to common status code responses.
|
345
434
|
* @template StatusCode - A type for the status code, defaulting to number.
|
346
435
|
*/
|
347
|
-
interface ForklaunchResponse<ResBodyMap extends Record<number, unknown>, ResHeaders extends Record<string, string>, LocalsObj extends Record<string, unknown>> {
|
436
|
+
interface ForklaunchResponse<BaseResponse, ResBodyMap extends Record<number, unknown>, ResHeaders extends Record<string, string>, LocalsObj extends Record<string, unknown>> {
|
348
437
|
/** Data of the response body */
|
349
438
|
bodyData: unknown;
|
350
439
|
/** Status code of the response */
|
@@ -361,19 +450,22 @@ interface ForklaunchResponse<ResBodyMap extends Record<number, unknown>, ResHead
|
|
361
450
|
* @param {string} key - The header key.
|
362
451
|
* @param {string} value - The header value.
|
363
452
|
*/
|
364
|
-
setHeader:
|
453
|
+
setHeader: {
|
454
|
+
<K extends keyof (ResHeaders & ForklaunchResHeaders)>(key: K, value: K extends keyof ForklaunchResHeaders ? ForklaunchResHeaders[K] : ResHeaders[K]): void;
|
455
|
+
<K extends keyof (ResHeaders & ForklaunchResHeaders)>(key: K, value: K extends keyof ForklaunchResHeaders ? ForklaunchResHeaders[K] : ResHeaders[K]): BaseResponse;
|
456
|
+
};
|
365
457
|
/**
|
366
458
|
* Adds an event listener to the response.
|
367
459
|
* @param {string} event - The event to listen for.
|
368
460
|
* @param {Function} listener - The listener function.
|
369
461
|
*/
|
370
|
-
on(event: 'close', listener: () => void): this;
|
371
|
-
on(event: 'drain', listener: () => void): this;
|
372
|
-
on(event: 'error', listener: (err: Error) => void): this;
|
373
|
-
on(event: 'finish', listener: () => void): this;
|
374
|
-
on(event: 'pipe', listener: (src: Readable) => void): this;
|
375
|
-
on(event: 'unpipe', listener: (src: Readable) => void): this;
|
376
|
-
on(event: string | symbol, listener: (...args: unknown[]) => void): this;
|
462
|
+
on(event: 'close', listener: () => void): BaseResponse & this;
|
463
|
+
on(event: 'drain', listener: () => void): BaseResponse & this;
|
464
|
+
on(event: 'error', listener: (err: Error) => void): BaseResponse & this;
|
465
|
+
on(event: 'finish', listener: () => void): BaseResponse & this;
|
466
|
+
on(event: 'pipe', listener: (src: Readable) => void): BaseResponse & this;
|
467
|
+
on(event: 'unpipe', listener: (src: Readable) => void): BaseResponse & this;
|
468
|
+
on(event: string | symbol, listener: (...args: unknown[]) => void): BaseResponse & this;
|
377
469
|
/**
|
378
470
|
* Sets the status of the response.
|
379
471
|
* @param {U} code - The status code.
|
@@ -381,19 +473,25 @@ interface ForklaunchResponse<ResBodyMap extends Record<number, unknown>, ResHead
|
|
381
473
|
* @returns {ForklaunchResponse<(ResBodyMap)[U], ResHeaders, U, LocalsObj>} - The response with the given status.
|
382
474
|
*/
|
383
475
|
status: {
|
384
|
-
<U extends keyof (ResBodyMap & ForklaunchResErrors)
|
385
|
-
<U extends keyof (ResBodyMap & ForklaunchResErrors)
|
476
|
+
<U extends ToNumber<keyof (ResBodyMap & ForklaunchResErrors)>>(code: U): Omit<BaseResponse, keyof ForklaunchStatusResponse<(Omit<ForklaunchResErrors, keyof ResBodyMap> & ResBodyMap)[U]>> & ForklaunchStatusResponse<(Omit<ForklaunchResErrors, keyof ResBodyMap> & ResBodyMap)[U]>;
|
477
|
+
<U extends ToNumber<keyof (ResBodyMap & ForklaunchResErrors)>>(code: U, message?: string): Omit<BaseResponse, keyof ForklaunchStatusResponse<(Omit<ForklaunchResErrors, keyof ResBodyMap> & ResBodyMap)[U]>> & ForklaunchStatusResponse<(Omit<ForklaunchResErrors, keyof ResBodyMap> & ResBodyMap)[U]>;
|
386
478
|
};
|
387
479
|
/**
|
388
480
|
* Ends the response.
|
389
481
|
* @param {string} [data] - Optional data to send.
|
390
482
|
*/
|
391
|
-
end:
|
483
|
+
end: {
|
484
|
+
(data?: string): void;
|
485
|
+
(cb?: (() => void) | undefined): BaseResponse;
|
486
|
+
};
|
392
487
|
/**
|
393
488
|
* Sets the content type of the response.
|
394
489
|
* @param {string} type - The content type.
|
395
490
|
*/
|
396
|
-
type:
|
491
|
+
type: {
|
492
|
+
(type: string): void;
|
493
|
+
(type: string): BaseResponse;
|
494
|
+
};
|
397
495
|
/** Local variables */
|
398
496
|
locals: LocalsObj;
|
399
497
|
/** Cors */
|
@@ -402,6 +500,8 @@ interface ForklaunchResponse<ResBodyMap extends Record<number, unknown>, ResHead
|
|
402
500
|
responseSchemas: ResponseCompiledSchema;
|
403
501
|
/** Whether the metric has been recorded */
|
404
502
|
metricRecorded: boolean;
|
503
|
+
/** Whether the response has been sent */
|
504
|
+
sent: boolean;
|
405
505
|
}
|
406
506
|
/**
|
407
507
|
* Type representing the next function in a middleware.
|
@@ -432,13 +532,20 @@ type ResolvedForklaunchRequest<SV extends AnySchemaValidator, P extends ParamsDi
|
|
432
532
|
* @template StatusCode - A type for the status code, defaulting to number.
|
433
533
|
*/
|
434
534
|
interface ExpressLikeHandler<SV extends AnySchemaValidator, P extends ParamsDictionary, ResBodyMap extends Record<number, unknown>, ReqBody extends Record<string, unknown>, ReqQuery extends ParsedQs, ReqHeaders extends Record<string, string>, ResHeaders extends Record<string, string>, LocalsObj extends Record<string, unknown>, BaseRequest, BaseResponse, NextFunction> {
|
435
|
-
(req: ResolvedForklaunchRequest<SV, P, ReqBody, ReqQuery, ReqHeaders, BaseRequest>, res: unknown extends BaseResponse ? ForklaunchResponse<ResBodyMap, ResHeaders, LocalsObj> : {
|
436
|
-
[key in keyof BaseResponse]: key extends keyof ForklaunchResponse<ResBodyMap, ResHeaders, LocalsObj> ? ForklaunchResponse<ResBodyMap, ResHeaders, LocalsObj>[key] : key extends keyof BaseResponse ? BaseResponse[key] : never;
|
437
|
-
}, next
|
535
|
+
(req: ResolvedForklaunchRequest<SV, P, ReqBody, ReqQuery, ReqHeaders, BaseRequest>, res: unknown extends BaseResponse ? ForklaunchResponse<BaseResponse, ResBodyMap, ResHeaders, LocalsObj> : {
|
536
|
+
[key in keyof BaseResponse]: key extends keyof ForklaunchResponse<BaseResponse, ResBodyMap, ResHeaders, LocalsObj> ? ForklaunchResponse<BaseResponse, ResBodyMap, ResHeaders, LocalsObj>[key] : key extends keyof BaseResponse ? BaseResponse[key] : never;
|
537
|
+
}, next: NextFunction): void | Promise<void>;
|
438
538
|
}
|
439
539
|
type MapParamsSchema<SV extends AnySchemaValidator, P extends ParamsObject<SV>> = MapSchema<SV, P> extends infer Params ? unknown extends Params ? ParamsDictionary : Params : ParamsDictionary;
|
440
|
-
type
|
441
|
-
|
540
|
+
type ExtractContentType<SV extends AnySchemaValidator, T extends ResponseBody<SV> | unknown> = T extends {
|
541
|
+
contentType: string;
|
542
|
+
} ? T['contentType'] : T extends JsonBody<SV> ? 'application/json' : T extends TextBody<SV> ? 'text/plain' : T extends FileBody<SV> ? 'application/octet-stream' : T extends ServerSentEventBody<SV> ? 'text/event-stream' : T extends UnknownResponseBody<SV> ? 'application/json' : T extends SV['string'] ? 'text/plain' : 'text/plain';
|
543
|
+
type ExtractResponseBody<SV extends AnySchemaValidator, T extends ResponseBody<SV> | unknown> = T extends JsonBody<SV> ? MapSchema<SV, T['json']> : T extends TextBody<SV> ? MapSchema<SV, T['text']> : T extends FileBody<SV> ? File | Blob : T extends ServerSentEventBody<SV> ? AsyncGenerator<MapSchema<SV, T['event']>> : T extends UnknownResponseBody<SV> ? MapSchema<SV, T['schema']> : MapSchema<SV, T>;
|
544
|
+
type MapResBodyMapSchema<SV extends AnySchemaValidator, ResBodyMap extends ResponsesObject<SV>> = unknown extends ResBodyMap ? ForklaunchResErrors : {
|
545
|
+
[K in keyof ResBodyMap]: ExtractResponseBody<SV, ResBodyMap[K]>;
|
546
|
+
};
|
547
|
+
type ExtractBody<SV extends AnySchemaValidator, T extends Body<SV>> = T extends JsonBody<SV> ? T['json'] : T extends TextBody<SV> ? T['text'] : T extends FileBody<SV> ? T['file'] : T extends MultipartForm<SV> ? T['multipartForm'] : T extends UrlEncodedForm<SV> ? T['urlEncodedForm'] : T extends UnknownBody<SV> ? T['schema'] : T;
|
548
|
+
type MapReqBodySchema<SV extends AnySchemaValidator, ReqBody extends Body<SV>> = MapSchema<SV, ExtractBody<SV, ReqBody>> extends infer Body ? unknown extends Body ? Record<string, unknown> : Body : Record<string, unknown>;
|
442
549
|
type MapReqQuerySchema<SV extends AnySchemaValidator, ReqQuery extends QueryObject<SV>> = MapSchema<SV, ReqQuery> extends infer Query ? unknown extends Query ? ParsedQs : Query : ParsedQs;
|
443
550
|
type MapReqHeadersSchema<SV extends AnySchemaValidator, ReqHeaders extends HeadersObject<SV>> = MapSchema<SV, ReqHeaders> extends infer RequestHeaders ? unknown extends RequestHeaders ? Record<string, string> : RequestHeaders : Record<string, string>;
|
444
551
|
type MapResHeadersSchema<SV extends AnySchemaValidator, ResHeaders extends HeadersObject<SV>> = MapSchema<SV, ResHeaders> extends infer ResponseHeaders ? unknown extends ResponseHeaders ? ForklaunchResHeaders : ResponseHeaders : ForklaunchResHeaders;
|
@@ -489,27 +596,35 @@ type LiveTypeFunction<SV extends AnySchemaValidator, Route extends string, P ext
|
|
489
596
|
query: MapSchema<SV, ReqQuery>;
|
490
597
|
}) & (HeadersObject<SV> extends ReqHeaders ? unknown : {
|
491
598
|
headers: MapSchema<SV, ReqHeaders>;
|
492
|
-
}) extends infer Request ? SdkResponse<
|
599
|
+
}) extends infer Request ? SdkResponse<SV, ResponsesObject<SV> extends ResBodyMap ? Record<number, unknown> : ResBodyMap, ForklaunchResHeaders extends ResHeaders ? unknown : MapSchema<SV, ResHeaders>> extends infer Return ? unknown extends Request ? (route: RemoveTrailingSlash<Route>) => Promise<Return> : (route: RemoveTrailingSlash<Route>, request: Prettify<MakePropertyOptionalIfChildrenOptional<Request>>) => Promise<Return> : never : never;
|
493
600
|
/**
|
494
601
|
* Represents a basic SDK Response object.
|
495
602
|
*
|
496
603
|
* @template ResBodyMap - A type for the response body.
|
497
604
|
* @template ResHeaders - A type for the response headers.
|
498
605
|
*/
|
499
|
-
type SdkResponse<ResBodyMap extends Record<number, unknown>, ResHeaders extends Record<string, string> | unknown> = Prettify<{
|
606
|
+
type SdkResponse<SV extends AnySchemaValidator, ResBodyMap extends Record<number, unknown>, ResHeaders extends Record<string, string> | unknown> = Prettify<({
|
607
|
+
[K in keyof ForklaunchResErrors]: {
|
608
|
+
code: K;
|
609
|
+
contentType: 'text/plain';
|
610
|
+
response: ForklaunchResErrors[K];
|
611
|
+
};
|
612
|
+
} & {
|
500
613
|
[K in keyof ResBodyMap]: {
|
501
614
|
code: K;
|
502
|
-
|
615
|
+
contentType: ExtractContentType<SV, ResBodyMap[K]>;
|
616
|
+
response: ExtractResponseBody<SV, ResBodyMap[K]>;
|
503
617
|
} & (unknown extends ResHeaders ? unknown : {
|
504
618
|
headers: ResHeaders;
|
505
619
|
});
|
506
|
-
}[keyof ResBodyMap]>;
|
620
|
+
})[keyof (ForklaunchResErrors & ResBodyMap)]>;
|
507
621
|
/**
|
508
622
|
* Represents the default error types for responses.
|
509
623
|
*/
|
510
|
-
type ForklaunchResErrors<BadRequest = string, Unauthorized = string, Forbidden = string, InternalServerErrorType = string> = {
|
624
|
+
type ForklaunchResErrors<BadRequest = string, Unauthorized = string, NotFound = string, Forbidden = string, InternalServerErrorType = string> = {
|
511
625
|
400: BadRequest;
|
512
626
|
401: Unauthorized;
|
627
|
+
404: NotFound;
|
513
628
|
403: Forbidden;
|
514
629
|
500: InternalServerErrorType;
|
515
630
|
};
|
@@ -644,12 +759,13 @@ declare class ForklaunchExpressLikeRouter<SV extends AnySchemaValidator, BasePat
|
|
644
759
|
#private;
|
645
760
|
readonly schemaValidator: SV;
|
646
761
|
readonly internal: Internal;
|
762
|
+
readonly postEnrichMiddleware: RouterHandler[];
|
647
763
|
readonly openTelemetryCollector: OpenTelemetryCollector<MetricsDefinition>;
|
648
764
|
requestHandler: RouterHandler;
|
649
765
|
routers: ForklaunchRouter<SV>[];
|
650
766
|
readonly routes: ForklaunchRoute<SV>[];
|
651
767
|
readonly basePath: BasePath;
|
652
|
-
constructor(basePath: BasePath, schemaValidator: SV, internal: Internal, openTelemetryCollector: OpenTelemetryCollector<MetricsDefinition>);
|
768
|
+
constructor(basePath: BasePath, schemaValidator: SV, internal: Internal, postEnrichMiddleware: RouterHandler[], openTelemetryCollector: OpenTelemetryCollector<MetricsDefinition>);
|
653
769
|
registerRoute<ContractMethod extends Method, Path extends `/${string}`, P extends ParamsObject<SV>, ResBodyMap extends ResponsesObject<SV>, ReqBody extends Body<SV>, ReqQuery extends QueryObject<SV>, ReqHeaders extends HeadersObject<SV>, ResHeaders extends HeadersObject<SV>, LocalsObj extends Record<string, unknown>>(method: ContractMethod, path: Path, registrationMethod: PathBasedHandler<RouterHandler>, contractDetailsOrMiddlewareOrTypedHandler: ContractDetailsOrMiddlewareOrTypedHandler<SV, ContractMethod, Path, P, ResBodyMap, ReqBody, ReqQuery, ReqHeaders, ResHeaders, LocalsObj, BaseRequest, BaseResponse, NextFunction>, ...middlewareOrMiddlewareAndTypedHandler: MiddlewareOrMiddlewareWithTypedHandler<SV, ContractMethod, Path, P, ResBodyMap, ReqBody, ReqQuery, ReqHeaders, ResHeaders, LocalsObj, BaseRequest, BaseResponse, NextFunction>[]): LiveTypeFunction<SV, `${BasePath}${Path}`, P, ResBodyMap, ReqBody, ReqQuery, ReqHeaders, ResHeaders>;
|
654
770
|
registerMiddlewareHandler<Path extends `/${string}`, P extends ParamsObject<SV>, ResBodyMap extends ResponsesObject<SV>, ReqBody extends Body<SV>, ReqQuery extends QueryObject<SV>, ReqHeaders extends HeadersObject<SV>, ResHeaders extends HeadersObject<SV>, LocalsObj extends Record<string, unknown>>(registrationMethod: PathOrMiddlewareBasedHandler<RouterHandler>, pathOrContractDetailsOrMiddlewareOrTypedHandler: Path | ContractDetailsOrMiddlewareOrTypedHandler<SV, 'middleware', Path, P, ResBodyMap, ReqBody, ReqQuery, ReqHeaders, ResHeaders, LocalsObj, BaseRequest, BaseResponse, NextFunction>, contractDetailsOrMiddlewareOrTypedHandler?: ContractDetailsOrMiddlewareOrTypedHandler<SV, 'middleware', Path, P, ResBodyMap, ReqBody, ReqQuery, ReqHeaders, ResHeaders, LocalsObj, BaseRequest, BaseResponse, NextFunction>, ...middlewareOrMiddlewareWithTypedHandler: MiddlewareOrMiddlewareWithTypedHandler<SV, 'middleware', Path, P, ResBodyMap, ReqBody, ReqQuery, ReqHeaders, ResHeaders, LocalsObj, BaseRequest, BaseResponse, NextFunction>[]): this;
|
655
771
|
registerNestableMiddlewareHandler<Path extends `/${string}`, P extends ParamsObject<SV>, ResBodyMap extends ResponsesObject<SV>, ReqBody extends Body<SV>, ReqQuery extends QueryObject<SV>, ReqHeaders extends HeadersObject<SV>, ResHeaders extends HeadersObject<SV>, LocalsObj extends Record<string, unknown>>(registrationMethod: NestableRouterBasedHandler<RouterHandler, Internal>, pathOrContractDetailsOrMiddlewareOrTypedHandler: Path | ContractDetailsOrMiddlewareOrTypedHandler<SV, 'middleware', Path, P, ResBodyMap, ReqBody, ReqQuery, ReqHeaders, ResHeaders, LocalsObj, BaseRequest, BaseResponse, NextFunction> | ConstrainedForklaunchRouter<SV, RouterHandler>, contractDetailsOrMiddlewareOrTypedHandler?: ContractDetailsOrMiddlewareOrTypedHandler<SV, 'middleware', Path, P, ResBodyMap, ReqBody, ReqQuery, ReqHeaders, ResHeaders, LocalsObj, BaseRequest, BaseResponse, NextFunction> | ConstrainedForklaunchRouter<SV, RouterHandler>, ...middlewareOrMiddlewareWithTypedHandler: (MiddlewareOrMiddlewareWithTypedHandler<SV, 'middleware', Path, P, ResBodyMap, ReqBody, ReqQuery, ReqHeaders, ResHeaders, LocalsObj, BaseRequest, BaseResponse, NextFunction> | ConstrainedForklaunchRouter<SV, RouterHandler>)[]): this;
|
@@ -779,13 +895,14 @@ declare class ForklaunchExpressLikeRouter<SV extends AnySchemaValidator, BasePat
|
|
779
895
|
declare abstract class ForklaunchExpressLikeApplication<SV extends AnySchemaValidator, Server extends ExpressLikeRouter<RouterHandler, Server>, RouterHandler, BaseRequest, BaseResponse, NextFunction> extends ForklaunchExpressLikeRouter<SV, '/', RouterHandler, Server, BaseRequest, BaseResponse, NextFunction> {
|
780
896
|
readonly schemaValidator: SV;
|
781
897
|
readonly internal: Server;
|
898
|
+
readonly postEnrichMiddleware: RouterHandler[];
|
782
899
|
readonly openTelemetryCollector: OpenTelemetryCollector<MetricsDefinition>;
|
783
900
|
/**
|
784
901
|
* Creates an instance of the Application class.
|
785
902
|
*
|
786
903
|
* @param {SV} schemaValidator - The schema validator.
|
787
904
|
*/
|
788
|
-
constructor(schemaValidator: SV, internal: Server, openTelemetryCollector: OpenTelemetryCollector<MetricsDefinition>);
|
905
|
+
constructor(schemaValidator: SV, internal: Server, postEnrichMiddleware: RouterHandler[], openTelemetryCollector: OpenTelemetryCollector<MetricsDefinition>);
|
789
906
|
abstract listen(...args: unknown[]): void;
|
790
907
|
}
|
791
908
|
|
@@ -963,7 +1080,7 @@ declare const getCodeForStatus: (status: string) => null | StatusCode;
|
|
963
1080
|
*
|
964
1081
|
* @returns {unknown} The return value of the original `send` method, typically the response itself.
|
965
1082
|
*/
|
966
|
-
declare function enrichExpressLikeSend<SV extends AnySchemaValidator, P extends ParamsDictionary, ResBodyMap extends Record<number, unknown>, ReqBody extends Record<string, unknown>, ReqQuery extends ParsedQs, ReqHeaders extends Record<string, string>, ResHeaders extends Record<string, string>, LocalsObj extends Record<string, unknown>>(instance: unknown, req: ForklaunchRequest<SV, P, ReqBody, ReqQuery, ReqHeaders>, res: ForklaunchResponse<ResBodyMap, ForklaunchResHeaders & ResHeaders, LocalsObj>,
|
1083
|
+
declare function enrichExpressLikeSend<SV extends AnySchemaValidator, P extends ParamsDictionary, ResBodyMap extends Record<number, unknown>, ReqBody extends Record<string, unknown>, ReqQuery extends ParsedQs, ReqHeaders extends Record<string, string>, ResHeaders extends Record<string, string>, LocalsObj extends Record<string, unknown>>(instance: unknown, req: ForklaunchRequest<SV, P, ReqBody, ReqQuery, ReqHeaders>, res: ForklaunchResponse<unknown, ResBodyMap, ForklaunchResHeaders & ResHeaders, LocalsObj>, originalOperation: ForklaunchStatusResponse<ForklaunchSendableData>['json'] | ForklaunchStatusResponse<ForklaunchSendableData>['jsonp'], originalSend: ForklaunchStatusResponse<ForklaunchSendableData>['send'], data: ForklaunchSendableData | File | Blob | AsyncGenerator<Record<string, unknown>> | null | undefined, shouldEnrich: boolean): void;
|
967
1084
|
|
968
1085
|
/**
|
969
1086
|
* Generates a Swagger document from given routers.
|
@@ -976,6 +1093,47 @@ declare function enrichExpressLikeSend<SV extends AnySchemaValidator, P extends
|
|
976
1093
|
*/
|
977
1094
|
declare function generateSwaggerDocument<SV extends AnySchemaValidator>(schemaValidator: SV, port: string | number, routers: ForklaunchRouter<SV>[]): OpenAPIObject;
|
978
1095
|
|
1096
|
+
/**
|
1097
|
+
* Discriminates the body type from the given contract details and returns information
|
1098
|
+
* necessary for parsing and validation.
|
1099
|
+
*
|
1100
|
+
* @template SV - Schema validator type.
|
1101
|
+
* @template Path - Route path type.
|
1102
|
+
* @template P - Params schema type.
|
1103
|
+
* @template ResBodyMap - Response body map type.
|
1104
|
+
* @template ReqBody - Request body type.
|
1105
|
+
* @template ReqQuery - Request query object type.
|
1106
|
+
* @template ReqHeaders - Request headers object type.
|
1107
|
+
* @template ResHeaders - Response headers object type.
|
1108
|
+
* @param contractDetails - The HTTP contract details.
|
1109
|
+
* @returns An object containing the content type, parser type, and schema.
|
1110
|
+
* @throws If no body-related information is found in the contract details.
|
1111
|
+
*/
|
1112
|
+
declare function discriminateBody<SV extends AnySchemaValidator>(schemaValidator: SV, body: Body<SV> | undefined): {
|
1113
|
+
contentType: string;
|
1114
|
+
parserType: 'json' | 'text' | 'multipart' | 'urlEncoded' | 'file';
|
1115
|
+
schema: SV['_ValidSchemaObject'];
|
1116
|
+
} | undefined;
|
1117
|
+
/**
|
1118
|
+
* Discriminates all response body types from the given contract details and returns
|
1119
|
+
* information necessary for parsing and validation for each status code.
|
1120
|
+
*
|
1121
|
+
* @template SV - Schema validator type.
|
1122
|
+
* @template Path - Route path type.
|
1123
|
+
* @template P - Params schema type.
|
1124
|
+
* @template ResBodyMap - Response body map type.
|
1125
|
+
* @template ReqQuery - Request query object type.
|
1126
|
+
* @template ReqHeaders - Request headers object type.
|
1127
|
+
* @template ResHeaders - Response headers object type.
|
1128
|
+
* @param contractDetails - The contract details containing response schemas.
|
1129
|
+
* @returns A record mapping status codes to content type, parser type, and schema info.
|
1130
|
+
*/
|
1131
|
+
declare function discriminateResponseBodies<SV extends AnySchemaValidator>(schemaValidator: SV, responses: ResponsesObject<SV>): Record<number, {
|
1132
|
+
contentType: string;
|
1133
|
+
parserType: "json" | "text" | "serverSentEvent" | "file" | "multipart";
|
1134
|
+
schema: SV["_ValidSchemaObject"];
|
1135
|
+
}>;
|
1136
|
+
|
979
1137
|
declare const ATTR_API_NAME = "api.name";
|
980
1138
|
declare const ATTR_CORRELATION_ID = "correlation.id";
|
981
1139
|
|
@@ -1007,7 +1165,7 @@ declare class PinoLogger {
|
|
1007
1165
|
}
|
1008
1166
|
declare function logger(level: LevelWithSilentOrString, meta?: AnyValueMap): PinoLogger;
|
1009
1167
|
|
1010
|
-
declare function recordMetric<SV extends AnySchemaValidator, P extends ParamsDictionary, ReqBody extends Record<string, unknown>, ReqQuery extends ParsedQs, ResBodyMap extends Record<string, unknown>, ReqHeaders extends Record<string, string>, ResHeaders extends Record<string, string>, LocalsObj extends Record<string, unknown>>(req: ForklaunchRequest<SV, P, ReqBody, ReqQuery, ReqHeaders>, res: ForklaunchResponse<ResBodyMap, ResHeaders, LocalsObj>): void;
|
1168
|
+
declare function recordMetric<SV extends AnySchemaValidator, P extends ParamsDictionary, ReqBody extends Record<string, unknown>, ReqQuery extends ParsedQs, ResBodyMap extends Record<string, unknown>, ReqHeaders extends Record<string, string>, ResHeaders extends Record<string, string>, LocalsObj extends Record<string, unknown>>(req: ForklaunchRequest<SV, P, ReqBody, ReqQuery, ReqHeaders>, res: ForklaunchResponse<unknown, ResBodyMap, ResHeaders, LocalsObj>): void;
|
1011
1169
|
|
1012
1170
|
type DocsConfiguration = ({
|
1013
1171
|
type: 'scalar';
|
@@ -1015,4 +1173,4 @@ type DocsConfiguration = ({
|
|
1015
1173
|
type: 'swagger';
|
1016
1174
|
};
|
1017
1175
|
|
1018
|
-
export { ATTR_API_NAME, ATTR_CORRELATION_ID, type ApiClient, type AuthMethods, type AuthMethodsBase, type Body, type BodyObject, type ConstrainedForklaunchRouter, type ContractDetails, type ContractDetailsExpressLikeSchemaHandler, type ContractDetailsOrMiddlewareOrTypedHandler, type DocsConfiguration, type ErrorContainer, type ExpressLikeAuthMapper, type ExpressLikeHandler, type ExpressLikeRouter, type ExpressLikeSchemaAuthMapper, type ExpressLikeSchemaHandler, type ExpressLikeTypedHandler, type ExtractedParamsObject, type ForklaunchBaseRequest, ForklaunchExpressLikeApplication, ForklaunchExpressLikeRouter, type ForklaunchNextFunction, type ForklaunchRequest, type ForklaunchResErrors, type ForklaunchResHeaders, type ForklaunchResponse, type ForklaunchRoute, type ForklaunchRouter, type ForklaunchSendableData, type ForklaunchStatusResponse, HTTPStatuses, type HeadersObject, type HttpContractDetails, type HttpMethod, type LiveTypeFunction, type LiveTypeRouteDefinition, LogFn, LoggerMeta, type MapParamsSchema, type MapReqBodySchema, type MapReqHeadersSchema, type MapReqQuerySchema, type MapResBodyMapSchema, type MapResHeadersSchema, type MapSchema, type Method, MetricsDefinition, type MiddlewareContractDetails, type MiddlewareOrMiddlewareWithTypedHandler, type NestableRouterBasedHandler, type NumberOnlyObject, OpenTelemetryCollector, type ParamsDictionary, type ParamsObject, type PathBasedHandler, type PathMatch, type PathOrMiddlewareBasedHandler, type PathParamHttpContractDetails, type PathParamMethod, type QueryObject, type RequestContext, type ResolvedForklaunchRequest, type ResponseCompiledSchema, type ResponseShape, type ResponsesObject, type SchemaAuthMethods, type StatusCode, type StringOnlyObject, TelemetryOptions, type TypedHandler, type TypedMiddlewareDefinition, type TypedNestableMiddlewareDefinition, delete_, enrichExpressLikeSend, evaluateTelemetryOptions, generateSwaggerDocument, get, getCodeForStatus, head, isClientError, isForklaunchRequest, isForklaunchRouter, isInformational, isRedirection, isServerError, isSuccessful, isValidStatusCode, logger, meta, metricsDefinitions, middleware, options, patch, post, put, recordMetric, trace, typedHandler };
|
1176
|
+
export { ATTR_API_NAME, ATTR_CORRELATION_ID, type ApiClient, type AuthMethods, type AuthMethodsBase, type Body, type BodyObject, type ConstrainedForklaunchRouter, type ContractDetails, type ContractDetailsExpressLikeSchemaHandler, type ContractDetailsOrMiddlewareOrTypedHandler, type DocsConfiguration, type ErrorContainer, type ExpressLikeAuthMapper, type ExpressLikeHandler, type ExpressLikeRouter, type ExpressLikeSchemaAuthMapper, type ExpressLikeSchemaHandler, type ExpressLikeTypedHandler, type ExtractBody, type ExtractContentType, type ExtractResponseBody, type ExtractedParamsObject, type FileBody, type ForklaunchBaseRequest, ForklaunchExpressLikeApplication, ForklaunchExpressLikeRouter, type ForklaunchNextFunction, type ForklaunchRequest, type ForklaunchResErrors, type ForklaunchResHeaders, type ForklaunchResponse, type ForklaunchRoute, type ForklaunchRouter, type ForklaunchSendableData, type ForklaunchStatusResponse, HTTPStatuses, type HeadersObject, type HttpContractDetails, type HttpMethod, type JsonBody, type LiveTypeFunction, type LiveTypeRouteDefinition, LogFn, LoggerMeta, type MapParamsSchema, type MapReqBodySchema, type MapReqHeadersSchema, type MapReqQuerySchema, type MapResBodyMapSchema, type MapResHeadersSchema, type MapSchema, type Method, MetricsDefinition, type MiddlewareContractDetails, type MiddlewareOrMiddlewareWithTypedHandler, type MultipartForm, type NestableRouterBasedHandler, type NumberOnlyObject, OpenTelemetryCollector, type ParamsDictionary, type ParamsObject, type PathBasedHandler, type PathMatch, type PathOrMiddlewareBasedHandler, type PathParamHttpContractDetails, type PathParamMethod, type QueryObject, type RawTypedResponseBody, type RequestContext, type ResolvedForklaunchRequest, type ResponseBody, type ResponseCompiledSchema, type ResponseShape, type ResponsesObject, type SchemaAuthMethods, type ServerSentEventBody, type StatusCode, type StringOnlyObject, TelemetryOptions, type TextBody, type TypedBody, type TypedHandler, type TypedMiddlewareDefinition, type TypedNestableMiddlewareDefinition, type TypedResponseBody, type UnknownBody, type UnknownResponseBody, type UrlEncodedForm, delete_, discriminateBody, discriminateResponseBodies, enrichExpressLikeSend, evaluateTelemetryOptions, generateSwaggerDocument, get, getCodeForStatus, head, isClientError, isForklaunchRequest, isForklaunchRouter, isInformational, isRedirection, isServerError, isSuccessful, isValidStatusCode, logger, meta, metricsDefinitions, middleware, options, patch, post, put, recordMetric, trace, typedHandler };
|