@spec2ts/openapi-client 3.1.3 → 4.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.
package/README.md CHANGED
@@ -8,15 +8,16 @@
8
8
 
9
9
  ## Features
10
10
 
11
- * **AST-based:** Unlike other code generators `@spec2ts/openapi-client` does not use templates to generate code but uses TypeScript's built-in API to generate and pretty-print an abstract syntax tree.
12
- * **Tree-shakeable:** Individually exported types allows you to bundle only the ones you actually use.
13
- * **YAML or JSON:** Use YAML or JSON for your OpenAPI v3 specification.
14
- * **External references:** Resolves automatically external references and bundle or import them in generated files.
15
- * **Implementation agnostic:** Use generated types in any projet or framework.
11
+ - **AST-based:** Unlike other code generators `@spec2ts/openapi-client` does not use templates to generate code but uses TypeScript's built-in API to generate and pretty-print an abstract syntax tree.
12
+ - **Tree-shakeable:** Individually exported types allows you to bundle only the ones you actually use.
13
+ - **YAML or JSON:** Use YAML or JSON for your OpenAPI v3 specification.
14
+ - **External references:** Resolves automatically external references and bundle or import them in generated files.
15
+ - **Implementation agnostic:** Use generated types in any projet or framework.
16
16
 
17
17
  ## Installation
18
18
 
19
19
  Install in your project:
20
+
20
21
  ```bash
21
22
  npm install @spec2ts/openapi-client
22
23
  ```
@@ -65,9 +66,11 @@ async function generate(path: string): Promise<string> {
65
66
  ## Compatibility Matrix
66
67
 
67
68
  | TypeScript version | spec2ts version |
68
- |--------------------|-----------------|
69
- | v3.x.x | v1 |
70
- | v4.x.x | v2 |
69
+ | ------------------ | --------------- |
70
+ | v3.x.x | v1 |
71
+ | v4.x.x | v2 |
72
+ | v5.x.x | v3 |
73
+ | v6.x.x | v4 |
71
74
 
72
75
  ## License
73
76
 
@@ -0,0 +1 @@
1
+ export { };
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env node
2
+ import { i as usage, n as describe, r as handler, t as builder } from "../command-Djig04Ub.mjs";
3
+ import yargs from "yargs";
4
+ import { hideBin } from "yargs/helpers";
5
+ //#region src/bin/oapi2tsclient.ts
6
+ yargs(hideBin(process.argv)).command(usage, describe, builder, handler).help("help", "Show help usage").demandCommand().argv;
7
+ //#endregion
8
+ export {};
@@ -0,0 +1,24 @@
1
+ import { t as OApiGeneratorOptions } from "../openapi-generator-DfC4jH9J.mjs";
2
+ import { Argv } from "yargs";
3
+ //#region src/cli/command.d.ts
4
+ interface BuildClientFromOpenApiOptions extends OApiGeneratorOptions {
5
+ input: string | string[];
6
+ output?: string;
7
+ banner?: string;
8
+ importFetchVersion?: string;
9
+ importFormDataVersion?: string;
10
+ packageName?: string;
11
+ packageVersion?: string;
12
+ packageAuthor?: string;
13
+ packageLicense?: string;
14
+ packagePrivate?: boolean;
15
+ packageBuildTarget?: string;
16
+ packageBuildModule?: string;
17
+ packageBuildModuleResolution?: string;
18
+ }
19
+ declare const usage = "$0 <input..>";
20
+ declare const describe = "Generate TypeScript HTTP client from OpenAPI specification";
21
+ declare function builder(argv: Argv): Argv<BuildClientFromOpenApiOptions>;
22
+ declare function handler(options: BuildClientFromOpenApiOptions): Promise<void>;
23
+ //#endregion
24
+ export { BuildClientFromOpenApiOptions, builder, describe, handler, usage };
@@ -0,0 +1,2 @@
1
+ import { i as usage, n as describe, r as handler, t as builder } from "../command-Djig04Ub.mjs";
2
+ export { builder, describe, handler, usage };
@@ -0,0 +1,143 @@
1
+ import { n as generateClientFromFile } from "./openapi-generator-DwzcjKrv.mjs";
2
+ import * as path from "path";
3
+ import { cli, printer } from "@spec2ts/core";
4
+ import { promises } from "fs";
5
+ //#region src/cli/command.ts
6
+ const usage = "$0 <input..>";
7
+ const describe = "Generate TypeScript HTTP client from OpenAPI specification";
8
+ function builder(argv) {
9
+ return argv.positional("input", {
10
+ array: true,
11
+ type: "string",
12
+ describe: "Path to OpenAPI Specification(s) to convert to TypeScript HTTP client",
13
+ demandOption: true
14
+ }).option("output", {
15
+ type: "string",
16
+ alias: "o",
17
+ describe: "Output file for generated client"
18
+ }).option("cwd", {
19
+ type: "string",
20
+ alias: "c",
21
+ describe: "Root directory for resolving $refs"
22
+ }).option("baseUrl", {
23
+ type: "string",
24
+ describe: "Base url of the server"
25
+ }).option("prefix", {
26
+ type: "string",
27
+ describe: "Only generate paths with this prefix"
28
+ }).option("avoidAny", {
29
+ type: "boolean",
30
+ describe: "Avoid the `any` type and use `unknown` instead"
31
+ }).option("enableDate", {
32
+ choices: [
33
+ true,
34
+ "strict",
35
+ "lax"
36
+ ],
37
+ describe: "Build `Date` for format `date` and `date-time`"
38
+ }).option("inlineRequired", {
39
+ type: "boolean",
40
+ describe: "Create a method argument for each required parameter"
41
+ }).option("importFetch", {
42
+ choices: [
43
+ "node-fetch",
44
+ "cross-fetch",
45
+ "isomorphic-fetch"
46
+ ],
47
+ describe: "Use a custom fetch implementation"
48
+ }).option("typesPath", {
49
+ type: "string",
50
+ describe: "Generate client types in external file relative to the output file"
51
+ }).option("importFetchVersion", {
52
+ type: "string",
53
+ describe: "Use a custom fetch implementation version"
54
+ }).option("importFormDataVersion", {
55
+ type: "string",
56
+ describe: "Use a custom form-data implementation version"
57
+ }).option("packageName", {
58
+ type: "string",
59
+ describe: "Generate a package.json with given name"
60
+ }).option("packageVersion", {
61
+ type: "string",
62
+ describe: "Set the version of the package.json"
63
+ }).option("packageAuthor", {
64
+ type: "string",
65
+ describe: "Set the author of the package.json"
66
+ }).option("packageLicense", {
67
+ type: "string",
68
+ describe: "Set the license of the package.json"
69
+ }).option("packagePrivate", {
70
+ type: "boolean",
71
+ describe: "Set the package.json private"
72
+ }).option("packageBuildTarget", {
73
+ type: "string",
74
+ describe: "Set the TypeScript build target"
75
+ }).option("packageBuildModule", {
76
+ type: "string",
77
+ describe: "Set the TypeScript build module"
78
+ }).option("packageBuildModuleResolution", {
79
+ type: "string",
80
+ describe: "Set the TypeScript build module resolution"
81
+ }).option("banner", {
82
+ type: "string",
83
+ alias: "b",
84
+ describe: "Comment prepended to the top of each generated file"
85
+ });
86
+ }
87
+ async function handler(options) {
88
+ const files = await cli.findFiles(options.input);
89
+ for (const file of files) {
90
+ const output = options.output || cli.getOutputPath(file, options);
91
+ await cli.mkdirp(output);
92
+ if (options.typesPath) {
93
+ if (!options.typesPath.startsWith(".")) options.typesPath = "./" + options.typesPath;
94
+ const res = await generateClientFromFile(file, options);
95
+ await printFile(res.client, output, options);
96
+ const outputTypes = path.resolve(path.dirname(output), options.typesPath + ".ts");
97
+ await printFile(res.types, outputTypes, options);
98
+ } else await printFile(await generateClientFromFile(file, options), output, options);
99
+ if (options.packageName) await generatePackage(output, options);
100
+ }
101
+ }
102
+ async function printFile(file, output, options) {
103
+ const content = printer.printFile(file);
104
+ await cli.mkdirp(output);
105
+ await cli.writeFile(output, (options.banner || defaultBanner()) + "\n\n" + content);
106
+ }
107
+ function defaultBanner() {
108
+ return `/**
109
+ * DO NOT MODIFY
110
+ * Generated using @spec2ts/openapi-client.
111
+ * See https://www.npmjs.com/package/@spec2ts/openapi-client
112
+ */
113
+
114
+ /* eslint-disable */`;
115
+ }
116
+ async function generatePackage(output, options) {
117
+ const outputDir = path.dirname(output);
118
+ const main = path.relative(outputDir, output);
119
+ const pkg = {
120
+ name: options.packageName,
121
+ version: options.packageVersion || "1.0.0",
122
+ description: "OpenAPI v3 client for " + options.packageName,
123
+ author: options.packageAuthor || "@spec2ts/openapi-client",
124
+ license: options.packageLicense || "UNLICENSED",
125
+ main: main.replace(/\.ts$/, ".js"),
126
+ files: ["*.js", "*.d.ts"],
127
+ scripts: {
128
+ build: `tsc ${main} --strict --target ${options.packageBuildTarget || "ES2018"} --module ${options.packageBuildModule || "node16"} --moduleResolution ${options.packageBuildModuleResolution || "node16"} --skipLibCheck`,
129
+ prepublishOnly: "npm run build"
130
+ },
131
+ dependencies: {},
132
+ devDependencies: { typescript: "^6.0.0" }
133
+ };
134
+ if (options.importFetch) {
135
+ pkg.dependencies[options.importFetch] = options.importFetchVersion || "*";
136
+ pkg.dependencies["form-data"] = options.importFormDataVersion || "*";
137
+ pkg.devDependencies["@types/node"] = "*";
138
+ }
139
+ if (options.packagePrivate) pkg.private = options.packagePrivate;
140
+ await promises.writeFile(path.join(outputDir, "package.json"), JSON.stringify(pkg, null, 2), "utf8");
141
+ }
142
+ //#endregion
143
+ export { usage as i, describe as n, handler as r, builder as t };
@@ -0,0 +1,2 @@
1
+ import { i as generateClientFromFile, n as SeparatedClientResult, r as generateClient, t as OApiGeneratorOptions } from "./openapi-generator-DfC4jH9J.mjs";
2
+ export { OApiGeneratorOptions, SeparatedClientResult, generateClient, generateClientFromFile };
package/dist/index.mjs ADDED
@@ -0,0 +1,2 @@
1
+ import { n as generateClientFromFile, t as generateClient } from "./openapi-generator-DwzcjKrv.mjs";
2
+ export { generateClient, generateClientFromFile };
@@ -0,0 +1,25 @@
1
+ import { ParserOptions } from "@spec2ts/jsonschema";
2
+ import * as ts from "typescript";
3
+ import { OpenAPIObject } from "openapi3-ts/oas31";
4
+ //#region src/lib/openapi-generator.d.ts
5
+ interface OApiGeneratorOptions extends ParserOptions {
6
+ inlineRequired?: boolean;
7
+ importFetch?: "node-fetch" | "cross-fetch" | "isomorphic-fetch";
8
+ typesPath?: string;
9
+ baseUrl?: string;
10
+ prefix?: string;
11
+ }
12
+ declare function generateClientFromFile(file: string, options: OApiGeneratorOptions & {
13
+ typesPath: string;
14
+ }): Promise<SeparatedClientResult>;
15
+ declare function generateClientFromFile(file: string, options?: OApiGeneratorOptions): Promise<ts.SourceFile>;
16
+ declare function generateClient(spec: OpenAPIObject, options: OApiGeneratorOptions & {
17
+ typesPath: string;
18
+ }): Promise<SeparatedClientResult>;
19
+ declare function generateClient(spec: OpenAPIObject, options?: OApiGeneratorOptions): Promise<ts.SourceFile>;
20
+ interface SeparatedClientResult {
21
+ client: ts.SourceFile;
22
+ types: ts.SourceFile;
23
+ }
24
+ //#endregion
25
+ export { generateClientFromFile as i, SeparatedClientResult as n, generateClient as r, OApiGeneratorOptions as t };