transit-kit 0.5.0 → 0.6.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.
@@ -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
- export declare function generateOpenApiDoc(targetPath: string): Promise<OpenAPIV3.Document<{}>>;
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(targetPath) {
141
- const serverModule = await import(path.resolve(process.cwd(), targetPath));
142
- const server = serverModule.default;
143
- if (hasValue(server) &&
144
- hasValue(server.endpointDefinitions) &&
145
- Array.isArray(server.endpointDefinitions)) {
146
- const endpointDefinitions = server.endpointDefinitions;
147
- const paths = endpointDefinitions.reduce((acc, def) => {
148
- const [openApiPath, pathItem] = translateToOpenAPIPathItem(def);
149
- if (acc[openApiPath]) {
150
- acc[openApiPath] = {
151
- ...acc[openApiPath],
152
- ...pathItem,
153
- };
154
- }
155
- else {
156
- acc[openApiPath] = pathItem;
157
- }
158
- return acc;
159
- }, {});
160
- const securitySchemes = extractSecuritySchemes(endpointDefinitions);
161
- const openApiDocument = {
162
- openapi: "3.0.0",
163
- info: {
164
- title: "Generated API",
165
- version: "1.0.0",
166
- },
167
- paths: paths,
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,
@@ -0,0 +1 @@
1
+ export { generateOpenApiDoc } from "./generateOpenApi";
@@ -0,0 +1 @@
1
+ export { generateOpenApiDoc } from "./generateOpenApi";
@@ -42,6 +42,7 @@ export function createServer(config) {
42
42
  endpointDefinitions: [],
43
43
  registerApiEndpoint(endpoint) {
44
44
  registerApiEndpoint(app, endpoint);
45
+ this.endpointDefinitions.push(endpoint.definition);
45
46
  },
46
47
  start() {
47
48
  app.listen(port);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "transit-kit",
3
- "version": "0.5.0",
3
+ "version": "0.6.1",
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.d.ts DELETED
@@ -1 +0,0 @@
1
- export {};
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