···1111*.iml
1212dist
1313coverage
1414-samples/generated
1515-samples/swagger-codegen-cli-v2.jar
1616-samples/swagger-codegen-cli-v3.jar
1714.env
18151916# test files
+12-1
README.md
···4949yarn add @nicolas-chaulet/openapi-typescript-codegen -D
5050```
51515252-and add a script to your `package.json` file
5252+If you want to use `openapi-ts` with CLI, add a script to your `package.json` file
53535454```json
5555"scripts": {
5656 "openapi-ts": "openapi-ts"
5757}
5858+```
5959+6060+You can also generate your client programmatically by importing `openapi-ts` in a `.ts` file.
6161+6262+```ts
6363+import { createClient } from '@nicolas-chaulet/openapi-typescript-codegen'
6464+6565+createClient({
6666+ input: 'path/to/openapi.json',
6767+ output: 'src/client',
6868+})
5869```
59706071> ⚠️ You need to be running Node.js v18 or newer
···4455import { sync } from 'cross-spawn';
6677+import type { Client } from './client/interfaces/Client';
78import { parse as parseV2 } from './openApi/v2';
89import { parse as parseV3 } from './openApi/v3';
910import type { Config, UserConfig } from './types/config';
···7980const getConfig = async (userConfig: UserConfig, dependencies: Dependencies) => {
8081 const userConfigFromFile = await getConfigFromFile();
8182 if (userConfigFromFile) {
8282- userConfig = { ...userConfig, ...userConfigFromFile };
8383+ userConfig = { ...userConfigFromFile, ...userConfig };
8384 }
84858586 const {
8687 autoformat = true,
8788 base,
8888- clientName,
8989 enums = false,
9090 exportCore = true,
9191 exportModels = true,
9292 exportSchemas = false,
9393 exportServices = true,
9494 input,
9595+ name,
9596 operationId = true,
9697 output,
9798 postfixModels = '',
···109110 autoformat,
110111 base,
111112 client,
112112- clientName,
113113 enums,
114114 exportCore,
115115 exportModels,
116116 exportSchemas,
117117 exportServices,
118118 input,
119119+ name,
119120 operationId,
120121 output,
121122 postfixModels,
···142143 * Generate the OpenAPI client. This method will read the OpenAPI specification and based on the
143144 * given language it will generate the client, including the typed models, validation schemas,
144145 * service layer, etc.
145145- * @param userConfig {@link UserConfig} passed to the generate method
146146+ * @param userConfig {@link UserConfig} passed to the `createClient()` method
146147 */
147147-export const generate = async (userConfig: UserConfig): Promise<void> => {
148148+export async function createClient(userConfig: UserConfig): Promise<Client> {
148149 const pkg = JSON.parse(readFileSync(path.resolve(process.cwd(), 'package.json')).toString());
149150150151 const dependencies = [pkg.dependencies, pkg.devDependencies].reduce(
···172173 }
173174174175 console.log('✨ Done! Your client is located in:', config.output);
175175-};
176176+ return client;
177177+}
178178+179179+/**
180180+ * Type helper for openapi-ts.config.ts, returns {@link UserConfig} object
181181+ */
182182+export function defineConfig(config: UserConfig): UserConfig {
183183+ return config;
184184+}
176185177186export default {
178178- generate,
187187+ createClient,
188188+ defineConfig,
179189};
+1-9
src/node/index.ts
···11-import type { UserConfig } from '../types/config';
22-11+export { createClient, defineConfig } from '../';
32export type { UserConfig } from '../types/config';
44-55-/**
66- * Type helper for openapi-ts.config.ts, returns {@link UserConfig} object
77- */
88-export function defineConfig(config: UserConfig): UserConfig {
99- return config;
1010-}
+1-1
src/openApi/v2/index.ts
···1010 * Parse the OpenAPI specification to a Client model that contains
1111 * all the models, services and schema's we should output.
1212 * @param openApi The OpenAPI spec that we have loaded from disk.
1313- * @param options Options passed to the generate method
1313+ * @param options {@link Config} passed to the `createClient()` method
1414 */
1515export const parse = (openApi: OpenApi, options: Config): Client => {
1616 const version = getServiceVersion(openApi.info.version);
+1-1
src/openApi/v3/index.ts
···1010 * Parse the OpenAPI specification to a Client model that contains
1111 * all the models, services and schema's we should output.
1212 * @param openApi The OpenAPI spec that we have loaded from disk.
1313- * @param options Options passed to the generate method
1313+ * @param options {@link Config} passed to the `createClient()` method
1414 */
1515export const parse = (openApi: OpenApi, options: Config): Client => {
1616 const version = getServiceVersion(openApi.info.version);
+2-2
src/templates/client.hbs
···4545 {{/each}}
4646 ]
4747})
4848-export class {{{@root.$config.clientName}}} {}
4848+export class {{{@root.$config.name}}} {}
4949{{else}}
5050type HttpRequestConstructor = new (config: OpenAPIConfig) => BaseHttpRequest;
51515252-export class {{{@root.$config.clientName}}} {
5252+export class {{{@root.$config.name}}} {
53535454 {{#each services}}
5555 public readonly {{{camelCase name}}}: {{{name}}}{{{@root.$config.postfixServices}}};