@icib.dev/api-client 1.0.3 → 1.0.4
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 +4 -0
- package/dist/scripts/generate.js +16 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -41,6 +41,10 @@ npx api-client-generate --url https://api.example.com/docs/openapi --out api
|
|
|
41
41
|
|
|
42
42
|
# Using BASE_URL env (default: $BASE_URL/docs/openapi)
|
|
43
43
|
BASE_URL=https://api.example.com npx api-client-generate --out api
|
|
44
|
+
|
|
45
|
+
# Custom base path (overrides spec; default from spec or empty)
|
|
46
|
+
npx api-client-generate --base-path /v1/api
|
|
47
|
+
BASE_PATH=/v2 npx api-client-generate
|
|
44
48
|
```
|
|
45
49
|
|
|
46
50
|
The client is generated in your project directory (e.g. `./api/`).
|
package/dist/scripts/generate.js
CHANGED
|
@@ -3,14 +3,14 @@ import { readFileSync, mkdirSync, writeFileSync } from "fs";
|
|
|
3
3
|
import { dirname, join } from "path";
|
|
4
4
|
import { fileURLToPath } from "url";
|
|
5
5
|
import SwaggerParser from "@apidevtools/swagger-parser";
|
|
6
|
-
import { normalizedJsonHash, computeClientHash
|
|
6
|
+
import { normalizedJsonHash, computeClientHash } from "./hash.js";
|
|
7
7
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
8
8
|
const DEFAULT_OUT = "api";
|
|
9
9
|
function getDefaultUrl() {
|
|
10
10
|
const base = process.env.BASE_URL;
|
|
11
11
|
if (base) {
|
|
12
12
|
const normalized = base.replace(/\/$/, "");
|
|
13
|
-
return `${normalized}/docs/
|
|
13
|
+
return `${normalized}/docs/json`;
|
|
14
14
|
}
|
|
15
15
|
return "https://api.icib.dev/docs/?format=openapi";
|
|
16
16
|
}
|
|
@@ -18,6 +18,7 @@ function parseArgs() {
|
|
|
18
18
|
const args = process.argv.slice(2);
|
|
19
19
|
let url = getDefaultUrl();
|
|
20
20
|
let out = DEFAULT_OUT;
|
|
21
|
+
let basePath = process.env.BASE_PATH;
|
|
21
22
|
for (let i = 0; i < args.length; i++) {
|
|
22
23
|
if (args[i] === "--url" && args[i + 1]) {
|
|
23
24
|
url = args[++i];
|
|
@@ -25,8 +26,11 @@ function parseArgs() {
|
|
|
25
26
|
else if (args[i] === "--out" && args[i + 1]) {
|
|
26
27
|
out = args[++i];
|
|
27
28
|
}
|
|
29
|
+
else if ((args[i] === "--base-path" || args[i] === "--basePath") && args[i + 1]) {
|
|
30
|
+
basePath = args[++i];
|
|
31
|
+
}
|
|
28
32
|
}
|
|
29
|
-
return { url, out };
|
|
33
|
+
return { url, out, basePath };
|
|
30
34
|
}
|
|
31
35
|
async function fetchSpec(url) {
|
|
32
36
|
const res = await fetch(url);
|
|
@@ -68,7 +72,7 @@ function getOrigin(doc) {
|
|
|
68
72
|
}
|
|
69
73
|
function getBasePath(doc) {
|
|
70
74
|
const oas2 = doc;
|
|
71
|
-
return oas2.basePath ?? "
|
|
75
|
+
return oas2.basePath ?? "";
|
|
72
76
|
}
|
|
73
77
|
function getDefinitions(doc) {
|
|
74
78
|
return doc.definitions ?? doc.components?.schemas ?? {};
|
|
@@ -615,15 +619,21 @@ function generateIndex(contextTags) {
|
|
|
615
619
|
return exports.join("\n");
|
|
616
620
|
}
|
|
617
621
|
async function main() {
|
|
618
|
-
const { url, out } = parseArgs();
|
|
622
|
+
const { url, out, basePath: basePathOverride } = parseArgs();
|
|
619
623
|
console.log(`Fetching spec from ${url}...`);
|
|
620
624
|
const rawSpec = await loadRawSpec(url);
|
|
621
625
|
const doc = await parseSpec(rawSpec);
|
|
622
626
|
const baseUrl = getOrigin(doc);
|
|
623
|
-
const basePath =
|
|
627
|
+
const basePath = (() => {
|
|
628
|
+
const raw = basePathOverride ?? getBasePath(doc);
|
|
629
|
+
if (raw === "")
|
|
630
|
+
return "";
|
|
631
|
+
return raw.startsWith("/") ? raw : `/${raw}`;
|
|
632
|
+
})();
|
|
624
633
|
const definitions = getDefinitions(doc);
|
|
625
634
|
const paths = getPaths(doc);
|
|
626
635
|
console.log(`Base URL: ${baseUrl}`);
|
|
636
|
+
console.log(`Base path: ${basePath}`);
|
|
627
637
|
console.log(`Paths: ${Object.keys(paths).length}`);
|
|
628
638
|
console.log(`Definitions: ${Object.keys(definitions).length}`);
|
|
629
639
|
const ops = extractOperations(paths, basePath, definitions);
|