@duplojs/http 0.5.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/functionsBuilders/route/build.d.ts +1 -1
- package/dist/core/functionsBuilders/route/hook.cjs +1 -0
- package/dist/core/functionsBuilders/route/hook.d.ts +4 -3
- package/dist/core/functionsBuilders/route/hook.mjs +1 -0
- package/dist/core/functionsBuilders/steps/build.d.ts +1 -1
- package/dist/core/hub/hooks.cjs +4 -0
- package/dist/core/hub/hooks.d.ts +3 -0
- package/dist/core/hub/hooks.mjs +4 -1
- package/dist/core/hub/index.cjs +1 -0
- package/dist/core/hub/index.mjs +1 -1
- package/dist/core/implementHttpServer.cjs +36 -0
- package/dist/core/implementHttpServer.d.ts +13 -0
- package/dist/core/implementHttpServer.mjs +34 -0
- package/dist/core/index.cjs +5 -0
- package/dist/core/index.d.ts +2 -0
- package/dist/core/index.mjs +3 -1
- package/dist/core/narrowingInput.cjs +14 -0
- package/dist/core/narrowingInput.d.ts +10 -0
- package/dist/core/narrowingInput.mjs +12 -0
- package/dist/core/route/hooks.cjs +0 -1
- package/dist/core/route/hooks.d.ts +0 -1
- package/dist/core/route/hooks.mjs +0 -1
- package/dist/core/router/types/buildedRouter.d.ts +2 -1
- package/dist/interfaces/node/createHttpServer.cjs +36 -46
- package/dist/interfaces/node/createHttpServer.d.ts +1 -5
- package/dist/interfaces/node/createHttpServer.mjs +36 -46
- package/dist/interfaces/node/hooks.cjs +12 -9
- package/dist/interfaces/node/hooks.d.ts +1 -1
- package/dist/interfaces/node/hooks.mjs +12 -9
- package/dist/plugins/codeGenerator/plugin.cjs +3 -0
- package/dist/plugins/codeGenerator/plugin.mjs +4 -1
- package/dist/plugins/openApiGenerator/makeOpenApiRoute.cjs +3 -1
- package/dist/plugins/openApiGenerator/makeOpenApiRoute.d.ts +1 -1
- package/dist/plugins/openApiGenerator/makeOpenApiRoute.mjs +3 -1
- package/dist/plugins/openApiGenerator/plugin.cjs +17 -9
- package/dist/plugins/openApiGenerator/plugin.d.ts +2 -2
- package/dist/plugins/openApiGenerator/plugin.mjs +18 -10
- package/package.json +1 -1
|
@@ -7,4 +7,4 @@ export interface BuildRouteFunctionParams extends BuildStepFunctionParams {
|
|
|
7
7
|
readonly globalHooksRouteLifeCycle: readonly HookRouteLifeCycle[];
|
|
8
8
|
readonly defaultExtractContract: ResponseContract.Contract;
|
|
9
9
|
}
|
|
10
|
-
export declare function buildRouteFunction(route: Route, params: BuildRouteFunctionParams): Promise<
|
|
10
|
+
export declare function buildRouteFunction(route: Route, params: BuildRouteFunctionParams): Promise<import("..").BuildStepNotSupportEither | import("./create").BuildRouteSuccessEither | BuildRouteNotSupportEither>;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { HookResponse } from "../../response";
|
|
2
|
+
import { type HookAfterSendResponse, type HookBeforeRouteExecution, type HookBeforeSendResponse, type HookError, type HookParseBody, type HookRouteLifeCycle, type HookSendResponse, type RouteHookErrorParams, type RouteHookParams, type RouteHookParamsAfter } from "../../route";
|
|
2
3
|
export declare function exitHookFunction(): import("@duplojs/utils").Kind<import("@duplojs/utils").KindDefinition<"@DuplojsHttpCore/route-hook-exit", unknown>, unknown>;
|
|
3
4
|
export declare function nextHookFunction(): import("@duplojs/utils").Kind<import("@duplojs/utils").KindDefinition<"@DuplojsHttpCore/route-hook-next", unknown>, unknown>;
|
|
4
|
-
export declare function buildHookBefore(hooks: (HookBeforeRouteExecution | HookParseBody)[]): typeof exitHookFunction | ((params: RouteHookParams) => Promise<import("../../route").RouteHookExit | import("@duplojs/utils").Kind<import("@duplojs/utils").KindDefinition<"@DuplojsHttpCore/route-hook-next", unknown>, unknown> | HookResponse<import("../../
|
|
5
|
-
export declare function buildHookErrorBefore(hooks: HookError[]): typeof exitHookFunction | ((params: RouteHookErrorParams) => Promise<import("../../route").RouteHookExit | import("@duplojs/utils").Kind<import("@duplojs/utils").KindDefinition<"@DuplojsHttpCore/route-hook-next", unknown>, unknown> | HookResponse<import("../../
|
|
5
|
+
export declare function buildHookBefore(hooks: (HookBeforeRouteExecution | HookParseBody)[]): typeof exitHookFunction | ((params: RouteHookParams) => Promise<import("../../route").RouteHookExit | import("@duplojs/utils").Kind<import("@duplojs/utils").KindDefinition<"@DuplojsHttpCore/route-hook-next", unknown>, unknown> | HookResponse<import("../../response").ResponseCode, string, unknown>>);
|
|
6
|
+
export declare function buildHookErrorBefore(hooks: HookError[]): typeof exitHookFunction | ((params: RouteHookErrorParams) => Promise<import("../../route").RouteHookExit | import("@duplojs/utils").Kind<import("@duplojs/utils").KindDefinition<"@DuplojsHttpCore/route-hook-next", unknown>, unknown> | HookResponse<import("../../response").ResponseCode, string, unknown>>);
|
|
6
7
|
export declare function buildHookAfter(hooks: (HookBeforeSendResponse | HookSendResponse | HookAfterSendResponse)[]): typeof exitHookFunction | ((params: RouteHookParamsAfter) => Promise<import("../../route").RouteHookExit | import("@duplojs/utils").Kind<import("@duplojs/utils").KindDefinition<"@DuplojsHttpCore/route-hook-next", unknown>, unknown>>);
|
|
7
8
|
export declare function createHookResponse(from: keyof HookRouteLifeCycle): RouteHookParams["response"];
|
|
@@ -7,4 +7,4 @@ export interface BuildStepFunctionParams {
|
|
|
7
7
|
readonly environment: Environment;
|
|
8
8
|
readonly defaultExtractContract: ResponseContract.Contract;
|
|
9
9
|
}
|
|
10
|
-
export declare function buildStepFunction(step: Steps, params: BuildStepFunctionParams): Promise<
|
|
10
|
+
export declare function buildStepFunction(step: Steps, params: BuildStepFunctionParams): Promise<import("./create").BuildStepSuccessEither | BuildStepNotSupportEither>;
|
package/dist/core/hub/hooks.cjs
CHANGED
|
@@ -28,7 +28,11 @@ async function launchHookServerError(hooks, params) {
|
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
|
+
function createHookHubLifeCycle(hookHubLifeCycle) {
|
|
32
|
+
return hookHubLifeCycle;
|
|
33
|
+
}
|
|
31
34
|
|
|
35
|
+
exports.createHookHubLifeCycle = createHookHubLifeCycle;
|
|
32
36
|
exports.hookServerExitKind = hookServerExitKind;
|
|
33
37
|
exports.hookServerNextKind = hookServerNextKind;
|
|
34
38
|
exports.launchHookBeforeBuildRoute = launchHookBeforeBuildRoute;
|
package/dist/core/hub/hooks.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type Route } from "../route";
|
|
2
2
|
import { type EscapeVoid, type Kind, type MaybePromise } from "@duplojs/utils";
|
|
3
3
|
import { type Hub } from ".";
|
|
4
|
+
import { type RouterInitializationData } from "../router";
|
|
4
5
|
export declare const hookServerExitKind: import("@duplojs/utils").KindHandler<import("@duplojs/utils").KindDefinition<"@DuplojsHttpCore/server-hook-exit", unknown>>;
|
|
5
6
|
export interface ServerHookExit extends Kind<typeof hookServerExitKind.definition> {
|
|
6
7
|
}
|
|
@@ -19,6 +20,7 @@ export interface HttpServerErrorParams {
|
|
|
19
20
|
readonly error: unknown;
|
|
20
21
|
next(): ServerHookNext;
|
|
21
22
|
exit(): ServerHookExit;
|
|
23
|
+
routerInitializationData: RouterInitializationData;
|
|
22
24
|
}
|
|
23
25
|
export type HookServerError = (httpServerErrorParams: HttpServerErrorParams) => MaybePromise<ServerHookExit | ServerHookNext>;
|
|
24
26
|
export declare function serverErrorExitHookFunction(): Kind<import("@duplojs/utils").KindDefinition<"@DuplojsHttpCore/server-hook-exit", unknown>, unknown>;
|
|
@@ -31,3 +33,4 @@ export interface HookHubLifeCycle {
|
|
|
31
33
|
beforeServerBuildRoutes?: HookBeforeServerBuildRoutes;
|
|
32
34
|
serverError?: HookServerError;
|
|
33
35
|
}
|
|
36
|
+
export declare function createHookHubLifeCycle(hookHubLifeCycle: HookHubLifeCycle): HookHubLifeCycle;
|
package/dist/core/hub/hooks.mjs
CHANGED
|
@@ -26,5 +26,8 @@ async function launchHookServerError(hooks, params) {
|
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
|
+
function createHookHubLifeCycle(hookHubLifeCycle) {
|
|
30
|
+
return hookHubLifeCycle;
|
|
31
|
+
}
|
|
29
32
|
|
|
30
|
-
export { hookServerExitKind, hookServerNextKind, launchHookBeforeBuildRoute, launchHookServer, launchHookServerError, serverErrorExitHookFunction, serverErrorNextHookFunction };
|
|
33
|
+
export { createHookHubLifeCycle, hookServerExitKind, hookServerNextKind, launchHookBeforeBuildRoute, launchHookServer, launchHookServerError, serverErrorExitHookFunction, serverErrorNextHookFunction };
|
package/dist/core/hub/index.cjs
CHANGED
|
@@ -141,6 +141,7 @@ function createHub(config) {
|
|
|
141
141
|
|
|
142
142
|
exports.defaultNotfoundHandler = defaultNotfoundHandler.defaultNotfoundHandler;
|
|
143
143
|
exports.defaultExtractContract = defaultExtractContract.defaultExtractContract;
|
|
144
|
+
exports.createHookHubLifeCycle = hooks.createHookHubLifeCycle;
|
|
144
145
|
exports.hookServerExitKind = hooks.hookServerExitKind;
|
|
145
146
|
exports.hookServerNextKind = hooks.hookServerNextKind;
|
|
146
147
|
exports.launchHookBeforeBuildRoute = hooks.launchHookBeforeBuildRoute;
|
package/dist/core/hub/index.mjs
CHANGED
|
@@ -5,7 +5,7 @@ import '../steps/index.mjs';
|
|
|
5
5
|
import { Request } from '../request.mjs';
|
|
6
6
|
import { defaultNotfoundHandler } from './defaultNotfoundHandler.mjs';
|
|
7
7
|
import { defaultExtractContract } from './defaultExtractContract.mjs';
|
|
8
|
-
export { hookServerExitKind, hookServerNextKind, launchHookBeforeBuildRoute, launchHookServer, launchHookServerError, serverErrorExitHookFunction, serverErrorNextHookFunction } from './hooks.mjs';
|
|
8
|
+
export { createHookHubLifeCycle, hookServerExitKind, hookServerNextKind, launchHookBeforeBuildRoute, launchHookServer, launchHookServerError, serverErrorExitHookFunction, serverErrorNextHookFunction } from './hooks.mjs';
|
|
9
9
|
import { createHandlerStep } from '../steps/handler.mjs';
|
|
10
10
|
|
|
11
11
|
const hubKind = createCoreLibKind("hub");
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
require('./hub/index.cjs');
|
|
4
|
+
var index = require('./router/index.cjs');
|
|
5
|
+
var utils = require('@duplojs/utils');
|
|
6
|
+
var hooks = require('./hub/hooks.cjs');
|
|
7
|
+
|
|
8
|
+
async function implementHttpServer(params, initHttpServer) {
|
|
9
|
+
const newHub1 = await hooks.launchHookServer(params.hub.aggregatesHooksHubLifeCycle("beforeServerBuildRoutes"), params.hub, params.httpServerParams);
|
|
10
|
+
const router = await index.buildRouter(newHub1);
|
|
11
|
+
const newHub2 = await hooks.launchHookServer(newHub1.aggregatesHooksHubLifeCycle("beforeStartServer"), newHub1, params.httpServerParams);
|
|
12
|
+
const serverErrorHooks = newHub1.aggregatesHooksHubLifeCycle("serverError");
|
|
13
|
+
function catchCriticalError(error) {
|
|
14
|
+
console.error("Critical Error :", error);
|
|
15
|
+
}
|
|
16
|
+
const execRouteSystem = (routerInitializationData, whenUncaughtError) => router
|
|
17
|
+
.exec(routerInitializationData)
|
|
18
|
+
.catch(async (error) => {
|
|
19
|
+
await hooks.launchHookServerError(serverErrorHooks, {
|
|
20
|
+
error,
|
|
21
|
+
exit: hooks.serverErrorExitHookFunction,
|
|
22
|
+
next: hooks.serverErrorNextHookFunction,
|
|
23
|
+
routerInitializationData: routerInitializationData,
|
|
24
|
+
}).catch(utils.forward);
|
|
25
|
+
await whenUncaughtError(error, routerInitializationData);
|
|
26
|
+
})
|
|
27
|
+
.catch(catchCriticalError);
|
|
28
|
+
const result = await initHttpServer({
|
|
29
|
+
execRouteSystem: execRouteSystem,
|
|
30
|
+
httpServerParams: params.httpServerParams,
|
|
31
|
+
});
|
|
32
|
+
await hooks.launchHookServer(newHub2.aggregatesHooksHubLifeCycle("afterStartServer"), newHub2, params.httpServerParams);
|
|
33
|
+
return result;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
exports.implementHttpServer = implementHttpServer;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type Hub, type HttpServerParams } from "./hub";
|
|
2
|
+
import { type RouterInitializationData } from "./router";
|
|
3
|
+
import { type MaybePromise } from "@duplojs/utils";
|
|
4
|
+
export interface ImplementHttpServerParams {
|
|
5
|
+
readonly hub: Hub;
|
|
6
|
+
readonly httpServerParams: HttpServerParams;
|
|
7
|
+
}
|
|
8
|
+
export type ExecRouteSystem = (routerInitializationData: RouterInitializationData, whenUncaughtError: (error: unknown, routerInitializationData: RouterInitializationData) => MaybePromise<void>) => Promise<void>;
|
|
9
|
+
export interface InitHttpServerParams {
|
|
10
|
+
readonly execRouteSystem: ExecRouteSystem;
|
|
11
|
+
readonly httpServerParams: HttpServerParams;
|
|
12
|
+
}
|
|
13
|
+
export declare function implementHttpServer<GenericServer extends unknown>(params: ImplementHttpServerParams, initHttpServer: (params: InitHttpServerParams) => MaybePromise<GenericServer>): Promise<GenericServer>;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import './hub/index.mjs';
|
|
2
|
+
import { buildRouter } from './router/index.mjs';
|
|
3
|
+
import { forward } from '@duplojs/utils';
|
|
4
|
+
import { launchHookServer, launchHookServerError, serverErrorNextHookFunction, serverErrorExitHookFunction } from './hub/hooks.mjs';
|
|
5
|
+
|
|
6
|
+
async function implementHttpServer(params, initHttpServer) {
|
|
7
|
+
const newHub1 = await launchHookServer(params.hub.aggregatesHooksHubLifeCycle("beforeServerBuildRoutes"), params.hub, params.httpServerParams);
|
|
8
|
+
const router = await buildRouter(newHub1);
|
|
9
|
+
const newHub2 = await launchHookServer(newHub1.aggregatesHooksHubLifeCycle("beforeStartServer"), newHub1, params.httpServerParams);
|
|
10
|
+
const serverErrorHooks = newHub1.aggregatesHooksHubLifeCycle("serverError");
|
|
11
|
+
function catchCriticalError(error) {
|
|
12
|
+
console.error("Critical Error :", error);
|
|
13
|
+
}
|
|
14
|
+
const execRouteSystem = (routerInitializationData, whenUncaughtError) => router
|
|
15
|
+
.exec(routerInitializationData)
|
|
16
|
+
.catch(async (error) => {
|
|
17
|
+
await launchHookServerError(serverErrorHooks, {
|
|
18
|
+
error,
|
|
19
|
+
exit: serverErrorExitHookFunction,
|
|
20
|
+
next: serverErrorNextHookFunction,
|
|
21
|
+
routerInitializationData: routerInitializationData,
|
|
22
|
+
}).catch(forward);
|
|
23
|
+
await whenUncaughtError(error, routerInitializationData);
|
|
24
|
+
})
|
|
25
|
+
.catch(catchCriticalError);
|
|
26
|
+
const result = await initHttpServer({
|
|
27
|
+
execRouteSystem: execRouteSystem,
|
|
28
|
+
httpServerParams: params.httpServerParams,
|
|
29
|
+
});
|
|
30
|
+
await launchHookServer(newHub2.aggregatesHooksHubLifeCycle("afterStartServer"), newHub2, params.httpServerParams);
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export { implementHttpServer };
|
package/dist/core/index.cjs
CHANGED
|
@@ -16,6 +16,8 @@ require('./functionsBuilders/index.cjs');
|
|
|
16
16
|
var index$3 = require('./router/index.cjs');
|
|
17
17
|
var stringIdentifier = require('./stringIdentifier.cjs');
|
|
18
18
|
require('./metadata/index.cjs');
|
|
19
|
+
var implementHttpServer = require('./implementHttpServer.cjs');
|
|
20
|
+
var narrowingInput = require('./narrowingInput.cjs');
|
|
19
21
|
var checker = require('./builders/checker.cjs');
|
|
20
22
|
var builder = require('./builders/route/builder.cjs');
|
|
21
23
|
var store = require('./builders/route/store.cjs');
|
|
@@ -71,6 +73,8 @@ exports.createHub = index$2.createHub;
|
|
|
71
73
|
exports.hubKind = index$2.hubKind;
|
|
72
74
|
exports.buildRouter = index$3.buildRouter;
|
|
73
75
|
exports.createCoreLibStringIdentifier = stringIdentifier.createCoreLibStringIdentifier;
|
|
76
|
+
exports.implementHttpServer = implementHttpServer.implementHttpServer;
|
|
77
|
+
exports.createNarrowingInput = narrowingInput.createNarrowingInput;
|
|
74
78
|
exports.checkerBuilder = checker.checkerBuilder;
|
|
75
79
|
exports.useCheckerBuilder = checker.useCheckerBuilder;
|
|
76
80
|
exports.routeBuilderHandler = builder.routeBuilderHandler;
|
|
@@ -105,6 +109,7 @@ exports.createProcessStep = process.createProcessStep;
|
|
|
105
109
|
exports.processStepKind = process.processStepKind;
|
|
106
110
|
exports.createPresetCheckerStep = presetChecker.createPresetCheckerStep;
|
|
107
111
|
exports.presetCheckerStepKind = presetChecker.presetCheckerStepKind;
|
|
112
|
+
exports.createHookHubLifeCycle = hooks$1.createHookHubLifeCycle;
|
|
108
113
|
exports.hookServerExitKind = hooks$1.hookServerExitKind;
|
|
109
114
|
exports.hookServerNextKind = hooks$1.hookServerNextKind;
|
|
110
115
|
exports.launchHookBeforeBuildRoute = hooks$1.launchHookBeforeBuildRoute;
|
package/dist/core/index.d.ts
CHANGED
package/dist/core/index.mjs
CHANGED
|
@@ -14,6 +14,8 @@ import './functionsBuilders/index.mjs';
|
|
|
14
14
|
export { buildRouter } from './router/index.mjs';
|
|
15
15
|
export { createCoreLibStringIdentifier } from './stringIdentifier.mjs';
|
|
16
16
|
import './metadata/index.mjs';
|
|
17
|
+
export { implementHttpServer } from './implementHttpServer.mjs';
|
|
18
|
+
export { createNarrowingInput } from './narrowingInput.mjs';
|
|
17
19
|
export { checkerBuilder, useCheckerBuilder } from './builders/checker.mjs';
|
|
18
20
|
export { routeBuilderHandler, useRouteBuilder } from './builders/route/builder.mjs';
|
|
19
21
|
export { routeStore } from './builders/route/store.mjs';
|
|
@@ -32,7 +34,7 @@ export { createCutStep, cutStepKind, cutStepOutputKind } from './steps/cut.mjs';
|
|
|
32
34
|
export { createHandlerStep, handlerStepKind } from './steps/handler.mjs';
|
|
33
35
|
export { createProcessStep, processStepKind } from './steps/process.mjs';
|
|
34
36
|
export { createPresetCheckerStep, presetCheckerStepKind } from './steps/presetChecker.mjs';
|
|
35
|
-
export { hookServerExitKind, hookServerNextKind, launchHookBeforeBuildRoute, launchHookServer, launchHookServerError, serverErrorExitHookFunction, serverErrorNextHookFunction } from './hub/hooks.mjs';
|
|
37
|
+
export { createHookHubLifeCycle, hookServerExitKind, hookServerNextKind, launchHookBeforeBuildRoute, launchHookServer, launchHookServerError, serverErrorExitHookFunction, serverErrorNextHookFunction } from './hub/hooks.mjs';
|
|
36
38
|
export { defaultNotfoundHandler } from './hub/defaultNotfoundHandler.mjs';
|
|
37
39
|
export { defaultExtractContract } from './hub/defaultExtractContract.mjs';
|
|
38
40
|
export { buildRouteFunction } from './functionsBuilders/route/build.mjs';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type SimplifyTopLevel, type AnyFunction, type ObjectKey } from "@duplojs/utils";
|
|
2
|
+
export interface NarrowingInput<N extends ObjectKey = ObjectKey, V extends unknown = unknown> {
|
|
3
|
+
inputName: N;
|
|
4
|
+
value: V;
|
|
5
|
+
}
|
|
6
|
+
export type ShrinkerInput<T extends object = object> = SimplifyTopLevel<{
|
|
7
|
+
[P in keyof T]: (value: T[P]) => NarrowingInput<P, T[P]>;
|
|
8
|
+
}>;
|
|
9
|
+
export type GetNarrowingInput<I extends ShrinkerInput, N extends keyof I = keyof I> = ReturnType<I[N] extends AnyFunction ? I[N] : never>;
|
|
10
|
+
export declare function createNarrowingInput<T extends object>(): ShrinkerInput<T>;
|
|
@@ -2,7 +2,6 @@ import { type Request } from "../request";
|
|
|
2
2
|
import { type UnionToIntersection, type AnyFunction, type Kind, type MaybePromise, type SimplifyTopLevel, type IsEqual } from "@duplojs/utils";
|
|
3
3
|
import { type HookResponse } from "../response";
|
|
4
4
|
import { type ResponseCode, type Response } from "../response";
|
|
5
|
-
export * from "../response";
|
|
6
5
|
export interface HookParamsOnConstructRequest {
|
|
7
6
|
request: Request;
|
|
8
7
|
addRequestProperties<GenericNewProperties extends Record<string, unknown>>(newProperties: GenericNewProperties): Request & GenericNewProperties;
|
|
@@ -2,8 +2,9 @@ import { type createStepFunctionBuilder, type createRouteFunctionBuilder } from
|
|
|
2
2
|
import { type HookHubLifeCycle } from "../../hub";
|
|
3
3
|
import { type RequestInitializationData } from "../../request";
|
|
4
4
|
import { type HookRouteLifeCycle, type Route, type RouteDefinition } from "../../route";
|
|
5
|
+
export type RouterInitializationData = Omit<RequestInitializationData, "matchedPath" | "params" | "path" | "query">;
|
|
5
6
|
export interface BuildedRouter {
|
|
6
|
-
exec(initializationData:
|
|
7
|
+
exec(initializationData: RouterInitializationData): Promise<void>;
|
|
7
8
|
readonly routes: readonly Route<RouteDefinition>[];
|
|
8
9
|
readonly hooksRouteLifeCycle: readonly HookRouteLifeCycle[];
|
|
9
10
|
readonly routeFunctionBuilders: readonly ReturnType<typeof createRouteFunctionBuilder>[];
|
|
@@ -1,52 +1,25 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
require('../../core/hub/index.cjs');
|
|
4
|
-
var index = require('../../core/router/index.cjs');
|
|
5
3
|
var utils = require('@duplojs/utils');
|
|
6
4
|
var http = require('http');
|
|
7
5
|
var https = require('https');
|
|
8
|
-
var hooks
|
|
9
|
-
var
|
|
6
|
+
var hooks = require('./hooks.cjs');
|
|
7
|
+
var implementHttpServer = require('../../core/implementHttpServer.cjs');
|
|
10
8
|
|
|
11
|
-
|
|
12
|
-
const httpServerParams = {
|
|
13
|
-
|
|
9
|
+
function createHttpServer(inputHub, params) {
|
|
10
|
+
const httpServerParams = utils.O.override({
|
|
11
|
+
host: "localhost",
|
|
12
|
+
port: 80,
|
|
14
13
|
maxBodySize: "50mb",
|
|
15
14
|
informationHeaderKey: "information",
|
|
16
15
|
predictedHeaderKey: "predicted",
|
|
17
16
|
fromHookHeaderKey: "from-hook",
|
|
18
17
|
interface: "node",
|
|
19
|
-
};
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
process.exit(0);
|
|
25
|
-
}
|
|
26
|
-
const server = params.https
|
|
27
|
-
? https.createServer(params.https)
|
|
28
|
-
: http.createServer(params.http ?? {});
|
|
29
|
-
const serverErrorHooks = newHub1.aggregatesHooksHubLifeCycle("serverError");
|
|
30
|
-
server.addListener("request", (serverRequest, serverResponse) => router
|
|
31
|
-
.exec({
|
|
32
|
-
method: serverRequest.method ?? "",
|
|
33
|
-
headers: serverRequest.headers,
|
|
34
|
-
host: serverRequest.headers.host ?? "",
|
|
35
|
-
origin: serverRequest.headers.origin ?? "",
|
|
36
|
-
url: serverRequest.url ?? "",
|
|
37
|
-
raw: {
|
|
38
|
-
request: serverRequest,
|
|
39
|
-
response: serverResponse,
|
|
40
|
-
},
|
|
41
|
-
})
|
|
42
|
-
.catch(async (error) => {
|
|
43
|
-
await hooks.launchHookServerError(serverErrorHooks, {
|
|
44
|
-
error,
|
|
45
|
-
exit: hooks.serverErrorExitHookFunction,
|
|
46
|
-
next: hooks.serverErrorNextHookFunction,
|
|
47
|
-
serverRequest,
|
|
48
|
-
serverResponse,
|
|
49
|
-
}).catch(utils.forward);
|
|
18
|
+
}, params);
|
|
19
|
+
const hooks$1 = hooks.makeNodeHook(inputHub, httpServerParams);
|
|
20
|
+
const hub = inputHub.addRouteHooks(hooks$1);
|
|
21
|
+
function whenUncaughtError(error, routerInitializationData) {
|
|
22
|
+
const serverResponse = routerInitializationData.raw.response;
|
|
50
23
|
if (!serverResponse.headersSent && !serverResponse.writableEnded) {
|
|
51
24
|
serverResponse.writeHead(500, {
|
|
52
25
|
[httpServerParams.informationHeaderKey]: "critical-server-error",
|
|
@@ -59,15 +32,32 @@ async function createHttpServer(inputHub, params) {
|
|
|
59
32
|
if (!serverResponse.writableEnded) {
|
|
60
33
|
serverResponse.end();
|
|
61
34
|
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
35
|
+
}
|
|
36
|
+
return implementHttpServer.implementHttpServer({
|
|
37
|
+
hub,
|
|
38
|
+
httpServerParams,
|
|
39
|
+
}, ({ httpServerParams, execRouteSystem }) => {
|
|
40
|
+
const server = httpServerParams.https
|
|
41
|
+
? https.createServer(httpServerParams.https)
|
|
42
|
+
: http.createServer(httpServerParams.http ?? {});
|
|
43
|
+
server.addListener("request", (serverRequest, serverResponse) => execRouteSystem({
|
|
44
|
+
method: serverRequest.method ?? "",
|
|
45
|
+
headers: serverRequest.headers,
|
|
46
|
+
host: serverRequest.headers.host ?? "",
|
|
47
|
+
origin: serverRequest.headers.origin ?? "",
|
|
48
|
+
url: serverRequest.url ?? "",
|
|
49
|
+
raw: {
|
|
50
|
+
request: serverRequest,
|
|
51
|
+
response: serverResponse,
|
|
52
|
+
},
|
|
53
|
+
}, whenUncaughtError));
|
|
54
|
+
return new Promise((resolve) => {
|
|
55
|
+
server.listen({
|
|
56
|
+
port: httpServerParams.port,
|
|
57
|
+
host: httpServerParams.host,
|
|
58
|
+
}, () => void resolve(server));
|
|
59
|
+
});
|
|
68
60
|
});
|
|
69
|
-
await hooks.launchHookServer(newHub2.aggregatesHooksHubLifeCycle("afterStartServer"), newHub2, httpServerParams);
|
|
70
|
-
return server;
|
|
71
61
|
}
|
|
72
62
|
|
|
73
63
|
exports.createHttpServer = createHttpServer;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type HttpServerParams, type Hub } from "../../core/hub";
|
|
2
2
|
import { type Hosts } from "./types/host";
|
|
3
|
-
import { type
|
|
3
|
+
import { type BytesInString, O } from "@duplojs/utils";
|
|
4
4
|
import http from "http";
|
|
5
5
|
import https from "https";
|
|
6
6
|
declare module "../../core/hub" {
|
|
@@ -15,10 +15,6 @@ declare module "../../core/hub" {
|
|
|
15
15
|
readonly http?: http.ServerOptions;
|
|
16
16
|
readonly https?: https.ServerOptions;
|
|
17
17
|
}
|
|
18
|
-
interface HttpServerErrorParams {
|
|
19
|
-
readonly serverRequest: http.IncomingMessage;
|
|
20
|
-
readonly serverResponse: http.ServerResponse;
|
|
21
|
-
}
|
|
22
18
|
}
|
|
23
19
|
export type CreateHttpServerParams = O.PartialKeys<Omit<HttpServerParams, "interface">, "maxBodySize" | "informationHeaderKey" | "predictedHeaderKey" | "fromHookHeaderKey">;
|
|
24
20
|
export declare function createHttpServer(inputHub: Hub, params: CreateHttpServerParams): Promise<https.Server<typeof http.IncomingMessage, typeof http.ServerResponse> | http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>>;
|
|
@@ -1,50 +1,23 @@
|
|
|
1
|
-
import '
|
|
2
|
-
import { buildRouter } from '../../core/router/index.mjs';
|
|
3
|
-
import { forward } from '@duplojs/utils';
|
|
1
|
+
import { O } from '@duplojs/utils';
|
|
4
2
|
import http from 'http';
|
|
5
3
|
import https from 'https';
|
|
6
4
|
import { makeNodeHook } from './hooks.mjs';
|
|
7
|
-
import {
|
|
5
|
+
import { implementHttpServer } from '../../core/implementHttpServer.mjs';
|
|
8
6
|
|
|
9
|
-
|
|
10
|
-
const httpServerParams = {
|
|
11
|
-
|
|
7
|
+
function createHttpServer(inputHub, params) {
|
|
8
|
+
const httpServerParams = O.override({
|
|
9
|
+
host: "localhost",
|
|
10
|
+
port: 80,
|
|
12
11
|
maxBodySize: "50mb",
|
|
13
12
|
informationHeaderKey: "information",
|
|
14
13
|
predictedHeaderKey: "predicted",
|
|
15
14
|
fromHookHeaderKey: "from-hook",
|
|
16
15
|
interface: "node",
|
|
17
|
-
};
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
process.exit(0);
|
|
23
|
-
}
|
|
24
|
-
const server = params.https
|
|
25
|
-
? https.createServer(params.https)
|
|
26
|
-
: http.createServer(params.http ?? {});
|
|
27
|
-
const serverErrorHooks = newHub1.aggregatesHooksHubLifeCycle("serverError");
|
|
28
|
-
server.addListener("request", (serverRequest, serverResponse) => router
|
|
29
|
-
.exec({
|
|
30
|
-
method: serverRequest.method ?? "",
|
|
31
|
-
headers: serverRequest.headers,
|
|
32
|
-
host: serverRequest.headers.host ?? "",
|
|
33
|
-
origin: serverRequest.headers.origin ?? "",
|
|
34
|
-
url: serverRequest.url ?? "",
|
|
35
|
-
raw: {
|
|
36
|
-
request: serverRequest,
|
|
37
|
-
response: serverResponse,
|
|
38
|
-
},
|
|
39
|
-
})
|
|
40
|
-
.catch(async (error) => {
|
|
41
|
-
await launchHookServerError(serverErrorHooks, {
|
|
42
|
-
error,
|
|
43
|
-
exit: serverErrorExitHookFunction,
|
|
44
|
-
next: serverErrorNextHookFunction,
|
|
45
|
-
serverRequest,
|
|
46
|
-
serverResponse,
|
|
47
|
-
}).catch(forward);
|
|
16
|
+
}, params);
|
|
17
|
+
const hooks = makeNodeHook(inputHub, httpServerParams);
|
|
18
|
+
const hub = inputHub.addRouteHooks(hooks);
|
|
19
|
+
function whenUncaughtError(error, routerInitializationData) {
|
|
20
|
+
const serverResponse = routerInitializationData.raw.response;
|
|
48
21
|
if (!serverResponse.headersSent && !serverResponse.writableEnded) {
|
|
49
22
|
serverResponse.writeHead(500, {
|
|
50
23
|
[httpServerParams.informationHeaderKey]: "critical-server-error",
|
|
@@ -57,15 +30,32 @@ async function createHttpServer(inputHub, params) {
|
|
|
57
30
|
if (!serverResponse.writableEnded) {
|
|
58
31
|
serverResponse.end();
|
|
59
32
|
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
33
|
+
}
|
|
34
|
+
return implementHttpServer({
|
|
35
|
+
hub,
|
|
36
|
+
httpServerParams,
|
|
37
|
+
}, ({ httpServerParams, execRouteSystem }) => {
|
|
38
|
+
const server = httpServerParams.https
|
|
39
|
+
? https.createServer(httpServerParams.https)
|
|
40
|
+
: http.createServer(httpServerParams.http ?? {});
|
|
41
|
+
server.addListener("request", (serverRequest, serverResponse) => execRouteSystem({
|
|
42
|
+
method: serverRequest.method ?? "",
|
|
43
|
+
headers: serverRequest.headers,
|
|
44
|
+
host: serverRequest.headers.host ?? "",
|
|
45
|
+
origin: serverRequest.headers.origin ?? "",
|
|
46
|
+
url: serverRequest.url ?? "",
|
|
47
|
+
raw: {
|
|
48
|
+
request: serverRequest,
|
|
49
|
+
response: serverResponse,
|
|
50
|
+
},
|
|
51
|
+
}, whenUncaughtError));
|
|
52
|
+
return new Promise((resolve) => {
|
|
53
|
+
server.listen({
|
|
54
|
+
port: httpServerParams.port,
|
|
55
|
+
host: httpServerParams.host,
|
|
56
|
+
}, () => void resolve(server));
|
|
57
|
+
});
|
|
66
58
|
});
|
|
67
|
-
await launchHookServer(newHub2.aggregatesHooksHubLifeCycle("afterStartServer"), newHub2, httpServerParams);
|
|
68
|
-
return server;
|
|
69
59
|
}
|
|
70
60
|
|
|
71
61
|
export { createHttpServer };
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
require('../../core/route/index.cjs');
|
|
4
4
|
var utils = require('@duplojs/utils');
|
|
5
5
|
require('./error/index.cjs');
|
|
6
|
+
require('../../core/response/index.cjs');
|
|
6
7
|
var hooks = require('../../core/route/hooks.cjs');
|
|
7
8
|
var predicted = require('../../core/response/predicted.cjs');
|
|
8
9
|
var hook = require('../../core/response/hook.cjs');
|
|
@@ -80,15 +81,17 @@ function makeNodeHook(hub, serverParams) {
|
|
|
80
81
|
return exit();
|
|
81
82
|
},
|
|
82
83
|
beforeSendResponse({ request, currentResponse, exit }) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
84
|
+
if (!currentResponse.headers?.["content-type"]) {
|
|
85
|
+
const body = currentResponse.body;
|
|
86
|
+
if (typeof body === "string"
|
|
87
|
+
|| body instanceof Error) {
|
|
88
|
+
currentResponse.setHeader("content-type", "text/plain; charset=utf-8");
|
|
89
|
+
}
|
|
90
|
+
else if (typeof body === "object"
|
|
91
|
+
|| typeof body === "number"
|
|
92
|
+
|| typeof body === "boolean") {
|
|
93
|
+
currentResponse.setHeader("content-type", "application/json; charset=utf-8");
|
|
94
|
+
}
|
|
92
95
|
}
|
|
93
96
|
currentResponse.setHeader(informationHeaderKey, currentResponse.information);
|
|
94
97
|
if (currentResponse instanceof predicted.PredictedResponse) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type HttpServerParams, type Hub } from "../../core/hub";
|
|
2
|
-
import { HookResponse } from "../../core/
|
|
2
|
+
import { HookResponse } from "../../core/response";
|
|
3
3
|
export declare function makeNodeHook(hub: Hub, serverParams: HttpServerParams): {
|
|
4
4
|
parseBody({ request, exit }: import("../../core/route").RouteHookParams<import("../../core/request").Request>): Promise<import("../../core/route").RouteHookExit>;
|
|
5
5
|
error({ error, response, exit }: import("../../core/route").RouteHookErrorParams<import("../../core/request").Request>): import("../../core/route").RouteHookExit | HookResponse<"400", "body-size-exceeds-limit-error", unknown> | HookResponse<"400", "body-parse-wrong-chunk-received", unknown> | HookResponse<"400", "body-parse-unknown-error", unknown>;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import '../../core/route/index.mjs';
|
|
2
2
|
import { stringToBytes } from '@duplojs/utils';
|
|
3
3
|
import './error/index.mjs';
|
|
4
|
+
import '../../core/response/index.mjs';
|
|
4
5
|
import { createHookRouteLifeCycle } from '../../core/route/hooks.mjs';
|
|
5
6
|
import { PredictedResponse } from '../../core/response/predicted.mjs';
|
|
6
7
|
import { HookResponse } from '../../core/response/hook.mjs';
|
|
@@ -78,15 +79,17 @@ function makeNodeHook(hub, serverParams) {
|
|
|
78
79
|
return exit();
|
|
79
80
|
},
|
|
80
81
|
beforeSendResponse({ request, currentResponse, exit }) {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
82
|
+
if (!currentResponse.headers?.["content-type"]) {
|
|
83
|
+
const body = currentResponse.body;
|
|
84
|
+
if (typeof body === "string"
|
|
85
|
+
|| body instanceof Error) {
|
|
86
|
+
currentResponse.setHeader("content-type", "text/plain; charset=utf-8");
|
|
87
|
+
}
|
|
88
|
+
else if (typeof body === "object"
|
|
89
|
+
|| typeof body === "number"
|
|
90
|
+
|| typeof body === "boolean") {
|
|
91
|
+
currentResponse.setHeader("content-type", "application/json; charset=utf-8");
|
|
92
|
+
}
|
|
90
93
|
}
|
|
91
94
|
currentResponse.setHeader(informationHeaderKey, currentResponse.information);
|
|
92
95
|
if (currentResponse instanceof PredictedResponse) {
|
|
@@ -30,6 +30,9 @@ function codeGeneratorPlugin(pluginParams) {
|
|
|
30
30
|
hooksHubLifeCycle: [
|
|
31
31
|
{
|
|
32
32
|
beforeStartServer: async (hub) => {
|
|
33
|
+
if (!utils.equal(hub.config.environment, ["DEV", "BUILD"])) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
33
36
|
const routes = hub.aggregatesRoutes();
|
|
34
37
|
const dataParserRoutes = utils.A.flatMap(routes, (route) => routeToDataParser.routeToDataParser(route, {
|
|
35
38
|
defaultExtractContract: hub.defaultExtractContract,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as DataParserToTypescript from '@duplojs/data-parser-tools/toTypescript';
|
|
2
|
-
import { A, DP } from '@duplojs/utils';
|
|
2
|
+
import { equal, A, DP } from '@duplojs/utils';
|
|
3
3
|
import { routeToDataParser } from './routeToDataParser.mjs';
|
|
4
4
|
import { writeFile } from 'node:fs/promises';
|
|
5
5
|
|
|
@@ -9,6 +9,9 @@ function codeGeneratorPlugin(pluginParams) {
|
|
|
9
9
|
hooksHubLifeCycle: [
|
|
10
10
|
{
|
|
11
11
|
beforeStartServer: async (hub) => {
|
|
12
|
+
if (!equal(hub.config.environment, ["DEV", "BUILD"])) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
12
15
|
const routes = hub.aggregatesRoutes();
|
|
13
16
|
const dataParserRoutes = A.flatMap(routes, (route) => routeToDataParser(route, {
|
|
14
17
|
defaultExtractContract: hub.defaultExtractContract,
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
require('../../core/builders/index.cjs');
|
|
4
|
+
require('../../core/metadata/index.cjs');
|
|
4
5
|
require('../../core/response/index.cjs');
|
|
5
6
|
var utils = require('@duplojs/utils');
|
|
6
7
|
var builder = require('../../core/builders/route/builder.cjs');
|
|
8
|
+
var ignoreByRouteStore = require('../../core/metadata/ignoreByRouteStore.cjs');
|
|
7
9
|
var contract = require('../../core/response/contract.cjs');
|
|
8
10
|
|
|
9
11
|
function makeOpenApiRoute(routePath, openApiPage) {
|
|
10
|
-
return builder.useRouteBuilder("GET", routePath)
|
|
12
|
+
return builder.useRouteBuilder("GET", routePath, { metadata: [ignoreByRouteStore.IgnoreByRouteStoreMetadata()] })
|
|
11
13
|
.handler(contract.ResponseContract.ok("swaggerUi", utils.DP.string()), (__, { response }) => response("swaggerUi", openApiPage)
|
|
12
14
|
.setHeader("content-type", "text/html"));
|
|
13
15
|
}
|
|
@@ -3,7 +3,7 @@ import type { RoutePath } from "../../core/route";
|
|
|
3
3
|
import { DP } from "@duplojs/utils";
|
|
4
4
|
export declare function makeOpenApiRoute(routePath: RoutePath, openApiPage: string): import("../../core/route").Route<{
|
|
5
5
|
readonly method: "GET";
|
|
6
|
-
readonly metadata: readonly [];
|
|
6
|
+
readonly metadata: readonly [import("../../core/metadata").Metadata<"ignore-by-route-store", unknown>];
|
|
7
7
|
readonly hooks: readonly [];
|
|
8
8
|
readonly preflightSteps: readonly [];
|
|
9
9
|
readonly paths: readonly [`/${string}`];
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import '../../core/builders/index.mjs';
|
|
2
|
+
import '../../core/metadata/index.mjs';
|
|
2
3
|
import '../../core/response/index.mjs';
|
|
3
4
|
import { DP } from '@duplojs/utils';
|
|
4
5
|
import { useRouteBuilder } from '../../core/builders/route/builder.mjs';
|
|
6
|
+
import { IgnoreByRouteStoreMetadata } from '../../core/metadata/ignoreByRouteStore.mjs';
|
|
5
7
|
import { ResponseContract } from '../../core/response/contract.mjs';
|
|
6
8
|
|
|
7
9
|
function makeOpenApiRoute(routePath, openApiPage) {
|
|
8
|
-
return useRouteBuilder("GET", routePath)
|
|
10
|
+
return useRouteBuilder("GET", routePath, { metadata: [IgnoreByRouteStoreMetadata()] })
|
|
9
11
|
.handler(ResponseContract.ok("swaggerUi", DP.string()), (__, { response }) => response("swaggerUi", openApiPage)
|
|
10
12
|
.setHeader("content-type", "text/html"));
|
|
11
13
|
}
|
|
@@ -12,6 +12,11 @@ function openApiGeneratorPlugin(pluginParams) {
|
|
|
12
12
|
hooksHubLifeCycle: [
|
|
13
13
|
{
|
|
14
14
|
beforeServerBuildRoutes: async (hub) => {
|
|
15
|
+
if (!utils.equal(hub.config.environment, ["DEV", "BUILD"])
|
|
16
|
+
|| (!pluginParams.routePath
|
|
17
|
+
&& !pluginParams.outputFile)) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
15
20
|
const contextToJsonSchemaFactory = new Map();
|
|
16
21
|
const resultSchemaContext = new Map();
|
|
17
22
|
const routes = hub.aggregatesRoutes();
|
|
@@ -71,16 +76,19 @@ function openApiGeneratorPlugin(pluginParams) {
|
|
|
71
76
|
: undefined,
|
|
72
77
|
};
|
|
73
78
|
const openApiDocumentString = JSON.stringify(openApiDocument, null, 2);
|
|
74
|
-
if (pluginParams.
|
|
75
|
-
await promises.writeFile(pluginParams.
|
|
79
|
+
if (pluginParams.outputFile) {
|
|
80
|
+
await promises.writeFile(pluginParams.outputFile, openApiDocumentString);
|
|
81
|
+
}
|
|
82
|
+
if (pluginParams.routePath) {
|
|
83
|
+
const openApiPage = makeOpenApiPage.makeOpenApiPage({
|
|
84
|
+
openApiDocument: openApiDocumentString,
|
|
85
|
+
pageTitle: pluginParams.title ?? "Swagger API",
|
|
86
|
+
swaggerUiVersion: pluginParams.swaggerUiVersion ?? "5.31.0",
|
|
87
|
+
});
|
|
88
|
+
const openApiRoute = makeOpenApiRoute.makeOpenApiRoute(pluginParams.routePath, openApiPage);
|
|
89
|
+
return hub.register(openApiRoute);
|
|
76
90
|
}
|
|
77
|
-
|
|
78
|
-
openApiDocument: openApiDocumentString,
|
|
79
|
-
pageTitle: pluginParams.title ?? "Swagger API",
|
|
80
|
-
swaggerUiVersion: pluginParams.swaggerUiVersion ?? "5.31.0",
|
|
81
|
-
});
|
|
82
|
-
const openApiRoute = makeOpenApiRoute.makeOpenApiRoute(pluginParams.routePath, openApiPage);
|
|
83
|
-
return hub.register(openApiRoute);
|
|
91
|
+
return;
|
|
84
92
|
},
|
|
85
93
|
},
|
|
86
94
|
],
|
|
@@ -14,8 +14,8 @@ interface OpenApiSecurityOptionBasic {
|
|
|
14
14
|
type: "basic";
|
|
15
15
|
}
|
|
16
16
|
export interface OpenApiGeneratorPluginParams {
|
|
17
|
-
routePath
|
|
18
|
-
|
|
17
|
+
routePath?: RoutePath;
|
|
18
|
+
outputFile?: string;
|
|
19
19
|
/**
|
|
20
20
|
* @default "Swagger API"
|
|
21
21
|
*/
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { routeToOpenApi } from './routeToOpenApi.mjs';
|
|
2
|
-
import { pipe, A, O, G, P, justReturn } from '@duplojs/utils';
|
|
2
|
+
import { equal, pipe, A, O, G, P, justReturn } from '@duplojs/utils';
|
|
3
3
|
import { makeOpenApiPage } from './makeOpenApiPage.mjs';
|
|
4
4
|
import { makeOpenApiRoute } from './makeOpenApiRoute.mjs';
|
|
5
5
|
import { writeFile } from 'fs/promises';
|
|
@@ -10,6 +10,11 @@ function openApiGeneratorPlugin(pluginParams) {
|
|
|
10
10
|
hooksHubLifeCycle: [
|
|
11
11
|
{
|
|
12
12
|
beforeServerBuildRoutes: async (hub) => {
|
|
13
|
+
if (!equal(hub.config.environment, ["DEV", "BUILD"])
|
|
14
|
+
|| (!pluginParams.routePath
|
|
15
|
+
&& !pluginParams.outputFile)) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
13
18
|
const contextToJsonSchemaFactory = new Map();
|
|
14
19
|
const resultSchemaContext = new Map();
|
|
15
20
|
const routes = hub.aggregatesRoutes();
|
|
@@ -69,16 +74,19 @@ function openApiGeneratorPlugin(pluginParams) {
|
|
|
69
74
|
: undefined,
|
|
70
75
|
};
|
|
71
76
|
const openApiDocumentString = JSON.stringify(openApiDocument, null, 2);
|
|
72
|
-
if (pluginParams.
|
|
73
|
-
await writeFile(pluginParams.
|
|
77
|
+
if (pluginParams.outputFile) {
|
|
78
|
+
await writeFile(pluginParams.outputFile, openApiDocumentString);
|
|
79
|
+
}
|
|
80
|
+
if (pluginParams.routePath) {
|
|
81
|
+
const openApiPage = makeOpenApiPage({
|
|
82
|
+
openApiDocument: openApiDocumentString,
|
|
83
|
+
pageTitle: pluginParams.title ?? "Swagger API",
|
|
84
|
+
swaggerUiVersion: pluginParams.swaggerUiVersion ?? "5.31.0",
|
|
85
|
+
});
|
|
86
|
+
const openApiRoute = makeOpenApiRoute(pluginParams.routePath, openApiPage);
|
|
87
|
+
return hub.register(openApiRoute);
|
|
74
88
|
}
|
|
75
|
-
|
|
76
|
-
openApiDocument: openApiDocumentString,
|
|
77
|
-
pageTitle: pluginParams.title ?? "Swagger API",
|
|
78
|
-
swaggerUiVersion: pluginParams.swaggerUiVersion ?? "5.31.0",
|
|
79
|
-
});
|
|
80
|
-
const openApiRoute = makeOpenApiRoute(pluginParams.routePath, openApiPage);
|
|
81
|
-
return hub.register(openApiRoute);
|
|
89
|
+
return;
|
|
82
90
|
},
|
|
83
91
|
},
|
|
84
92
|
],
|