transit-kit 0.4.4 → 0.6.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/{cli → generator}/generateOpenApi.d.ts +6 -1
- package/dist/{cli → generator}/generateOpenApi.js +28 -38
- package/dist/{cli → generator}/generateOpenApi.spec.js +2 -2
- package/dist/generator/index.d.ts +1 -0
- package/dist/generator/index.js +1 -0
- package/dist/server/handlers/api/EndpointHandler.d.ts +4 -2
- package/dist/server/handlers/api/createApiHandler.js +5 -5
- package/dist/server/handlers/api/responses/emptyResponse.spec.js +9 -0
- package/dist/server/handlers/api/responses/jsonResponse.spec.d.ts +1 -0
- package/dist/server/handlers/api/responses/jsonResponse.spec.js +39 -0
- package/package.json +6 -3
- package/dist/cli/cli.js +0 -21
- /package/dist/{cli → generator}/generateOpenApi.spec.d.ts +0 -0
- /package/dist/{cli/cli.d.ts → server/handlers/api/responses/emptyResponse.spec.d.ts} +0 -0
|
@@ -3,8 +3,13 @@ import { ApiEndpointDefinition } from "../server/handlers/api/EndpointDefinition
|
|
|
3
3
|
import { GenericResponseSchemaMap } from "../server/handlers/api/responses";
|
|
4
4
|
import { ZodType } from "zod";
|
|
5
5
|
import { OpenAPIV3 } from "openapi-types";
|
|
6
|
+
import { Server } from "../server";
|
|
6
7
|
declare function translateToOpenAPIPathItem(definition: ApiEndpointDefinition<string, HttpMethod, ZodType | undefined, ZodType | undefined, GenericResponseSchemaMap>): [string, OpenAPIV3.PathItemObject];
|
|
7
|
-
|
|
8
|
+
interface GeneratorOptions {
|
|
9
|
+
title: string;
|
|
10
|
+
version: string;
|
|
11
|
+
}
|
|
12
|
+
export declare function generateOpenApiDoc(server: Server, options: GeneratorOptions): Promise<OpenAPIV3.Document>;
|
|
8
13
|
export declare const __TEST_EXPORTS: {
|
|
9
14
|
translateToOpenAPIPathItem: typeof translateToOpenAPIPathItem;
|
|
10
15
|
};
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import z from "zod";
|
|
2
2
|
import { hasValue } from "../server/utils/typeGuards";
|
|
3
|
-
import path from "path";
|
|
4
3
|
import { isJsonResponseSchema } from "../server/handlers/api/responses/jsonResponse";
|
|
5
4
|
function extractPathAndParameters(path) {
|
|
6
5
|
const parameters = path.match(/:([a-zA-Z0-9_]+)/g)?.map((param) => {
|
|
@@ -137,43 +136,34 @@ function extractSecuritySchemes(endpointDefinitions) {
|
|
|
137
136
|
return { ...acc, ...scheme };
|
|
138
137
|
}, {});
|
|
139
138
|
}
|
|
140
|
-
export async function generateOpenApiDoc(
|
|
141
|
-
const
|
|
142
|
-
const
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
components: {
|
|
169
|
-
securitySchemes,
|
|
170
|
-
},
|
|
171
|
-
};
|
|
172
|
-
return openApiDocument;
|
|
173
|
-
}
|
|
174
|
-
else {
|
|
175
|
-
throw new Error("The specified module does not export a valid server instance.");
|
|
176
|
-
}
|
|
139
|
+
export async function generateOpenApiDoc(server, options) {
|
|
140
|
+
const endpointDefinitions = server.endpointDefinitions;
|
|
141
|
+
const paths = endpointDefinitions.reduce((acc, def) => {
|
|
142
|
+
const [openApiPath, pathItem] = translateToOpenAPIPathItem(def);
|
|
143
|
+
if (acc[openApiPath]) {
|
|
144
|
+
acc[openApiPath] = {
|
|
145
|
+
...acc[openApiPath],
|
|
146
|
+
...pathItem,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
acc[openApiPath] = pathItem;
|
|
151
|
+
}
|
|
152
|
+
return acc;
|
|
153
|
+
}, {});
|
|
154
|
+
const securitySchemes = extractSecuritySchemes(endpointDefinitions);
|
|
155
|
+
const openApiDocument = {
|
|
156
|
+
openapi: "3.0.0",
|
|
157
|
+
info: {
|
|
158
|
+
title: options.title,
|
|
159
|
+
version: options.version,
|
|
160
|
+
},
|
|
161
|
+
paths: paths,
|
|
162
|
+
components: {
|
|
163
|
+
securitySchemes,
|
|
164
|
+
},
|
|
165
|
+
};
|
|
166
|
+
return openApiDocument;
|
|
177
167
|
}
|
|
178
168
|
export const __TEST_EXPORTS = {
|
|
179
169
|
translateToOpenAPIPathItem,
|
|
@@ -20,11 +20,11 @@ describe("translateToOpenAPIPathItem", () => {
|
|
|
20
20
|
dataSchema: z.string(),
|
|
21
21
|
},
|
|
22
22
|
},
|
|
23
|
-
}, async (
|
|
23
|
+
}, async ({ request }) => {
|
|
24
24
|
return {
|
|
25
25
|
code: 200,
|
|
26
26
|
dataType: "application/json",
|
|
27
|
-
json:
|
|
27
|
+
json: request.params.id,
|
|
28
28
|
};
|
|
29
29
|
});
|
|
30
30
|
const [path, pathItem] = translateToOpenAPIPathItem(definition);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { generateOpenApiDoc } from "./generateOpenApi";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { generateOpenApiDoc } from "./generateOpenApi";
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import { Request } from "express";
|
|
1
|
+
import { Request, Response } from "express";
|
|
2
2
|
import { HttpStatusCodes } from "../../constants/HttpStatusCodes";
|
|
3
3
|
import { GenericResponse } from "./responses";
|
|
4
|
-
export type ApiEndpointHandler<PathParams extends Record<string, string> = {}, RequestBody = unknown, Query = unknown, Responses extends GenericResponse = never, Caller = unknown> = (
|
|
4
|
+
export type ApiEndpointHandler<PathParams extends Record<string, string> = {}, RequestBody = unknown, Query = unknown, Responses extends GenericResponse = never, Caller = unknown> = (typedRequestData: {
|
|
5
|
+
request: Request<PathParams, unknown, RequestBody, Query, Record<string, unknown>>;
|
|
6
|
+
response: Response<unknown>;
|
|
5
7
|
parameters: PathParams;
|
|
6
8
|
query: Query;
|
|
7
9
|
body: RequestBody;
|
|
@@ -8,14 +8,14 @@ export function createApiEndpointHandler(definition, handler) {
|
|
|
8
8
|
}
|
|
9
9
|
export function buildApiEndpointHandler(handler) {
|
|
10
10
|
return expressAsyncHandler(async (request, response) => {
|
|
11
|
-
const
|
|
12
|
-
|
|
11
|
+
const result = await handler({
|
|
12
|
+
request,
|
|
13
|
+
response,
|
|
13
14
|
parameters: request.params,
|
|
14
15
|
query: request.query,
|
|
15
16
|
body: request.body,
|
|
16
|
-
caller,
|
|
17
|
-
};
|
|
18
|
-
const result = await handler(request, extractedRequestData);
|
|
17
|
+
caller: response.locals.caller,
|
|
18
|
+
});
|
|
19
19
|
if (isJsonResponse(result)) {
|
|
20
20
|
response.status(result.code).json(result.json);
|
|
21
21
|
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { describe, expectTypeOf, it } from "vitest";
|
|
2
|
+
describe("empty response", () => {
|
|
3
|
+
it("can correctly infer the response type from schema", () => {
|
|
4
|
+
expectTypeOf().toEqualTypeOf();
|
|
5
|
+
});
|
|
6
|
+
it("can correctly identify a literal with the type", () => {
|
|
7
|
+
expectTypeOf().toEqualTypeOf();
|
|
8
|
+
});
|
|
9
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { describe, expect, expectTypeOf, it } from "vitest";
|
|
2
|
+
import z from "zod";
|
|
3
|
+
import { isJsonResponse, isJsonResponseSchema, } from "./jsonResponse";
|
|
4
|
+
describe("empty response", () => {
|
|
5
|
+
it("can correctly infer the response type from schema", () => {
|
|
6
|
+
expectTypeOf().toEqualTypeOf();
|
|
7
|
+
});
|
|
8
|
+
it("can correctly identify a literal with the type", () => {
|
|
9
|
+
expectTypeOf().toEqualTypeOf();
|
|
10
|
+
});
|
|
11
|
+
it("can correctly validate if a response is of type", () => {
|
|
12
|
+
const res = {
|
|
13
|
+
dataType: "application/json",
|
|
14
|
+
json: "string",
|
|
15
|
+
};
|
|
16
|
+
expect(isJsonResponse(res)).toBe(true);
|
|
17
|
+
});
|
|
18
|
+
it("can correctly validate if a response is not of type", () => {
|
|
19
|
+
const res = {
|
|
20
|
+
dataType: "application/data", //<=== Wrong
|
|
21
|
+
json: "string",
|
|
22
|
+
};
|
|
23
|
+
expect(isJsonResponse(res)).toBe(false);
|
|
24
|
+
});
|
|
25
|
+
it("can correctly validate if a response schema is of type", () => {
|
|
26
|
+
const res = {
|
|
27
|
+
dataType: "application/json",
|
|
28
|
+
dataSchema: z.string(),
|
|
29
|
+
};
|
|
30
|
+
expect(isJsonResponseSchema(res)).toBe(true);
|
|
31
|
+
});
|
|
32
|
+
it("can correctly validate if a response schema is not of type", () => {
|
|
33
|
+
const res = {
|
|
34
|
+
dataType: "application/data", // <=== Wrong
|
|
35
|
+
dataSchema: z.string(),
|
|
36
|
+
};
|
|
37
|
+
expect(isJsonResponseSchema(res)).toBe(false);
|
|
38
|
+
});
|
|
39
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "transit-kit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "A declarative TypeScript framework for building type-safe Express.js APIs with automatic OpenAPI generation",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"express",
|
|
@@ -31,9 +31,13 @@
|
|
|
31
31
|
"types": "./dist/server/index.d.ts",
|
|
32
32
|
"import": "./dist/server/index.js",
|
|
33
33
|
"require": "./dist/server/index.js"
|
|
34
|
+
},
|
|
35
|
+
"./generator": {
|
|
36
|
+
"types": "./dist/generator/index.d.ts",
|
|
37
|
+
"import": "./dist/generator/index.js",
|
|
38
|
+
"require": "./dist/generator/index.js"
|
|
34
39
|
}
|
|
35
40
|
},
|
|
36
|
-
"bin": "./dist/cli/cli.js",
|
|
37
41
|
"scripts": {
|
|
38
42
|
"lint": "eslint",
|
|
39
43
|
"test": "vitest",
|
|
@@ -59,7 +63,6 @@
|
|
|
59
63
|
},
|
|
60
64
|
"dependencies": {
|
|
61
65
|
"colors": "^1.4.0",
|
|
62
|
-
"commander": "^14.0.2",
|
|
63
66
|
"cookie-parser": "^1.4.7",
|
|
64
67
|
"express": "^4.21.1",
|
|
65
68
|
"express-async-handler": "^1.2.0",
|
package/dist/cli/cli.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { Command } from "commander";
|
|
2
|
-
import fs from "fs";
|
|
3
|
-
import { generateOpenApiDoc } from "./generateOpenApi";
|
|
4
|
-
const program = new Command();
|
|
5
|
-
program
|
|
6
|
-
.name("transit-kit")
|
|
7
|
-
.description("CLI of the transitKit backend framework")
|
|
8
|
-
.version("1.0.0");
|
|
9
|
-
program
|
|
10
|
-
.command("generate-openapi")
|
|
11
|
-
.option("-o, --output <path>", "Output path for the generated OpenAPI document", "openapi.json")
|
|
12
|
-
.option("-t, --target <path>", "Target path to search for endpoint definitions", ".")
|
|
13
|
-
.action(async (options) => {
|
|
14
|
-
const { output, target } = options;
|
|
15
|
-
const generatedDoc = await generateOpenApiDoc(target);
|
|
16
|
-
fs.writeFileSync(output, JSON.stringify(generatedDoc, null, 2), {
|
|
17
|
-
encoding: "utf-8",
|
|
18
|
-
});
|
|
19
|
-
console.log(`OpenAPI document generated at: ${output}`);
|
|
20
|
-
});
|
|
21
|
-
program.parse();
|
|
File without changes
|
|
File without changes
|