@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 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 '@tanstack/react-query';
182
- import type {
183
- UseQueryOptions,
184
- UseMutationOptions,
185
- QueryKey
186
- } from '@tanstack/react-query';
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(([name, endpoint]) => {
510
- const cap = this.capitalize(name);
511
- if (endpoint.response)
512
- types.push(
513
- `export type ${cap}Response = ${this.inferNonNull(`typeof apiConfig.endpoints.${name}.response`)};`
514
- );
515
- if (endpoint.body)
516
- types.push(
517
- `export type ${cap}Input = ${this.inferNonNull(`typeof apiConfig.endpoints.${name}.body`)};`
518
- );
519
- if (endpoint.query)
520
- types.push(
521
- `export type ${cap}Query = ${this.inferNonNull(`typeof apiConfig.endpoints.${name}.query`)};`
522
- );
523
- if (endpoint.params)
524
- types.push(
525
- `export type ${cap}Params = ${this.inferNonNull(`typeof apiConfig.endpoints.${name}.params`)};`
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(this.context.config.outputDir, "server-client.ts");
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-safe API client methods
554
+ // Type the methods based on endpoints
563
555
  type APIClientMethods = {
564
- [K in keyof APIEndpoints]: APIEndpoints[K] extends {
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 'GET'
572
- ? P extends { _type: any }
573
- ? Q extends { _type: any }
574
- ? (params: P['_type'], query?: Q['_type']) => Promise<R['_type']>
575
- : (params: P['_type']) => Promise<R['_type']>
576
- : Q extends { _type: any }
577
- ? (query?: Q['_type']) => Promise<R['_type']>
578
- : () => Promise<R['_type']>
579
- : P extends { _type: any }
580
- ? B extends { _type: any }
581
- ? (params: P['_type'], body: B['_type']) => Promise<R['_type']>
582
- : (params: P['_type']) => Promise<R['_type']>
583
- : B extends { _type: any }
584
- ? (body: B['_type']) => Promise<R['_type']>
585
- : () => Promise<R['_type']>
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(([name, endpoint]) => {
669
- const inferParams = this.inferNonNull(
670
- `typeof apiConfig.endpoints.${name}.params`
671
- );
672
- const inferQuery = this.inferNonNull(
673
- `typeof apiConfig.endpoints.${name}.query`
674
- );
675
- const inferBody = this.inferNonNull(
676
- `typeof apiConfig.endpoints.${name}.body`
677
- );
678
- const inferResponse = this.inferNonNull(
679
- `typeof apiConfig.endpoints.${name}.response`
680
- );
681
- if (endpoint.method === "GET") {
682
- if (endpoint.params && endpoint.query) {
683
- methods.push(` ${name}: (params: ${inferParams}, query?: ${inferQuery}): Promise<${inferResponse}> =>
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
- } else if (endpoint.params) {
686
- methods.push(` ${name}: (params: ${inferParams}): Promise<${inferResponse}> =>
679
+ } else if (endpoint.params) {
680
+ methods.push(` ${name}: (params: ${inferParams}): Promise<${inferResponse}> =>
687
681
  (baseClient as any).${name}(params),`);
688
- } else if (endpoint.query) {
689
- methods.push(` ${name}: (query?: ${inferQuery}): Promise<${inferResponse}> =>
682
+ } else if (endpoint.query) {
683
+ methods.push(` ${name}: (query?: ${inferQuery}): Promise<${inferResponse}> =>
690
684
  (baseClient as any).${name}(query),`);
691
- } else {
692
- methods.push(` ${name}: (): Promise<${inferResponse}> =>
685
+ } else {
686
+ methods.push(` ${name}: (): Promise<${inferResponse}> =>
693
687
  (baseClient as any).${name}(),`);
694
- }
695
- } else {
696
- if (endpoint.params && endpoint.body) {
697
- methods.push(` ${name}: (params: ${inferParams}, body: ${inferBody}): Promise<${inferResponse}> =>
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
- } else if (endpoint.params) {
700
- methods.push(` ${name}: (params: ${inferParams}): Promise<${inferResponse}> =>
693
+ } else if (endpoint.params) {
694
+ methods.push(` ${name}: (params: ${inferParams}): Promise<${inferResponse}> =>
701
695
  (baseClient as any).${name}(params),`);
702
- } else if (endpoint.body) {
703
- methods.push(` ${name}: (body: ${inferBody}): Promise<${inferResponse}> =>
696
+ } else if (endpoint.body) {
697
+ methods.push(` ${name}: (body: ${inferBody}): Promise<${inferResponse}> =>
704
698
  (baseClient as any).${name}(body),`);
705
- } else {
706
- methods.push(` ${name}: (): Promise<${inferResponse}> =>
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
  };