@zenstackhq/server 3.5.5 → 3.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api.cjs +2729 -4451
- package/dist/api.cjs.map +1 -1
- package/dist/api.d.cts +219 -207
- package/dist/api.d.mts +249 -0
- package/dist/api.mjs +2745 -0
- package/dist/api.mjs.map +1 -0
- package/dist/common-BjzwQE8S.d.mts +16 -0
- package/dist/common-Cdj4rLFr.mjs +9 -0
- package/dist/common-Cdj4rLFr.mjs.map +1 -0
- package/dist/common-D2RyWTAZ.cjs +14 -0
- package/dist/common-D2RyWTAZ.cjs.map +1 -0
- package/dist/common-D3fx-IHt.d.cts +16 -0
- package/dist/elysia.cjs +46 -114
- package/dist/elysia.cjs.map +1 -1
- package/dist/elysia.d.cts +37 -36
- package/dist/elysia.d.mts +54 -0
- package/dist/elysia.mjs +48 -0
- package/dist/elysia.mjs.map +1 -0
- package/dist/express.cjs +40 -110
- package/dist/express.cjs.map +1 -1
- package/dist/express.d.cts +21 -20
- package/dist/express.d.mts +32 -0
- package/dist/express.mjs +42 -0
- package/dist/express.mjs.map +1 -0
- package/dist/fastify.cjs +36 -100
- package/dist/fastify.cjs.map +1 -1
- package/dist/fastify.d.cts +15 -14
- package/dist/fastify.d.mts +23 -0
- package/dist/fastify.mjs +36 -0
- package/dist/fastify.mjs.map +1 -0
- package/dist/hono.cjs +31 -108
- package/dist/hono.cjs.map +1 -1
- package/dist/hono.d.cts +11 -10
- package/dist/hono.d.mts +19 -0
- package/dist/hono.mjs +33 -0
- package/dist/hono.mjs.map +1 -0
- package/dist/next.cjs +81 -169
- package/dist/next.cjs.map +1 -1
- package/dist/next.d.cts +32 -29
- package/dist/{next.d.ts → next.d.mts} +32 -29
- package/dist/next.mjs +86 -0
- package/dist/next.mjs.map +1 -0
- package/dist/nuxt.cjs +414 -104
- package/dist/nuxt.cjs.map +1 -1
- package/dist/nuxt.d.cts +538 -12
- package/dist/nuxt.d.mts +545 -0
- package/dist/nuxt.mjs +416 -0
- package/dist/nuxt.mjs.map +1 -0
- package/dist/openapi-BPFL4Hn6.mjs +585 -0
- package/dist/openapi-BPFL4Hn6.mjs.map +1 -0
- package/dist/openapi-BUzfCKi-.cjs +586 -0
- package/dist/openapi-BUzfCKi-.cjs.map +1 -0
- package/dist/spec-utils-C7vlChMD.cjs +278 -0
- package/dist/spec-utils-C7vlChMD.cjs.map +1 -0
- package/dist/spec-utils-Syk4SLCX.mjs +200 -0
- package/dist/spec-utils-Syk4SLCX.mjs.map +1 -0
- package/dist/sveltekit.cjs +87 -188
- package/dist/sveltekit.cjs.map +1 -1
- package/dist/sveltekit.d.cts +24 -22
- package/dist/sveltekit.d.mts +43 -0
- package/dist/sveltekit.mjs +88 -0
- package/dist/sveltekit.mjs.map +1 -0
- package/dist/tanstack-start.cjs +59 -133
- package/dist/tanstack-start.cjs.map +1 -1
- package/dist/tanstack-start.d.cts +19 -14
- package/dist/tanstack-start.d.mts +37 -0
- package/dist/tanstack-start.mjs +60 -0
- package/dist/tanstack-start.mjs.map +1 -0
- package/dist/types-6NFtxov5.d.mts +74 -0
- package/dist/types-DYdFPTkH.d.cts +74 -0
- package/dist/utils-B9NX7OcP.cjs +85 -0
- package/dist/utils-B9NX7OcP.cjs.map +1 -0
- package/dist/utils-Doc_CQNd.mjs +39 -0
- package/dist/utils-Doc_CQNd.mjs.map +1 -0
- package/package.json +28 -27
- package/dist/api.d.ts +0 -237
- package/dist/api.js +0 -4437
- package/dist/api.js.map +0 -1
- package/dist/common-Cuu8bjzK.d.cts +0 -14
- package/dist/common-WHbiInuf.d.ts +0 -14
- package/dist/elysia.d.ts +0 -53
- package/dist/elysia.js +0 -82
- package/dist/elysia.js.map +0 -1
- package/dist/express.d.ts +0 -31
- package/dist/express.js +0 -78
- package/dist/express.js.map +0 -1
- package/dist/fastify.d.ts +0 -22
- package/dist/fastify.js +0 -68
- package/dist/fastify.js.map +0 -1
- package/dist/hono.d.ts +0 -18
- package/dist/hono.js +0 -76
- package/dist/hono.js.map +0 -1
- package/dist/next.js +0 -140
- package/dist/next.js.map +0 -1
- package/dist/nuxt.d.ts +0 -19
- package/dist/nuxt.js +0 -73
- package/dist/nuxt.js.map +0 -1
- package/dist/sveltekit.d.ts +0 -41
- package/dist/sveltekit.js +0 -156
- package/dist/sveltekit.js.map +0 -1
- package/dist/tanstack-start.d.ts +0 -32
- package/dist/tanstack-start.js +0 -104
- package/dist/tanstack-start.js.map +0 -1
- package/dist/types-CU_PWKr9.d.cts +0 -72
- package/dist/types-CU_PWKr9.d.ts +0 -72
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { t as ApiHandler } from "./types-6NFtxov5.mjs";
|
|
2
|
+
import { SchemaDef } from "@zenstackhq/orm/schema";
|
|
3
|
+
|
|
4
|
+
//#region src/adapter/common.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Options common to all adapters
|
|
7
|
+
*/
|
|
8
|
+
interface CommonAdapterOptions<Schema extends SchemaDef> {
|
|
9
|
+
/**
|
|
10
|
+
* The API handler to process requests
|
|
11
|
+
*/
|
|
12
|
+
apiHandler: ApiHandler<Schema>;
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
export { CommonAdapterOptions as t };
|
|
16
|
+
//# sourceMappingURL=common-BjzwQE8S.d.mts.map
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { n as log } from "./utils-Doc_CQNd.mjs";
|
|
2
|
+
//#region src/adapter/common.ts
|
|
3
|
+
function logInternalError(logger, err) {
|
|
4
|
+
log(logger, "error", `An unhandled error occurred while processing the request: ${err}${err instanceof Error ? "\n" + err.stack : ""}`);
|
|
5
|
+
}
|
|
6
|
+
//#endregion
|
|
7
|
+
export { logInternalError as t };
|
|
8
|
+
|
|
9
|
+
//# sourceMappingURL=common-Cdj4rLFr.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"common-Cdj4rLFr.mjs","names":[],"sources":["../src/adapter/common.ts"],"sourcesContent":["import type { SchemaDef } from '@zenstackhq/orm/schema';\nimport { log } from '../api/utils';\nimport type { ApiHandler, LogConfig } from '../types';\n\n/**\n * Options common to all adapters\n */\nexport interface CommonAdapterOptions<Schema extends SchemaDef> {\n /**\n * The API handler to process requests\n */\n apiHandler: ApiHandler<Schema>;\n}\n\nexport function logInternalError(logger: LogConfig | undefined, err: unknown) {\n log(\n logger,\n 'error',\n `An unhandled error occurred while processing the request: ${err}${err instanceof Error ? '\\n' + err.stack : ''}`,\n );\n}\n"],"mappings":";;AAcA,SAAgB,iBAAiB,QAA+B,KAAc;AAC1E,KACI,QACA,SACA,6DAA6D,MAAM,eAAe,QAAQ,OAAO,IAAI,QAAQ,KAChH"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const require_utils = require("./utils-B9NX7OcP.cjs");
|
|
2
|
+
//#region src/adapter/common.ts
|
|
3
|
+
function logInternalError(logger, err) {
|
|
4
|
+
require_utils.log(logger, "error", `An unhandled error occurred while processing the request: ${err}${err instanceof Error ? "\n" + err.stack : ""}`);
|
|
5
|
+
}
|
|
6
|
+
//#endregion
|
|
7
|
+
Object.defineProperty(exports, "logInternalError", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: function() {
|
|
10
|
+
return logInternalError;
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
//# sourceMappingURL=common-D2RyWTAZ.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"common-D2RyWTAZ.cjs","names":[],"sources":["../src/adapter/common.ts"],"sourcesContent":["import type { SchemaDef } from '@zenstackhq/orm/schema';\nimport { log } from '../api/utils';\nimport type { ApiHandler, LogConfig } from '../types';\n\n/**\n * Options common to all adapters\n */\nexport interface CommonAdapterOptions<Schema extends SchemaDef> {\n /**\n * The API handler to process requests\n */\n apiHandler: ApiHandler<Schema>;\n}\n\nexport function logInternalError(logger: LogConfig | undefined, err: unknown) {\n log(\n logger,\n 'error',\n `An unhandled error occurred while processing the request: ${err}${err instanceof Error ? '\\n' + err.stack : ''}`,\n );\n}\n"],"mappings":";;AAcA,SAAgB,iBAAiB,QAA+B,KAAc;AAC1E,eAAA,IACI,QACA,SACA,6DAA6D,MAAM,eAAe,QAAQ,OAAO,IAAI,QAAQ,KAChH"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { t as ApiHandler } from "./types-DYdFPTkH.cjs";
|
|
2
|
+
import { SchemaDef } from "@zenstackhq/orm/schema";
|
|
3
|
+
|
|
4
|
+
//#region src/adapter/common.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Options common to all adapters
|
|
7
|
+
*/
|
|
8
|
+
interface CommonAdapterOptions<Schema extends SchemaDef> {
|
|
9
|
+
/**
|
|
10
|
+
* The API handler to process requests
|
|
11
|
+
*/
|
|
12
|
+
apiHandler: ApiHandler<Schema>;
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
export { CommonAdapterOptions as t };
|
|
16
|
+
//# sourceMappingURL=common-D3fx-IHt.d.cts.map
|
package/dist/elysia.cjs
CHANGED
|
@@ -1,117 +1,49 @@
|
|
|
1
|
-
"
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
var __export = (target, all) => {
|
|
10
|
-
for (var name in all)
|
|
11
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
-
};
|
|
13
|
-
var __copyProps = (to, from, except, desc) => {
|
|
14
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
-
for (let key of __getOwnPropNames(from))
|
|
16
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
-
}
|
|
19
|
-
return to;
|
|
20
|
-
};
|
|
21
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
-
mod
|
|
28
|
-
));
|
|
29
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
-
|
|
31
|
-
// src/adapter/elysia/index.ts
|
|
32
|
-
var elysia_exports = {};
|
|
33
|
-
__export(elysia_exports, {
|
|
34
|
-
createElysiaHandler: () => createElysiaHandler
|
|
35
|
-
});
|
|
36
|
-
module.exports = __toCommonJS(elysia_exports);
|
|
37
|
-
|
|
38
|
-
// src/api/utils.ts
|
|
39
|
-
var import_decimal = require("decimal.js");
|
|
40
|
-
var import_superjson = __toESM(require("superjson"), 1);
|
|
41
|
-
var import_ts_pattern = require("ts-pattern");
|
|
42
|
-
var import_v3 = require("zod-validation-error/v3");
|
|
43
|
-
var import_v4 = require("zod-validation-error/v4");
|
|
44
|
-
function log(logger, level, message, error) {
|
|
45
|
-
if (!logger) {
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
const getMessage = typeof message === "function" ? message : () => message;
|
|
49
|
-
if (typeof logger === "function") {
|
|
50
|
-
logger(level, getMessage(), error);
|
|
51
|
-
} else if (logger.includes(level)) {
|
|
52
|
-
const logFn = (0, import_ts_pattern.match)(level).with("debug", () => console.debug).with("info", () => console.info).with("warn", () => console.warn).with("error", () => console.error).exhaustive();
|
|
53
|
-
logFn(`@zenstackhq/server: [${level}] ${getMessage()}${error ? `
|
|
54
|
-
${error}` : ""}`);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
__name(log, "log");
|
|
58
|
-
|
|
59
|
-
// src/adapter/common.ts
|
|
60
|
-
function logInternalError(logger, err) {
|
|
61
|
-
log(logger, "error", `An unhandled error occurred while processing the request: ${err}${err instanceof Error ? "\n" + err.stack : ""}`);
|
|
62
|
-
}
|
|
63
|
-
__name(logInternalError, "logInternalError");
|
|
64
|
-
|
|
65
|
-
// src/adapter/elysia/handler.ts
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_common = require("./common-D2RyWTAZ.cjs");
|
|
3
|
+
require("elysia");
|
|
4
|
+
//#region src/adapter/elysia/handler.ts
|
|
5
|
+
/**
|
|
6
|
+
* Creates an Elysia middleware handler for ZenStack.
|
|
7
|
+
* This handler provides automatic CRUD APIs through Elysia's routing system.
|
|
8
|
+
*/
|
|
66
9
|
function createElysiaHandler(options) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
set.status = 500;
|
|
103
|
-
logInternalError(options.apiHandler.log, err);
|
|
104
|
-
return {
|
|
105
|
-
message: "An internal server error occurred"
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
});
|
|
109
|
-
return app;
|
|
110
|
-
};
|
|
10
|
+
return async (app) => {
|
|
11
|
+
app.all("/*", async (ctx) => {
|
|
12
|
+
const { query, body, set, request } = ctx;
|
|
13
|
+
const client = await options.getClient(ctx);
|
|
14
|
+
if (!client) {
|
|
15
|
+
set.status = 500;
|
|
16
|
+
return { message: "unable to get ZenStackClient from request context" };
|
|
17
|
+
}
|
|
18
|
+
let path = new URL(request.url).pathname;
|
|
19
|
+
if (options.basePath && path.startsWith(options.basePath)) {
|
|
20
|
+
path = path.slice(options.basePath.length);
|
|
21
|
+
if (!path.startsWith("/")) path = "/" + path;
|
|
22
|
+
}
|
|
23
|
+
if (!path || path === "/") {
|
|
24
|
+
set.status = 400;
|
|
25
|
+
return { message: "missing path parameter" };
|
|
26
|
+
}
|
|
27
|
+
try {
|
|
28
|
+
const r = await options.apiHandler.handleRequest({
|
|
29
|
+
method: request.method,
|
|
30
|
+
path,
|
|
31
|
+
query,
|
|
32
|
+
requestBody: body,
|
|
33
|
+
client
|
|
34
|
+
});
|
|
35
|
+
set.status = r.status;
|
|
36
|
+
return r.body;
|
|
37
|
+
} catch (err) {
|
|
38
|
+
set.status = 500;
|
|
39
|
+
require_common.logInternalError(options.apiHandler.log, err);
|
|
40
|
+
return { message: "An internal server error occurred" };
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
return app;
|
|
44
|
+
};
|
|
111
45
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
createElysiaHandler
|
|
116
|
-
});
|
|
46
|
+
//#endregion
|
|
47
|
+
exports.createElysiaHandler = createElysiaHandler;
|
|
48
|
+
|
|
117
49
|
//# sourceMappingURL=elysia.cjs.map
|
package/dist/elysia.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"elysia.cjs","names":[],"sources":["../src/adapter/elysia/handler.ts"],"sourcesContent":["import type { ClientContract } from '@zenstackhq/orm';\nimport type { SchemaDef } from '@zenstackhq/orm/schema';\nimport { Elysia, type Context as ElysiaContext } from 'elysia';\nimport { logInternalError, type CommonAdapterOptions } from '../common';\n\n/**\n * Options for initializing an Elysia middleware.\n */\nexport interface ElysiaOptions<Schema extends SchemaDef> extends CommonAdapterOptions<Schema> {\n /**\n * Callback method for getting a ZenStackClient instance for the given request context.\n */\n getClient: (context: ElysiaContext) => Promise<ClientContract<Schema>> | ClientContract<Schema>;\n\n /**\n * Optional base path to strip from the request path before passing to the API handler.\n */\n basePath?: string;\n}\n\n/**\n * Creates an Elysia middleware handler for ZenStack.\n * This handler provides automatic CRUD APIs through Elysia's routing system.\n */\nexport function createElysiaHandler<Schema extends SchemaDef>(options: ElysiaOptions<Schema>) {\n return async (app: Elysia) => {\n app.all('/*', async (ctx: ElysiaContext) => {\n const { query, body, set, request } = ctx;\n const client = await options.getClient(ctx);\n if (!client) {\n set.status = 500;\n return {\n message: 'unable to get ZenStackClient from request context',\n };\n }\n\n const url = new URL(request.url);\n let path = url.pathname;\n\n if (options.basePath && path.startsWith(options.basePath)) {\n path = path.slice(options.basePath.length);\n if (!path.startsWith('/')) {\n path = '/' + path;\n }\n }\n\n if (!path || path === '/') {\n set.status = 400;\n return {\n message: 'missing path parameter',\n };\n }\n\n try {\n const r = await options.apiHandler.handleRequest({\n method: request.method,\n path,\n query,\n requestBody: body,\n client,\n });\n\n set.status = r.status;\n return r.body;\n } catch (err) {\n set.status = 500;\n logInternalError(options.apiHandler.log, err);\n return {\n message: 'An internal server error occurred',\n };\n }\n });\n\n return app;\n };\n}\n"],"mappings":";;;;;;;;AAwBA,SAAgB,oBAA8C,SAAgC;AAC1F,QAAO,OAAO,QAAgB;AAC1B,MAAI,IAAI,MAAM,OAAO,QAAuB;GACxC,MAAM,EAAE,OAAO,MAAM,KAAK,YAAY;GACtC,MAAM,SAAS,MAAM,QAAQ,UAAU,IAAI;AAC3C,OAAI,CAAC,QAAQ;AACT,QAAI,SAAS;AACb,WAAO,EACH,SAAS,qDACZ;;GAIL,IAAI,OADQ,IAAI,IAAI,QAAQ,IAAI,CACjB;AAEf,OAAI,QAAQ,YAAY,KAAK,WAAW,QAAQ,SAAS,EAAE;AACvD,WAAO,KAAK,MAAM,QAAQ,SAAS,OAAO;AAC1C,QAAI,CAAC,KAAK,WAAW,IAAI,CACrB,QAAO,MAAM;;AAIrB,OAAI,CAAC,QAAQ,SAAS,KAAK;AACvB,QAAI,SAAS;AACb,WAAO,EACH,SAAS,0BACZ;;AAGL,OAAI;IACA,MAAM,IAAI,MAAM,QAAQ,WAAW,cAAc;KAC7C,QAAQ,QAAQ;KAChB;KACA;KACA,aAAa;KACb;KACH,CAAC;AAEF,QAAI,SAAS,EAAE;AACf,WAAO,EAAE;YACJ,KAAK;AACV,QAAI,SAAS;AACb,mBAAA,iBAAiB,QAAQ,WAAW,KAAK,IAAI;AAC7C,WAAO,EACH,SAAS,qCACZ;;IAEP;AAEF,SAAO"}
|
package/dist/elysia.d.cts
CHANGED
|
@@ -1,53 +1,54 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import './types-CU_PWKr9.cjs';
|
|
1
|
+
import { t as CommonAdapterOptions } from "./common-D3fx-IHt.cjs";
|
|
2
|
+
import { ClientContract } from "@zenstackhq/orm";
|
|
3
|
+
import { SchemaDef } from "@zenstackhq/orm/schema";
|
|
4
|
+
import { Context, Elysia } from "elysia";
|
|
6
5
|
|
|
6
|
+
//#region src/adapter/elysia/handler.d.ts
|
|
7
7
|
/**
|
|
8
8
|
* Options for initializing an Elysia middleware.
|
|
9
9
|
*/
|
|
10
10
|
interface ElysiaOptions<Schema extends SchemaDef> extends CommonAdapterOptions<Schema> {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
11
|
+
/**
|
|
12
|
+
* Callback method for getting a ZenStackClient instance for the given request context.
|
|
13
|
+
*/
|
|
14
|
+
getClient: (context: Context) => Promise<ClientContract<Schema>> | ClientContract<Schema>;
|
|
15
|
+
/**
|
|
16
|
+
* Optional base path to strip from the request path before passing to the API handler.
|
|
17
|
+
*/
|
|
18
|
+
basePath?: string;
|
|
19
19
|
}
|
|
20
20
|
/**
|
|
21
21
|
* Creates an Elysia middleware handler for ZenStack.
|
|
22
22
|
* This handler provides automatic CRUD APIs through Elysia's routing system.
|
|
23
23
|
*/
|
|
24
24
|
declare function createElysiaHandler<Schema extends SchemaDef>(options: ElysiaOptions<Schema>): (app: Elysia) => Promise<Elysia<"", {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
decorator: {};
|
|
26
|
+
store: {};
|
|
27
|
+
derive: {};
|
|
28
|
+
resolve: {};
|
|
29
29
|
}, {
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
typebox: {};
|
|
31
|
+
error: {};
|
|
32
32
|
}, {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
33
|
+
schema: {};
|
|
34
|
+
standaloneSchema: {};
|
|
35
|
+
macro: {};
|
|
36
|
+
macroFn: {};
|
|
37
|
+
parser: {};
|
|
38
|
+
response: {};
|
|
39
39
|
}, {}, {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
40
|
+
derive: {};
|
|
41
|
+
resolve: {};
|
|
42
|
+
schema: {};
|
|
43
|
+
standaloneSchema: {};
|
|
44
|
+
response: {};
|
|
45
45
|
}, {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
46
|
+
derive: {};
|
|
47
|
+
resolve: {};
|
|
48
|
+
schema: {};
|
|
49
|
+
standaloneSchema: {};
|
|
50
|
+
response: {};
|
|
51
51
|
}>>;
|
|
52
|
-
|
|
52
|
+
//#endregion
|
|
53
53
|
export { type ElysiaOptions, createElysiaHandler };
|
|
54
|
+
//# sourceMappingURL=elysia.d.cts.map
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { t as CommonAdapterOptions } from "./common-BjzwQE8S.mjs";
|
|
2
|
+
import { ClientContract } from "@zenstackhq/orm";
|
|
3
|
+
import { SchemaDef } from "@zenstackhq/orm/schema";
|
|
4
|
+
import { Context, Elysia } from "elysia";
|
|
5
|
+
|
|
6
|
+
//#region src/adapter/elysia/handler.d.ts
|
|
7
|
+
/**
|
|
8
|
+
* Options for initializing an Elysia middleware.
|
|
9
|
+
*/
|
|
10
|
+
interface ElysiaOptions<Schema extends SchemaDef> extends CommonAdapterOptions<Schema> {
|
|
11
|
+
/**
|
|
12
|
+
* Callback method for getting a ZenStackClient instance for the given request context.
|
|
13
|
+
*/
|
|
14
|
+
getClient: (context: Context) => Promise<ClientContract<Schema>> | ClientContract<Schema>;
|
|
15
|
+
/**
|
|
16
|
+
* Optional base path to strip from the request path before passing to the API handler.
|
|
17
|
+
*/
|
|
18
|
+
basePath?: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Creates an Elysia middleware handler for ZenStack.
|
|
22
|
+
* This handler provides automatic CRUD APIs through Elysia's routing system.
|
|
23
|
+
*/
|
|
24
|
+
declare function createElysiaHandler<Schema extends SchemaDef>(options: ElysiaOptions<Schema>): (app: Elysia) => Promise<Elysia<"", {
|
|
25
|
+
decorator: {};
|
|
26
|
+
store: {};
|
|
27
|
+
derive: {};
|
|
28
|
+
resolve: {};
|
|
29
|
+
}, {
|
|
30
|
+
typebox: {};
|
|
31
|
+
error: {};
|
|
32
|
+
}, {
|
|
33
|
+
schema: {};
|
|
34
|
+
standaloneSchema: {};
|
|
35
|
+
macro: {};
|
|
36
|
+
macroFn: {};
|
|
37
|
+
parser: {};
|
|
38
|
+
response: {};
|
|
39
|
+
}, {}, {
|
|
40
|
+
derive: {};
|
|
41
|
+
resolve: {};
|
|
42
|
+
schema: {};
|
|
43
|
+
standaloneSchema: {};
|
|
44
|
+
response: {};
|
|
45
|
+
}, {
|
|
46
|
+
derive: {};
|
|
47
|
+
resolve: {};
|
|
48
|
+
schema: {};
|
|
49
|
+
standaloneSchema: {};
|
|
50
|
+
response: {};
|
|
51
|
+
}>>;
|
|
52
|
+
//#endregion
|
|
53
|
+
export { type ElysiaOptions, createElysiaHandler };
|
|
54
|
+
//# sourceMappingURL=elysia.d.mts.map
|
package/dist/elysia.mjs
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { t as logInternalError } from "./common-Cdj4rLFr.mjs";
|
|
2
|
+
import "elysia";
|
|
3
|
+
//#region src/adapter/elysia/handler.ts
|
|
4
|
+
/**
|
|
5
|
+
* Creates an Elysia middleware handler for ZenStack.
|
|
6
|
+
* This handler provides automatic CRUD APIs through Elysia's routing system.
|
|
7
|
+
*/
|
|
8
|
+
function createElysiaHandler(options) {
|
|
9
|
+
return async (app) => {
|
|
10
|
+
app.all("/*", async (ctx) => {
|
|
11
|
+
const { query, body, set, request } = ctx;
|
|
12
|
+
const client = await options.getClient(ctx);
|
|
13
|
+
if (!client) {
|
|
14
|
+
set.status = 500;
|
|
15
|
+
return { message: "unable to get ZenStackClient from request context" };
|
|
16
|
+
}
|
|
17
|
+
let path = new URL(request.url).pathname;
|
|
18
|
+
if (options.basePath && path.startsWith(options.basePath)) {
|
|
19
|
+
path = path.slice(options.basePath.length);
|
|
20
|
+
if (!path.startsWith("/")) path = "/" + path;
|
|
21
|
+
}
|
|
22
|
+
if (!path || path === "/") {
|
|
23
|
+
set.status = 400;
|
|
24
|
+
return { message: "missing path parameter" };
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
const r = await options.apiHandler.handleRequest({
|
|
28
|
+
method: request.method,
|
|
29
|
+
path,
|
|
30
|
+
query,
|
|
31
|
+
requestBody: body,
|
|
32
|
+
client
|
|
33
|
+
});
|
|
34
|
+
set.status = r.status;
|
|
35
|
+
return r.body;
|
|
36
|
+
} catch (err) {
|
|
37
|
+
set.status = 500;
|
|
38
|
+
logInternalError(options.apiHandler.log, err);
|
|
39
|
+
return { message: "An internal server error occurred" };
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
return app;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
//#endregion
|
|
46
|
+
export { createElysiaHandler };
|
|
47
|
+
|
|
48
|
+
//# sourceMappingURL=elysia.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"elysia.mjs","names":[],"sources":["../src/adapter/elysia/handler.ts"],"sourcesContent":["import type { ClientContract } from '@zenstackhq/orm';\nimport type { SchemaDef } from '@zenstackhq/orm/schema';\nimport { Elysia, type Context as ElysiaContext } from 'elysia';\nimport { logInternalError, type CommonAdapterOptions } from '../common';\n\n/**\n * Options for initializing an Elysia middleware.\n */\nexport interface ElysiaOptions<Schema extends SchemaDef> extends CommonAdapterOptions<Schema> {\n /**\n * Callback method for getting a ZenStackClient instance for the given request context.\n */\n getClient: (context: ElysiaContext) => Promise<ClientContract<Schema>> | ClientContract<Schema>;\n\n /**\n * Optional base path to strip from the request path before passing to the API handler.\n */\n basePath?: string;\n}\n\n/**\n * Creates an Elysia middleware handler for ZenStack.\n * This handler provides automatic CRUD APIs through Elysia's routing system.\n */\nexport function createElysiaHandler<Schema extends SchemaDef>(options: ElysiaOptions<Schema>) {\n return async (app: Elysia) => {\n app.all('/*', async (ctx: ElysiaContext) => {\n const { query, body, set, request } = ctx;\n const client = await options.getClient(ctx);\n if (!client) {\n set.status = 500;\n return {\n message: 'unable to get ZenStackClient from request context',\n };\n }\n\n const url = new URL(request.url);\n let path = url.pathname;\n\n if (options.basePath && path.startsWith(options.basePath)) {\n path = path.slice(options.basePath.length);\n if (!path.startsWith('/')) {\n path = '/' + path;\n }\n }\n\n if (!path || path === '/') {\n set.status = 400;\n return {\n message: 'missing path parameter',\n };\n }\n\n try {\n const r = await options.apiHandler.handleRequest({\n method: request.method,\n path,\n query,\n requestBody: body,\n client,\n });\n\n set.status = r.status;\n return r.body;\n } catch (err) {\n set.status = 500;\n logInternalError(options.apiHandler.log, err);\n return {\n message: 'An internal server error occurred',\n };\n }\n });\n\n return app;\n };\n}\n"],"mappings":";;;;;;;AAwBA,SAAgB,oBAA8C,SAAgC;AAC1F,QAAO,OAAO,QAAgB;AAC1B,MAAI,IAAI,MAAM,OAAO,QAAuB;GACxC,MAAM,EAAE,OAAO,MAAM,KAAK,YAAY;GACtC,MAAM,SAAS,MAAM,QAAQ,UAAU,IAAI;AAC3C,OAAI,CAAC,QAAQ;AACT,QAAI,SAAS;AACb,WAAO,EACH,SAAS,qDACZ;;GAIL,IAAI,OADQ,IAAI,IAAI,QAAQ,IAAI,CACjB;AAEf,OAAI,QAAQ,YAAY,KAAK,WAAW,QAAQ,SAAS,EAAE;AACvD,WAAO,KAAK,MAAM,QAAQ,SAAS,OAAO;AAC1C,QAAI,CAAC,KAAK,WAAW,IAAI,CACrB,QAAO,MAAM;;AAIrB,OAAI,CAAC,QAAQ,SAAS,KAAK;AACvB,QAAI,SAAS;AACb,WAAO,EACH,SAAS,0BACZ;;AAGL,OAAI;IACA,MAAM,IAAI,MAAM,QAAQ,WAAW,cAAc;KAC7C,QAAQ,QAAQ;KAChB;KACA;KACA,aAAa;KACb;KACH,CAAC;AAEF,QAAI,SAAS,EAAE;AACf,WAAO,EAAE;YACJ,KAAK;AACV,QAAI,SAAS;AACb,qBAAiB,QAAQ,WAAW,KAAK,IAAI;AAC7C,WAAO,EACH,SAAS,qCACZ;;IAEP;AAEF,SAAO"}
|
package/dist/express.cjs
CHANGED
|
@@ -1,113 +1,43 @@
|
|
|
1
|
-
"
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_common = require("./common-D2RyWTAZ.cjs");
|
|
3
|
+
//#region src/adapter/express/middleware.ts
|
|
4
|
+
/**
|
|
5
|
+
* Creates an Express middleware for handling CRUD requests.
|
|
6
|
+
*/
|
|
7
|
+
const factory = (options) => {
|
|
8
|
+
const requestHandler = options.apiHandler;
|
|
9
|
+
return async (request, response, next) => {
|
|
10
|
+
const client = await options.getClient(request, response);
|
|
11
|
+
const { sendResponse } = options;
|
|
12
|
+
if (sendResponse === false && !client) throw new Error("unable to get ZenStackClient from request context");
|
|
13
|
+
if (!client) return response.status(500).json({ message: "unable to get ZenStackClient from request context" });
|
|
14
|
+
const url = request.protocol + "://" + request.get("host") + request.originalUrl;
|
|
15
|
+
const searchParams = new URL(url).searchParams;
|
|
16
|
+
const query = Object.fromEntries(searchParams);
|
|
17
|
+
try {
|
|
18
|
+
const r = await requestHandler.handleRequest({
|
|
19
|
+
method: request.method,
|
|
20
|
+
path: request.path,
|
|
21
|
+
query,
|
|
22
|
+
requestBody: request.body,
|
|
23
|
+
client
|
|
24
|
+
});
|
|
25
|
+
if (sendResponse === false) {
|
|
26
|
+
response.locals["zenstack"] = {
|
|
27
|
+
status: r.status,
|
|
28
|
+
body: r.body
|
|
29
|
+
};
|
|
30
|
+
return next();
|
|
31
|
+
}
|
|
32
|
+
return response.status(r.status).json(r.body);
|
|
33
|
+
} catch (err) {
|
|
34
|
+
if (sendResponse === false) throw err;
|
|
35
|
+
require_common.logInternalError(options.apiHandler.log, err);
|
|
36
|
+
return response.status(500).json({ message: `An internal server error occurred` });
|
|
37
|
+
}
|
|
38
|
+
};
|
|
12
39
|
};
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
for (let key of __getOwnPropNames(from))
|
|
16
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
-
}
|
|
19
|
-
return to;
|
|
20
|
-
};
|
|
21
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
-
mod
|
|
28
|
-
));
|
|
29
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
-
|
|
31
|
-
// src/adapter/express/index.ts
|
|
32
|
-
var express_exports = {};
|
|
33
|
-
__export(express_exports, {
|
|
34
|
-
ZenStackMiddleware: () => factory
|
|
35
|
-
});
|
|
36
|
-
module.exports = __toCommonJS(express_exports);
|
|
37
|
-
|
|
38
|
-
// src/api/utils.ts
|
|
39
|
-
var import_decimal = require("decimal.js");
|
|
40
|
-
var import_superjson = __toESM(require("superjson"), 1);
|
|
41
|
-
var import_ts_pattern = require("ts-pattern");
|
|
42
|
-
var import_v3 = require("zod-validation-error/v3");
|
|
43
|
-
var import_v4 = require("zod-validation-error/v4");
|
|
44
|
-
function log(logger, level, message, error) {
|
|
45
|
-
if (!logger) {
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
const getMessage = typeof message === "function" ? message : () => message;
|
|
49
|
-
if (typeof logger === "function") {
|
|
50
|
-
logger(level, getMessage(), error);
|
|
51
|
-
} else if (logger.includes(level)) {
|
|
52
|
-
const logFn = (0, import_ts_pattern.match)(level).with("debug", () => console.debug).with("info", () => console.info).with("warn", () => console.warn).with("error", () => console.error).exhaustive();
|
|
53
|
-
logFn(`@zenstackhq/server: [${level}] ${getMessage()}${error ? `
|
|
54
|
-
${error}` : ""}`);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
__name(log, "log");
|
|
58
|
-
|
|
59
|
-
// src/adapter/common.ts
|
|
60
|
-
function logInternalError(logger, err) {
|
|
61
|
-
log(logger, "error", `An unhandled error occurred while processing the request: ${err}${err instanceof Error ? "\n" + err.stack : ""}`);
|
|
62
|
-
}
|
|
63
|
-
__name(logInternalError, "logInternalError");
|
|
40
|
+
//#endregion
|
|
41
|
+
exports.ZenStackMiddleware = factory;
|
|
64
42
|
|
|
65
|
-
// src/adapter/express/middleware.ts
|
|
66
|
-
var factory = /* @__PURE__ */ __name((options) => {
|
|
67
|
-
const requestHandler = options.apiHandler;
|
|
68
|
-
return async (request, response, next) => {
|
|
69
|
-
const client = await options.getClient(request, response);
|
|
70
|
-
const { sendResponse } = options;
|
|
71
|
-
if (sendResponse === false && !client) {
|
|
72
|
-
throw new Error("unable to get ZenStackClient from request context");
|
|
73
|
-
}
|
|
74
|
-
if (!client) {
|
|
75
|
-
return response.status(500).json({
|
|
76
|
-
message: "unable to get ZenStackClient from request context"
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
const url = request.protocol + "://" + request.get("host") + request.originalUrl;
|
|
80
|
-
const searchParams = new URL(url).searchParams;
|
|
81
|
-
const query = Object.fromEntries(searchParams);
|
|
82
|
-
try {
|
|
83
|
-
const r = await requestHandler.handleRequest({
|
|
84
|
-
method: request.method,
|
|
85
|
-
path: request.path,
|
|
86
|
-
query,
|
|
87
|
-
requestBody: request.body,
|
|
88
|
-
client
|
|
89
|
-
});
|
|
90
|
-
if (sendResponse === false) {
|
|
91
|
-
response.locals["zenstack"] = {
|
|
92
|
-
status: r.status,
|
|
93
|
-
body: r.body
|
|
94
|
-
};
|
|
95
|
-
return next();
|
|
96
|
-
}
|
|
97
|
-
return response.status(r.status).json(r.body);
|
|
98
|
-
} catch (err) {
|
|
99
|
-
if (sendResponse === false) {
|
|
100
|
-
throw err;
|
|
101
|
-
}
|
|
102
|
-
logInternalError(options.apiHandler.log, err);
|
|
103
|
-
return response.status(500).json({
|
|
104
|
-
message: `An internal server error occurred`
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
|
-
}, "factory");
|
|
109
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
110
|
-
0 && (module.exports = {
|
|
111
|
-
ZenStackMiddleware
|
|
112
|
-
});
|
|
113
43
|
//# sourceMappingURL=express.cjs.map
|