@zenstackhq/trpc 1.0.0-beta.2 → 1.0.0-beta.21
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.d.ts +1 -1
- package/generator.js +198 -59
- package/generator.js.map +1 -1
- package/helpers.d.ts +9 -3
- package/helpers.js +227 -22
- package/helpers.js.map +1 -1
- package/index.d.ts +2 -1
- package/index.js +2 -1
- package/index.js.map +1 -1
- package/package.json +15 -8
- package/res/client/next.ts +17 -0
- package/res/client/react.ts +17 -0
- package/res/client/utils.ts +32 -0
- package/utils/uncapitalizeFirstLetter.d.ts +0 -1
- package/utils/uncapitalizeFirstLetter.js +0 -8
- package/utils/uncapitalizeFirstLetter.js.map +0 -1
- package/zod/generator.d.ts +0 -4
- package/zod/generator.js +0 -92
- package/zod/generator.js.map +0 -1
- package/zod/index.d.ts +0 -5
- package/zod/index.js +0 -21
- package/zod/index.js.map +0 -1
- package/zod/transformer.d.ts +0 -65
- package/zod/transformer.js +0 -446
- package/zod/transformer.js.map +0 -1
- package/zod/types.d.ts +0 -20
- package/zod/types.js +0 -3
- package/zod/types.js.map +0 -1
- package/zod/utils/formatFile.d.ts +0 -1
- package/zod/utils/formatFile.js +0 -32
- package/zod/utils/formatFile.js.map +0 -1
- package/zod/utils/removeDir.d.ts +0 -1
- package/zod/utils/removeDir.js +0 -30
- package/zod/utils/removeDir.js.map +0 -1
- package/zod/utils/writeFileSafely.d.ts +0 -1
- package/zod/utils/writeFileSafely.js +0 -26
- package/zod/utils/writeFileSafely.js.map +0 -1
package/generator.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DMMF } from '@prisma/generator-helper';
|
|
1
|
+
import type { DMMF } from '@prisma/generator-helper';
|
|
2
2
|
import { PluginOptions } from '@zenstackhq/sdk';
|
|
3
3
|
import { Model } from '@zenstackhq/sdk/ast';
|
|
4
4
|
export declare function generate(model: Model, options: PluginOptions, dmmf: DMMF.Document): Promise<void>;
|
package/generator.js
CHANGED
|
@@ -25,85 +25,129 @@ 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 fs_1 = require("fs");
|
|
28
|
+
const fs_1 = __importDefault(require("fs"));
|
|
29
29
|
const lower_case_first_1 = require("lower-case-first");
|
|
30
30
|
const path_1 = __importDefault(require("path"));
|
|
31
|
+
const ts_morph_1 = require("ts-morph");
|
|
32
|
+
const upper_case_first_1 = require("upper-case-first");
|
|
31
33
|
const _1 = require(".");
|
|
32
34
|
const helpers_1 = require("./helpers");
|
|
33
35
|
const project_1 = require("./project");
|
|
34
36
|
const removeDir_1 = __importDefault(require("./utils/removeDir"));
|
|
35
|
-
const generator_1 = require("./zod/generator");
|
|
36
37
|
function generate(model, options, dmmf) {
|
|
38
|
+
var _a;
|
|
37
39
|
return __awaiter(this, void 0, void 0, function* () {
|
|
38
40
|
let outDir = (0, sdk_1.requireOption)(options, 'output');
|
|
39
41
|
outDir = (0, sdk_1.resolvePath)(outDir, options);
|
|
40
42
|
// resolve "generateModelActions" option
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
.split(',')
|
|
47
|
-
.filter((i) => !!i)
|
|
48
|
-
.map((i) => i.trim());
|
|
49
|
-
}
|
|
50
|
-
else if (Array.isArray(options.generateModelActions) &&
|
|
51
|
-
options.generateModelActions.every((i) => typeof i === 'string')) {
|
|
52
|
-
// string array
|
|
53
|
-
generateModelActions = options.generateModelActions;
|
|
54
|
-
}
|
|
55
|
-
else {
|
|
56
|
-
throw new sdk_1.PluginError(_1.name, `Invalid "generateModelActions" option: must be a comma-separated string or an array of strings`);
|
|
57
|
-
}
|
|
43
|
+
const generateModelActions = parseOptionAsStrings(options, 'generateModelActions');
|
|
44
|
+
// resolve "generateClientHelpers" option
|
|
45
|
+
const generateClientHelpers = parseOptionAsStrings(options, 'generateClientHelpers');
|
|
46
|
+
if (generateClientHelpers && !generateClientHelpers.every((v) => ['react', 'next'].includes(v))) {
|
|
47
|
+
throw new sdk_1.PluginError(_1.name, `Option "generateClientHelpers" only support values "react" and "next"`);
|
|
58
48
|
}
|
|
59
|
-
|
|
49
|
+
if (options.zodSchemasImport && typeof options.zodSchemasImport !== 'string') {
|
|
50
|
+
throw new sdk_1.PluginError(_1.name, `Option "zodSchemasImport" must be a string`);
|
|
51
|
+
}
|
|
52
|
+
yield fs_1.default.promises.mkdir(outDir, { recursive: true });
|
|
60
53
|
yield (0, removeDir_1.default)(outDir, true);
|
|
61
|
-
yield (0, generator_1.generate)(model, options, dmmf);
|
|
62
54
|
const prismaClientDmmf = dmmf;
|
|
63
55
|
const modelOperations = prismaClientDmmf.mappings.modelOperations;
|
|
64
56
|
const models = prismaClientDmmf.datamodel.models;
|
|
65
57
|
const hiddenModels = [];
|
|
66
58
|
(0, helpers_1.resolveModelsComments)(models, hiddenModels);
|
|
67
|
-
|
|
59
|
+
const zodSchemasImport = (_a = options.zodSchemasImport) !== null && _a !== void 0 ? _a : '@zenstackhq/runtime/zod';
|
|
60
|
+
createAppRouter(outDir, modelOperations, hiddenModels, generateModelActions, generateClientHelpers, model, zodSchemasImport);
|
|
68
61
|
createHelper(outDir);
|
|
69
62
|
yield (0, sdk_1.saveProject)(project_1.project);
|
|
70
63
|
});
|
|
71
64
|
}
|
|
72
65
|
exports.generate = generate;
|
|
73
|
-
function createAppRouter(outDir, modelOperations, hiddenModels, generateModelActions) {
|
|
74
|
-
const
|
|
66
|
+
function createAppRouter(outDir, modelOperations, hiddenModels, generateModelActions, generateClientHelpers, zmodel, zodSchemasImport) {
|
|
67
|
+
const indexFile = path_1.default.resolve(outDir, 'routers', `index.ts`);
|
|
68
|
+
const appRouter = project_1.project.createSourceFile(indexFile, undefined, {
|
|
75
69
|
overwrite: true,
|
|
76
70
|
});
|
|
77
71
|
appRouter.addStatements('/* eslint-disable */');
|
|
72
|
+
const prismaImport = (0, sdk_1.getPrismaClientImportSpec)(zmodel, path_1.default.dirname(indexFile));
|
|
78
73
|
appRouter.addImportDeclarations([
|
|
79
74
|
{
|
|
80
|
-
namedImports: ['AnyRootConfig'],
|
|
75
|
+
namedImports: ['type AnyRootConfig', 'type Procedure', 'type ProcedureParams', 'type ProcedureType'],
|
|
81
76
|
moduleSpecifier: '@trpc/server',
|
|
82
77
|
},
|
|
83
78
|
{
|
|
84
|
-
namedImports: ['PrismaClient'],
|
|
85
|
-
moduleSpecifier:
|
|
79
|
+
namedImports: ['type PrismaClient', 'type Prisma'],
|
|
80
|
+
moduleSpecifier: prismaImport,
|
|
86
81
|
},
|
|
87
82
|
{
|
|
88
|
-
namedImports: ['createRouterFactory'],
|
|
83
|
+
namedImports: ['type createRouterFactory', 'AnyRouter'],
|
|
89
84
|
moduleSpecifier: '@trpc/server/dist/core/router',
|
|
90
85
|
},
|
|
91
86
|
{
|
|
92
|
-
namedImports: ['
|
|
87
|
+
namedImports: ['type ProcedureBuilder'],
|
|
93
88
|
moduleSpecifier: '@trpc/server/dist/core/internals/procedureBuilder',
|
|
94
89
|
},
|
|
90
|
+
{ defaultImport: 'z', moduleSpecifier: 'zod', isTypeOnly: true },
|
|
95
91
|
]);
|
|
96
92
|
appRouter.addStatements(`
|
|
93
|
+
${ /** to be used by the other routers without making a bigger commit */''}
|
|
94
|
+
export { PrismaClient } from '${prismaImport}';
|
|
95
|
+
|
|
97
96
|
export type BaseConfig = AnyRootConfig;
|
|
98
97
|
|
|
99
98
|
export type RouterFactory<Config extends BaseConfig> = ReturnType<
|
|
100
99
|
typeof createRouterFactory<Config>
|
|
101
100
|
>;
|
|
102
101
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
102
|
+
${
|
|
103
|
+
/** this is needed in order to prevent type errors between a procedure and a middleware-extended procedure */ ''}
|
|
104
|
+
export type ProcBuilder<Config extends BaseConfig> = ProcedureBuilder<{
|
|
105
|
+
_config: Config;
|
|
106
|
+
_ctx_out: Config['$types']['ctx'];
|
|
107
|
+
_input_in: any;
|
|
108
|
+
_input_out: any;
|
|
109
|
+
_output_in: any;
|
|
110
|
+
_output_out: any;
|
|
111
|
+
_meta: Config['$types']['meta'];
|
|
112
|
+
}>;
|
|
113
|
+
|
|
114
|
+
type ExtractParamsFromProcBuilder<Builder extends ProcedureBuilder<any>> =
|
|
115
|
+
Builder extends ProcedureBuilder<infer P> ? P : never;
|
|
106
116
|
|
|
117
|
+
type FromPromise<P extends Promise<any>> = P extends Promise<infer T>
|
|
118
|
+
? T
|
|
119
|
+
: never;
|
|
120
|
+
|
|
121
|
+
${ /** workaround to avoid creating 'typeof unsetMarker & object' on the procedure output */''}
|
|
122
|
+
type Join<A, B> = A extends symbol ? B : A & B;
|
|
123
|
+
|
|
124
|
+
${
|
|
125
|
+
/** you can name it whatever you want, but this is to make sure that
|
|
126
|
+
the types from the middleware and the procedure are correctly merged */ ''}
|
|
127
|
+
export type ProcReturns<
|
|
128
|
+
PType extends ProcedureType,
|
|
129
|
+
PBuilder extends ProcBuilder<BaseConfig>,
|
|
130
|
+
ZType extends z.ZodType,
|
|
131
|
+
PPromise extends Prisma.PrismaPromise<any>
|
|
132
|
+
> = Procedure<
|
|
133
|
+
PType,
|
|
134
|
+
ProcedureParams<
|
|
135
|
+
ExtractParamsFromProcBuilder<PBuilder>["_config"],
|
|
136
|
+
ExtractParamsFromProcBuilder<PBuilder>["_ctx_out"],
|
|
137
|
+
Join<ExtractParamsFromProcBuilder<PBuilder>["_input_in"], z.infer<ZType>>,
|
|
138
|
+
Join<ExtractParamsFromProcBuilder<PBuilder>["_input_out"], z.infer<ZType>>,
|
|
139
|
+
Join<
|
|
140
|
+
ExtractParamsFromProcBuilder<PBuilder>["_output_in"],
|
|
141
|
+
FromPromise<PPromise>
|
|
142
|
+
>,
|
|
143
|
+
Join<
|
|
144
|
+
ExtractParamsFromProcBuilder<PBuilder>["_output_out"],
|
|
145
|
+
FromPromise<PPromise>
|
|
146
|
+
>,
|
|
147
|
+
ExtractParamsFromProcBuilder<PBuilder>["_meta"]
|
|
148
|
+
>
|
|
149
|
+
>;
|
|
150
|
+
|
|
107
151
|
export function db(ctx: any) {
|
|
108
152
|
if (!ctx.prisma) {
|
|
109
153
|
throw new Error('Missing "prisma" field in trpc context');
|
|
@@ -112,73 +156,148 @@ function createAppRouter(outDir, modelOperations, hiddenModels, generateModelAct
|
|
|
112
156
|
}
|
|
113
157
|
|
|
114
158
|
`);
|
|
115
|
-
const
|
|
116
|
-
|
|
159
|
+
const filteredModelOperations = modelOperations.filter((mo) => !hiddenModels.includes(mo.model));
|
|
160
|
+
appRouter
|
|
161
|
+
.addFunction({
|
|
162
|
+
name: 'createRouter<Router extends RouterFactory<BaseConfig>, Proc extends ProcBuilder<BaseConfig>>',
|
|
117
163
|
parameters: [
|
|
118
|
-
{ name: 'router', type: '
|
|
119
|
-
{ name: 'procedure', type: '
|
|
164
|
+
{ name: 'router', type: 'Router' },
|
|
165
|
+
{ name: 'procedure', type: 'Proc' },
|
|
120
166
|
],
|
|
121
167
|
isExported: true,
|
|
122
|
-
})
|
|
123
|
-
|
|
168
|
+
})
|
|
169
|
+
.setBodyText((writer) => {
|
|
124
170
|
writer.write('return router(');
|
|
125
171
|
writer.block(() => {
|
|
126
|
-
for (const modelOperation of
|
|
172
|
+
for (const modelOperation of filteredModelOperations) {
|
|
127
173
|
const { model } = modelOperation, operations = __rest(modelOperation, ["model"]);
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
generateModelCreateRouter(project_1.project, model, operations, outDir, generateModelActions);
|
|
174
|
+
// "count" operation is missing from Prisma DMMF, add it here
|
|
175
|
+
operations.count = `count${model}`;
|
|
176
|
+
generateModelCreateRouter(project_1.project, model, operations, outDir, generateModelActions, generateClientHelpers, zmodel, zodSchemasImport);
|
|
132
177
|
appRouter.addImportDeclaration({
|
|
133
178
|
defaultImport: `create${model}Router`,
|
|
134
179
|
moduleSpecifier: `./${model}.router`,
|
|
135
180
|
});
|
|
136
|
-
writer.writeLine(`${(0, lower_case_first_1.lowerCaseFirst)(model)}: create${model}Router<
|
|
181
|
+
writer.writeLine(`${(0, lower_case_first_1.lowerCaseFirst)(model)}: create${model}Router<Router, Proc>(router, procedure),`);
|
|
137
182
|
}
|
|
138
183
|
});
|
|
139
184
|
writer.write(');');
|
|
140
185
|
});
|
|
186
|
+
if (generateClientHelpers) {
|
|
187
|
+
appRouter.addInterface({
|
|
188
|
+
name: 'ClientType',
|
|
189
|
+
typeParameters: ['AppRouter extends AnyRouter'],
|
|
190
|
+
isExported: true,
|
|
191
|
+
properties: filteredModelOperations.map(({ model }) => {
|
|
192
|
+
appRouter.addImportDeclaration({
|
|
193
|
+
namedImports: [{ name: 'ClientType', alias: `${(0, upper_case_first_1.upperCaseFirst)(model)}ClientType` }],
|
|
194
|
+
moduleSpecifier: `./${model}.router`,
|
|
195
|
+
});
|
|
196
|
+
return {
|
|
197
|
+
name: (0, lower_case_first_1.lowerCaseFirst)(model),
|
|
198
|
+
type: `${(0, upper_case_first_1.upperCaseFirst)(model)}ClientType<AppRouter>`,
|
|
199
|
+
};
|
|
200
|
+
}),
|
|
201
|
+
});
|
|
202
|
+
createClientHelpers(outDir, generateClientHelpers);
|
|
203
|
+
}
|
|
141
204
|
appRouter.formatText();
|
|
142
205
|
}
|
|
143
|
-
function
|
|
206
|
+
function createClientHelpers(outputDir, generateClientHelpers) {
|
|
207
|
+
const utils = project_1.project.createSourceFile(path_1.default.resolve(outputDir, 'client', `utils.ts`), undefined, {
|
|
208
|
+
overwrite: true,
|
|
209
|
+
});
|
|
210
|
+
utils.replaceWithText(fs_1.default.readFileSync(path_1.default.join(__dirname, './res/client/utils.ts'), 'utf-8'));
|
|
211
|
+
for (const client of generateClientHelpers) {
|
|
212
|
+
switch (client) {
|
|
213
|
+
case 'react': {
|
|
214
|
+
const content = fs_1.default.readFileSync(path_1.default.join(__dirname, './res/client/react.ts'), 'utf-8');
|
|
215
|
+
project_1.project.createSourceFile(path_1.default.resolve(outputDir, 'client', 'react.ts'), content, {
|
|
216
|
+
overwrite: true,
|
|
217
|
+
});
|
|
218
|
+
break;
|
|
219
|
+
}
|
|
220
|
+
case 'next': {
|
|
221
|
+
const content = fs_1.default.readFileSync(path_1.default.join(__dirname, './res/client/next.ts'), 'utf-8');
|
|
222
|
+
project_1.project.createSourceFile(path_1.default.resolve(outputDir, 'client', 'next.ts'), content, { overwrite: true });
|
|
223
|
+
break;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
function generateModelCreateRouter(project, model, operations, outputDir, generateModelActions, generateClientHelpers, zmodel, zodSchemasImport) {
|
|
144
229
|
const modelRouter = project.createSourceFile(path_1.default.resolve(outputDir, 'routers', `${model}.router.ts`), undefined, {
|
|
145
230
|
overwrite: true,
|
|
146
231
|
});
|
|
147
232
|
modelRouter.addStatements('/* eslint-disable */');
|
|
148
233
|
modelRouter.addImportDeclarations([
|
|
149
234
|
{
|
|
150
|
-
namedImports: [
|
|
235
|
+
namedImports: [
|
|
236
|
+
'type RouterFactory',
|
|
237
|
+
'type ProcBuilder',
|
|
238
|
+
'type BaseConfig',
|
|
239
|
+
'type ProcReturns',
|
|
240
|
+
'type PrismaClient',
|
|
241
|
+
'db',
|
|
242
|
+
],
|
|
151
243
|
moduleSpecifier: '.',
|
|
152
244
|
},
|
|
153
245
|
]);
|
|
154
|
-
(0, helpers_1.generateRouterSchemaImports)(modelRouter, model);
|
|
246
|
+
(0, helpers_1.generateRouterSchemaImports)(modelRouter, (0, upper_case_first_1.upperCaseFirst)(model), zodSchemasImport);
|
|
155
247
|
(0, helpers_1.generateHelperImport)(modelRouter);
|
|
156
|
-
|
|
157
|
-
.
|
|
158
|
-
|
|
248
|
+
if (generateClientHelpers) {
|
|
249
|
+
(0, helpers_1.generateRouterTypingImports)(modelRouter, zmodel);
|
|
250
|
+
}
|
|
251
|
+
const createRouterFunc = modelRouter.addFunction({
|
|
252
|
+
name: 'createRouter<Router extends RouterFactory<BaseConfig>, Proc extends ProcBuilder<BaseConfig>>',
|
|
159
253
|
parameters: [
|
|
160
|
-
{ name: 'router', type: '
|
|
161
|
-
{ name: 'procedure', type: '
|
|
254
|
+
{ name: 'router', type: 'Router' },
|
|
255
|
+
{ name: 'procedure', type: 'Proc' },
|
|
162
256
|
],
|
|
163
257
|
isExported: true,
|
|
164
258
|
isDefaultExport: true,
|
|
165
|
-
})
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
259
|
+
});
|
|
260
|
+
let routerTypingStructure = undefined;
|
|
261
|
+
if (generateClientHelpers) {
|
|
262
|
+
// generate an interface for precise Prisma-like typing for the router procedures
|
|
263
|
+
// which will be used to correct tRPC's typing on the client side
|
|
264
|
+
routerTypingStructure = {
|
|
265
|
+
kind: ts_morph_1.StructureKind.Interface,
|
|
266
|
+
name: 'ClientType',
|
|
267
|
+
isExported: true,
|
|
268
|
+
typeParameters: ['AppRouter extends AnyRouter', `Context = AppRouter['_def']['_config']['$types']['ctx']`],
|
|
269
|
+
properties: [],
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
createRouterFunc.setBodyText((funcWriter) => {
|
|
273
|
+
funcWriter.write('return router(');
|
|
274
|
+
funcWriter.block(() => {
|
|
275
|
+
var _a;
|
|
169
276
|
for (const [opType, opNameWithModel] of Object.entries(operations)) {
|
|
170
277
|
const baseOpType = opType.replace('OrThrow', '');
|
|
171
|
-
const inputType = (0, helpers_1.
|
|
278
|
+
const inputType = (0, helpers_1.getInputSchemaByOpName)(baseOpType, model);
|
|
172
279
|
const generateOpName = opType.replace(/One$/, '');
|
|
173
280
|
if (opNameWithModel &&
|
|
174
281
|
inputType &&
|
|
175
282
|
(!generateModelActions || generateModelActions.includes(generateOpName))) {
|
|
176
|
-
(0, helpers_1.generateProcedure)(
|
|
283
|
+
(0, helpers_1.generateProcedure)(funcWriter, generateOpName, (0, upper_case_first_1.upperCaseFirst)(inputType), model, baseOpType);
|
|
284
|
+
if (routerTypingStructure) {
|
|
285
|
+
(_a = routerTypingStructure.properties) === null || _a === void 0 ? void 0 : _a.push({
|
|
286
|
+
kind: ts_morph_1.StructureKind.PropertySignature,
|
|
287
|
+
name: generateOpName,
|
|
288
|
+
type: (writer) => {
|
|
289
|
+
(0, helpers_1.generateRouterTyping)(writer, generateOpName, model, baseOpType);
|
|
290
|
+
},
|
|
291
|
+
});
|
|
292
|
+
}
|
|
177
293
|
}
|
|
178
294
|
}
|
|
179
295
|
});
|
|
180
|
-
|
|
296
|
+
funcWriter.write(');');
|
|
181
297
|
});
|
|
298
|
+
if (routerTypingStructure) {
|
|
299
|
+
modelRouter.addInterface(routerTypingStructure);
|
|
300
|
+
}
|
|
182
301
|
modelRouter.formatText();
|
|
183
302
|
}
|
|
184
303
|
function createHelper(outDir) {
|
|
@@ -276,4 +395,24 @@ function createHelper(outDir) {
|
|
|
276
395
|
`);
|
|
277
396
|
checkRead.formatText();
|
|
278
397
|
}
|
|
398
|
+
function parseOptionAsStrings(options, optionaName) {
|
|
399
|
+
const value = options[optionaName];
|
|
400
|
+
if (value === undefined) {
|
|
401
|
+
return undefined;
|
|
402
|
+
}
|
|
403
|
+
else if (typeof value === 'string') {
|
|
404
|
+
// comma separated string
|
|
405
|
+
return value
|
|
406
|
+
.split(',')
|
|
407
|
+
.filter((i) => !!i)
|
|
408
|
+
.map((i) => i.trim());
|
|
409
|
+
}
|
|
410
|
+
else if (Array.isArray(value) && value.every((i) => typeof i === 'string')) {
|
|
411
|
+
// string array
|
|
412
|
+
return value;
|
|
413
|
+
}
|
|
414
|
+
else {
|
|
415
|
+
throw new sdk_1.PluginError(_1.name, `Invalid "${optionaName}" option: must be a comma-separated string or an array of strings`);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
279
418
|
//# 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,yCASyB;AAEzB,4CAAoB;AACpB,uDAAkD;AAClD,gDAAwB;AACxB,uCAA6G;AAC7G,uDAAkD;AAClD,wBAAyB;AACzB,uCAQmB;AACnB,uCAAoC;AACpC,kEAA0C;AAE1C,SAAsB,QAAQ,CAAC,KAAY,EAAE,OAAsB,EAAE,IAAmB;;;QACpF,IAAI,MAAM,GAAG,IAAA,mBAAa,EAAS,OAAO,EAAE,QAAQ,CAAC,CAAC;QACtD,MAAM,GAAG,IAAA,iBAAW,EAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QAEtC,wCAAwC;QACxC,MAAM,oBAAoB,GAAG,oBAAoB,CAAC,OAAO,EAAE,sBAAsB,CAAC,CAAC;QAEnF,yCAAyC;QACzC,MAAM,qBAAqB,GAAG,oBAAoB,CAAC,OAAO,EAAE,uBAAuB,CAAC,CAAC;QACrF,IAAI,qBAAqB,IAAI,CAAC,qBAAqB,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE;YAC7F,MAAM,IAAI,iBAAW,CAAC,OAAI,EAAE,uEAAuE,CAAC,CAAC;SACxG;QAED,IAAI,OAAO,CAAC,gBAAgB,IAAI,OAAO,OAAO,CAAC,gBAAgB,KAAK,QAAQ,EAAE;YAC1E,MAAM,IAAI,iBAAW,CAAC,OAAI,EAAE,4CAA4C,CAAC,CAAC;SAC7E;QAED,MAAM,YAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,MAAM,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QACrD,MAAM,IAAA,mBAAS,EA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C;IACF,SAAS,CAAC,UAAU,EAAE,CAAC;AAC3B,CAAC;AAED,SAAS,oBAAoB,CAAC,OAAsB,EAAE,WAAmB;IACrE,MAAM,KAAK,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;IACnC,IAAI,KAAK,KAAK,SAAS,EAAE;QACrB,OAAO,SAAS,CAAC;KACpB;SAAM,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;QAClC,yBAAyB;QACzB,OAAO,KAAK;aACP,KAAK,CAAC,GAAG,CAAC;aACV,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;aAClB,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;KAC7B;SAAM,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,QAAQ,CAAC,EAAE;QAC1E,eAAe;QACf,OAAO,KAAiB,CAAC;KAC5B;SAAM;QACH,MAAM,IAAI,iBAAW,CACjB,OAAI,EACJ,YAAY,WAAW,mEAAmE,CAC7F,CAAC;KACL;AACL,CAAC"}
|
package/helpers.d.ts
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
|
-
import { DMMF } from '@prisma/generator-helper';
|
|
1
|
+
import type { DMMF } from '@prisma/generator-helper';
|
|
2
|
+
import { Model } from '@zenstackhq/sdk/ast';
|
|
2
3
|
import { CodeBlockWriter, SourceFile } from 'ts-morph';
|
|
3
4
|
export declare function generateProcedure(writer: CodeBlockWriter, opType: string, typeName: string, modelName: string, baseOpType: string): void;
|
|
4
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Generate precise Prisma-like typing for router procedures.
|
|
7
|
+
*/
|
|
8
|
+
export declare function generateRouterTyping(writer: CodeBlockWriter, opType: string, modelName: string, baseOpType: string): void;
|
|
9
|
+
export declare function generateRouterTypingImports(sourceFile: SourceFile, model: Model): void;
|
|
10
|
+
export declare function generateRouterSchemaImports(sourceFile: SourceFile, name: string, zodSchemasImport: string): void;
|
|
5
11
|
export declare function generateHelperImport(sourceFile: SourceFile): void;
|
|
6
|
-
export declare const
|
|
12
|
+
export declare const getInputSchemaByOpName: (opName: string, modelName: string) => string | undefined;
|
|
7
13
|
export declare const getProcedureTypeByOpName: (opName: string) => string | undefined;
|
|
8
14
|
export declare function resolveModelsComments(models: DMMF.Model[], hiddenModels: string[]): void;
|