@zenstackhq/server 1.0.0-beta.8 → 1.0.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/api/base.d.ts +49 -0
- package/api/base.js +19 -0
- package/api/base.js.map +1 -0
- package/api/rest/index.d.ts +3 -2
- package/api/rest/index.js +119 -80
- package/api/rest/index.js.map +1 -1
- package/api/rpc/index.d.ts +3 -2
- package/api/rpc/index.js +156 -61
- package/api/rpc/index.js.map +1 -1
- package/api/utils.d.ts +2 -12
- package/api/utils.js +19 -43
- package/api/utils.js.map +1 -1
- package/express/middleware.js +16 -41
- package/express/middleware.js.map +1 -1
- package/fastify/plugin.d.ts +2 -1
- package/fastify/plugin.js +14 -30
- package/fastify/plugin.js.map +1 -1
- package/next/app-route-handler.js +13 -28
- package/next/app-route-handler.js.map +1 -1
- package/next/pages-route-handler.js +12 -27
- package/next/pages-route-handler.js.map +1 -1
- package/package.json +24 -8
- package/shared.d.ts +6 -0
- package/shared.js +23 -0
- package/shared.js.map +1 -0
- package/sveltekit/handler.js +12 -35
- package/sveltekit/handler.js.map +1 -1
- package/types.d.ts +8 -45
- package/utils.d.ts +0 -17
- package/utils.js +0 -93
- package/utils.js.map +0 -1
package/fastify/plugin.js
CHANGED
|
@@ -15,7 +15,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
15
15
|
const fastify_plugin_1 = __importDefault(require("fastify-plugin"));
|
|
16
16
|
const rpc_1 = __importDefault(require("../api/rpc"));
|
|
17
17
|
const utils_1 = require("../api/utils");
|
|
18
|
-
const
|
|
18
|
+
const shared_1 = require("../shared");
|
|
19
19
|
/**
|
|
20
20
|
* Fastify plugin for handling CRUD requests.
|
|
21
21
|
*/
|
|
@@ -23,50 +23,34 @@ const pluginHandler = (fastify, options, done) => {
|
|
|
23
23
|
var _a, _b;
|
|
24
24
|
const prefix = (_a = options.prefix) !== null && _a !== void 0 ? _a : '';
|
|
25
25
|
(0, utils_1.logInfo)(options.logger, `ZenStackPlugin installing routes at prefix: ${prefix}`);
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
const { modelMeta, zodSchemas } = (0, shared_1.loadAssets)(options);
|
|
27
|
+
const requestHandler = (_b = options.handler) !== null && _b !== void 0 ? _b : (0, rpc_1.default)();
|
|
28
|
+
if (options.useSuperJson !== undefined) {
|
|
29
|
+
console.warn('The option "useSuperJson" is deprecated. The server APIs automatically use superjson for serialization.');
|
|
29
30
|
}
|
|
30
|
-
else if (options.zodSchemas === true) {
|
|
31
|
-
zodSchemas = require('@zenstackhq/runtime/zod');
|
|
32
|
-
if (!zodSchemas) {
|
|
33
|
-
throw new Error('Unable to load zod schemas from default location');
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
const requestHanler = (_b = options.handler) !== null && _b !== void 0 ? _b : (0, rpc_1.default)();
|
|
37
|
-
const useSuperJson = options.useSuperJson === true;
|
|
38
31
|
fastify.all(`${prefix}/*`, (request, reply) => __awaiter(void 0, void 0, void 0, function* () {
|
|
39
32
|
const prisma = (yield options.getPrisma(request, reply));
|
|
40
33
|
if (!prisma) {
|
|
41
|
-
reply
|
|
42
|
-
|
|
43
|
-
.send((0, utils_2.marshalToObject)({ message: 'unable to get prisma from request context' }, useSuperJson));
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
let query = {};
|
|
47
|
-
try {
|
|
48
|
-
query = (0, utils_2.buildUrlQuery)(request.query, useSuperJson);
|
|
49
|
-
}
|
|
50
|
-
catch (_c) {
|
|
51
|
-
reply.status(400).send((0, utils_2.marshalToObject)({ message: 'invalid query parameters' }, useSuperJson));
|
|
52
|
-
return;
|
|
34
|
+
reply.status(500).send({ message: 'unable to get prisma from request context' });
|
|
35
|
+
return reply;
|
|
53
36
|
}
|
|
54
37
|
try {
|
|
55
|
-
const response = yield
|
|
38
|
+
const response = yield requestHandler({
|
|
56
39
|
method: request.method,
|
|
57
40
|
path: request.params['*'],
|
|
58
|
-
query,
|
|
59
|
-
requestBody:
|
|
41
|
+
query: request.query,
|
|
42
|
+
requestBody: request.body,
|
|
60
43
|
prisma,
|
|
61
|
-
modelMeta
|
|
44
|
+
modelMeta,
|
|
62
45
|
zodSchemas,
|
|
63
46
|
logger: options.logger,
|
|
64
47
|
});
|
|
65
|
-
reply.status(response.status).send(
|
|
48
|
+
reply.status(response.status).send(response.body);
|
|
66
49
|
}
|
|
67
50
|
catch (err) {
|
|
68
|
-
reply.status(500).send(
|
|
51
|
+
reply.status(500).send({ message: `An unhandled error occurred: ${err}` });
|
|
69
52
|
}
|
|
53
|
+
return reply;
|
|
70
54
|
}));
|
|
71
55
|
done();
|
|
72
56
|
};
|
package/fastify/plugin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../src/fastify/plugin.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAGA,oEAAgC;AAChC,qDAAuC;AACvC,wCAAuC;
|
|
1
|
+
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../src/fastify/plugin.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAGA,oEAAgC;AAChC,qDAAuC;AACvC,wCAAuC;AACvC,sCAAuC;AAkBvC;;GAEG;AACH,MAAM,aAAa,GAAyC,CAAC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE;;IACnF,MAAM,MAAM,GAAG,MAAA,OAAO,CAAC,MAAM,mCAAI,EAAE,CAAC;IACpC,IAAA,eAAO,EAAC,OAAO,CAAC,MAAM,EAAE,+CAA+C,MAAM,EAAE,CAAC,CAAC;IAEjF,MAAM,EAAE,SAAS,EAAE,UAAU,EAAE,GAAG,IAAA,mBAAU,EAAC,OAAO,CAAC,CAAC;IAEtD,MAAM,cAAc,GAAG,MAAA,OAAO,CAAC,OAAO,mCAAI,IAAA,aAAa,GAAE,CAAC;IAC1D,IAAI,OAAO,CAAC,YAAY,KAAK,SAAS,EAAE;QACpC,OAAO,CAAC,IAAI,CACR,yGAAyG,CAC5G,CAAC;KACL;IAED,OAAO,CAAC,GAAG,CAAC,GAAG,MAAM,IAAI,EAAE,CAAO,OAAO,EAAE,KAAK,EAAE,EAAE;QAChD,MAAM,MAAM,GAAG,CAAC,MAAM,OAAO,CAAC,SAAS,CAAC,OAAO,EAAE,KAAK,CAAC,CAAqB,CAAC;QAC7E,IAAI,CAAC,MAAM,EAAE;YACT,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,2CAA2C,EAAE,CAAC,CAAC;YACjF,OAAO,KAAK,CAAC;SAChB;QAED,IAAI;YACA,MAAM,QAAQ,GAAG,MAAM,cAAc,CAAC;gBAClC,MAAM,EAAE,OAAO,CAAC,MAAM;gBACtB,IAAI,EAAG,OAAO,CAAC,MAAc,CAAC,GAAG,CAAC;gBAClC,KAAK,EAAE,OAAO,CAAC,KAA0C;gBACzD,WAAW,EAAE,OAAO,CAAC,IAAI;gBACzB,MAAM;gBACN,SAAS;gBACT,UAAU;gBACV,MAAM,EAAE,OAAO,CAAC,MAAM;aACzB,CAAC,CAAC;YACH,KAAK,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;SACrD;QAAC,OAAO,GAAG,EAAE;YACV,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,gCAAgC,GAAG,EAAE,EAAE,CAAC,CAAC;SAC9E;QAED,OAAO,KAAK,CAAC;IACjB,CAAC,CAAA,CAAC,CAAC;IAEH,IAAI,EAAE,CAAC;AACX,CAAC,CAAC;AAEF,kBAAe,IAAA,wBAAE,EAAC,aAAa,CAAC,CAAC"}
|
|
@@ -15,7 +15,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
15
15
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
16
|
const server_1 = require("next/server");
|
|
17
17
|
const rpc_1 = __importDefault(require("../api/rpc"));
|
|
18
|
-
const
|
|
18
|
+
const shared_1 = require("../shared");
|
|
19
19
|
/**
|
|
20
20
|
* Creates a Next.js 13 "app dir" API route request handler which encapsulates Prisma CRUD operations.
|
|
21
21
|
*
|
|
@@ -23,35 +23,20 @@ const utils_1 = require("../utils");
|
|
|
23
23
|
* @returns An API route request handler
|
|
24
24
|
*/
|
|
25
25
|
function factory(options) {
|
|
26
|
-
|
|
27
|
-
if (typeof options.zodSchemas === 'object') {
|
|
28
|
-
zodSchemas = options.zodSchemas;
|
|
29
|
-
}
|
|
30
|
-
else if (options.zodSchemas === true) {
|
|
31
|
-
zodSchemas = require('@zenstackhq/runtime/zod');
|
|
32
|
-
if (!zodSchemas) {
|
|
33
|
-
throw new Error('Unable to load zod schemas from default location');
|
|
34
|
-
}
|
|
35
|
-
}
|
|
26
|
+
const { modelMeta, zodSchemas } = (0, shared_1.loadAssets)(options);
|
|
36
27
|
const requestHandler = options.handler || (0, rpc_1.default)();
|
|
37
|
-
|
|
28
|
+
if (options.useSuperJson !== undefined) {
|
|
29
|
+
console.warn('The option "useSuperJson" is deprecated. The server APIs automatically use superjson for serialization.');
|
|
30
|
+
}
|
|
38
31
|
return (req, context) => __awaiter(this, void 0, void 0, function* () {
|
|
39
32
|
const prisma = (yield options.getPrisma(req));
|
|
40
33
|
if (!prisma) {
|
|
41
|
-
return server_1.NextResponse.json(
|
|
34
|
+
return server_1.NextResponse.json({ message: 'unable to get prisma from request context' }, { status: 500 });
|
|
42
35
|
}
|
|
43
36
|
const url = new URL(req.url);
|
|
44
|
-
|
|
45
|
-
try {
|
|
46
|
-
query = (0, utils_1.buildUrlQuery)(query, useSuperJson);
|
|
47
|
-
}
|
|
48
|
-
catch (_a) {
|
|
49
|
-
return server_1.NextResponse.json((0, utils_1.marshalToObject)({ message: 'invalid query parameters' }, useSuperJson), {
|
|
50
|
-
status: 400,
|
|
51
|
-
});
|
|
52
|
-
}
|
|
37
|
+
const query = Object.fromEntries(url.searchParams);
|
|
53
38
|
if (!context.params.path) {
|
|
54
|
-
return server_1.NextResponse.json(
|
|
39
|
+
return server_1.NextResponse.json({ message: 'missing path parameter' }, {
|
|
55
40
|
status: 400,
|
|
56
41
|
});
|
|
57
42
|
}
|
|
@@ -61,7 +46,7 @@ function factory(options) {
|
|
|
61
46
|
try {
|
|
62
47
|
requestBody = yield req.json();
|
|
63
48
|
}
|
|
64
|
-
catch (
|
|
49
|
+
catch (_a) {
|
|
65
50
|
// noop
|
|
66
51
|
}
|
|
67
52
|
}
|
|
@@ -70,16 +55,16 @@ function factory(options) {
|
|
|
70
55
|
method: req.method,
|
|
71
56
|
path,
|
|
72
57
|
query,
|
|
73
|
-
requestBody
|
|
58
|
+
requestBody,
|
|
74
59
|
prisma,
|
|
75
|
-
modelMeta
|
|
60
|
+
modelMeta,
|
|
76
61
|
zodSchemas,
|
|
77
62
|
logger: options.logger,
|
|
78
63
|
});
|
|
79
|
-
return server_1.NextResponse.json(
|
|
64
|
+
return server_1.NextResponse.json(r.body, { status: r.status });
|
|
80
65
|
}
|
|
81
66
|
catch (err) {
|
|
82
|
-
return server_1.NextResponse.json(
|
|
67
|
+
return server_1.NextResponse.json({ message: `An unhandled error occurred: ${err}` }, { status: 500 });
|
|
83
68
|
}
|
|
84
69
|
});
|
|
85
70
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"app-route-handler.js","sourceRoot":"","sources":["../../src/next/app-route-handler.ts"],"names":[],"mappings":";AAAA,6DAA6D;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"app-route-handler.js","sourceRoot":"","sources":["../../src/next/app-route-handler.ts"],"names":[],"mappings":";AAAA,6DAA6D;;;;;;;;;;;;;;AAG7D,wCAAwD;AAExD,qDAAuC;AACvC,sCAAuC;AAIvC;;;;;GAKG;AACH,SAAwB,OAAO,CAC3B,OAAsC;IAEtC,MAAM,EAAE,SAAS,EAAE,UAAU,EAAE,GAAG,IA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,SAAS;gBACT,UAAU;gBACV,MAAM,EAAE,OAAO,CAAC,MAAM;aACzB,CAAC,CAAC;YACH,OAAO,qBAAY,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC;SAC1D;QAAC,OAAO,GAAG,EAAE;YACV,OAAO,qBAAY,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,gCAAgC,GAAG,EAAE,EAAE,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC;SACjG;IACL,CAAC,CAAA,CAAC;AACN,CAAC;AAxDD,0BAwDC"}
|
|
@@ -14,7 +14,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
14
14
|
};
|
|
15
15
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
16
|
const rpc_1 = __importDefault(require("../api/rpc"));
|
|
17
|
-
const
|
|
17
|
+
const shared_1 = require("../shared");
|
|
18
18
|
/**
|
|
19
19
|
* Creates a Next.js API endpoint (traditional "pages" route) request handler which encapsulates Prisma CRUD operations.
|
|
20
20
|
*
|
|
@@ -22,34 +22,19 @@ const utils_1 = require("../utils");
|
|
|
22
22
|
* @returns An API endpoint request handler
|
|
23
23
|
*/
|
|
24
24
|
function factory(options) {
|
|
25
|
-
|
|
26
|
-
if (typeof options.zodSchemas === 'object') {
|
|
27
|
-
zodSchemas = options.zodSchemas;
|
|
28
|
-
}
|
|
29
|
-
else if (options.zodSchemas === true) {
|
|
30
|
-
zodSchemas = require('@zenstackhq/runtime/zod');
|
|
31
|
-
if (!zodSchemas) {
|
|
32
|
-
throw new Error('Unable to load zod schemas from default location');
|
|
33
|
-
}
|
|
34
|
-
}
|
|
25
|
+
const { modelMeta, zodSchemas } = (0, shared_1.loadAssets)(options);
|
|
35
26
|
const requestHandler = options.handler || (0, rpc_1.default)();
|
|
36
|
-
|
|
27
|
+
if (options.useSuperJson !== undefined) {
|
|
28
|
+
console.warn('The option "useSuperJson" is deprecated. The server APIs automatically use superjson for serialization.');
|
|
29
|
+
}
|
|
37
30
|
return (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
38
31
|
const prisma = (yield options.getPrisma(req, res));
|
|
39
32
|
if (!prisma) {
|
|
40
|
-
res.status(500).json(
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
let query = {};
|
|
44
|
-
try {
|
|
45
|
-
query = (0, utils_1.buildUrlQuery)(req.query, useSuperJson);
|
|
46
|
-
}
|
|
47
|
-
catch (_a) {
|
|
48
|
-
res.status(400).json((0, utils_1.marshalToObject)({ message: 'invalid query parameters' }, useSuperJson));
|
|
33
|
+
res.status(500).json({ message: 'unable to get prisma from request context' });
|
|
49
34
|
return;
|
|
50
35
|
}
|
|
51
36
|
if (!req.query.path) {
|
|
52
|
-
res.status(400).json(
|
|
37
|
+
res.status(400).json({ message: 'missing path parameter' });
|
|
53
38
|
return;
|
|
54
39
|
}
|
|
55
40
|
const path = req.query.path.join('/');
|
|
@@ -57,17 +42,17 @@ function factory(options) {
|
|
|
57
42
|
const r = yield requestHandler({
|
|
58
43
|
method: req.method,
|
|
59
44
|
path,
|
|
60
|
-
query,
|
|
61
|
-
requestBody:
|
|
45
|
+
query: req.query,
|
|
46
|
+
requestBody: req.body,
|
|
62
47
|
prisma,
|
|
63
|
-
modelMeta
|
|
48
|
+
modelMeta,
|
|
64
49
|
zodSchemas,
|
|
65
50
|
logger: options.logger,
|
|
66
51
|
});
|
|
67
|
-
res.status(r.status).send(
|
|
52
|
+
res.status(r.status).send(r.body);
|
|
68
53
|
}
|
|
69
54
|
catch (err) {
|
|
70
|
-
res.status(500).send(
|
|
55
|
+
res.status(500).send({ message: `An unhandled error occurred: ${err}` });
|
|
71
56
|
}
|
|
72
57
|
});
|
|
73
58
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pages-route-handler.js","sourceRoot":"","sources":["../../src/next/pages-route-handler.ts"],"names":[],"mappings":";AAAA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;gBACrB,MAAM;gBACN,SAAS;gBACT,UAAU;gBACV,MAAM,EAAE,OAAO,CAAC,MAAM;aACzB,CAAC,CAAC;YACH,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;SACrC;QAAC,OAAO,GAAG,EAAE;YACV,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,gCAAgC,GAAG,EAAE,EAAE,CAAC,CAAC;SAC5E;IACL,CAAC,CAAA,CAAC;AACN,CAAC;AAzCD,0BAyCC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zenstackhq/server",
|
|
3
|
-
"version": "1.0.0
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"displayName": "ZenStack Server-side Adapters",
|
|
5
5
|
"description": "ZenStack server-side adapters",
|
|
6
6
|
"homepage": "https://zenstack.dev",
|
|
@@ -9,7 +9,11 @@
|
|
|
9
9
|
"linkDirectory": true
|
|
10
10
|
},
|
|
11
11
|
"keywords": [
|
|
12
|
-
"fastify"
|
|
12
|
+
"fastify",
|
|
13
|
+
"express",
|
|
14
|
+
"nextjs",
|
|
15
|
+
"sveltekit",
|
|
16
|
+
"nuxtjs"
|
|
13
17
|
],
|
|
14
18
|
"author": "",
|
|
15
19
|
"license": "MIT",
|
|
@@ -23,19 +27,19 @@
|
|
|
23
27
|
"url-pattern": "^1.0.3",
|
|
24
28
|
"zod": "3.21.1",
|
|
25
29
|
"zod-validation-error": "^0.2.1",
|
|
26
|
-
"@zenstackhq/openapi": "1.0.0
|
|
27
|
-
"@zenstackhq/runtime": "1.0.0
|
|
30
|
+
"@zenstackhq/openapi": "1.0.0",
|
|
31
|
+
"@zenstackhq/runtime": "1.0.0"
|
|
28
32
|
},
|
|
29
33
|
"devDependencies": {
|
|
30
34
|
"@sveltejs/kit": "1.21.0",
|
|
31
35
|
"@types/body-parser": "^1.19.2",
|
|
32
36
|
"@types/express": "^4.17.17",
|
|
33
37
|
"@types/jest": "^29.5.0",
|
|
34
|
-
"@types/
|
|
38
|
+
"@types/node": "^18.0.0",
|
|
35
39
|
"@types/supertest": "^2.0.12",
|
|
36
|
-
"@types/upper-case-first": "^1.1.2",
|
|
37
40
|
"body-parser": "^1.20.2",
|
|
38
41
|
"copyfiles": "^2.4.1",
|
|
42
|
+
"decimal.js": "^10.4.2",
|
|
39
43
|
"express": "^4.18.2",
|
|
40
44
|
"fastify": "^4.14.1",
|
|
41
45
|
"fastify-plugin": "^4.5.0",
|
|
@@ -46,14 +50,26 @@
|
|
|
46
50
|
"supertest": "^6.3.3",
|
|
47
51
|
"ts-jest": "^29.0.5",
|
|
48
52
|
"typescript": "^4.9.4",
|
|
49
|
-
"@zenstackhq/testtools": "1.0.0
|
|
53
|
+
"@zenstackhq/testtools": "1.0.0"
|
|
54
|
+
},
|
|
55
|
+
"exports": {
|
|
56
|
+
"./package.json": "./package.json",
|
|
57
|
+
"./api/rest": "./api/rest/index.js",
|
|
58
|
+
"./api/rpc": "./api/rpc/index.js",
|
|
59
|
+
"./express": "./express/index.js",
|
|
60
|
+
"./fastify": "./fastify/index.js",
|
|
61
|
+
"./next/app-route-handler": "./next/app-route-handler.js",
|
|
62
|
+
"./next": "./next/index.js",
|
|
63
|
+
"./next/pages-route-handler": "./next/pages-route-handler.js",
|
|
64
|
+
"./sveltekit": "./sveltekit/index.js",
|
|
65
|
+
"./types": "./types.js"
|
|
50
66
|
},
|
|
51
67
|
"scripts": {
|
|
52
68
|
"clean": "rimraf dist",
|
|
53
69
|
"build": "pnpm lint && pnpm clean && tsc && copyfiles ./package.json ./README.md ./LICENSE dist && pnpm pack dist --pack-destination '../../../.build'",
|
|
54
70
|
"watch": "tsc --watch",
|
|
55
71
|
"lint": "eslint src --ext ts",
|
|
56
|
-
"test": "jest",
|
|
72
|
+
"test": "ZENSTACK_TEST=1 jest",
|
|
57
73
|
"publish-dev": "pnpm publish --tag dev"
|
|
58
74
|
}
|
|
59
75
|
}
|
package/shared.d.ts
ADDED
package/shared.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.loadAssets = void 0;
|
|
4
|
+
const runtime_1 = require("@zenstackhq/runtime");
|
|
5
|
+
function loadAssets(options) {
|
|
6
|
+
var _a;
|
|
7
|
+
// model metadata
|
|
8
|
+
const modelMeta = (_a = options.modelMeta) !== null && _a !== void 0 ? _a : (0, runtime_1.getDefaultModelMeta)(options.loadPath);
|
|
9
|
+
// zod schemas
|
|
10
|
+
let zodSchemas;
|
|
11
|
+
if (typeof options.zodSchemas === 'object') {
|
|
12
|
+
zodSchemas = options.zodSchemas;
|
|
13
|
+
}
|
|
14
|
+
else if (options.zodSchemas === true) {
|
|
15
|
+
zodSchemas = (0, runtime_1.getDefaultZodSchemas)(options.loadPath);
|
|
16
|
+
if (!zodSchemas) {
|
|
17
|
+
throw new Error('Unable to load zod schemas from default location');
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return { modelMeta, zodSchemas };
|
|
21
|
+
}
|
|
22
|
+
exports.loadAssets = loadAssets;
|
|
23
|
+
//# sourceMappingURL=shared.js.map
|
package/shared.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared.js","sourceRoot":"","sources":["../src/shared.ts"],"names":[],"mappings":";;;AAAA,iDAA4F;AAG5F,SAAgB,UAAU,CAAC,OAA2B;;IAClD,iBAAiB;IACjB,MAAM,SAAS,GAAG,MAAA,OAAO,CAAC,SAAS,mCAAI,IAAA,6BAAmB,EAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAE7E,cAAc;IACd,IAAI,UAAkC,CAAC;IACvC,IAAI,OAAO,OAAO,CAAC,UAAU,KAAK,QAAQ,EAAE;QACxC,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;KACnC;SAAM,IAAI,OAAO,CAAC,UAAU,KAAK,IAAI,EAAE;QACpC,UAAU,GAAG,IAAA,8BAAoB,EAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;QACpD,IAAI,CAAC,UAAU,EAAE;YACb,MAAM,IAAI,KAAK,CAAC,kDAAkD,CAAC,CAAC;SACvE;KACJ;IAED,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,CAAC;AACrC,CAAC;AAhBD,gCAgBC"}
|
package/sveltekit/handler.js
CHANGED
|
@@ -14,72 +14,49 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
const rpc_1 = __importDefault(require("../api/rpc"));
|
|
16
16
|
const utils_1 = require("../api/utils");
|
|
17
|
-
const
|
|
17
|
+
const shared_1 = require("../shared");
|
|
18
18
|
/**
|
|
19
19
|
* SvelteKit server hooks handler for handling CRUD requests.
|
|
20
20
|
*/
|
|
21
21
|
function createHandler(options) {
|
|
22
22
|
var _a;
|
|
23
23
|
(0, utils_1.logInfo)(options.logger, `ZenStackHandler installing routes at prefix: ${options.prefix}`);
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
const { modelMeta, zodSchemas } = (0, shared_1.loadAssets)(options);
|
|
25
|
+
const requestHandler = (_a = options.handler) !== null && _a !== void 0 ? _a : (0, rpc_1.default)();
|
|
26
|
+
if (options.useSuperJson !== undefined) {
|
|
27
|
+
console.warn('The option "useSuperJson" is deprecated. The server APIs automatically use superjson for serialization.');
|
|
27
28
|
}
|
|
28
|
-
else if (options.zodSchemas === true) {
|
|
29
|
-
zodSchemas = require('@zenstackhq/runtime/zod');
|
|
30
|
-
if (!zodSchemas) {
|
|
31
|
-
throw new Error('Unable to load zod schemas from default location');
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
const requestHanler = (_a = options.handler) !== null && _a !== void 0 ? _a : (0, rpc_1.default)();
|
|
35
|
-
const useSuperJson = options.useSuperJson === true;
|
|
36
29
|
return ({ event, resolve }) => __awaiter(this, void 0, void 0, function* () {
|
|
37
30
|
if (event.url.pathname.startsWith(options.prefix)) {
|
|
38
31
|
const prisma = (yield options.getPrisma(event));
|
|
39
32
|
if (!prisma) {
|
|
40
|
-
return new Response(
|
|
41
|
-
status: 400,
|
|
42
|
-
headers: {
|
|
43
|
-
'content-type': 'application/json',
|
|
44
|
-
},
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
const queryObj = {};
|
|
48
|
-
for (const key of event.url.searchParams.keys()) {
|
|
49
|
-
const values = event.url.searchParams.getAll(key);
|
|
50
|
-
queryObj[key] = values;
|
|
51
|
-
}
|
|
52
|
-
let query = {};
|
|
53
|
-
try {
|
|
54
|
-
query = (0, utils_2.buildUrlQuery)(queryObj, useSuperJson);
|
|
55
|
-
}
|
|
56
|
-
catch (_b) {
|
|
57
|
-
return new Response((0, utils_2.marshalToString)({ message: 'invalid query parameters' }, useSuperJson), {
|
|
33
|
+
return new Response(JSON.stringify({ message: 'unable to get prisma from request context' }), {
|
|
58
34
|
status: 400,
|
|
59
35
|
headers: {
|
|
60
36
|
'content-type': 'application/json',
|
|
61
37
|
},
|
|
62
38
|
});
|
|
63
39
|
}
|
|
40
|
+
const query = Object.fromEntries(event.url.searchParams);
|
|
64
41
|
let requestBody;
|
|
65
42
|
if (event.request.body) {
|
|
66
43
|
const text = yield event.request.text();
|
|
67
44
|
if (text) {
|
|
68
|
-
requestBody =
|
|
45
|
+
requestBody = JSON.parse(text);
|
|
69
46
|
}
|
|
70
47
|
}
|
|
71
48
|
const path = event.url.pathname.substring(options.prefix.length);
|
|
72
49
|
try {
|
|
73
|
-
const r = yield
|
|
50
|
+
const r = yield requestHandler({
|
|
74
51
|
method: event.request.method,
|
|
75
52
|
path,
|
|
76
53
|
query,
|
|
77
54
|
requestBody,
|
|
78
55
|
prisma,
|
|
56
|
+
modelMeta,
|
|
79
57
|
zodSchemas,
|
|
80
|
-
modelMeta: options.modelMeta,
|
|
81
58
|
});
|
|
82
|
-
return new Response(
|
|
59
|
+
return new Response(JSON.stringify(r.body), {
|
|
83
60
|
status: r.status,
|
|
84
61
|
headers: {
|
|
85
62
|
'content-type': 'application/json',
|
|
@@ -87,7 +64,7 @@ function createHandler(options) {
|
|
|
87
64
|
});
|
|
88
65
|
}
|
|
89
66
|
catch (err) {
|
|
90
|
-
return new Response(
|
|
67
|
+
return new Response(JSON.stringify({ message: `An unhandled error occurred: ${err}` }), {
|
|
91
68
|
status: 500,
|
|
92
69
|
headers: {
|
|
93
70
|
'content-type': 'application/json',
|
package/sveltekit/handler.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handler.js","sourceRoot":"","sources":["../../src/sveltekit/handler.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAEA,qDAAuC;AACvC,wCAAuC;
|
|
1
|
+
{"version":3,"file":"handler.js","sourceRoot":"","sources":["../../src/sveltekit/handler.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAEA,qDAAuC;AACvC,wCAAuC;AACvC,sCAAuC;AAkBvC;;GAEG;AACH,SAAwB,aAAa,CAAC,OAAuB;;IACzD,IAAA,eAAO,EAAC,OAAO,CAAC,MAAM,EAAE,gDAAgD,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;IAE1F,MAAM,EAAE,SAAS,EAAE,UAAU,EAAE,GAAG,IAAA,mBAAU,EAAC,OAAO,CAAC,CAAC;IAEtD,MAAM,cAAc,GAAG,MAAA,OAAO,CAAC,OAAO,mCAAI,IAAA,aAAa,GAAE,CAAC;IAC1D,IAAI,OAAO,CAAC,YAAY,KAAK,SAAS,EAAE;QACpC,OAAO,CAAC,IAAI,CACR,yGAAyG,CAC5G,CAAC;KACL;IAED,OAAO,CAAO,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE,EAAE;QAChC,IAAI,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,UAAU,CAAC,OAA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AAI,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,OAAO,EAAE,gCAAgC,GAAG,EAAE,EAAE,CAAC,EAAE;oBACpF,MAAM,EAAE,GAAG;oBACX,OAAO,EAAE;wBACL,cAAc,EAAE,kBAAkB;qBACrC;iBACJ,CAAC,CAAC;aACN;SACJ;QAED,OAAO,OAAO,CAAC,KAAK,CAAC,CAAC;IAC1B,CAAC,CAAA,CAAC;AACN,CAAC;AAhED,gCAgEC"}
|
package/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
1
|
+
import type { ModelMeta, ZodSchemas } from '@zenstackhq/runtime';
|
|
2
|
+
import { RequestContext } from './api/base';
|
|
3
3
|
type LoggerMethod = (message: string, code?: string) => void;
|
|
4
4
|
/**
|
|
5
5
|
* Logger config.
|
|
@@ -10,49 +10,6 @@ export type LoggerConfig = {
|
|
|
10
10
|
warn?: LoggerMethod;
|
|
11
11
|
error?: LoggerMethod;
|
|
12
12
|
};
|
|
13
|
-
/**
|
|
14
|
-
* API request context
|
|
15
|
-
*/
|
|
16
|
-
export type RequestContext = {
|
|
17
|
-
/**
|
|
18
|
-
* The PrismaClient instance
|
|
19
|
-
*/
|
|
20
|
-
prisma: DbClientContract;
|
|
21
|
-
/**
|
|
22
|
-
* The HTTP method
|
|
23
|
-
*/
|
|
24
|
-
method: string;
|
|
25
|
-
/**
|
|
26
|
-
* The request endpoint path (excluding any prefix)
|
|
27
|
-
*/
|
|
28
|
-
path: string;
|
|
29
|
-
/**
|
|
30
|
-
* The query parameters
|
|
31
|
-
*/
|
|
32
|
-
query?: Record<string, string | string[]>;
|
|
33
|
-
/**
|
|
34
|
-
* The request body object
|
|
35
|
-
*/
|
|
36
|
-
requestBody?: unknown;
|
|
37
|
-
/**
|
|
38
|
-
* Model metadata. By default loaded from the standard output location
|
|
39
|
-
* of the `@zenstackhq/model-meta` plugin. You can pass it in explicitly
|
|
40
|
-
* if you configured the plugin to output to a different location.
|
|
41
|
-
*/
|
|
42
|
-
modelMeta?: ModelMeta;
|
|
43
|
-
/**
|
|
44
|
-
* Zod schemas for validating create and update payloads. By default
|
|
45
|
-
* loaded from the standard output location of the `@zenstackhq/zod`
|
|
46
|
-
* plugin. You can pass it in explicitly if you configured the plugin
|
|
47
|
-
* to output to a different location.
|
|
48
|
-
*/
|
|
49
|
-
zodSchemas?: ZodSchemas;
|
|
50
|
-
/**
|
|
51
|
-
* Logging configuration. Set to `null` to disable logging.
|
|
52
|
-
* If unset or set to `undefined`, log will be output to console.
|
|
53
|
-
*/
|
|
54
|
-
logger?: LoggerConfig;
|
|
55
|
-
};
|
|
56
13
|
/**
|
|
57
14
|
* API response
|
|
58
15
|
*/
|
|
@@ -83,6 +40,10 @@ export interface AdapterBaseOptions {
|
|
|
83
40
|
* (need to enable `@core/zod` plugin in schema.zmodel) or omit to disable input validation.
|
|
84
41
|
*/
|
|
85
42
|
zodSchemas?: ZodSchemas | boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Path to load model metadata and zod schemas from. Defaults to `node_modules/.zenstack`.
|
|
45
|
+
*/
|
|
46
|
+
loadPath?: string;
|
|
86
47
|
/**
|
|
87
48
|
* Api request handler function. Can be created using `@zenstackhq/server/api/rest` or `@zenstackhq/server/api/rpc` factory functions.
|
|
88
49
|
* Defaults to RPC-style API handler created with `/api/rpc`.
|
|
@@ -90,6 +51,8 @@ export interface AdapterBaseOptions {
|
|
|
90
51
|
handler?: HandleRequestFn;
|
|
91
52
|
/**
|
|
92
53
|
* Whether to use superjson for serialization/deserialization. Defaults to `false`.
|
|
54
|
+
*
|
|
55
|
+
* @deprecated Not needed anymore and will be removed in a future release.
|
|
93
56
|
*/
|
|
94
57
|
useSuperJson?: boolean;
|
|
95
58
|
}
|
package/utils.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Marshal an object to string
|
|
3
|
-
*/
|
|
4
|
-
export declare function marshalToString(value: unknown, useSuperJson?: boolean): string;
|
|
5
|
-
/**
|
|
6
|
-
* Marshals an object
|
|
7
|
-
*/
|
|
8
|
-
export declare function marshalToObject(value: unknown, useSuperJson?: boolean): any;
|
|
9
|
-
/**
|
|
10
|
-
* Unmarshal a string to object
|
|
11
|
-
*/
|
|
12
|
-
export declare function unmarshalFromString(value: string, useSuperJson?: boolean): any;
|
|
13
|
-
/**
|
|
14
|
-
* Unmarshal an object
|
|
15
|
-
*/
|
|
16
|
-
export declare function unmarshalFromObject(value: unknown, useSuperJson?: boolean): unknown;
|
|
17
|
-
export declare function buildUrlQuery(query: unknown, useSuperJson: boolean): Record<string, string | string[]>;
|