@duplojs/http 0.6.1 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client/getBody.cjs +1 -1
- package/dist/client/getBody.mjs +1 -1
- package/dist/client/hooks.d.ts +1 -25
- package/dist/client/httpClient.d.ts +29 -25
- package/dist/client/promiseRequest.cjs +3 -0
- package/dist/client/promiseRequest.d.ts +31 -39
- package/dist/client/promiseRequest.mjs +4 -1
- package/dist/client/types/clientResponse.d.ts +6 -6
- package/dist/client/types/hooks.d.ts +25 -0
- package/dist/client/types/index.cjs +2 -0
- package/dist/client/types/index.d.ts +2 -0
- package/dist/client/types/index.mjs +2 -0
- package/dist/client/types/promiseRequestParams.cjs +2 -0
- package/dist/client/types/promiseRequestParams.d.ts +9 -0
- package/dist/client/types/promiseRequestParams.mjs +1 -0
- package/dist/client/types/serverRoute.d.ts +19 -1
- package/dist/client/unexpectedResponseError.d.ts +1 -2
- package/dist/core/builders/preflight/route.cjs +1 -0
- package/dist/core/builders/preflight/route.d.ts +4 -2
- package/dist/core/builders/preflight/route.mjs +1 -0
- package/dist/core/builders/route/builder.cjs +1 -0
- package/dist/core/builders/route/builder.d.ts +4 -2
- package/dist/core/builders/route/builder.mjs +1 -0
- package/dist/core/clean/constraint.cjs +24 -0
- package/dist/core/clean/constraint.d.ts +8 -0
- package/dist/core/clean/constraint.mjs +22 -0
- package/dist/core/clean/constraintsSet.cjs +27 -0
- package/dist/core/clean/constraintsSet.d.ts +8 -0
- package/dist/core/clean/constraintsSet.mjs +25 -0
- package/dist/core/clean/entity.cjs +33 -0
- package/dist/core/clean/entity.d.ts +20 -0
- package/dist/core/clean/entity.mjs +31 -0
- package/dist/core/clean/index.cjs +8 -0
- package/dist/core/clean/index.d.ts +5 -0
- package/dist/core/clean/index.mjs +5 -0
- package/dist/core/clean/newType.cjs +15 -0
- package/dist/core/clean/newType.d.ts +8 -0
- package/dist/core/clean/newType.mjs +13 -0
- package/dist/core/clean/primitive.cjs +12 -0
- package/dist/core/clean/primitive.d.ts +8 -0
- package/dist/core/clean/primitive.mjs +10 -0
- package/dist/core/defaultHooks/index.cjs +50 -0
- package/dist/core/defaultHooks/index.d.ts +5 -0
- package/dist/core/defaultHooks/index.mjs +48 -0
- package/dist/{interfaces/node/error → core/errors}/bodyParseWrongChunkReceived.cjs +6 -4
- package/dist/core/errors/bodyParseWrongChunkReceived.d.ts +9 -0
- package/dist/core/errors/bodyParseWrongChunkReceived.mjs +14 -0
- package/dist/{interfaces/node/error → core/errors}/bodySizeExceedsLimitError.cjs +2 -2
- package/dist/{interfaces/node/error → core/errors}/bodySizeExceedsLimitError.d.ts +2 -2
- package/dist/{interfaces/node/error → core/errors}/bodySizeExceedsLimitError.mjs +2 -2
- package/dist/{interfaces/node/error → core/errors}/index.cjs +6 -4
- package/dist/{interfaces/node/error → core/errors}/index.d.ts +3 -2
- package/dist/{interfaces/node/error → core/errors}/index.mjs +3 -2
- package/dist/core/errors/parseJsonError.cjs +16 -0
- package/dist/core/errors/parseJsonError.d.ts +9 -0
- package/dist/core/errors/parseJsonError.mjs +14 -0
- package/dist/core/errors/wrongContentTypeError.cjs +16 -0
- package/dist/core/errors/wrongContentTypeError.d.ts +9 -0
- package/dist/core/errors/wrongContentTypeError.mjs +14 -0
- package/dist/core/functionsBuilders/route/create.d.ts +2 -2
- package/dist/core/functionsBuilders/route/default.cjs +0 -11
- package/dist/core/functionsBuilders/route/default.mjs +0 -11
- package/dist/core/functionsBuilders/route/hook.d.ts +2 -2
- package/dist/core/functionsBuilders/steps/create.d.ts +2 -2
- package/dist/core/functionsBuilders/steps/defaults/cutStep.cjs +15 -18
- package/dist/core/functionsBuilders/steps/defaults/cutStep.mjs +15 -18
- package/dist/core/functionsBuilders/steps/defaults/extractStep.cjs +43 -19
- package/dist/core/functionsBuilders/steps/defaults/extractStep.d.ts +2 -1
- package/dist/core/functionsBuilders/steps/defaults/extractStep.mjs +44 -20
- package/dist/core/functionsBuilders/steps/defaults/handlerStep.cjs +14 -8
- package/dist/core/functionsBuilders/steps/defaults/handlerStep.mjs +14 -8
- package/dist/core/hub/defaultBodyController.cjs +8 -0
- package/dist/core/hub/defaultBodyController.d.ts +1 -0
- package/dist/core/hub/defaultBodyController.mjs +6 -0
- package/dist/core/hub/hooks.cjs +3 -1
- package/dist/core/hub/hooks.d.ts +2 -3
- package/dist/core/hub/hooks.mjs +3 -1
- package/dist/core/hub/index.cjs +101 -127
- package/dist/core/hub/index.d.ts +33 -34
- package/dist/core/hub/index.mjs +100 -128
- package/dist/core/implementHttpServer.cjs +5 -5
- package/dist/core/implementHttpServer.d.ts +2 -1
- package/dist/core/implementHttpServer.mjs +5 -5
- package/dist/core/index.cjs +35 -10
- package/dist/core/index.d.ts +3 -0
- package/dist/core/index.mjs +14 -2
- package/dist/core/request/bodyController/base.cjs +36 -0
- package/dist/core/request/bodyController/base.d.ts +28 -0
- package/dist/core/request/bodyController/base.mjs +34 -0
- package/dist/core/request/bodyController/formData.cjs +28 -0
- package/dist/core/request/bodyController/formData.d.ts +22 -0
- package/dist/core/request/bodyController/formData.mjs +25 -0
- package/dist/core/request/bodyController/index.cjs +13 -0
- package/dist/core/request/bodyController/index.d.ts +3 -0
- package/dist/core/request/bodyController/index.mjs +3 -0
- package/dist/core/request/bodyController/text.cjs +14 -0
- package/dist/core/request/bodyController/text.d.ts +10 -0
- package/dist/core/request/bodyController/text.mjs +11 -0
- package/dist/core/{request.cjs → request/index.cjs} +21 -3
- package/dist/core/{request.d.ts → request/index.d.ts} +10 -3
- package/dist/core/request/index.mjs +50 -0
- package/dist/core/response/contract.d.ts +1 -1
- package/dist/core/route/hooks.d.ts +0 -2
- package/dist/core/route/index.d.ts +2 -1
- package/dist/core/router/index.cjs +27 -8
- package/dist/core/router/index.d.ts +2 -1
- package/dist/core/router/index.mjs +28 -10
- package/dist/core/router/notFoundBodyReaderImplementationError.cjs +16 -0
- package/dist/core/router/notFoundBodyReaderImplementationError.d.ts +11 -0
- package/dist/core/router/notFoundBodyReaderImplementationError.mjs +14 -0
- package/dist/core/router/types/buildedRouter.d.ts +3 -3
- package/dist/core/steps/extract.d.ts +3 -1
- package/dist/core/steps/types/steps.d.ts +1 -3
- package/dist/core/types/hosts.cjs +2 -0
- package/dist/core/types/hosts.d.ts +4 -0
- package/dist/core/types/hosts.mjs +1 -0
- package/dist/core/types/httpServerParams.cjs +2 -0
- package/dist/core/types/httpServerParams.d.ts +11 -0
- package/dist/core/types/httpServerParams.mjs +1 -0
- package/dist/core/types/index.cjs +2 -0
- package/dist/core/types/index.d.ts +2 -0
- package/dist/core/types/index.mjs +2 -0
- package/dist/interfaces/bun/types/request.cjs +1 -1
- package/dist/interfaces/bun/types/request.mjs +1 -1
- package/dist/interfaces/node/bodyReaders/formData/error.cjs +14 -0
- package/dist/interfaces/node/bodyReaders/formData/error.d.ts +8 -0
- package/dist/interfaces/node/bodyReaders/formData/error.mjs +12 -0
- package/dist/interfaces/node/bodyReaders/formData/index.cjs +94 -0
- package/dist/interfaces/node/bodyReaders/formData/index.d.ts +4 -0
- package/dist/interfaces/node/bodyReaders/formData/index.mjs +90 -0
- package/dist/interfaces/node/bodyReaders/formData/readRequestFormData.cjs +175 -0
- package/dist/interfaces/node/bodyReaders/formData/readRequestFormData.d.ts +21 -0
- package/dist/interfaces/node/bodyReaders/formData/readRequestFormData.mjs +173 -0
- package/dist/interfaces/node/bodyReaders/index.cjs +9 -0
- package/dist/interfaces/node/bodyReaders/index.d.ts +2 -0
- package/dist/interfaces/node/bodyReaders/index.mjs +2 -0
- package/dist/interfaces/node/bodyReaders/text/index.cjs +41 -0
- package/dist/interfaces/node/bodyReaders/text/index.d.ts +3 -0
- package/dist/interfaces/node/bodyReaders/text/index.mjs +38 -0
- package/dist/interfaces/node/bodyReaders/text/readRequestText.cjs +37 -0
- package/dist/interfaces/node/bodyReaders/text/readRequestText.d.ts +6 -0
- package/dist/interfaces/node/bodyReaders/text/readRequestText.mjs +35 -0
- package/dist/interfaces/node/createHttpServer.cjs +13 -5
- package/dist/interfaces/node/createHttpServer.d.ts +13 -12
- package/dist/interfaces/node/createHttpServer.mjs +13 -5
- package/dist/interfaces/node/hooks/index.cjs +47 -0
- package/dist/interfaces/node/hooks/index.d.ts +5 -0
- package/dist/interfaces/node/hooks/index.mjs +45 -0
- package/dist/interfaces/node/index.cjs +13 -9
- package/dist/interfaces/node/index.d.ts +1 -1
- package/dist/interfaces/node/index.mjs +7 -5
- package/dist/interfaces/node/types/index.cjs +0 -1
- package/dist/interfaces/node/types/index.d.ts +0 -1
- package/dist/interfaces/node/types/index.mjs +0 -1
- package/dist/interfaces/node/types/request.cjs +1 -1
- package/dist/interfaces/node/types/request.mjs +1 -1
- package/dist/plugins/codeGenerator/index.cjs +2 -0
- package/dist/plugins/codeGenerator/index.mjs +1 -1
- package/dist/plugins/codeGenerator/plugin.cjs +8 -4
- package/dist/plugins/codeGenerator/plugin.mjs +9 -5
- package/dist/plugins/codeGenerator/routeToDataParser.cjs +24 -1
- package/dist/plugins/codeGenerator/routeToDataParser.d.ts +34 -0
- package/dist/plugins/codeGenerator/routeToDataParser.mjs +24 -3
- package/dist/plugins/codeGenerator/typescriptTransfomer.cjs +9 -0
- package/dist/plugins/codeGenerator/typescriptTransfomer.d.ts +1 -0
- package/dist/plugins/codeGenerator/typescriptTransfomer.mjs +7 -0
- package/dist/plugins/openApiGenerator/makeOpenApiRoute.d.ts +3 -0
- package/dist/plugins/openApiGenerator/plugin.cjs +3 -3
- package/dist/plugins/openApiGenerator/plugin.mjs +4 -4
- package/dist/plugins/openApiGenerator/routeToOpenApi.cjs +16 -1
- package/dist/plugins/openApiGenerator/routeToOpenApi.d.ts +10 -1
- package/dist/plugins/openApiGenerator/routeToOpenApi.mjs +16 -1
- package/dist/plugins/openApiGenerator/types/entrypoint.d.ts +6 -1
- package/dist/plugins/openApiGenerator/types/openApiMethod.d.ts +1 -1
- package/package.json +5 -6
- package/dist/core/request.mjs +0 -32
- package/dist/interfaces/node/error/bodyParseUnknownError.cjs +0 -16
- package/dist/interfaces/node/error/bodyParseUnknownError.d.ts +0 -9
- package/dist/interfaces/node/error/bodyParseUnknownError.mjs +0 -14
- package/dist/interfaces/node/error/bodyParseWrongChunkReceived.d.ts +0 -8
- package/dist/interfaces/node/error/bodyParseWrongChunkReceived.mjs +0 -12
- package/dist/interfaces/node/hooks.cjs +0 -126
- package/dist/interfaces/node/hooks.d.ts +0 -8
- package/dist/interfaces/node/hooks.mjs +0 -124
- package/dist/interfaces/node/types/host.d.ts +0 -4
- /package/dist/{interfaces/node/types/host.cjs → client/types/hooks.cjs} +0 -0
- /package/dist/{interfaces/node/types/host.mjs → client/types/hooks.mjs} +0 -0
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { HookResponse } from "../../response";
|
|
2
|
-
import { type HookAfterSendResponse, type HookBeforeRouteExecution, type HookBeforeSendResponse, type HookError, type
|
|
2
|
+
import { type HookAfterSendResponse, type HookBeforeRouteExecution, type HookBeforeSendResponse, type HookError, type HookRouteLifeCycle, type HookSendResponse, type RouteHookErrorParams, type RouteHookParams, type RouteHookParamsAfter } from "../../route";
|
|
3
3
|
export declare function exitHookFunction(): import("@duplojs/utils").Kind<import("@duplojs/utils").KindDefinition<"@DuplojsHttpCore/route-hook-exit", unknown>, unknown>;
|
|
4
4
|
export declare function nextHookFunction(): import("@duplojs/utils").Kind<import("@duplojs/utils").KindDefinition<"@DuplojsHttpCore/route-hook-next", unknown>, unknown>;
|
|
5
|
-
export declare function buildHookBefore(hooks: (HookBeforeRouteExecution
|
|
5
|
+
export declare function buildHookBefore(hooks: (HookBeforeRouteExecution)[]): 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
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>>);
|
|
7
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>>);
|
|
8
8
|
export declare function createHookResponse(from: keyof HookRouteLifeCycle): RouteHookParams["response"];
|
|
@@ -10,8 +10,8 @@ export interface BuildStepResult {
|
|
|
10
10
|
readonly buildedFunction: BuildedStepFunction;
|
|
11
11
|
readonly hooksRouteLifeCycle: readonly HookRouteLifeCycle[];
|
|
12
12
|
}
|
|
13
|
-
export type BuildStepSuccessEither = E.
|
|
14
|
-
export type BuildStepNotSupportEither = E.
|
|
13
|
+
export type BuildStepSuccessEither = E.Right<"buildSuccess", BuildStepResult>;
|
|
14
|
+
export type BuildStepNotSupportEither = E.Left<"stepNotSupport", Steps>;
|
|
15
15
|
export interface StepFunctionBuilderParams {
|
|
16
16
|
buildStep(element: Steps): Promise<BuildStepSuccessEither | BuildStepNotSupportEither>;
|
|
17
17
|
success(result: BuildStepResult): BuildStepSuccessEither;
|
|
@@ -19,32 +19,29 @@ const defaultCutStepFunctionBuilder = create.createStepFunctionBuilder(cut.cutSt
|
|
|
19
19
|
if (!currentContract) {
|
|
20
20
|
throw new contract.ResponseContract.Error(information);
|
|
21
21
|
}
|
|
22
|
-
const result = currentContract.body.parse(body);
|
|
23
|
-
if (utils.E.isLeft(result)) {
|
|
24
|
-
throw new contract.ResponseContract.Error(information, utils.unwrap(result));
|
|
25
|
-
}
|
|
26
22
|
return new predicted.PredictedResponse(currentContract.code, currentContract.information, body);
|
|
27
23
|
};
|
|
28
|
-
function treatResult(result, floor) {
|
|
29
|
-
if (cut.cutStepOutputKind.has(result)) {
|
|
30
|
-
return {
|
|
31
|
-
...floor,
|
|
32
|
-
...utils.unwrap(result),
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
return result;
|
|
36
|
-
}
|
|
37
24
|
return success({
|
|
38
|
-
buildedFunction: (request, floor) => {
|
|
39
|
-
const
|
|
25
|
+
buildedFunction: async (request, floor) => {
|
|
26
|
+
const cutResult = await cutFunction(floor, {
|
|
40
27
|
request,
|
|
41
28
|
output,
|
|
42
29
|
response,
|
|
43
30
|
});
|
|
44
|
-
if (
|
|
45
|
-
|
|
31
|
+
if (cutResult instanceof predicted.PredictedResponse) {
|
|
32
|
+
const currentContract = preparedContractResponse[cutResult.information];
|
|
33
|
+
const resultBody = currentContract.body.isAsynchronous()
|
|
34
|
+
? await currentContract.body.asyncParse(cutResult.body)
|
|
35
|
+
: currentContract.body.parse(cutResult.body);
|
|
36
|
+
if (utils.E.isLeft(resultBody)) {
|
|
37
|
+
throw new contract.ResponseContract.Error(cutResult.information, utils.unwrap(resultBody));
|
|
38
|
+
}
|
|
39
|
+
return cutResult;
|
|
46
40
|
}
|
|
47
|
-
return
|
|
41
|
+
return {
|
|
42
|
+
...floor,
|
|
43
|
+
...utils.unwrap(cutResult),
|
|
44
|
+
};
|
|
48
45
|
},
|
|
49
46
|
hooksRouteLifeCycle: [],
|
|
50
47
|
});
|
|
@@ -17,32 +17,29 @@ const defaultCutStepFunctionBuilder = createStepFunctionBuilder(cutStepKind.has,
|
|
|
17
17
|
if (!currentContract) {
|
|
18
18
|
throw new ResponseContract.Error(information);
|
|
19
19
|
}
|
|
20
|
-
const result = currentContract.body.parse(body);
|
|
21
|
-
if (E.isLeft(result)) {
|
|
22
|
-
throw new ResponseContract.Error(information, unwrap(result));
|
|
23
|
-
}
|
|
24
20
|
return new PredictedResponse(currentContract.code, currentContract.information, body);
|
|
25
21
|
};
|
|
26
|
-
function treatResult(result, floor) {
|
|
27
|
-
if (cutStepOutputKind.has(result)) {
|
|
28
|
-
return {
|
|
29
|
-
...floor,
|
|
30
|
-
...unwrap(result),
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
return result;
|
|
34
|
-
}
|
|
35
22
|
return success({
|
|
36
|
-
buildedFunction: (request, floor) => {
|
|
37
|
-
const
|
|
23
|
+
buildedFunction: async (request, floor) => {
|
|
24
|
+
const cutResult = await cutFunction(floor, {
|
|
38
25
|
request,
|
|
39
26
|
output,
|
|
40
27
|
response,
|
|
41
28
|
});
|
|
42
|
-
if (
|
|
43
|
-
|
|
29
|
+
if (cutResult instanceof PredictedResponse) {
|
|
30
|
+
const currentContract = preparedContractResponse[cutResult.information];
|
|
31
|
+
const resultBody = currentContract.body.isAsynchronous()
|
|
32
|
+
? await currentContract.body.asyncParse(cutResult.body)
|
|
33
|
+
: currentContract.body.parse(cutResult.body);
|
|
34
|
+
if (E.isLeft(resultBody)) {
|
|
35
|
+
throw new ResponseContract.Error(cutResult.information, unwrap(resultBody));
|
|
36
|
+
}
|
|
37
|
+
return cutResult;
|
|
44
38
|
}
|
|
45
|
-
return
|
|
39
|
+
return {
|
|
40
|
+
...floor,
|
|
41
|
+
...unwrap(cutResult),
|
|
42
|
+
};
|
|
46
43
|
},
|
|
47
44
|
hooksRouteLifeCycle: [],
|
|
48
45
|
});
|
|
@@ -10,32 +10,56 @@ var predicted = require('../../../response/predicted.cjs');
|
|
|
10
10
|
const defaultExtractStepFunctionBuilder = create.createStepFunctionBuilder(extract.extractStepKind.has, (step, { success, environment, defaultExtractContract }) => {
|
|
11
11
|
const { shape, responseContract: stepResponseContract, } = step.definition;
|
|
12
12
|
const responseContract = stepResponseContract ?? defaultExtractContract;
|
|
13
|
-
function
|
|
14
|
-
const
|
|
15
|
-
?
|
|
16
|
-
: undefined);
|
|
17
|
-
|
|
18
|
-
? response.setHeader("extract-key", `request.${key}`)
|
|
19
|
-
: response.setHeader("extract-key", `request.${key}.${subKey}`);
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
13
|
+
function createExtractor(parser, key, subKey) {
|
|
14
|
+
const createResponse = environment === "DEV"
|
|
15
|
+
? (result) => new predicted.PredictedResponse(responseContract.code, responseContract.information, result)
|
|
16
|
+
: () => new predicted.PredictedResponse(responseContract.code, responseContract.information, undefined);
|
|
17
|
+
const setHeader = subKey === undefined || key === "body"
|
|
18
|
+
? (response) => response.setHeader("extract-key", `request.${key}`)
|
|
19
|
+
: (response) => response.setHeader("extract-key", `request.${key}.${subKey}`);
|
|
20
|
+
const getResponse = (result) => setHeader(createResponse(result));
|
|
21
|
+
const treatResult = (result, floor) => utils.E.isLeft(result)
|
|
22
|
+
? getResponse(utils.unwrap(result))
|
|
23
|
+
: {
|
|
24
|
+
...floor,
|
|
25
|
+
[subKey ?? key]: utils.unwrap(result),
|
|
26
|
+
};
|
|
27
|
+
const getValue = typeof subKey === "string"
|
|
28
|
+
? (value) => value?.[subKey]
|
|
29
|
+
: utils.forward;
|
|
30
|
+
if (key === "body") {
|
|
31
|
+
const parseFunction = parser.isAsynchronous()
|
|
32
|
+
? parser.asyncParse
|
|
33
|
+
: parser.parse;
|
|
34
|
+
return async (request, floor) => {
|
|
35
|
+
const bodyResult = await request.getBody();
|
|
36
|
+
if (utils.E.isLeft(bodyResult)) {
|
|
37
|
+
return treatResult(bodyResult, floor);
|
|
38
|
+
}
|
|
39
|
+
const result = await parseFunction(getValue(utils.unwrap(bodyResult)));
|
|
40
|
+
return treatResult(result, floor);
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
if (parser.isAsynchronous()) {
|
|
44
|
+
const parseFunction = parser.asyncParse;
|
|
45
|
+
return async (request, floor) => {
|
|
46
|
+
const result = await parseFunction(getValue(request[key]));
|
|
47
|
+
return treatResult(result, floor);
|
|
48
|
+
};
|
|
24
49
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
50
|
+
const parseFunction = parser.parse;
|
|
51
|
+
return (request, floor) => {
|
|
52
|
+
const result = parseFunction(getValue(request[key]));
|
|
53
|
+
return treatResult(result, floor);
|
|
28
54
|
};
|
|
29
55
|
}
|
|
30
|
-
const extractors = utils.A.reduce(utils.O.entries(shape), utils.A.reduceFrom([]), ({ lastValue, element: [key, value], next }) =>
|
|
31
|
-
? utils.A.push(lastValue, (request, floor) => treatResult(value.parse(request[key]), floor, key))
|
|
32
|
-
: utils.pipe(value, utils.P.when(utils.isType("undefined"), utils.justReturn(lastValue)), utils.P.otherwise(utils.innerPipe(utils.O.entries, utils.A.map(([subKey, subValue]) => ((request, floor) => treatResult(subValue.parse(request[key]?.[subKey]), floor, key, subKey))), (subExtractor) => utils.A.concat(lastValue, subExtractor))))));
|
|
56
|
+
const extractors = utils.A.reduce(utils.O.entries(shape), utils.A.reduceFrom([]), ({ lastValue, element: [key, value], next, }) => utils.pipe(value, utils.P.when(utils.DP.dataParserKind.has, (value) => utils.A.push(lastValue, createExtractor(value, key, undefined))), utils.P.otherwise((value) => utils.pipe(value, utils.P.when(utils.isType("undefined"), utils.justReturn(lastValue)), utils.P.otherwise(utils.innerPipe(utils.O.entries, utils.A.map(([subKey, subValue]) => createExtractor(subValue, key, subKey)), (subExtractor) => utils.A.concat(lastValue, subExtractor))))), next));
|
|
33
57
|
return success({
|
|
34
|
-
buildedFunction: (request, floor) => {
|
|
58
|
+
buildedFunction: async (request, floor) => {
|
|
35
59
|
let newFloor = floor;
|
|
36
60
|
// eslint-disable-next-line @typescript-eslint/prefer-for-of
|
|
37
61
|
for (let index = 0; index < extractors.length; index++) {
|
|
38
|
-
const result = extractors[index](request, newFloor);
|
|
62
|
+
const result = await extractors[index](request, newFloor);
|
|
39
63
|
if (result instanceof predicted.PredictedResponse) {
|
|
40
64
|
return result;
|
|
41
65
|
}
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import { type MaybePromise } from "@duplojs/utils";
|
|
2
|
+
export declare const defaultExtractStepFunctionBuilder: (step: import("../../../steps").Steps, params: import("..").StepFunctionBuilderParams) => MaybePromise<import("..").BuildStepSuccessEither | import("..").BuildStepNotSupportEither>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import '../../../steps/index.mjs';
|
|
2
|
-
import { unwrap, E, A, O,
|
|
2
|
+
import { unwrap, E, forward, A, O, pipe, P, DP, isType, justReturn, innerPipe } from '@duplojs/utils';
|
|
3
3
|
import '../../../response/index.mjs';
|
|
4
4
|
import { createStepFunctionBuilder } from '../create.mjs';
|
|
5
5
|
import { extractStepKind } from '../../../steps/extract.mjs';
|
|
@@ -8,32 +8,56 @@ import { PredictedResponse } from '../../../response/predicted.mjs';
|
|
|
8
8
|
const defaultExtractStepFunctionBuilder = createStepFunctionBuilder(extractStepKind.has, (step, { success, environment, defaultExtractContract }) => {
|
|
9
9
|
const { shape, responseContract: stepResponseContract, } = step.definition;
|
|
10
10
|
const responseContract = stepResponseContract ?? defaultExtractContract;
|
|
11
|
-
function
|
|
12
|
-
const
|
|
13
|
-
?
|
|
14
|
-
: undefined);
|
|
15
|
-
|
|
16
|
-
? response.setHeader("extract-key", `request.${key}`)
|
|
17
|
-
: response.setHeader("extract-key", `request.${key}.${subKey}`);
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
11
|
+
function createExtractor(parser, key, subKey) {
|
|
12
|
+
const createResponse = environment === "DEV"
|
|
13
|
+
? (result) => new PredictedResponse(responseContract.code, responseContract.information, result)
|
|
14
|
+
: () => new PredictedResponse(responseContract.code, responseContract.information, undefined);
|
|
15
|
+
const setHeader = subKey === undefined || key === "body"
|
|
16
|
+
? (response) => response.setHeader("extract-key", `request.${key}`)
|
|
17
|
+
: (response) => response.setHeader("extract-key", `request.${key}.${subKey}`);
|
|
18
|
+
const getResponse = (result) => setHeader(createResponse(result));
|
|
19
|
+
const treatResult = (result, floor) => E.isLeft(result)
|
|
20
|
+
? getResponse(unwrap(result))
|
|
21
|
+
: {
|
|
22
|
+
...floor,
|
|
23
|
+
[subKey ?? key]: unwrap(result),
|
|
24
|
+
};
|
|
25
|
+
const getValue = typeof subKey === "string"
|
|
26
|
+
? (value) => value?.[subKey]
|
|
27
|
+
: forward;
|
|
28
|
+
if (key === "body") {
|
|
29
|
+
const parseFunction = parser.isAsynchronous()
|
|
30
|
+
? parser.asyncParse
|
|
31
|
+
: parser.parse;
|
|
32
|
+
return async (request, floor) => {
|
|
33
|
+
const bodyResult = await request.getBody();
|
|
34
|
+
if (E.isLeft(bodyResult)) {
|
|
35
|
+
return treatResult(bodyResult, floor);
|
|
36
|
+
}
|
|
37
|
+
const result = await parseFunction(getValue(unwrap(bodyResult)));
|
|
38
|
+
return treatResult(result, floor);
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
if (parser.isAsynchronous()) {
|
|
42
|
+
const parseFunction = parser.asyncParse;
|
|
43
|
+
return async (request, floor) => {
|
|
44
|
+
const result = await parseFunction(getValue(request[key]));
|
|
45
|
+
return treatResult(result, floor);
|
|
46
|
+
};
|
|
22
47
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
48
|
+
const parseFunction = parser.parse;
|
|
49
|
+
return (request, floor) => {
|
|
50
|
+
const result = parseFunction(getValue(request[key]));
|
|
51
|
+
return treatResult(result, floor);
|
|
26
52
|
};
|
|
27
53
|
}
|
|
28
|
-
const extractors = A.reduce(O.entries(shape), A.reduceFrom([]), ({ lastValue, element: [key, value], next }) =>
|
|
29
|
-
? A.push(lastValue, (request, floor) => treatResult(value.parse(request[key]), floor, key))
|
|
30
|
-
: pipe(value, P.when(isType("undefined"), justReturn(lastValue)), P.otherwise(innerPipe(O.entries, A.map(([subKey, subValue]) => ((request, floor) => treatResult(subValue.parse(request[key]?.[subKey]), floor, key, subKey))), (subExtractor) => A.concat(lastValue, subExtractor))))));
|
|
54
|
+
const extractors = A.reduce(O.entries(shape), A.reduceFrom([]), ({ lastValue, element: [key, value], next, }) => pipe(value, P.when(DP.dataParserKind.has, (value) => A.push(lastValue, createExtractor(value, key, undefined))), P.otherwise((value) => pipe(value, P.when(isType("undefined"), justReturn(lastValue)), P.otherwise(innerPipe(O.entries, A.map(([subKey, subValue]) => createExtractor(subValue, key, subKey)), (subExtractor) => A.concat(lastValue, subExtractor))))), next));
|
|
31
55
|
return success({
|
|
32
|
-
buildedFunction: (request, floor) => {
|
|
56
|
+
buildedFunction: async (request, floor) => {
|
|
33
57
|
let newFloor = floor;
|
|
34
58
|
// eslint-disable-next-line @typescript-eslint/prefer-for-of
|
|
35
59
|
for (let index = 0; index < extractors.length; index++) {
|
|
36
|
-
const result = extractors[index](request, newFloor);
|
|
60
|
+
const result = await extractors[index](request, newFloor);
|
|
37
61
|
if (result instanceof PredictedResponse) {
|
|
38
62
|
return result;
|
|
39
63
|
}
|
|
@@ -18,17 +18,23 @@ const defaultHandlerStepFunctionBuilder = create.createStepFunctionBuilder(handl
|
|
|
18
18
|
if (!currentContract) {
|
|
19
19
|
throw new contract.ResponseContract.Error(information);
|
|
20
20
|
}
|
|
21
|
-
const result = currentContract.body.parse(body);
|
|
22
|
-
if (utils.E.isLeft(result)) {
|
|
23
|
-
throw new contract.ResponseContract.Error(information, utils.unwrap(result));
|
|
24
|
-
}
|
|
25
21
|
return new predicted.PredictedResponse(currentContract.code, currentContract.information, body);
|
|
26
22
|
};
|
|
27
23
|
return success({
|
|
28
|
-
buildedFunction: (request, floor) =>
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
24
|
+
buildedFunction: async (request, floor) => {
|
|
25
|
+
const predictedResponse = await handlerFunction(floor, {
|
|
26
|
+
request,
|
|
27
|
+
response,
|
|
28
|
+
});
|
|
29
|
+
const currentContract = preparedContractResponse[predictedResponse.information];
|
|
30
|
+
const result = currentContract.body.isAsynchronous()
|
|
31
|
+
? await currentContract.body.asyncParse(predictedResponse.body)
|
|
32
|
+
: currentContract.body.parse(predictedResponse.body);
|
|
33
|
+
if (utils.E.isLeft(result)) {
|
|
34
|
+
throw new contract.ResponseContract.Error(predictedResponse.information, utils.unwrap(result));
|
|
35
|
+
}
|
|
36
|
+
return predictedResponse;
|
|
37
|
+
},
|
|
32
38
|
hooksRouteLifeCycle: [],
|
|
33
39
|
});
|
|
34
40
|
});
|
|
@@ -16,17 +16,23 @@ const defaultHandlerStepFunctionBuilder = createStepFunctionBuilder(handlerStepK
|
|
|
16
16
|
if (!currentContract) {
|
|
17
17
|
throw new ResponseContract.Error(information);
|
|
18
18
|
}
|
|
19
|
-
const result = currentContract.body.parse(body);
|
|
20
|
-
if (E.isLeft(result)) {
|
|
21
|
-
throw new ResponseContract.Error(information, unwrap(result));
|
|
22
|
-
}
|
|
23
19
|
return new PredictedResponse(currentContract.code, currentContract.information, body);
|
|
24
20
|
};
|
|
25
21
|
return success({
|
|
26
|
-
buildedFunction: (request, floor) =>
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
22
|
+
buildedFunction: async (request, floor) => {
|
|
23
|
+
const predictedResponse = await handlerFunction(floor, {
|
|
24
|
+
request,
|
|
25
|
+
response,
|
|
26
|
+
});
|
|
27
|
+
const currentContract = preparedContractResponse[predictedResponse.information];
|
|
28
|
+
const result = currentContract.body.isAsynchronous()
|
|
29
|
+
? await currentContract.body.asyncParse(predictedResponse.body)
|
|
30
|
+
: currentContract.body.parse(predictedResponse.body);
|
|
31
|
+
if (E.isLeft(result)) {
|
|
32
|
+
throw new ResponseContract.Error(predictedResponse.information, unwrap(result));
|
|
33
|
+
}
|
|
34
|
+
return predictedResponse;
|
|
35
|
+
},
|
|
30
36
|
hooksRouteLifeCycle: [],
|
|
31
37
|
});
|
|
32
38
|
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const defaultBodyController: import("../request").BodyController<"text", import("../request").TextBodyReaderParams>;
|
package/dist/core/hub/hooks.cjs
CHANGED
|
@@ -9,7 +9,9 @@ async function launchHookBeforeBuildRoute(hooks, route) {
|
|
|
9
9
|
return utils.G.asyncReduce(hooks, utils.G.reduceFrom(route), async ({ element: hook, lastValue, next, }) => next(await hook(lastValue)));
|
|
10
10
|
}
|
|
11
11
|
async function launchHookServer(hooks, hub, httpServerParams) {
|
|
12
|
-
|
|
12
|
+
for (const hook of hooks) {
|
|
13
|
+
await hook(hub, httpServerParams);
|
|
14
|
+
}
|
|
13
15
|
}
|
|
14
16
|
const hookExit = hookServerExitKind.setTo({});
|
|
15
17
|
const hookNext = hookServerNextKind.setTo({});
|
package/dist/core/hub/hooks.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { type Route } from "../route";
|
|
|
2
2
|
import { type EscapeVoid, type Kind, type MaybePromise } from "@duplojs/utils";
|
|
3
3
|
import { type Hub } from ".";
|
|
4
4
|
import { type RouterInitializationData } from "../router";
|
|
5
|
+
import { type HttpServerParams } from "../types";
|
|
5
6
|
export declare const hookServerExitKind: import("@duplojs/utils").KindHandler<import("@duplojs/utils").KindDefinition<"@DuplojsHttpCore/server-hook-exit", unknown>>;
|
|
6
7
|
export interface ServerHookExit extends Kind<typeof hookServerExitKind.definition> {
|
|
7
8
|
}
|
|
@@ -10,12 +11,10 @@ export interface ServerHookNext extends Kind<typeof hookServerNextKind.definitio
|
|
|
10
11
|
}
|
|
11
12
|
export type HookBeforeBuildRoute = (route: Route) => MaybePromise<Route>;
|
|
12
13
|
export declare function launchHookBeforeBuildRoute(hooks: Iterable<HookBeforeBuildRoute>, route: Route): Promise<Route<import("../route").RouteDefinition>>;
|
|
13
|
-
export interface HttpServerParams {
|
|
14
|
-
}
|
|
15
14
|
export type HookBeforeServerBuildRoutes = (hub: Hub, httpServerParams: HttpServerParams) => MaybePromise<Hub | EscapeVoid>;
|
|
16
15
|
export type HookBeforeStartServer = (hub: Hub, httpServerParams: HttpServerParams) => MaybePromise<Hub | EscapeVoid>;
|
|
17
16
|
export type HookAfterStartServer = (hub: Hub, httpServerParams: HttpServerParams) => MaybePromise<Hub | EscapeVoid>;
|
|
18
|
-
export declare function launchHookServer(hooks: Iterable<HookBeforeStartServer | HookAfterStartServer | HookBeforeServerBuildRoutes>, hub: Hub, httpServerParams: HttpServerParams): Promise<
|
|
17
|
+
export declare function launchHookServer(hooks: Iterable<HookBeforeStartServer | HookAfterStartServer | HookBeforeServerBuildRoutes>, hub: Hub, httpServerParams: HttpServerParams): Promise<void>;
|
|
19
18
|
export interface HttpServerErrorParams {
|
|
20
19
|
readonly error: unknown;
|
|
21
20
|
next(): ServerHookNext;
|
package/dist/core/hub/hooks.mjs
CHANGED
|
@@ -7,7 +7,9 @@ async function launchHookBeforeBuildRoute(hooks, route) {
|
|
|
7
7
|
return G.asyncReduce(hooks, G.reduceFrom(route), async ({ element: hook, lastValue, next, }) => next(await hook(lastValue)));
|
|
8
8
|
}
|
|
9
9
|
async function launchHookServer(hooks, hub, httpServerParams) {
|
|
10
|
-
|
|
10
|
+
for (const hook of hooks) {
|
|
11
|
+
await hook(hub, httpServerParams);
|
|
12
|
+
}
|
|
11
13
|
}
|
|
12
14
|
const hookExit = hookServerExitKind.setTo({});
|
|
13
15
|
const hookNext = hookServerNextKind.setTo({});
|