@zenstackhq/trpc 1.0.0-alpha.98 → 1.0.0-alpha.99
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/generator.js +152 -52
- package/generator.js.map +1 -1
- package/helpers.d.ts +1 -2
- package/helpers.js +7 -29
- package/helpers.js.map +1 -1
- package/package.json +3 -3
package/generator.js
CHANGED
|
@@ -25,13 +25,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
26
|
exports.generate = void 0;
|
|
27
27
|
const sdk_1 = require("@zenstackhq/sdk");
|
|
28
|
+
const change_case_1 = require("change-case");
|
|
28
29
|
const fs_1 = require("fs");
|
|
29
30
|
const path_1 = __importDefault(require("path"));
|
|
30
|
-
const generator_1 = require("./zod/generator");
|
|
31
31
|
const helpers_1 = require("./helpers");
|
|
32
32
|
const project_1 = require("./project");
|
|
33
33
|
const removeDir_1 = __importDefault(require("./utils/removeDir"));
|
|
34
|
-
const
|
|
34
|
+
const generator_1 = require("./zod/generator");
|
|
35
35
|
function generate(model, options, dmmf) {
|
|
36
36
|
return __awaiter(this, void 0, void 0, function* () {
|
|
37
37
|
let outDir = options.output;
|
|
@@ -50,29 +50,36 @@ function generate(model, options, dmmf) {
|
|
|
50
50
|
const models = prismaClientDmmf.datamodel.models;
|
|
51
51
|
const hiddenModels = [];
|
|
52
52
|
(0, helpers_1.resolveModelsComments)(models, hiddenModels);
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
53
|
+
createAppRouter(outDir, modelOperations, hiddenModels);
|
|
54
|
+
createHelper(outDir);
|
|
55
|
+
yield project_1.project.save();
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
exports.generate = generate;
|
|
59
|
+
function createAppRouter(outDir, modelOperations, hiddenModels) {
|
|
60
|
+
const appRouter = project_1.project.createSourceFile(path_1.default.resolve(outDir, 'routers', `index.ts`), undefined, {
|
|
61
|
+
overwrite: true,
|
|
62
|
+
});
|
|
63
|
+
appRouter.addStatements('/* eslint-disable */');
|
|
64
|
+
appRouter.addImportDeclarations([
|
|
65
|
+
{
|
|
66
|
+
namedImports: ['AnyRootConfig'],
|
|
67
|
+
moduleSpecifier: '@trpc/server',
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
namedImports: ['PrismaClient'],
|
|
71
|
+
moduleSpecifier: '@prisma/client',
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
namedImports: ['createRouterFactory'],
|
|
75
|
+
moduleSpecifier: '@trpc/server/dist/core/router',
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
namedImports: ['createBuilder'],
|
|
79
|
+
moduleSpecifier: '@trpc/server/dist/core/internals/procedureBuilder',
|
|
80
|
+
},
|
|
81
|
+
]);
|
|
82
|
+
appRouter.addStatements(`
|
|
76
83
|
export type BaseConfig = AnyRootConfig;
|
|
77
84
|
|
|
78
85
|
export type RouterFactory<Config extends BaseConfig> = ReturnType<
|
|
@@ -91,37 +98,34 @@ function generate(model, options, dmmf) {
|
|
|
91
98
|
}
|
|
92
99
|
|
|
93
100
|
`);
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
generateModelCreateRouter(project_1.project, model, operations, outDir);
|
|
111
|
-
appRouter.addImportDeclaration({
|
|
112
|
-
defaultImport: `create${model}Router`,
|
|
113
|
-
moduleSpecifier: `./${model}.router`,
|
|
114
|
-
});
|
|
115
|
-
writer.writeLine(`${(0, change_case_1.camelCase)(model)}: create${model}Router<Config>(router, procedure),`);
|
|
101
|
+
const createFunction = appRouter.addFunction({
|
|
102
|
+
name: 'createRouter<Config extends BaseConfig>',
|
|
103
|
+
parameters: [
|
|
104
|
+
{ name: 'router', type: 'RouterFactory<Config>' },
|
|
105
|
+
{ name: 'procedure', type: 'ProcBuilder<Config>' },
|
|
106
|
+
],
|
|
107
|
+
isExported: true,
|
|
108
|
+
});
|
|
109
|
+
createFunction.setBodyText((writer) => {
|
|
110
|
+
writer.write('return router(');
|
|
111
|
+
writer.block(() => {
|
|
112
|
+
for (const modelOperation of modelOperations) {
|
|
113
|
+
const { model } = modelOperation, operations = __rest(modelOperation, ["model"]);
|
|
114
|
+
if (hiddenModels.includes(model)) {
|
|
115
|
+
continue;
|
|
116
116
|
}
|
|
117
|
-
|
|
118
|
-
|
|
117
|
+
generateModelCreateRouter(project_1.project, model, operations, outDir);
|
|
118
|
+
appRouter.addImportDeclaration({
|
|
119
|
+
defaultImport: `create${model}Router`,
|
|
120
|
+
moduleSpecifier: `./${model}.router`,
|
|
121
|
+
});
|
|
122
|
+
writer.writeLine(`${(0, change_case_1.camelCase)(model)}: create${model}Router<Config>(router, procedure),`);
|
|
123
|
+
}
|
|
119
124
|
});
|
|
120
|
-
|
|
121
|
-
yield project_1.project.save();
|
|
125
|
+
writer.write(');');
|
|
122
126
|
});
|
|
127
|
+
appRouter.formatText();
|
|
123
128
|
}
|
|
124
|
-
exports.generate = generate;
|
|
125
129
|
function generateModelCreateRouter(project, model, operations, outputDir) {
|
|
126
130
|
const modelRouter = project.createSourceFile(path_1.default.resolve(outputDir, 'routers', `${model}.router.ts`), undefined, {
|
|
127
131
|
overwrite: true,
|
|
@@ -134,6 +138,7 @@ function generateModelCreateRouter(project, model, operations, outputDir) {
|
|
|
134
138
|
},
|
|
135
139
|
]);
|
|
136
140
|
(0, helpers_1.generateRouterSchemaImports)(modelRouter, model);
|
|
141
|
+
(0, helpers_1.generateHelperImport)(modelRouter);
|
|
137
142
|
modelRouter
|
|
138
143
|
.addFunction({
|
|
139
144
|
name: 'createRouter<Config extends BaseConfig>',
|
|
@@ -159,4 +164,99 @@ function generateModelCreateRouter(project, model, operations, outputDir) {
|
|
|
159
164
|
});
|
|
160
165
|
modelRouter.formatText();
|
|
161
166
|
}
|
|
167
|
+
function createHelper(outDir) {
|
|
168
|
+
const sf = project_1.project.createSourceFile(path_1.default.resolve(outDir, 'helper.ts'), undefined, {
|
|
169
|
+
overwrite: true,
|
|
170
|
+
});
|
|
171
|
+
sf.addStatements(`import { TRPCError } from '@trpc/server';`);
|
|
172
|
+
sf.addStatements(`import { isPrismaClientKnownRequestError } from '${sdk_1.RUNTIME_PACKAGE}';`);
|
|
173
|
+
const checkMutate = sf.addFunction({
|
|
174
|
+
name: 'checkMutate',
|
|
175
|
+
typeParameters: [{ name: 'T' }],
|
|
176
|
+
parameters: [
|
|
177
|
+
{
|
|
178
|
+
name: 'promise',
|
|
179
|
+
type: 'Promise<T>',
|
|
180
|
+
},
|
|
181
|
+
],
|
|
182
|
+
isAsync: true,
|
|
183
|
+
isExported: true,
|
|
184
|
+
returnType: 'Promise<T | undefined>',
|
|
185
|
+
});
|
|
186
|
+
checkMutate.setBodyText(`try {
|
|
187
|
+
return await promise;
|
|
188
|
+
} catch (err: any) {
|
|
189
|
+
if (isPrismaClientKnownRequestError(err)) {
|
|
190
|
+
if (err.code === 'P2004') {
|
|
191
|
+
if (err.meta?.reason === '${sdk_1.CrudFailureReason.RESULT_NOT_READABLE}') {
|
|
192
|
+
// unable to readback data
|
|
193
|
+
return undefined;
|
|
194
|
+
} else {
|
|
195
|
+
// rejected by policy
|
|
196
|
+
throw new TRPCError({
|
|
197
|
+
code: 'FORBIDDEN',
|
|
198
|
+
message: err.message,
|
|
199
|
+
cause: err,
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
} else {
|
|
203
|
+
// request error
|
|
204
|
+
throw new TRPCError({
|
|
205
|
+
code: 'BAD_REQUEST',
|
|
206
|
+
message: err.message,
|
|
207
|
+
cause: err,
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
} else {
|
|
211
|
+
throw err;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
`);
|
|
215
|
+
checkMutate.formatText();
|
|
216
|
+
const checkRead = sf.addFunction({
|
|
217
|
+
name: 'checkRead',
|
|
218
|
+
typeParameters: [{ name: 'T' }],
|
|
219
|
+
parameters: [
|
|
220
|
+
{
|
|
221
|
+
name: 'promise',
|
|
222
|
+
type: 'Promise<T>',
|
|
223
|
+
},
|
|
224
|
+
],
|
|
225
|
+
isAsync: true,
|
|
226
|
+
isExported: true,
|
|
227
|
+
returnType: 'Promise<T>',
|
|
228
|
+
});
|
|
229
|
+
checkRead.setBodyText(`try {
|
|
230
|
+
return await promise;
|
|
231
|
+
} catch (err: any) {
|
|
232
|
+
if (isPrismaClientKnownRequestError(err)) {
|
|
233
|
+
if (err.code === 'P2004') {
|
|
234
|
+
// rejected by policy
|
|
235
|
+
throw new TRPCError({
|
|
236
|
+
code: 'FORBIDDEN',
|
|
237
|
+
message: err.message,
|
|
238
|
+
cause: err,
|
|
239
|
+
});
|
|
240
|
+
} else if (err.code === 'P2025') {
|
|
241
|
+
// not found
|
|
242
|
+
throw new TRPCError({
|
|
243
|
+
code: 'NOT_FOUND',
|
|
244
|
+
message: err.message,
|
|
245
|
+
cause: err,
|
|
246
|
+
});
|
|
247
|
+
} else {
|
|
248
|
+
// request error
|
|
249
|
+
throw new TRPCError({
|
|
250
|
+
code: 'BAD_REQUEST',
|
|
251
|
+
message: err.message,
|
|
252
|
+
cause: err,
|
|
253
|
+
})
|
|
254
|
+
}
|
|
255
|
+
} else {
|
|
256
|
+
throw err;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
`);
|
|
260
|
+
checkRead.formatText();
|
|
261
|
+
}
|
|
162
262
|
//# sourceMappingURL=generator.js.map
|
package/generator.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generator.js","sourceRoot":"","sources":["../src/generator.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AACA,
|
|
1
|
+
{"version":3,"file":"generator.js","sourceRoot":"","sources":["../src/generator.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AACA,yCAAiG;AAEjG,6CAAwC;AACxC,2BAAoC;AACpC,gDAAwB;AAExB,uCAMmB;AACnB,uCAAoC;AACpC,kEAA0C;AAC1C,+CAAiE;AAEjE,SAAsB,QAAQ,CAAC,KAAY,EAAE,OAAsB,EAAE,IAAmB;;QACpF,IAAI,MAAM,GAAG,OAAO,CAAC,MAAgB,CAAC;QACtC,IAAI,CAAC,MAAM,EAAE;YACT,MAAM,IAAI,iBAAW,CAAC,6BAA6B,CAAC,CAAC;SACxD;QAED,IAAI,CAAC,cAAI,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE;YAC1B,0DAA0D;YAC1D,MAAM,GAAG,cAAI,CAAC,IAAI,CAAC,cAAI,CAAC,OAAO,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC,CAAC;SAChE;QAED,MAAM,aAAE,CAAC,KAAK,CAAC,MAAM,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QAC5C,MAAM,IAAA,mBAAS,EAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QAE9B,MAAM,IAAA,oBAAkB,EAAC,KAAK,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;QAE/C,MAAM,gBAAgB,GAAG,IAAI,CAAC;QAE9B,MAAM,eAAe,GAAG,gBAAgB,CAAC,QAAQ,CAAC,eAAe,CAAC;QAClE,MAAM,MAAM,GAAG,gBAAgB,CAAC,SAAS,CAAC,MAAM,CAAC;QACjD,MAA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
|
3
|
-
export declare const generatetRPCImport: (sourceFile: SourceFile) => void;
|
|
4
|
-
export declare const generateRouterImport: (sourceFile: SourceFile, modelNamePlural: string, modelNameCamelCase: string) => void;
|
|
5
3
|
export declare function generateProcedure(writer: CodeBlockWriter, opType: string, typeName: string, modelName: string, baseOpType: string): void;
|
|
6
4
|
export declare function generateRouterSchemaImports(sourceFile: SourceFile, name: string): void;
|
|
5
|
+
export declare function generateHelperImport(sourceFile: SourceFile): void;
|
|
7
6
|
export declare const getInputTypeByOpName: (opName: string, modelName: string) => string | undefined;
|
|
8
7
|
export declare const getProcedureTypeByOpName: (opName: string) => string | undefined;
|
|
9
8
|
export declare function resolveModelsComments(models: DMMF.Model[], hiddenModels: string[]): void;
|
package/helpers.js
CHANGED
|
@@ -1,44 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.resolveModelsComments = exports.getProcedureTypeByOpName = exports.getInputTypeByOpName = exports.
|
|
4
|
-
const sdk_1 = require("@zenstackhq/sdk");
|
|
3
|
+
exports.resolveModelsComments = exports.getProcedureTypeByOpName = exports.getInputTypeByOpName = exports.generateHelperImport = exports.generateRouterSchemaImports = exports.generateProcedure = void 0;
|
|
5
4
|
const uncapitalizeFirstLetter_1 = require("./utils/uncapitalizeFirstLetter");
|
|
6
|
-
const generatetRPCImport = (sourceFile) => {
|
|
7
|
-
sourceFile.addImportDeclaration({
|
|
8
|
-
moduleSpecifier: '@trpc/server',
|
|
9
|
-
namespaceImport: 'trpc',
|
|
10
|
-
});
|
|
11
|
-
};
|
|
12
|
-
exports.generatetRPCImport = generatetRPCImport;
|
|
13
|
-
const generateRouterImport = (sourceFile, modelNamePlural, modelNameCamelCase) => {
|
|
14
|
-
sourceFile.addImportDeclaration({
|
|
15
|
-
moduleSpecifier: `./${modelNameCamelCase}.router`,
|
|
16
|
-
namedImports: [`${modelNamePlural}Router`],
|
|
17
|
-
});
|
|
18
|
-
};
|
|
19
|
-
exports.generateRouterImport = generateRouterImport;
|
|
20
5
|
function generateProcedure(writer, opType, typeName, modelName, baseOpType) {
|
|
21
6
|
const procType = (0, exports.getProcedureTypeByOpName)(baseOpType);
|
|
22
7
|
const prismaMethod = opType.replace('One', '');
|
|
23
8
|
if (procType === 'query') {
|
|
24
9
|
writer.write(`
|
|
25
|
-
${opType}: procedure.input(${typeName}).query(({ctx, input}) => db(ctx).${(0, uncapitalizeFirstLetter_1.uncapitalizeFirstLetter)(modelName)}.${prismaMethod}(input)),
|
|
10
|
+
${opType}: procedure.input(${typeName}).query(({ctx, input}) => checkRead(db(ctx).${(0, uncapitalizeFirstLetter_1.uncapitalizeFirstLetter)(modelName)}.${prismaMethod}(input))),
|
|
26
11
|
`);
|
|
27
12
|
}
|
|
28
13
|
else if (procType === 'mutation') {
|
|
29
14
|
writer.write(`
|
|
30
|
-
${opType}: procedure.input(${typeName}).mutation(async ({ctx, input}) => {
|
|
31
|
-
try {
|
|
32
|
-
return await db(ctx).${(0, uncapitalizeFirstLetter_1.uncapitalizeFirstLetter)(modelName)}.${prismaMethod}(input);
|
|
33
|
-
} catch (err: any) {
|
|
34
|
-
if (err.code === 'P2004' && err.meta?.reason === '${sdk_1.CrudFailureReason.RESULT_NOT_READABLE}') {
|
|
35
|
-
// unable to readback data
|
|
36
|
-
return undefined;
|
|
37
|
-
} else {
|
|
38
|
-
throw err;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}),
|
|
15
|
+
${opType}: procedure.input(${typeName}).mutation(async ({ctx, input}) => checkMutate(db(ctx).${(0, uncapitalizeFirstLetter_1.uncapitalizeFirstLetter)(modelName)}.${prismaMethod}(input))),
|
|
42
16
|
`);
|
|
43
17
|
}
|
|
44
18
|
}
|
|
@@ -47,6 +21,10 @@ function generateRouterSchemaImports(sourceFile, name) {
|
|
|
47
21
|
sourceFile.addStatements(`import { ${name}Schema } from '../schemas/${name}.schema';`);
|
|
48
22
|
}
|
|
49
23
|
exports.generateRouterSchemaImports = generateRouterSchemaImports;
|
|
24
|
+
function generateHelperImport(sourceFile) {
|
|
25
|
+
sourceFile.addStatements(`import { checkRead, checkMutate } from '../helper';`);
|
|
26
|
+
}
|
|
27
|
+
exports.generateHelperImport = generateHelperImport;
|
|
50
28
|
const getInputTypeByOpName = (opName, modelName) => {
|
|
51
29
|
let inputType;
|
|
52
30
|
switch (opName) {
|
package/helpers.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":";;;AAEA,6EAA0E;AAE1E,SAAgB,iBAAiB,CAC7B,MAAuB,EACvB,MAAc,EACd,QAAgB,EAChB,SAAiB,EACjB,UAAkB;IAElB,MAAM,QAAQ,GAAG,IAAA,gCAAwB,EAAC,UAAU,CAAC,CAAC;IACtD,MAAM,YAAY,GAAG,MAAM,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;IAE/C,IAAI,QAAQ,KAAK,OAAO,EAAE;QACtB,MAAM,CAAC,KAAK,CAAC;UACX,MAAM,qBAAqB,QAAQ,+CAA+C,IAAA,iDAAuB,EACvG,SAAS,CACZ,IAAI,YAAY;KACpB,CAAC,CAAC;KACF;SAAM,IAAI,QAAQ,KAAK,UAAU,EAAE;QAChC,MAAM,CAAC,KAAK,CAAC;UACX,MAAM,qBAAqB,QAAQ,0DAA0D,IAAA,iDAAuB,EAClH,SAAS,CACZ,IAAI,YAAY;KACpB,CAAC,CAAC;KACF;AACL,CAAC;AAvBD,8CAuBC;AAED,SAAgB,2BAA2B,CAAC,UAAsB,EAAE,IAAY;IAC5E,UAAU,CAAC,aAAa,CAAC,YAAY,IAAI,6BAA6B,IAAI,WAAW,CAAC,CAAC;AAC3F,CAAC;AAFD,kEAEC;AAED,SAAgB,oBAAoB,CAAC,UAAsB;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,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;aACjC;SACJ;KACJ;AACL,CAAC;AAhCD,sDAgCC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zenstackhq/trpc",
|
|
3
3
|
"displayName": "ZenStack plugin for tRPC",
|
|
4
|
-
"version": "1.0.0-alpha.
|
|
4
|
+
"version": "1.0.0-alpha.99",
|
|
5
5
|
"description": "ZenStack plugin for tRPC",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"repository": {
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"ts-morph": "^16.0.0",
|
|
24
24
|
"tslib": "^2.4.1",
|
|
25
25
|
"zod": "^3.19.1",
|
|
26
|
-
"@zenstackhq/sdk": "1.0.0-alpha.
|
|
26
|
+
"@zenstackhq/sdk": "1.0.0-alpha.99"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@types/jest": "^29.5.0",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"rimraf": "^3.0.2",
|
|
34
34
|
"ts-jest": "^29.0.5",
|
|
35
35
|
"typescript": "^4.9.4",
|
|
36
|
-
"@zenstackhq/testtools": "1.0.0-alpha.
|
|
36
|
+
"@zenstackhq/testtools": "1.0.0-alpha.99"
|
|
37
37
|
},
|
|
38
38
|
"scripts": {
|
|
39
39
|
"clean": "rimraf dist",
|