@duplojs/http 0.6.2 → 0.7.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.
Files changed (174) hide show
  1. package/dist/client/getBody.cjs +1 -1
  2. package/dist/client/getBody.mjs +1 -1
  3. package/dist/client/hooks.d.ts +1 -25
  4. package/dist/client/httpClient.d.ts +29 -25
  5. package/dist/client/promiseRequest.cjs +3 -0
  6. package/dist/client/promiseRequest.d.ts +31 -39
  7. package/dist/client/promiseRequest.mjs +4 -1
  8. package/dist/client/queryToString.cjs +2 -2
  9. package/dist/client/queryToString.mjs +2 -2
  10. package/dist/client/types/clientRequestParams.d.ts +9 -3
  11. package/dist/client/types/clientResponse.d.ts +6 -6
  12. package/dist/client/types/hooks.d.ts +25 -0
  13. package/dist/client/types/index.cjs +2 -0
  14. package/dist/client/types/index.d.ts +2 -0
  15. package/dist/client/types/index.mjs +2 -0
  16. package/dist/client/types/promiseRequestParams.cjs +2 -0
  17. package/dist/client/types/promiseRequestParams.d.ts +9 -0
  18. package/dist/client/types/promiseRequestParams.mjs +1 -0
  19. package/dist/client/types/serverRoute.d.ts +28 -4
  20. package/dist/client/unexpectedResponseError.d.ts +1 -2
  21. package/dist/core/builders/preflight/route.cjs +1 -0
  22. package/dist/core/builders/preflight/route.d.ts +4 -2
  23. package/dist/core/builders/preflight/route.mjs +1 -0
  24. package/dist/core/builders/route/builder.cjs +1 -0
  25. package/dist/core/builders/route/builder.d.ts +4 -2
  26. package/dist/core/builders/route/builder.mjs +1 -0
  27. package/dist/core/clean/newType.d.ts +1 -1
  28. package/dist/core/clean/primitive.d.ts +1 -1
  29. package/dist/core/defaultHooks/index.cjs +50 -0
  30. package/dist/core/defaultHooks/index.d.ts +5 -0
  31. package/dist/core/defaultHooks/index.mjs +48 -0
  32. package/dist/{interfaces/node/error → core/errors}/bodyParseWrongChunkReceived.cjs +6 -4
  33. package/dist/core/errors/bodyParseWrongChunkReceived.d.ts +9 -0
  34. package/dist/core/errors/bodyParseWrongChunkReceived.mjs +14 -0
  35. package/dist/{interfaces/node/error → core/errors}/bodySizeExceedsLimitError.cjs +2 -2
  36. package/dist/{interfaces/node/error → core/errors}/bodySizeExceedsLimitError.d.ts +2 -2
  37. package/dist/{interfaces/node/error → core/errors}/bodySizeExceedsLimitError.mjs +2 -2
  38. package/dist/{interfaces/node/error → core/errors}/index.cjs +6 -4
  39. package/dist/{interfaces/node/error → core/errors}/index.d.ts +3 -2
  40. package/dist/{interfaces/node/error → core/errors}/index.mjs +3 -2
  41. package/dist/core/errors/parseJsonError.cjs +16 -0
  42. package/dist/core/errors/parseJsonError.d.ts +9 -0
  43. package/dist/core/errors/parseJsonError.mjs +14 -0
  44. package/dist/core/errors/wrongContentTypeError.cjs +16 -0
  45. package/dist/core/errors/wrongContentTypeError.d.ts +9 -0
  46. package/dist/core/errors/wrongContentTypeError.mjs +14 -0
  47. package/dist/core/functionsBuilders/route/create.d.ts +2 -2
  48. package/dist/core/functionsBuilders/route/default.cjs +0 -11
  49. package/dist/core/functionsBuilders/route/default.mjs +0 -11
  50. package/dist/core/functionsBuilders/route/hook.d.ts +2 -2
  51. package/dist/core/functionsBuilders/steps/create.d.ts +2 -2
  52. package/dist/core/functionsBuilders/steps/defaults/cutStep.cjs +15 -18
  53. package/dist/core/functionsBuilders/steps/defaults/cutStep.mjs +15 -18
  54. package/dist/core/functionsBuilders/steps/defaults/extractStep.cjs +43 -19
  55. package/dist/core/functionsBuilders/steps/defaults/extractStep.d.ts +2 -1
  56. package/dist/core/functionsBuilders/steps/defaults/extractStep.mjs +44 -20
  57. package/dist/core/functionsBuilders/steps/defaults/handlerStep.cjs +14 -8
  58. package/dist/core/functionsBuilders/steps/defaults/handlerStep.mjs +14 -8
  59. package/dist/core/hub/defaultBodyController.cjs +8 -0
  60. package/dist/core/hub/defaultBodyController.d.ts +1 -0
  61. package/dist/core/hub/defaultBodyController.mjs +6 -0
  62. package/dist/core/hub/hooks.cjs +3 -1
  63. package/dist/core/hub/hooks.d.ts +2 -3
  64. package/dist/core/hub/hooks.mjs +3 -1
  65. package/dist/core/hub/index.cjs +101 -127
  66. package/dist/core/hub/index.d.ts +33 -34
  67. package/dist/core/hub/index.mjs +100 -128
  68. package/dist/core/implementHttpServer.cjs +5 -5
  69. package/dist/core/implementHttpServer.d.ts +2 -1
  70. package/dist/core/implementHttpServer.mjs +5 -5
  71. package/dist/core/index.cjs +34 -10
  72. package/dist/core/index.d.ts +2 -0
  73. package/dist/core/index.mjs +13 -2
  74. package/dist/core/request/bodyController/base.cjs +36 -0
  75. package/dist/core/request/bodyController/base.d.ts +28 -0
  76. package/dist/core/request/bodyController/base.mjs +34 -0
  77. package/dist/core/request/bodyController/formData.cjs +28 -0
  78. package/dist/core/request/bodyController/formData.d.ts +22 -0
  79. package/dist/core/request/bodyController/formData.mjs +25 -0
  80. package/dist/core/request/bodyController/index.cjs +13 -0
  81. package/dist/core/request/bodyController/index.d.ts +3 -0
  82. package/dist/core/request/bodyController/index.mjs +3 -0
  83. package/dist/core/request/bodyController/text.cjs +14 -0
  84. package/dist/core/request/bodyController/text.d.ts +10 -0
  85. package/dist/core/request/bodyController/text.mjs +11 -0
  86. package/dist/core/{request.cjs → request/index.cjs} +21 -3
  87. package/dist/core/{request.d.ts → request/index.d.ts} +10 -3
  88. package/dist/core/request/index.mjs +50 -0
  89. package/dist/core/response/contract.d.ts +1 -1
  90. package/dist/core/route/hooks.d.ts +0 -2
  91. package/dist/core/route/index.d.ts +2 -1
  92. package/dist/core/router/index.cjs +27 -8
  93. package/dist/core/router/index.d.ts +2 -1
  94. package/dist/core/router/index.mjs +28 -10
  95. package/dist/core/router/notFoundBodyReaderImplementationError.cjs +16 -0
  96. package/dist/core/router/notFoundBodyReaderImplementationError.d.ts +11 -0
  97. package/dist/core/router/notFoundBodyReaderImplementationError.mjs +14 -0
  98. package/dist/core/router/types/buildedRouter.d.ts +3 -3
  99. package/dist/core/steps/extract.d.ts +3 -1
  100. package/dist/core/steps/types/steps.d.ts +1 -3
  101. package/dist/core/types/hosts.cjs +2 -0
  102. package/dist/core/types/hosts.d.ts +4 -0
  103. package/dist/core/types/hosts.mjs +1 -0
  104. package/dist/core/types/httpServerParams.cjs +2 -0
  105. package/dist/core/types/httpServerParams.d.ts +11 -0
  106. package/dist/core/types/httpServerParams.mjs +1 -0
  107. package/dist/core/types/index.cjs +2 -0
  108. package/dist/core/types/index.d.ts +2 -0
  109. package/dist/core/types/index.mjs +2 -0
  110. package/dist/interfaces/bun/types/request.cjs +1 -1
  111. package/dist/interfaces/bun/types/request.mjs +1 -1
  112. package/dist/interfaces/node/bodyReaders/formData/error.cjs +14 -0
  113. package/dist/interfaces/node/bodyReaders/formData/error.d.ts +8 -0
  114. package/dist/interfaces/node/bodyReaders/formData/error.mjs +12 -0
  115. package/dist/interfaces/node/bodyReaders/formData/index.cjs +94 -0
  116. package/dist/interfaces/node/bodyReaders/formData/index.d.ts +4 -0
  117. package/dist/interfaces/node/bodyReaders/formData/index.mjs +90 -0
  118. package/dist/interfaces/node/bodyReaders/formData/readRequestFormData.cjs +175 -0
  119. package/dist/interfaces/node/bodyReaders/formData/readRequestFormData.d.ts +21 -0
  120. package/dist/interfaces/node/bodyReaders/formData/readRequestFormData.mjs +173 -0
  121. package/dist/interfaces/node/bodyReaders/index.cjs +9 -0
  122. package/dist/interfaces/node/bodyReaders/index.d.ts +2 -0
  123. package/dist/interfaces/node/bodyReaders/index.mjs +2 -0
  124. package/dist/interfaces/node/bodyReaders/text/index.cjs +41 -0
  125. package/dist/interfaces/node/bodyReaders/text/index.d.ts +3 -0
  126. package/dist/interfaces/node/bodyReaders/text/index.mjs +38 -0
  127. package/dist/interfaces/node/bodyReaders/text/readRequestText.cjs +37 -0
  128. package/dist/interfaces/node/bodyReaders/text/readRequestText.d.ts +6 -0
  129. package/dist/interfaces/node/bodyReaders/text/readRequestText.mjs +35 -0
  130. package/dist/interfaces/node/createHttpServer.cjs +13 -5
  131. package/dist/interfaces/node/createHttpServer.d.ts +13 -12
  132. package/dist/interfaces/node/createHttpServer.mjs +13 -5
  133. package/dist/interfaces/node/hooks/index.cjs +47 -0
  134. package/dist/interfaces/node/hooks/index.d.ts +5 -0
  135. package/dist/interfaces/node/hooks/index.mjs +45 -0
  136. package/dist/interfaces/node/index.cjs +13 -9
  137. package/dist/interfaces/node/index.d.ts +1 -1
  138. package/dist/interfaces/node/index.mjs +7 -5
  139. package/dist/interfaces/node/types/index.cjs +0 -1
  140. package/dist/interfaces/node/types/index.d.ts +0 -1
  141. package/dist/interfaces/node/types/index.mjs +0 -1
  142. package/dist/interfaces/node/types/request.cjs +1 -1
  143. package/dist/interfaces/node/types/request.mjs +1 -1
  144. package/dist/plugins/codeGenerator/index.cjs +2 -0
  145. package/dist/plugins/codeGenerator/index.mjs +1 -1
  146. package/dist/plugins/codeGenerator/plugin.cjs +11 -4
  147. package/dist/plugins/codeGenerator/plugin.mjs +12 -5
  148. package/dist/plugins/codeGenerator/routeToDataParser.cjs +24 -1
  149. package/dist/plugins/codeGenerator/routeToDataParser.d.ts +34 -0
  150. package/dist/plugins/codeGenerator/routeToDataParser.mjs +24 -3
  151. package/dist/plugins/codeGenerator/typescriptTransfomer.cjs +28 -0
  152. package/dist/plugins/codeGenerator/typescriptTransfomer.d.ts +4 -0
  153. package/dist/plugins/codeGenerator/typescriptTransfomer.mjs +24 -0
  154. package/dist/plugins/openApiGenerator/makeOpenApiRoute.d.ts +3 -0
  155. package/dist/plugins/openApiGenerator/plugin.cjs +3 -3
  156. package/dist/plugins/openApiGenerator/plugin.mjs +4 -4
  157. package/dist/plugins/openApiGenerator/routeToOpenApi.cjs +16 -6
  158. package/dist/plugins/openApiGenerator/routeToOpenApi.d.ts +10 -1
  159. package/dist/plugins/openApiGenerator/routeToOpenApi.mjs +16 -6
  160. package/dist/plugins/openApiGenerator/types/entrypoint.d.ts +6 -1
  161. package/dist/plugins/openApiGenerator/types/openApiMethod.d.ts +1 -1
  162. package/package.json +5 -6
  163. package/dist/core/request.mjs +0 -32
  164. package/dist/interfaces/node/error/bodyParseUnknownError.cjs +0 -16
  165. package/dist/interfaces/node/error/bodyParseUnknownError.d.ts +0 -9
  166. package/dist/interfaces/node/error/bodyParseUnknownError.mjs +0 -14
  167. package/dist/interfaces/node/error/bodyParseWrongChunkReceived.d.ts +0 -8
  168. package/dist/interfaces/node/error/bodyParseWrongChunkReceived.mjs +0 -12
  169. package/dist/interfaces/node/hooks.cjs +0 -126
  170. package/dist/interfaces/node/hooks.d.ts +0 -8
  171. package/dist/interfaces/node/hooks.mjs +0 -124
  172. package/dist/interfaces/node/types/host.d.ts +0 -4
  173. /package/dist/{interfaces/node/types/host.cjs → client/types/hooks.cjs} +0 -0
  174. /package/dist/{interfaces/node/types/host.mjs → client/types/hooks.mjs} +0 -0
@@ -0,0 +1,3 @@
1
+ export * from "./base";
2
+ export * from "./formData";
3
+ export * from "./text";
@@ -0,0 +1,3 @@
1
+ export { createBodyController } from './base.mjs';
2
+ export { FormDataBodyController, controlBodyAsFormData } from './formData.mjs';
3
+ export { TextBodyController, controlBodyAsText } from './text.mjs';
@@ -0,0 +1,14 @@
1
+ 'use strict';
2
+
3
+ var utils = require('@duplojs/utils');
4
+ var base = require('./base.cjs');
5
+
6
+ const TextBodyController = base.createBodyController("text");
7
+ function controlBodyAsText(params) {
8
+ return TextBodyController.create({
9
+ bodyMaxSize: params?.bodyMaxSize && utils.stringToBytes(params.bodyMaxSize),
10
+ });
11
+ }
12
+
13
+ exports.TextBodyController = TextBodyController;
14
+ exports.controlBodyAsText = controlBodyAsText;
@@ -0,0 +1,10 @@
1
+ import { type BytesInString } from "@duplojs/utils";
2
+ import { type BodyControllerParams } from "./base";
3
+ export interface TextBodyReaderParams extends BodyControllerParams {
4
+ }
5
+ export declare const TextBodyController: import("./base").BodyControllerHandler<"text", TextBodyReaderParams>;
6
+ export type TextBodyController = typeof TextBodyController;
7
+ export interface ControlBodyAsTextParams {
8
+ bodyMaxSize?: number | BytesInString;
9
+ }
10
+ export declare function controlBodyAsText(params?: ControlBodyAsTextParams): import("./base").BodyController<"text", TextBodyReaderParams>;
@@ -0,0 +1,11 @@
1
+ import { stringToBytes } from '@duplojs/utils';
2
+ import { createBodyController } from './base.mjs';
3
+
4
+ const TextBodyController = createBodyController("text");
5
+ function controlBodyAsText(params) {
6
+ return TextBodyController.create({
7
+ bodyMaxSize: params?.bodyMaxSize && stringToBytes(params.bodyMaxSize),
8
+ });
9
+ }
10
+
11
+ export { TextBodyController, controlBodyAsText };
@@ -1,7 +1,8 @@
1
1
  'use strict';
2
2
 
3
3
  var utils = require('@duplojs/utils');
4
- var kind = require('./kind.cjs');
4
+ var kind = require('../kind.cjs');
5
+ require('./bodyController/index.cjs');
5
6
 
6
7
  class Request extends utils.kindHeritage("request", kind.createCoreLibKind("request")) {
7
8
  method;
@@ -13,8 +14,10 @@ class Request extends utils.kindHeritage("request", kind.createCoreLibKind("requ
13
14
  params;
14
15
  query;
15
16
  matchedPath;
16
- body = undefined;
17
- constructor({ method, headers, url, host, origin, path, params, query, matchedPath, ...rest }) {
17
+ bodyReader;
18
+ bodyResult = undefined;
19
+ filesAttache = undefined;
20
+ constructor({ method, headers, url, host, origin, path, params, query, matchedPath, bodyReader, ...rest }) {
18
21
  super();
19
22
  this.method = method;
20
23
  this.headers = headers;
@@ -25,10 +28,25 @@ class Request extends utils.kindHeritage("request", kind.createCoreLibKind("requ
25
28
  this.params = params;
26
29
  this.query = query;
27
30
  this.matchedPath = matchedPath;
31
+ this.bodyReader = bodyReader;
28
32
  for (const key in rest) {
29
33
  this[key] = rest[key];
30
34
  }
31
35
  }
36
+ getBody() {
37
+ if (this.bodyResult !== undefined) {
38
+ return this.bodyResult;
39
+ }
40
+ const externalPromise = utils.createExternalPromise();
41
+ this.bodyResult = externalPromise.promise;
42
+ return this.bodyReader
43
+ .read(this)
44
+ .then((result) => {
45
+ externalPromise.resolve(result);
46
+ this.bodyResult = result;
47
+ return result;
48
+ });
49
+ }
32
50
  }
33
51
 
34
52
  exports.Request = Request;
@@ -1,8 +1,12 @@
1
+ import { type E, type MaybePromise } from "@duplojs/utils";
1
2
  import { type GetPropsWithValue } from "@duplojs/utils/object";
3
+ import { type BodyReader } from "./bodyController";
4
+ export * from "./bodyController";
2
5
  export interface RequestMethodsWrapper {
3
6
  GET: true;
4
7
  POST: true;
5
8
  PUT: true;
9
+ PATCH: true;
6
10
  DELETE: true;
7
11
  HEAD: true;
8
12
  OPTIONS: true;
@@ -20,6 +24,7 @@ export interface RequestInitializationData {
20
24
  readonly path: string;
21
25
  readonly query: Record<string, string | string[]>;
22
26
  readonly url: string;
27
+ readonly bodyReader: BodyReader;
23
28
  }
24
29
  declare const Request_base: new (params?: {
25
30
  "@DuplojsHttpCore/request"?: unknown;
@@ -34,7 +39,9 @@ export declare class Request extends Request_base implements RequestInitializati
34
39
  params: Record<string, string>;
35
40
  query: Record<string, string | string[]>;
36
41
  matchedPath: string | null;
37
- body: unknown;
38
- constructor({ method, headers, url, host, origin, path, params, query, matchedPath, ...rest }: RequestInitializationData);
42
+ bodyReader: BodyReader;
43
+ private bodyResult?;
44
+ filesAttache: string[] | undefined;
45
+ constructor({ method, headers, url, host, origin, path, params, query, matchedPath, bodyReader, ...rest }: RequestInitializationData);
46
+ getBody(): MaybePromise<E.Success | E.Error>;
39
47
  }
40
- export {};
@@ -0,0 +1,50 @@
1
+ import { kindHeritage, createExternalPromise } from '@duplojs/utils';
2
+ import { createCoreLibKind } from '../kind.mjs';
3
+ import './bodyController/index.mjs';
4
+
5
+ class Request extends kindHeritage("request", createCoreLibKind("request")) {
6
+ method;
7
+ headers;
8
+ url;
9
+ host;
10
+ origin;
11
+ path;
12
+ params;
13
+ query;
14
+ matchedPath;
15
+ bodyReader;
16
+ bodyResult = undefined;
17
+ filesAttache = undefined;
18
+ constructor({ method, headers, url, host, origin, path, params, query, matchedPath, bodyReader, ...rest }) {
19
+ super();
20
+ this.method = method;
21
+ this.headers = headers;
22
+ this.url = url;
23
+ this.host = host;
24
+ this.origin = origin;
25
+ this.path = path;
26
+ this.params = params;
27
+ this.query = query;
28
+ this.matchedPath = matchedPath;
29
+ this.bodyReader = bodyReader;
30
+ for (const key in rest) {
31
+ this[key] = rest[key];
32
+ }
33
+ }
34
+ getBody() {
35
+ if (this.bodyResult !== undefined) {
36
+ return this.bodyResult;
37
+ }
38
+ const externalPromise = createExternalPromise();
39
+ this.bodyResult = externalPromise.promise;
40
+ return this.bodyReader
41
+ .read(this)
42
+ .then((result) => {
43
+ externalPromise.resolve(result);
44
+ this.bodyResult = result;
45
+ return result;
46
+ });
47
+ }
48
+ }
49
+
50
+ export { Request };
@@ -283,7 +283,7 @@ export declare namespace ResponseContract {
283
283
  }>>(information: GenericInformation, schema?: (GenericSchema & ForbiddenBigintDataParser<GenericSchema>) | undefined) => NoInfer<Contract<"511", GenericInformation, NeverCoalescing<GenericSchema, DP.DataParserEmpty<DP.DataParserDefinitionEmpty>>>>;
284
284
  const Error_base: new (params: {
285
285
  "@DuplojsHttpCore/contract-error"?: unknown;
286
- }, parentParams: readonly [message?: string | undefined, options?: ErrorOptions | undefined]) => Kind<import("@duplojs/utils").KindDefinition<"@DuplojsHttpCore/contract-error", unknown>, unknown> & Kind<import("@duplojs/utils").KindDefinition<"contract-error", unknown>, unknown> & globalThis.Error;
286
+ }, parentParams: readonly [message?: string | undefined, options?: ErrorOptions | undefined]) => globalThis.Error & Kind<import("@duplojs/utils").KindDefinition<"@DuplojsHttpCore/contract-error", unknown>, unknown> & Kind<import("@duplojs/utils").KindDefinition<"contract-error", unknown>, unknown>;
287
287
  export class Error extends Error_base {
288
288
  information: string;
289
289
  dataParserError?: DP.DataParserError | undefined;
@@ -20,7 +20,6 @@ export interface RouteHookParams<GenericRequest extends Request = Request> {
20
20
  response<GenericCode extends ResponseCode = ResponseCode, GenericInformation extends string = string, GenericBody extends unknown = unknown>(code: GenericCode, information: GenericInformation, body?: GenericBody): HookResponse<GenericCode, GenericInformation, GenericBody | undefined>;
21
21
  }
22
22
  export type HookBeforeRouteExecution<GenericRequest extends Request = Request> = (params: RouteHookParams<GenericRequest>) => MaybePromise<HookResponse | RouteHookExit | RouteHookNext>;
23
- export type HookParseBody<GenericRequest extends Request = Request> = (params: RouteHookParams<GenericRequest>) => MaybePromise<HookResponse | RouteHookExit | RouteHookNext>;
24
23
  export interface RouteHookErrorParams<GenericRequest extends Request = Request> {
25
24
  readonly request: GenericRequest;
26
25
  readonly error: unknown;
@@ -41,7 +40,6 @@ export type HookAfterSendResponse<GenericRequest extends Request = Request> = (p
41
40
  export interface HookRouteLifeCycle<GenericRequest extends Request = Request> {
42
41
  onConstructRequest?: HookOnConstructRequest<GenericRequest>;
43
42
  beforeRouteExecution?: HookBeforeRouteExecution<GenericRequest>;
44
- parseBody?: HookParseBody<GenericRequest>;
45
43
  error?: HookError;
46
44
  beforeSendResponse?: HookBeforeSendResponse<GenericRequest>;
47
45
  sendResponse?: HookSendResponse<GenericRequest>;
@@ -1,5 +1,5 @@
1
1
  import { type O, type Kind } from "@duplojs/utils";
2
- import { type RequestMethods } from "../request";
2
+ import { type BodyController, type RequestMethods } from "../request";
3
3
  import { type ExtractStep, type CheckerStep, type CutStep, type HandlerStep, type ProcessStep, type stepKind, type PresetCheckerStep } from "../steps";
4
4
  import { type HookRouteLifeCycle } from "./hooks";
5
5
  import { type Metadata } from "../metadata";
@@ -19,6 +19,7 @@ export interface RouteDefinition {
19
19
  readonly steps: readonly RouteSteps[];
20
20
  readonly hooks: readonly HookRouteLifeCycle[];
21
21
  readonly metadata: readonly Metadata[];
22
+ readonly bodyController: BodyController | null;
22
23
  }
23
24
  export declare const routeKind: import("@duplojs/utils").KindHandler<import("@duplojs/utils").KindDefinition<"@DuplojsHttpCore/route", unknown>>;
24
25
  export interface Route<GenericDefinition extends RouteDefinition = RouteDefinition> extends Kind<typeof routeKind.definition> {
@@ -6,8 +6,10 @@ var pathToRegExp = require('./pathToRegExp.cjs');
6
6
  var index = require('../route/index.cjs');
7
7
  var buildError = require('./buildError.cjs');
8
8
  require('../functionsBuilders/route/index.cjs');
9
- require('../functionsBuilders/steps/index.cjs');
10
9
  var decodeUrl = require('./decodeUrl.cjs');
10
+ var text = require('../request/bodyController/text.cjs');
11
+ var notFoundBodyReaderImplementationError = require('./notFoundBodyReaderImplementationError.cjs');
12
+ require('../functionsBuilders/index.cjs');
11
13
  require('./types/index.cjs');
12
14
  var _default = require('../functionsBuilders/route/default.cjs');
13
15
  var checkerStep = require('../functionsBuilders/steps/defaults/checkerStep.cjs');
@@ -18,18 +20,21 @@ var processStep = require('../functionsBuilders/steps/defaults/processStep.cjs')
18
20
  var hooks = require('../hub/hooks.cjs');
19
21
  var build = require('../functionsBuilders/route/build.cjs');
20
22
 
21
- async function buildRouter(inputHub) {
22
- const hub = inputHub
23
- .addRouteFunctionBuilder(_default.defaultRouteFunctionBuilder)
24
- .addStepFunctionBuilder([
23
+ async function buildRouter(hub) {
24
+ const { environment } = hub.config;
25
+ const { hooksRouteLifeCycle, routes, hooksHubLifeCycle, bodyReaderImplementations, } = hub;
26
+ const routeFunctionBuilders = [
27
+ ...hub.routeFunctionBuilders,
28
+ _default.defaultRouteFunctionBuilder,
29
+ ];
30
+ const stepFunctionBuilders = [
31
+ ...hub.stepFunctionBuilders,
25
32
  checkerStep.defaultCheckerStepFunctionBuilder,
26
33
  cutStep.defaultCutStepFunctionBuilder,
27
34
  handlerStep.defaultHandlerStepFunctionBuilder,
28
35
  extractStep.defaultExtractStepFunctionBuilder,
29
36
  processStep.defaultProcessStepFunctionBuilder,
30
- ]);
31
- const { environment } = hub.config;
32
- const { hooksRouteLifeCycle, routeFunctionBuilders, routes, stepFunctionBuilders, hooksHubLifeCycle, } = hub.aggregates();
37
+ ];
33
38
  const hooksBeforeBuildRoute = utils.pipe(hooksHubLifeCycle, utils.A.map(({ beforeBuildRoute }) => beforeBuildRoute), utils.A.filter(utils.isType("function")));
34
39
  const buildParams = {
35
40
  environment,
@@ -44,14 +49,23 @@ async function buildRouter(inputHub) {
44
49
  if (utils.E.isLeft(buildedRoute)) {
45
50
  throw new buildError.RouterBuildError(route, utils.unwrap(buildedRoute));
46
51
  }
52
+ const routeBodyController = route.definition.bodyController ?? hub.defaultBodyController;
53
+ const bodyReader = utils.pipe(bodyReaderImplementations, utils.A.reduce(utils.A.reduceFrom(null), ({ element, next, exit }) => utils.pipe(element, routeBodyController.tryToCreateReader, utils.E.whenIsRight(exit), utils.E.whenIsLeft(utils.justReturn(next(null))))));
54
+ if (!bodyReader) {
55
+ throw new notFoundBodyReaderImplementationError.NotFoundBodyReaderImplementationError(route, routeBodyController);
56
+ }
47
57
  return nextWithObject(lastValue, {
48
58
  [route.definition.method]: utils.A.concat(lastValue[route.definition.method] ?? [], utils.A.map(route.definition.paths, utils.O.to({
49
59
  pattern: pathToRegExp.pathToRegExp,
50
60
  buildedRoute: utils.justReturn(utils.unwrap(buildedRoute)),
51
61
  matchedPath: utils.forward,
62
+ bodyReader: utils.justReturn(bodyReader),
52
63
  }))),
53
64
  });
54
65
  });
66
+ const bodyControllerNotfoundRoute = text.controlBodyAsText();
67
+ const bodyReaderNotFoundRoute = utils.unwrap(bodyControllerNotfoundRoute.tryToCreateReader(text.TextBodyController.createReaderImplementation(() => Promise.resolve(utils.E.error(new Error("Inaccessible body in not found route."))))));
68
+ utils.asserts(bodyReaderNotFoundRoute, utils.isType("object"));
55
69
  const buildedNotfoundRoute = await utils.asyncPipe(index.createRoute({
56
70
  method: "GET",
57
71
  paths: ["/"],
@@ -59,6 +73,7 @@ async function buildRouter(inputHub) {
59
73
  preflightSteps: [],
60
74
  steps: [hub.notfoundHandler],
61
75
  metadata: [],
76
+ bodyController: bodyControllerNotfoundRoute,
62
77
  }), async (route) => {
63
78
  const result = await build.buildRouteFunction(route, buildParams);
64
79
  return utils.E.whenIsLeft(result, (element) => {
@@ -76,6 +91,7 @@ async function buildRouter(inputHub) {
76
91
  ...decodedUrl,
77
92
  params: {},
78
93
  matchedPath: null,
94
+ bodyReader: bodyReaderNotFoundRoute,
79
95
  }));
80
96
  }
81
97
  // eslint-disable-next-line @typescript-eslint/prefer-for-of
@@ -90,6 +106,7 @@ async function buildRouter(inputHub) {
90
106
  ...decodedUrl,
91
107
  params: result.groups ?? {},
92
108
  matchedPath: routerElement.matchedPath,
109
+ bodyReader: routerElement.bodyReader,
93
110
  }));
94
111
  }
95
112
  return buildedNotfoundRoute(new Request({
@@ -97,6 +114,7 @@ async function buildRouter(inputHub) {
97
114
  ...decodedUrl,
98
115
  params: {},
99
116
  matchedPath: null,
117
+ bodyReader: bodyReaderNotFoundRoute,
100
118
  }));
101
119
  },
102
120
  hooksRouteLifeCycle,
@@ -112,4 +130,5 @@ exports.RouterBuildError = buildError.RouterBuildError;
112
130
  exports.decodeUrl = decodeUrl.decodeUrl;
113
131
  exports.regexQueryAnalyser = decodeUrl.regexQueryAnalyser;
114
132
  exports.regexUrlAnalyser = decodeUrl.regexUrlAnalyser;
133
+ exports.NotFoundBodyReaderImplementationError = notFoundBodyReaderImplementationError.NotFoundBodyReaderImplementationError;
115
134
  exports.buildRouter = buildRouter;
@@ -4,4 +4,5 @@ export * from "./types";
4
4
  export * from "./pathToRegExp";
5
5
  export * from "./buildError";
6
6
  export * from "./decodeUrl";
7
- export declare function buildRouter(inputHub: Hub): Promise<BuildedRouter>;
7
+ export * from "./notFoundBodyReaderImplementationError";
8
+ export declare function buildRouter(hub: Hub): Promise<BuildedRouter>;
@@ -1,12 +1,14 @@
1
1
  import '../hub/index.mjs';
2
- import { pipe, A, isType, G, E, unwrap, O, forward, justReturn, asyncPipe } from '@duplojs/utils';
2
+ import { pipe, A, isType, G, E, unwrap, justReturn, O, forward, asserts, asyncPipe } from '@duplojs/utils';
3
3
  import { pathToRegExp } from './pathToRegExp.mjs';
4
4
  import { createRoute } from '../route/index.mjs';
5
5
  import { RouterBuildError } from './buildError.mjs';
6
6
  import '../functionsBuilders/route/index.mjs';
7
- import '../functionsBuilders/steps/index.mjs';
8
7
  import { decodeUrl } from './decodeUrl.mjs';
9
8
  export { regexQueryAnalyser, regexUrlAnalyser } from './decodeUrl.mjs';
9
+ import { controlBodyAsText, TextBodyController } from '../request/bodyController/text.mjs';
10
+ import { NotFoundBodyReaderImplementationError } from './notFoundBodyReaderImplementationError.mjs';
11
+ import '../functionsBuilders/index.mjs';
10
12
  import './types/index.mjs';
11
13
  import { defaultRouteFunctionBuilder } from '../functionsBuilders/route/default.mjs';
12
14
  import { defaultCheckerStepFunctionBuilder } from '../functionsBuilders/steps/defaults/checkerStep.mjs';
@@ -17,18 +19,21 @@ import { defaultProcessStepFunctionBuilder } from '../functionsBuilders/steps/de
17
19
  import { launchHookBeforeBuildRoute } from '../hub/hooks.mjs';
18
20
  import { buildRouteFunction } from '../functionsBuilders/route/build.mjs';
19
21
 
20
- async function buildRouter(inputHub) {
21
- const hub = inputHub
22
- .addRouteFunctionBuilder(defaultRouteFunctionBuilder)
23
- .addStepFunctionBuilder([
22
+ async function buildRouter(hub) {
23
+ const { environment } = hub.config;
24
+ const { hooksRouteLifeCycle, routes, hooksHubLifeCycle, bodyReaderImplementations, } = hub;
25
+ const routeFunctionBuilders = [
26
+ ...hub.routeFunctionBuilders,
27
+ defaultRouteFunctionBuilder,
28
+ ];
29
+ const stepFunctionBuilders = [
30
+ ...hub.stepFunctionBuilders,
24
31
  defaultCheckerStepFunctionBuilder,
25
32
  defaultCutStepFunctionBuilder,
26
33
  defaultHandlerStepFunctionBuilder,
27
34
  defaultExtractStepFunctionBuilder,
28
35
  defaultProcessStepFunctionBuilder,
29
- ]);
30
- const { environment } = hub.config;
31
- const { hooksRouteLifeCycle, routeFunctionBuilders, routes, stepFunctionBuilders, hooksHubLifeCycle, } = hub.aggregates();
36
+ ];
32
37
  const hooksBeforeBuildRoute = pipe(hooksHubLifeCycle, A.map(({ beforeBuildRoute }) => beforeBuildRoute), A.filter(isType("function")));
33
38
  const buildParams = {
34
39
  environment,
@@ -43,14 +48,23 @@ async function buildRouter(inputHub) {
43
48
  if (E.isLeft(buildedRoute)) {
44
49
  throw new RouterBuildError(route, unwrap(buildedRoute));
45
50
  }
51
+ const routeBodyController = route.definition.bodyController ?? hub.defaultBodyController;
52
+ const bodyReader = pipe(bodyReaderImplementations, A.reduce(A.reduceFrom(null), ({ element, next, exit }) => pipe(element, routeBodyController.tryToCreateReader, E.whenIsRight(exit), E.whenIsLeft(justReturn(next(null))))));
53
+ if (!bodyReader) {
54
+ throw new NotFoundBodyReaderImplementationError(route, routeBodyController);
55
+ }
46
56
  return nextWithObject(lastValue, {
47
57
  [route.definition.method]: A.concat(lastValue[route.definition.method] ?? [], A.map(route.definition.paths, O.to({
48
58
  pattern: pathToRegExp,
49
59
  buildedRoute: justReturn(unwrap(buildedRoute)),
50
60
  matchedPath: forward,
61
+ bodyReader: justReturn(bodyReader),
51
62
  }))),
52
63
  });
53
64
  });
65
+ const bodyControllerNotfoundRoute = controlBodyAsText();
66
+ const bodyReaderNotFoundRoute = unwrap(bodyControllerNotfoundRoute.tryToCreateReader(TextBodyController.createReaderImplementation(() => Promise.resolve(E.error(new Error("Inaccessible body in not found route."))))));
67
+ asserts(bodyReaderNotFoundRoute, isType("object"));
54
68
  const buildedNotfoundRoute = await asyncPipe(createRoute({
55
69
  method: "GET",
56
70
  paths: ["/"],
@@ -58,6 +72,7 @@ async function buildRouter(inputHub) {
58
72
  preflightSteps: [],
59
73
  steps: [hub.notfoundHandler],
60
74
  metadata: [],
75
+ bodyController: bodyControllerNotfoundRoute,
61
76
  }), async (route) => {
62
77
  const result = await buildRouteFunction(route, buildParams);
63
78
  return E.whenIsLeft(result, (element) => {
@@ -75,6 +90,7 @@ async function buildRouter(inputHub) {
75
90
  ...decodedUrl,
76
91
  params: {},
77
92
  matchedPath: null,
93
+ bodyReader: bodyReaderNotFoundRoute,
78
94
  }));
79
95
  }
80
96
  // eslint-disable-next-line @typescript-eslint/prefer-for-of
@@ -89,6 +105,7 @@ async function buildRouter(inputHub) {
89
105
  ...decodedUrl,
90
106
  params: result.groups ?? {},
91
107
  matchedPath: routerElement.matchedPath,
108
+ bodyReader: routerElement.bodyReader,
92
109
  }));
93
110
  }
94
111
  return buildedNotfoundRoute(new Request({
@@ -96,6 +113,7 @@ async function buildRouter(inputHub) {
96
113
  ...decodedUrl,
97
114
  params: {},
98
115
  matchedPath: null,
116
+ bodyReader: bodyReaderNotFoundRoute,
99
117
  }));
100
118
  },
101
119
  hooksRouteLifeCycle,
@@ -106,4 +124,4 @@ async function buildRouter(inputHub) {
106
124
  };
107
125
  }
108
126
 
109
- export { RouterBuildError, buildRouter, decodeUrl, pathToRegExp };
127
+ export { NotFoundBodyReaderImplementationError, RouterBuildError, buildRouter, decodeUrl, pathToRegExp };
@@ -0,0 +1,16 @@
1
+ 'use strict';
2
+
3
+ var kind = require('../kind.cjs');
4
+ var utils = require('@duplojs/utils');
5
+
6
+ class NotFoundBodyReaderImplementationError extends utils.kindHeritage("not-found-body-reader-implementation-error", kind.createCoreLibKind("not-found-body-reader-implementation-error"), Error) {
7
+ route;
8
+ bodyController;
9
+ constructor(route, bodyController) {
10
+ super({}, ["Body reader implementation not found."]);
11
+ this.route = route;
12
+ this.bodyController = bodyController;
13
+ }
14
+ }
15
+
16
+ exports.NotFoundBodyReaderImplementationError = NotFoundBodyReaderImplementationError;
@@ -0,0 +1,11 @@
1
+ import { type BodyController } from "../request";
2
+ import { type Route } from "../route";
3
+ declare const NotFoundBodyReaderImplementationError_base: new (params: {
4
+ "@DuplojsHttpCore/not-found-body-reader-implementation-error"?: unknown;
5
+ }, parentParams: readonly [message?: string | undefined, options?: ErrorOptions | undefined]) => Error & import("@duplojs/utils").Kind<import("@duplojs/utils").KindDefinition<"@DuplojsHttpCore/not-found-body-reader-implementation-error", unknown>, unknown> & import("@duplojs/utils").Kind<import("@duplojs/utils").KindDefinition<"not-found-body-reader-implementation-error", unknown>, unknown>;
6
+ export declare class NotFoundBodyReaderImplementationError extends NotFoundBodyReaderImplementationError_base {
7
+ route: Route;
8
+ bodyController: BodyController;
9
+ constructor(route: Route, bodyController: BodyController);
10
+ }
11
+ export {};
@@ -0,0 +1,14 @@
1
+ import { createCoreLibKind } from '../kind.mjs';
2
+ import { kindHeritage } from '@duplojs/utils';
3
+
4
+ class NotFoundBodyReaderImplementationError extends kindHeritage("not-found-body-reader-implementation-error", createCoreLibKind("not-found-body-reader-implementation-error"), Error) {
5
+ route;
6
+ bodyController;
7
+ constructor(route, bodyController) {
8
+ super({}, ["Body reader implementation not found."]);
9
+ this.route = route;
10
+ this.bodyController = bodyController;
11
+ }
12
+ }
13
+
14
+ export { NotFoundBodyReaderImplementationError };
@@ -1,11 +1,11 @@
1
1
  import { type createStepFunctionBuilder, type createRouteFunctionBuilder } from "../../functionsBuilders";
2
2
  import { type HookHubLifeCycle } from "../../hub";
3
3
  import { type RequestInitializationData } from "../../request";
4
- import { type HookRouteLifeCycle, type Route, type RouteDefinition } from "../../route";
5
- export type RouterInitializationData = Omit<RequestInitializationData, "matchedPath" | "params" | "path" | "query">;
4
+ import { type HookRouteLifeCycle, type Route } from "../../route";
5
+ export type RouterInitializationData = Omit<RequestInitializationData, "matchedPath" | "bodyReader" | "params" | "path" | "query">;
6
6
  export interface BuildedRouter {
7
7
  exec(initializationData: RouterInitializationData): Promise<void>;
8
- readonly routes: readonly Route<RouteDefinition>[];
8
+ readonly routes: ReadonlySet<Route>;
9
9
  readonly hooksRouteLifeCycle: readonly HookRouteLifeCycle[];
10
10
  readonly routeFunctionBuilders: readonly ReturnType<typeof createRouteFunctionBuilder>[];
11
11
  readonly stepFunctionBuilders: readonly ReturnType<typeof createStepFunctionBuilder>[];
@@ -6,7 +6,9 @@ import { type Metadata } from "../metadata";
6
6
  export interface DisabledExtractKeysCustom {
7
7
  }
8
8
  export type DisabledExtractKeys = O.GetPropsWithValue<DisabledExtractKeysCustom, true>;
9
- export type ExtractShape<GenericRequest extends Request = Request> = Partial<Record<Exclude<keyof GenericRequest, O.GetPropsWithValueExtends<GenericRequest, AnyFunction> | DisabledExtractKeys>, DP.DataParser | Record<string, DP.DataParser>>>;
9
+ export type ExtractShape<GenericRequest extends Request = Request> = Partial<Record<Exclude<keyof GenericRequest, O.GetPropsWithValueExtends<GenericRequest, AnyFunction> | DisabledExtractKeys | "body" | "bodyReader" | symbol>, DP.DataParser | Record<string, DP.DataParser>> & {
10
+ body: (DP.DataParser | Record<string, DP.DataParser>);
11
+ }>;
10
12
  export interface ExtractStepDefinition {
11
13
  readonly shape: ExtractShape;
12
14
  readonly responseContract?: ResponseContract.Contract<ClientErrorResponseCode, string, DP.DataParserEmpty>;
@@ -1,11 +1,9 @@
1
- import { type Kind, type O } from "@duplojs/utils";
2
1
  import { type CheckerStep } from "../checker";
3
2
  import { type CutStep } from "../cut";
4
3
  import { type ExtractStep } from "../extract";
5
4
  import { type HandlerStep } from "../handler";
6
5
  import { type PresetCheckerStep } from "../presetChecker";
7
6
  import { type ProcessStep } from "../process";
8
- import { type stepKind } from "../kind";
9
7
  export interface StepsCustom {
10
8
  }
11
- export type Steps = (StepsCustom[O.GetPropsWithValueExtends<StepsCustom, Kind<typeof stepKind.definition>>] | CheckerStep | CutStep | ExtractStep | HandlerStep | PresetCheckerStep | ProcessStep);
9
+ export type Steps = (StepsCustom[keyof StepsCustom] | CheckerStep | CutStep | ExtractStep | HandlerStep | PresetCheckerStep | ProcessStep);
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+
@@ -0,0 +1,4 @@
1
+ import { type O } from "@duplojs/utils";
2
+ export interface HostCustom {
3
+ }
4
+ export type Hosts = O.GetPropsWithValue<HostCustom, true>;
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+
@@ -0,0 +1,11 @@
1
+ import { type BytesInString } from "@duplojs/utils";
2
+ import { type Hosts } from "./hosts";
3
+ export interface HttpServerParams {
4
+ readonly host: Hosts;
5
+ readonly port: number;
6
+ readonly maxBodySize: BytesInString | number;
7
+ readonly informationHeaderKey: string;
8
+ readonly predictedHeaderKey: string;
9
+ readonly fromHookHeaderKey: string;
10
+ readonly uploadFolder: string;
11
+ }
@@ -2,4 +2,6 @@
2
2
 
3
3
  require('./environment.cjs');
4
4
  require('./forbiddenBigintDataParser.cjs');
5
+ require('./httpServerParams.cjs');
6
+ require('./hosts.cjs');
5
7
 
@@ -1,2 +1,4 @@
1
1
  export * from "./environment";
2
2
  export * from "./forbiddenBigintDataParser";
3
+ export * from "./httpServerParams";
4
+ export * from "./hosts";
@@ -1,2 +1,4 @@
1
1
  import './environment.mjs';
2
2
  import './forbiddenBigintDataParser.mjs';
3
+ import './httpServerParams.mjs';
4
+ import './hosts.mjs';
@@ -1,5 +1,5 @@
1
1
  'use strict';
2
2
 
3
- require('../../../core/request.cjs');
3
+ require('../../../core/request/index.cjs');
4
4
  require('../../../core/steps/index.cjs');
5
5
 
@@ -1,2 +1,2 @@
1
- import '../../../core/request.mjs';
1
+ import '../../../core/request/index.mjs';
2
2
  import '../../../core/steps/index.mjs';
@@ -0,0 +1,14 @@
1
+ 'use strict';
2
+
3
+ var utils = require('@duplojs/utils');
4
+ var kind = require('../../kind.cjs');
5
+
6
+ class BodyParseFormDataError extends utils.kindHeritage("body-parse-form-data-error", kind.createInterfacesNodeLibKind("body-parse-form-data-error"), Error) {
7
+ information;
8
+ constructor(information) {
9
+ super({}, [`Body parse form date error: ${information}`]);
10
+ this.information = information;
11
+ }
12
+ }
13
+
14
+ exports.BodyParseFormDataError = BodyParseFormDataError;
@@ -0,0 +1,8 @@
1
+ declare const BodyParseFormDataError_base: new (params: {
2
+ "@DuplojsHttpInterfacesNode/body-parse-form-data-error"?: unknown;
3
+ }, parentParams: readonly [message?: string | undefined, options?: ErrorOptions | undefined]) => Error & import("@duplojs/utils").Kind<import("@duplojs/utils").KindDefinition<"@DuplojsHttpInterfacesNode/body-parse-form-data-error", unknown>, unknown> & import("@duplojs/utils").Kind<import("@duplojs/utils").KindDefinition<"body-parse-form-data-error", unknown>, unknown>;
4
+ export declare class BodyParseFormDataError extends BodyParseFormDataError_base {
5
+ information: string;
6
+ constructor(information: string);
7
+ }
8
+ export {};
@@ -0,0 +1,12 @@
1
+ import { kindHeritage } from '@duplojs/utils';
2
+ import { createInterfacesNodeLibKind } from '../../kind.mjs';
3
+
4
+ class BodyParseFormDataError extends kindHeritage("body-parse-form-data-error", createInterfacesNodeLibKind("body-parse-form-data-error"), Error) {
5
+ information;
6
+ constructor(information) {
7
+ super({}, [`Body parse form date error: ${information}`]);
8
+ this.information = information;
9
+ }
10
+ }
11
+
12
+ export { BodyParseFormDataError };