snap-on-openapi 1.0.16 → 1.0.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/OpenApi.d.ts +2 -1
- package/dist/OpenApi.js +17 -6
- package/dist/services/ConfigBuilder/ConfigBuilder.js +6 -5
- package/dist/services/ConfigBuilder/types/DefaultConfig.d.ts +3 -3
- package/dist/services/ConfigBuilder/types/DefaultRouteContextMap.d.ts +1 -0
- package/dist/services/ConfigBuilder/types/DefaultRouteContextMap.js +1 -0
- package/dist/types/config/Config.d.ts +4 -4
- package/dist/types/events/OnErrorEvent.d.ts +2 -1
- package/dist/types/events/OnHandlerEvent.d.ts +2 -1
- package/dist/types/events/OnResponseEvent.d.ts +2 -1
- package/dist/types/events/OnRouteEvent.d.ts +4 -2
- package/package.json +1 -1
package/dist/OpenApi.d.ts
CHANGED
|
@@ -22,6 +22,7 @@ import { InitialBuilder } from './types/InitialBuilder.js';
|
|
|
22
22
|
import { DefaultRouteContextMap } from './services/ConfigBuilder/types/DefaultRouteContextMap.js';
|
|
23
23
|
import { DefaultRouteParamsMap } from './services/ConfigBuilder/types/DefaultRouteParamsMap.js';
|
|
24
24
|
import z from 'zod';
|
|
25
|
+
import { OnResponseEvent } from './types/events/OnResponseEvent.js';
|
|
25
26
|
import { RouteResponse } from './types/RouteResponse.js';
|
|
26
27
|
export declare class OpenApi<TRouteTypes extends string, TErrorCodes extends string, TConfig extends AnyConfig<TRouteTypes, TErrorCodes>> {
|
|
27
28
|
static readonly builder: InitialBuilder;
|
|
@@ -48,7 +49,7 @@ export declare class OpenApi<TRouteTypes extends string, TErrorCodes extends str
|
|
|
48
49
|
addServer(url: string, description: string): void;
|
|
49
50
|
protected getRouteForPath(path: string, method: string): AnyRoute<TRouteTypes> | null;
|
|
50
51
|
processRootRoute(originalReq: Request): Promise<RouteResponse>;
|
|
51
|
-
protected handleError(e: unknown, req: Request): Promise<{
|
|
52
|
+
protected handleError(e: unknown, req: Request, eventPieces?: Partial<OnResponseEvent<TRouteTypes, TConfig['routes'][TRouteTypes]['extraProps']>>): Promise<{
|
|
52
53
|
status: number;
|
|
53
54
|
body: z.TypeOf<import("./index.js").OpenApiErrorConfigMap<TErrorCodes>[TErrorCodes]["responseValidator"]>;
|
|
54
55
|
headers: {};
|
package/dist/OpenApi.js
CHANGED
|
@@ -128,9 +128,14 @@ export class OpenApi {
|
|
|
128
128
|
return null;
|
|
129
129
|
}
|
|
130
130
|
async processRootRoute(originalReq) {
|
|
131
|
+
let onRequest;
|
|
132
|
+
let onRoute;
|
|
133
|
+
let onResponse;
|
|
134
|
+
let onHandler;
|
|
131
135
|
try {
|
|
132
136
|
if (this.config.onRequest) {
|
|
133
|
-
|
|
137
|
+
onRequest = { request: originalReq, logger: this.logger };
|
|
138
|
+
await this.config.onRequest(onRequest);
|
|
134
139
|
}
|
|
135
140
|
const url = new URL(originalReq.url);
|
|
136
141
|
const basePath = this.getBasePath() === '/' ? '' : this.getBasePath();
|
|
@@ -179,7 +184,7 @@ export class OpenApi {
|
|
|
179
184
|
query: reqQuery,
|
|
180
185
|
body: body,
|
|
181
186
|
};
|
|
182
|
-
|
|
187
|
+
onRoute = {
|
|
183
188
|
request: originalReq,
|
|
184
189
|
logger: this.logger,
|
|
185
190
|
path: urlPath,
|
|
@@ -221,7 +226,7 @@ export class OpenApi {
|
|
|
221
226
|
body: bodyData,
|
|
222
227
|
},
|
|
223
228
|
});
|
|
224
|
-
|
|
229
|
+
onHandler = {
|
|
225
230
|
...onRoute,
|
|
226
231
|
validated: {
|
|
227
232
|
query: query.data,
|
|
@@ -256,7 +261,7 @@ export class OpenApi {
|
|
|
256
261
|
headers: route.validators.responseHeaders?.strict() ?? z.object({}),
|
|
257
262
|
status: z.literal(200),
|
|
258
263
|
});
|
|
259
|
-
|
|
264
|
+
onResponse = {
|
|
260
265
|
...onHandler,
|
|
261
266
|
response,
|
|
262
267
|
};
|
|
@@ -276,12 +281,18 @@ export class OpenApi {
|
|
|
276
281
|
return response;
|
|
277
282
|
}
|
|
278
283
|
catch (e) {
|
|
279
|
-
return await this.handleError(e, originalReq
|
|
284
|
+
return await this.handleError(e, originalReq, {
|
|
285
|
+
...onHandler,
|
|
286
|
+
...onRequest,
|
|
287
|
+
...onRoute,
|
|
288
|
+
...onResponse,
|
|
289
|
+
});
|
|
280
290
|
}
|
|
281
291
|
}
|
|
282
|
-
async handleError(e, req) {
|
|
292
|
+
async handleError(e, req, eventPieces) {
|
|
283
293
|
try {
|
|
284
294
|
const event = {
|
|
295
|
+
...eventPieces,
|
|
285
296
|
request: req,
|
|
286
297
|
logger: this.logger,
|
|
287
298
|
error: e,
|
|
@@ -45,18 +45,19 @@ export class ConfigBuilder {
|
|
|
45
45
|
if (conf) {
|
|
46
46
|
return this.construct(conf);
|
|
47
47
|
}
|
|
48
|
-
const routeMap = this.routeMap ?? undefined;
|
|
49
48
|
const contextMap = this.routeContextMap ?? undefined;
|
|
50
|
-
if (this.
|
|
51
|
-
|
|
52
|
-
|
|
49
|
+
if (this.routeMap) {
|
|
50
|
+
const keys = Object.keys(this.routeMap);
|
|
51
|
+
for (const key of keys) {
|
|
52
|
+
const factory = contextMap?.[key] ? contextMap[key] : async () => undefined;
|
|
53
|
+
this.routeMap[key].contextFactory = factory;
|
|
53
54
|
}
|
|
54
55
|
}
|
|
55
56
|
const def = new DefaultConfig();
|
|
56
57
|
const builtConf = {
|
|
57
58
|
...def,
|
|
58
59
|
...this.conf,
|
|
59
|
-
...(routeMap ? { routes: routeMap } : {}),
|
|
60
|
+
...(this.routeMap ? { routes: this.routeMap } : {}),
|
|
60
61
|
...(this.errorMap ? { errors: this.errorMap } : {}),
|
|
61
62
|
...(this.defaultError ? { defaultError: this.defaultError } : {}),
|
|
62
63
|
};
|
|
@@ -21,9 +21,9 @@ export declare class DefaultConfig implements Config<SampleRouteType, ErrorCode,
|
|
|
21
21
|
};
|
|
22
22
|
};
|
|
23
23
|
onRequest?: () => Promise<void>;
|
|
24
|
-
onRoute?: (e: OnRouteEvent) => Promise<void>;
|
|
24
|
+
onRoute?: (e: OnRouteEvent<SampleRouteType, DefaultRouteParamsMap>) => Promise<void>;
|
|
25
25
|
onHandler?: () => Promise<void>;
|
|
26
|
-
onResponse?: (e: OnResponseEvent) => Promise<void>;
|
|
27
|
-
onError?: (e: OnErrorEvent) => Promise<ErrorResponse<ErrorCode, DefaultErrorMap>>;
|
|
26
|
+
onResponse?: (e: OnResponseEvent<SampleRouteType, DefaultRouteParamsMap>) => Promise<void>;
|
|
27
|
+
onError?: (e: OnErrorEvent<SampleRouteType, DefaultRouteParamsMap>) => Promise<ErrorResponse<ErrorCode, DefaultErrorMap>>;
|
|
28
28
|
skipDescriptionsCheck?: boolean;
|
|
29
29
|
}
|
|
@@ -3,4 +3,5 @@ import { RouteContextMap } from '../../../types/config/RouteContextMap.js';
|
|
|
3
3
|
import { DefaultRouteParamsMap } from './DefaultRouteParamsMap.js';
|
|
4
4
|
export declare class DefaultRouteContextMap implements RouteContextMap<SampleRouteType, DefaultRouteParamsMap> {
|
|
5
5
|
Public: () => Promise<{}>;
|
|
6
|
+
User: () => Promise<{}>;
|
|
6
7
|
}
|
|
@@ -33,8 +33,8 @@ export type Config<TRouteTypes extends string, TErrorCodes extends string, TErro
|
|
|
33
33
|
servers?: Server[];
|
|
34
34
|
logLevel?: LogLevel;
|
|
35
35
|
onRequest?: (e: OnRequestEvent) => Promise<void>;
|
|
36
|
-
onRoute?: (e: OnRouteEvent) => Promise<void>;
|
|
37
|
-
onHandler?: (e: OnHandlerEvent) => Promise<void>;
|
|
38
|
-
onResponse?: (e: OnResponseEvent) => Promise<void>;
|
|
39
|
-
onError?: (e: OnErrorEvent) => Promise<ErrorResponse<TErrorCodes, TErrorConfigMap>>;
|
|
36
|
+
onRoute?: (e: OnRouteEvent<TRouteTypes, TRouteParamMap>) => Promise<void>;
|
|
37
|
+
onHandler?: (e: OnHandlerEvent<TRouteTypes, TRouteParamMap>) => Promise<void>;
|
|
38
|
+
onResponse?: (e: OnResponseEvent<TRouteTypes, TRouteParamMap>) => Promise<void>;
|
|
39
|
+
onError?: (e: OnErrorEvent<TRouteTypes, TRouteParamMap>) => Promise<ErrorResponse<TErrorCodes, TErrorConfigMap>>;
|
|
40
40
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Logger } from '../../services/Logger/Logger.js';
|
|
2
|
+
import { RouteExtraPropsMap } from '../config/RouteExtraPropsMap.js';
|
|
2
3
|
import { OnResponseEvent } from './OnResponseEvent.js';
|
|
3
|
-
export interface OnErrorEvent extends Partial<OnResponseEvent
|
|
4
|
+
export interface OnErrorEvent<TRouteType extends string, TContextMap extends RouteExtraPropsMap<TRouteType>> extends Partial<OnResponseEvent<TRouteType, TContextMap>> {
|
|
4
5
|
request: Request;
|
|
5
6
|
logger: Logger;
|
|
6
7
|
error: unknown;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { RouteExtraPropsMap } from '../config/RouteExtraPropsMap.js';
|
|
1
2
|
import { OnRouteEvent } from './OnRouteEvent.js';
|
|
2
|
-
export interface OnHandlerEvent extends OnRouteEvent {
|
|
3
|
+
export interface OnHandlerEvent<TRouteType extends string, TContextMap extends RouteExtraPropsMap<TRouteType>> extends OnRouteEvent<TRouteType, TContextMap> {
|
|
3
4
|
validated: {
|
|
4
5
|
query: Record<string, unknown>;
|
|
5
6
|
path: Record<string, unknown>;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { RouteExtraPropsMap } from '../config/RouteExtraPropsMap.js';
|
|
1
2
|
import { RouteResponse } from '../RouteResponse.js';
|
|
2
3
|
import { OnHandlerEvent } from './OnHandlerEvent.js';
|
|
3
|
-
export interface OnResponseEvent extends OnHandlerEvent {
|
|
4
|
+
export interface OnResponseEvent<TRouteType extends string, TContextMap extends RouteExtraPropsMap<TRouteType>> extends OnHandlerEvent<TRouteType, TContextMap> {
|
|
4
5
|
response: RouteResponse;
|
|
5
6
|
}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { AnyRoute } from '../AnyRoute.js';
|
|
2
|
+
import { RouteExtraProps } from '../config/RouteExtraProps.js';
|
|
3
|
+
import { RouteExtraPropsMap } from '../config/RouteExtraPropsMap.js';
|
|
2
4
|
import { OnRequestEvent } from './OnRequestEvent.js';
|
|
3
|
-
export interface OnRouteEvent extends OnRequestEvent {
|
|
5
|
+
export interface OnRouteEvent<TRouteType extends string, TContextMap extends RouteExtraPropsMap<TRouteType>> extends OnRequestEvent {
|
|
4
6
|
path: string;
|
|
5
7
|
method: string;
|
|
6
8
|
params: Record<string, string>;
|
|
7
9
|
query: Record<string, string | string[]>;
|
|
8
10
|
body: unknown;
|
|
9
|
-
route: AnyRoute<
|
|
11
|
+
route: AnyRoute<TRouteType> & RouteExtraProps<TContextMap[TRouteType]>;
|
|
10
12
|
}
|
package/package.json
CHANGED