@forklaunch/universal-sdk 0.5.2 → 0.5.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/lib/index.d.mts +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js +32 -14
- package/lib/index.mjs +32 -14
- package/package.json +6 -6
package/lib/index.d.mts
CHANGED
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -170,11 +170,16 @@ function mapContentType(contentType) {
|
|
|
170
170
|
}
|
|
171
171
|
}
|
|
172
172
|
|
|
173
|
+
// src/guards/isOpenApiObject.ts
|
|
174
|
+
function isOpenAPIObject(obj) {
|
|
175
|
+
return typeof obj === "object" && obj !== null && "openapi" in obj;
|
|
176
|
+
}
|
|
177
|
+
|
|
173
178
|
// src/core/openApi.ts
|
|
174
179
|
function getSdkPathMap(registryOpenApiJson) {
|
|
175
180
|
const sdkPathMap = {};
|
|
176
|
-
Object.entries(registryOpenApiJson
|
|
177
|
-
([path, pathItem]) => {
|
|
181
|
+
Object.entries(registryOpenApiJson).forEach(([version, openApi]) => {
|
|
182
|
+
Object.entries(openApi?.paths || {}).forEach(([path, pathItem]) => {
|
|
178
183
|
const methods = [
|
|
179
184
|
"get",
|
|
180
185
|
"post",
|
|
@@ -185,16 +190,18 @@ function getSdkPathMap(registryOpenApiJson) {
|
|
|
185
190
|
"head",
|
|
186
191
|
"trace"
|
|
187
192
|
];
|
|
193
|
+
const versionedPath = version === "latest" ? void 0 : version.substring(1);
|
|
188
194
|
methods.forEach((method) => {
|
|
189
195
|
if (pathItem[method]?.operationId) {
|
|
190
|
-
sdkPathMap[pathItem[method].operationId] = {
|
|
196
|
+
sdkPathMap[`${pathItem[method].operationId}${versionedPath ? `.${versionedPath}` : ""}`] = {
|
|
191
197
|
method,
|
|
192
|
-
path
|
|
198
|
+
path,
|
|
199
|
+
version
|
|
193
200
|
};
|
|
194
201
|
}
|
|
195
202
|
});
|
|
196
|
-
}
|
|
197
|
-
);
|
|
203
|
+
});
|
|
204
|
+
});
|
|
198
205
|
return sdkPathMap;
|
|
199
206
|
}
|
|
200
207
|
async function refreshOpenApi(host, registryOptions, existingRegistryOpenApiHash) {
|
|
@@ -204,11 +211,14 @@ async function refreshOpenApi(host, registryOptions, existingRegistryOpenApiHash
|
|
|
204
211
|
};
|
|
205
212
|
}
|
|
206
213
|
if ("raw" in registryOptions) {
|
|
214
|
+
const rawVersionedOpenApi = isOpenAPIObject(registryOptions.raw) ? {
|
|
215
|
+
latest: registryOptions.raw
|
|
216
|
+
} : registryOptions.raw;
|
|
207
217
|
return {
|
|
208
218
|
updateRequired: true,
|
|
209
|
-
registryOpenApiJson:
|
|
219
|
+
registryOpenApiJson: rawVersionedOpenApi,
|
|
210
220
|
registryOpenApiHash: "static",
|
|
211
|
-
sdkPathMap: getSdkPathMap(
|
|
221
|
+
sdkPathMap: getSdkPathMap(rawVersionedOpenApi)
|
|
212
222
|
};
|
|
213
223
|
}
|
|
214
224
|
const registry = "path" in registryOptions ? `${host}/${registryOptions.path}` : "url" in registryOptions ? registryOptions.url : null;
|
|
@@ -394,7 +404,12 @@ var UniversalSdk = class _UniversalSdk {
|
|
|
394
404
|
this.registryOpenApiHash = refreshResult.registryOpenApiHash;
|
|
395
405
|
this.sdkPathMap = refreshResult.sdkPathMap;
|
|
396
406
|
}
|
|
397
|
-
return this.execute(
|
|
407
|
+
return this.execute(
|
|
408
|
+
path,
|
|
409
|
+
request?.method ?? "get",
|
|
410
|
+
request?.version ? `v${request.version}` : "latest",
|
|
411
|
+
request
|
|
412
|
+
);
|
|
398
413
|
}
|
|
399
414
|
async executeSdkCall(sdkPath, request) {
|
|
400
415
|
if (!this.host) {
|
|
@@ -417,16 +432,16 @@ var UniversalSdk = class _UniversalSdk {
|
|
|
417
432
|
}
|
|
418
433
|
const fullSdkPath = sdkPath.split(".");
|
|
419
434
|
while (fullSdkPath.length > 0) {
|
|
420
|
-
fullSdkPath.shift();
|
|
421
435
|
if (fullSdkPath.join(".") in this.sdkPathMap) {
|
|
422
436
|
break;
|
|
423
437
|
}
|
|
438
|
+
fullSdkPath.shift();
|
|
424
439
|
}
|
|
425
440
|
if (fullSdkPath.length === 0) {
|
|
426
441
|
throw new Error(`Sdk path not found: ${sdkPath}`);
|
|
427
442
|
}
|
|
428
|
-
const { method, path } = this.sdkPathMap?.[fullSdkPath.join(".")] || {};
|
|
429
|
-
return this.execute(path, method, request);
|
|
443
|
+
const { method, path, version } = this.sdkPathMap?.[fullSdkPath.join(".")] || {};
|
|
444
|
+
return this.execute(path, method, version, request);
|
|
430
445
|
}
|
|
431
446
|
/**
|
|
432
447
|
* Executes an HTTP request.
|
|
@@ -436,7 +451,7 @@ var UniversalSdk = class _UniversalSdk {
|
|
|
436
451
|
* @param {RequestType} [request] - The request object.
|
|
437
452
|
* @returns {Promise<ResponseType>} - The response object.
|
|
438
453
|
*/
|
|
439
|
-
async execute(path, method, request) {
|
|
454
|
+
async execute(path, method, version, request) {
|
|
440
455
|
const { params, body, query, headers } = request || {};
|
|
441
456
|
let url = getSdkPath(this.host + path);
|
|
442
457
|
if (params) {
|
|
@@ -509,7 +524,7 @@ var UniversalSdk = class _UniversalSdk {
|
|
|
509
524
|
},
|
|
510
525
|
body: parsedBody
|
|
511
526
|
});
|
|
512
|
-
const responseOpenApi = path != null && method != null ? this.registryOpenApiJson?.paths?.[(0, import_common2.openApiCompliantPath)(path)]?.[method?.toLowerCase()]?.responses?.[response.status] : null;
|
|
527
|
+
const responseOpenApi = path != null && method != null ? this.registryOpenApiJson?.[version]?.paths?.[(0, import_common2.openApiCompliantPath)(path)]?.[method?.toLowerCase()]?.responses?.[response.status] : null;
|
|
513
528
|
if (responseOpenApi == null) {
|
|
514
529
|
throw new Error(
|
|
515
530
|
`Response ${response.status} not found in OpenAPI spec for ${path} with method ${method}`
|
|
@@ -671,6 +686,9 @@ var universalSdk = async (options) => {
|
|
|
671
686
|
if (prop === "then" || prop === "catch" || prop === "finally") {
|
|
672
687
|
return void 0;
|
|
673
688
|
}
|
|
689
|
+
if (prop === "fetch") {
|
|
690
|
+
return sdkInternal.executeFetchCall;
|
|
691
|
+
}
|
|
674
692
|
if (typeof prop === "string" && prop in sdkInternal) {
|
|
675
693
|
const value = sdkInternal[prop];
|
|
676
694
|
if (typeof value === "function") {
|
package/lib/index.mjs
CHANGED
|
@@ -138,11 +138,16 @@ function mapContentType(contentType) {
|
|
|
138
138
|
}
|
|
139
139
|
}
|
|
140
140
|
|
|
141
|
+
// src/guards/isOpenApiObject.ts
|
|
142
|
+
function isOpenAPIObject(obj) {
|
|
143
|
+
return typeof obj === "object" && obj !== null && "openapi" in obj;
|
|
144
|
+
}
|
|
145
|
+
|
|
141
146
|
// src/core/openApi.ts
|
|
142
147
|
function getSdkPathMap(registryOpenApiJson) {
|
|
143
148
|
const sdkPathMap = {};
|
|
144
|
-
Object.entries(registryOpenApiJson
|
|
145
|
-
([path, pathItem]) => {
|
|
149
|
+
Object.entries(registryOpenApiJson).forEach(([version, openApi]) => {
|
|
150
|
+
Object.entries(openApi?.paths || {}).forEach(([path, pathItem]) => {
|
|
146
151
|
const methods = [
|
|
147
152
|
"get",
|
|
148
153
|
"post",
|
|
@@ -153,16 +158,18 @@ function getSdkPathMap(registryOpenApiJson) {
|
|
|
153
158
|
"head",
|
|
154
159
|
"trace"
|
|
155
160
|
];
|
|
161
|
+
const versionedPath = version === "latest" ? void 0 : version.substring(1);
|
|
156
162
|
methods.forEach((method) => {
|
|
157
163
|
if (pathItem[method]?.operationId) {
|
|
158
|
-
sdkPathMap[pathItem[method].operationId] = {
|
|
164
|
+
sdkPathMap[`${pathItem[method].operationId}${versionedPath ? `.${versionedPath}` : ""}`] = {
|
|
159
165
|
method,
|
|
160
|
-
path
|
|
166
|
+
path,
|
|
167
|
+
version
|
|
161
168
|
};
|
|
162
169
|
}
|
|
163
170
|
});
|
|
164
|
-
}
|
|
165
|
-
);
|
|
171
|
+
});
|
|
172
|
+
});
|
|
166
173
|
return sdkPathMap;
|
|
167
174
|
}
|
|
168
175
|
async function refreshOpenApi(host, registryOptions, existingRegistryOpenApiHash) {
|
|
@@ -172,11 +179,14 @@ async function refreshOpenApi(host, registryOptions, existingRegistryOpenApiHash
|
|
|
172
179
|
};
|
|
173
180
|
}
|
|
174
181
|
if ("raw" in registryOptions) {
|
|
182
|
+
const rawVersionedOpenApi = isOpenAPIObject(registryOptions.raw) ? {
|
|
183
|
+
latest: registryOptions.raw
|
|
184
|
+
} : registryOptions.raw;
|
|
175
185
|
return {
|
|
176
186
|
updateRequired: true,
|
|
177
|
-
registryOpenApiJson:
|
|
187
|
+
registryOpenApiJson: rawVersionedOpenApi,
|
|
178
188
|
registryOpenApiHash: "static",
|
|
179
|
-
sdkPathMap: getSdkPathMap(
|
|
189
|
+
sdkPathMap: getSdkPathMap(rawVersionedOpenApi)
|
|
180
190
|
};
|
|
181
191
|
}
|
|
182
192
|
const registry = "path" in registryOptions ? `${host}/${registryOptions.path}` : "url" in registryOptions ? registryOptions.url : null;
|
|
@@ -362,7 +372,12 @@ var UniversalSdk = class _UniversalSdk {
|
|
|
362
372
|
this.registryOpenApiHash = refreshResult.registryOpenApiHash;
|
|
363
373
|
this.sdkPathMap = refreshResult.sdkPathMap;
|
|
364
374
|
}
|
|
365
|
-
return this.execute(
|
|
375
|
+
return this.execute(
|
|
376
|
+
path,
|
|
377
|
+
request?.method ?? "get",
|
|
378
|
+
request?.version ? `v${request.version}` : "latest",
|
|
379
|
+
request
|
|
380
|
+
);
|
|
366
381
|
}
|
|
367
382
|
async executeSdkCall(sdkPath, request) {
|
|
368
383
|
if (!this.host) {
|
|
@@ -385,16 +400,16 @@ var UniversalSdk = class _UniversalSdk {
|
|
|
385
400
|
}
|
|
386
401
|
const fullSdkPath = sdkPath.split(".");
|
|
387
402
|
while (fullSdkPath.length > 0) {
|
|
388
|
-
fullSdkPath.shift();
|
|
389
403
|
if (fullSdkPath.join(".") in this.sdkPathMap) {
|
|
390
404
|
break;
|
|
391
405
|
}
|
|
406
|
+
fullSdkPath.shift();
|
|
392
407
|
}
|
|
393
408
|
if (fullSdkPath.length === 0) {
|
|
394
409
|
throw new Error(`Sdk path not found: ${sdkPath}`);
|
|
395
410
|
}
|
|
396
|
-
const { method, path } = this.sdkPathMap?.[fullSdkPath.join(".")] || {};
|
|
397
|
-
return this.execute(path, method, request);
|
|
411
|
+
const { method, path, version } = this.sdkPathMap?.[fullSdkPath.join(".")] || {};
|
|
412
|
+
return this.execute(path, method, version, request);
|
|
398
413
|
}
|
|
399
414
|
/**
|
|
400
415
|
* Executes an HTTP request.
|
|
@@ -404,7 +419,7 @@ var UniversalSdk = class _UniversalSdk {
|
|
|
404
419
|
* @param {RequestType} [request] - The request object.
|
|
405
420
|
* @returns {Promise<ResponseType>} - The response object.
|
|
406
421
|
*/
|
|
407
|
-
async execute(path, method, request) {
|
|
422
|
+
async execute(path, method, version, request) {
|
|
408
423
|
const { params, body, query, headers } = request || {};
|
|
409
424
|
let url = getSdkPath(this.host + path);
|
|
410
425
|
if (params) {
|
|
@@ -477,7 +492,7 @@ var UniversalSdk = class _UniversalSdk {
|
|
|
477
492
|
},
|
|
478
493
|
body: parsedBody
|
|
479
494
|
});
|
|
480
|
-
const responseOpenApi = path != null && method != null ? this.registryOpenApiJson?.paths?.[openApiCompliantPath(path)]?.[method?.toLowerCase()]?.responses?.[response.status] : null;
|
|
495
|
+
const responseOpenApi = path != null && method != null ? this.registryOpenApiJson?.[version]?.paths?.[openApiCompliantPath(path)]?.[method?.toLowerCase()]?.responses?.[response.status] : null;
|
|
481
496
|
if (responseOpenApi == null) {
|
|
482
497
|
throw new Error(
|
|
483
498
|
`Response ${response.status} not found in OpenAPI spec for ${path} with method ${method}`
|
|
@@ -639,6 +654,9 @@ var universalSdk = async (options) => {
|
|
|
639
654
|
if (prop === "then" || prop === "catch" || prop === "finally") {
|
|
640
655
|
return void 0;
|
|
641
656
|
}
|
|
657
|
+
if (prop === "fetch") {
|
|
658
|
+
return sdkInternal.executeFetchCall;
|
|
659
|
+
}
|
|
642
660
|
if (typeof prop === "string" && prop in sdkInternal) {
|
|
643
661
|
const value = sdkInternal[prop];
|
|
644
662
|
if (typeof value === "function") {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@forklaunch/universal-sdk",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.4",
|
|
4
4
|
"description": "Cross runtime fetch library for forklaunch router sdks",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"fetch",
|
|
@@ -33,18 +33,18 @@
|
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"ajv": "^8.17.1",
|
|
35
35
|
"ajv-formats": "^3.0.1",
|
|
36
|
-
"@forklaunch/common": "0.4.
|
|
36
|
+
"@forklaunch/common": "0.4.6"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@types/node": "^24.0
|
|
40
|
-
"@typescript/native-preview": "7.0.0-dev.
|
|
39
|
+
"@types/node": "^24.1.0",
|
|
40
|
+
"@typescript/native-preview": "7.0.0-dev.20250802.1",
|
|
41
41
|
"fetch-mock": "^12.5.3",
|
|
42
|
-
"jest": "^30.0.
|
|
42
|
+
"jest": "^30.0.5",
|
|
43
43
|
"openapi3-ts": "^4.5.0",
|
|
44
44
|
"prettier": "^3.6.2",
|
|
45
45
|
"ts-jest": "^29.4.0",
|
|
46
46
|
"tsup": "^8.5.0",
|
|
47
|
-
"typedoc": "^0.28.
|
|
47
|
+
"typedoc": "^0.28.9"
|
|
48
48
|
},
|
|
49
49
|
"scripts": {
|
|
50
50
|
"build": "tsgo --noEmit && tsup index.ts --format cjs,esm --no-splitting --dts --tsconfig tsconfig.json --out-dir lib --clean",
|