Skip to content
This repository was archived by the owner on Sep 27, 2024. It is now read-only.

Commit 1be9355

Browse files
committed
update
1 parent 0b5ab03 commit 1be9355

24 files changed

+237
-209
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@omer-x/ts-openapi-interface-generator",
3-
"version": "0.2.0",
3+
"version": "1.0.0",
44
"description": "OpenAPI interface generator for TypeScript",
55
"keywords": [
66
"typescript",

src/core/arguments.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import yargs from "yargs";
22

33
const argv = yargs.option("output", {
44
alias: "o",
5-
describe: "Specify the output directry",
5+
describe: "Specify the output directory",
66
type: "string",
77
demandOption: false,
88
}).argv;

src/core/file.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import fs from "node:fs/promises";
2+
import path from "node:path";
3+
4+
export default async function createFile(content: string, fileName: string, outputDir: string, targetFolder = ".") {
5+
const targetPath = path.resolve(outputDir, targetFolder);
6+
await fs.mkdir(targetPath, { recursive: true });
7+
const filePath = path.resolve(targetPath, fileName);
8+
await fs.writeFile(filePath, content);
9+
}

src/core/interface.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/core/openapi.ts

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
type SchemaComponent = {
2-
type: "object" | "unknown",
3-
properties: Record<string, SchemaDefinition>,
4-
required?: string[],
5-
};
6-
71
export type SchemaDefinition = {
82
type?: "string" | "number" | "array" | "object",
93
format?: "date" | "unknown",
@@ -15,18 +9,24 @@ export type SchemaDefinition = {
159
enum?: (string | null)[],
1610
};
1711

18-
export type PathParameter = {
12+
type SchemaComponent = {
13+
type: "object" | "unknown",
14+
properties: Record<string, SchemaDefinition>,
15+
required?: string[],
16+
};
17+
18+
export type OperationParameter = {
1919
in: "path" | "query" | "header" | "cookie",
2020
name: string,
2121
description: string,
2222
required: boolean,
2323
schema: SchemaDefinition,
2424
};
2525

26-
export type RequestBody = {
26+
type RequestBody = {
2727
description: string,
2828
required: boolean,
29-
content: Record<ContentType, ResponseContent>,
29+
content: Content,
3030
};
3131

3232
type ResponseContent = {
@@ -35,33 +35,29 @@ type ResponseContent = {
3535

3636
type ContentType = "application/json" | "text/plain";
3737

38-
export type HttpResponse = {
38+
export type Content = Record<ContentType, ResponseContent>;
39+
40+
type HttpResponse = {
3941
description: string,
40-
content?: Record<ContentType, ResponseContent>,
42+
content?: Content,
4143
};
4244

4345
type HttpCode = "200" | "404";
4446

45-
export type ApiPath = {
46-
summary: string,
47+
export type Operation = {
4748
operationId: string,
48-
parameters?: PathParameter[],
49+
summary: string,
50+
description: string,
51+
parameters?: OperationParameter[],
4952
requestBody?: RequestBody,
5053
responses: Record<HttpCode, HttpResponse>,
5154
};
5255

53-
export type HttpMethod = "get" | "post" | "patch" | "put" | "delete";
56+
type HttpMethod = "get" | "post" | "patch" | "put" | "delete";
5457

55-
type OpenApiSpecification = {
58+
export type OpenAPI = {
5659
components: {
5760
schemas: Record<string, SchemaComponent>,
5861
},
59-
paths: Record<string, Record<HttpMethod, ApiPath>>,
62+
paths: Record<string, Record<HttpMethod, Operation>>,
6063
};
61-
62-
export async function getSwaggerJSON(base: string, specs?: string) {
63-
const url = new URL(specs ?? "/swagger", base);
64-
const response = await fetch(url);
65-
const data = await response.json();
66-
return data as OpenApiSpecification;
67-
}

src/core/operation.ts

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/core/parameters.ts

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/core/renderers/interface.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import Handlebars from "handlebars";
2+
import type { OperationTemplate } from "~/core/resolvers/operation";
3+
import interfaceTemplate from "~/templates/interface.hbs";
4+
5+
type InterfaceTemplate = {
6+
baseUrl: string,
7+
schemas: string[],
8+
operations: OperationTemplate[],
9+
};
10+
11+
export default function generateInterface(baseUrl: string, schemas: string[], operations: OperationTemplate[]) {
12+
const template = Handlebars.compile<InterfaceTemplate>(interfaceTemplate);
13+
return template({
14+
baseUrl,
15+
schemas,
16+
operations,
17+
});
18+
}

src/core/renderers/operation.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Handlebars from "handlebars";
2+
import operationTemplate from "~/templates/operation.hbs";
3+
4+
Handlebars.registerHelper("hasRequestBody", (input: string[]) => {
5+
return input.includes("requestBody");
6+
});
7+
8+
Handlebars.registerPartial("operation", operationTemplate);

0 commit comments

Comments
 (0)