mobiletto-orm-typedef-gen 2.3.41 → 2.8.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/README.md +16 -0
- package/lib/esm/generate.d.ts +12 -2
- package/lib/esm/generate.js +213 -30
- package/lib/esm/generate.js.map +1 -0
- package/lib/esm/generators/h3.d.ts +7 -0
- package/lib/esm/generators/h3.js +84 -0
- package/lib/esm/generators/h3.js.map +1 -0
- package/lib/esm/generators/jsonSchema.d.ts +3 -0
- package/lib/esm/generators/jsonSchema.js +48 -0
- package/lib/esm/generators/jsonSchema.js.map +1 -0
- package/lib/esm/generators/server.d.ts +14 -9
- package/lib/esm/generators/server.js +79 -53
- package/lib/esm/generators/server.js.map +1 -0
- package/lib/esm/generators/tsType.d.ts +2 -2
- package/lib/esm/generators/tsType.js +2 -1
- package/lib/esm/generators/tsType.js.map +1 -0
- package/lib/esm/generators/vue/admin.d.ts +5 -0
- package/lib/esm/generators/vue/admin.js +83 -0
- package/lib/esm/generators/vue/admin.js.map +1 -0
- package/lib/esm/generators/vue/service.d.ts +4 -0
- package/lib/esm/generators/vue/service.js +47 -0
- package/lib/esm/generators/vue/service.js.map +1 -0
- package/lib/esm/generators/vue/store.d.ts +5 -0
- package/lib/esm/generators/vue/store.js +43 -0
- package/lib/esm/generators/vue/store.js.map +1 -0
- package/lib/esm/helpers.d.ts +4 -0
- package/lib/esm/helpers.js +160 -0
- package/lib/esm/helpers.js.map +1 -0
- package/lib/esm/index.d.ts +6 -2
- package/lib/esm/index.js +7 -2
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/templates/admin/adminComponent.vue.hbs +662 -0
- package/lib/esm/templates/admin/adminHelper.ts.hbs +52 -0
- package/lib/esm/templates/admin/adminPage.vue.hbs +53 -0
- package/lib/esm/templates/api/h3.ts.hbs +13 -0
- package/lib/esm/templates/api/id.delete.ts.hbs +94 -32
- package/lib/esm/templates/api/id.get.ts.hbs +79 -18
- package/lib/esm/templates/api/id.patch.ts.hbs +76 -24
- package/lib/esm/templates/api/id.put.ts.hbs +65 -18
- package/lib/esm/templates/api/index.post.ts.hbs +111 -22
- package/lib/esm/templates/api/index.put.ts.hbs +75 -0
- package/lib/esm/templates/api/refs.ts.hbs +26 -0
- package/lib/esm/templates/filter/errorFilter.ts.hbs +10 -0
- package/lib/esm/templates/filter/sessionFilter.ts.hbs +125 -25
- package/lib/esm/templates/schema/jsonSchema.json.hbs +62 -0
- package/lib/esm/templates/service/service.ts.hbs +59 -29
- package/lib/esm/templates/service/serviceHelper.ts.hbs +38 -27
- package/lib/esm/templates/service/singletonService.ts.hbs +46 -24
- package/lib/esm/templates/store/singletonStore.ts.hbs +57 -28
- package/lib/esm/templates/store/store.ts.hbs +169 -39
- package/lib/esm/templates/store/storeHelper.ts.hbs +21 -9
- package/lib/esm/templates/type/tsType.ts.hbs +7 -3
- package/package.json +37 -22
- package/src/generate.ts +400 -0
- package/src/generators/h3.ts +125 -0
- package/src/generators/jsonSchema.ts +65 -0
- package/src/generators/server.ts +242 -0
- package/src/generators/tsType.ts +6 -0
- package/src/generators/vue/admin.ts +136 -0
- package/src/generators/vue/service.ts +76 -0
- package/src/generators/vue/store.ts +74 -0
- package/src/helpers.ts +195 -0
- package/src/index.ts +10 -0
- package/lib/cjs/generate.d.ts +0 -19
- package/lib/cjs/generate.js +0 -154
- package/lib/cjs/generators/client.d.ts +0 -8
- package/lib/cjs/generators/client.js +0 -75
- package/lib/cjs/generators/server.d.ts +0 -11
- package/lib/cjs/generators/server.js +0 -91
- package/lib/cjs/generators/tsType.d.ts +0 -3
- package/lib/cjs/generators/tsType.js +0 -4
- package/lib/cjs/generators/yup.d.ts +0 -3
- package/lib/cjs/generators/yup.js +0 -8
- package/lib/cjs/index.d.ts +0 -6
- package/lib/cjs/index.js +0 -6
- package/lib/cjs/package.json +0 -16
- package/lib/cjs/templates/api/id.delete.ts.hbs +0 -66
- package/lib/cjs/templates/api/id.get.ts.hbs +0 -38
- package/lib/cjs/templates/api/id.patch.ts.hbs +0 -51
- package/lib/cjs/templates/api/id.put.ts.hbs +0 -42
- package/lib/cjs/templates/api/index.post.ts.hbs +0 -39
- package/lib/cjs/templates/component/admin.vue.hbs +0 -493
- package/lib/cjs/templates/component/adminHelper.ts.hbs +0 -65
- package/lib/cjs/templates/component/componentHelper.d.ts +0 -3
- package/lib/cjs/templates/component/componentHelper.js +0 -53
- package/lib/cjs/templates/filter/errorFilter.ts.hbs +0 -51
- package/lib/cjs/templates/filter/sessionFilter.ts.hbs +0 -56
- package/lib/cjs/templates/service/service.ts.hbs +0 -58
- package/lib/cjs/templates/service/serviceHelper.ts.hbs +0 -97
- package/lib/cjs/templates/service/singletonService.ts.hbs +0 -49
- package/lib/cjs/templates/store/singletonStore.ts.hbs +0 -74
- package/lib/cjs/templates/store/store.ts.hbs +0 -94
- package/lib/cjs/templates/store/storeHelper.d.ts +0 -15
- package/lib/cjs/templates/store/storeHelper.js +0 -15
- package/lib/cjs/templates/store/storeHelper.ts.hbs +0 -34
- package/lib/cjs/templates/type/tsType.ts.hbs +0 -7
- package/lib/cjs/templates/validation/yup.ts.hbs +0 -39
- package/lib/esm/generators/client.d.ts +0 -8
- package/lib/esm/generators/client.js +0 -75
- package/lib/esm/generators/yup.d.ts +0 -3
- package/lib/esm/generators/yup.js +0 -8
- package/lib/esm/templates/component/admin.vue.hbs +0 -493
- package/lib/esm/templates/component/adminHelper.ts.hbs +0 -65
- package/lib/esm/templates/component/componentHelper.d.ts +0 -3
- package/lib/esm/templates/component/componentHelper.js +0 -53
- package/lib/esm/templates/store/storeHelper.d.ts +0 -15
- package/lib/esm/templates/store/storeHelper.js +0 -15
- package/lib/esm/templates/validation/yup.ts.hbs +0 -39
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MobilettoOrmTypeDef,
|
|
3
|
+
type MobilettoApiConfig,
|
|
4
|
+
type MobilettoApiEndpointConfig,
|
|
5
|
+
type MobilettoApiPath,
|
|
6
|
+
} from "mobiletto-orm-typedef";
|
|
7
|
+
import {
|
|
8
|
+
DUMMY_TYPEDEF,
|
|
9
|
+
GenerateOptions,
|
|
10
|
+
generate,
|
|
11
|
+
checkFileForDisclaimer,
|
|
12
|
+
STANDARD_AUTOGEN_FILE_DISCLAIMER,
|
|
13
|
+
} from "../generate.js";
|
|
14
|
+
import * as fs from "fs";
|
|
15
|
+
|
|
16
|
+
const apiOutfile = (opts: GenerateOptions, tsFile: string) =>
|
|
17
|
+
opts.outfile ? Object.assign({}, opts, { outfile: `${opts.outfile}/${tsFile}` }) : opts;
|
|
18
|
+
|
|
19
|
+
export type GeneratedApi = {
|
|
20
|
+
lookup?: string;
|
|
21
|
+
create?: string;
|
|
22
|
+
createWithoutId?: string;
|
|
23
|
+
update?: string;
|
|
24
|
+
delete?: string;
|
|
25
|
+
search?: string;
|
|
26
|
+
paths?: GeneratedApi[];
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export const _generateApiEndpoint = (
|
|
30
|
+
outfile: string,
|
|
31
|
+
templateName: string,
|
|
32
|
+
apiPath: MobilettoApiPath,
|
|
33
|
+
endpoint: MobilettoApiEndpointConfig | undefined,
|
|
34
|
+
parentPaths: MobilettoApiPath[],
|
|
35
|
+
opts: GenerateOptions,
|
|
36
|
+
ctx: Record<string, string>,
|
|
37
|
+
): string | undefined => {
|
|
38
|
+
const genOpts = apiOutfile(opts, outfile);
|
|
39
|
+
const epFile = genOpts.outfile;
|
|
40
|
+
if (endpoint && !endpoint.disabled) {
|
|
41
|
+
return generate(apiPath, parentPaths, `templates/api/${templateName}.ts.hbs`, genOpts, ctx);
|
|
42
|
+
} else {
|
|
43
|
+
if (epFile) {
|
|
44
|
+
if (checkFileForDisclaimer(epFile, STANDARD_AUTOGEN_FILE_DISCLAIMER)) {
|
|
45
|
+
// deleting a previously generated file is a side effect worth surfacing loudly
|
|
46
|
+
console.warn(`_generateApiEndpoint: removing disabled endpoint: ${epFile}`);
|
|
47
|
+
fs.rmSync(epFile);
|
|
48
|
+
} else if (fs.existsSync(epFile)) {
|
|
49
|
+
console.warn(`_generateApiEndpoint: WARNING: detected non-autogenerated endpoint: ${epFile}`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export const _generateApi = (
|
|
56
|
+
apiPath: MobilettoApiPath,
|
|
57
|
+
parentPaths: MobilettoApiPath[],
|
|
58
|
+
prefixPath: string,
|
|
59
|
+
opts: GenerateOptions,
|
|
60
|
+
ctx: Record<string, string>,
|
|
61
|
+
): GeneratedApi => {
|
|
62
|
+
const typeDef: MobilettoOrmTypeDef = apiPath.typeDef;
|
|
63
|
+
const apiConfig: MobilettoApiConfig = apiPath.apiConfig;
|
|
64
|
+
if (opts.outfile) {
|
|
65
|
+
const stat = fs.statSync(opts.outfile, { throwIfNoEntry: false });
|
|
66
|
+
if (stat && !stat.isDirectory()) {
|
|
67
|
+
throw new Error(
|
|
68
|
+
`_generateApi: opts.outfile (${opts.outfile}) must either not exist, or be an existing directory`,
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (!typeDef.apiConfig) {
|
|
73
|
+
throw new Error(`_generateApi: typeDef ${typeDef.typeName} has no apiConfig`);
|
|
74
|
+
}
|
|
75
|
+
if (!typeDef.shortId) {
|
|
76
|
+
throw new Error(`_generateApi: typeDef ${typeDef.typeName} must define a shortId to use for path IDs`);
|
|
77
|
+
}
|
|
78
|
+
const idDir = `[${typeDef.shortId}_id]`;
|
|
79
|
+
const baseApi: GeneratedApi = {};
|
|
80
|
+
|
|
81
|
+
baseApi.lookup = _generateApiEndpoint(
|
|
82
|
+
`${prefixPath}${idDir}/index.get.ts`,
|
|
83
|
+
"id.get",
|
|
84
|
+
apiPath,
|
|
85
|
+
apiConfig.lookup,
|
|
86
|
+
parentPaths,
|
|
87
|
+
opts,
|
|
88
|
+
ctx,
|
|
89
|
+
);
|
|
90
|
+
if (typeDef.autoId) {
|
|
91
|
+
baseApi.createWithoutId = _generateApiEndpoint(
|
|
92
|
+
`${prefixPath}index.put.ts`,
|
|
93
|
+
"index.put",
|
|
94
|
+
apiPath,
|
|
95
|
+
apiConfig.create,
|
|
96
|
+
parentPaths,
|
|
97
|
+
opts,
|
|
98
|
+
ctx,
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
baseApi.create = _generateApiEndpoint(
|
|
102
|
+
`${prefixPath}${idDir}/index.put.ts`,
|
|
103
|
+
"id.put",
|
|
104
|
+
apiPath,
|
|
105
|
+
apiConfig.create,
|
|
106
|
+
parentPaths,
|
|
107
|
+
opts,
|
|
108
|
+
ctx,
|
|
109
|
+
);
|
|
110
|
+
baseApi.update = _generateApiEndpoint(
|
|
111
|
+
`${prefixPath}${idDir}/index.patch.ts`,
|
|
112
|
+
"id.patch",
|
|
113
|
+
apiPath,
|
|
114
|
+
apiConfig.update,
|
|
115
|
+
parentPaths,
|
|
116
|
+
opts,
|
|
117
|
+
ctx,
|
|
118
|
+
);
|
|
119
|
+
baseApi.delete = _generateApiEndpoint(
|
|
120
|
+
`${prefixPath}${idDir}/index.delete.ts`,
|
|
121
|
+
"id.delete",
|
|
122
|
+
apiPath,
|
|
123
|
+
apiConfig.delete,
|
|
124
|
+
parentPaths,
|
|
125
|
+
opts,
|
|
126
|
+
ctx,
|
|
127
|
+
);
|
|
128
|
+
baseApi.search = _generateApiEndpoint(
|
|
129
|
+
`${prefixPath}index.post.ts`,
|
|
130
|
+
"index.post",
|
|
131
|
+
apiPath,
|
|
132
|
+
apiConfig.search,
|
|
133
|
+
parentPaths,
|
|
134
|
+
opts,
|
|
135
|
+
ctx,
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
if (apiConfig.paths && apiConfig.paths.length > 0) {
|
|
139
|
+
baseApi.paths = [];
|
|
140
|
+
const thisPath = { typeDef, apiConfig };
|
|
141
|
+
for (const apiPath of apiConfig.paths) {
|
|
142
|
+
baseApi.paths.push(
|
|
143
|
+
_generateApi(
|
|
144
|
+
apiPath,
|
|
145
|
+
[...parentPaths, thisPath],
|
|
146
|
+
(prefixPath.length > 0 ? prefixPath + "/" : "") +
|
|
147
|
+
`[${typeDef.shortId}_id]/${apiPath.typeDef.typeName}/`,
|
|
148
|
+
opts,
|
|
149
|
+
ctx,
|
|
150
|
+
),
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return baseApi;
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
export const generateApi = (
|
|
158
|
+
typeDef: MobilettoOrmTypeDef,
|
|
159
|
+
typeDefPackage: string,
|
|
160
|
+
callerType: string,
|
|
161
|
+
sessionType: string,
|
|
162
|
+
opts?: GenerateOptions,
|
|
163
|
+
singletonDefault?: string,
|
|
164
|
+
singletonDefaultImport?: string,
|
|
165
|
+
utilsImportPath?: string,
|
|
166
|
+
): GeneratedApi => {
|
|
167
|
+
if (!typeDef.apiConfig) {
|
|
168
|
+
throw new Error(`generateApi: typeDef ${typeDef.typeName} has no apiConfig`);
|
|
169
|
+
}
|
|
170
|
+
opts ||= {};
|
|
171
|
+
utilsImportPath ||= "~/utils/model";
|
|
172
|
+
|
|
173
|
+
const callerTypeName = callerType.replace(/Type$/, "");
|
|
174
|
+
const ctx: Record<string, string> = {
|
|
175
|
+
typeDefPackage,
|
|
176
|
+
findApiOptsPackage: opts.findApiOptsPackage || typeDefPackage,
|
|
177
|
+
errorFilterImport: opts.errorFilterImport || "~/filter/errorFilter.js",
|
|
178
|
+
utilsImportPath,
|
|
179
|
+
callerType,
|
|
180
|
+
callerTypeName,
|
|
181
|
+
sessionType,
|
|
182
|
+
};
|
|
183
|
+
if (singletonDefault) ctx.singletonDefault = singletonDefault;
|
|
184
|
+
if (singletonDefaultImport) ctx.singletonDefaultImport = singletonDefaultImport;
|
|
185
|
+
|
|
186
|
+
return _generateApi({ typeDef, apiConfig: typeDef.apiConfig }, [], "", opts, ctx);
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
export const generateErrorFilter = (typeDefPackage: string, opts?: GenerateOptions): string => {
|
|
190
|
+
return generate({ typeDef: DUMMY_TYPEDEF, apiConfig: {} }, [], `templates/filter/errorFilter.ts.hbs`, opts || {}, {
|
|
191
|
+
typeDefPackage,
|
|
192
|
+
});
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
export const generateSessionFilter = (
|
|
196
|
+
typeDefPackage: string,
|
|
197
|
+
opts?: GenerateOptions,
|
|
198
|
+
AccountType?: string,
|
|
199
|
+
SessionType?: string,
|
|
200
|
+
SESSION_HEADER?: string,
|
|
201
|
+
sessionHeaderImport?: string,
|
|
202
|
+
SESSION_COOKIE_NAME?: string,
|
|
203
|
+
sessionCookieNameImport?: string,
|
|
204
|
+
cookieOptions?: string,
|
|
205
|
+
cookieOptionsImport?: string,
|
|
206
|
+
errorFilterImport?: string,
|
|
207
|
+
sessionRepository?: string,
|
|
208
|
+
sessionRepositoryImport?: string,
|
|
209
|
+
accountRepository?: string,
|
|
210
|
+
accountRepositoryImport?: string,
|
|
211
|
+
): string => {
|
|
212
|
+
AccountType ||= "AccountType";
|
|
213
|
+
SessionType ||= "SessionType";
|
|
214
|
+
SESSION_HEADER ||= "SESSION_HEADER";
|
|
215
|
+
sessionHeaderImport ||= "~/utils/auth.js";
|
|
216
|
+
SESSION_COOKIE_NAME ||= "SESSION_COOKIE_NAME";
|
|
217
|
+
sessionCookieNameImport ||= sessionHeaderImport;
|
|
218
|
+
cookieOptions ||= "cookieOptions";
|
|
219
|
+
cookieOptionsImport ||= "~/utils/cookie.js";
|
|
220
|
+
errorFilterImport = opts?.errorFilterImport || "~/filter/errorFilter.js";
|
|
221
|
+
sessionRepository ||= "sessionRepository";
|
|
222
|
+
sessionRepositoryImport ||= "~/repo/sessionRepo.js";
|
|
223
|
+
accountRepository ||= "accountRepository";
|
|
224
|
+
accountRepositoryImport ||= "~/repo/accountRepo.js";
|
|
225
|
+
opts ||= {};
|
|
226
|
+
return generate({ typeDef: DUMMY_TYPEDEF, apiConfig: {} }, [], `templates/filter/sessionFilter.ts.hbs`, opts, {
|
|
227
|
+
typeDefPackage,
|
|
228
|
+
AccountType,
|
|
229
|
+
SessionType,
|
|
230
|
+
SESSION_HEADER,
|
|
231
|
+
sessionHeaderImport,
|
|
232
|
+
SESSION_COOKIE_NAME,
|
|
233
|
+
sessionCookieNameImport,
|
|
234
|
+
cookieOptions,
|
|
235
|
+
cookieOptionsImport,
|
|
236
|
+
errorFilterImport,
|
|
237
|
+
sessionRepository,
|
|
238
|
+
sessionRepositoryImport,
|
|
239
|
+
accountRepository,
|
|
240
|
+
accountRepositoryImport,
|
|
241
|
+
});
|
|
242
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { MobilettoOrmTypeDef } from "mobiletto-orm-typedef";
|
|
2
|
+
import { generate, GenerateOptions } from "../generate.js";
|
|
3
|
+
|
|
4
|
+
export const generateTypeScriptType = (typeDef: MobilettoOrmTypeDef, opts?: GenerateOptions): string => {
|
|
5
|
+
return generate({ typeDef, apiConfig: typeDef.apiConfig || {} }, [], "templates/type/tsType.ts.hbs", opts);
|
|
6
|
+
};
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { capitalize, uncapitalize } from "zilla-util";
|
|
2
|
+
import { MobilettoApiConfig, MobilettoApiPath, MobilettoOrmTypeDef } from "mobiletto-orm-typedef";
|
|
3
|
+
import { DUMMY_TYPEDEF, generate, GenerateOptions } from "../../generate.js";
|
|
4
|
+
|
|
5
|
+
const _generateAdminComponent = (
|
|
6
|
+
results: Record<string, string>,
|
|
7
|
+
apiPath: MobilettoApiPath,
|
|
8
|
+
parentPaths: MobilettoApiPath[],
|
|
9
|
+
prefixFilePath: string,
|
|
10
|
+
prefixFileName: string,
|
|
11
|
+
opts: GenerateOptions,
|
|
12
|
+
ctx: Record<string, object | string | number | null | undefined>,
|
|
13
|
+
) => {
|
|
14
|
+
const typeDef = apiPath.typeDef;
|
|
15
|
+
const apiConfig = apiPath.apiConfig;
|
|
16
|
+
const fullAdminName = prefixFileName + typeDef.TypeName;
|
|
17
|
+
const fullAdminPath = prefixFilePath + "Model" + fullAdminName + "Admin.vue";
|
|
18
|
+
const genOpts = Object.assign({}, opts, {
|
|
19
|
+
outfile: opts.outfile ? `${opts.outfile}/${fullAdminPath}` : undefined,
|
|
20
|
+
});
|
|
21
|
+
const firstParentTypeDef = parentPaths.length === 0 ? typeDef : parentPaths[0].typeDef;
|
|
22
|
+
results[typeDef.typeName] = generate(apiPath, parentPaths, "templates/admin/adminComponent.vue.hbs", genOpts, {
|
|
23
|
+
...ctx,
|
|
24
|
+
fullAdminName: uncapitalize(fullAdminName),
|
|
25
|
+
FullAdminName: capitalize(fullAdminName),
|
|
26
|
+
firstParentTypeDef,
|
|
27
|
+
});
|
|
28
|
+
if (apiConfig.paths?.length && apiConfig.paths?.length > 0) {
|
|
29
|
+
const thisPath = { typeDef, apiConfig };
|
|
30
|
+
for (const path of apiConfig.paths) {
|
|
31
|
+
_generateAdminComponent(
|
|
32
|
+
results,
|
|
33
|
+
path,
|
|
34
|
+
[...parentPaths, thisPath],
|
|
35
|
+
`${prefixFilePath}/${path.typeDef.typeName}/`,
|
|
36
|
+
fullAdminName,
|
|
37
|
+
opts,
|
|
38
|
+
ctx,
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export const generateAdminComponent = (
|
|
45
|
+
typeDef: MobilettoOrmTypeDef,
|
|
46
|
+
typeDefPackage: string,
|
|
47
|
+
opts?: GenerateOptions,
|
|
48
|
+
utilsImportPath?: string,
|
|
49
|
+
adminHelperPath?: string,
|
|
50
|
+
sessionStoreImport?: string,
|
|
51
|
+
localeMessages?: string,
|
|
52
|
+
): Record<string, string> => {
|
|
53
|
+
utilsImportPath ||= `~/utils/model`;
|
|
54
|
+
adminHelperPath ||= `~/utils/model/adminHelper`;
|
|
55
|
+
sessionStoreImport ||= `~/stores/sessionStore`;
|
|
56
|
+
localeMessages ||= "localeMessages";
|
|
57
|
+
// work on a copy: the caller's opts must not be mutated
|
|
58
|
+
const genOpts: GenerateOptions = { ...opts, rootOnly: true };
|
|
59
|
+
const results: Record<string, string> = {};
|
|
60
|
+
const ctx = {
|
|
61
|
+
typeDefPackage,
|
|
62
|
+
adminHelperPath,
|
|
63
|
+
sessionStoreImport,
|
|
64
|
+
localeMessages,
|
|
65
|
+
utilsImportPath,
|
|
66
|
+
};
|
|
67
|
+
_generateAdminComponent(results, { typeDef, apiConfig: typeDef.apiConfig || {} }, [], "", "", genOpts, ctx);
|
|
68
|
+
return results;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const _generateAdminPage = (
|
|
72
|
+
results: Record<string, string>,
|
|
73
|
+
apiPath: MobilettoApiPath,
|
|
74
|
+
parentPaths: MobilettoApiPath[],
|
|
75
|
+
prefixFilePath: string,
|
|
76
|
+
prefixFileName: string,
|
|
77
|
+
opts: GenerateOptions,
|
|
78
|
+
ctx: Record<string, object | string | number | null | undefined>,
|
|
79
|
+
) => {
|
|
80
|
+
const typeDef: MobilettoOrmTypeDef = apiPath.typeDef;
|
|
81
|
+
const apiConfig: MobilettoApiConfig = apiPath.apiConfig;
|
|
82
|
+
const fullAdminName = prefixFileName + typeDef.TypeName;
|
|
83
|
+
const genOpts = Object.assign({}, opts, {
|
|
84
|
+
outfile: opts.outfile ? `${opts.outfile}/${prefixFilePath}/admin.vue` : undefined,
|
|
85
|
+
});
|
|
86
|
+
results[typeDef.typeName] = generate(apiPath, parentPaths, "templates/admin/adminPage.vue.hbs", genOpts, {
|
|
87
|
+
...ctx,
|
|
88
|
+
fullAdminName: uncapitalize(fullAdminName),
|
|
89
|
+
FullAdminName: capitalize(fullAdminName),
|
|
90
|
+
});
|
|
91
|
+
if (apiConfig.paths?.length && apiConfig.paths?.length > 0) {
|
|
92
|
+
const thisPath = { typeDef, apiConfig };
|
|
93
|
+
for (const path of apiConfig.paths) {
|
|
94
|
+
_generateAdminPage(
|
|
95
|
+
results,
|
|
96
|
+
path,
|
|
97
|
+
[...parentPaths, thisPath],
|
|
98
|
+
`${prefixFilePath}[${typeDef.shortId}_id]/${path.typeDef.typeName}/`,
|
|
99
|
+
fullAdminName,
|
|
100
|
+
opts,
|
|
101
|
+
ctx,
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
export const generateAdminPage = (
|
|
108
|
+
typeDef: MobilettoOrmTypeDef,
|
|
109
|
+
typeDefPackage: string,
|
|
110
|
+
opts?: GenerateOptions,
|
|
111
|
+
utilsImportPath?: string,
|
|
112
|
+
adminHelperPath?: string,
|
|
113
|
+
sessionStoreImport?: string,
|
|
114
|
+
localeMessages?: string,
|
|
115
|
+
): Record<string, string> => {
|
|
116
|
+
utilsImportPath ||= `~/utils/model`;
|
|
117
|
+
adminHelperPath ||= `~/utils/model/adminHelper`;
|
|
118
|
+
sessionStoreImport ||= `~/stores/sessionStore`;
|
|
119
|
+
localeMessages ||= "localeMessages";
|
|
120
|
+
// work on a copy: the caller's opts must not be mutated
|
|
121
|
+
const genOpts: GenerateOptions = { ...opts, rootOnly: true };
|
|
122
|
+
const results: Record<string, string> = {};
|
|
123
|
+
const ctx = {
|
|
124
|
+
typeDefPackage,
|
|
125
|
+
adminHelperPath,
|
|
126
|
+
sessionStoreImport,
|
|
127
|
+
localeMessages,
|
|
128
|
+
utilsImportPath,
|
|
129
|
+
};
|
|
130
|
+
_generateAdminPage(results, { typeDef, apiConfig: typeDef.apiConfig || {} }, [], "", "", genOpts, ctx);
|
|
131
|
+
return results;
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
export const generateAdminHelper = (opts?: GenerateOptions): string => {
|
|
135
|
+
return generate({ typeDef: DUMMY_TYPEDEF, apiConfig: {} }, [], "templates/admin/adminHelper.ts.hbs", opts || {});
|
|
136
|
+
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { basename } from "pathe";
|
|
2
|
+
import { capitalize } from "zilla-util";
|
|
3
|
+
import { type MobilettoApiConfig, type MobilettoApiPath, MobilettoOrmTypeDef } from "mobiletto-orm-typedef";
|
|
4
|
+
import { DUMMY_TYPEDEF, generate, GenerateOptions } from "../../generate.js";
|
|
5
|
+
|
|
6
|
+
const _generateService = (
|
|
7
|
+
results: Record<string, string>,
|
|
8
|
+
apiPath: MobilettoApiPath,
|
|
9
|
+
parentPaths: MobilettoApiPath[],
|
|
10
|
+
prefixFilePath: string,
|
|
11
|
+
opts: GenerateOptions,
|
|
12
|
+
ctx: Record<string, object | string | number | null | undefined>,
|
|
13
|
+
) => {
|
|
14
|
+
const typeDef: MobilettoOrmTypeDef = apiPath.typeDef;
|
|
15
|
+
const apiConfig: MobilettoApiConfig = apiPath.apiConfig;
|
|
16
|
+
const serviceFilename = `${prefixFilePath}${
|
|
17
|
+
prefixFilePath.length > 0 ? capitalize(typeDef.typeName) : typeDef.typeName
|
|
18
|
+
}Service.ts`;
|
|
19
|
+
const fullServiceName = basename(serviceFilename).replace(/Service\.ts$/, "");
|
|
20
|
+
const genOpts = Object.assign({}, opts, {
|
|
21
|
+
outfile: opts.outfile ? `${opts.outfile}/${serviceFilename}` : undefined,
|
|
22
|
+
});
|
|
23
|
+
results[typeDef.typeName] = generate(
|
|
24
|
+
apiPath,
|
|
25
|
+
parentPaths,
|
|
26
|
+
`templates/service/${typeDef.singleton ? "singletonS" : "s"}ervice.ts.hbs`,
|
|
27
|
+
genOpts,
|
|
28
|
+
{ ...ctx, fullServiceName },
|
|
29
|
+
);
|
|
30
|
+
if (apiConfig.paths?.length && apiConfig.paths?.length > 0) {
|
|
31
|
+
const thisPath = { typeDef, apiConfig };
|
|
32
|
+
for (const path of apiConfig.paths) {
|
|
33
|
+
_generateService(results, path, [...parentPaths, thisPath], fullServiceName, opts, ctx);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return results;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export const generateService = (
|
|
40
|
+
typeDef: MobilettoOrmTypeDef,
|
|
41
|
+
typeDefPackage: string,
|
|
42
|
+
opts?: GenerateOptions,
|
|
43
|
+
utilsPath?: string,
|
|
44
|
+
): Record<string, string> => {
|
|
45
|
+
utilsPath ||= `~/utils/model`;
|
|
46
|
+
// work on a copy: the caller's opts must not be mutated
|
|
47
|
+
const genOpts: GenerateOptions = {
|
|
48
|
+
...opts,
|
|
49
|
+
rootOnly: true,
|
|
50
|
+
...(opts?.outfile ? { outfile: `${opts.outfile}/${typeDef.typeName}` } : {}),
|
|
51
|
+
};
|
|
52
|
+
const ctx: Record<string, object | string | number | null | undefined> = { utilsPath, typeDefPackage };
|
|
53
|
+
const results: Record<string, string> = {};
|
|
54
|
+
_generateService(results, { typeDef, apiConfig: typeDef.apiConfig }, [], "", genOpts, ctx);
|
|
55
|
+
return results;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export const generateServiceHelper = (
|
|
59
|
+
opts?: GenerateOptions,
|
|
60
|
+
sessionHeader?: string,
|
|
61
|
+
sessionHeaderImport?: string,
|
|
62
|
+
sessionCookie?: string,
|
|
63
|
+
sessionCookieImport?: string,
|
|
64
|
+
): string => {
|
|
65
|
+
sessionHeader ||= "SESSION_HEADER";
|
|
66
|
+
sessionHeaderImport ||= "~/utils/auth";
|
|
67
|
+
sessionCookie ||= "sessionCookie";
|
|
68
|
+
sessionCookieImport ||= "~/utils/auth";
|
|
69
|
+
const genOpts: GenerateOptions = { ...opts, rootOnly: true };
|
|
70
|
+
return generate({ typeDef: DUMMY_TYPEDEF, apiConfig: {} }, [], "templates/service/serviceHelper.ts.hbs", genOpts, {
|
|
71
|
+
sessionHeader,
|
|
72
|
+
sessionHeaderImport,
|
|
73
|
+
sessionCookie,
|
|
74
|
+
sessionCookieImport,
|
|
75
|
+
});
|
|
76
|
+
};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { basename } from "pathe";
|
|
2
|
+
import { capitalize } from "zilla-util";
|
|
3
|
+
import { type MobilettoApiConfig, type MobilettoApiPath, MobilettoOrmTypeDef } from "mobiletto-orm-typedef";
|
|
4
|
+
import { DUMMY_TYPEDEF, generate, GenerateOptions } from "../../generate.js";
|
|
5
|
+
|
|
6
|
+
export const _generateStore = (
|
|
7
|
+
results: Record<string, string>,
|
|
8
|
+
apiPath: MobilettoApiPath,
|
|
9
|
+
parentPaths: MobilettoApiPath[],
|
|
10
|
+
prefixFilePath: string,
|
|
11
|
+
opts: GenerateOptions,
|
|
12
|
+
ctx?: Record<string, object | string | number | null | undefined>,
|
|
13
|
+
) => {
|
|
14
|
+
const typeDef: MobilettoOrmTypeDef = apiPath.typeDef;
|
|
15
|
+
const apiConfig: MobilettoApiConfig = apiPath.apiConfig;
|
|
16
|
+
const storeFilename = `${prefixFilePath}${
|
|
17
|
+
prefixFilePath.length > 0 ? capitalize(typeDef.typeName) : typeDef.typeName
|
|
18
|
+
}Store.ts`;
|
|
19
|
+
const fullStoreName = basename(storeFilename).replace(/Store\.ts$/, "");
|
|
20
|
+
const genOpts = Object.assign({}, opts, {
|
|
21
|
+
outfile: opts.outfile ? `${opts.outfile}/${storeFilename}` : undefined,
|
|
22
|
+
});
|
|
23
|
+
const firstParentTypeDef = parentPaths.length === 0 ? typeDef : parentPaths[0].typeDef;
|
|
24
|
+
results[typeDef.typeName] = generate(
|
|
25
|
+
apiPath,
|
|
26
|
+
parentPaths,
|
|
27
|
+
`templates/store/${typeDef.singleton ? "singletonS" : "s"}tore.ts.hbs`,
|
|
28
|
+
genOpts,
|
|
29
|
+
{ ...ctx, fullStoreName, FullStoreName: capitalize(fullStoreName), firstParentTypeDef },
|
|
30
|
+
);
|
|
31
|
+
if (apiConfig.paths?.length && apiConfig.paths?.length > 0) {
|
|
32
|
+
const thisPath = { typeDef, apiConfig };
|
|
33
|
+
for (const path of apiConfig.paths) {
|
|
34
|
+
_generateStore(
|
|
35
|
+
results,
|
|
36
|
+
path,
|
|
37
|
+
[...parentPaths, thisPath],
|
|
38
|
+
basename(storeFilename).replace(/Store\.ts$/, ""),
|
|
39
|
+
opts,
|
|
40
|
+
ctx,
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export const generateStore = (
|
|
47
|
+
typeDef: MobilettoOrmTypeDef,
|
|
48
|
+
typeDefPackage: string,
|
|
49
|
+
opts?: GenerateOptions,
|
|
50
|
+
servicesImportPath?: string,
|
|
51
|
+
utilsImportPath?: string,
|
|
52
|
+
): Record<string, string> => {
|
|
53
|
+
servicesImportPath ||= "~/utils/services/model";
|
|
54
|
+
utilsImportPath ||= "~/utils/model";
|
|
55
|
+
// work on a copy: the caller's opts must not be mutated
|
|
56
|
+
const genOpts: GenerateOptions = {
|
|
57
|
+
...opts,
|
|
58
|
+
rootOnly: true,
|
|
59
|
+
...(opts?.outfile ? { outfile: `${opts.outfile}/${typeDef.typeName}` } : {}),
|
|
60
|
+
};
|
|
61
|
+
const ctx = {
|
|
62
|
+
typeDefPackage,
|
|
63
|
+
servicesImportPath,
|
|
64
|
+
utilsImportPath,
|
|
65
|
+
};
|
|
66
|
+
const results: Record<string, string> = {};
|
|
67
|
+
_generateStore(results, { typeDef, apiConfig: typeDef.apiConfig || {} }, [], "", genOpts, ctx);
|
|
68
|
+
return results;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export const generateStoreHelper = (opts?: GenerateOptions): string => {
|
|
72
|
+
const genOpts: GenerateOptions = { ...opts, rootOnly: true };
|
|
73
|
+
return generate({ typeDef: DUMMY_TYPEDEF, apiConfig: {} }, [], "templates/store/storeHelper.ts.hbs", genOpts);
|
|
74
|
+
};
|