@gromlab/api-codegen 1.0.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.
@@ -0,0 +1,24 @@
1
+ <%
2
+ const { route, utils, config } = it;
3
+ const { _, pascalCase, require } = utils;
4
+ const { query, payload, pathParams, headers } = route.request;
5
+
6
+ const routeDocs = includeFile("./route-docs", { config, route, utils });
7
+ const isValidIdentifier = (name) => /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name);
8
+ const routeNamespace = pascalCase(route.routeName.usage);
9
+
10
+ %>
11
+
12
+ /**
13
+ <%~ routeDocs.description %>
14
+
15
+ <%~ routeDocs.lines %>
16
+
17
+ */
18
+ export namespace <% if (isValidIdentifier(routeNamespace)) { %><%~ routeNamespace %><% } else { %>"<%~ routeNamespace %>"<% } %> {
19
+ export type RequestParams = <%~ (pathParams && pathParams.type) || '{}' %>;
20
+ export type RequestQuery = <%~ (query && query.type) || '{}' %>;
21
+ export type RequestBody = <%~ (payload && payload.type) || 'never' %>;
22
+ export type RequestHeaders = <%~ (headers && headers.type) || '{}' %>;
23
+ export type ResponseBody = <%~ route.response.type %>;
24
+ }
@@ -0,0 +1,32 @@
1
+ <%
2
+ const { utils, config, routes, modelTypes } = it;
3
+ const { _, pascalCase } = utils;
4
+ const dataContracts = config.modular ? _.map(modelTypes, "name") : [];
5
+ %>
6
+
7
+ <% if (dataContracts.length) { %>
8
+ import { <%~ dataContracts.join(", ") %> } from "./<%~ config.fileNames.dataContracts %>"
9
+ <% } %>
10
+
11
+ <%
12
+ /* TODO: outOfModule, combined should be attributes of route, which will allow to avoid duplication of code */
13
+ %>
14
+
15
+ <% if (routes.outOfModule) { %>
16
+ <% for (const { routes: outOfModuleRoutes = [] } of routes.outOfModule) { %>
17
+ <% for (const route of outOfModuleRoutes) { %>
18
+ <%~ includeFile('./route-type.ejs', { ...it, route }) %>
19
+ <% } %>
20
+ <% } %>
21
+ <% } %>
22
+
23
+ <% if (routes.combined) { %>
24
+ <% for (const { routes: combinedRoutes = [], moduleName } of routes.combined) { %>
25
+ export namespace <%~ pascalCase(moduleName) %> {
26
+ <% for (const route of combinedRoutes) { %>
27
+ <%~ includeFile('./route-type.ejs', { ...it, route }) %>
28
+ <% } %>
29
+ }
30
+
31
+ <% } %>
32
+ <% } %>
@@ -0,0 +1,15 @@
1
+ <%
2
+ const { contract, utils } = it;
3
+ const { formatDescription, require, _ } = utils;
4
+
5
+ %>
6
+ <% if (contract.$content.length) { %>
7
+ export type <%~ contract.name %> = {
8
+ <% for (const field of contract.$content) { %>
9
+ <%~ includeFile('./object-field-jsdoc.ejs', { ...it, field }) %>
10
+ <%~ field.field %>;
11
+ <% } %>
12
+ }<%~ utils.isNeedToAddNull(contract) ? ' | null' : ''%>
13
+ <% } else { %>
14
+ export type <%~ contract.name %> = Record<string, any>;
15
+ <% } %>
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@gromlab/api-codegen",
3
+ "version": "1.0.0",
4
+ "description": "CLI tool to generate TypeScript API client from OpenAPI specification",
5
+ "type": "module",
6
+ "bin": {
7
+ "api-codegen": "dist/cli.js"
8
+ },
9
+ "files": [
10
+ "dist"
11
+ ],
12
+ "engines": {
13
+ "node": ">=18"
14
+ },
15
+ "scripts": {
16
+ "build": "bun build src/cli.ts --target=node --outdir=dist --format=esm && cp -r src/templates dist/",
17
+ "dev": "bun run src/cli.ts",
18
+ "test": "bun test",
19
+ "test:unit": "bun test tests/unit",
20
+ "test:integration": "bun test tests/integration",
21
+ "test:watch": "bun test --watch",
22
+ "test:coverage": "bun test --coverage",
23
+ "prepublishOnly": "bun run build"
24
+ },
25
+ "dependencies": {
26
+ "@biomejs/wasm-bundler": "^2.3.0",
27
+ "@biomejs/wasm-web": "^2.3.0",
28
+ "chalk": "^5.3.0",
29
+ "commander": "^12.1.0",
30
+ "ejs": "^3.1.10",
31
+ "js-yaml": "^4.1.0",
32
+ "swagger-typescript-api": "^13.0.22"
33
+ },
34
+ "devDependencies": {
35
+ "@types/bun": "latest",
36
+ "@types/ejs": "^3.1.5",
37
+ "@types/js-yaml": "^4.0.9",
38
+ "@types/node": "^22.10.2",
39
+ "@types/react": "^18.3.0",
40
+ "@types/tmp": "^0.2.6",
41
+ "execa": "^8.0.0",
42
+ "msw": "^2.0.0",
43
+ "react": "^18.3.0",
44
+ "swr": "^2.3.0",
45
+ "tmp": "^0.2.1"
46
+ },
47
+ "peerDependencies": {
48
+ "typescript": "^5"
49
+ },
50
+ "keywords": [
51
+ "openapi",
52
+ "swagger",
53
+ "api",
54
+ "codegen",
55
+ "typescript",
56
+ "generator",
57
+ "cli"
58
+ ],
59
+ "author": "",
60
+ "license": "MIT"
61
+ }