typed-openapi 1.2.0 → 1.3.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.
@@ -685,8 +685,8 @@ var getTransitiveDependencies = (directDependencies) => {
685
685
 
686
686
  // src/ts-factory.ts
687
687
  var tsFactory = createFactory({
688
- union: (types) => types.map(unwrap).join(" | "),
689
- intersection: (types) => types.map(unwrap).join(" & "),
688
+ union: (types) => `(${types.map(unwrap).join(" | ")})`,
689
+ intersection: (types) => `(${types.map(unwrap).join(" & ")})`,
690
690
  array: (type2) => `Array<${unwrap(type2)}>`,
691
691
  optional: (type2) => `${unwrap(type2)} | undefined`,
692
692
  reference: (name, typeArgs) => `${name}${typeArgs ? `<${typeArgs.map(unwrap).join(", ")}>` : ""}`,
@@ -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
  }
package/dist/cli.js CHANGED
@@ -4,7 +4,7 @@ import {
4
4
  generateTanstackQueryFile,
5
5
  mapOpenApiEndpoints,
6
6
  prettify
7
- } from "./chunk-SLMCJHD5.js";
7
+ } from "./chunk-YLPQQ24J.js";
8
8
 
9
9
  // src/cli.ts
10
10
  import SwaggerParser from "@apidevtools/swagger-parser";
package/dist/index.js CHANGED
@@ -8,7 +8,7 @@ import {
8
8
  openApiSchemaToTs,
9
9
  tsFactory,
10
10
  unwrap
11
- } from "./chunk-SLMCJHD5.js";
11
+ } from "./chunk-YLPQQ24J.js";
12
12
  export {
13
13
  createBoxFactory,
14
14
  createFactory,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "typed-openapi",
3
3
  "type": "module",
4
- "version": "1.2.0",
4
+ "version": "1.3.1",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
7
7
  "exports": {
@@ -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
  }
package/src/ts-factory.ts CHANGED
@@ -3,8 +3,8 @@ import { createFactory, unwrap } from "./box-factory.ts";
3
3
  import { wrapWithQuotesIfNeeded } from "./string-utils.ts";
4
4
 
5
5
  export const tsFactory = createFactory({
6
- union: (types) => types.map(unwrap).join(" | "),
7
- intersection: (types) => types.map(unwrap).join(" & "),
6
+ union: (types) => `(${types.map(unwrap).join(" | ")})`,
7
+ intersection: (types) => `(${types.map(unwrap).join(" & ")})`,
8
8
  array: (type) => `Array<${unwrap(type)}>`,
9
9
  optional: (type) => `${unwrap(type)} | undefined`,
10
10
  reference: (name, typeArgs) => `${name}${typeArgs ? `<${typeArgs.map(unwrap).join(", ")}>` : ""}`,