@cushin/api-codegen 1.0.4 → 1.0.5

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/dist/index.js CHANGED
@@ -443,24 +443,12 @@ var HooksGenerator = class extends BaseGenerator {
443
443
  const endpointsPath = path6.join(this.context.config.endpointsPath);
444
444
  const relativePath = path6.relative(path6.dirname(outputPath), endpointsPath).replace(/\\/g, "/");
445
445
  const content = `${useClientDirective ? "'use client';\n" : ""}
446
- import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
447
- import type {
448
- UseQueryOptions,
449
- UseMutationOptions,
450
- QueryKey
451
- } from '@tanstack/react-query';
452
- import { apiClient } from './client';
453
- import type {
454
- APIEndpoints,
455
- ExtractBody,
456
- ExtractParams,
457
- ExtractQuery,
458
- ExtractResponse
459
- } from './types';
460
- import { queryKeys } from './query-keys';
461
- ${this.hasQueryOptions() ? "import { apiQueryOptions } from './query-options';" : ""}
462
- import { z } from 'zod';
463
- import { apiConfig } from '${relativePath}';
446
+ import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
447
+ import { apiClient } from "./client";
448
+ import { queryKeys } from "./query-keys";
449
+ import { apiQueryOptions } from "./query-options";
450
+ import { z } from "zod";
451
+ import { apiConfig } from "${relativePath}";
464
452
 
465
453
  ${this.generateQueryHooks()}
466
454
  ${this.generateMutationHooks()}
@@ -771,25 +759,27 @@ ${this.generateEndpointTypes()}
771
759
  }
772
760
  generateEndpointTypes() {
773
761
  const types = [];
774
- Object.entries(this.context.apiConfig.endpoints).forEach(([name, endpoint]) => {
775
- const cap = this.capitalize(name);
776
- if (endpoint.response)
777
- types.push(
778
- `export type ${cap}Response = ${this.inferNonNull(`typeof apiConfig.endpoints.${name}.response`)};`
779
- );
780
- if (endpoint.body)
781
- types.push(
782
- `export type ${cap}Input = ${this.inferNonNull(`typeof apiConfig.endpoints.${name}.body`)};`
783
- );
784
- if (endpoint.query)
785
- types.push(
786
- `export type ${cap}Query = ${this.inferNonNull(`typeof apiConfig.endpoints.${name}.query`)};`
787
- );
788
- if (endpoint.params)
789
- types.push(
790
- `export type ${cap}Params = ${this.inferNonNull(`typeof apiConfig.endpoints.${name}.params`)};`
791
- );
792
- });
762
+ Object.entries(this.context.apiConfig.endpoints).forEach(
763
+ ([name, endpoint]) => {
764
+ const cap = this.capitalize(name);
765
+ if (endpoint.response)
766
+ types.push(
767
+ `export type ${cap}Response = ${this.inferNonNull(`typeof apiConfig.endpoints.${name}.response`)};`
768
+ );
769
+ if (endpoint.body)
770
+ types.push(
771
+ `export type ${cap}Input = ${this.inferNonNull(`typeof apiConfig.endpoints.${name}.body`)};`
772
+ );
773
+ if (endpoint.query)
774
+ types.push(
775
+ `export type ${cap}Query = ${this.inferNonNull(`typeof apiConfig.endpoints.${name}.query`)};`
776
+ );
777
+ if (endpoint.params)
778
+ types.push(
779
+ `export type ${cap}Params = ${this.inferNonNull(`typeof apiConfig.endpoints.${name}.params`)};`
780
+ );
781
+ }
782
+ );
793
783
  return types.join("\n");
794
784
  }
795
785
  };
@@ -808,7 +798,10 @@ var ClientGenerator = class extends BaseGenerator {
808
798
  }
809
799
  async generateServerClientFile() {
810
800
  const content = this.generateServerClientContent();
811
- const outputPath = path6.join(this.context.config.outputDir, "server-client.ts");
801
+ const outputPath = path6.join(
802
+ this.context.config.outputDir,
803
+ "server-client.ts"
804
+ );
812
805
  await fs5.mkdir(path6.dirname(outputPath), { recursive: true });
813
806
  await fs5.writeFile(outputPath, content, "utf-8");
814
807
  }
@@ -821,36 +814,36 @@ var ClientGenerator = class extends BaseGenerator {
821
814
  import { createAPIClient } from '@cushin/api-codegen/client';
822
815
  import type { AuthCallbacks } from '@cushin/api-codegen/client';
823
816
  import { apiConfig } from '${relativePath}';
824
- import type { APIEndpoints } from './types';
825
817
  import { z } from 'zod';
826
818
 
827
- // Type-safe API client methods
819
+ // Type the methods based on endpoints
828
820
  type APIClientMethods = {
829
- [K in keyof APIEndpoints]: APIEndpoints[K] extends {
821
+ [K in keyof typeof apiConfig.endpoints]: (typeof apiConfig.endpoints)[K] extends {
830
822
  method: infer M;
831
823
  params?: infer P;
832
824
  query?: infer Q;
833
825
  body?: infer B;
834
826
  response: infer R;
835
827
  }
836
- ? M extends 'GET'
837
- ? P extends { _type: any }
838
- ? Q extends { _type: any }
839
- ? (params: P['_type'], query?: Q['_type']) => Promise<R['_type']>
840
- : (params: P['_type']) => Promise<R['_type']>
841
- : Q extends { _type: any }
842
- ? (query?: Q['_type']) => Promise<R['_type']>
843
- : () => Promise<R['_type']>
844
- : P extends { _type: any }
845
- ? B extends { _type: any }
846
- ? (params: P['_type'], body: B['_type']) => Promise<R['_type']>
847
- : (params: P['_type']) => Promise<R['_type']>
848
- : B extends { _type: any }
849
- ? (body: B['_type']) => Promise<R['_type']>
850
- : () => Promise<R['_type']>
828
+ ? M extends "GET"
829
+ ? P extends z.ZodJSONSchema
830
+ ? Q extends z.ZodJSONSchema
831
+ ? (params: z.infer<P>, query?: z.infer<Q>) => Promise<z.infer<R>>
832
+ : (params: z.infer<P>) => Promise<z.infer<R>>
833
+ : Q extends z.ZodJSONSchema
834
+ ? (query?: z.infer<Q>) => Promise<z.infer<R>>
835
+ : () => Promise<z.infer<R>>
836
+ : P extends z.ZodJSONSchema
837
+ ? B extends z.ZodJSONSchema
838
+ ? (params: z.infer<P>, body: z.infer<B>) => Promise<z.infer<R>>
839
+ : (params: z.infer<P>) => Promise<z.infer<R>>
840
+ : B extends z.ZodJSONSchema
841
+ ? (body: z.infer<B>) => Promise<z.infer<R>>
842
+ : () => Promise<z.infer<R>>
851
843
  : never;
852
844
  };
853
845
 
846
+
854
847
  // Export singleton instance (will be initialized later)
855
848
  export let baseClient: APIClientMethods & {
856
849
  refreshAuth: () => Promise<void>;
@@ -930,49 +923,51 @@ export const serverClient = createAPIClient(apiConfig) as APIClientMethods;
930
923
  }
931
924
  generateApiClientMethods() {
932
925
  const methods = [];
933
- Object.entries(this.context.apiConfig.endpoints).forEach(([name, endpoint]) => {
934
- const inferParams = this.inferNonNull(
935
- `typeof apiConfig.endpoints.${name}.params`
936
- );
937
- const inferQuery = this.inferNonNull(
938
- `typeof apiConfig.endpoints.${name}.query`
939
- );
940
- const inferBody = this.inferNonNull(
941
- `typeof apiConfig.endpoints.${name}.body`
942
- );
943
- const inferResponse = this.inferNonNull(
944
- `typeof apiConfig.endpoints.${name}.response`
945
- );
946
- if (endpoint.method === "GET") {
947
- if (endpoint.params && endpoint.query) {
948
- methods.push(` ${name}: (params: ${inferParams}, query?: ${inferQuery}): Promise<${inferResponse}> =>
926
+ Object.entries(this.context.apiConfig.endpoints).forEach(
927
+ ([name, endpoint]) => {
928
+ const inferParams = this.inferNonNull(
929
+ `typeof apiConfig.endpoints.${name}.params`
930
+ );
931
+ const inferQuery = this.inferNonNull(
932
+ `typeof apiConfig.endpoints.${name}.query`
933
+ );
934
+ const inferBody = this.inferNonNull(
935
+ `typeof apiConfig.endpoints.${name}.body`
936
+ );
937
+ const inferResponse = this.inferNonNull(
938
+ `typeof apiConfig.endpoints.${name}.response`
939
+ );
940
+ if (endpoint.method === "GET") {
941
+ if (endpoint.params && endpoint.query) {
942
+ methods.push(` ${name}: (params: ${inferParams}, query?: ${inferQuery}): Promise<${inferResponse}> =>
949
943
  (baseClient as any).${name}(params, query),`);
950
- } else if (endpoint.params) {
951
- methods.push(` ${name}: (params: ${inferParams}): Promise<${inferResponse}> =>
944
+ } else if (endpoint.params) {
945
+ methods.push(` ${name}: (params: ${inferParams}): Promise<${inferResponse}> =>
952
946
  (baseClient as any).${name}(params),`);
953
- } else if (endpoint.query) {
954
- methods.push(` ${name}: (query?: ${inferQuery}): Promise<${inferResponse}> =>
947
+ } else if (endpoint.query) {
948
+ methods.push(` ${name}: (query?: ${inferQuery}): Promise<${inferResponse}> =>
955
949
  (baseClient as any).${name}(query),`);
956
- } else {
957
- methods.push(` ${name}: (): Promise<${inferResponse}> =>
950
+ } else {
951
+ methods.push(` ${name}: (): Promise<${inferResponse}> =>
958
952
  (baseClient as any).${name}(),`);
959
- }
960
- } else {
961
- if (endpoint.params && endpoint.body) {
962
- methods.push(` ${name}: (params: ${inferParams}, body: ${inferBody}): Promise<${inferResponse}> =>
953
+ }
954
+ } else {
955
+ if (endpoint.params && endpoint.body) {
956
+ methods.push(` ${name}: (params: ${inferParams}, body: ${inferBody}): Promise<${inferResponse}> =>
963
957
  (baseClient as any).${name}(params, body),`);
964
- } else if (endpoint.params) {
965
- methods.push(` ${name}: (params: ${inferParams}): Promise<${inferResponse}> =>
958
+ } else if (endpoint.params) {
959
+ methods.push(` ${name}: (params: ${inferParams}): Promise<${inferResponse}> =>
966
960
  (baseClient as any).${name}(params),`);
967
- } else if (endpoint.body) {
968
- methods.push(` ${name}: (body: ${inferBody}): Promise<${inferResponse}> =>
961
+ } else if (endpoint.body) {
962
+ methods.push(` ${name}: (body: ${inferBody}): Promise<${inferResponse}> =>
969
963
  (baseClient as any).${name}(body),`);
970
- } else {
971
- methods.push(` ${name}: (): Promise<${inferResponse}> =>
964
+ } else {
965
+ methods.push(` ${name}: (): Promise<${inferResponse}> =>
972
966
  (baseClient as any).${name}(),`);
967
+ }
973
968
  }
974
969
  }
975
- });
970
+ );
976
971
  return methods.join("\n");
977
972
  }
978
973
  };