@duplojs/http 0.2.0 → 0.4.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.
Files changed (151) hide show
  1. package/dist/client/getBody.cjs +22 -0
  2. package/dist/client/getBody.d.ts +1 -0
  3. package/dist/client/getBody.mjs +20 -0
  4. package/dist/client/hooks.cjs +79 -0
  5. package/dist/client/hooks.d.ts +33 -0
  6. package/dist/client/hooks.mjs +70 -0
  7. package/dist/client/httpClient.cjs +153 -0
  8. package/dist/client/httpClient.d.ts +60 -0
  9. package/dist/client/httpClient.mjs +130 -0
  10. package/dist/client/index.cjs +33 -0
  11. package/dist/client/index.d.ts +9 -0
  12. package/dist/client/index.mjs +9 -0
  13. package/dist/client/insertParamsInPath.cjs +12 -0
  14. package/dist/client/insertParamsInPath.d.ts +2 -0
  15. package/dist/client/insertParamsInPath.mjs +10 -0
  16. package/dist/client/kind.cjs +9 -0
  17. package/dist/client/kind.d.ts +6 -0
  18. package/dist/client/kind.mjs +7 -0
  19. package/dist/client/promiseRequest.cjs +355 -0
  20. package/dist/client/promiseRequest.d.ts +98 -0
  21. package/dist/client/promiseRequest.mjs +332 -0
  22. package/dist/client/queryToString.cjs +25 -0
  23. package/dist/client/queryToString.d.ts +2 -0
  24. package/dist/client/queryToString.mjs +23 -0
  25. package/dist/client/types/ObjectCanBeEmpty.cjs +2 -0
  26. package/dist/client/types/ObjectCanBeEmpty.d.ts +4 -0
  27. package/dist/client/types/ObjectCanBeEmpty.mjs +1 -0
  28. package/dist/client/types/clientRequestParams.cjs +2 -0
  29. package/dist/client/types/clientRequestParams.d.ts +51 -0
  30. package/dist/client/types/clientRequestParams.mjs +1 -0
  31. package/dist/client/types/clientResponse.cjs +2 -0
  32. package/dist/client/types/clientResponse.d.ts +34 -0
  33. package/dist/client/types/clientResponse.mjs +1 -0
  34. package/dist/client/types/index.cjs +7 -0
  35. package/dist/client/types/index.d.ts +4 -0
  36. package/dist/client/types/index.mjs +4 -0
  37. package/dist/client/types/serverRoute.cjs +2 -0
  38. package/dist/client/types/serverRoute.d.ts +22 -0
  39. package/dist/client/types/serverRoute.mjs +1 -0
  40. package/dist/client/unexpectedResponseError.cjs +44 -0
  41. package/dist/client/unexpectedResponseError.d.ts +38 -0
  42. package/dist/client/unexpectedResponseError.mjs +39 -0
  43. package/dist/core/functionsBuilders/route/default.cjs +10 -9
  44. package/dist/core/functionsBuilders/route/default.mjs +3 -2
  45. package/dist/core/functionsBuilders/route/hook.cjs +22 -22
  46. package/dist/core/functionsBuilders/route/hook.d.ts +2 -2
  47. package/dist/core/functionsBuilders/route/hook.mjs +2 -2
  48. package/dist/core/functionsBuilders/steps/create.d.ts +2 -2
  49. package/dist/core/functionsBuilders/steps/defaults/checkerStep.cjs +3 -2
  50. package/dist/core/functionsBuilders/steps/defaults/checkerStep.mjs +3 -2
  51. package/dist/core/functionsBuilders/steps/defaults/cutStep.cjs +3 -2
  52. package/dist/core/functionsBuilders/steps/defaults/cutStep.mjs +3 -2
  53. package/dist/core/functionsBuilders/steps/defaults/extractStep.cjs +6 -5
  54. package/dist/core/functionsBuilders/steps/defaults/extractStep.mjs +4 -3
  55. package/dist/core/functionsBuilders/steps/defaults/handlerStep.cjs +3 -2
  56. package/dist/core/functionsBuilders/steps/defaults/handlerStep.mjs +3 -2
  57. package/dist/core/functionsBuilders/steps/defaults/processStep.cjs +5 -4
  58. package/dist/core/functionsBuilders/steps/defaults/processStep.mjs +3 -2
  59. package/dist/core/hub/defaultNotfoundHandler.d.ts +1 -1
  60. package/dist/core/hub/index.cjs +1 -1
  61. package/dist/core/hub/index.mjs +1 -1
  62. package/dist/core/index.cjs +37 -34
  63. package/dist/core/index.mjs +5 -3
  64. package/dist/core/response/base.cjs +40 -0
  65. package/dist/core/response/base.d.ts +21 -0
  66. package/dist/core/response/base.mjs +38 -0
  67. package/dist/core/response/contract.cjs +2 -2
  68. package/dist/core/response/contract.d.ts +120 -118
  69. package/dist/core/response/contract.mjs +2 -2
  70. package/dist/core/{route/hooks/response.cjs → response/hook.cjs} +4 -3
  71. package/dist/core/{route/hooks/response.d.ts → response/hook.d.ts} +2 -2
  72. package/dist/core/{route/hooks/response.mjs → response/hook.mjs} +3 -2
  73. package/dist/core/response/index.cjs +9 -38
  74. package/dist/core/response/index.d.ts +3 -20
  75. package/dist/core/response/index.mjs +3 -38
  76. package/dist/core/response/predicted.cjs +22 -0
  77. package/dist/core/response/predicted.d.ts +11 -0
  78. package/dist/core/response/predicted.mjs +20 -0
  79. package/dist/core/route/{hooks/index.cjs → hooks.cjs} +2 -3
  80. package/dist/core/route/{hooks/index.d.ts → hooks.d.ts} +4 -4
  81. package/dist/core/route/{hooks/index.mjs → hooks.mjs} +2 -2
  82. package/dist/core/route/index.cjs +4 -4
  83. package/dist/core/route/index.mjs +1 -1
  84. package/dist/core/steps/cut.d.ts +3 -3
  85. package/dist/core/steps/handler.d.ts +3 -3
  86. package/dist/core/steps/identifier.d.ts +2 -2
  87. package/dist/core/steps/types/stepFunctionParams.d.ts +2 -2
  88. package/dist/core/types/forbiddenBigintDataParser.cjs +2 -0
  89. package/dist/core/types/forbiddenBigintDataParser.d.ts +6 -0
  90. package/dist/core/types/forbiddenBigintDataParser.mjs +1 -0
  91. package/dist/core/types/index.cjs +1 -0
  92. package/dist/core/types/index.d.ts +1 -0
  93. package/dist/core/types/index.mjs +1 -0
  94. package/dist/interfaces/node/createHttpServer.cjs +1 -0
  95. package/dist/interfaces/node/createHttpServer.d.ts +2 -1
  96. package/dist/interfaces/node/createHttpServer.mjs +1 -0
  97. package/dist/interfaces/node/hooks.cjs +16 -13
  98. package/dist/interfaces/node/hooks.mjs +15 -12
  99. package/dist/plugins/codeGenerator/index.cjs +0 -3
  100. package/dist/plugins/codeGenerator/index.mjs +1 -1
  101. package/dist/plugins/codeGenerator/plugin.cjs +1 -1
  102. package/dist/plugins/codeGenerator/plugin.mjs +1 -1
  103. package/dist/plugins/codeGenerator/routeToDataParser.cjs +4 -55
  104. package/dist/plugins/codeGenerator/routeToDataParser.d.ts +2 -181
  105. package/dist/plugins/codeGenerator/routeToDataParser.mjs +6 -54
  106. package/dist/plugins/openApiGenerator/aggregateStepContract.cjs +62 -0
  107. package/dist/plugins/openApiGenerator/aggregateStepContract.d.ts +18 -0
  108. package/dist/plugins/openApiGenerator/aggregateStepContract.mjs +60 -0
  109. package/dist/plugins/openApiGenerator/index.cjs +17 -0
  110. package/dist/plugins/openApiGenerator/index.d.ts +7 -0
  111. package/dist/plugins/openApiGenerator/index.mjs +7 -0
  112. package/dist/plugins/openApiGenerator/makeOpenApiPage.cjs +30 -0
  113. package/dist/plugins/openApiGenerator/makeOpenApiPage.d.ts +7 -0
  114. package/dist/plugins/openApiGenerator/makeOpenApiPage.mjs +28 -0
  115. package/dist/plugins/openApiGenerator/makeOpenApiRoute.cjs +15 -0
  116. package/dist/plugins/openApiGenerator/makeOpenApiRoute.d.ts +18 -0
  117. package/dist/plugins/openApiGenerator/makeOpenApiRoute.mjs +13 -0
  118. package/dist/plugins/openApiGenerator/plugin.cjs +90 -0
  119. package/dist/plugins/openApiGenerator/plugin.d.ts +49 -0
  120. package/dist/plugins/openApiGenerator/plugin.mjs +88 -0
  121. package/dist/plugins/openApiGenerator/routeToOpenApi.cjs +163 -0
  122. package/dist/plugins/openApiGenerator/routeToOpenApi.d.ts +35 -0
  123. package/dist/plugins/openApiGenerator/routeToOpenApi.mjs +161 -0
  124. package/dist/plugins/openApiGenerator/types/endpointResponse.cjs +2 -0
  125. package/dist/plugins/openApiGenerator/types/endpointResponse.d.ts +19 -0
  126. package/dist/plugins/openApiGenerator/types/endpointResponse.mjs +1 -0
  127. package/dist/plugins/openApiGenerator/types/entrypoint.cjs +2 -0
  128. package/dist/plugins/openApiGenerator/types/entrypoint.d.ts +22 -0
  129. package/dist/plugins/openApiGenerator/types/entrypoint.mjs +1 -0
  130. package/dist/plugins/openApiGenerator/types/entrypointKey.cjs +2 -0
  131. package/dist/plugins/openApiGenerator/types/entrypointKey.d.ts +1 -0
  132. package/dist/plugins/openApiGenerator/types/entrypointKey.mjs +1 -0
  133. package/dist/plugins/openApiGenerator/types/index.cjs +11 -0
  134. package/dist/plugins/openApiGenerator/types/index.d.ts +8 -0
  135. package/dist/plugins/openApiGenerator/types/index.mjs +8 -0
  136. package/dist/plugins/openApiGenerator/types/openApiDocument.cjs +2 -0
  137. package/dist/plugins/openApiGenerator/types/openApiDocument.d.ts +31 -0
  138. package/dist/plugins/openApiGenerator/types/openApiDocument.mjs +1 -0
  139. package/dist/plugins/openApiGenerator/types/openApiMethod.cjs +2 -0
  140. package/dist/plugins/openApiGenerator/types/openApiMethod.d.ts +1 -0
  141. package/dist/plugins/openApiGenerator/types/openApiMethod.mjs +1 -0
  142. package/dist/plugins/openApiGenerator/types/openApiOperation.cjs +2 -0
  143. package/dist/plugins/openApiGenerator/types/openApiOperation.d.ts +8 -0
  144. package/dist/plugins/openApiGenerator/types/openApiOperation.mjs +1 -0
  145. package/dist/plugins/openApiGenerator/types/openApiPath.cjs +2 -0
  146. package/dist/plugins/openApiGenerator/types/openApiPath.d.ts +4 -0
  147. package/dist/plugins/openApiGenerator/types/openApiPath.mjs +1 -0
  148. package/dist/plugins/openApiGenerator/types/openApiSecuritySchema.cjs +2 -0
  149. package/dist/plugins/openApiGenerator/types/openApiSecuritySchema.d.ts +8 -0
  150. package/dist/plugins/openApiGenerator/types/openApiSecuritySchema.mjs +1 -0
  151. package/package.json +17 -4
@@ -0,0 +1,49 @@
1
+ import type { HubPlugin } from "../../core/hub";
2
+ import type { RoutePath } from "../../core/route";
3
+ import type { SupportedBearerFormat } from "./types";
4
+ interface OpenApiSecurityOptionBearer {
5
+ type: "bearer";
6
+ bearerFormat?: SupportedBearerFormat;
7
+ }
8
+ interface OpenApiSecurityOptionApiKey {
9
+ type: "apiKey";
10
+ paramName: string;
11
+ in: "header" | "query" | "cookie";
12
+ }
13
+ interface OpenApiSecurityOptionBasic {
14
+ type: "basic";
15
+ }
16
+ export interface OpenApiGeneratorPluginParams {
17
+ routePath: RoutePath;
18
+ outputFilePath?: string;
19
+ /**
20
+ * @default "Swagger API"
21
+ */
22
+ title?: string;
23
+ /**
24
+ * @default "0.0.0"
25
+ */
26
+ version?: string;
27
+ summary?: string;
28
+ contact?: {
29
+ name?: string;
30
+ email?: string;
31
+ url?: string;
32
+ };
33
+ license?: {
34
+ name: string;
35
+ url?: string;
36
+ identifier?: string;
37
+ };
38
+ security?: OpenApiSecurityOptionBearer | OpenApiSecurityOptionApiKey | OpenApiSecurityOptionBasic;
39
+ servers?: {
40
+ url: string;
41
+ description?: string;
42
+ }[];
43
+ /**
44
+ * @default "5.31.0"
45
+ */
46
+ swaggerUiVersion?: string;
47
+ }
48
+ export declare function openApiGeneratorPlugin(pluginParams: OpenApiGeneratorPluginParams): () => HubPlugin;
49
+ export {};
@@ -0,0 +1,88 @@
1
+ import { routeToOpenApi } from './routeToOpenApi.mjs';
2
+ import { pipe, A, O, G, P, justReturn } from '@duplojs/utils';
3
+ import { makeOpenApiPage } from './makeOpenApiPage.mjs';
4
+ import { makeOpenApiRoute } from './makeOpenApiRoute.mjs';
5
+ import { writeFile } from 'fs/promises';
6
+
7
+ function openApiGeneratorPlugin(pluginParams) {
8
+ return () => ({
9
+ name: "open-api-generator",
10
+ hooksHubLifeCycle: [
11
+ {
12
+ beforeServerBuildRoutes: async (hub) => {
13
+ const contextToJsonSchemaFactory = new Map();
14
+ const resultSchemaContext = new Map();
15
+ const routes = hub.aggregatesRoutes();
16
+ const openApiRoutes = pipe(routes, A.filter((route) => route.definition.method !== "OPTIONS"), A.flatMap((route) => routeToOpenApi(route, {
17
+ defaultExtractContract: hub.defaultExtractContract,
18
+ resultSchemaContext,
19
+ contextToJsonSchemaFactory,
20
+ })));
21
+ if (!A.minElements(openApiRoutes, 1)) {
22
+ return;
23
+ }
24
+ const paths = pipe(openApiRoutes, A.group((element, { output }) => output(element.path, element)), O.entries, A.filter((entry) => entry[1] !== undefined), A.map(([path, value]) => pipe(value, A.group(({ method, path, ...rest }, { output }) => output(method, rest)), O.entries, A.filter((entry) => entry[1] !== undefined), A.map(([method, value]) => O.entry(method, A.first(value))), O.fromEntries, (value) => O.entry(path, value))), O.fromEntries);
25
+ const schemaComponents = G.reduce(resultSchemaContext.values(), G.reduceFrom({}), ({ lastValue, element, nextWithObject }) => nextWithObject(lastValue, element));
26
+ const securityScheme = P.match(pluginParams.security)
27
+ .with({ type: "bearer" }, (security) => ({
28
+ type: "http",
29
+ scheme: "bearer",
30
+ bearerFormat: security.bearerFormat ?? "JWT",
31
+ }))
32
+ .with({ type: "basic" }, justReturn({
33
+ type: "http",
34
+ scheme: "basic",
35
+ }))
36
+ .with({ type: "apiKey" }, (security) => ({
37
+ type: "apiKey",
38
+ name: security.paramName,
39
+ in: security.in,
40
+ }))
41
+ .otherwise(justReturn(undefined));
42
+ const securitySchemeName = "auth";
43
+ const securitySchemes = securityScheme
44
+ ? {
45
+ [securitySchemeName]: securityScheme,
46
+ }
47
+ : undefined;
48
+ const openApiDocument = {
49
+ openapi: "3.1.0",
50
+ info: {
51
+ title: pluginParams.title ?? "Swagger API",
52
+ version: pluginParams.version ?? "0.0.0",
53
+ summary: pluginParams.summary,
54
+ contact: pluginParams.contact,
55
+ license: pluginParams.license,
56
+ },
57
+ servers: pluginParams.servers,
58
+ paths,
59
+ components: {
60
+ schemas: schemaComponents,
61
+ securitySchemes,
62
+ },
63
+ security: pluginParams.security
64
+ ? [
65
+ {
66
+ [securitySchemeName]: [],
67
+ },
68
+ ]
69
+ : undefined,
70
+ };
71
+ const openApiDocumentString = JSON.stringify(openApiDocument, null, 2);
72
+ if (pluginParams.outputFilePath) {
73
+ await writeFile(pluginParams.outputFilePath, openApiDocumentString);
74
+ }
75
+ const openApiPage = makeOpenApiPage({
76
+ openApiDocument: openApiDocumentString,
77
+ pageTitle: pluginParams.title ?? "Swagger API",
78
+ swaggerUiVersion: pluginParams.swaggerUiVersion ?? "5.31.0",
79
+ });
80
+ const openApiRoute = makeOpenApiRoute(pluginParams.routePath, openApiPage);
81
+ return hub.register(openApiRoute);
82
+ },
83
+ },
84
+ ],
85
+ });
86
+ }
87
+
88
+ export { openApiGeneratorPlugin };
@@ -0,0 +1,163 @@
1
+ 'use strict';
2
+
3
+ var aggregateStepContract = require('./aggregateStepContract.cjs');
4
+ var utils = require('@duplojs/utils');
5
+ var toJsonSchema = require('@duplojs/data-parser-tools/toJsonSchema');
6
+
7
+ function factoryJsonSchema(params) {
8
+ const identifier = params.schema.definition.identifier
9
+ ?? `NotIdentified${params.resultSchemaContext.size}`;
10
+ const renderResult = toJsonSchema.render(params.schema, {
11
+ identifier,
12
+ mode: params.mode,
13
+ context: params.context,
14
+ version: "openApi31",
15
+ transformers: toJsonSchema.defaultTransformers,
16
+ });
17
+ params.resultSchemaContext.set(identifier, renderResult.components.schemas);
18
+ return utils.O.pick(renderResult, { $ref: true });
19
+ }
20
+ const parameterKeyMapper = {
21
+ query: "query",
22
+ params: "path",
23
+ headers: "header",
24
+ };
25
+ const methodMapper = {
26
+ GET: "get",
27
+ POST: "post",
28
+ PUT: "put",
29
+ DELETE: "delete",
30
+ HEAD: "head",
31
+ TRACE: "trace",
32
+ CONNECT: "connect",
33
+ OPTIONS: "options",
34
+ };
35
+ function routeToOpenApi(route, params) {
36
+ const aggregateStepResult = aggregateStepContract.aggregateStepContract([
37
+ ...route.definition.preflightSteps,
38
+ ...route.definition.steps,
39
+ ], {
40
+ defaultExtractContract: params.defaultExtractContract,
41
+ });
42
+ const { body, ...restEntrypoint } = aggregateStepResult.entrypointContract;
43
+ const parameters = utils.pipe(restEntrypoint, utils.O.entries, utils.A.select(({ element: [key, value], select, skip }) => !utils.DP.dataParserKind.has(value) && utils.O.countKeys(value)
44
+ ? select(utils.O.entry(key, utils.O.entries(value)))
45
+ : skip()), utils.A.flatMap(([key, value]) => utils.A.map(value, ([name, schema]) => ({
46
+ name,
47
+ in: parameterKeyMapper[key],
48
+ required: !utils.DP.identifier(schema, utils.DP.optionalKind),
49
+ schema: factoryJsonSchema({
50
+ context: params.contextToJsonSchemaFactory,
51
+ resultSchemaContext: params.resultSchemaContext,
52
+ mode: "in",
53
+ schema,
54
+ }),
55
+ }))));
56
+ const requestBody = utils.pipe(body, utils.when((value) => utils.O.countKeys(value) === 0, utils.justReturn(utils.DP.empty())), utils.whenNot(utils.DP.dataParserKind.has, utils.DP.object), utils.P.when(utils.DP.identifier(utils.DP.emptyKind), utils.justReturn(undefined)), utils.P.when(utils.DP.identifier(utils.DP.objectKind), (objectSchema) => ({
57
+ required: true,
58
+ content: {
59
+ "application/json": {
60
+ schema: factoryJsonSchema({
61
+ context: params.contextToJsonSchemaFactory,
62
+ resultSchemaContext: params.resultSchemaContext,
63
+ mode: "in",
64
+ schema: objectSchema,
65
+ }),
66
+ },
67
+ },
68
+ })), utils.P.otherwise((primitiveSchema) => ({
69
+ required: true,
70
+ content: {
71
+ "text/plain": {
72
+ schema: factoryJsonSchema({
73
+ context: params.contextToJsonSchemaFactory,
74
+ resultSchemaContext: params.resultSchemaContext,
75
+ mode: "in",
76
+ schema: primitiveSchema,
77
+ }),
78
+ },
79
+ },
80
+ })));
81
+ const responses = utils.pipe(aggregateStepResult.endpointContract, utils.A.reduce(utils.A.reduceFrom({}), ({ lastValue, element: { information, body, code }, nextWithObject }) => {
82
+ const schemaResponse = !utils.DP.identifier(body, utils.DP.emptyKind)
83
+ ? factoryJsonSchema({
84
+ context: params.contextToJsonSchemaFactory,
85
+ resultSchemaContext: params.resultSchemaContext,
86
+ mode: "in",
87
+ schema: body,
88
+ })
89
+ : undefined;
90
+ const headerInformation = {
91
+ const: information,
92
+ type: "string",
93
+ };
94
+ const headerDescription = utils.P.match(lastValue[code])
95
+ .when(utils.isType("object"), (value) => {
96
+ if (utils.S.includes(value.headers.information.description, information)) {
97
+ return value.headers.information.description;
98
+ }
99
+ return utils.S.concat(value.headers.information.description, " | ", information);
100
+ })
101
+ .when(utils.isType("undefined"), utils.justReturn(information))
102
+ .exhaustive();
103
+ const headers = {
104
+ information: {
105
+ schema: lastValue[code]
106
+ ? {
107
+ anyOf: [
108
+ lastValue[code].headers.information.schema,
109
+ headerInformation,
110
+ ],
111
+ }
112
+ : headerInformation,
113
+ description: headerDescription,
114
+ },
115
+ };
116
+ const content = utils.pipe(body, utils.P.when(utils.DP.identifier(utils.DP.emptyKind), utils.justReturn(lastValue[code]?.content)), utils.P.otherwise((value) => {
117
+ if (utils.DP.identifier(value, utils.DP.stringKind) && lastValue[code]?.content?.["plain/text"]) {
118
+ return lastValue[code].content;
119
+ }
120
+ if (utils.DP.identifier(value, utils.DP.stringKind)) {
121
+ return {
122
+ ...lastValue[code]?.content,
123
+ "plain/text": {
124
+ schema: schemaResponse,
125
+ },
126
+ };
127
+ }
128
+ if (utils.DP.identifier(value, utils.DP.objectKind) && lastValue[code]?.content?.["application/json"]) {
129
+ return {
130
+ ...lastValue[code]?.content,
131
+ "application/json": {
132
+ schema: {
133
+ anyOf: [
134
+ lastValue[code].content["application/json"].schema,
135
+ schemaResponse,
136
+ ],
137
+ },
138
+ },
139
+ };
140
+ }
141
+ return {
142
+ "application/json": {
143
+ schema: schemaResponse,
144
+ },
145
+ };
146
+ }));
147
+ return nextWithObject(lastValue, {
148
+ [code]: {
149
+ headers,
150
+ content,
151
+ },
152
+ });
153
+ }));
154
+ return utils.A.map(route.definition.paths, (path) => ({
155
+ path,
156
+ method: methodMapper[route.definition.method],
157
+ parameters,
158
+ requestBody,
159
+ responses,
160
+ }));
161
+ }
162
+
163
+ exports.routeToOpenApi = routeToOpenApi;
@@ -0,0 +1,35 @@
1
+ import type { Route } from "../../core/route";
2
+ import type { ResponseCode, ResponseContract } from "../../core/response";
3
+ import { type MapContext, type JsonSchema } from "@duplojs/data-parser-tools/toJsonSchema";
4
+ import type { EndpointResponse, EntrypointParameter } from "./types";
5
+ export type ResultSchemaContext = Map<string, Record<string, JsonSchema>>;
6
+ export interface RouteToOpenApiParams {
7
+ readonly contextToJsonSchemaFactory: MapContext;
8
+ readonly resultSchemaContext: ResultSchemaContext;
9
+ readonly defaultExtractContract: ResponseContract.Contract;
10
+ }
11
+ export declare function routeToOpenApi(route: Route, params: RouteToOpenApiParams): {
12
+ path: `/${string}`;
13
+ method: "options" | "get" | "post" | "put" | "delete" | "head" | "trace" | "connect";
14
+ parameters: EntrypointParameter[];
15
+ requestBody: {
16
+ required: true;
17
+ content: {
18
+ "application/json": {
19
+ schema: {
20
+ $ref: `#/components/schemas/${string}`;
21
+ };
22
+ };
23
+ };
24
+ } | {
25
+ required: true;
26
+ content: {
27
+ "text/plain": {
28
+ schema: {
29
+ $ref: `#/components/schemas/${string}`;
30
+ };
31
+ };
32
+ };
33
+ } | undefined;
34
+ responses: Partial<Record<ResponseCode, EndpointResponse>>;
35
+ }[];
@@ -0,0 +1,161 @@
1
+ import { aggregateStepContract } from './aggregateStepContract.mjs';
2
+ import { O, pipe, A, DP, when, justReturn, whenNot, P, isType, S } from '@duplojs/utils';
3
+ import { render, defaultTransformers } from '@duplojs/data-parser-tools/toJsonSchema';
4
+
5
+ function factoryJsonSchema(params) {
6
+ const identifier = params.schema.definition.identifier
7
+ ?? `NotIdentified${params.resultSchemaContext.size}`;
8
+ const renderResult = render(params.schema, {
9
+ identifier,
10
+ mode: params.mode,
11
+ context: params.context,
12
+ version: "openApi31",
13
+ transformers: defaultTransformers,
14
+ });
15
+ params.resultSchemaContext.set(identifier, renderResult.components.schemas);
16
+ return O.pick(renderResult, { $ref: true });
17
+ }
18
+ const parameterKeyMapper = {
19
+ query: "query",
20
+ params: "path",
21
+ headers: "header",
22
+ };
23
+ const methodMapper = {
24
+ GET: "get",
25
+ POST: "post",
26
+ PUT: "put",
27
+ DELETE: "delete",
28
+ HEAD: "head",
29
+ TRACE: "trace",
30
+ CONNECT: "connect",
31
+ OPTIONS: "options",
32
+ };
33
+ function routeToOpenApi(route, params) {
34
+ const aggregateStepResult = aggregateStepContract([
35
+ ...route.definition.preflightSteps,
36
+ ...route.definition.steps,
37
+ ], {
38
+ defaultExtractContract: params.defaultExtractContract,
39
+ });
40
+ const { body, ...restEntrypoint } = aggregateStepResult.entrypointContract;
41
+ const parameters = pipe(restEntrypoint, O.entries, A.select(({ element: [key, value], select, skip }) => !DP.dataParserKind.has(value) && O.countKeys(value)
42
+ ? select(O.entry(key, O.entries(value)))
43
+ : skip()), A.flatMap(([key, value]) => A.map(value, ([name, schema]) => ({
44
+ name,
45
+ in: parameterKeyMapper[key],
46
+ required: !DP.identifier(schema, DP.optionalKind),
47
+ schema: factoryJsonSchema({
48
+ context: params.contextToJsonSchemaFactory,
49
+ resultSchemaContext: params.resultSchemaContext,
50
+ mode: "in",
51
+ schema,
52
+ }),
53
+ }))));
54
+ const requestBody = pipe(body, when((value) => O.countKeys(value) === 0, justReturn(DP.empty())), whenNot(DP.dataParserKind.has, DP.object), P.when(DP.identifier(DP.emptyKind), justReturn(undefined)), P.when(DP.identifier(DP.objectKind), (objectSchema) => ({
55
+ required: true,
56
+ content: {
57
+ "application/json": {
58
+ schema: factoryJsonSchema({
59
+ context: params.contextToJsonSchemaFactory,
60
+ resultSchemaContext: params.resultSchemaContext,
61
+ mode: "in",
62
+ schema: objectSchema,
63
+ }),
64
+ },
65
+ },
66
+ })), P.otherwise((primitiveSchema) => ({
67
+ required: true,
68
+ content: {
69
+ "text/plain": {
70
+ schema: factoryJsonSchema({
71
+ context: params.contextToJsonSchemaFactory,
72
+ resultSchemaContext: params.resultSchemaContext,
73
+ mode: "in",
74
+ schema: primitiveSchema,
75
+ }),
76
+ },
77
+ },
78
+ })));
79
+ const responses = pipe(aggregateStepResult.endpointContract, A.reduce(A.reduceFrom({}), ({ lastValue, element: { information, body, code }, nextWithObject }) => {
80
+ const schemaResponse = !DP.identifier(body, DP.emptyKind)
81
+ ? factoryJsonSchema({
82
+ context: params.contextToJsonSchemaFactory,
83
+ resultSchemaContext: params.resultSchemaContext,
84
+ mode: "in",
85
+ schema: body,
86
+ })
87
+ : undefined;
88
+ const headerInformation = {
89
+ const: information,
90
+ type: "string",
91
+ };
92
+ const headerDescription = P.match(lastValue[code])
93
+ .when(isType("object"), (value) => {
94
+ if (S.includes(value.headers.information.description, information)) {
95
+ return value.headers.information.description;
96
+ }
97
+ return S.concat(value.headers.information.description, " | ", information);
98
+ })
99
+ .when(isType("undefined"), justReturn(information))
100
+ .exhaustive();
101
+ const headers = {
102
+ information: {
103
+ schema: lastValue[code]
104
+ ? {
105
+ anyOf: [
106
+ lastValue[code].headers.information.schema,
107
+ headerInformation,
108
+ ],
109
+ }
110
+ : headerInformation,
111
+ description: headerDescription,
112
+ },
113
+ };
114
+ const content = pipe(body, P.when(DP.identifier(DP.emptyKind), justReturn(lastValue[code]?.content)), P.otherwise((value) => {
115
+ if (DP.identifier(value, DP.stringKind) && lastValue[code]?.content?.["plain/text"]) {
116
+ return lastValue[code].content;
117
+ }
118
+ if (DP.identifier(value, DP.stringKind)) {
119
+ return {
120
+ ...lastValue[code]?.content,
121
+ "plain/text": {
122
+ schema: schemaResponse,
123
+ },
124
+ };
125
+ }
126
+ if (DP.identifier(value, DP.objectKind) && lastValue[code]?.content?.["application/json"]) {
127
+ return {
128
+ ...lastValue[code]?.content,
129
+ "application/json": {
130
+ schema: {
131
+ anyOf: [
132
+ lastValue[code].content["application/json"].schema,
133
+ schemaResponse,
134
+ ],
135
+ },
136
+ },
137
+ };
138
+ }
139
+ return {
140
+ "application/json": {
141
+ schema: schemaResponse,
142
+ },
143
+ };
144
+ }));
145
+ return nextWithObject(lastValue, {
146
+ [code]: {
147
+ headers,
148
+ content,
149
+ },
150
+ });
151
+ }));
152
+ return A.map(route.definition.paths, (path) => ({
153
+ path,
154
+ method: methodMapper[route.definition.method],
155
+ parameters,
156
+ requestBody,
157
+ responses,
158
+ }));
159
+ }
160
+
161
+ export { routeToOpenApi };
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+
@@ -0,0 +1,19 @@
1
+ import type { JsonSchema, JsonSchemaLiteral, JsonSchemaString } from "@duplojs/data-parser-tools/toJsonSchema";
2
+ export interface EndpointResponseHeader {
3
+ information: {
4
+ schema: JsonSchemaLiteral;
5
+ description: string;
6
+ };
7
+ }
8
+ export interface EndpointResponseContent {
9
+ "application/json"?: {
10
+ schema: JsonSchema;
11
+ };
12
+ "plain/text"?: {
13
+ schema: JsonSchemaString;
14
+ };
15
+ }
16
+ export interface EndpointResponse {
17
+ headers: EndpointResponseHeader;
18
+ content?: EndpointResponseContent;
19
+ }
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+
@@ -0,0 +1,22 @@
1
+ import type { JsonSchema } from "@duplojs/data-parser-tools/toJsonSchema";
2
+ export interface EntrypointParameter {
3
+ name: string;
4
+ in: "path" | "query" | "header";
5
+ required: boolean;
6
+ schema: JsonSchema;
7
+ }
8
+ export interface EntrypointContentBodyApplicationJson {
9
+ "application/json": {
10
+ schema: JsonSchema;
11
+ };
12
+ }
13
+ export interface EntrypointContentBodyTextPlain {
14
+ "text/plain": {
15
+ schema: JsonSchema;
16
+ };
17
+ }
18
+ export type EntrypointContentBody = EntrypointContentBodyApplicationJson | EntrypointContentBodyTextPlain;
19
+ export interface EntrypointRequestBody {
20
+ required: true;
21
+ content: EntrypointContentBody;
22
+ }
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+
@@ -0,0 +1 @@
1
+ export type EntrypointKey = "query" | "body" | "params" | "headers";
@@ -0,0 +1,11 @@
1
+ 'use strict';
2
+
3
+ require('./entrypointKey.cjs');
4
+ require('./openApiMethod.cjs');
5
+ require('./entrypoint.cjs');
6
+ require('./openApiSecuritySchema.cjs');
7
+ require('./endpointResponse.cjs');
8
+ require('./openApiOperation.cjs');
9
+ require('./openApiPath.cjs');
10
+ require('./openApiDocument.cjs');
11
+
@@ -0,0 +1,8 @@
1
+ export * from "./entrypointKey";
2
+ export * from "./openApiMethod";
3
+ export * from "./entrypoint";
4
+ export * from "./openApiSecuritySchema";
5
+ export * from "./endpointResponse";
6
+ export * from "./openApiOperation";
7
+ export * from "./openApiPath";
8
+ export * from "./openApiDocument";
@@ -0,0 +1,8 @@
1
+ import './entrypointKey.mjs';
2
+ import './openApiMethod.mjs';
3
+ import './entrypoint.mjs';
4
+ import './openApiSecuritySchema.mjs';
5
+ import './endpointResponse.mjs';
6
+ import './openApiOperation.mjs';
7
+ import './openApiPath.mjs';
8
+ import './openApiDocument.mjs';
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+
@@ -0,0 +1,31 @@
1
+ import type { JsonSchema } from "@duplojs/data-parser-tools/toJsonSchema";
2
+ import type { OpenApiSecuritySchema } from "./openApiSecuritySchema";
3
+ import type { OpenApiPath } from "./openApiPath";
4
+ export interface OpenApiDocument {
5
+ openapi: "3.1.0";
6
+ info: {
7
+ title: string;
8
+ version: string;
9
+ summary?: string;
10
+ contact?: {
11
+ name?: string;
12
+ email?: string;
13
+ url?: string;
14
+ };
15
+ license?: {
16
+ name: string;
17
+ url?: string;
18
+ identifier?: string;
19
+ };
20
+ };
21
+ servers?: {
22
+ url: string;
23
+ description?: string;
24
+ }[];
25
+ paths: OpenApiPath;
26
+ components: {
27
+ schemas: Record<string, JsonSchema>;
28
+ securitySchemes?: Record<string, OpenApiSecuritySchema>;
29
+ };
30
+ security?: Record<string, string[]>[];
31
+ }
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+
@@ -0,0 +1 @@
1
+ export type OpenApiMethod = "get" | "post" | "put" | "delete" | "head" | "trace" | "connect" | "options";
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+
@@ -0,0 +1,8 @@
1
+ import type { ResponseCode } from "../../../core/response";
2
+ import type { EntrypointParameter, EntrypointRequestBody } from "./entrypoint";
3
+ import type { EndpointResponse } from "./endpointResponse";
4
+ export interface OpenApiOperation {
5
+ parameters?: EntrypointParameter[];
6
+ requestBody?: EntrypointRequestBody;
7
+ responses: Partial<Record<ResponseCode, EndpointResponse>>;
8
+ }
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+