@cushin/api-codegen 1.1.0 → 2.0.0
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 +104 -201
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +141 -7
- package/dist/index.js +100 -53
- package/dist/index.js.map +1 -1
- package/package.json +14 -19
- package/README.md +0 -435
- package/dist/cli.d.ts +0 -1
- package/dist/config/index.d.ts +0 -84
- package/dist/config/index.js +0 -69
- package/dist/config/index.js.map +0 -1
- package/dist/config/schema.d.ts +0 -43
- package/dist/config/schema.js +0 -14
- package/dist/config/schema.js.map +0 -1
- package/dist/runtime/client.d.ts +0 -40
- package/dist/runtime/client.js +0 -260
- package/dist/runtime/client.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
import { cosmiconfig } from 'cosmiconfig';
|
|
2
|
-
import path6 from 'path';
|
|
3
|
-
import ky, { HTTPError } from 'ky';
|
|
4
|
-
import { createJiti } from 'jiti';
|
|
5
|
-
import fs5 from 'fs/promises';
|
|
6
|
-
import { fileURLToPath } from 'url';
|
|
7
|
-
|
|
8
1
|
// src/config/schema.ts
|
|
9
2
|
function defineConfig(config) {
|
|
10
3
|
return config;
|
|
@@ -15,6 +8,10 @@ function defineEndpoint(config) {
|
|
|
15
8
|
function defineEndpoints(endpoints) {
|
|
16
9
|
return endpoints;
|
|
17
10
|
}
|
|
11
|
+
|
|
12
|
+
// src/config/index.ts
|
|
13
|
+
import { cosmiconfig } from "cosmiconfig";
|
|
14
|
+
import path from "path";
|
|
18
15
|
var explorer = cosmiconfig("api-codegen", {
|
|
19
16
|
searchPlaces: [
|
|
20
17
|
"api-codegen.config.js",
|
|
@@ -33,9 +30,9 @@ async function loadConfig(configPath) {
|
|
|
33
30
|
return null;
|
|
34
31
|
}
|
|
35
32
|
const userConfig = result.config;
|
|
36
|
-
const rootDir =
|
|
37
|
-
const endpointsPath =
|
|
38
|
-
const outputDir =
|
|
33
|
+
const rootDir = path.dirname(result.filepath);
|
|
34
|
+
const endpointsPath = path.resolve(rootDir, userConfig.endpoints);
|
|
35
|
+
const outputDir = path.resolve(rootDir, userConfig.output);
|
|
39
36
|
const generateHooks = userConfig.generateHooks ?? true;
|
|
40
37
|
const generateServerActions = userConfig.generateServerActions ?? userConfig.provider === "nextjs";
|
|
41
38
|
const generateServerQueries = userConfig.generateServerQueries ?? userConfig.provider === "nextjs";
|
|
@@ -76,6 +73,9 @@ function validateConfig(config) {
|
|
|
76
73
|
throw new Error('Config error: "output" directory is required');
|
|
77
74
|
}
|
|
78
75
|
}
|
|
76
|
+
|
|
77
|
+
// src/runtime/client.ts
|
|
78
|
+
import ky, { HTTPError } from "ky";
|
|
79
79
|
var APIError = class extends Error {
|
|
80
80
|
constructor(message, status, response) {
|
|
81
81
|
super(message);
|
|
@@ -330,6 +330,13 @@ function createAPIClient(config, authCallbacks) {
|
|
|
330
330
|
};
|
|
331
331
|
}
|
|
332
332
|
|
|
333
|
+
// src/core/codegen.ts
|
|
334
|
+
import { createJiti } from "jiti";
|
|
335
|
+
|
|
336
|
+
// src/generators/hooks.ts
|
|
337
|
+
import fs from "fs/promises";
|
|
338
|
+
import path2 from "path";
|
|
339
|
+
|
|
333
340
|
// src/generators/base.ts
|
|
334
341
|
var BaseGenerator = class {
|
|
335
342
|
constructor(context) {
|
|
@@ -434,15 +441,15 @@ var BaseGenerator = class {
|
|
|
434
441
|
var HooksGenerator = class extends BaseGenerator {
|
|
435
442
|
async generate() {
|
|
436
443
|
const content = this.generateContent();
|
|
437
|
-
const outputPath =
|
|
438
|
-
await
|
|
439
|
-
await
|
|
444
|
+
const outputPath = path2.join(this.context.config.outputDir, "hooks.ts");
|
|
445
|
+
await fs.mkdir(path2.dirname(outputPath), { recursive: true });
|
|
446
|
+
await fs.writeFile(outputPath, content, "utf-8");
|
|
440
447
|
}
|
|
441
448
|
generateContent() {
|
|
442
449
|
const useClientDirective = this.context.config.options?.useClientDirective ?? true;
|
|
443
|
-
const outputPath =
|
|
444
|
-
const endpointsPath =
|
|
445
|
-
const relativePath =
|
|
450
|
+
const outputPath = path2.join(this.context.config.outputDir, "types.ts");
|
|
451
|
+
const endpointsPath = path2.join(this.context.config.endpointsPath);
|
|
452
|
+
const relativePath = path2.relative(path2.dirname(outputPath), endpointsPath).replace(/\\/g, "/");
|
|
446
453
|
const content = `${useClientDirective ? "'use client';\n" : ""}
|
|
447
454
|
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
|
448
455
|
import { apiClient } from "./client";
|
|
@@ -568,12 +575,16 @@ export function ${hookName}(${params.join(",\n ")}) {
|
|
|
568
575
|
}`;
|
|
569
576
|
}
|
|
570
577
|
};
|
|
578
|
+
|
|
579
|
+
// src/generators/actions.ts
|
|
580
|
+
import fs2 from "fs/promises";
|
|
581
|
+
import path3 from "path";
|
|
571
582
|
var ServerActionsGenerator = class extends BaseGenerator {
|
|
572
583
|
async generate() {
|
|
573
584
|
const content = this.generateContent();
|
|
574
|
-
const outputPath =
|
|
575
|
-
await
|
|
576
|
-
await
|
|
585
|
+
const outputPath = path3.join(this.context.config.outputDir, "actions.ts");
|
|
586
|
+
await fs2.mkdir(path3.dirname(outputPath), { recursive: true });
|
|
587
|
+
await fs2.writeFile(outputPath, content, "utf-8");
|
|
577
588
|
}
|
|
578
589
|
generateContent() {
|
|
579
590
|
const imports = `'use server';
|
|
@@ -641,12 +652,16 @@ ${revalidateStatements}
|
|
|
641
652
|
}`;
|
|
642
653
|
}
|
|
643
654
|
};
|
|
655
|
+
|
|
656
|
+
// src/generators/queries.ts
|
|
657
|
+
import fs3 from "fs/promises";
|
|
658
|
+
import path4 from "path";
|
|
644
659
|
var ServerQueriesGenerator = class extends BaseGenerator {
|
|
645
660
|
async generate() {
|
|
646
661
|
const content = this.generateContent();
|
|
647
|
-
const outputPath =
|
|
648
|
-
await
|
|
649
|
-
await
|
|
662
|
+
const outputPath = path4.join(this.context.config.outputDir, "queries.ts");
|
|
663
|
+
await fs3.mkdir(path4.dirname(outputPath), { recursive: true });
|
|
664
|
+
await fs3.writeFile(outputPath, content, "utf-8");
|
|
650
665
|
}
|
|
651
666
|
generateContent() {
|
|
652
667
|
const imports = `import { cache } from 'react';
|
|
@@ -700,17 +715,21 @@ export const ${queryName} = cache(async (
|
|
|
700
715
|
});`;
|
|
701
716
|
}
|
|
702
717
|
};
|
|
718
|
+
|
|
719
|
+
// src/generators/types.ts
|
|
720
|
+
import fs4 from "fs/promises";
|
|
721
|
+
import path5 from "path";
|
|
703
722
|
var TypesGenerator = class extends BaseGenerator {
|
|
704
723
|
async generate() {
|
|
705
724
|
const content = this.generateContent();
|
|
706
|
-
const outputPath =
|
|
707
|
-
await
|
|
708
|
-
await
|
|
725
|
+
const outputPath = path5.join(this.context.config.outputDir, "types.ts");
|
|
726
|
+
await fs4.mkdir(path5.dirname(outputPath), { recursive: true });
|
|
727
|
+
await fs4.writeFile(outputPath, content, "utf-8");
|
|
709
728
|
}
|
|
710
729
|
generateContent() {
|
|
711
|
-
const outputPath =
|
|
712
|
-
const endpointsPath =
|
|
713
|
-
const relativePath =
|
|
730
|
+
const outputPath = path5.join(this.context.config.outputDir, "types.ts");
|
|
731
|
+
const endpointsPath = path5.join(this.context.config.endpointsPath);
|
|
732
|
+
const relativePath = path5.relative(path5.dirname(outputPath), endpointsPath).replace(/\\/g, "/");
|
|
714
733
|
return `// Auto-generated type definitions
|
|
715
734
|
// Do not edit this file manually
|
|
716
735
|
|
|
@@ -719,7 +738,7 @@ import { apiConfig } from '${relativePath}';
|
|
|
719
738
|
|
|
720
739
|
|
|
721
740
|
// Re-export endpoint configuration types
|
|
722
|
-
export type { APIConfig, APIEndpoint, HTTPMethod } from '
|
|
741
|
+
export type { APIConfig, APIEndpoint, HTTPMethod } from './schema';
|
|
723
742
|
|
|
724
743
|
/**
|
|
725
744
|
* Type helper to extract params schema from an endpoint
|
|
@@ -784,6 +803,10 @@ ${this.generateEndpointTypes()}
|
|
|
784
803
|
return types.join("\n");
|
|
785
804
|
}
|
|
786
805
|
};
|
|
806
|
+
|
|
807
|
+
// src/generators/client.ts
|
|
808
|
+
import fs5 from "fs/promises";
|
|
809
|
+
import path6 from "path";
|
|
787
810
|
var ClientGenerator = class extends BaseGenerator {
|
|
788
811
|
async generate() {
|
|
789
812
|
await this.generateClientFile();
|
|
@@ -812,8 +835,8 @@ var ClientGenerator = class extends BaseGenerator {
|
|
|
812
835
|
const endpointsPath = path6.join(this.context.config.endpointsPath);
|
|
813
836
|
const relativePath = path6.relative(path6.dirname(outputPath), endpointsPath).replace(/\\/g, "/");
|
|
814
837
|
return `${useClientDirective ? "'use client';\n" : ""}
|
|
815
|
-
import { createAPIClient } from '
|
|
816
|
-
import type { AuthCallbacks } from '
|
|
838
|
+
import { createAPIClient } from './core';
|
|
839
|
+
import type { AuthCallbacks } from './core';
|
|
817
840
|
import { apiConfig } from '${relativePath}';
|
|
818
841
|
import { z } from 'zod';
|
|
819
842
|
|
|
@@ -881,7 +904,7 @@ export type { AuthCallbacks };
|
|
|
881
904
|
`;
|
|
882
905
|
}
|
|
883
906
|
generateServerClientContent() {
|
|
884
|
-
return `import { createAPIClient } from '
|
|
907
|
+
return `import { createAPIClient } from './core';
|
|
885
908
|
import { apiConfig } from '../config/endpoints';
|
|
886
909
|
import type { APIEndpoints } from './types';
|
|
887
910
|
|
|
@@ -969,20 +992,24 @@ export const serverClient = createAPIClient(apiConfig) as APIClientMethods;
|
|
|
969
992
|
return methods.join("\n");
|
|
970
993
|
}
|
|
971
994
|
};
|
|
995
|
+
|
|
996
|
+
// src/generators/query-keys.ts
|
|
997
|
+
import fs6 from "fs/promises";
|
|
998
|
+
import path7 from "path";
|
|
972
999
|
var QueryKeysGenerator = class extends BaseGenerator {
|
|
973
1000
|
async generate() {
|
|
974
1001
|
const content = this.generateContent();
|
|
975
|
-
const outputPath =
|
|
1002
|
+
const outputPath = path7.join(
|
|
976
1003
|
this.context.config.outputDir,
|
|
977
1004
|
"query-keys.ts"
|
|
978
1005
|
);
|
|
979
|
-
await
|
|
980
|
-
await
|
|
1006
|
+
await fs6.mkdir(path7.dirname(outputPath), { recursive: true });
|
|
1007
|
+
await fs6.writeFile(outputPath, content, "utf-8");
|
|
981
1008
|
}
|
|
982
1009
|
generateContent() {
|
|
983
|
-
const outputPath =
|
|
984
|
-
const endpointsPath =
|
|
985
|
-
const relativePath =
|
|
1010
|
+
const outputPath = path7.join(this.context.config.outputDir, "types.ts");
|
|
1011
|
+
const endpointsPath = path7.join(this.context.config.endpointsPath);
|
|
1012
|
+
const relativePath = path7.relative(path7.dirname(outputPath), endpointsPath).replace(/\\/g, "/");
|
|
986
1013
|
const content = `// Auto-generated query keys
|
|
987
1014
|
import { z } from 'zod';
|
|
988
1015
|
import { apiConfig } from '${relativePath}';
|
|
@@ -1032,20 +1059,24 @@ ${resourceKeys.join("\n")}
|
|
|
1032
1059
|
return keys.join("\n");
|
|
1033
1060
|
}
|
|
1034
1061
|
};
|
|
1062
|
+
|
|
1063
|
+
// src/generators/query-options.ts
|
|
1064
|
+
import fs7 from "fs/promises";
|
|
1065
|
+
import path8 from "path";
|
|
1035
1066
|
var QueryOptionsGenerator = class extends BaseGenerator {
|
|
1036
1067
|
async generate() {
|
|
1037
1068
|
const content = this.generateContent();
|
|
1038
|
-
const outputPath =
|
|
1069
|
+
const outputPath = path8.join(
|
|
1039
1070
|
this.context.config.outputDir,
|
|
1040
1071
|
"query-options.ts"
|
|
1041
1072
|
);
|
|
1042
|
-
await
|
|
1043
|
-
await
|
|
1073
|
+
await fs7.mkdir(path8.dirname(outputPath), { recursive: true });
|
|
1074
|
+
await fs7.writeFile(outputPath, content, "utf-8");
|
|
1044
1075
|
}
|
|
1045
1076
|
generateContent() {
|
|
1046
|
-
const outputPath =
|
|
1047
|
-
const endpointsPath =
|
|
1048
|
-
const relativePath =
|
|
1077
|
+
const outputPath = path8.join(this.context.config.outputDir, "types.ts");
|
|
1078
|
+
const endpointsPath = path8.join(this.context.config.endpointsPath);
|
|
1079
|
+
const relativePath = path8.relative(path8.dirname(outputPath), endpointsPath).replace(/\\/g, "/");
|
|
1049
1080
|
const content = `// Auto-generated query options
|
|
1050
1081
|
import { queryOptions } from '@tanstack/react-query';
|
|
1051
1082
|
import { apiClient } from './client';
|
|
@@ -1114,22 +1145,26 @@ ${resourceOptions.join("\n")}
|
|
|
1114
1145
|
}
|
|
1115
1146
|
generateQueryOptionsExports() {
|
|
1116
1147
|
const groups = this.groupEndpointsByResource();
|
|
1117
|
-
const exports
|
|
1148
|
+
const exports = [];
|
|
1118
1149
|
Object.keys(groups).forEach((resource) => {
|
|
1119
1150
|
const hasQueries = groups[resource].some(
|
|
1120
1151
|
({ endpoint }) => endpoint.method === "GET"
|
|
1121
1152
|
);
|
|
1122
|
-
if (hasQueries) exports
|
|
1153
|
+
if (hasQueries) exports.push(` ${resource}: ${resource}QueryOptions,`);
|
|
1123
1154
|
});
|
|
1124
|
-
return exports
|
|
1155
|
+
return exports.join("\n");
|
|
1125
1156
|
}
|
|
1126
1157
|
};
|
|
1158
|
+
|
|
1159
|
+
// src/generators/prefetch.ts
|
|
1160
|
+
import fs8 from "fs/promises";
|
|
1161
|
+
import path9 from "path";
|
|
1127
1162
|
var PrefetchGenerator = class extends BaseGenerator {
|
|
1128
1163
|
async generate() {
|
|
1129
1164
|
const content = this.generateContent();
|
|
1130
|
-
const outputPath =
|
|
1131
|
-
await
|
|
1132
|
-
await
|
|
1165
|
+
const outputPath = path9.join(this.context.config.outputDir, "prefetchs.ts");
|
|
1166
|
+
await fs8.mkdir(path9.dirname(outputPath), { recursive: true });
|
|
1167
|
+
await fs8.writeFile(outputPath, content, "utf-8");
|
|
1133
1168
|
}
|
|
1134
1169
|
generateContent() {
|
|
1135
1170
|
const content = `// Auto-generated prefetch utilities
|
|
@@ -1212,6 +1247,9 @@ var CodeGenerator = class {
|
|
|
1212
1247
|
return generators;
|
|
1213
1248
|
}
|
|
1214
1249
|
};
|
|
1250
|
+
|
|
1251
|
+
// src/core/codegen.ts
|
|
1252
|
+
import { fileURLToPath } from "url";
|
|
1215
1253
|
var CodegenCore = class {
|
|
1216
1254
|
constructor(config) {
|
|
1217
1255
|
this.config = config;
|
|
@@ -1245,7 +1283,16 @@ var CodegenCore = class {
|
|
|
1245
1283
|
}
|
|
1246
1284
|
}
|
|
1247
1285
|
};
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1286
|
+
export {
|
|
1287
|
+
APIError,
|
|
1288
|
+
AuthError,
|
|
1289
|
+
CodeGenerator,
|
|
1290
|
+
CodegenCore,
|
|
1291
|
+
createAPIClient,
|
|
1292
|
+
defineConfig,
|
|
1293
|
+
defineEndpoint,
|
|
1294
|
+
defineEndpoints,
|
|
1295
|
+
loadConfig,
|
|
1296
|
+
validateConfig
|
|
1297
|
+
};
|
|
1251
1298
|
//# sourceMappingURL=index.js.map
|