codegen-openapi-ts 1.0.0 → 1.0.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.
Files changed (2) hide show
  1. package/dist/index.d.ts +172 -0
  2. package/package.json +3 -2
@@ -0,0 +1,172 @@
1
+ declare enum HttpClient {
2
+ FETCH = "fetch",
3
+ XHR = "xhr",
4
+ NODE = "node",
5
+ AXIOS = "axios"
6
+ }
7
+
8
+ declare enum Indent {
9
+ SPACE_4 = "4",
10
+ SPACE_2 = "2",
11
+ TAB = "tab"
12
+ }
13
+
14
+ type Options = {
15
+ input: string | Record<string, any>;
16
+ output: string;
17
+ httpClient?: HttpClient;
18
+ clientName?: string;
19
+ useOptions?: boolean;
20
+ useUnionTypes?: boolean;
21
+ exportCore?: boolean;
22
+ exportServices?: boolean;
23
+ exportModels?: boolean;
24
+ exportSchemas?: boolean;
25
+ indent?: Indent;
26
+ postfixServices?: string;
27
+ postfixModels?: string;
28
+ request?: string;
29
+ write?: boolean;
30
+ selectedOnly?: boolean;
31
+ appendTemplate?: ReturnType<typeof defineConfig>['appendTemplate'];
32
+ };
33
+ /**
34
+ * Generate the OpenAPI client. This method will read the OpenAPI specification and based on the
35
+ * given language it will generate the client, including the typed models, validation schemas,
36
+ * service layer, etc.
37
+ * @param input The relative location of the OpenAPI spec
38
+ * @param output The relative location of the output directory
39
+ * @param httpClient The selected httpClient (fetch, xhr, node or axios)
40
+ * @param clientName Custom client class name
41
+ * @param useOptions Use options or arguments functions
42
+ * @param useUnionTypes Use union types instead of enums
43
+ * @param exportCore Generate core client classes
44
+ * @param exportServices Generate services
45
+ * @param exportModels Generate models
46
+ * @param exportSchemas Generate schemas
47
+ * @param indent Indentation options (4, 2 or tab)
48
+ * @param postfixServices Service name postfix
49
+ * @param postfixModels Model name postfix
50
+ * @param request Path to custom request file
51
+ * @param write Write the files to disk (true or false)
52
+ */
53
+ declare const generate: ({ input, output, httpClient, clientName, useOptions, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas, indent, postfixServices, postfixModels, request, write, selectedOnly, appendTemplate, }: Options) => Promise<void>;
54
+ declare const _default: {
55
+ HttpClient: typeof HttpClient;
56
+ generate: ({ input, output, httpClient, clientName, useOptions, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas, indent, postfixServices, postfixModels, request, write, selectedOnly, appendTemplate, }: Options) => Promise<void>;
57
+ };
58
+
59
+ /**
60
+ * Generate the OpenAPI client with options to convert swagger to openapi etc.
61
+ * @param converterInput.from The schema specification for the response (swagger_1, swagger_2, openapi_3)
62
+ * @param converterInput.to The schema specification for the output (openapi_3)
63
+ * @param converterInput.source The relative location of the OpenAPI spec
64
+ * @param options.httpClient The selected httpClient (fetch, xhr, node or axios)
65
+ * @param options.useUnionTypes Use union types instead of enums
66
+ * @param options.exportCore: Generate core client classes
67
+ * @param options.exportServices: Generate services
68
+ * @param options.exportModels: Generate models
69
+ * @param options.exportSchemas: Generate schemas
70
+ * @param options.postfix: Service name postfix
71
+ * @param options.request: Path to custom request file
72
+ * @param options.write Write the files to disk (true or false)
73
+ */
74
+ declare function convertAndGenerate({ from, source }: {
75
+ from: string;
76
+ source: string;
77
+ }, { input, output, useOptions, useUnionTypes }: Options, urlMethodMapping?: ServiceConfigWithMappings['urlMethodMapping'], selectedOnly?: ServiceConfigWithMappings['selectedOnly'], modelNameMapping?: BaseServiceConfig['modelNameMapping'], appendTemplate?: ReturnType<typeof defineConfig>['appendTemplate'], proxyConfig?: BaseServiceConfig['proxyConfig']): Promise<void>;
78
+ type BaseServiceConfig = {
79
+ /**
80
+ * API Docs request url for the json response
81
+ */
82
+ source: string;
83
+ /**
84
+ * Specify the API specs response format version
85
+ */
86
+ from: 'swagger_1' | 'swagger_2' | 'openapi_3' | 'api_blueprint' | 'io_docs' | 'google' | 'raml' | 'wadl';
87
+ /**
88
+ * Specify the folder for the codegen output
89
+ */
90
+ output: string;
91
+ /**
92
+ * Create a function for proxying the request
93
+ * @example
94
+ * {
95
+ * // ... other config
96
+ * proxyConfig: (path) => {
97
+ * return path.replace('/api/', '/be/')
98
+ * }
99
+ * }
100
+ * @param {string} path
101
+ */
102
+ proxyConfig?: (path: string) => string;
103
+ /**
104
+ * Can be used to replace long model names specified on the schema.
105
+ * Please use the api-schema.json generated on root project folder
106
+ * to debug the desired results. Also note that the original schema name
107
+ * with dot (.) will be generated as underscore (_). Example:
108
+ * some.long.name will be generated as some_long_name,
109
+ * if this modelNameMapping supplied
110
+ * @example
111
+ * {
112
+ * // ... other config
113
+ * modelNameMapping: (json) => {
114
+ * // remember to use global flag to all regexp used here
115
+ * return config.replace(new RegExp('some.long.name', g), 'shortname')
116
+ * }
117
+ * }
118
+ *
119
+ * @param {string} json - stringified json schema
120
+ */
121
+ modelNameMapping?: (json: string) => string;
122
+ };
123
+ type ServiceConfigDefault = BaseServiceConfig & {
124
+ urlMethodMapping: undefined;
125
+ selectedOnly: undefined;
126
+ };
127
+ declare type ServiceConfigWithMappings = BaseServiceConfig & {
128
+ /**
129
+ * Custom spec paths mapping. You can configure to rename the method name
130
+ * or customise the proxyUrl for the specific API
131
+ *
132
+ * @example
133
+ * {
134
+ * // ... other config
135
+ * urlMethodMapping: [
136
+ * { originalUrl: '/pokemon-list', method: 'get', methodName: 'GetPokemonList' },
137
+ * { originalUrl: '/pokemon-detail/{id}', method: 'get', methodName: 'GetPokemonList', proxyUrl: '/proxy/pokemon-detail/{id}' }
138
+ * ]
139
+ * }
140
+ *
141
+ */
142
+ urlMethodMapping: {
143
+ originalUrl: string;
144
+ method: 'get' | 'post' | 'put' | 'delete';
145
+ methodName: string;
146
+ proxyUrl?: string;
147
+ }[];
148
+ /**
149
+ * Flag to only generate listed specs based on urlMethodMapping.
150
+ * The codegen will still generate all the models listed on the api specs
151
+ */
152
+ selectedOnly: boolean;
153
+ };
154
+ /**
155
+ * Type helper to make it easier to use codegen.config.js
156
+ */
157
+ declare function defineConfig(config: {
158
+ /**
159
+ * Custom api templates append on top of service files
160
+ */
161
+ appendTemplate?: string;
162
+ /**
163
+ * List config for every services
164
+ */
165
+ services: (ServiceConfigDefault | ServiceConfigWithMappings)[];
166
+ }): {
167
+ appendTemplate?: string | undefined;
168
+ services: (ServiceConfigDefault | ServiceConfigWithMappings)[];
169
+ };
170
+
171
+ export { HttpClient, Indent, convertAndGenerate, _default as default, defineConfig, generate };
172
+ export type { BaseServiceConfig, Options, ServiceConfigDefault, ServiceConfigWithMappings };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codegen-openapi-ts",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Library that generates Typescript clients based on the OpenAPI specification.",
5
5
  "type": "module",
6
6
  "author": "devteaa",
@@ -93,9 +93,9 @@
93
93
  "@types/fs-extra": "^11.0.4",
94
94
  "@types/glob": "8.1.0",
95
95
  "@types/node": "^24.0.0",
96
- "@types/shelljs": "^0.8.15",
97
96
  "@types/node-fetch": "2.6.10",
98
97
  "@types/qs": "6.9.11",
98
+ "@types/shelljs": "^0.8.15",
99
99
  "@typescript-eslint/eslint-plugin": "6.20.0",
100
100
  "@typescript-eslint/parser": "6.20.0",
101
101
  "@vitest/coverage-v8": "^2.1.0",
@@ -116,6 +116,7 @@
116
116
  "qs": "6.11.2",
117
117
  "rimraf": "5.0.5",
118
118
  "rollup": "4.7.0",
119
+ "rollup-plugin-dts": "6.5.1",
119
120
  "tslib": "2.6.2",
120
121
  "typescript": "5.2.2",
121
122
  "vitest": "^2.1.0"