s3db.js 12.2.0 → 12.2.1
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/s3db.cjs.js +6 -1966
- package/dist/s3db.cjs.js.map +1 -1
- package/dist/s3db.es.js +6 -1966
- package/dist/s3db.es.js.map +1 -1
- package/package.json +1 -1
- package/src/plugins/api/index.js +5 -1
- package/src/plugins/index.js +2 -1
package/dist/s3db.cjs.js
CHANGED
|
@@ -5,9 +5,6 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
5
5
|
var crypto$1 = require('crypto');
|
|
6
6
|
var nanoid = require('nanoid');
|
|
7
7
|
var EventEmitter = require('events');
|
|
8
|
-
var hono = require('hono');
|
|
9
|
-
var nodeServer = require('@hono/node-server');
|
|
10
|
-
var swaggerUi = require('@hono/swagger-ui');
|
|
11
8
|
var promises = require('fs/promises');
|
|
12
9
|
var fs = require('fs');
|
|
13
10
|
var promises$1 = require('stream/promises');
|
|
@@ -2474,1968 +2471,6 @@ const PluginObject = {
|
|
|
2474
2471
|
}
|
|
2475
2472
|
};
|
|
2476
2473
|
|
|
2477
|
-
function success(data, options = {}) {
|
|
2478
|
-
const { status = 200, meta = {} } = options;
|
|
2479
|
-
return {
|
|
2480
|
-
success: true,
|
|
2481
|
-
data,
|
|
2482
|
-
meta: {
|
|
2483
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
2484
|
-
...meta
|
|
2485
|
-
},
|
|
2486
|
-
_status: status
|
|
2487
|
-
};
|
|
2488
|
-
}
|
|
2489
|
-
function error(error2, options = {}) {
|
|
2490
|
-
const { status = 500, code = "INTERNAL_ERROR", details = {} } = options;
|
|
2491
|
-
const errorMessage = error2 instanceof Error ? error2.message : error2;
|
|
2492
|
-
const errorStack = error2 instanceof Error && process.env.NODE_ENV !== "production" ? error2.stack : void 0;
|
|
2493
|
-
return {
|
|
2494
|
-
success: false,
|
|
2495
|
-
error: {
|
|
2496
|
-
message: errorMessage,
|
|
2497
|
-
code,
|
|
2498
|
-
details,
|
|
2499
|
-
stack: errorStack
|
|
2500
|
-
},
|
|
2501
|
-
meta: {
|
|
2502
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
2503
|
-
},
|
|
2504
|
-
_status: status
|
|
2505
|
-
};
|
|
2506
|
-
}
|
|
2507
|
-
function list(items, pagination = {}) {
|
|
2508
|
-
const { total, page, pageSize, pageCount } = pagination;
|
|
2509
|
-
return {
|
|
2510
|
-
success: true,
|
|
2511
|
-
data: items,
|
|
2512
|
-
pagination: {
|
|
2513
|
-
total: total || items.length,
|
|
2514
|
-
page: page || 1,
|
|
2515
|
-
pageSize: pageSize || items.length,
|
|
2516
|
-
pageCount: pageCount || 1
|
|
2517
|
-
},
|
|
2518
|
-
meta: {
|
|
2519
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
2520
|
-
},
|
|
2521
|
-
_status: 200
|
|
2522
|
-
};
|
|
2523
|
-
}
|
|
2524
|
-
function created(data, location) {
|
|
2525
|
-
return {
|
|
2526
|
-
success: true,
|
|
2527
|
-
data,
|
|
2528
|
-
meta: {
|
|
2529
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
2530
|
-
location
|
|
2531
|
-
},
|
|
2532
|
-
_status: 201
|
|
2533
|
-
};
|
|
2534
|
-
}
|
|
2535
|
-
function noContent() {
|
|
2536
|
-
return {
|
|
2537
|
-
success: true,
|
|
2538
|
-
data: null,
|
|
2539
|
-
meta: {
|
|
2540
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
2541
|
-
},
|
|
2542
|
-
_status: 204
|
|
2543
|
-
};
|
|
2544
|
-
}
|
|
2545
|
-
function notFound(resource, id) {
|
|
2546
|
-
return error(`${resource} with id '${id}' not found`, {
|
|
2547
|
-
status: 404,
|
|
2548
|
-
code: "NOT_FOUND",
|
|
2549
|
-
details: { resource, id }
|
|
2550
|
-
});
|
|
2551
|
-
}
|
|
2552
|
-
function payloadTooLarge(size, limit) {
|
|
2553
|
-
return error("Request payload too large", {
|
|
2554
|
-
status: 413,
|
|
2555
|
-
code: "PAYLOAD_TOO_LARGE",
|
|
2556
|
-
details: {
|
|
2557
|
-
receivedSize: size,
|
|
2558
|
-
maxSize: limit,
|
|
2559
|
-
receivedMB: (size / 1024 / 1024).toFixed(2),
|
|
2560
|
-
maxMB: (limit / 1024 / 1024).toFixed(2)
|
|
2561
|
-
}
|
|
2562
|
-
});
|
|
2563
|
-
}
|
|
2564
|
-
|
|
2565
|
-
const errorStatusMap = {
|
|
2566
|
-
"ValidationError": 400,
|
|
2567
|
-
"InvalidResourceItem": 400,
|
|
2568
|
-
"ResourceNotFound": 404,
|
|
2569
|
-
"NoSuchKey": 404,
|
|
2570
|
-
"NoSuchBucket": 404,
|
|
2571
|
-
"PartitionError": 400,
|
|
2572
|
-
"CryptoError": 500,
|
|
2573
|
-
"SchemaError": 400,
|
|
2574
|
-
"QueueError": 500,
|
|
2575
|
-
"ResourceError": 500
|
|
2576
|
-
};
|
|
2577
|
-
function getStatusFromError(err) {
|
|
2578
|
-
if (err.name && errorStatusMap[err.name]) {
|
|
2579
|
-
return errorStatusMap[err.name];
|
|
2580
|
-
}
|
|
2581
|
-
if (err.constructor && err.constructor.name && errorStatusMap[err.constructor.name]) {
|
|
2582
|
-
return errorStatusMap[err.constructor.name];
|
|
2583
|
-
}
|
|
2584
|
-
if (err.message) {
|
|
2585
|
-
if (err.message.includes("not found") || err.message.includes("does not exist")) {
|
|
2586
|
-
return 404;
|
|
2587
|
-
}
|
|
2588
|
-
if (err.message.includes("validation") || err.message.includes("invalid")) {
|
|
2589
|
-
return 400;
|
|
2590
|
-
}
|
|
2591
|
-
if (err.message.includes("unauthorized") || err.message.includes("authentication")) {
|
|
2592
|
-
return 401;
|
|
2593
|
-
}
|
|
2594
|
-
if (err.message.includes("forbidden") || err.message.includes("permission")) {
|
|
2595
|
-
return 403;
|
|
2596
|
-
}
|
|
2597
|
-
}
|
|
2598
|
-
return 500;
|
|
2599
|
-
}
|
|
2600
|
-
function errorHandler(err, c) {
|
|
2601
|
-
const status = getStatusFromError(err);
|
|
2602
|
-
const code = err.name || "INTERNAL_ERROR";
|
|
2603
|
-
const details = {};
|
|
2604
|
-
if (err.resource) details.resource = err.resource;
|
|
2605
|
-
if (err.bucket) details.bucket = err.bucket;
|
|
2606
|
-
if (err.key) details.key = err.key;
|
|
2607
|
-
if (err.operation) details.operation = err.operation;
|
|
2608
|
-
if (err.suggestion) details.suggestion = err.suggestion;
|
|
2609
|
-
if (err.availableResources) details.availableResources = err.availableResources;
|
|
2610
|
-
const response = error(err, {
|
|
2611
|
-
status,
|
|
2612
|
-
code,
|
|
2613
|
-
details
|
|
2614
|
-
});
|
|
2615
|
-
if (status >= 500) {
|
|
2616
|
-
console.error("[API Plugin] Error:", {
|
|
2617
|
-
message: err.message,
|
|
2618
|
-
code,
|
|
2619
|
-
status,
|
|
2620
|
-
stack: err.stack,
|
|
2621
|
-
details
|
|
2622
|
-
});
|
|
2623
|
-
} else if (status >= 400 && status < 500 && c.get("verbose")) {
|
|
2624
|
-
console.warn("[API Plugin] Client error:", {
|
|
2625
|
-
message: err.message,
|
|
2626
|
-
code,
|
|
2627
|
-
status,
|
|
2628
|
-
details
|
|
2629
|
-
});
|
|
2630
|
-
}
|
|
2631
|
-
return c.json(response, response._status);
|
|
2632
|
-
}
|
|
2633
|
-
function asyncHandler(fn) {
|
|
2634
|
-
return async (c) => {
|
|
2635
|
-
try {
|
|
2636
|
-
return await fn(c);
|
|
2637
|
-
} catch (err) {
|
|
2638
|
-
return errorHandler(err, c);
|
|
2639
|
-
}
|
|
2640
|
-
};
|
|
2641
|
-
}
|
|
2642
|
-
|
|
2643
|
-
function createResourceRoutes(resource, version, config = {}) {
|
|
2644
|
-
const app = new hono.Hono();
|
|
2645
|
-
const {
|
|
2646
|
-
methods = ["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"],
|
|
2647
|
-
customMiddleware = [],
|
|
2648
|
-
enableValidation = true
|
|
2649
|
-
} = config;
|
|
2650
|
-
const resourceName = resource.name;
|
|
2651
|
-
const basePath = `/${version}/${resourceName}`;
|
|
2652
|
-
customMiddleware.forEach((middleware) => {
|
|
2653
|
-
app.use("*", middleware);
|
|
2654
|
-
});
|
|
2655
|
-
if (methods.includes("GET")) {
|
|
2656
|
-
app.get("/", asyncHandler(async (c) => {
|
|
2657
|
-
const query = c.req.query();
|
|
2658
|
-
const limit = parseInt(query.limit) || 100;
|
|
2659
|
-
const offset = parseInt(query.offset) || 0;
|
|
2660
|
-
const partition = query.partition;
|
|
2661
|
-
const partitionValues = query.partitionValues ? JSON.parse(query.partitionValues) : void 0;
|
|
2662
|
-
const reservedKeys = ["limit", "offset", "partition", "partitionValues", "sort"];
|
|
2663
|
-
const filters = {};
|
|
2664
|
-
for (const [key, value] of Object.entries(query)) {
|
|
2665
|
-
if (!reservedKeys.includes(key)) {
|
|
2666
|
-
try {
|
|
2667
|
-
filters[key] = JSON.parse(value);
|
|
2668
|
-
} catch {
|
|
2669
|
-
filters[key] = value;
|
|
2670
|
-
}
|
|
2671
|
-
}
|
|
2672
|
-
}
|
|
2673
|
-
let items;
|
|
2674
|
-
let total;
|
|
2675
|
-
if (Object.keys(filters).length > 0) {
|
|
2676
|
-
items = await resource.query(filters, { limit: limit + offset });
|
|
2677
|
-
items = items.slice(offset, offset + limit);
|
|
2678
|
-
total = items.length;
|
|
2679
|
-
} else if (partition && partitionValues) {
|
|
2680
|
-
items = await resource.listPartition({
|
|
2681
|
-
partition,
|
|
2682
|
-
partitionValues,
|
|
2683
|
-
limit: limit + offset
|
|
2684
|
-
});
|
|
2685
|
-
items = items.slice(offset, offset + limit);
|
|
2686
|
-
total = items.length;
|
|
2687
|
-
} else {
|
|
2688
|
-
items = await resource.list({ limit: limit + offset });
|
|
2689
|
-
items = items.slice(offset, offset + limit);
|
|
2690
|
-
total = items.length;
|
|
2691
|
-
}
|
|
2692
|
-
const response = list(items, {
|
|
2693
|
-
total,
|
|
2694
|
-
page: Math.floor(offset / limit) + 1,
|
|
2695
|
-
pageSize: limit,
|
|
2696
|
-
pageCount: Math.ceil(total / limit)
|
|
2697
|
-
});
|
|
2698
|
-
c.header("X-Total-Count", total.toString());
|
|
2699
|
-
c.header("X-Page-Count", Math.ceil(total / limit).toString());
|
|
2700
|
-
return c.json(response, response._status);
|
|
2701
|
-
}));
|
|
2702
|
-
}
|
|
2703
|
-
if (methods.includes("GET")) {
|
|
2704
|
-
app.get("/:id", asyncHandler(async (c) => {
|
|
2705
|
-
const id = c.req.param("id");
|
|
2706
|
-
const query = c.req.query();
|
|
2707
|
-
const partition = query.partition;
|
|
2708
|
-
const partitionValues = query.partitionValues ? JSON.parse(query.partitionValues) : void 0;
|
|
2709
|
-
let item;
|
|
2710
|
-
if (partition && partitionValues) {
|
|
2711
|
-
item = await resource.getFromPartition({
|
|
2712
|
-
id,
|
|
2713
|
-
partitionName: partition,
|
|
2714
|
-
partitionValues
|
|
2715
|
-
});
|
|
2716
|
-
} else {
|
|
2717
|
-
item = await resource.get(id);
|
|
2718
|
-
}
|
|
2719
|
-
if (!item) {
|
|
2720
|
-
const response2 = notFound(resourceName, id);
|
|
2721
|
-
return c.json(response2, response2._status);
|
|
2722
|
-
}
|
|
2723
|
-
const response = success(item);
|
|
2724
|
-
return c.json(response, response._status);
|
|
2725
|
-
}));
|
|
2726
|
-
}
|
|
2727
|
-
if (methods.includes("POST")) {
|
|
2728
|
-
app.post("/", asyncHandler(async (c) => {
|
|
2729
|
-
const data = await c.req.json();
|
|
2730
|
-
const item = await resource.insert(data);
|
|
2731
|
-
const location = `${basePath}/${item.id}`;
|
|
2732
|
-
const response = created(item, location);
|
|
2733
|
-
c.header("Location", location);
|
|
2734
|
-
return c.json(response, response._status);
|
|
2735
|
-
}));
|
|
2736
|
-
}
|
|
2737
|
-
if (methods.includes("PUT")) {
|
|
2738
|
-
app.put("/:id", asyncHandler(async (c) => {
|
|
2739
|
-
const id = c.req.param("id");
|
|
2740
|
-
const data = await c.req.json();
|
|
2741
|
-
const existing = await resource.get(id);
|
|
2742
|
-
if (!existing) {
|
|
2743
|
-
const response2 = notFound(resourceName, id);
|
|
2744
|
-
return c.json(response2, response2._status);
|
|
2745
|
-
}
|
|
2746
|
-
const updated = await resource.update(id, data);
|
|
2747
|
-
const response = success(updated);
|
|
2748
|
-
return c.json(response, response._status);
|
|
2749
|
-
}));
|
|
2750
|
-
}
|
|
2751
|
-
if (methods.includes("PATCH")) {
|
|
2752
|
-
app.patch("/:id", asyncHandler(async (c) => {
|
|
2753
|
-
const id = c.req.param("id");
|
|
2754
|
-
const data = await c.req.json();
|
|
2755
|
-
const existing = await resource.get(id);
|
|
2756
|
-
if (!existing) {
|
|
2757
|
-
const response2 = notFound(resourceName, id);
|
|
2758
|
-
return c.json(response2, response2._status);
|
|
2759
|
-
}
|
|
2760
|
-
const merged = { ...existing, ...data, id };
|
|
2761
|
-
const updated = await resource.update(id, merged);
|
|
2762
|
-
const response = success(updated);
|
|
2763
|
-
return c.json(response, response._status);
|
|
2764
|
-
}));
|
|
2765
|
-
}
|
|
2766
|
-
if (methods.includes("DELETE")) {
|
|
2767
|
-
app.delete("/:id", asyncHandler(async (c) => {
|
|
2768
|
-
const id = c.req.param("id");
|
|
2769
|
-
const existing = await resource.get(id);
|
|
2770
|
-
if (!existing) {
|
|
2771
|
-
const response2 = notFound(resourceName, id);
|
|
2772
|
-
return c.json(response2, response2._status);
|
|
2773
|
-
}
|
|
2774
|
-
await resource.delete(id);
|
|
2775
|
-
const response = noContent();
|
|
2776
|
-
return c.json(response, response._status);
|
|
2777
|
-
}));
|
|
2778
|
-
}
|
|
2779
|
-
if (methods.includes("HEAD")) {
|
|
2780
|
-
app.on("HEAD", "/", asyncHandler(async (c) => {
|
|
2781
|
-
const total = await resource.count();
|
|
2782
|
-
const allItems = await resource.list({ limit: 1e3 });
|
|
2783
|
-
const stats = {
|
|
2784
|
-
total,
|
|
2785
|
-
version: resource.config?.currentVersion || resource.version || "v1"
|
|
2786
|
-
};
|
|
2787
|
-
c.header("X-Total-Count", total.toString());
|
|
2788
|
-
c.header("X-Resource-Version", stats.version);
|
|
2789
|
-
c.header("X-Schema-Fields", Object.keys(resource.config?.attributes || {}).length.toString());
|
|
2790
|
-
return c.body(null, 200);
|
|
2791
|
-
}));
|
|
2792
|
-
app.on("HEAD", "/:id", asyncHandler(async (c) => {
|
|
2793
|
-
const id = c.req.param("id");
|
|
2794
|
-
const item = await resource.get(id);
|
|
2795
|
-
if (!item) {
|
|
2796
|
-
return c.body(null, 404);
|
|
2797
|
-
}
|
|
2798
|
-
if (item.updatedAt) {
|
|
2799
|
-
c.header("Last-Modified", new Date(item.updatedAt).toUTCString());
|
|
2800
|
-
}
|
|
2801
|
-
return c.body(null, 200);
|
|
2802
|
-
}));
|
|
2803
|
-
}
|
|
2804
|
-
if (methods.includes("OPTIONS")) {
|
|
2805
|
-
app.options("/", asyncHandler(async (c) => {
|
|
2806
|
-
c.header("Allow", methods.join(", "));
|
|
2807
|
-
const total = await resource.count();
|
|
2808
|
-
const schema = resource.config?.attributes || {};
|
|
2809
|
-
const version2 = resource.config?.currentVersion || resource.version || "v1";
|
|
2810
|
-
const metadata = {
|
|
2811
|
-
resource: resourceName,
|
|
2812
|
-
version: version2,
|
|
2813
|
-
totalRecords: total,
|
|
2814
|
-
allowedMethods: methods,
|
|
2815
|
-
schema: Object.entries(schema).map(([name, def]) => ({
|
|
2816
|
-
name,
|
|
2817
|
-
type: typeof def === "string" ? def.split("|")[0] : def.type,
|
|
2818
|
-
rules: typeof def === "string" ? def.split("|").slice(1) : []
|
|
2819
|
-
})),
|
|
2820
|
-
endpoints: {
|
|
2821
|
-
list: `/${version2}/${resourceName}`,
|
|
2822
|
-
get: `/${version2}/${resourceName}/:id`,
|
|
2823
|
-
create: `/${version2}/${resourceName}`,
|
|
2824
|
-
update: `/${version2}/${resourceName}/:id`,
|
|
2825
|
-
delete: `/${version2}/${resourceName}/:id`
|
|
2826
|
-
},
|
|
2827
|
-
queryParameters: {
|
|
2828
|
-
limit: "number (1-1000, default: 100)",
|
|
2829
|
-
offset: "number (min: 0, default: 0)",
|
|
2830
|
-
partition: "string (partition name)",
|
|
2831
|
-
partitionValues: "JSON string",
|
|
2832
|
-
"[any field]": "any (filter by field value)"
|
|
2833
|
-
}
|
|
2834
|
-
};
|
|
2835
|
-
return c.json(metadata);
|
|
2836
|
-
}));
|
|
2837
|
-
app.options("/:id", (c) => {
|
|
2838
|
-
c.header("Allow", methods.filter((m) => m !== "POST").join(", "));
|
|
2839
|
-
return c.body(null, 204);
|
|
2840
|
-
});
|
|
2841
|
-
}
|
|
2842
|
-
return app;
|
|
2843
|
-
}
|
|
2844
|
-
function createRelationalRoutes(sourceResource, relationName, relationConfig, version) {
|
|
2845
|
-
const app = new hono.Hono();
|
|
2846
|
-
const resourceName = sourceResource.name;
|
|
2847
|
-
const relatedResourceName = relationConfig.resource;
|
|
2848
|
-
app.get("/:id", asyncHandler(async (c) => {
|
|
2849
|
-
const id = c.req.param("id");
|
|
2850
|
-
const query = c.req.query();
|
|
2851
|
-
const source = await sourceResource.get(id);
|
|
2852
|
-
if (!source) {
|
|
2853
|
-
const response = notFound(resourceName, id);
|
|
2854
|
-
return c.json(response, response._status);
|
|
2855
|
-
}
|
|
2856
|
-
const result = await sourceResource.get(id, {
|
|
2857
|
-
include: [relationName]
|
|
2858
|
-
});
|
|
2859
|
-
const relatedData = result[relationName];
|
|
2860
|
-
if (!relatedData) {
|
|
2861
|
-
if (relationConfig.type === "hasMany" || relationConfig.type === "belongsToMany") {
|
|
2862
|
-
const response = list([], {
|
|
2863
|
-
total: 0,
|
|
2864
|
-
page: 1,
|
|
2865
|
-
pageSize: 100,
|
|
2866
|
-
pageCount: 0
|
|
2867
|
-
});
|
|
2868
|
-
return c.json(response, response._status);
|
|
2869
|
-
} else {
|
|
2870
|
-
const response = notFound(relatedResourceName, "related resource");
|
|
2871
|
-
return c.json(response, response._status);
|
|
2872
|
-
}
|
|
2873
|
-
}
|
|
2874
|
-
if (relationConfig.type === "hasMany" || relationConfig.type === "belongsToMany") {
|
|
2875
|
-
const items = Array.isArray(relatedData) ? relatedData : [relatedData];
|
|
2876
|
-
const limit = parseInt(query.limit) || 100;
|
|
2877
|
-
const offset = parseInt(query.offset) || 0;
|
|
2878
|
-
const paginatedItems = items.slice(offset, offset + limit);
|
|
2879
|
-
const response = list(paginatedItems, {
|
|
2880
|
-
total: items.length,
|
|
2881
|
-
page: Math.floor(offset / limit) + 1,
|
|
2882
|
-
pageSize: limit,
|
|
2883
|
-
pageCount: Math.ceil(items.length / limit)
|
|
2884
|
-
});
|
|
2885
|
-
c.header("X-Total-Count", items.length.toString());
|
|
2886
|
-
c.header("X-Page-Count", Math.ceil(items.length / limit).toString());
|
|
2887
|
-
return c.json(response, response._status);
|
|
2888
|
-
} else {
|
|
2889
|
-
const response = success(relatedData);
|
|
2890
|
-
return c.json(response, response._status);
|
|
2891
|
-
}
|
|
2892
|
-
}));
|
|
2893
|
-
return app;
|
|
2894
|
-
}
|
|
2895
|
-
|
|
2896
|
-
function mapFieldTypeToOpenAPI(fieldType) {
|
|
2897
|
-
const type = fieldType.split("|")[0].trim();
|
|
2898
|
-
const typeMap = {
|
|
2899
|
-
"string": { type: "string" },
|
|
2900
|
-
"number": { type: "number" },
|
|
2901
|
-
"integer": { type: "integer" },
|
|
2902
|
-
"boolean": { type: "boolean" },
|
|
2903
|
-
"array": { type: "array", items: { type: "string" } },
|
|
2904
|
-
"object": { type: "object" },
|
|
2905
|
-
"json": { type: "object" },
|
|
2906
|
-
"secret": { type: "string", format: "password" },
|
|
2907
|
-
"email": { type: "string", format: "email" },
|
|
2908
|
-
"url": { type: "string", format: "uri" },
|
|
2909
|
-
"date": { type: "string", format: "date" },
|
|
2910
|
-
"datetime": { type: "string", format: "date-time" },
|
|
2911
|
-
"ip4": { type: "string", format: "ipv4", description: "IPv4 address" },
|
|
2912
|
-
"ip6": { type: "string", format: "ipv6", description: "IPv6 address" },
|
|
2913
|
-
"embedding": { type: "array", items: { type: "number" }, description: "Vector embedding" }
|
|
2914
|
-
};
|
|
2915
|
-
if (type.startsWith("embedding:")) {
|
|
2916
|
-
const length = parseInt(type.split(":")[1]);
|
|
2917
|
-
return {
|
|
2918
|
-
type: "array",
|
|
2919
|
-
items: { type: "number" },
|
|
2920
|
-
minItems: length,
|
|
2921
|
-
maxItems: length,
|
|
2922
|
-
description: `Vector embedding (${length} dimensions)`
|
|
2923
|
-
};
|
|
2924
|
-
}
|
|
2925
|
-
return typeMap[type] || { type: "string" };
|
|
2926
|
-
}
|
|
2927
|
-
function extractValidationRules(fieldDef) {
|
|
2928
|
-
const rules = {};
|
|
2929
|
-
const parts = fieldDef.split("|");
|
|
2930
|
-
for (const part of parts) {
|
|
2931
|
-
const [rule, value] = part.split(":").map((s) => s.trim());
|
|
2932
|
-
switch (rule) {
|
|
2933
|
-
case "required":
|
|
2934
|
-
rules.required = true;
|
|
2935
|
-
break;
|
|
2936
|
-
case "min":
|
|
2937
|
-
rules.minimum = parseFloat(value);
|
|
2938
|
-
break;
|
|
2939
|
-
case "max":
|
|
2940
|
-
rules.maximum = parseFloat(value);
|
|
2941
|
-
break;
|
|
2942
|
-
case "minlength":
|
|
2943
|
-
rules.minLength = parseInt(value);
|
|
2944
|
-
break;
|
|
2945
|
-
case "maxlength":
|
|
2946
|
-
rules.maxLength = parseInt(value);
|
|
2947
|
-
break;
|
|
2948
|
-
case "pattern":
|
|
2949
|
-
rules.pattern = value;
|
|
2950
|
-
break;
|
|
2951
|
-
case "enum":
|
|
2952
|
-
rules.enum = value.split(",").map((v) => v.trim());
|
|
2953
|
-
break;
|
|
2954
|
-
case "default":
|
|
2955
|
-
rules.default = value;
|
|
2956
|
-
break;
|
|
2957
|
-
}
|
|
2958
|
-
}
|
|
2959
|
-
return rules;
|
|
2960
|
-
}
|
|
2961
|
-
function generateResourceSchema(resource) {
|
|
2962
|
-
const properties = {};
|
|
2963
|
-
const required = [];
|
|
2964
|
-
const attributes = resource.config?.attributes || resource.attributes || {};
|
|
2965
|
-
const resourceDescription = resource.config?.description;
|
|
2966
|
-
const attributeDescriptions = typeof resourceDescription === "object" ? resourceDescription.attributes || {} : {};
|
|
2967
|
-
properties.id = {
|
|
2968
|
-
type: "string",
|
|
2969
|
-
description: "Unique identifier for the resource",
|
|
2970
|
-
example: "2_gDTpeU6EI0e8B92n_R3Y",
|
|
2971
|
-
readOnly: true
|
|
2972
|
-
};
|
|
2973
|
-
for (const [fieldName, fieldDef] of Object.entries(attributes)) {
|
|
2974
|
-
if (typeof fieldDef === "object" && fieldDef.type) {
|
|
2975
|
-
const baseType = mapFieldTypeToOpenAPI(fieldDef.type);
|
|
2976
|
-
properties[fieldName] = {
|
|
2977
|
-
...baseType,
|
|
2978
|
-
description: fieldDef.description || attributeDescriptions[fieldName] || void 0
|
|
2979
|
-
};
|
|
2980
|
-
if (fieldDef.required) {
|
|
2981
|
-
required.push(fieldName);
|
|
2982
|
-
}
|
|
2983
|
-
if (fieldDef.type === "object" && fieldDef.props) {
|
|
2984
|
-
properties[fieldName].properties = {};
|
|
2985
|
-
for (const [propName, propDef] of Object.entries(fieldDef.props)) {
|
|
2986
|
-
const propType = typeof propDef === "string" ? propDef : propDef.type;
|
|
2987
|
-
properties[fieldName].properties[propName] = mapFieldTypeToOpenAPI(propType);
|
|
2988
|
-
}
|
|
2989
|
-
}
|
|
2990
|
-
if (fieldDef.type === "array" && fieldDef.items) {
|
|
2991
|
-
properties[fieldName].items = mapFieldTypeToOpenAPI(fieldDef.items);
|
|
2992
|
-
}
|
|
2993
|
-
} else if (typeof fieldDef === "string") {
|
|
2994
|
-
const baseType = mapFieldTypeToOpenAPI(fieldDef);
|
|
2995
|
-
const rules = extractValidationRules(fieldDef);
|
|
2996
|
-
properties[fieldName] = {
|
|
2997
|
-
...baseType,
|
|
2998
|
-
...rules,
|
|
2999
|
-
description: attributeDescriptions[fieldName] || void 0
|
|
3000
|
-
};
|
|
3001
|
-
if (rules.required) {
|
|
3002
|
-
required.push(fieldName);
|
|
3003
|
-
delete properties[fieldName].required;
|
|
3004
|
-
}
|
|
3005
|
-
}
|
|
3006
|
-
}
|
|
3007
|
-
return {
|
|
3008
|
-
type: "object",
|
|
3009
|
-
properties,
|
|
3010
|
-
required: required.length > 0 ? required : void 0
|
|
3011
|
-
};
|
|
3012
|
-
}
|
|
3013
|
-
function generateResourcePaths(resource, version, config = {}) {
|
|
3014
|
-
const resourceName = resource.name;
|
|
3015
|
-
const basePath = `/${version}/${resourceName}`;
|
|
3016
|
-
const schema = generateResourceSchema(resource);
|
|
3017
|
-
const methods = config.methods || ["GET", "POST", "PUT", "PATCH", "DELETE"];
|
|
3018
|
-
const authMethods = config.auth || [];
|
|
3019
|
-
const requiresAuth = authMethods && authMethods.length > 0;
|
|
3020
|
-
const paths = {};
|
|
3021
|
-
const security = [];
|
|
3022
|
-
if (requiresAuth) {
|
|
3023
|
-
if (authMethods.includes("jwt")) security.push({ bearerAuth: [] });
|
|
3024
|
-
if (authMethods.includes("apiKey")) security.push({ apiKeyAuth: [] });
|
|
3025
|
-
if (authMethods.includes("basic")) security.push({ basicAuth: [] });
|
|
3026
|
-
}
|
|
3027
|
-
const partitions = resource.config?.options?.partitions || resource.config?.partitions || resource.partitions || {};
|
|
3028
|
-
const partitionNames = Object.keys(partitions);
|
|
3029
|
-
const hasPartitions = partitionNames.length > 0;
|
|
3030
|
-
let partitionDescription = "Partition name for filtering";
|
|
3031
|
-
let partitionValuesDescription = "Partition values as JSON string";
|
|
3032
|
-
let partitionExample = void 0;
|
|
3033
|
-
let partitionValuesExample = void 0;
|
|
3034
|
-
if (hasPartitions) {
|
|
3035
|
-
const partitionDocs = partitionNames.map((name) => {
|
|
3036
|
-
const partition = partitions[name];
|
|
3037
|
-
const fields = Object.keys(partition.fields || {});
|
|
3038
|
-
const fieldTypes = Object.entries(partition.fields || {}).map(([field, type]) => `${field}: ${type}`).join(", ");
|
|
3039
|
-
return `- **${name}**: Filters by ${fields.join(", ")} (${fieldTypes})`;
|
|
3040
|
-
}).join("\n");
|
|
3041
|
-
partitionDescription = `Available partitions:
|
|
3042
|
-
${partitionDocs}`;
|
|
3043
|
-
const examplePartition = partitionNames[0];
|
|
3044
|
-
const exampleFields = partitions[examplePartition]?.fields || {};
|
|
3045
|
-
Object.entries(exampleFields).map(([field, type]) => `"${field}": <${type} value>`).join(", ");
|
|
3046
|
-
partitionValuesDescription = `Partition field values as JSON string. Must match the structure of the selected partition.
|
|
3047
|
-
|
|
3048
|
-
Example for "${examplePartition}" partition: \`{"${Object.keys(exampleFields)[0]}": "value"}\``;
|
|
3049
|
-
partitionExample = examplePartition;
|
|
3050
|
-
const firstField = Object.keys(exampleFields)[0];
|
|
3051
|
-
const firstFieldType = exampleFields[firstField];
|
|
3052
|
-
let exampleValue = "example";
|
|
3053
|
-
if (firstFieldType === "number" || firstFieldType === "integer") {
|
|
3054
|
-
exampleValue = 123;
|
|
3055
|
-
} else if (firstFieldType === "boolean") {
|
|
3056
|
-
exampleValue = true;
|
|
3057
|
-
}
|
|
3058
|
-
partitionValuesExample = JSON.stringify({ [firstField]: exampleValue });
|
|
3059
|
-
}
|
|
3060
|
-
const attributeQueryParams = [];
|
|
3061
|
-
if (hasPartitions) {
|
|
3062
|
-
const partitionFieldsSet = /* @__PURE__ */ new Set();
|
|
3063
|
-
for (const [partitionName, partition] of Object.entries(partitions)) {
|
|
3064
|
-
const fields = partition.fields || {};
|
|
3065
|
-
for (const fieldName of Object.keys(fields)) {
|
|
3066
|
-
partitionFieldsSet.add(fieldName);
|
|
3067
|
-
}
|
|
3068
|
-
}
|
|
3069
|
-
const attributes = resource.config?.attributes || resource.attributes || {};
|
|
3070
|
-
for (const fieldName of partitionFieldsSet) {
|
|
3071
|
-
const fieldDef = attributes[fieldName];
|
|
3072
|
-
if (!fieldDef) continue;
|
|
3073
|
-
let fieldType;
|
|
3074
|
-
if (typeof fieldDef === "object" && fieldDef.type) {
|
|
3075
|
-
fieldType = fieldDef.type;
|
|
3076
|
-
} else if (typeof fieldDef === "string") {
|
|
3077
|
-
fieldType = fieldDef.split("|")[0].trim();
|
|
3078
|
-
} else {
|
|
3079
|
-
fieldType = "string";
|
|
3080
|
-
}
|
|
3081
|
-
const openAPIType = mapFieldTypeToOpenAPI(fieldType);
|
|
3082
|
-
attributeQueryParams.push({
|
|
3083
|
-
name: fieldName,
|
|
3084
|
-
in: "query",
|
|
3085
|
-
description: `Filter by ${fieldName} field (indexed via partitions for efficient querying). Value will be parsed as JSON if possible, otherwise treated as string.`,
|
|
3086
|
-
required: false,
|
|
3087
|
-
schema: openAPIType
|
|
3088
|
-
});
|
|
3089
|
-
}
|
|
3090
|
-
}
|
|
3091
|
-
if (methods.includes("GET")) {
|
|
3092
|
-
paths[basePath] = {
|
|
3093
|
-
get: {
|
|
3094
|
-
tags: [resourceName],
|
|
3095
|
-
summary: `List ${resourceName}`,
|
|
3096
|
-
description: `Retrieve a paginated list of ${resourceName}. Supports filtering by passing any resource field as a query parameter (e.g., ?status=active&year=2024). Values are parsed as JSON if possible, otherwise treated as strings.
|
|
3097
|
-
|
|
3098
|
-
**Pagination**: Use \`limit\` and \`offset\` to paginate results. For example:
|
|
3099
|
-
- First page (10 items): \`?limit=10&offset=0\`
|
|
3100
|
-
- Second page: \`?limit=10&offset=10\`
|
|
3101
|
-
- Third page: \`?limit=10&offset=20\`
|
|
3102
|
-
|
|
3103
|
-
The response includes pagination metadata in the \`pagination\` object with total count and page information.${hasPartitions ? "\n\n**Partitioning**: This resource supports partitioned queries for optimized filtering. Use the `partition` and `partitionValues` parameters together." : ""}`,
|
|
3104
|
-
parameters: [
|
|
3105
|
-
{
|
|
3106
|
-
name: "limit",
|
|
3107
|
-
in: "query",
|
|
3108
|
-
description: "Maximum number of items to return per page (page size)",
|
|
3109
|
-
schema: { type: "integer", default: 100, minimum: 1, maximum: 1e3 },
|
|
3110
|
-
example: 10
|
|
3111
|
-
},
|
|
3112
|
-
{
|
|
3113
|
-
name: "offset",
|
|
3114
|
-
in: "query",
|
|
3115
|
-
description: "Number of items to skip before starting to return results. Use for pagination: offset = (page - 1) * limit",
|
|
3116
|
-
schema: { type: "integer", default: 0, minimum: 0 },
|
|
3117
|
-
example: 0
|
|
3118
|
-
},
|
|
3119
|
-
...hasPartitions ? [
|
|
3120
|
-
{
|
|
3121
|
-
name: "partition",
|
|
3122
|
-
in: "query",
|
|
3123
|
-
description: partitionDescription,
|
|
3124
|
-
schema: {
|
|
3125
|
-
type: "string",
|
|
3126
|
-
enum: partitionNames
|
|
3127
|
-
},
|
|
3128
|
-
example: partitionExample
|
|
3129
|
-
},
|
|
3130
|
-
{
|
|
3131
|
-
name: "partitionValues",
|
|
3132
|
-
in: "query",
|
|
3133
|
-
description: partitionValuesDescription,
|
|
3134
|
-
schema: { type: "string" },
|
|
3135
|
-
example: partitionValuesExample
|
|
3136
|
-
}
|
|
3137
|
-
] : [],
|
|
3138
|
-
...attributeQueryParams
|
|
3139
|
-
],
|
|
3140
|
-
responses: {
|
|
3141
|
-
200: {
|
|
3142
|
-
description: "Successful response",
|
|
3143
|
-
content: {
|
|
3144
|
-
"application/json": {
|
|
3145
|
-
schema: {
|
|
3146
|
-
type: "object",
|
|
3147
|
-
properties: {
|
|
3148
|
-
success: { type: "boolean", example: true },
|
|
3149
|
-
data: {
|
|
3150
|
-
type: "array",
|
|
3151
|
-
items: schema
|
|
3152
|
-
},
|
|
3153
|
-
pagination: {
|
|
3154
|
-
type: "object",
|
|
3155
|
-
description: "Pagination metadata for the current request",
|
|
3156
|
-
properties: {
|
|
3157
|
-
total: {
|
|
3158
|
-
type: "integer",
|
|
3159
|
-
description: "Total number of items available",
|
|
3160
|
-
example: 150
|
|
3161
|
-
},
|
|
3162
|
-
page: {
|
|
3163
|
-
type: "integer",
|
|
3164
|
-
description: "Current page number (1-indexed)",
|
|
3165
|
-
example: 1
|
|
3166
|
-
},
|
|
3167
|
-
pageSize: {
|
|
3168
|
-
type: "integer",
|
|
3169
|
-
description: "Number of items per page (same as limit parameter)",
|
|
3170
|
-
example: 10
|
|
3171
|
-
},
|
|
3172
|
-
pageCount: {
|
|
3173
|
-
type: "integer",
|
|
3174
|
-
description: "Total number of pages available",
|
|
3175
|
-
example: 15
|
|
3176
|
-
}
|
|
3177
|
-
}
|
|
3178
|
-
}
|
|
3179
|
-
}
|
|
3180
|
-
}
|
|
3181
|
-
}
|
|
3182
|
-
},
|
|
3183
|
-
headers: {
|
|
3184
|
-
"X-Total-Count": {
|
|
3185
|
-
description: "Total number of records",
|
|
3186
|
-
schema: { type: "integer" }
|
|
3187
|
-
},
|
|
3188
|
-
"X-Page-Count": {
|
|
3189
|
-
description: "Total number of pages",
|
|
3190
|
-
schema: { type: "integer" }
|
|
3191
|
-
}
|
|
3192
|
-
}
|
|
3193
|
-
}
|
|
3194
|
-
},
|
|
3195
|
-
security: security.length > 0 ? security : void 0
|
|
3196
|
-
}
|
|
3197
|
-
};
|
|
3198
|
-
}
|
|
3199
|
-
if (methods.includes("GET")) {
|
|
3200
|
-
paths[`${basePath}/{id}`] = {
|
|
3201
|
-
get: {
|
|
3202
|
-
tags: [resourceName],
|
|
3203
|
-
summary: `Get ${resourceName} by ID`,
|
|
3204
|
-
description: `Retrieve a single ${resourceName} by its ID${hasPartitions ? ". Optionally specify a partition for more efficient retrieval." : ""}`,
|
|
3205
|
-
parameters: [
|
|
3206
|
-
{
|
|
3207
|
-
name: "id",
|
|
3208
|
-
in: "path",
|
|
3209
|
-
required: true,
|
|
3210
|
-
description: `${resourceName} ID`,
|
|
3211
|
-
schema: { type: "string" }
|
|
3212
|
-
},
|
|
3213
|
-
...hasPartitions ? [
|
|
3214
|
-
{
|
|
3215
|
-
name: "partition",
|
|
3216
|
-
in: "query",
|
|
3217
|
-
description: partitionDescription,
|
|
3218
|
-
schema: {
|
|
3219
|
-
type: "string",
|
|
3220
|
-
enum: partitionNames
|
|
3221
|
-
},
|
|
3222
|
-
example: partitionExample
|
|
3223
|
-
},
|
|
3224
|
-
{
|
|
3225
|
-
name: "partitionValues",
|
|
3226
|
-
in: "query",
|
|
3227
|
-
description: partitionValuesDescription,
|
|
3228
|
-
schema: { type: "string" },
|
|
3229
|
-
example: partitionValuesExample
|
|
3230
|
-
}
|
|
3231
|
-
] : []
|
|
3232
|
-
],
|
|
3233
|
-
responses: {
|
|
3234
|
-
200: {
|
|
3235
|
-
description: "Successful response",
|
|
3236
|
-
content: {
|
|
3237
|
-
"application/json": {
|
|
3238
|
-
schema: {
|
|
3239
|
-
type: "object",
|
|
3240
|
-
properties: {
|
|
3241
|
-
success: { type: "boolean", example: true },
|
|
3242
|
-
data: schema
|
|
3243
|
-
}
|
|
3244
|
-
}
|
|
3245
|
-
}
|
|
3246
|
-
}
|
|
3247
|
-
},
|
|
3248
|
-
404: {
|
|
3249
|
-
description: "Resource not found",
|
|
3250
|
-
content: {
|
|
3251
|
-
"application/json": {
|
|
3252
|
-
schema: { $ref: "#/components/schemas/Error" }
|
|
3253
|
-
}
|
|
3254
|
-
}
|
|
3255
|
-
}
|
|
3256
|
-
},
|
|
3257
|
-
security: security.length > 0 ? security : void 0
|
|
3258
|
-
}
|
|
3259
|
-
};
|
|
3260
|
-
}
|
|
3261
|
-
if (methods.includes("POST")) {
|
|
3262
|
-
if (!paths[basePath]) paths[basePath] = {};
|
|
3263
|
-
paths[basePath].post = {
|
|
3264
|
-
tags: [resourceName],
|
|
3265
|
-
summary: `Create ${resourceName}`,
|
|
3266
|
-
description: `Create a new ${resourceName}`,
|
|
3267
|
-
requestBody: {
|
|
3268
|
-
required: true,
|
|
3269
|
-
content: {
|
|
3270
|
-
"application/json": {
|
|
3271
|
-
schema
|
|
3272
|
-
}
|
|
3273
|
-
}
|
|
3274
|
-
},
|
|
3275
|
-
responses: {
|
|
3276
|
-
201: {
|
|
3277
|
-
description: "Resource created successfully",
|
|
3278
|
-
content: {
|
|
3279
|
-
"application/json": {
|
|
3280
|
-
schema: {
|
|
3281
|
-
type: "object",
|
|
3282
|
-
properties: {
|
|
3283
|
-
success: { type: "boolean", example: true },
|
|
3284
|
-
data: schema
|
|
3285
|
-
}
|
|
3286
|
-
}
|
|
3287
|
-
}
|
|
3288
|
-
},
|
|
3289
|
-
headers: {
|
|
3290
|
-
Location: {
|
|
3291
|
-
description: "URL of the created resource",
|
|
3292
|
-
schema: { type: "string" }
|
|
3293
|
-
}
|
|
3294
|
-
}
|
|
3295
|
-
},
|
|
3296
|
-
400: {
|
|
3297
|
-
description: "Validation error",
|
|
3298
|
-
content: {
|
|
3299
|
-
"application/json": {
|
|
3300
|
-
schema: { $ref: "#/components/schemas/ValidationError" }
|
|
3301
|
-
}
|
|
3302
|
-
}
|
|
3303
|
-
}
|
|
3304
|
-
},
|
|
3305
|
-
security: security.length > 0 ? security : void 0
|
|
3306
|
-
};
|
|
3307
|
-
}
|
|
3308
|
-
if (methods.includes("PUT")) {
|
|
3309
|
-
if (!paths[`${basePath}/{id}`]) paths[`${basePath}/{id}`] = {};
|
|
3310
|
-
paths[`${basePath}/{id}`].put = {
|
|
3311
|
-
tags: [resourceName],
|
|
3312
|
-
summary: `Update ${resourceName} (full)`,
|
|
3313
|
-
description: `Fully update a ${resourceName}`,
|
|
3314
|
-
parameters: [
|
|
3315
|
-
{
|
|
3316
|
-
name: "id",
|
|
3317
|
-
in: "path",
|
|
3318
|
-
required: true,
|
|
3319
|
-
schema: { type: "string" }
|
|
3320
|
-
}
|
|
3321
|
-
],
|
|
3322
|
-
requestBody: {
|
|
3323
|
-
required: true,
|
|
3324
|
-
content: {
|
|
3325
|
-
"application/json": {
|
|
3326
|
-
schema
|
|
3327
|
-
}
|
|
3328
|
-
}
|
|
3329
|
-
},
|
|
3330
|
-
responses: {
|
|
3331
|
-
200: {
|
|
3332
|
-
description: "Resource updated successfully",
|
|
3333
|
-
content: {
|
|
3334
|
-
"application/json": {
|
|
3335
|
-
schema: {
|
|
3336
|
-
type: "object",
|
|
3337
|
-
properties: {
|
|
3338
|
-
success: { type: "boolean", example: true },
|
|
3339
|
-
data: schema
|
|
3340
|
-
}
|
|
3341
|
-
}
|
|
3342
|
-
}
|
|
3343
|
-
}
|
|
3344
|
-
},
|
|
3345
|
-
404: {
|
|
3346
|
-
description: "Resource not found",
|
|
3347
|
-
content: {
|
|
3348
|
-
"application/json": {
|
|
3349
|
-
schema: { $ref: "#/components/schemas/Error" }
|
|
3350
|
-
}
|
|
3351
|
-
}
|
|
3352
|
-
}
|
|
3353
|
-
},
|
|
3354
|
-
security: security.length > 0 ? security : void 0
|
|
3355
|
-
};
|
|
3356
|
-
}
|
|
3357
|
-
if (methods.includes("PATCH")) {
|
|
3358
|
-
if (!paths[`${basePath}/{id}`]) paths[`${basePath}/{id}`] = {};
|
|
3359
|
-
paths[`${basePath}/{id}`].patch = {
|
|
3360
|
-
tags: [resourceName],
|
|
3361
|
-
summary: `Update ${resourceName} (partial)`,
|
|
3362
|
-
description: `Partially update a ${resourceName}`,
|
|
3363
|
-
parameters: [
|
|
3364
|
-
{
|
|
3365
|
-
name: "id",
|
|
3366
|
-
in: "path",
|
|
3367
|
-
required: true,
|
|
3368
|
-
schema: { type: "string" }
|
|
3369
|
-
}
|
|
3370
|
-
],
|
|
3371
|
-
requestBody: {
|
|
3372
|
-
required: true,
|
|
3373
|
-
content: {
|
|
3374
|
-
"application/json": {
|
|
3375
|
-
schema: {
|
|
3376
|
-
...schema,
|
|
3377
|
-
required: void 0
|
|
3378
|
-
// Partial updates don't require all fields
|
|
3379
|
-
}
|
|
3380
|
-
}
|
|
3381
|
-
}
|
|
3382
|
-
},
|
|
3383
|
-
responses: {
|
|
3384
|
-
200: {
|
|
3385
|
-
description: "Resource updated successfully",
|
|
3386
|
-
content: {
|
|
3387
|
-
"application/json": {
|
|
3388
|
-
schema: {
|
|
3389
|
-
type: "object",
|
|
3390
|
-
properties: {
|
|
3391
|
-
success: { type: "boolean", example: true },
|
|
3392
|
-
data: schema
|
|
3393
|
-
}
|
|
3394
|
-
}
|
|
3395
|
-
}
|
|
3396
|
-
}
|
|
3397
|
-
},
|
|
3398
|
-
404: {
|
|
3399
|
-
description: "Resource not found",
|
|
3400
|
-
content: {
|
|
3401
|
-
"application/json": {
|
|
3402
|
-
schema: { $ref: "#/components/schemas/Error" }
|
|
3403
|
-
}
|
|
3404
|
-
}
|
|
3405
|
-
}
|
|
3406
|
-
},
|
|
3407
|
-
security: security.length > 0 ? security : void 0
|
|
3408
|
-
};
|
|
3409
|
-
}
|
|
3410
|
-
if (methods.includes("DELETE")) {
|
|
3411
|
-
if (!paths[`${basePath}/{id}`]) paths[`${basePath}/{id}`] = {};
|
|
3412
|
-
paths[`${basePath}/{id}`].delete = {
|
|
3413
|
-
tags: [resourceName],
|
|
3414
|
-
summary: `Delete ${resourceName}`,
|
|
3415
|
-
description: `Delete a ${resourceName} by ID`,
|
|
3416
|
-
parameters: [
|
|
3417
|
-
{
|
|
3418
|
-
name: "id",
|
|
3419
|
-
in: "path",
|
|
3420
|
-
required: true,
|
|
3421
|
-
schema: { type: "string" }
|
|
3422
|
-
}
|
|
3423
|
-
],
|
|
3424
|
-
responses: {
|
|
3425
|
-
204: {
|
|
3426
|
-
description: "Resource deleted successfully"
|
|
3427
|
-
},
|
|
3428
|
-
404: {
|
|
3429
|
-
description: "Resource not found",
|
|
3430
|
-
content: {
|
|
3431
|
-
"application/json": {
|
|
3432
|
-
schema: { $ref: "#/components/schemas/Error" }
|
|
3433
|
-
}
|
|
3434
|
-
}
|
|
3435
|
-
}
|
|
3436
|
-
},
|
|
3437
|
-
security: security.length > 0 ? security : void 0
|
|
3438
|
-
};
|
|
3439
|
-
}
|
|
3440
|
-
if (methods.includes("HEAD")) {
|
|
3441
|
-
if (!paths[basePath]) paths[basePath] = {};
|
|
3442
|
-
paths[basePath].head = {
|
|
3443
|
-
tags: [resourceName],
|
|
3444
|
-
summary: `Get ${resourceName} statistics`,
|
|
3445
|
-
description: `Get statistics about ${resourceName} collection without retrieving data. Returns statistics in response headers.`,
|
|
3446
|
-
responses: {
|
|
3447
|
-
200: {
|
|
3448
|
-
description: "Statistics retrieved successfully",
|
|
3449
|
-
headers: {
|
|
3450
|
-
"X-Total-Count": {
|
|
3451
|
-
description: "Total number of records",
|
|
3452
|
-
schema: { type: "integer" }
|
|
3453
|
-
},
|
|
3454
|
-
"X-Resource-Version": {
|
|
3455
|
-
description: "Current resource version",
|
|
3456
|
-
schema: { type: "string" }
|
|
3457
|
-
},
|
|
3458
|
-
"X-Schema-Fields": {
|
|
3459
|
-
description: "Number of schema fields",
|
|
3460
|
-
schema: { type: "integer" }
|
|
3461
|
-
}
|
|
3462
|
-
}
|
|
3463
|
-
}
|
|
3464
|
-
},
|
|
3465
|
-
security: security.length > 0 ? security : void 0
|
|
3466
|
-
};
|
|
3467
|
-
if (!paths[`${basePath}/{id}`]) paths[`${basePath}/{id}`] = {};
|
|
3468
|
-
paths[`${basePath}/{id}`].head = {
|
|
3469
|
-
tags: [resourceName],
|
|
3470
|
-
summary: `Check if ${resourceName} exists`,
|
|
3471
|
-
description: `Check if a ${resourceName} exists without retrieving its data`,
|
|
3472
|
-
parameters: [
|
|
3473
|
-
{
|
|
3474
|
-
name: "id",
|
|
3475
|
-
in: "path",
|
|
3476
|
-
required: true,
|
|
3477
|
-
schema: { type: "string" }
|
|
3478
|
-
}
|
|
3479
|
-
],
|
|
3480
|
-
responses: {
|
|
3481
|
-
200: {
|
|
3482
|
-
description: "Resource exists",
|
|
3483
|
-
headers: {
|
|
3484
|
-
"Last-Modified": {
|
|
3485
|
-
description: "Last modification date",
|
|
3486
|
-
schema: { type: "string", format: "date-time" }
|
|
3487
|
-
}
|
|
3488
|
-
}
|
|
3489
|
-
},
|
|
3490
|
-
404: {
|
|
3491
|
-
description: "Resource not found"
|
|
3492
|
-
}
|
|
3493
|
-
},
|
|
3494
|
-
security: security.length > 0 ? security : void 0
|
|
3495
|
-
};
|
|
3496
|
-
}
|
|
3497
|
-
if (methods.includes("OPTIONS")) {
|
|
3498
|
-
if (!paths[basePath]) paths[basePath] = {};
|
|
3499
|
-
paths[basePath].options = {
|
|
3500
|
-
tags: [resourceName],
|
|
3501
|
-
summary: `Get ${resourceName} metadata`,
|
|
3502
|
-
description: `Get complete metadata about ${resourceName} resource including schema, allowed methods, endpoints, and query parameters`,
|
|
3503
|
-
responses: {
|
|
3504
|
-
200: {
|
|
3505
|
-
description: "Metadata retrieved successfully",
|
|
3506
|
-
headers: {
|
|
3507
|
-
"Allow": {
|
|
3508
|
-
description: "Allowed HTTP methods",
|
|
3509
|
-
schema: { type: "string", example: "GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS" }
|
|
3510
|
-
}
|
|
3511
|
-
},
|
|
3512
|
-
content: {
|
|
3513
|
-
"application/json": {
|
|
3514
|
-
schema: {
|
|
3515
|
-
type: "object",
|
|
3516
|
-
properties: {
|
|
3517
|
-
resource: { type: "string" },
|
|
3518
|
-
version: { type: "string" },
|
|
3519
|
-
totalRecords: { type: "integer" },
|
|
3520
|
-
allowedMethods: {
|
|
3521
|
-
type: "array",
|
|
3522
|
-
items: { type: "string" }
|
|
3523
|
-
},
|
|
3524
|
-
schema: {
|
|
3525
|
-
type: "array",
|
|
3526
|
-
items: {
|
|
3527
|
-
type: "object",
|
|
3528
|
-
properties: {
|
|
3529
|
-
name: { type: "string" },
|
|
3530
|
-
type: { type: "string" },
|
|
3531
|
-
rules: { type: "array", items: { type: "string" } }
|
|
3532
|
-
}
|
|
3533
|
-
}
|
|
3534
|
-
},
|
|
3535
|
-
endpoints: {
|
|
3536
|
-
type: "object",
|
|
3537
|
-
properties: {
|
|
3538
|
-
list: { type: "string" },
|
|
3539
|
-
get: { type: "string" },
|
|
3540
|
-
create: { type: "string" },
|
|
3541
|
-
update: { type: "string" },
|
|
3542
|
-
delete: { type: "string" }
|
|
3543
|
-
}
|
|
3544
|
-
},
|
|
3545
|
-
queryParameters: { type: "object" }
|
|
3546
|
-
}
|
|
3547
|
-
}
|
|
3548
|
-
}
|
|
3549
|
-
}
|
|
3550
|
-
}
|
|
3551
|
-
}
|
|
3552
|
-
};
|
|
3553
|
-
if (!paths[`${basePath}/{id}`]) paths[`${basePath}/{id}`] = {};
|
|
3554
|
-
paths[`${basePath}/{id}`].options = {
|
|
3555
|
-
tags: [resourceName],
|
|
3556
|
-
summary: `Get allowed methods for ${resourceName} item`,
|
|
3557
|
-
description: `Get allowed HTTP methods for individual ${resourceName} operations`,
|
|
3558
|
-
parameters: [
|
|
3559
|
-
{
|
|
3560
|
-
name: "id",
|
|
3561
|
-
in: "path",
|
|
3562
|
-
required: true,
|
|
3563
|
-
schema: { type: "string" }
|
|
3564
|
-
}
|
|
3565
|
-
],
|
|
3566
|
-
responses: {
|
|
3567
|
-
204: {
|
|
3568
|
-
description: "Methods retrieved successfully",
|
|
3569
|
-
headers: {
|
|
3570
|
-
"Allow": {
|
|
3571
|
-
description: "Allowed HTTP methods",
|
|
3572
|
-
schema: { type: "string", example: "GET, PUT, PATCH, DELETE, HEAD, OPTIONS" }
|
|
3573
|
-
}
|
|
3574
|
-
}
|
|
3575
|
-
}
|
|
3576
|
-
}
|
|
3577
|
-
};
|
|
3578
|
-
}
|
|
3579
|
-
return paths;
|
|
3580
|
-
}
|
|
3581
|
-
function generateRelationalPaths(resource, relationName, relationConfig, version, relatedSchema) {
|
|
3582
|
-
const resourceName = resource.name;
|
|
3583
|
-
const basePath = `/${version}/${resourceName}/{id}/${relationName}`;
|
|
3584
|
-
relationConfig.resource;
|
|
3585
|
-
const isToMany = relationConfig.type === "hasMany" || relationConfig.type === "belongsToMany";
|
|
3586
|
-
const paths = {};
|
|
3587
|
-
paths[basePath] = {
|
|
3588
|
-
get: {
|
|
3589
|
-
tags: [resourceName],
|
|
3590
|
-
summary: `Get ${relationName} of ${resourceName}`,
|
|
3591
|
-
description: `Retrieve ${relationName} (${relationConfig.type}) associated with this ${resourceName}. This endpoint uses the RelationPlugin to efficiently load related data` + (relationConfig.partitionHint ? ` via the '${relationConfig.partitionHint}' partition.` : "."),
|
|
3592
|
-
parameters: [
|
|
3593
|
-
{
|
|
3594
|
-
name: "id",
|
|
3595
|
-
in: "path",
|
|
3596
|
-
required: true,
|
|
3597
|
-
description: `${resourceName} ID`,
|
|
3598
|
-
schema: { type: "string" }
|
|
3599
|
-
},
|
|
3600
|
-
...isToMany ? [
|
|
3601
|
-
{
|
|
3602
|
-
name: "limit",
|
|
3603
|
-
in: "query",
|
|
3604
|
-
description: "Maximum number of items to return",
|
|
3605
|
-
schema: { type: "integer", default: 100, minimum: 1, maximum: 1e3 }
|
|
3606
|
-
},
|
|
3607
|
-
{
|
|
3608
|
-
name: "offset",
|
|
3609
|
-
in: "query",
|
|
3610
|
-
description: "Number of items to skip",
|
|
3611
|
-
schema: { type: "integer", default: 0, minimum: 0 }
|
|
3612
|
-
}
|
|
3613
|
-
] : []
|
|
3614
|
-
],
|
|
3615
|
-
responses: {
|
|
3616
|
-
200: {
|
|
3617
|
-
description: "Successful response",
|
|
3618
|
-
content: {
|
|
3619
|
-
"application/json": {
|
|
3620
|
-
schema: isToMany ? {
|
|
3621
|
-
type: "object",
|
|
3622
|
-
properties: {
|
|
3623
|
-
success: { type: "boolean", example: true },
|
|
3624
|
-
data: {
|
|
3625
|
-
type: "array",
|
|
3626
|
-
items: relatedSchema
|
|
3627
|
-
},
|
|
3628
|
-
pagination: {
|
|
3629
|
-
type: "object",
|
|
3630
|
-
properties: {
|
|
3631
|
-
total: { type: "integer" },
|
|
3632
|
-
page: { type: "integer" },
|
|
3633
|
-
pageSize: { type: "integer" },
|
|
3634
|
-
pageCount: { type: "integer" }
|
|
3635
|
-
}
|
|
3636
|
-
}
|
|
3637
|
-
}
|
|
3638
|
-
} : {
|
|
3639
|
-
type: "object",
|
|
3640
|
-
properties: {
|
|
3641
|
-
success: { type: "boolean", example: true },
|
|
3642
|
-
data: relatedSchema
|
|
3643
|
-
}
|
|
3644
|
-
}
|
|
3645
|
-
}
|
|
3646
|
-
},
|
|
3647
|
-
...isToMany ? {
|
|
3648
|
-
headers: {
|
|
3649
|
-
"X-Total-Count": {
|
|
3650
|
-
description: "Total number of related records",
|
|
3651
|
-
schema: { type: "integer" }
|
|
3652
|
-
},
|
|
3653
|
-
"X-Page-Count": {
|
|
3654
|
-
description: "Total number of pages",
|
|
3655
|
-
schema: { type: "integer" }
|
|
3656
|
-
}
|
|
3657
|
-
}
|
|
3658
|
-
} : {}
|
|
3659
|
-
},
|
|
3660
|
-
404: {
|
|
3661
|
-
description: `${resourceName} not found` + (isToMany ? "" : " or no related resource exists"),
|
|
3662
|
-
content: {
|
|
3663
|
-
"application/json": {
|
|
3664
|
-
schema: { $ref: "#/components/schemas/Error" }
|
|
3665
|
-
}
|
|
3666
|
-
}
|
|
3667
|
-
}
|
|
3668
|
-
}
|
|
3669
|
-
}
|
|
3670
|
-
};
|
|
3671
|
-
return paths;
|
|
3672
|
-
}
|
|
3673
|
-
function generateOpenAPISpec(database, config = {}) {
|
|
3674
|
-
const {
|
|
3675
|
-
title = "s3db.js API",
|
|
3676
|
-
version = "1.0.0",
|
|
3677
|
-
description = "Auto-generated REST API documentation for s3db.js resources",
|
|
3678
|
-
serverUrl = "http://localhost:3000",
|
|
3679
|
-
auth = {},
|
|
3680
|
-
resources: resourceConfigs = {}
|
|
3681
|
-
} = config;
|
|
3682
|
-
const resourcesTableRows = [];
|
|
3683
|
-
for (const [name, resource] of Object.entries(database.resources)) {
|
|
3684
|
-
if (name.startsWith("plg_") && !resourceConfigs[name]) {
|
|
3685
|
-
continue;
|
|
3686
|
-
}
|
|
3687
|
-
const version2 = resource.config?.currentVersion || resource.version || "v1";
|
|
3688
|
-
const resourceDescription = resource.config?.description;
|
|
3689
|
-
const descText = typeof resourceDescription === "object" ? resourceDescription.resource : resourceDescription || "No description";
|
|
3690
|
-
resourcesTableRows.push(`| ${name} | ${descText} | \`/${version2}/${name}\` |`);
|
|
3691
|
-
}
|
|
3692
|
-
const enhancedDescription = `${description}
|
|
3693
|
-
|
|
3694
|
-
## Available Resources
|
|
3695
|
-
|
|
3696
|
-
| Resource | Description | Base Path |
|
|
3697
|
-
|----------|-------------|-----------|
|
|
3698
|
-
${resourcesTableRows.join("\n")}
|
|
3699
|
-
|
|
3700
|
-
---
|
|
3701
|
-
|
|
3702
|
-
For detailed information about each endpoint, see the sections below.`;
|
|
3703
|
-
const spec = {
|
|
3704
|
-
openapi: "3.1.0",
|
|
3705
|
-
info: {
|
|
3706
|
-
title,
|
|
3707
|
-
version,
|
|
3708
|
-
description: enhancedDescription,
|
|
3709
|
-
contact: {
|
|
3710
|
-
name: "s3db.js",
|
|
3711
|
-
url: "https://github.com/forattini-dev/s3db.js"
|
|
3712
|
-
}
|
|
3713
|
-
},
|
|
3714
|
-
servers: [
|
|
3715
|
-
{
|
|
3716
|
-
url: serverUrl,
|
|
3717
|
-
description: "API Server"
|
|
3718
|
-
}
|
|
3719
|
-
],
|
|
3720
|
-
paths: {},
|
|
3721
|
-
components: {
|
|
3722
|
-
schemas: {
|
|
3723
|
-
Error: {
|
|
3724
|
-
type: "object",
|
|
3725
|
-
properties: {
|
|
3726
|
-
success: { type: "boolean", example: false },
|
|
3727
|
-
error: {
|
|
3728
|
-
type: "object",
|
|
3729
|
-
properties: {
|
|
3730
|
-
message: { type: "string" },
|
|
3731
|
-
code: { type: "string" },
|
|
3732
|
-
details: { type: "object" }
|
|
3733
|
-
}
|
|
3734
|
-
}
|
|
3735
|
-
}
|
|
3736
|
-
},
|
|
3737
|
-
ValidationError: {
|
|
3738
|
-
type: "object",
|
|
3739
|
-
properties: {
|
|
3740
|
-
success: { type: "boolean", example: false },
|
|
3741
|
-
error: {
|
|
3742
|
-
type: "object",
|
|
3743
|
-
properties: {
|
|
3744
|
-
message: { type: "string", example: "Validation failed" },
|
|
3745
|
-
code: { type: "string", example: "VALIDATION_ERROR" },
|
|
3746
|
-
details: {
|
|
3747
|
-
type: "object",
|
|
3748
|
-
properties: {
|
|
3749
|
-
errors: {
|
|
3750
|
-
type: "array",
|
|
3751
|
-
items: {
|
|
3752
|
-
type: "object",
|
|
3753
|
-
properties: {
|
|
3754
|
-
field: { type: "string" },
|
|
3755
|
-
message: { type: "string" },
|
|
3756
|
-
expected: { type: "string" },
|
|
3757
|
-
actual: {}
|
|
3758
|
-
}
|
|
3759
|
-
}
|
|
3760
|
-
}
|
|
3761
|
-
}
|
|
3762
|
-
}
|
|
3763
|
-
}
|
|
3764
|
-
}
|
|
3765
|
-
}
|
|
3766
|
-
}
|
|
3767
|
-
},
|
|
3768
|
-
securitySchemes: {}
|
|
3769
|
-
},
|
|
3770
|
-
tags: []
|
|
3771
|
-
};
|
|
3772
|
-
if (auth.jwt?.enabled) {
|
|
3773
|
-
spec.components.securitySchemes.bearerAuth = {
|
|
3774
|
-
type: "http",
|
|
3775
|
-
scheme: "bearer",
|
|
3776
|
-
bearerFormat: "JWT",
|
|
3777
|
-
description: "JWT authentication"
|
|
3778
|
-
};
|
|
3779
|
-
}
|
|
3780
|
-
if (auth.apiKey?.enabled) {
|
|
3781
|
-
spec.components.securitySchemes.apiKeyAuth = {
|
|
3782
|
-
type: "apiKey",
|
|
3783
|
-
in: "header",
|
|
3784
|
-
name: auth.apiKey.headerName || "X-API-Key",
|
|
3785
|
-
description: "API Key authentication"
|
|
3786
|
-
};
|
|
3787
|
-
}
|
|
3788
|
-
if (auth.basic?.enabled) {
|
|
3789
|
-
spec.components.securitySchemes.basicAuth = {
|
|
3790
|
-
type: "http",
|
|
3791
|
-
scheme: "basic",
|
|
3792
|
-
description: "HTTP Basic authentication"
|
|
3793
|
-
};
|
|
3794
|
-
}
|
|
3795
|
-
const resources = database.resources;
|
|
3796
|
-
const relationsPlugin = database.plugins?.relation || database.plugins?.RelationPlugin || null;
|
|
3797
|
-
for (const [name, resource] of Object.entries(resources)) {
|
|
3798
|
-
if (name.startsWith("plg_") && !resourceConfigs[name]) {
|
|
3799
|
-
continue;
|
|
3800
|
-
}
|
|
3801
|
-
const config2 = resourceConfigs[name] || {
|
|
3802
|
-
methods: ["GET", "POST", "PUT", "PATCH", "DELETE"],
|
|
3803
|
-
auth: false
|
|
3804
|
-
};
|
|
3805
|
-
const version2 = resource.config?.currentVersion || resource.version || "v1";
|
|
3806
|
-
const paths = generateResourcePaths(resource, version2, config2);
|
|
3807
|
-
Object.assign(spec.paths, paths);
|
|
3808
|
-
const resourceDescription = resource.config?.description;
|
|
3809
|
-
const tagDescription = typeof resourceDescription === "object" ? resourceDescription.resource : resourceDescription || `Operations for ${name} resource`;
|
|
3810
|
-
spec.tags.push({
|
|
3811
|
-
name,
|
|
3812
|
-
description: tagDescription
|
|
3813
|
-
});
|
|
3814
|
-
spec.components.schemas[name] = generateResourceSchema(resource);
|
|
3815
|
-
if (relationsPlugin && relationsPlugin.relations && relationsPlugin.relations[name]) {
|
|
3816
|
-
const relationsDef = relationsPlugin.relations[name];
|
|
3817
|
-
for (const [relationName, relationConfig] of Object.entries(relationsDef)) {
|
|
3818
|
-
if (relationConfig.type === "belongsTo") {
|
|
3819
|
-
continue;
|
|
3820
|
-
}
|
|
3821
|
-
const exposeRelation = config2?.relations?.[relationName]?.expose !== false;
|
|
3822
|
-
if (!exposeRelation) {
|
|
3823
|
-
continue;
|
|
3824
|
-
}
|
|
3825
|
-
const relatedResource = database.resources[relationConfig.resource];
|
|
3826
|
-
if (!relatedResource) {
|
|
3827
|
-
continue;
|
|
3828
|
-
}
|
|
3829
|
-
const relatedSchema = generateResourceSchema(relatedResource);
|
|
3830
|
-
const relationalPaths = generateRelationalPaths(
|
|
3831
|
-
resource,
|
|
3832
|
-
relationName,
|
|
3833
|
-
relationConfig,
|
|
3834
|
-
version2,
|
|
3835
|
-
relatedSchema
|
|
3836
|
-
);
|
|
3837
|
-
Object.assign(spec.paths, relationalPaths);
|
|
3838
|
-
}
|
|
3839
|
-
}
|
|
3840
|
-
}
|
|
3841
|
-
if (auth.jwt?.enabled || auth.apiKey?.enabled || auth.basic?.enabled) {
|
|
3842
|
-
spec.paths["/auth/login"] = {
|
|
3843
|
-
post: {
|
|
3844
|
-
tags: ["Authentication"],
|
|
3845
|
-
summary: "Login",
|
|
3846
|
-
description: "Authenticate with username and password",
|
|
3847
|
-
requestBody: {
|
|
3848
|
-
required: true,
|
|
3849
|
-
content: {
|
|
3850
|
-
"application/json": {
|
|
3851
|
-
schema: {
|
|
3852
|
-
type: "object",
|
|
3853
|
-
properties: {
|
|
3854
|
-
username: { type: "string" },
|
|
3855
|
-
password: { type: "string", format: "password" }
|
|
3856
|
-
},
|
|
3857
|
-
required: ["username", "password"]
|
|
3858
|
-
}
|
|
3859
|
-
}
|
|
3860
|
-
}
|
|
3861
|
-
},
|
|
3862
|
-
responses: {
|
|
3863
|
-
200: {
|
|
3864
|
-
description: "Login successful",
|
|
3865
|
-
content: {
|
|
3866
|
-
"application/json": {
|
|
3867
|
-
schema: {
|
|
3868
|
-
type: "object",
|
|
3869
|
-
properties: {
|
|
3870
|
-
success: { type: "boolean", example: true },
|
|
3871
|
-
data: {
|
|
3872
|
-
type: "object",
|
|
3873
|
-
properties: {
|
|
3874
|
-
token: { type: "string" },
|
|
3875
|
-
user: { type: "object" }
|
|
3876
|
-
}
|
|
3877
|
-
}
|
|
3878
|
-
}
|
|
3879
|
-
}
|
|
3880
|
-
}
|
|
3881
|
-
}
|
|
3882
|
-
},
|
|
3883
|
-
401: {
|
|
3884
|
-
description: "Invalid credentials",
|
|
3885
|
-
content: {
|
|
3886
|
-
"application/json": {
|
|
3887
|
-
schema: { $ref: "#/components/schemas/Error" }
|
|
3888
|
-
}
|
|
3889
|
-
}
|
|
3890
|
-
}
|
|
3891
|
-
}
|
|
3892
|
-
}
|
|
3893
|
-
};
|
|
3894
|
-
spec.paths["/auth/register"] = {
|
|
3895
|
-
post: {
|
|
3896
|
-
tags: ["Authentication"],
|
|
3897
|
-
summary: "Register",
|
|
3898
|
-
description: "Register a new user",
|
|
3899
|
-
requestBody: {
|
|
3900
|
-
required: true,
|
|
3901
|
-
content: {
|
|
3902
|
-
"application/json": {
|
|
3903
|
-
schema: {
|
|
3904
|
-
type: "object",
|
|
3905
|
-
properties: {
|
|
3906
|
-
username: { type: "string", minLength: 3 },
|
|
3907
|
-
password: { type: "string", format: "password", minLength: 8 },
|
|
3908
|
-
email: { type: "string", format: "email" }
|
|
3909
|
-
},
|
|
3910
|
-
required: ["username", "password"]
|
|
3911
|
-
}
|
|
3912
|
-
}
|
|
3913
|
-
}
|
|
3914
|
-
},
|
|
3915
|
-
responses: {
|
|
3916
|
-
201: {
|
|
3917
|
-
description: "User registered successfully",
|
|
3918
|
-
content: {
|
|
3919
|
-
"application/json": {
|
|
3920
|
-
schema: {
|
|
3921
|
-
type: "object",
|
|
3922
|
-
properties: {
|
|
3923
|
-
success: { type: "boolean", example: true },
|
|
3924
|
-
data: {
|
|
3925
|
-
type: "object",
|
|
3926
|
-
properties: {
|
|
3927
|
-
token: { type: "string" },
|
|
3928
|
-
user: { type: "object" }
|
|
3929
|
-
}
|
|
3930
|
-
}
|
|
3931
|
-
}
|
|
3932
|
-
}
|
|
3933
|
-
}
|
|
3934
|
-
}
|
|
3935
|
-
}
|
|
3936
|
-
}
|
|
3937
|
-
}
|
|
3938
|
-
};
|
|
3939
|
-
spec.tags.push({
|
|
3940
|
-
name: "Authentication",
|
|
3941
|
-
description: "Authentication endpoints"
|
|
3942
|
-
});
|
|
3943
|
-
}
|
|
3944
|
-
spec.paths["/health"] = {
|
|
3945
|
-
get: {
|
|
3946
|
-
tags: ["Health"],
|
|
3947
|
-
summary: "Generic Health Check",
|
|
3948
|
-
description: "Generic health check endpoint that includes references to liveness and readiness probes",
|
|
3949
|
-
responses: {
|
|
3950
|
-
200: {
|
|
3951
|
-
description: "API is healthy",
|
|
3952
|
-
content: {
|
|
3953
|
-
"application/json": {
|
|
3954
|
-
schema: {
|
|
3955
|
-
type: "object",
|
|
3956
|
-
properties: {
|
|
3957
|
-
success: { type: "boolean", example: true },
|
|
3958
|
-
data: {
|
|
3959
|
-
type: "object",
|
|
3960
|
-
properties: {
|
|
3961
|
-
status: { type: "string", example: "ok" },
|
|
3962
|
-
uptime: { type: "number", description: "Process uptime in seconds" },
|
|
3963
|
-
timestamp: { type: "string", format: "date-time" },
|
|
3964
|
-
checks: {
|
|
3965
|
-
type: "object",
|
|
3966
|
-
properties: {
|
|
3967
|
-
liveness: { type: "string", example: "/health/live" },
|
|
3968
|
-
readiness: { type: "string", example: "/health/ready" }
|
|
3969
|
-
}
|
|
3970
|
-
}
|
|
3971
|
-
}
|
|
3972
|
-
}
|
|
3973
|
-
}
|
|
3974
|
-
}
|
|
3975
|
-
}
|
|
3976
|
-
}
|
|
3977
|
-
}
|
|
3978
|
-
}
|
|
3979
|
-
}
|
|
3980
|
-
};
|
|
3981
|
-
spec.paths["/health/live"] = {
|
|
3982
|
-
get: {
|
|
3983
|
-
tags: ["Health"],
|
|
3984
|
-
summary: "Liveness Probe",
|
|
3985
|
-
description: "Kubernetes liveness probe - checks if the application is alive. If this fails, Kubernetes will restart the pod.",
|
|
3986
|
-
responses: {
|
|
3987
|
-
200: {
|
|
3988
|
-
description: "Application is alive",
|
|
3989
|
-
content: {
|
|
3990
|
-
"application/json": {
|
|
3991
|
-
schema: {
|
|
3992
|
-
type: "object",
|
|
3993
|
-
properties: {
|
|
3994
|
-
success: { type: "boolean", example: true },
|
|
3995
|
-
data: {
|
|
3996
|
-
type: "object",
|
|
3997
|
-
properties: {
|
|
3998
|
-
status: { type: "string", example: "alive" },
|
|
3999
|
-
timestamp: { type: "string", format: "date-time" }
|
|
4000
|
-
}
|
|
4001
|
-
}
|
|
4002
|
-
}
|
|
4003
|
-
}
|
|
4004
|
-
}
|
|
4005
|
-
}
|
|
4006
|
-
}
|
|
4007
|
-
}
|
|
4008
|
-
}
|
|
4009
|
-
};
|
|
4010
|
-
spec.paths["/health/ready"] = {
|
|
4011
|
-
get: {
|
|
4012
|
-
tags: ["Health"],
|
|
4013
|
-
summary: "Readiness Probe",
|
|
4014
|
-
description: "Kubernetes readiness probe - checks if the application is ready to receive traffic. If this fails, Kubernetes will remove the pod from service endpoints.",
|
|
4015
|
-
responses: {
|
|
4016
|
-
200: {
|
|
4017
|
-
description: "Application is ready to receive traffic",
|
|
4018
|
-
content: {
|
|
4019
|
-
"application/json": {
|
|
4020
|
-
schema: {
|
|
4021
|
-
type: "object",
|
|
4022
|
-
properties: {
|
|
4023
|
-
success: { type: "boolean", example: true },
|
|
4024
|
-
data: {
|
|
4025
|
-
type: "object",
|
|
4026
|
-
properties: {
|
|
4027
|
-
status: { type: "string", example: "ready" },
|
|
4028
|
-
database: {
|
|
4029
|
-
type: "object",
|
|
4030
|
-
properties: {
|
|
4031
|
-
connected: { type: "boolean", example: true },
|
|
4032
|
-
resources: { type: "integer", example: 5 }
|
|
4033
|
-
}
|
|
4034
|
-
},
|
|
4035
|
-
timestamp: { type: "string", format: "date-time" }
|
|
4036
|
-
}
|
|
4037
|
-
}
|
|
4038
|
-
}
|
|
4039
|
-
}
|
|
4040
|
-
}
|
|
4041
|
-
}
|
|
4042
|
-
},
|
|
4043
|
-
503: {
|
|
4044
|
-
description: "Application is not ready",
|
|
4045
|
-
content: {
|
|
4046
|
-
"application/json": {
|
|
4047
|
-
schema: {
|
|
4048
|
-
type: "object",
|
|
4049
|
-
properties: {
|
|
4050
|
-
success: { type: "boolean", example: false },
|
|
4051
|
-
error: {
|
|
4052
|
-
type: "object",
|
|
4053
|
-
properties: {
|
|
4054
|
-
message: { type: "string", example: "Service not ready" },
|
|
4055
|
-
code: { type: "string", example: "NOT_READY" },
|
|
4056
|
-
details: {
|
|
4057
|
-
type: "object",
|
|
4058
|
-
properties: {
|
|
4059
|
-
database: {
|
|
4060
|
-
type: "object",
|
|
4061
|
-
properties: {
|
|
4062
|
-
connected: { type: "boolean", example: false },
|
|
4063
|
-
resources: { type: "integer", example: 0 }
|
|
4064
|
-
}
|
|
4065
|
-
}
|
|
4066
|
-
}
|
|
4067
|
-
}
|
|
4068
|
-
}
|
|
4069
|
-
}
|
|
4070
|
-
}
|
|
4071
|
-
}
|
|
4072
|
-
}
|
|
4073
|
-
}
|
|
4074
|
-
}
|
|
4075
|
-
}
|
|
4076
|
-
}
|
|
4077
|
-
};
|
|
4078
|
-
spec.tags.push({
|
|
4079
|
-
name: "Health",
|
|
4080
|
-
description: "Health check endpoints for monitoring and Kubernetes probes"
|
|
4081
|
-
});
|
|
4082
|
-
const metricsPlugin = database.plugins?.metrics || database.plugins?.MetricsPlugin;
|
|
4083
|
-
if (metricsPlugin && metricsPlugin.config?.prometheus?.enabled) {
|
|
4084
|
-
const metricsPath = metricsPlugin.config.prometheus.path || "/metrics";
|
|
4085
|
-
const isIntegrated = metricsPlugin.config.prometheus.mode !== "standalone";
|
|
4086
|
-
if (isIntegrated) {
|
|
4087
|
-
spec.paths[metricsPath] = {
|
|
4088
|
-
get: {
|
|
4089
|
-
tags: ["Monitoring"],
|
|
4090
|
-
summary: "Prometheus Metrics",
|
|
4091
|
-
description: "Exposes application metrics in Prometheus text-based exposition format for monitoring and observability. Metrics include operation counts, durations, errors, uptime, and resource statistics.",
|
|
4092
|
-
responses: {
|
|
4093
|
-
200: {
|
|
4094
|
-
description: "Metrics in Prometheus format",
|
|
4095
|
-
content: {
|
|
4096
|
-
"text/plain": {
|
|
4097
|
-
schema: {
|
|
4098
|
-
type: "string",
|
|
4099
|
-
example: '# HELP s3db_operations_total Total number of operations by type and resource\n# TYPE s3db_operations_total counter\ns3db_operations_total{operation="insert",resource="cars"} 1523\ns3db_operations_total{operation="update",resource="cars"} 342\n\n# HELP s3db_operation_duration_seconds Average operation duration in seconds\n# TYPE s3db_operation_duration_seconds gauge\ns3db_operation_duration_seconds{operation="insert",resource="cars"} 0.045\n\n# HELP s3db_operation_errors_total Total number of operation errors\n# TYPE s3db_operation_errors_total counter\ns3db_operation_errors_total{operation="insert",resource="cars"} 12\n'
|
|
4100
|
-
}
|
|
4101
|
-
}
|
|
4102
|
-
}
|
|
4103
|
-
}
|
|
4104
|
-
}
|
|
4105
|
-
}
|
|
4106
|
-
};
|
|
4107
|
-
spec.tags.push({
|
|
4108
|
-
name: "Monitoring",
|
|
4109
|
-
description: "Monitoring and observability endpoints (Prometheus)"
|
|
4110
|
-
});
|
|
4111
|
-
}
|
|
4112
|
-
}
|
|
4113
|
-
return spec;
|
|
4114
|
-
}
|
|
4115
|
-
|
|
4116
|
-
class ApiServer {
|
|
4117
|
-
/**
|
|
4118
|
-
* Create API server
|
|
4119
|
-
* @param {Object} options - Server options
|
|
4120
|
-
* @param {number} options.port - Server port
|
|
4121
|
-
* @param {string} options.host - Server host
|
|
4122
|
-
* @param {Object} options.database - s3db.js database instance
|
|
4123
|
-
* @param {Object} options.resources - Resource configuration
|
|
4124
|
-
* @param {Array} options.middlewares - Global middlewares
|
|
4125
|
-
*/
|
|
4126
|
-
constructor(options = {}) {
|
|
4127
|
-
this.options = {
|
|
4128
|
-
port: options.port || 3e3,
|
|
4129
|
-
host: options.host || "0.0.0.0",
|
|
4130
|
-
database: options.database,
|
|
4131
|
-
resources: options.resources || {},
|
|
4132
|
-
middlewares: options.middlewares || [],
|
|
4133
|
-
verbose: options.verbose || false,
|
|
4134
|
-
auth: options.auth || {},
|
|
4135
|
-
docsEnabled: options.docsEnabled !== false,
|
|
4136
|
-
// Enable /docs by default
|
|
4137
|
-
docsUI: options.docsUI || "redoc",
|
|
4138
|
-
// 'swagger' or 'redoc'
|
|
4139
|
-
maxBodySize: options.maxBodySize || 10 * 1024 * 1024,
|
|
4140
|
-
// 10MB default
|
|
4141
|
-
rootHandler: options.rootHandler,
|
|
4142
|
-
// Custom handler for root path, if not provided redirects to /docs
|
|
4143
|
-
apiInfo: {
|
|
4144
|
-
title: options.apiTitle || "s3db.js API",
|
|
4145
|
-
version: options.apiVersion || "1.0.0",
|
|
4146
|
-
description: options.apiDescription || "Auto-generated REST API for s3db.js resources"
|
|
4147
|
-
}
|
|
4148
|
-
};
|
|
4149
|
-
this.app = new hono.Hono();
|
|
4150
|
-
this.server = null;
|
|
4151
|
-
this.isRunning = false;
|
|
4152
|
-
this.openAPISpec = null;
|
|
4153
|
-
this.relationsPlugin = this.options.database?.plugins?.relation || this.options.database?.plugins?.RelationPlugin || null;
|
|
4154
|
-
this._setupRoutes();
|
|
4155
|
-
}
|
|
4156
|
-
/**
|
|
4157
|
-
* Setup all routes
|
|
4158
|
-
* @private
|
|
4159
|
-
*/
|
|
4160
|
-
_setupRoutes() {
|
|
4161
|
-
this.options.middlewares.forEach((middleware) => {
|
|
4162
|
-
this.app.use("*", middleware);
|
|
4163
|
-
});
|
|
4164
|
-
this.app.use("*", async (c, next) => {
|
|
4165
|
-
const method = c.req.method;
|
|
4166
|
-
if (["POST", "PUT", "PATCH"].includes(method)) {
|
|
4167
|
-
const contentLength = c.req.header("content-length");
|
|
4168
|
-
if (contentLength) {
|
|
4169
|
-
const size = parseInt(contentLength);
|
|
4170
|
-
if (size > this.options.maxBodySize) {
|
|
4171
|
-
const response = payloadTooLarge(size, this.options.maxBodySize);
|
|
4172
|
-
c.header("Connection", "close");
|
|
4173
|
-
return c.json(response, response._status);
|
|
4174
|
-
}
|
|
4175
|
-
}
|
|
4176
|
-
}
|
|
4177
|
-
await next();
|
|
4178
|
-
});
|
|
4179
|
-
this.app.get("/health/live", (c) => {
|
|
4180
|
-
const response = success({
|
|
4181
|
-
status: "alive",
|
|
4182
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
4183
|
-
});
|
|
4184
|
-
return c.json(response);
|
|
4185
|
-
});
|
|
4186
|
-
this.app.get("/health/ready", (c) => {
|
|
4187
|
-
const isReady = this.options.database && this.options.database.connected && Object.keys(this.options.database.resources).length > 0;
|
|
4188
|
-
if (!isReady) {
|
|
4189
|
-
const response2 = error("Service not ready", {
|
|
4190
|
-
status: 503,
|
|
4191
|
-
code: "NOT_READY",
|
|
4192
|
-
details: {
|
|
4193
|
-
database: {
|
|
4194
|
-
connected: this.options.database?.connected || false,
|
|
4195
|
-
resources: Object.keys(this.options.database?.resources || {}).length
|
|
4196
|
-
}
|
|
4197
|
-
}
|
|
4198
|
-
});
|
|
4199
|
-
return c.json(response2, 503);
|
|
4200
|
-
}
|
|
4201
|
-
const response = success({
|
|
4202
|
-
status: "ready",
|
|
4203
|
-
database: {
|
|
4204
|
-
connected: true,
|
|
4205
|
-
resources: Object.keys(this.options.database.resources).length
|
|
4206
|
-
},
|
|
4207
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
4208
|
-
});
|
|
4209
|
-
return c.json(response);
|
|
4210
|
-
});
|
|
4211
|
-
this.app.get("/health", (c) => {
|
|
4212
|
-
const response = success({
|
|
4213
|
-
status: "ok",
|
|
4214
|
-
uptime: process.uptime(),
|
|
4215
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
4216
|
-
checks: {
|
|
4217
|
-
liveness: "/health/live",
|
|
4218
|
-
readiness: "/health/ready"
|
|
4219
|
-
}
|
|
4220
|
-
});
|
|
4221
|
-
return c.json(response);
|
|
4222
|
-
});
|
|
4223
|
-
this.app.get("/", (c) => {
|
|
4224
|
-
if (this.options.rootHandler) {
|
|
4225
|
-
return this.options.rootHandler(c);
|
|
4226
|
-
}
|
|
4227
|
-
return c.redirect("/docs", 302);
|
|
4228
|
-
});
|
|
4229
|
-
if (this.options.docsEnabled) {
|
|
4230
|
-
this.app.get("/openapi.json", (c) => {
|
|
4231
|
-
if (!this.openAPISpec) {
|
|
4232
|
-
this.openAPISpec = this._generateOpenAPISpec();
|
|
4233
|
-
}
|
|
4234
|
-
return c.json(this.openAPISpec);
|
|
4235
|
-
});
|
|
4236
|
-
if (this.options.docsUI === "swagger") {
|
|
4237
|
-
this.app.get("/docs", swaggerUi.swaggerUI({
|
|
4238
|
-
url: "/openapi.json"
|
|
4239
|
-
}));
|
|
4240
|
-
} else {
|
|
4241
|
-
this.app.get("/docs", (c) => {
|
|
4242
|
-
return c.html(`<!DOCTYPE html>
|
|
4243
|
-
<html lang="en">
|
|
4244
|
-
<head>
|
|
4245
|
-
<meta charset="UTF-8">
|
|
4246
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
4247
|
-
<title>${this.options.apiInfo.title} - API Documentation</title>
|
|
4248
|
-
<style>
|
|
4249
|
-
body {
|
|
4250
|
-
margin: 0;
|
|
4251
|
-
padding: 0;
|
|
4252
|
-
}
|
|
4253
|
-
</style>
|
|
4254
|
-
</head>
|
|
4255
|
-
<body>
|
|
4256
|
-
<redoc spec-url="/openapi.json"></redoc>
|
|
4257
|
-
<script src="https://cdn.redoc.ly/redoc/v2.5.1/bundles/redoc.standalone.js"><\/script>
|
|
4258
|
-
</body>
|
|
4259
|
-
</html>`);
|
|
4260
|
-
});
|
|
4261
|
-
}
|
|
4262
|
-
}
|
|
4263
|
-
this._setupResourceRoutes();
|
|
4264
|
-
if (this.relationsPlugin) {
|
|
4265
|
-
this._setupRelationalRoutes();
|
|
4266
|
-
}
|
|
4267
|
-
this.app.onError((err, c) => {
|
|
4268
|
-
return errorHandler(err, c);
|
|
4269
|
-
});
|
|
4270
|
-
this.app.notFound((c) => {
|
|
4271
|
-
const response = error("Route not found", {
|
|
4272
|
-
status: 404,
|
|
4273
|
-
code: "NOT_FOUND",
|
|
4274
|
-
details: {
|
|
4275
|
-
path: c.req.path,
|
|
4276
|
-
method: c.req.method
|
|
4277
|
-
}
|
|
4278
|
-
});
|
|
4279
|
-
return c.json(response, 404);
|
|
4280
|
-
});
|
|
4281
|
-
}
|
|
4282
|
-
/**
|
|
4283
|
-
* Setup routes for all resources
|
|
4284
|
-
* @private
|
|
4285
|
-
*/
|
|
4286
|
-
_setupResourceRoutes() {
|
|
4287
|
-
const { database, resources: resourceConfigs } = this.options;
|
|
4288
|
-
const resources = database.resources;
|
|
4289
|
-
for (const [name, resource] of Object.entries(resources)) {
|
|
4290
|
-
if (name.startsWith("plg_") && !resourceConfigs[name]) {
|
|
4291
|
-
continue;
|
|
4292
|
-
}
|
|
4293
|
-
const config = resourceConfigs[name] || {
|
|
4294
|
-
methods: ["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"]};
|
|
4295
|
-
const version = resource.config?.currentVersion || resource.version || "v1";
|
|
4296
|
-
const resourceApp = createResourceRoutes(resource, version, {
|
|
4297
|
-
methods: config.methods,
|
|
4298
|
-
customMiddleware: config.customMiddleware || [],
|
|
4299
|
-
enableValidation: config.validation !== false
|
|
4300
|
-
});
|
|
4301
|
-
this.app.route(`/${version}/${name}`, resourceApp);
|
|
4302
|
-
if (this.options.verbose) {
|
|
4303
|
-
console.log(`[API Plugin] Mounted routes for resource '${name}' at /${version}/${name}`);
|
|
4304
|
-
}
|
|
4305
|
-
}
|
|
4306
|
-
}
|
|
4307
|
-
/**
|
|
4308
|
-
* Setup relational routes (when RelationPlugin is active)
|
|
4309
|
-
* @private
|
|
4310
|
-
*/
|
|
4311
|
-
_setupRelationalRoutes() {
|
|
4312
|
-
if (!this.relationsPlugin || !this.relationsPlugin.relations) {
|
|
4313
|
-
return;
|
|
4314
|
-
}
|
|
4315
|
-
const { database } = this.options;
|
|
4316
|
-
const relations = this.relationsPlugin.relations;
|
|
4317
|
-
if (this.options.verbose) {
|
|
4318
|
-
console.log("[API Plugin] Setting up relational routes...");
|
|
4319
|
-
}
|
|
4320
|
-
for (const [resourceName, relationsDef] of Object.entries(relations)) {
|
|
4321
|
-
const resource = database.resources[resourceName];
|
|
4322
|
-
if (!resource) {
|
|
4323
|
-
if (this.options.verbose) {
|
|
4324
|
-
console.warn(`[API Plugin] Resource '${resourceName}' not found for relational routes`);
|
|
4325
|
-
}
|
|
4326
|
-
continue;
|
|
4327
|
-
}
|
|
4328
|
-
if (resourceName.startsWith("plg_") && !this.options.resources[resourceName]) {
|
|
4329
|
-
continue;
|
|
4330
|
-
}
|
|
4331
|
-
const version = resource.config?.currentVersion || resource.version || "v1";
|
|
4332
|
-
for (const [relationName, relationConfig] of Object.entries(relationsDef)) {
|
|
4333
|
-
if (relationConfig.type === "belongsTo") {
|
|
4334
|
-
continue;
|
|
4335
|
-
}
|
|
4336
|
-
const resourceConfig = this.options.resources[resourceName];
|
|
4337
|
-
const exposeRelation = resourceConfig?.relations?.[relationName]?.expose !== false;
|
|
4338
|
-
if (!exposeRelation) {
|
|
4339
|
-
continue;
|
|
4340
|
-
}
|
|
4341
|
-
const relationalApp = createRelationalRoutes(
|
|
4342
|
-
resource,
|
|
4343
|
-
relationName,
|
|
4344
|
-
relationConfig);
|
|
4345
|
-
this.app.route(`/${version}/${resourceName}/:id/${relationName}`, relationalApp);
|
|
4346
|
-
if (this.options.verbose) {
|
|
4347
|
-
console.log(
|
|
4348
|
-
`[API Plugin] Mounted relational route: /${version}/${resourceName}/:id/${relationName} (${relationConfig.type} -> ${relationConfig.resource})`
|
|
4349
|
-
);
|
|
4350
|
-
}
|
|
4351
|
-
}
|
|
4352
|
-
}
|
|
4353
|
-
}
|
|
4354
|
-
/**
|
|
4355
|
-
* Start the server
|
|
4356
|
-
* @returns {Promise<void>}
|
|
4357
|
-
*/
|
|
4358
|
-
async start() {
|
|
4359
|
-
if (this.isRunning) {
|
|
4360
|
-
console.warn("[API Plugin] Server is already running");
|
|
4361
|
-
return;
|
|
4362
|
-
}
|
|
4363
|
-
const { port, host } = this.options;
|
|
4364
|
-
return new Promise((resolve, reject) => {
|
|
4365
|
-
try {
|
|
4366
|
-
this.server = nodeServer.serve({
|
|
4367
|
-
fetch: this.app.fetch,
|
|
4368
|
-
port,
|
|
4369
|
-
hostname: host
|
|
4370
|
-
}, (info) => {
|
|
4371
|
-
this.isRunning = true;
|
|
4372
|
-
console.log(`[API Plugin] Server listening on http://${info.address}:${info.port}`);
|
|
4373
|
-
resolve();
|
|
4374
|
-
});
|
|
4375
|
-
} catch (err) {
|
|
4376
|
-
reject(err);
|
|
4377
|
-
}
|
|
4378
|
-
});
|
|
4379
|
-
}
|
|
4380
|
-
/**
|
|
4381
|
-
* Stop the server
|
|
4382
|
-
* @returns {Promise<void>}
|
|
4383
|
-
*/
|
|
4384
|
-
async stop() {
|
|
4385
|
-
if (!this.isRunning) {
|
|
4386
|
-
console.warn("[API Plugin] Server is not running");
|
|
4387
|
-
return;
|
|
4388
|
-
}
|
|
4389
|
-
if (this.server && typeof this.server.close === "function") {
|
|
4390
|
-
await new Promise((resolve) => {
|
|
4391
|
-
this.server.close(() => {
|
|
4392
|
-
this.isRunning = false;
|
|
4393
|
-
console.log("[API Plugin] Server stopped");
|
|
4394
|
-
resolve();
|
|
4395
|
-
});
|
|
4396
|
-
});
|
|
4397
|
-
} else {
|
|
4398
|
-
this.isRunning = false;
|
|
4399
|
-
console.log("[API Plugin] Server stopped");
|
|
4400
|
-
}
|
|
4401
|
-
}
|
|
4402
|
-
/**
|
|
4403
|
-
* Get server info
|
|
4404
|
-
* @returns {Object} Server information
|
|
4405
|
-
*/
|
|
4406
|
-
getInfo() {
|
|
4407
|
-
return {
|
|
4408
|
-
isRunning: this.isRunning,
|
|
4409
|
-
port: this.options.port,
|
|
4410
|
-
host: this.options.host,
|
|
4411
|
-
resources: Object.keys(this.options.database.resources).length
|
|
4412
|
-
};
|
|
4413
|
-
}
|
|
4414
|
-
/**
|
|
4415
|
-
* Get Hono app instance
|
|
4416
|
-
* @returns {Hono} Hono app
|
|
4417
|
-
*/
|
|
4418
|
-
getApp() {
|
|
4419
|
-
return this.app;
|
|
4420
|
-
}
|
|
4421
|
-
/**
|
|
4422
|
-
* Generate OpenAPI specification
|
|
4423
|
-
* @private
|
|
4424
|
-
* @returns {Object} OpenAPI spec
|
|
4425
|
-
*/
|
|
4426
|
-
_generateOpenAPISpec() {
|
|
4427
|
-
const { port, host, database, resources, auth, apiInfo } = this.options;
|
|
4428
|
-
return generateOpenAPISpec(database, {
|
|
4429
|
-
title: apiInfo.title,
|
|
4430
|
-
version: apiInfo.version,
|
|
4431
|
-
description: apiInfo.description,
|
|
4432
|
-
serverUrl: `http://${host === "0.0.0.0" ? "localhost" : host}:${port}`,
|
|
4433
|
-
auth,
|
|
4434
|
-
resources
|
|
4435
|
-
});
|
|
4436
|
-
}
|
|
4437
|
-
}
|
|
4438
|
-
|
|
4439
2474
|
const PLUGIN_DEPENDENCIES = {
|
|
4440
2475
|
"postgresql-replicator": {
|
|
4441
2476
|
name: "PostgreSQL Replicator",
|
|
@@ -4996,6 +3031,11 @@ class ApiPlugin extends Plugin {
|
|
|
4996
3031
|
if (this.config.verbose) {
|
|
4997
3032
|
console.log("[API Plugin] Starting server...");
|
|
4998
3033
|
}
|
|
3034
|
+
const serverPath = "./server.js";
|
|
3035
|
+
const { ApiServer } = await import(
|
|
3036
|
+
/* @vite-ignore */
|
|
3037
|
+
serverPath
|
|
3038
|
+
);
|
|
4999
3039
|
this.server = new ApiServer({
|
|
5000
3040
|
port: this.config.port,
|
|
5001
3041
|
host: this.config.host,
|
|
@@ -22414,7 +20454,7 @@ class Database extends EventEmitter {
|
|
|
22414
20454
|
this.id = idGenerator(7);
|
|
22415
20455
|
this.version = "1";
|
|
22416
20456
|
this.s3dbVersion = (() => {
|
|
22417
|
-
const [ok, err, version] = tryFn(() => true ? "12.2.
|
|
20457
|
+
const [ok, err, version] = tryFn(() => true ? "12.2.1" : "latest");
|
|
22418
20458
|
return ok ? version : "latest";
|
|
22419
20459
|
})();
|
|
22420
20460
|
this._resourcesMap = {};
|