mobiletto-orm-typedef-gen 2.3.42 → 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,52 @@
|
|
|
1
|
+
{{#if disclaimer}}// {{{ disclaimer }}}{{#if header}}
|
|
2
|
+
{{{header}}}{{/if}}{{/if}}
|
|
3
|
+
|
|
4
|
+
import { type Ref } from "vue";
|
|
5
|
+
import type { MobilettoOrmObject, MobilettoOrmValidationErrors } from "mobiletto-orm-typedef";
|
|
6
|
+
import { errMatch, normalizeMsg, fieldErrorMessage } from "hokey-runtime";
|
|
7
|
+
|
|
8
|
+
export type ActionConfig = {
|
|
9
|
+
path?: string;
|
|
10
|
+
func?: (obj: MobilettoOrmObject, successSnackbar: Ref<string>, errorSnackbar: Ref<string>) => unknown;
|
|
11
|
+
message: string;
|
|
12
|
+
icon?: string;
|
|
13
|
+
when?: (obj: MobilettoOrmObject) => boolean;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export const typeDefFieldErrorMessage = (
|
|
17
|
+
field: string | string[],
|
|
18
|
+
messages: Record<string, string>,
|
|
19
|
+
labelPrefixes: string[],
|
|
20
|
+
clientErrors: MobilettoOrmValidationErrors,
|
|
21
|
+
serverErrors: MobilettoOrmValidationErrors,
|
|
22
|
+
submitted?: boolean,
|
|
23
|
+
): string => {
|
|
24
|
+
if (submitted === false) {
|
|
25
|
+
return "";
|
|
26
|
+
}
|
|
27
|
+
const fields = Array.isArray(field) ? field : [field];
|
|
28
|
+
const fieldName = fields[0];
|
|
29
|
+
if (!fieldName) return "";
|
|
30
|
+
for (const f of fields) {
|
|
31
|
+
const clientErrFields = Object.keys(clientErrors);
|
|
32
|
+
const serverErrFields = Object.keys(serverErrors);
|
|
33
|
+
let errMsg = clientErrFields.find(errMatch(f))
|
|
34
|
+
if (errMsg) {
|
|
35
|
+
const errs = clientErrors[errMsg]
|
|
36
|
+
errMsg = errMsg + '_' + (errs && errs.length > 0 ? errs[0] : 'unknown')
|
|
37
|
+
} else {
|
|
38
|
+
errMsg = serverErrFields.find(errMatch(f)) || undefined
|
|
39
|
+
if (errMsg) {
|
|
40
|
+
const errs = serverErrors[errMsg]
|
|
41
|
+
errMsg = errMsg + '_' + (errs && errs.length > 0 ? errs[0] : 'unknown')
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (errMsg) {
|
|
45
|
+
errMsg = normalizeMsg(errMsg);
|
|
46
|
+
const labelPfx = labelPrefixes.includes("") ? labelPrefixes : [...labelPrefixes, ""];
|
|
47
|
+
const fld = normalizeMsg(fieldName);
|
|
48
|
+
return fieldErrorMessage(fld, errMsg, messages, labelPfx);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return "";
|
|
52
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{{#if disclaimer}}<!-- {{{ disclaimer }}}{{#if header}}
|
|
2
|
+
{{{header}}}{{/if}} -->{{/if}}
|
|
3
|
+
|
|
4
|
+
<template>
|
|
5
|
+
<v-container>
|
|
6
|
+
<v-row>
|
|
7
|
+
<v-col v-if="account && account.admin === true">
|
|
8
|
+
<Model{{FullAdminName}}Admin
|
|
9
|
+
:label-prefixes="labelPrefixes"{{#if apiConfig.create.disabled}}{{#ifPropAbsent apiConfig.admin.props 'canAdd'}}
|
|
10
|
+
:can-add="() => false"{{/ifPropAbsent}}{{/if}}{{#if apiConfig.update.disabled}}{{#ifPropAbsent apiConfig.admin.props 'canEdit'}}
|
|
11
|
+
:can-edit="() => false"{{/ifPropAbsent}}{{/if}}{{#if apiConfig.delete.disabled}}{{#ifPropAbsent apiConfig.admin.props 'canDelete'}}
|
|
12
|
+
:can-delete="() => false"{{/ifPropAbsent}}{{/if}}{{#if typeDef.singleton}}{{#ifPropAbsent apiConfig.admin.props '@add-canceled'}}
|
|
13
|
+
@add-canceled="closeSingleton"{{/ifPropAbsent}}{{#ifPropAbsent apiConfig.admin.props '@added'}}
|
|
14
|
+
@added="closeSingleton"{{/ifPropAbsent}}{{#ifPropAbsent apiConfig.admin.props '@edit-canceled'}}
|
|
15
|
+
@edit-canceled="closeSingleton"{{/ifPropAbsent}}{{#ifPropAbsent apiConfig.admin.props '@edited'}}
|
|
16
|
+
@edited="closeSingleton"{{/ifPropAbsent}}{{/if}}{{#if apiConfig.admin.actionConfigs}}
|
|
17
|
+
:action-configs="actionConfigs"{{/if}}{{#if apiConfig.admin.props}}{{#each apiConfig.admin.props}}
|
|
18
|
+
{{@key}}="{{this}}"{{/each}}{{/if}}
|
|
19
|
+
/>
|
|
20
|
+
</v-col>
|
|
21
|
+
</v-row>
|
|
22
|
+
</v-container>
|
|
23
|
+
</template>
|
|
24
|
+
|
|
25
|
+
<script setup lang="ts">
|
|
26
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
27
|
+
import { storeToRefs } from "pinia";
|
|
28
|
+
{{#if apiConfig.admin.imports}}{{#each apiConfig.admin.imports}}
|
|
29
|
+
{{{this}}};{{/each}}{{/if}}
|
|
30
|
+
import { useSessionStore } from "{{sessionStoreImport}}";{{#if apiConfig.admin.actionConfigs}}
|
|
31
|
+
import type { ActionConfig } from "{{adminHelperPath}}";{{/if}}
|
|
32
|
+
import Model{{FullAdminName}}Admin from "~/components/model/{{#each parentPaths}}{{typeName}}/{{/each}}{{typeName}}/Model{{FullAdminName}}Admin.vue";
|
|
33
|
+
|
|
34
|
+
const sessionStore = useSessionStore();
|
|
35
|
+
const { account, localeMessages } = storeToRefs(sessionStore);
|
|
36
|
+
const messages = localeMessages;
|
|
37
|
+
{{#if typeDef.singleton}}
|
|
38
|
+
const closeSingleton = () => navigateTo({{#if apiConfig.admin.closeSingletonPath}}{{apiConfig.admin.closeSingletonPath}}{{else}}"/admin"{{/if}});{{/if}}
|
|
39
|
+
|
|
40
|
+
const labelPrefixes = [
|
|
41
|
+
'admin_label_{{typeDef.typeName}}_',
|
|
42
|
+
'label_{{typeDef.typeName}}_',
|
|
43
|
+
'label_',
|
|
44
|
+
''
|
|
45
|
+
];
|
|
46
|
+
{{#if apiConfig.admin.actionConfigs}}
|
|
47
|
+
const actionConfigs: Record<string, ActionConfig> = {{{curlyOpen}}}{{#each apiConfig.admin.actionConfigs}}
|
|
48
|
+
{{@key}}{{#ne @key this}}: {{this}}{{/ne}},{{/each}}
|
|
49
|
+
};
|
|
50
|
+
{{/if}}
|
|
51
|
+
{{#if apiConfig.admin.script}}
|
|
52
|
+
{{{apiConfig.admin.script}}}{{/if}}
|
|
53
|
+
</script>
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{{#if disclaimer}}// {{{disclaimer}}}{{/if}}
|
|
2
|
+
import { Router } from "h3";
|
|
3
|
+
|
|
4
|
+
{{#each imports}}import {{importName}} from '{{importPath}}';
|
|
5
|
+
{{/each}}
|
|
6
|
+
|
|
7
|
+
export const loadRoutes = (router: Router): void => {
|
|
8
|
+
{{#each routes}}
|
|
9
|
+
|
|
10
|
+
// {{ALL_CAPS method}} {{route}}
|
|
11
|
+
router.{{method}}('{{route}}', {{importName}});
|
|
12
|
+
{{/each}}
|
|
13
|
+
};
|
|
@@ -2,25 +2,75 @@
|
|
|
2
2
|
{{#if disclaimer}}// {{{disclaimer}}}{{#if header}}
|
|
3
3
|
{{{header}}}{{/if}}{{/if}}
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
5
|
+
import { defineEventHandler, H3Error, H3Event } from "h3";
|
|
6
|
+
import {
|
|
7
|
+
badRequest,
|
|
8
|
+
conflict,
|
|
9
|
+
filterErrors,
|
|
10
|
+
forbidden,
|
|
11
|
+
notFound,
|
|
12
|
+
notImplemented,
|
|
13
|
+
serverError,
|
|
14
|
+
validationError,
|
|
15
|
+
} from "{{errorFilterImport}}";
|
|
16
|
+
import {
|
|
17
|
+
requireDefaultAccess,
|
|
18
|
+
requireAccount,
|
|
19
|
+
requireAccountObject,
|
|
20
|
+
requireAdminAccountObject
|
|
21
|
+
} from "~/filter/sessionFilter.js";
|
|
22
|
+
import { {{name}}Repository } from "~/repo/{{name}}Repo.js";
|
|
23
|
+
import { type {{callerType}} } from "{{typeDefPackage}}";
|
|
24
|
+
import { {{logger}} } from "{{loggerImport}}";
|
|
25
|
+
import { checkPermission } from "~/utils/auth.js";{{#if apiConfig.delete.permission.filter}}
|
|
26
|
+
import { {{apiConfig.delete.permission.filter.symbol}} } from "{{apiConfig.delete.permission.filter.package}}";{{/if}}
|
|
27
|
+
{{#if parentPaths}}{{#each parentPaths}}import { {{typeName}}Repository } from "~/repo/{{typeName}}Repo.js";
|
|
28
|
+
{{/each}}
|
|
29
|
+
import { type MobilettoOrmObject, FIND_NOREDACT } from "mobiletto-orm-typedef";{{/if}}{{#if apiConfig.delete.disabled}}
|
|
30
|
+
|
|
31
|
+
export default defineEventHandler((_event: H3Event) => {
|
|
32
|
+
// apiConfig.delete.disabled was set; endpoint is disabled
|
|
33
|
+
throw notImplemented(`disabled: {{name}}::delete`);
|
|
34
|
+
});
|
|
35
|
+
{{else}}
|
|
36
|
+
{{#nor apiConfig.delete.permission.admin apiConfig.delete.permission.owner apiConfig.delete.permission.named apiConfig.delete.permission.filter}}
|
|
37
|
+
import { anonymousCaller } from "mobiletto-orm";{{/nor}}
|
|
38
|
+
|
|
39
|
+
import { MobilettoOrmSyncError, MobilettoOrmNotFoundError, MobilettoOrmValidationError } from "mobiletto-orm-typedef";
|
|
40
|
+
import { {{Name}}TypeDef, type {{sessionType}} } from "{{typeDefPackage}}";
|
|
41
|
+
[[[include 'refs.ts']]]
|
|
42
|
+
const apiAction = "{{name}}.delete";{{#nor apiConfig.delete.permission.admin apiConfig.delete.permission.owner apiConfig.delete.permission.named apiConfig.delete.permission.filter}}
|
|
43
|
+
const caller: {{callerType}} = anonymousCaller();{{/nor}}
|
|
8
44
|
|
|
9
45
|
export default defineEventHandler(async (event: H3Event) => {
|
|
10
|
-
return await filterErrors(event,
|
|
11
|
-
|
|
12
|
-
return await
|
|
13
|
-
return await
|
|
14
|
-
|
|
15
|
-
|
|
46
|
+
return await filterErrors(event, apiAction, async (event: H3Event) => {{{curlyOpen}}}
|
|
47
|
+
return await requireDefaultAccess(event, apiAction, async (event: H3Event, apiAction: string) => {{{curlyOpen}}}{{#if apiConfig.delete.permission.admin}}
|
|
48
|
+
return await requireAdminAccountObject(event, apiAction, async (event: H3Event, apiAction: string, session: {{sessionType}}, caller: {{callerType}}) => {{{curlyOpen}}}{{else}}{{#if apiConfig.delete.permission.owner}}
|
|
49
|
+
return await requireAccountObject(event, apiAction, async (event: H3Event, apiAction: string, session: {{sessionType}}, caller: {{callerType}}) => {{{curlyOpen}}}{{else}}{{#if apiConfig.delete.permission.named}}
|
|
50
|
+
return await requireAccountObject(event, apiAction, async (event: H3Event, apiAction: string, session: {{sessionType}}, caller: {{callerType}}) => {{{curlyOpen}}}{{else}}{{#if apiConfig.delete.permission.filter}}
|
|
51
|
+
return await requireAccountObject(event, apiAction, async (event: H3Event, apiAction: string, session: {{sessionType}}, caller: {{callerType}}) => {{{curlyOpen}}}{{else}}{{#if apiConfig.delete.permission.session}}
|
|
52
|
+
return await requireAccount(event, apiAction, async (event: H3Event, apiAction: string, session: {{sessionType}}) => {{{curlyOpen}}}
|
|
53
|
+
{{/if}}{{/if}}{{/if}}{{/if}}{{/if}}
|
|
54
|
+
{{#if parentPaths}}const parentRefs = await resolveRefs(caller, event);
|
|
55
|
+
{{/if}}const {{name}}Repo = {{name}}Repository({{eventContextVar}});
|
|
56
|
+
if ({{name}}Repo.initialize) await {{name}}Repo.initialize();
|
|
57
|
+
const id = event?.context?.params?.{{typeDef.idParam}};
|
|
16
58
|
if (!id) throw notFound({{name}}Repo.typeDef.idFieldName());
|
|
17
59
|
try {
|
|
18
|
-
const {{name}}_by_id = await {{name}}Repo.safeFindById(id);
|
|
19
|
-
if ({{name}}_by_id) {
|
|
20
|
-
{{
|
|
21
|
-
|
|
22
|
-
{{
|
|
23
|
-
|
|
60
|
+
const {{name}}_by_id = await {{name}}Repo.safeFindById(caller, id);
|
|
61
|
+
if ({{name}}_by_id) {{{curlyOpen}}}{{#if parentPaths}}
|
|
62
|
+
checkRefs({{name}}_by_id, parentRefs, event);
|
|
63
|
+
{{/if}}{{#if apiConfig.delete.permission.owner}}
|
|
64
|
+
if (!caller.admin && {{name}}_by_id.{{apiConfig.delete.permission.owner}} !== session.account) {
|
|
65
|
+
throw forbidden(`apiAction=${apiAction} caller not owner/admin`);
|
|
66
|
+
}
|
|
67
|
+
{{else}}{{#if apiConfig.delete.permission.named}}
|
|
68
|
+
await checkPermission(caller, apiAction, {{#if parentPaths}}parentRefs{{else}}{{{emptyObject}}}{{/if}}, {{name}}_by_id, {{Name}}TypeDef);{{else}}{{#if apiConfig.delete.permission.filter}}
|
|
69
|
+
if (!(await {{apiConfig.delete.permission.filter.symbol}}.allowAction({{eventContextVar}}, caller, apiAction, {{name}}_by_id, {{#if parentPaths}}parentRefs{{else}}{{{emptyObject}}}{{/if}}, {{Name}}TypeDef))) {
|
|
70
|
+
throw validationError({global: ["validation"]});
|
|
71
|
+
}{{/if}}{{/if}}{{/if}}{{#if apiConfig.delete.hasValidate}}
|
|
72
|
+
if (typeof {{Name}}TypeDef.apiConfig.delete?.validate === "function") {
|
|
73
|
+
const validated = await {{Name}}TypeDef.apiConfig.delete.validate(caller, {{name}}_by_id, {{eventContextVar}});
|
|
24
74
|
if (validated === true || (typeof validated === "object" && Object.keys(validated).length === 0)) {
|
|
25
75
|
// successfully validated
|
|
26
76
|
} else if (typeof validated === "object" && Object.keys(validated).length > 0) {
|
|
@@ -29,15 +79,19 @@ export default defineEventHandler(async (event: H3Event) => {
|
|
|
29
79
|
throw validationError({global: ["validation"]});
|
|
30
80
|
}
|
|
31
81
|
}{{/if}}
|
|
32
|
-
return await {{name}}Repo.remove({{name}}_by_id);
|
|
82
|
+
return await {{name}}Repo.remove(caller, {{name}}_by_id);
|
|
33
83
|
}
|
|
34
84
|
{{#each alternateLookupFields}}
|
|
35
|
-
const {{name}}_by_{{this}} = await {{name}}Repo.safeFindBy("{{this}}", id);
|
|
36
|
-
if ({{name}}_by_{{this}}) {
|
|
37
|
-
{{#if
|
|
38
|
-
if (!
|
|
39
|
-
|
|
40
|
-
|
|
85
|
+
const {{name}}_by_{{this}} = await {{name}}Repo.safeFindBy(caller, "{{this}}", id);
|
|
86
|
+
if ({{name}}_by_{{this}}) {{{curlyOpen}}}{{#if parentPaths}}
|
|
87
|
+
checkRefs({{name}}_by_{{this}}, parentRefs, event);{{/if}}{{#if apiConfig.delete.permission.owner}}
|
|
88
|
+
if (!caller.admin && {{name}}_by_{{this}}.account !== session.account) {
|
|
89
|
+
throw forbidden(`apiAction=${apiAction} caller not owner/admin`);
|
|
90
|
+
}
|
|
91
|
+
{{else}}{{#if apiConfig.delete.permission.named}}
|
|
92
|
+
await checkPermission(caller, apiAction, {{#if parentPaths}}parentRefs{{else}}{{{emptyObject}}}{{/if}}, {{name}}_by_{{this}}, {{Name}}TypeDef);{{/if}}{{/if}}{{#if apiConfig.delete.hasValidate}}
|
|
93
|
+
if (typeof {{Name}}TypeDef.apiConfig.delete?.validate === "function") {
|
|
94
|
+
const validated = await {{Name}}TypeDef.apiConfig.delete.validate(caller, {{name}}_by_{{this}});
|
|
41
95
|
if (validated === true || (typeof validated === "object" && Object.keys(validated).length === 0)) {
|
|
42
96
|
// successfully validated
|
|
43
97
|
} else if (typeof validated === "object" && Object.keys(validated).length > 0) {
|
|
@@ -46,21 +100,29 @@ export default defineEventHandler(async (event: H3Event) => {
|
|
|
46
100
|
throw validationError({global: ["validation"]});
|
|
47
101
|
}
|
|
48
102
|
}{{/if}}
|
|
49
|
-
return await {{name}}Repo.remove({{name}}_by_{{this}});
|
|
103
|
+
return await {{name}}Repo.remove(caller, {{name}}_by_{{this}});
|
|
50
104
|
}
|
|
51
105
|
{{/each}}
|
|
52
106
|
} catch (e) {
|
|
53
|
-
if (e instanceof H3Error
|
|
107
|
+
if (e instanceof H3Error || e instanceof MobilettoOrmNotFoundError || e instanceof MobilettoOrmValidationError) {
|
|
108
|
+
throw e;
|
|
109
|
+
}
|
|
54
110
|
if (e instanceof MobilettoOrmSyncError) {
|
|
55
|
-
|
|
56
|
-
|
|
111
|
+
const msg = `${apiAction}: sync error: ${e}`;
|
|
112
|
+
{{logger}}.warn(msg);
|
|
113
|
+
throw conflict(msg);
|
|
57
114
|
}
|
|
58
|
-
|
|
59
|
-
|
|
115
|
+
const msg = `${apiAction}: unexpected error: ${e}`;
|
|
116
|
+
{{logger}}.error(msg);
|
|
117
|
+
throw serverError(msg);
|
|
60
118
|
}
|
|
61
|
-
throw notFound(id);{{#if
|
|
62
|
-
});{{else}}{{#if
|
|
63
|
-
});{{else}}{{#if
|
|
64
|
-
});{{
|
|
119
|
+
throw notFound(id);{{#if apiConfig.delete.permission.admin}}
|
|
120
|
+
});{{else}}{{#if apiConfig.delete.permission.owner}}
|
|
121
|
+
});{{else}}{{#if apiConfig.delete.permission.named}}
|
|
122
|
+
});{{else}}{{#if apiConfig.delete.permission.filter}}
|
|
123
|
+
});{{else}}{{#if apiConfig.delete.permission.session}}
|
|
124
|
+
});{{/if}}{{/if}}{{/if}}{{/if}}{{/if}}
|
|
125
|
+
});
|
|
65
126
|
});
|
|
66
127
|
});
|
|
128
|
+
{{/if}}
|
|
@@ -2,20 +2,57 @@
|
|
|
2
2
|
{{#if disclaimer}}// {{{ disclaimer }}}{{#if header}}
|
|
3
3
|
{{{header}}}{{/if}}{{/if}}
|
|
4
4
|
|
|
5
|
-
import { H3Event } from "h3";
|
|
6
|
-
import {
|
|
5
|
+
import { defineEventHandler, H3Event } from "h3";
|
|
6
|
+
import {
|
|
7
|
+
filterErrors,
|
|
8
|
+
forbidden,
|
|
9
|
+
notFound,
|
|
10
|
+
notImplemented,
|
|
11
|
+
validationError,
|
|
12
|
+
} from "{{errorFilterImport}}";
|
|
13
|
+
import {
|
|
14
|
+
requireDefaultAccess,
|
|
15
|
+
requireAccount,
|
|
16
|
+
requireAccountObject,
|
|
17
|
+
requireAdminAccountObject,
|
|
18
|
+
} from "~/filter/sessionFilter.js";
|
|
19
|
+
import { type {{callerType}} } from "{{typeDefPackage}}";
|
|
20
|
+
import { {{name}}Repository } from "~/repo/{{name}}Repo.js";
|
|
21
|
+
import { checkPermission } from "~/utils/auth.js";{{#if apiConfig.lookup.permission.filter}}
|
|
22
|
+
import { {{apiConfig.lookup.permission.filter.symbol}} } from "{{apiConfig.lookup.permission.filter.package}}";{{/if}}
|
|
23
|
+
{{#if parentPaths}}{{#each parentPaths}}import { {{typeName}}Repository } from "~/repo/{{typeName}}Repo.js";
|
|
24
|
+
{{/each}}
|
|
25
|
+
import { type MobilettoOrmObject, FIND_NOREDACT } from "mobiletto-orm-typedef";{{/if}}{{#if apiConfig.lookup.disabled}}
|
|
26
|
+
|
|
27
|
+
export default defineEventHandler((_event: H3Event) => {
|
|
28
|
+
// apiConfig.lookup.disabled was set; endpoint is disabled
|
|
29
|
+
throw notImplemented(`disabled: {{name}}::lookup`);
|
|
30
|
+
});
|
|
31
|
+
{{else}}
|
|
32
|
+
{{#nor apiConfig.lookup.permission.admin apiConfig.lookup.permission.owner apiConfig.lookup.permission.named apiConfig.lookup.permission.filter}}
|
|
33
|
+
import { anonymousCaller } from "mobiletto-orm";{{/nor}}
|
|
34
|
+
|
|
35
|
+
import { {{Name}}TypeDef, type {{sessionType}} } from "{{typeDefPackage}}";{{#if singletonDefaultImport}}
|
|
7
36
|
import { {{singletonDefault}} } from "{{singletonDefaultImport}}";{{/if}}
|
|
37
|
+
[[[include 'refs.ts']]]
|
|
38
|
+
const apiAction = "{{name}}.lookup";{{#nor apiConfig.lookup.permission.admin apiConfig.lookup.permission.owner apiConfig.lookup.permission.named apiConfig.lookup.permission.filter}}
|
|
39
|
+
const caller: {{callerType}} = anonymousCaller();{{/nor}}
|
|
8
40
|
|
|
9
41
|
export default defineEventHandler(async (event: H3Event) => {
|
|
10
|
-
return await filterErrors(event,
|
|
11
|
-
|
|
12
|
-
return await
|
|
13
|
-
return await
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
42
|
+
return await filterErrors(event, apiAction, async (event: H3Event) => {{{curlyOpen}}}
|
|
43
|
+
return await requireDefaultAccess(event, apiAction, async (event: H3Event, apiAction: string) => {{{curlyOpen}}}{{#if apiConfig.lookup.permission.admin}}
|
|
44
|
+
return await requireAdminAccountObject(event, apiAction, async (event: H3Event, apiAction: string, session: {{sessionType}}, caller: {{callerType}}) => {{{curlyOpen}}}{{else}}{{#if apiConfig.lookup.permission.owner}}
|
|
45
|
+
return await requireAccountObject(event, apiAction, async (event: H3Event, apiAction: string, session: {{sessionType}}, caller: {{callerType}}) => {{{curlyOpen}}}{{else}}{{#if apiConfig.lookup.permission.named}}
|
|
46
|
+
return await requireAccountObject(event, apiAction, async (event: H3Event, apiAction: string, session: {{sessionType}}, caller: {{callerType}}) => {{{curlyOpen}}}{{else}}{{#if apiConfig.lookup.permission.filter}}
|
|
47
|
+
return await requireAccountObject(event, apiAction, async (event: H3Event, apiAction: string, session: {{sessionType}}, caller: {{callerType}}) => {{{curlyOpen}}}{{else}}{{#if apiConfig.lookup.permission.session}}
|
|
48
|
+
return await requireAccount(event, apiAction, async (event: H3Event, apiAction: string, session: {{sessionType}}) => {{{curlyOpen}}}{{/if}}{{/if}}{{/if}}{{/if}}{{/if}}
|
|
49
|
+
{{#if parentPaths}}const parentRefs = await resolveRefs(caller, event);
|
|
50
|
+
{{/if}}const {{name}}Repo = {{name}}Repository({{eventContextVar}});
|
|
51
|
+
if ({{name}}Repo.initialize) await {{name}}Repo.initialize();
|
|
52
|
+
const id = {{#if typeDef.singleton}} "{{typeDef.singleton}}"; // {{name}} is a singleton: there can be only one, with id "{{typeDef.singleton}}"{{else}}event?.context?.params?.{{typeDef.idParam}};{{/if}}
|
|
53
|
+
if (!id) throw notFound({{name}}Repo.typeDef.idFieldName());{{#if apiConfig.lookup.hasValidate}}
|
|
17
54
|
if (typeof {{Name}}TypeDef.apiConfig.lookup?.validate === "function") {
|
|
18
|
-
const validated = await {{Name}}TypeDef.apiConfig.lookup.validate(
|
|
55
|
+
const validated = await {{Name}}TypeDef.apiConfig.lookup.validate(caller, id, {{eventContextVar}});
|
|
19
56
|
if (validated === true || (typeof validated === "object" && Object.keys(validated).length === 0)) {
|
|
20
57
|
// successfully validated
|
|
21
58
|
} else if (typeof validated === "object" && Object.keys(validated).length > 0) {
|
|
@@ -24,15 +61,39 @@ export default defineEventHandler(async (event: H3Event) => {
|
|
|
24
61
|
throw validationError({global: ["validation"]});
|
|
25
62
|
}
|
|
26
63
|
}{{/if}}
|
|
27
|
-
const {{name}}_by_id = await {{name}}Repo.safeFindById(id);
|
|
28
|
-
if ({{name}}_by_id)
|
|
64
|
+
const {{name}}_by_id = await {{name}}Repo.safeFindById(caller, id);
|
|
65
|
+
if ({{name}}_by_id) {{{curlyOpen}}}{{#if parentPaths}}
|
|
66
|
+
checkRefs({{name}}_by_id, parentRefs, event);{{/if}}{{#if apiConfig.lookup.permission.owner}}
|
|
67
|
+
if (!caller.admin && {{name}}_by_id.{{apiConfig.lookup.permission.owner}} && {{name}}_by_id.{{apiConfig.lookup.permission.owner}} !== session.account) {
|
|
68
|
+
throw forbidden(`apiAction=${apiAction} caller not owner/admin`);
|
|
69
|
+
}
|
|
70
|
+
{{else}}{{#if apiConfig.lookup.permission.named}}
|
|
71
|
+
await checkPermission(caller, apiAction, {{#if parentPaths}}parentRefs{{else}}{{{emptyObject}}}{{/if}}, {{name}}_by_id, {{Name}}TypeDef);{{else}}{{#if apiConfig.lookup.permission.filter}}
|
|
72
|
+
if (!(await {{apiConfig.lookup.permission.filter.symbol}}.allowAction({{eventContextVar}}, caller, apiAction, {{name}}_by_id, {{#if parentPaths}}parentRefs{{else}}{{{emptyObject}}}{{/if}}, {{Name}}TypeDef))) {
|
|
73
|
+
throw notFound(id);
|
|
74
|
+
}
|
|
75
|
+
{{/if}}{{/if}}{{/if}}
|
|
76
|
+
return {{name}}_by_id;
|
|
77
|
+
}
|
|
29
78
|
{{#each typeDef.alternateLookupFields}}
|
|
30
|
-
const {{../name}}_by_{{this}} = await {{../name}}Repo.safeFindFirstBy("{{this}}", id);
|
|
31
|
-
if ({{../name}}_by_{{this}})
|
|
79
|
+
const {{../name}}_by_{{this}} = await {{../name}}Repo.safeFindFirstBy(caller, "{{this}}", id);
|
|
80
|
+
if ({{../name}}_by_{{this}}) {
|
|
81
|
+
{{#if apiConfig.lookup.permission.owner}}
|
|
82
|
+
if (!caller.admin && {{../name}}_by_{{this}}.{{apiConfig.lookup.permission.owner}} && {{../name}}_by_{{this}}.{{apiConfig.lookup.permission.owner}} !== session.account) {
|
|
83
|
+
throw forbidden(`apiAction=${apiAction} caller not owner/admin`);
|
|
84
|
+
}
|
|
85
|
+
{{else}}{{#if apiConfig.lookup.permission.named}}
|
|
86
|
+
await checkPermission(caller, apiAction, {{#if parentPaths}}parentRefs{{else}}{{{emptyObject}}}{{/if}}, {{../name}}_by_{{this}}, {{Name}}TypeDef);{{/if}}{{/if}}
|
|
87
|
+
return {{../name}}_by_{{this}};
|
|
88
|
+
}
|
|
32
89
|
{{/each}}
|
|
33
|
-
{{#if singletonDefault}}return {{singletonDefault}}{{else}}throw notFound(id){{/if}};{{#if
|
|
34
|
-
});{{else}}{{#if
|
|
35
|
-
});{{else}}{{#if
|
|
36
|
-
});{{
|
|
90
|
+
{{#if singletonDefault}}return {{singletonDefault}}{{else}}throw notFound(id){{/if}};{{#if apiConfig.lookup.permission.admin}}
|
|
91
|
+
});{{else}}{{#if apiConfig.lookup.permission.owner}}
|
|
92
|
+
});{{else}}{{#if apiConfig.lookup.permission.named}}
|
|
93
|
+
});{{else}}{{#if apiConfig.lookup.permission.filter}}
|
|
94
|
+
});{{else}}{{#if apiConfig.lookup.permission.session}}
|
|
95
|
+
});{{/if}}{{/if}}{{/if}}{{/if}}{{/if}}
|
|
96
|
+
});
|
|
37
97
|
});
|
|
38
98
|
});
|
|
99
|
+
{{/if}}
|
|
@@ -1,32 +1,79 @@
|
|
|
1
|
-
/* eslint-disable
|
|
1
|
+
/* eslint-disable camelcase,@typescript-eslint/no-unused-vars */
|
|
2
2
|
{{#if disclaimer}}// {{{disclaimer}}}{{#if header}}
|
|
3
3
|
{{{header}}}{{/if}}{{/if}}
|
|
4
4
|
|
|
5
|
-
import { H3Event } from "h3";
|
|
6
|
-
import {
|
|
7
|
-
|
|
5
|
+
import { defineEventHandler, H3Event, readBody } from "h3";
|
|
6
|
+
import {
|
|
7
|
+
badRequest,
|
|
8
|
+
filterErrors,
|
|
9
|
+
forbidden,
|
|
10
|
+
notFound,
|
|
11
|
+
notImplemented,
|
|
12
|
+
validationError,
|
|
13
|
+
} from "{{errorFilterImport}}";
|
|
14
|
+
import {
|
|
15
|
+
requireDefaultAccess,
|
|
16
|
+
requireAccount,
|
|
17
|
+
requireAccountObject,
|
|
18
|
+
requireAdminAccountObject,
|
|
19
|
+
} from "~/filter/sessionFilter.js";
|
|
20
|
+
import { type {{callerType}} } from "{{typeDefPackage}}";
|
|
21
|
+
import { {{name}}Repository } from "~/repo/{{name}}Repo.js";
|
|
22
|
+
import { checkPermission } from "~/utils/auth.js";{{#if apiConfig.update.permission.filter}}
|
|
23
|
+
import { {{apiConfig.update.permission.filter.symbol}} } from "{{apiConfig.update.permission.filter.package}}";{{/if}}
|
|
24
|
+
{{#if parentPaths}}{{#each parentPaths}}import { {{typeName}}Repository } from "~/repo/{{typeName}}Repo.js";
|
|
25
|
+
{{/each}}
|
|
26
|
+
import { type MobilettoOrmObject, FIND_NOREDACT } from "mobiletto-orm-typedef";{{/if}}{{#if apiConfig.update.disabled}}
|
|
27
|
+
|
|
28
|
+
export default defineEventHandler((_event: H3Event) => {
|
|
29
|
+
// apiConfig.update.disabled was set; endpoint is disabled
|
|
30
|
+
throw notImplemented(`disabled: {{name}}::update`);
|
|
31
|
+
});
|
|
32
|
+
{{else}}
|
|
33
|
+
{{#nor apiConfig.update.permission.admin apiConfig.update.permission.owner apiConfig.update.permission.named apiConfig.lookup.permission.filter}}
|
|
34
|
+
import { anonymousCaller } from "mobiletto-orm";{{/nor}}
|
|
35
|
+
|
|
36
|
+
import {
|
|
37
|
+
MobilettoOrmNotFoundError,
|
|
38
|
+
MobilettoOrmValidationErrors
|
|
39
|
+
} from "mobiletto-orm-typedef";
|
|
40
|
+
import { {{#ne callerTypeName Name}}type {{../Name}}Type, {{/ne}}{{Name}}TypeDef{{#ne sessionType TypeNameType}}, type {{../sessionType}}{{/ne}} } from "{{typeDefPackage}}";
|
|
41
|
+
[[[include 'refs.ts']]]
|
|
42
|
+
const apiAction = "{{name}}.update";{{#nor apiConfig.update.permission.admin apiConfig.update.permission.owner apiConfig.update.permission.named apiConfig.lookup.permission.filter}}
|
|
43
|
+
const caller: {{callerType}} = anonymousCaller();{{/nor}}
|
|
8
44
|
|
|
9
45
|
export default defineEventHandler(async (event: H3Event) => {
|
|
10
|
-
return await filterErrors(event,
|
|
11
|
-
|
|
12
|
-
return await
|
|
13
|
-
return await
|
|
14
|
-
|
|
15
|
-
|
|
46
|
+
return await filterErrors(event, apiAction, async (event: H3Event) => {{{curlyOpen}}}
|
|
47
|
+
return await requireDefaultAccess(event, apiAction, async (event: H3Event, apiAction: string) => {{{curlyOpen}}}{{#if apiConfig.update.permission.admin}}
|
|
48
|
+
return await requireAdminAccountObject(event, apiAction, async (event: H3Event, apiAction: string, session: {{sessionType}}, caller: {{callerType}}) => {{{curlyOpen}}}{{else}}{{#if apiConfig.update.permission.owner}}
|
|
49
|
+
return await requireAccountObject(event, apiAction, async (event: H3Event, apiAction: string, session: {{sessionType}}, caller: {{callerType}}) => {{{curlyOpen}}}{{else}}{{#if apiConfig.update.permission.named}}
|
|
50
|
+
return await requireAccountObject(event, apiAction, async (event: H3Event, apiAction: string, session: {{sessionType}}, caller: {{callerType}}) => {{{curlyOpen}}}{{else}}{{#if apiConfig.update.permission.filter}}
|
|
51
|
+
return await requireAccountObject(event, apiAction, async (event: H3Event, apiAction: string, session: {{sessionType}}, caller: {{callerType}}) => {{{curlyOpen}}}{{else}}{{#if apiConfig.update.permission.session}}
|
|
52
|
+
return await requireAccount(event, apiAction, async (event: H3Event, apiAction: string, session: {{sessionType}}) => {{{curlyOpen}}}{{/if}}{{/if}}{{/if}}{{/if}}{{/if}}
|
|
53
|
+
{{#if parentPaths}}const parentRefs = await resolveRefs(caller, event);
|
|
54
|
+
{{/if}}const {{name}}Repo = {{name}}Repository({{eventContextVar}});
|
|
55
|
+
if ({{name}}Repo.initialize) await {{name}}Repo.initialize();
|
|
56
|
+
const id = event?.context?.params?.{{typeDef.idParam}};
|
|
16
57
|
if (!id) throw notFound({{name}}Repo.typeDef.idFieldName());
|
|
17
58
|
const obj: {{Name}}Type = await readBody(event);
|
|
18
59
|
if (!obj) {
|
|
19
|
-
throw badRequest();
|
|
60
|
+
throw badRequest(`apiAction=${apiAction} no object in request body`);
|
|
20
61
|
}
|
|
21
62
|
if ({{Name}}TypeDef.id(obj) !== id) {
|
|
22
63
|
const errs: MobilettoOrmValidationErrors = {};
|
|
23
64
|
errs[{{name}}Repo.typeDef.idFieldName()] = ["mismatch"];
|
|
24
65
|
throw validationError(errs);
|
|
25
|
-
} else {
|
|
26
|
-
{{
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
66
|
+
} else {{{curlyOpen}}}{{#if apiConfig.update.permission.owner}}
|
|
67
|
+
if (!caller.admin && obj.{{apiConfig.update.permission.owner}} && obj.{{apiConfig.update.permission.owner}} !== session.account) {
|
|
68
|
+
throw forbidden(`apiAction=${apiAction} caller not owner/admin`);
|
|
69
|
+
}
|
|
70
|
+
{{else}}{{#if apiConfig.update.permission.named}}
|
|
71
|
+
await checkPermission(caller, apiAction, {{#if parentPaths}}parentRefs{{else}}{{{emptyObject}}}{{/if}}, obj, {{Name}}TypeDef);{{else}}{{#if apiConfig.update.permission.filter}}
|
|
72
|
+
if (!(await {{apiConfig.update.permission.filter.symbol}}.allowAction({{eventContextVar}}, caller, apiAction, obj, {{#if parentPaths}}parentRefs{{else}}{{{emptyObject}}}{{/if}}, {{Name}}TypeDef))) {
|
|
73
|
+
throw notFound(id);
|
|
74
|
+
}{{/if}}{{/if}}{{/if}}{{#if apiConfig.update.hasValidate}}
|
|
75
|
+
if (typeof {{Name}}TypeDef.apiConfig.update?.validate === "function") {
|
|
76
|
+
const validated = await {{Name}}TypeDef.apiConfig.update.validate(caller, obj, {{eventContextVar}});
|
|
30
77
|
if (validated === true || (typeof validated === "object" && Object.keys(validated).length === 0)) {
|
|
31
78
|
// successfully validated
|
|
32
79
|
} else if (typeof validated === "object" && Object.keys(validated).length > 0) {
|
|
@@ -34,18 +81,23 @@ export default defineEventHandler(async (event: H3Event) => {
|
|
|
34
81
|
} else {
|
|
35
82
|
throw validationError({global: ["validation"]});
|
|
36
83
|
}
|
|
37
|
-
}{{/if}}
|
|
84
|
+
}{{/if}}{{#if parentPaths}}
|
|
85
|
+
checkRefs(obj, parentRefs, event);{{/if}}
|
|
38
86
|
{{#if typeDef.singleton}}try {
|
|
39
|
-
return await {{name}}Repo.update(obj);
|
|
87
|
+
return await {{name}}Repo.update(caller, obj);
|
|
40
88
|
} catch (e) {
|
|
41
89
|
if (e instanceof MobilettoOrmNotFoundError) {
|
|
42
|
-
return await {{name}}Repo.create(obj);
|
|
90
|
+
return await {{name}}Repo.create(caller, obj);
|
|
43
91
|
}
|
|
44
92
|
throw e;
|
|
45
|
-
}{{else}}return await {{name}}Repo.update(obj);{{/if}}
|
|
46
|
-
}{{#if
|
|
47
|
-
});{{else}}{{#if
|
|
48
|
-
});{{else}}{{#if
|
|
49
|
-
});{{
|
|
93
|
+
}{{else}}return await {{name}}Repo.update(caller, obj);{{/if}}
|
|
94
|
+
}{{#if apiConfig.update.permission.admin}}
|
|
95
|
+
});{{else}}{{#if apiConfig.update.permission.owner}}
|
|
96
|
+
});{{else}}{{#if apiConfig.update.permission.named}}
|
|
97
|
+
});{{else}}{{#if apiConfig.update.permission.filter}}
|
|
98
|
+
});{{else}}{{#if apiConfig.update.permission.session}}
|
|
99
|
+
});{{/if}}{{/if}}{{/if}}{{/if}}{{/if}}
|
|
100
|
+
});
|
|
50
101
|
});
|
|
51
102
|
});
|
|
103
|
+
{{/if}}
|