typed-openapi 1.3.0 → 1.3.2

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.
@@ -952,20 +952,28 @@ var generateTanstackQueryFile = async (ctx) => {
952
952
  * Generic mutation method with full type-safety for any endpoint that doesnt require parameters to be passed initially
953
953
  */
954
954
  mutation<
955
- TMethod extends keyof EndpointByMethod,
956
- TPath extends keyof EndpointByMethod[TMethod],
957
- TEndpoint extends EndpointByMethod[TMethod][TPath]
958
- >(
959
- method: TMethod,
960
- path: TPath) {
955
+ TMethod extends keyof EndpointByMethod,
956
+ TPath extends keyof EndpointByMethod[TMethod],
957
+ TEndpoint extends EndpointByMethod[TMethod][TPath],
958
+ TSelection,
959
+ >(method: TMethod, path: TPath, selectFn?: (res: Omit<Response, "json"> & {
960
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/json) */
961
+ json: () => Promise<TEndpoint extends { response: infer Res } ? Res : never>;
962
+ }) => TSelection) {
961
963
  const mutationKey = [{ method, path }] as const;
962
964
  return {
965
+ /** type-only property if you need easy access to the endpoint params */
966
+ "~endpoint": {} as TEndpoint,
967
+ mutationKey: mutationKey,
968
+ mutationOptions: {
963
969
  mutationKey: mutationKey,
964
- mutationOptions: {
965
- mutationKey: mutationKey,
966
- mutationFn: async (params: TEndpoint extends { parameters: infer Parameters} ? Parameters: never) => this.client.request(method, path, params)
967
- }
968
- }
970
+ mutationFn: async (params: TEndpoint extends { parameters: infer Parameters } ? Parameters : never) => {
971
+ const response = await this.client.request(method, path, params);
972
+ const res = selectFn ? selectFn(response) : response
973
+ return res as unknown extends TSelection ? typeof response : Awaited<TSelection>
974
+ },
975
+ },
976
+ };
969
977
  }
970
978
  // </ApiClient.request>
971
979
  }
@@ -0,0 +1,46 @@
1
+ import {
2
+ allowedRuntimes,
3
+ generateFile,
4
+ generateTanstackQueryFile,
5
+ mapOpenApiEndpoints,
6
+ prettify
7
+ } from "./chunk-YLPQQ24J.js";
8
+
9
+ // src/generate-client-files.ts
10
+ import SwaggerParser from "@apidevtools/swagger-parser";
11
+ import { basename, join, dirname } from "pathe";
12
+ import { type } from "arktype";
13
+ import { writeFile } from "fs/promises";
14
+ var cwd = process.cwd();
15
+ var now = /* @__PURE__ */ new Date();
16
+ var optionsSchema = type({
17
+ "output?": "string",
18
+ runtime: allowedRuntimes,
19
+ tanstack: "boolean | string"
20
+ });
21
+ async function generateClientFiles(input, options) {
22
+ const openApiDoc = await SwaggerParser.bundle(input);
23
+ const ctx = mapOpenApiEndpoints(openApiDoc);
24
+ console.log(`Found ${ctx.endpointList.length} endpoints`);
25
+ const content = await prettify(generateFile({ ...ctx, runtime: options.runtime }));
26
+ const outputPath = join(
27
+ cwd,
28
+ options.output ?? input + `.${options.runtime === "none" ? "client" : options.runtime}.ts`
29
+ );
30
+ console.log("Generating client...", outputPath);
31
+ await writeFile(outputPath, content);
32
+ if (options.tanstack) {
33
+ const tanstackContent = await generateTanstackQueryFile({
34
+ ...ctx,
35
+ relativeApiClientPath: "./" + basename(outputPath)
36
+ });
37
+ const tanstackOutputPath = join(dirname(outputPath), typeof options.tanstack === "string" ? options.tanstack : `tanstack.client.ts`);
38
+ console.log("Generating tanstack client...", tanstackOutputPath);
39
+ await writeFile(tanstackOutputPath, tanstackContent);
40
+ }
41
+ console.log(`Done in ${(/* @__PURE__ */ new Date()).getTime() - now.getTime()}ms !`);
42
+ }
43
+
44
+ export {
45
+ generateClientFiles
46
+ };
package/dist/cli.js CHANGED
@@ -1,50 +1,21 @@
1
1
  import {
2
- allowedRuntimes,
3
- generateFile,
4
- generateTanstackQueryFile,
5
- mapOpenApiEndpoints,
6
- prettify
7
- } from "./chunk-EYTM4P4S.js";
2
+ generateClientFiles
3
+ } from "./chunk-YRJ7W5RY.js";
4
+ import {
5
+ allowedRuntimes
6
+ } from "./chunk-YLPQQ24J.js";
8
7
 
9
8
  // src/cli.ts
10
- import SwaggerParser from "@apidevtools/swagger-parser";
11
9
  import { cac } from "cac";
12
- import { basename, join } from "pathe";
13
- import { type } from "arktype";
14
- import { writeFile } from "fs/promises";
15
10
  import { readFileSync } from "fs";
16
- import { dirname } from "path";
17
11
  var { name, version } = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
18
- var cwd = process.cwd();
19
12
  var cli = cac(name);
20
- var now = /* @__PURE__ */ new Date();
21
- var optionsSchema = type({ "output?": "string", runtime: allowedRuntimes, tanstack: "boolean | string" });
22
13
  cli.command("<input>", "Generate").option("-o, --output <path>", "Output path for the api client ts file (defaults to `<input>.<runtime>.ts`)").option(
23
14
  "-r, --runtime <name>",
24
15
  `Runtime to use for validation; defaults to \`none\`; available: ${allowedRuntimes.toString()}`,
25
16
  { default: "none" }
26
17
  ).option("--tanstack [name]", "Generate tanstack client, defaults to false, can optionally specify a name for the generated file").action(async (input, _options) => {
27
- const options = optionsSchema.assert(_options);
28
- const openApiDoc = await SwaggerParser.bundle(input);
29
- const ctx = mapOpenApiEndpoints(openApiDoc);
30
- console.log(`Found ${ctx.endpointList.length} endpoints`);
31
- const content = await prettify(generateFile({ ...ctx, runtime: options.runtime }));
32
- const outputPath = join(
33
- cwd,
34
- options.output ?? input + `.${options.runtime === "none" ? "client" : options.runtime}.ts`
35
- );
36
- console.log("Generating client...", outputPath);
37
- await writeFile(outputPath, content);
38
- if (options.tanstack) {
39
- const tanstackContent = await generateTanstackQueryFile({
40
- ...ctx,
41
- relativeApiClientPath: "./" + basename(outputPath)
42
- });
43
- const tanstackOutputPath = join(dirname(outputPath), typeof options.tanstack === "string" ? options.tanstack : `tanstack.client.ts`);
44
- console.log("Generating tanstack client...", tanstackOutputPath);
45
- await writeFile(tanstackOutputPath, tanstackContent);
46
- }
47
- console.log(`Done in ${(/* @__PURE__ */ new Date()).getTime() - now.getTime()}ms !`);
18
+ return generateClientFiles(input, _options);
48
19
  });
49
20
  cli.help();
50
21
  cli.version(version);
package/dist/index.js CHANGED
@@ -8,7 +8,7 @@ import {
8
8
  openApiSchemaToTs,
9
9
  tsFactory,
10
10
  unwrap
11
- } from "./chunk-EYTM4P4S.js";
11
+ } from "./chunk-YLPQQ24J.js";
12
12
  export {
13
13
  createBoxFactory,
14
14
  createFactory,
@@ -0,0 +1,13 @@
1
+ import { Options } from 'prettier';
2
+ import * as arktype_internal_methods_object_ts from 'arktype/internal/methods/object.ts';
3
+
4
+ declare const prettify: (str: string, options?: Options | null) => string | Promise<string>;
5
+
6
+ declare const optionsSchema: arktype_internal_methods_object_ts.ObjectType<{
7
+ runtime: "none" | "arktype" | "io-ts" | "typebox" | "valibot" | "yup" | "zod";
8
+ tanstack: string | boolean;
9
+ output?: string;
10
+ }, {}>;
11
+ declare function generateClientFiles(input: string, options: typeof optionsSchema.infer): Promise<void>;
12
+
13
+ export { generateClientFiles, prettify };
@@ -0,0 +1,10 @@
1
+ import {
2
+ generateClientFiles
3
+ } from "./chunk-YRJ7W5RY.js";
4
+ import {
5
+ prettify
6
+ } from "./chunk-YLPQQ24J.js";
7
+ export {
8
+ generateClientFiles,
9
+ prettify
10
+ };
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "typed-openapi",
3
3
  "type": "module",
4
- "version": "1.3.0",
4
+ "version": "1.3.2",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
7
7
  "exports": {
8
- ".": "./dist/index.js"
8
+ ".": "./dist/index.js",
9
+ "./node": "./dist/node.export.js"
9
10
  },
10
11
  "bin": {
11
12
  "typed-openapi": "bin.js"
package/src/cli.ts CHANGED
@@ -1,23 +1,11 @@
1
- import SwaggerParser from "@apidevtools/swagger-parser";
2
1
  import { cac } from "cac";
3
- import type { OpenAPIObject } from "openapi3-ts/oas31";
4
- import { basename, join } from "pathe";
5
- import { type } from "arktype";
6
2
 
7
- import { writeFile } from "fs/promises";
8
- import { allowedRuntimes, generateFile } from "./generator.ts";
9
- import { mapOpenApiEndpoints } from "./map-openapi-endpoints.ts";
10
- import { generateTanstackQueryFile } from "./tanstack-query.generator.ts";
11
3
  import { readFileSync } from "fs";
12
- import { prettify } from "./format.ts";
13
- import { dirname } from "path";
4
+ import { generateClientFiles } from "./generate-client-files.ts";
5
+ import { allowedRuntimes } from "./generator.ts";
14
6
 
15
7
  const { name, version } = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
16
- const cwd = process.cwd();
17
8
  const cli = cac(name);
18
- const now = new Date();
19
-
20
- const optionsSchema = type({ "output?": "string", runtime: allowedRuntimes, tanstack: "boolean | string" });
21
9
 
22
10
  cli
23
11
  .command("<input>", "Generate")
@@ -29,32 +17,7 @@ cli
29
17
  )
30
18
  .option("--tanstack [name]", "Generate tanstack client, defaults to false, can optionally specify a name for the generated file")
31
19
  .action(async (input, _options) => {
32
- const options = optionsSchema.assert(_options);
33
- const openApiDoc = (await SwaggerParser.bundle(input)) as OpenAPIObject;
34
-
35
- const ctx = mapOpenApiEndpoints(openApiDoc);
36
- console.log(`Found ${ctx.endpointList.length} endpoints`);
37
-
38
- const content = await prettify(generateFile({ ...ctx, runtime: options.runtime }));
39
- const outputPath = join(
40
- cwd,
41
- options.output ?? input + `.${options.runtime === "none" ? "client" : options.runtime}.ts`,
42
- );
43
-
44
- console.log("Generating client...", outputPath);
45
- await writeFile(outputPath, content);
46
-
47
- if (options.tanstack) {
48
- const tanstackContent = await generateTanstackQueryFile({
49
- ...ctx,
50
- relativeApiClientPath: './' + basename(outputPath),
51
- });
52
- const tanstackOutputPath = join(dirname(outputPath), typeof options.tanstack === "string" ? options.tanstack : `tanstack.client.ts`);
53
- console.log("Generating tanstack client...", tanstackOutputPath);
54
- await writeFile(tanstackOutputPath, tanstackContent);
55
- }
56
-
57
- console.log(`Done in ${new Date().getTime() - now.getTime()}ms !`);
20
+ return generateClientFiles(input, _options);
58
21
  });
59
22
 
60
23
  cli.help();
@@ -0,0 +1,46 @@
1
+ import SwaggerParser from "@apidevtools/swagger-parser";
2
+ import type { OpenAPIObject } from "openapi3-ts/oas31";
3
+ import { basename, join, dirname } from "pathe";
4
+ import { type } from "arktype";
5
+ import { writeFile } from "fs/promises";
6
+ import { allowedRuntimes, generateFile } from "./generator.ts";
7
+ import { mapOpenApiEndpoints } from "./map-openapi-endpoints.ts";
8
+ import { generateTanstackQueryFile } from "./tanstack-query.generator.ts";
9
+ import { prettify } from "./format.ts";
10
+
11
+ const cwd = process.cwd();
12
+ const now = new Date();
13
+
14
+ export const optionsSchema = type({
15
+ "output?": "string",
16
+ runtime: allowedRuntimes,
17
+ tanstack: "boolean | string"
18
+ });
19
+
20
+ export async function generateClientFiles(input: string, options: typeof optionsSchema.infer) {
21
+ const openApiDoc = (await SwaggerParser.bundle(input)) as OpenAPIObject;
22
+
23
+ const ctx = mapOpenApiEndpoints(openApiDoc);
24
+ console.log(`Found ${ctx.endpointList.length} endpoints`);
25
+
26
+ const content = await prettify(generateFile({ ...ctx, runtime: options.runtime }));
27
+ const outputPath = join(
28
+ cwd,
29
+ options.output ?? input + `.${options.runtime === "none" ? "client" : options.runtime}.ts`,
30
+ );
31
+
32
+ console.log("Generating client...", outputPath);
33
+ await writeFile(outputPath, content);
34
+
35
+ if (options.tanstack) {
36
+ const tanstackContent = await generateTanstackQueryFile({
37
+ ...ctx,
38
+ relativeApiClientPath: './' + basename(outputPath),
39
+ });
40
+ const tanstackOutputPath = join(dirname(outputPath), typeof options.tanstack === "string" ? options.tanstack : `tanstack.client.ts`);
41
+ console.log("Generating tanstack client...", tanstackOutputPath);
42
+ await writeFile(tanstackOutputPath, tanstackContent);
43
+ }
44
+
45
+ console.log(`Done in ${new Date().getTime() - now.getTime()}ms !`);
46
+ }
@@ -0,0 +1,2 @@
1
+ export { prettify } from "./format.ts";
2
+ export { generateClientFiles } from "./generate-client-files.ts"
@@ -112,20 +112,28 @@ export const generateTanstackQueryFile = async (ctx: GeneratorContext & { relati
112
112
  * Generic mutation method with full type-safety for any endpoint that doesnt require parameters to be passed initially
113
113
  */
114
114
  mutation<
115
- TMethod extends keyof EndpointByMethod,
116
- TPath extends keyof EndpointByMethod[TMethod],
117
- TEndpoint extends EndpointByMethod[TMethod][TPath]
118
- >(
119
- method: TMethod,
120
- path: TPath) {
115
+ TMethod extends keyof EndpointByMethod,
116
+ TPath extends keyof EndpointByMethod[TMethod],
117
+ TEndpoint extends EndpointByMethod[TMethod][TPath],
118
+ TSelection,
119
+ >(method: TMethod, path: TPath, selectFn?: (res: Omit<Response, "json"> & {
120
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/json) */
121
+ json: () => Promise<TEndpoint extends { response: infer Res } ? Res : never>;
122
+ }) => TSelection) {
121
123
  const mutationKey = [{ method, path }] as const;
122
124
  return {
125
+ /** type-only property if you need easy access to the endpoint params */
126
+ "~endpoint": {} as TEndpoint,
127
+ mutationKey: mutationKey,
128
+ mutationOptions: {
123
129
  mutationKey: mutationKey,
124
- mutationOptions: {
125
- mutationKey: mutationKey,
126
- mutationFn: async (params: TEndpoint extends { parameters: infer Parameters} ? Parameters: never) => this.client.request(method, path, params)
127
- }
128
- }
130
+ mutationFn: async (params: TEndpoint extends { parameters: infer Parameters } ? Parameters : never) => {
131
+ const response = await this.client.request(method, path, params);
132
+ const res = selectFn ? selectFn(response) : response
133
+ return res as unknown extends TSelection ? typeof response : Awaited<TSelection>
134
+ },
135
+ },
136
+ };
129
137
  }
130
138
  // </ApiClient.request>
131
139
  }