@icib.dev/api-client 1.1.2 → 1.1.3
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/README.md +5 -1
- package/dist/scripts/generate.js +40 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -61,9 +61,13 @@ BASE_PATH=/v2 npx api-client-generate
|
|
|
61
61
|
# Custom client base URL (default: from spec URL, BASE_URL, or spec host)
|
|
62
62
|
npx api-client-generate --base-url https://api.mycompany.com
|
|
63
63
|
BASE_URL=https://api.mycompany.com npx api-client-generate
|
|
64
|
+
|
|
65
|
+
# Override client.ts (by default, existing client is preserved if you customized it)
|
|
66
|
+
npx api-client-generate --override-client # prompts for confirmation
|
|
67
|
+
npx api-client-generate --override-client --yes # skip confirmation (e.g. CI)
|
|
64
68
|
```
|
|
65
69
|
|
|
66
|
-
The client is generated in your project directory (e.g. `./api/`).
|
|
70
|
+
The client is generated in your project directory (e.g. `./api/`). If `client.ts` already exists, it is **not** overwritten unless you pass `--override-client` (which prompts for confirmation; use `--yes` to skip the prompt).
|
|
67
71
|
|
|
68
72
|
### From the library repo (maintainers)
|
|
69
73
|
|
package/dist/scripts/generate.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { readFileSync, mkdirSync, writeFileSync } from "fs";
|
|
2
|
+
import { readFileSync, mkdirSync, writeFileSync, existsSync } from "fs";
|
|
3
|
+
import { createInterface } from "readline";
|
|
3
4
|
import { dirname, join } from "path";
|
|
4
5
|
import { fileURLToPath } from "url";
|
|
5
6
|
import SwaggerParser from "@apidevtools/swagger-parser";
|
|
@@ -20,6 +21,8 @@ function parseArgs() {
|
|
|
20
21
|
let out = DEFAULT_OUT;
|
|
21
22
|
let basePath = process.env.BASE_PATH;
|
|
22
23
|
let baseUrl = process.env.BASE_URL;
|
|
24
|
+
let overrideClient = false;
|
|
25
|
+
let yes = false;
|
|
23
26
|
for (let i = 0; i < args.length; i++) {
|
|
24
27
|
if (args[i] === "--url" && args[i + 1]) {
|
|
25
28
|
url = args[++i];
|
|
@@ -35,8 +38,24 @@ function parseArgs() {
|
|
|
35
38
|
args[i + 1]) {
|
|
36
39
|
baseUrl = args[++i];
|
|
37
40
|
}
|
|
41
|
+
else if (args[i] === "--override-client" || args[i] === "--overwrite-client") {
|
|
42
|
+
overrideClient = true;
|
|
43
|
+
}
|
|
44
|
+
else if (args[i] === "--yes" || args[i] === "-y") {
|
|
45
|
+
yes = true;
|
|
46
|
+
}
|
|
38
47
|
}
|
|
39
|
-
return { url, out, basePath, baseUrl };
|
|
48
|
+
return { url, out, basePath, baseUrl, overrideClient, yes };
|
|
49
|
+
}
|
|
50
|
+
function askConfirmation(question) {
|
|
51
|
+
return new Promise((resolve) => {
|
|
52
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
53
|
+
rl.question(question, (answer) => {
|
|
54
|
+
rl.close();
|
|
55
|
+
const normalized = answer.trim().toLowerCase();
|
|
56
|
+
resolve(normalized === "y" || normalized === "yes");
|
|
57
|
+
});
|
|
58
|
+
});
|
|
40
59
|
}
|
|
41
60
|
async function fetchSpec(url) {
|
|
42
61
|
const res = await fetch(url);
|
|
@@ -720,7 +739,7 @@ function generateIndex(contextTags) {
|
|
|
720
739
|
return exports.join("\n");
|
|
721
740
|
}
|
|
722
741
|
async function main() {
|
|
723
|
-
const { url, out, basePath: basePathOverride, baseUrl: baseUrlOverride, } = parseArgs();
|
|
742
|
+
const { url, out, basePath: basePathOverride, baseUrl: baseUrlOverride, overrideClient, yes, } = parseArgs();
|
|
724
743
|
console.log(`Fetching spec from ${url}...`);
|
|
725
744
|
const rawSpec = await loadRawSpec(url);
|
|
726
745
|
const doc = await parseSpec(rawSpec);
|
|
@@ -757,7 +776,23 @@ async function main() {
|
|
|
757
776
|
? `${baseUrl.replace(/\/$/, "")}${basePath}`
|
|
758
777
|
: baseUrl;
|
|
759
778
|
writeFileSync(join(typesDir, "index.ts"), generateTypes(definitions));
|
|
760
|
-
|
|
779
|
+
const clientPath = join(outDir, "client.ts");
|
|
780
|
+
const clientExists = existsSync(clientPath);
|
|
781
|
+
let writeClient = !clientExists;
|
|
782
|
+
if (clientExists) {
|
|
783
|
+
if (overrideClient) {
|
|
784
|
+
if (yes) {
|
|
785
|
+
writeClient = true;
|
|
786
|
+
}
|
|
787
|
+
else {
|
|
788
|
+
const confirmed = await askConfirmation("This will overwrite your client.ts. Are you sure? (y/N) ");
|
|
789
|
+
writeClient = confirmed;
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
if (writeClient) {
|
|
794
|
+
writeFileSync(clientPath, generateClient(clientBaseUrl));
|
|
795
|
+
}
|
|
761
796
|
const sortedTags = [...byTag.keys()].sort();
|
|
762
797
|
for (const tag of sortedTags) {
|
|
763
798
|
const ctxName = sanitizeContextName(tag);
|
|
@@ -780,7 +815,7 @@ async function main() {
|
|
|
780
815
|
writeFileSync(manifestPath, JSON.stringify(manifest, null, 2));
|
|
781
816
|
console.log(`Generated API client in ${outDir}`);
|
|
782
817
|
console.log(` - types/index.ts`);
|
|
783
|
-
console.log(` - client.ts`);
|
|
818
|
+
console.log(` - client.ts${writeClient ? "" : " (skipped, use --override-client to overwrite)"}`);
|
|
784
819
|
console.log(` - apiClient.ts`);
|
|
785
820
|
console.log(` - contexts/*.ts (${sortedTags.length} files)`);
|
|
786
821
|
console.log(` - index.ts`);
|