@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/cli.js +84 -89
- package/dist/cli.js.map +1 -1
- package/dist/index.js +84 -89
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
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
|
|
447
|
-
import
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
} from
|
|
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(
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
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(
|
|
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
|
|
819
|
+
// Type the methods based on endpoints
|
|
828
820
|
type APIClientMethods = {
|
|
829
|
-
[K in keyof
|
|
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
|
|
837
|
-
? P extends
|
|
838
|
-
? Q extends
|
|
839
|
-
? (params: P
|
|
840
|
-
: (params: P
|
|
841
|
-
: Q extends
|
|
842
|
-
? (query?: Q
|
|
843
|
-
: () => Promise<R
|
|
844
|
-
: P extends
|
|
845
|
-
? B extends
|
|
846
|
-
? (params: P
|
|
847
|
-
: (params: P
|
|
848
|
-
: B extends
|
|
849
|
-
? (body: B
|
|
850
|
-
: () => Promise<R
|
|
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(
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
if (endpoint.
|
|
948
|
-
|
|
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
|
-
|
|
951
|
-
|
|
944
|
+
} else if (endpoint.params) {
|
|
945
|
+
methods.push(` ${name}: (params: ${inferParams}): Promise<${inferResponse}> =>
|
|
952
946
|
(baseClient as any).${name}(params),`);
|
|
953
|
-
|
|
954
|
-
|
|
947
|
+
} else if (endpoint.query) {
|
|
948
|
+
methods.push(` ${name}: (query?: ${inferQuery}): Promise<${inferResponse}> =>
|
|
955
949
|
(baseClient as any).${name}(query),`);
|
|
956
|
-
|
|
957
|
-
|
|
950
|
+
} else {
|
|
951
|
+
methods.push(` ${name}: (): Promise<${inferResponse}> =>
|
|
958
952
|
(baseClient as any).${name}(),`);
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
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
|
-
|
|
965
|
-
|
|
958
|
+
} else if (endpoint.params) {
|
|
959
|
+
methods.push(` ${name}: (params: ${inferParams}): Promise<${inferResponse}> =>
|
|
966
960
|
(baseClient as any).${name}(params),`);
|
|
967
|
-
|
|
968
|
-
|
|
961
|
+
} else if (endpoint.body) {
|
|
962
|
+
methods.push(` ${name}: (body: ${inferBody}): Promise<${inferResponse}> =>
|
|
969
963
|
(baseClient as any).${name}(body),`);
|
|
970
|
-
|
|
971
|
-
|
|
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
|
};
|