@cushin/api-codegen 1.0.4 → 1.0.6
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 +88 -98
- package/dist/index.js.map +1 -1
- package/dist/runtime/client.d.ts +2 -3
- package/dist/runtime/client.js +4 -9
- package/dist/runtime/client.js.map +1 -1
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -178,24 +178,12 @@ var HooksGenerator = class extends BaseGenerator {
|
|
|
178
178
|
const endpointsPath = path6.join(this.context.config.endpointsPath);
|
|
179
179
|
const relativePath = path6.relative(path6.dirname(outputPath), endpointsPath).replace(/\\/g, "/");
|
|
180
180
|
const content = `${useClientDirective ? "'use client';\n" : ""}
|
|
181
|
-
import { useQuery, useMutation, useQueryClient } from
|
|
182
|
-
import
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
} from
|
|
187
|
-
import { apiClient } from './client';
|
|
188
|
-
import type {
|
|
189
|
-
APIEndpoints,
|
|
190
|
-
ExtractBody,
|
|
191
|
-
ExtractParams,
|
|
192
|
-
ExtractQuery,
|
|
193
|
-
ExtractResponse
|
|
194
|
-
} from './types';
|
|
195
|
-
import { queryKeys } from './query-keys';
|
|
196
|
-
${this.hasQueryOptions() ? "import { apiQueryOptions } from './query-options';" : ""}
|
|
197
|
-
import { z } from 'zod';
|
|
198
|
-
import { apiConfig } from '${relativePath}';
|
|
181
|
+
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
|
182
|
+
import { apiClient } from "./client";
|
|
183
|
+
import { queryKeys } from "./query-keys";
|
|
184
|
+
import { apiQueryOptions } from "./query-options";
|
|
185
|
+
import { z } from "zod";
|
|
186
|
+
import { apiConfig } from "${relativePath}";
|
|
199
187
|
|
|
200
188
|
${this.generateQueryHooks()}
|
|
201
189
|
${this.generateMutationHooks()}
|
|
@@ -506,25 +494,27 @@ ${this.generateEndpointTypes()}
|
|
|
506
494
|
}
|
|
507
495
|
generateEndpointTypes() {
|
|
508
496
|
const types = [];
|
|
509
|
-
Object.entries(this.context.apiConfig.endpoints).forEach(
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
497
|
+
Object.entries(this.context.apiConfig.endpoints).forEach(
|
|
498
|
+
([name, endpoint]) => {
|
|
499
|
+
const cap = this.capitalize(name);
|
|
500
|
+
if (endpoint.response)
|
|
501
|
+
types.push(
|
|
502
|
+
`export type ${cap}Response = ${this.inferNonNull(`typeof apiConfig.endpoints.${name}.response`)};`
|
|
503
|
+
);
|
|
504
|
+
if (endpoint.body)
|
|
505
|
+
types.push(
|
|
506
|
+
`export type ${cap}Input = ${this.inferNonNull(`typeof apiConfig.endpoints.${name}.body`)};`
|
|
507
|
+
);
|
|
508
|
+
if (endpoint.query)
|
|
509
|
+
types.push(
|
|
510
|
+
`export type ${cap}Query = ${this.inferNonNull(`typeof apiConfig.endpoints.${name}.query`)};`
|
|
511
|
+
);
|
|
512
|
+
if (endpoint.params)
|
|
513
|
+
types.push(
|
|
514
|
+
`export type ${cap}Params = ${this.inferNonNull(`typeof apiConfig.endpoints.${name}.params`)};`
|
|
515
|
+
);
|
|
516
|
+
}
|
|
517
|
+
);
|
|
528
518
|
return types.join("\n");
|
|
529
519
|
}
|
|
530
520
|
};
|
|
@@ -543,7 +533,10 @@ var ClientGenerator = class extends BaseGenerator {
|
|
|
543
533
|
}
|
|
544
534
|
async generateServerClientFile() {
|
|
545
535
|
const content = this.generateServerClientContent();
|
|
546
|
-
const outputPath = path6.join(
|
|
536
|
+
const outputPath = path6.join(
|
|
537
|
+
this.context.config.outputDir,
|
|
538
|
+
"server-client.ts"
|
|
539
|
+
);
|
|
547
540
|
await fs5.mkdir(path6.dirname(outputPath), { recursive: true });
|
|
548
541
|
await fs5.writeFile(outputPath, content, "utf-8");
|
|
549
542
|
}
|
|
@@ -556,36 +549,36 @@ var ClientGenerator = class extends BaseGenerator {
|
|
|
556
549
|
import { createAPIClient } from '@cushin/api-codegen/client';
|
|
557
550
|
import type { AuthCallbacks } from '@cushin/api-codegen/client';
|
|
558
551
|
import { apiConfig } from '${relativePath}';
|
|
559
|
-
import type { APIEndpoints } from './types';
|
|
560
552
|
import { z } from 'zod';
|
|
561
553
|
|
|
562
|
-
// Type
|
|
554
|
+
// Type the methods based on endpoints
|
|
563
555
|
type APIClientMethods = {
|
|
564
|
-
[K in keyof
|
|
556
|
+
[K in keyof typeof apiConfig.endpoints]: (typeof apiConfig.endpoints)[K] extends {
|
|
565
557
|
method: infer M;
|
|
566
558
|
params?: infer P;
|
|
567
559
|
query?: infer Q;
|
|
568
560
|
body?: infer B;
|
|
569
561
|
response: infer R;
|
|
570
562
|
}
|
|
571
|
-
? M extends
|
|
572
|
-
? P extends
|
|
573
|
-
? Q extends
|
|
574
|
-
? (params: P
|
|
575
|
-
: (params: P
|
|
576
|
-
: Q extends
|
|
577
|
-
? (query?: Q
|
|
578
|
-
: () => Promise<R
|
|
579
|
-
: P extends
|
|
580
|
-
? B extends
|
|
581
|
-
? (params: P
|
|
582
|
-
: (params: P
|
|
583
|
-
: B extends
|
|
584
|
-
? (body: B
|
|
585
|
-
: () => Promise<R
|
|
563
|
+
? M extends "GET"
|
|
564
|
+
? P extends z.ZodJSONSchema
|
|
565
|
+
? Q extends z.ZodJSONSchema
|
|
566
|
+
? (params: z.infer<P>, query?: z.infer<Q>) => Promise<z.infer<R>>
|
|
567
|
+
: (params: z.infer<P>) => Promise<z.infer<R>>
|
|
568
|
+
: Q extends z.ZodJSONSchema
|
|
569
|
+
? (query?: z.infer<Q>) => Promise<z.infer<R>>
|
|
570
|
+
: () => Promise<z.infer<R>>
|
|
571
|
+
: P extends z.ZodJSONSchema
|
|
572
|
+
? B extends z.ZodJSONSchema
|
|
573
|
+
? (params: z.infer<P>, body: z.infer<B>) => Promise<z.infer<R>>
|
|
574
|
+
: (params: z.infer<P>) => Promise<z.infer<R>>
|
|
575
|
+
: B extends z.ZodJSONSchema
|
|
576
|
+
? (body: z.infer<B>) => Promise<z.infer<R>>
|
|
577
|
+
: () => Promise<z.infer<R>>
|
|
586
578
|
: never;
|
|
587
579
|
};
|
|
588
580
|
|
|
581
|
+
|
|
589
582
|
// Export singleton instance (will be initialized later)
|
|
590
583
|
export let baseClient: APIClientMethods & {
|
|
591
584
|
refreshAuth: () => Promise<void>;
|
|
@@ -665,49 +658,51 @@ export const serverClient = createAPIClient(apiConfig) as APIClientMethods;
|
|
|
665
658
|
}
|
|
666
659
|
generateApiClientMethods() {
|
|
667
660
|
const methods = [];
|
|
668
|
-
Object.entries(this.context.apiConfig.endpoints).forEach(
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
if (endpoint.
|
|
683
|
-
|
|
661
|
+
Object.entries(this.context.apiConfig.endpoints).forEach(
|
|
662
|
+
([name, endpoint]) => {
|
|
663
|
+
const inferParams = this.inferNonNull(
|
|
664
|
+
`typeof apiConfig.endpoints.${name}.params`
|
|
665
|
+
);
|
|
666
|
+
const inferQuery = this.inferNonNull(
|
|
667
|
+
`typeof apiConfig.endpoints.${name}.query`
|
|
668
|
+
);
|
|
669
|
+
const inferBody = this.inferNonNull(
|
|
670
|
+
`typeof apiConfig.endpoints.${name}.body`
|
|
671
|
+
);
|
|
672
|
+
const inferResponse = this.inferNonNull(
|
|
673
|
+
`typeof apiConfig.endpoints.${name}.response`
|
|
674
|
+
);
|
|
675
|
+
if (endpoint.method === "GET") {
|
|
676
|
+
if (endpoint.params && endpoint.query) {
|
|
677
|
+
methods.push(` ${name}: (params: ${inferParams}, query?: ${inferQuery}): Promise<${inferResponse}> =>
|
|
684
678
|
(baseClient as any).${name}(params, query),`);
|
|
685
|
-
|
|
686
|
-
|
|
679
|
+
} else if (endpoint.params) {
|
|
680
|
+
methods.push(` ${name}: (params: ${inferParams}): Promise<${inferResponse}> =>
|
|
687
681
|
(baseClient as any).${name}(params),`);
|
|
688
|
-
|
|
689
|
-
|
|
682
|
+
} else if (endpoint.query) {
|
|
683
|
+
methods.push(` ${name}: (query?: ${inferQuery}): Promise<${inferResponse}> =>
|
|
690
684
|
(baseClient as any).${name}(query),`);
|
|
691
|
-
|
|
692
|
-
|
|
685
|
+
} else {
|
|
686
|
+
methods.push(` ${name}: (): Promise<${inferResponse}> =>
|
|
693
687
|
(baseClient as any).${name}(),`);
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
688
|
+
}
|
|
689
|
+
} else {
|
|
690
|
+
if (endpoint.params && endpoint.body) {
|
|
691
|
+
methods.push(` ${name}: (params: ${inferParams}, body: ${inferBody}): Promise<${inferResponse}> =>
|
|
698
692
|
(baseClient as any).${name}(params, body),`);
|
|
699
|
-
|
|
700
|
-
|
|
693
|
+
} else if (endpoint.params) {
|
|
694
|
+
methods.push(` ${name}: (params: ${inferParams}): Promise<${inferResponse}> =>
|
|
701
695
|
(baseClient as any).${name}(params),`);
|
|
702
|
-
|
|
703
|
-
|
|
696
|
+
} else if (endpoint.body) {
|
|
697
|
+
methods.push(` ${name}: (body: ${inferBody}): Promise<${inferResponse}> =>
|
|
704
698
|
(baseClient as any).${name}(body),`);
|
|
705
|
-
|
|
706
|
-
|
|
699
|
+
} else {
|
|
700
|
+
methods.push(` ${name}: (): Promise<${inferResponse}> =>
|
|
707
701
|
(baseClient as any).${name}(),`);
|
|
702
|
+
}
|
|
708
703
|
}
|
|
709
704
|
}
|
|
710
|
-
|
|
705
|
+
);
|
|
711
706
|
return methods.join("\n");
|
|
712
707
|
}
|
|
713
708
|
};
|