@duplojs/http 0.7.4 → 0.8.4

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 (151) hide show
  1. package/dist/client/getBody.cjs +3 -3
  2. package/dist/client/getBody.mjs +3 -3
  3. package/dist/client/hooks.cjs +45 -0
  4. package/dist/client/hooks.d.ts +6 -1
  5. package/dist/client/hooks.mjs +41 -1
  6. package/dist/client/httpClient.cjs +25 -1
  7. package/dist/client/httpClient.d.ts +8 -3
  8. package/dist/client/httpClient.mjs +25 -1
  9. package/dist/client/index.cjs +7 -0
  10. package/dist/client/index.d.ts +1 -0
  11. package/dist/client/index.mjs +2 -1
  12. package/dist/client/insertParamsInPath.cjs +1 -1
  13. package/dist/client/insertParamsInPath.mjs +1 -1
  14. package/dist/client/promiseRequest.cjs +83 -26
  15. package/dist/client/promiseRequest.d.ts +46 -29
  16. package/dist/client/promiseRequest.mjs +83 -26
  17. package/dist/client/queryToString.cjs +1 -1
  18. package/dist/client/queryToString.mjs +1 -1
  19. package/dist/client/serverSentEvents.cjs +231 -0
  20. package/dist/client/serverSentEvents.d.ts +2 -0
  21. package/dist/client/serverSentEvents.mjs +208 -0
  22. package/dist/client/types/clientRequestParams.d.ts +2 -0
  23. package/dist/client/types/clientResponse.d.ts +34 -3
  24. package/dist/client/types/hooks.d.ts +17 -7
  25. package/dist/client/types/promiseRequestParams.d.ts +1 -0
  26. package/dist/client/types/serverRoute.d.ts +2 -0
  27. package/dist/core/builders/route/handler.d.ts +5 -2
  28. package/dist/core/defaultHooks/index.cjs +8 -0
  29. package/dist/core/defaultHooks/index.d.ts +1 -1
  30. package/dist/core/defaultHooks/index.mjs +8 -0
  31. package/dist/core/functionsBuilders/route/default.cjs +9 -13
  32. package/dist/core/functionsBuilders/route/default.mjs +2 -6
  33. package/dist/core/functionsBuilders/steps/create.d.ts +2 -2
  34. package/dist/core/functionsBuilders/steps/defaults/cutStep.cjs +1 -1
  35. package/dist/core/functionsBuilders/steps/defaults/cutStep.mjs +1 -1
  36. package/dist/core/functionsBuilders/steps/defaults/handlerStep.cjs +37 -17
  37. package/dist/core/functionsBuilders/steps/defaults/handlerStep.mjs +37 -17
  38. package/dist/core/functionsBuilders/steps/defaults/processStep.cjs +3 -3
  39. package/dist/core/functionsBuilders/steps/defaults/processStep.mjs +3 -3
  40. package/dist/core/hub/defaultEmptyReaderImplementation.cjs +9 -0
  41. package/dist/core/hub/defaultEmptyReaderImplementation.d.ts +1 -0
  42. package/dist/core/hub/defaultEmptyReaderImplementation.mjs +7 -0
  43. package/dist/core/hub/defaultMalformedUrlHandler.cjs +14 -0
  44. package/dist/core/hub/defaultMalformedUrlHandler.d.ts +10 -0
  45. package/dist/core/hub/defaultMalformedUrlHandler.mjs +12 -0
  46. package/dist/core/hub/defaultNotfoundHandler.d.ts +1 -1
  47. package/dist/core/hub/index.cjs +14 -1
  48. package/dist/core/hub/index.d.ts +4 -0
  49. package/dist/core/hub/index.mjs +13 -2
  50. package/dist/core/implementHttpServer.cjs +7 -2
  51. package/dist/core/implementHttpServer.d.ts +7 -1
  52. package/dist/core/implementHttpServer.mjs +5 -0
  53. package/dist/core/index.cjs +18 -0
  54. package/dist/core/index.d.ts +1 -0
  55. package/dist/core/index.mjs +8 -2
  56. package/dist/core/request/bodyController/base.cjs +24 -6
  57. package/dist/core/request/bodyController/base.d.ts +9 -0
  58. package/dist/core/request/bodyController/base.mjs +25 -8
  59. package/dist/core/request/bodyController/empty.cjs +11 -0
  60. package/dist/core/request/bodyController/empty.d.ts +3 -0
  61. package/dist/core/request/bodyController/empty.mjs +8 -0
  62. package/dist/core/request/bodyController/formData.d.ts +2 -2
  63. package/dist/core/request/bodyController/index.cjs +4 -0
  64. package/dist/core/request/bodyController/index.d.ts +1 -0
  65. package/dist/core/request/bodyController/index.mjs +2 -1
  66. package/dist/core/request/index.cjs +5 -0
  67. package/dist/core/request/index.d.ts +1 -1
  68. package/dist/core/request/index.mjs +6 -1
  69. package/dist/core/response/contract.cjs +17 -4
  70. package/dist/core/response/contract.d.ts +19 -4
  71. package/dist/core/response/contract.mjs +17 -4
  72. package/dist/core/response/index.cjs +2 -0
  73. package/dist/core/response/index.d.ts +1 -0
  74. package/dist/core/response/index.mjs +1 -0
  75. package/dist/core/response/serverSentEventsPredicted.cjs +23 -0
  76. package/dist/core/response/serverSentEventsPredicted.d.ts +14 -0
  77. package/dist/core/response/serverSentEventsPredicted.mjs +21 -0
  78. package/dist/core/route/hooks.cjs +9 -0
  79. package/dist/core/route/hooks.d.ts +10 -9
  80. package/dist/core/route/hooks.mjs +9 -1
  81. package/dist/core/route/index.cjs +1 -0
  82. package/dist/core/route/index.mjs +1 -1
  83. package/dist/core/router/buildSystemRoute.cjs +33 -0
  84. package/dist/core/router/buildSystemRoute.d.ts +11 -0
  85. package/dist/core/router/buildSystemRoute.mjs +31 -0
  86. package/dist/core/router/decodeUrl.cjs +5 -4
  87. package/dist/core/router/decodeUrl.d.ts +1 -1
  88. package/dist/core/router/decodeUrl.mjs +5 -4
  89. package/dist/core/router/index.cjs +24 -23
  90. package/dist/core/router/index.d.ts +1 -0
  91. package/dist/core/router/index.mjs +25 -25
  92. package/dist/core/serverSentEvents.cjs +96 -0
  93. package/dist/core/serverSentEvents.d.ts +33 -0
  94. package/dist/core/serverSentEvents.mjs +96 -0
  95. package/dist/core/steps/cut.d.ts +2 -2
  96. package/dist/core/steps/handler.d.ts +10 -5
  97. package/dist/interfaces/node/bodyReaders/formData/index.cjs +33 -14
  98. package/dist/interfaces/node/bodyReaders/formData/index.mjs +34 -15
  99. package/dist/interfaces/node/bodyReaders/formData/readRequestFormData.cjs +8 -11
  100. package/dist/interfaces/node/bodyReaders/formData/readRequestFormData.mjs +8 -11
  101. package/dist/interfaces/node/createHttpServer.cjs +2 -3
  102. package/dist/interfaces/node/createHttpServer.mjs +2 -3
  103. package/dist/interfaces/node/hooks/index.cjs +61 -38
  104. package/dist/interfaces/node/hooks/index.d.ts +6 -4
  105. package/dist/interfaces/node/hooks/index.mjs +61 -38
  106. package/dist/interfaces/node/index.cjs +1 -1
  107. package/dist/interfaces/node/index.mjs +1 -1
  108. package/dist/plugins/cacheController/createResponseHeader.cjs +43 -0
  109. package/dist/plugins/cacheController/createResponseHeader.d.ts +2 -0
  110. package/dist/plugins/cacheController/createResponseHeader.mjs +41 -0
  111. package/dist/plugins/cacheController/hooks.cjs +23 -0
  112. package/dist/plugins/cacheController/hooks.d.ts +4 -0
  113. package/dist/plugins/cacheController/hooks.mjs +21 -0
  114. package/dist/plugins/cacheController/index.cjs +10 -0
  115. package/dist/plugins/cacheController/index.d.ts +3 -0
  116. package/dist/plugins/cacheController/index.mjs +3 -0
  117. package/dist/plugins/cacheController/types/cacheControlDirectives.cjs +2 -0
  118. package/dist/plugins/cacheController/types/cacheControlDirectives.d.ts +16 -0
  119. package/dist/plugins/cacheController/types/cacheControlDirectives.mjs +1 -0
  120. package/dist/plugins/cacheController/types/index.cjs +4 -0
  121. package/dist/plugins/cacheController/types/index.d.ts +1 -0
  122. package/dist/plugins/cacheController/types/index.mjs +1 -0
  123. package/dist/plugins/codeGenerator/aggregateStepContract.cjs +9 -2
  124. package/dist/plugins/codeGenerator/aggregateStepContract.d.ts +1 -1
  125. package/dist/plugins/codeGenerator/aggregateStepContract.mjs +10 -3
  126. package/dist/plugins/codeGenerator/plugin.cjs +4 -4
  127. package/dist/plugins/codeGenerator/plugin.mjs +1 -1
  128. package/dist/plugins/openApiGenerator/aggregateStepContract.d.ts +2 -7
  129. package/dist/plugins/openApiGenerator/routeToOpenApi.cjs +46 -8
  130. package/dist/plugins/openApiGenerator/routeToOpenApi.d.ts +2 -2
  131. package/dist/plugins/openApiGenerator/routeToOpenApi.mjs +46 -8
  132. package/dist/plugins/openApiGenerator/types/endpointResponse.d.ts +7 -3
  133. package/dist/plugins/static/index.cjs +14 -0
  134. package/dist/plugins/static/index.d.ts +3 -0
  135. package/dist/plugins/static/index.mjs +3 -0
  136. package/dist/plugins/static/kind.cjs +9 -0
  137. package/dist/plugins/static/kind.d.ts +6 -0
  138. package/dist/plugins/static/kind.mjs +7 -0
  139. package/dist/plugins/static/makeRouteFile.cjs +62 -0
  140. package/dist/plugins/static/makeRouteFile.d.ts +48 -0
  141. package/dist/plugins/static/makeRouteFile.mjs +58 -0
  142. package/dist/plugins/static/makeRouteFolder.cjs +67 -0
  143. package/dist/plugins/static/makeRouteFolder.d.ts +39 -0
  144. package/dist/plugins/static/makeRouteFolder.mjs +65 -0
  145. package/dist/plugins/static/plugin.cjs +53 -0
  146. package/dist/plugins/static/plugin.d.ts +26 -0
  147. package/dist/plugins/static/plugin.mjs +50 -0
  148. package/package.json +18 -7
  149. /package/dist/plugins/codeGenerator/{typescriptTransfomer.cjs → typescriptTransformer.cjs} +0 -0
  150. /package/dist/plugins/codeGenerator/{typescriptTransfomer.d.ts → typescriptTransformer.d.ts} +0 -0
  151. /package/dist/plugins/codeGenerator/{typescriptTransfomer.mjs → typescriptTransformer.mjs} +0 -0
@@ -0,0 +1,65 @@
1
+ import { SDP, SF } from '@duplojs/server-utils';
2
+ import { whenNot, S, A, pipe, escapeRegExp, innerPipe, Path, E, unwrap } from '@duplojs/utils';
3
+ import '../../core/builders/index.mjs';
4
+ import '../../core/metadata/index.mjs';
5
+ import '../../core/response/index.mjs';
6
+ import { createCacheControllerHooks } from '../cacheController/hooks.mjs';
7
+ import { useRouteBuilder } from '../../core/builders/route/builder.mjs';
8
+ import { IgnoreByRouteStoreMetadata } from '../../core/metadata/ignoreByRouteStore.mjs';
9
+ import { ResponseContract } from '../../core/response/contract.mjs';
10
+
11
+ function makeRouteFolder(params) {
12
+ const sourcePath = whenNot(params.source.path, S.endsWith("/"), S.concat("/"));
13
+ const localPrefix = A.coalescing(params.prefix);
14
+ const routePath = A.mapTuple(localPrefix, (prefix) => `${prefix}/*`);
15
+ const prefixRegex = pipe(localPrefix, A.map(escapeRegExp), A.join("|"), (value) => new RegExp(`^(?:${value})(?:/|$)`));
16
+ const getResourcePath = innerPipe(S.replace(prefixRegex, ""), S.prepend(sourcePath));
17
+ return useRouteBuilder("GET", routePath, {
18
+ metadata: [IgnoreByRouteStoreMetadata()],
19
+ hooks: [createCacheControllerHooks(params.cacheControlConfig)],
20
+ })
21
+ .handler([
22
+ ResponseContract.ok("resource.found", SDP.file()),
23
+ ResponseContract.notFound("resource.notfound"),
24
+ ResponseContract.notModified("resource.notModified"),
25
+ ], async (__, { request, response }) => {
26
+ if (!Path.isAbsolute(request.path)) {
27
+ return response("resource.notfound");
28
+ }
29
+ const resourcePath = getResourcePath(request.path);
30
+ const resultStat = await SF.stat(resourcePath);
31
+ if (E.isLeft(resultStat)) {
32
+ return response("resource.notfound");
33
+ }
34
+ const stat = unwrap(resultStat);
35
+ if (stat.isDirectory && !params.directoryFallBackFile) {
36
+ return response("resource.notfound");
37
+ }
38
+ const resource = SF.createFileInterface(stat.isDirectory && params.directoryFallBackFile
39
+ ? `${resourcePath}/${params.directoryFallBackFile}`
40
+ : resourcePath);
41
+ const resultResourceStat = stat.isFile
42
+ ? stat
43
+ : await resource.stat();
44
+ if (E.isLeft(resultResourceStat)) {
45
+ return response("resource.notfound");
46
+ }
47
+ const resourceStat = unwrap(resultResourceStat);
48
+ if (!resourceStat.isFile) {
49
+ return response("resource.notfound");
50
+ }
51
+ if (request.headers["if-modified-since"]
52
+ && typeof request.headers["if-modified-since"] === "string"
53
+ && resourceStat.modifiedAt
54
+ && new Date(request.headers["if-modified-since"]).getTime() >= resourceStat.modifiedAt.getTime()) {
55
+ return response("resource.notModified")
56
+ .setHeader("last-modified", resourceStat.modifiedAt.toISOString());
57
+ }
58
+ return resourceStat.modifiedAt
59
+ ? response("resource.found", resource)
60
+ .setHeader("last-modified", resourceStat.modifiedAt.toISOString())
61
+ : response("resource.found", resource);
62
+ });
63
+ }
64
+
65
+ export { makeRouteFolder };
@@ -0,0 +1,53 @@
1
+ 'use strict';
2
+
3
+ var utils = require('@duplojs/utils');
4
+ var serverUtils = require('@duplojs/server-utils');
5
+ var kind = require('./kind.cjs');
6
+ var makeRouteFile = require('./makeRouteFile.cjs');
7
+ var makeRouteFolder = require('./makeRouteFolder.cjs');
8
+
9
+ class StaticPluginError extends utils.kindHeritage("static-plugin", kind.createStaticPluginKind("static-plugin"), Error) {
10
+ information;
11
+ error;
12
+ constructor(information, error) {
13
+ super({}, [`Error during registration static route: ${information}`]);
14
+ this.information = information;
15
+ this.error = error;
16
+ }
17
+ }
18
+ function staticPlugin(...args) {
19
+ const route = utils.P.match(args)
20
+ .with([utils.toCurriedPredicate(serverUtils.SF.isFolderInterface)], ([source, params]) => makeRouteFolder.makeRouteFolder({
21
+ source,
22
+ ...params,
23
+ }))
24
+ .with([utils.toCurriedPredicate(serverUtils.SF.isFileInterface)], ([source, params]) => makeRouteFile.makeRouteFile({
25
+ source,
26
+ ...params,
27
+ }))
28
+ .exhaustive();
29
+ return {
30
+ name: "static",
31
+ routes: [route],
32
+ hooksHubLifeCycle: [
33
+ {
34
+ beforeStartServer: async () => {
35
+ const statResult = await args[0].stat();
36
+ if (utils.E.isLeft(statResult)) {
37
+ throw new StaticPluginError(`source does not exit (${args[0].path}).`, utils.unwrap(statResult));
38
+ }
39
+ const stat = utils.unwrap(statResult);
40
+ if (serverUtils.SF.isFileInterface(args[0]) && !stat.isFile) {
41
+ throw new StaticPluginError(`source does not file (${args[0].path}).`, stat);
42
+ }
43
+ else if (serverUtils.SF.isFolderInterface(args[0]) && stat.isFile) {
44
+ throw new StaticPluginError(`source does not folder (${args[0].path}).`, stat);
45
+ }
46
+ },
47
+ },
48
+ ],
49
+ };
50
+ }
51
+
52
+ exports.StaticPluginError = StaticPluginError;
53
+ exports.staticPlugin = staticPlugin;
@@ -0,0 +1,26 @@
1
+ import { type AnyTuple } from "@duplojs/utils";
2
+ import { SF } from "@duplojs/server-utils";
3
+ import type { HubPlugin } from "../../core/hub";
4
+ import type { RoutePath } from "../../core/route";
5
+ import { type CacheControlDirectives } from "../cacheController/types";
6
+ export interface BaseStaticPluginParams {
7
+ readonly cacheControlConfig?: CacheControlDirectives;
8
+ }
9
+ export interface StaticPluginFileParams extends BaseStaticPluginParams {
10
+ readonly path: RoutePath | AnyTuple<RoutePath>;
11
+ }
12
+ export interface StaticPluginFolderParams extends BaseStaticPluginParams {
13
+ readonly prefix: RoutePath | AnyTuple<RoutePath>;
14
+ readonly directoryIndexFilePrefix?: string;
15
+ }
16
+ declare const StaticPluginError_base: new (params: {
17
+ "@DuplojsStaticPlugin/static-plugin"?: unknown;
18
+ }, parentParams: readonly [message?: string | undefined, options?: ErrorOptions | undefined]) => Error & import("@duplojs/utils").Kind<import("@duplojs/utils").KindDefinition<"@DuplojsStaticPlugin/static-plugin", unknown>, unknown> & import("@duplojs/utils").Kind<import("@duplojs/utils").KindDefinition<"static-plugin", unknown>, unknown>;
19
+ export declare class StaticPluginError extends StaticPluginError_base {
20
+ information: string;
21
+ error: unknown;
22
+ constructor(information: string, error: unknown);
23
+ }
24
+ export declare function staticPlugin(source: SF.FolderInterface, params: StaticPluginFolderParams): HubPlugin;
25
+ export declare function staticPlugin(source: SF.FileInterface, params: StaticPluginFileParams): HubPlugin;
26
+ export {};
@@ -0,0 +1,50 @@
1
+ import { kindHeritage, P, toCurriedPredicate, E, unwrap } from '@duplojs/utils';
2
+ import { SF } from '@duplojs/server-utils';
3
+ import { createStaticPluginKind } from './kind.mjs';
4
+ import { makeRouteFile } from './makeRouteFile.mjs';
5
+ import { makeRouteFolder } from './makeRouteFolder.mjs';
6
+
7
+ class StaticPluginError extends kindHeritage("static-plugin", createStaticPluginKind("static-plugin"), Error) {
8
+ information;
9
+ error;
10
+ constructor(information, error) {
11
+ super({}, [`Error during registration static route: ${information}`]);
12
+ this.information = information;
13
+ this.error = error;
14
+ }
15
+ }
16
+ function staticPlugin(...args) {
17
+ const route = P.match(args)
18
+ .with([toCurriedPredicate(SF.isFolderInterface)], ([source, params]) => makeRouteFolder({
19
+ source,
20
+ ...params,
21
+ }))
22
+ .with([toCurriedPredicate(SF.isFileInterface)], ([source, params]) => makeRouteFile({
23
+ source,
24
+ ...params,
25
+ }))
26
+ .exhaustive();
27
+ return {
28
+ name: "static",
29
+ routes: [route],
30
+ hooksHubLifeCycle: [
31
+ {
32
+ beforeStartServer: async () => {
33
+ const statResult = await args[0].stat();
34
+ if (E.isLeft(statResult)) {
35
+ throw new StaticPluginError(`source does not exit (${args[0].path}).`, unwrap(statResult));
36
+ }
37
+ const stat = unwrap(statResult);
38
+ if (SF.isFileInterface(args[0]) && !stat.isFile) {
39
+ throw new StaticPluginError(`source does not file (${args[0].path}).`, stat);
40
+ }
41
+ else if (SF.isFolderInterface(args[0]) && stat.isFile) {
42
+ throw new StaticPluginError(`source does not folder (${args[0].path}).`, stat);
43
+ }
44
+ },
45
+ },
46
+ ],
47
+ };
48
+ }
49
+
50
+ export { StaticPluginError, staticPlugin };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@duplojs/http",
3
- "version": "0.7.4",
3
+ "version": "0.8.4",
4
4
  "author": "mathcovax",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -17,9 +17,9 @@
17
17
  "test:tu:bench": "vitest bench",
18
18
  "test:tu:watch": "vitest --coverage --watch",
19
19
  "test:tu:update": "vitest --coverage --update",
20
- "test:types": "tsc -p tests/core/tsconfig.json && tsc -p tests/client/tsconfig.json && tsc -p tests/interfaces/node/tsconfig.json && tsc -p tests/interfaces/bun/tsconfig.json && tsc -p tests/interfaces/deno/tsconfig.json && tsc -p tests/plugins/codeGenerator/tsconfig.json && tsc -p tests/plugins/openApiGenerator/tsconfig.json && npm -w integration run test:types && npm -w docs run test:types",
21
- "test:lint": "eslint --quiet",
22
- "test:lint:fix": "eslint --fix --quiet",
20
+ "test:types": "./.commands/test-types.sh",
21
+ "test:lint": "./.commands/test-eslint.sh",
22
+ "test:lint:fix": "./.commands/test-eslint.sh --fix",
23
23
  "prepare": "husky"
24
24
  },
25
25
  "types": "./dist/core/index.d.ts",
@@ -58,6 +58,16 @@
58
58
  "import": "./dist/plugins/openApiGenerator/index.mjs",
59
59
  "require": "./dist/plugins/openApiGenerator/index.cjs",
60
60
  "types": "./dist/plugins/openApiGenerator/index.d.ts"
61
+ },
62
+ "./cacheController": {
63
+ "import": "./dist/plugins/cacheController/index.mjs",
64
+ "require": "./dist/plugins/cacheController/index.cjs",
65
+ "types": "./dist/plugins/cacheController/index.d.ts"
66
+ },
67
+ "./static": {
68
+ "import": "./dist/plugins/static/index.mjs",
69
+ "require": "./dist/plugins/static/index.cjs",
70
+ "types": "./dist/plugins/static/index.d.ts"
61
71
  }
62
72
  },
63
73
  "files": [
@@ -65,9 +75,9 @@
65
75
  "README.md"
66
76
  ],
67
77
  "peerDependencies": {
68
- "@duplojs/data-parser-tools": ">=0.2.8 <1.0.0",
69
- "@duplojs/server-utils": ">=0.2.0 <1.0.0",
70
- "@duplojs/utils": ">=1.5.8 <2.0.0"
78
+ "@duplojs/data-parser-tools": ">=0.2.9 <1.0.0",
79
+ "@duplojs/server-utils": ">=0.2.2 <1.0.0",
80
+ "@duplojs/utils": ">=1.5.13 <2.0.0"
71
81
  },
72
82
  "devDependencies": {
73
83
  "@commitlint/cli": "19.8.1",
@@ -80,6 +90,7 @@
80
90
  "@types/web": "^0.0.317",
81
91
  "@vitest/coverage-istanbul": "3.2.4",
82
92
  "eslint": "9.34.0",
93
+ "eventsource": "^4.1.0",
83
94
  "form-data": "^4.0.5",
84
95
  "husky": "9.1.7",
85
96
  "node-mocks-http": "^1.17.2",