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
|
@@ -1,30 +1,71 @@
|
|
|
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";
|
|
5
|
+
import { defineEventHandler, H3Event, readBody } from "h3";
|
|
6
|
+
import {
|
|
7
|
+
badRequest,
|
|
8
|
+
filterErrors,
|
|
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.create.permission.filter}}
|
|
22
|
+
import { {{apiConfig.create.permission.filter.symbol}} } from "{{apiConfig.create.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.create.disabled}}
|
|
26
|
+
|
|
27
|
+
export default defineEventHandler((_event: H3Event) => {
|
|
28
|
+
// apiConfig.create.disabled was set; endpoint is disabled
|
|
29
|
+
throw notImplemented(`disabled: {{name}}::create`);
|
|
30
|
+
});
|
|
31
|
+
{{else}}
|
|
32
|
+
{{#if parentPaths}}{{#nor apiConfig.create.permission.admin apiConfig.create.permission.owner apiConfig.create.permission.named apiConfig.create.permission.filter}}
|
|
33
|
+
import { anonymousCaller } from "mobiletto-orm";{{/nor}}{{/if}}
|
|
34
|
+
|
|
6
35
|
import { MobilettoOrmValidationErrors } from "mobiletto-orm-typedef";
|
|
7
|
-
import { {{Name}}Type, {{Name}}TypeDef } from "{{typeDefPackage}}";
|
|
36
|
+
import { {{#ne callerTypeName Name}}type {{../Name}}Type, {{/ne}}{{Name}}TypeDef{{#ne sessionType TypeNameType}}, type {{../sessionType}}{{/ne}} } from "{{typeDefPackage}}";
|
|
37
|
+
[[[include 'refs.ts']]]
|
|
38
|
+
const apiAction = "{{name}}.create";{{#nor apiConfig.create.permission.admin apiConfig.create.permission.owner apiConfig.create.permission.named apiConfig.create.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
|
-
|
|
42
|
+
return await filterErrors(event, apiAction, async (event: H3Event) => {{{curlyOpen}}}
|
|
43
|
+
return await requireDefaultAccess(event, apiAction, async (event: H3Event, apiAction: string) => {{{curlyOpen}}}{{#if apiConfig.create.permission.admin}}
|
|
44
|
+
return await requireAdminAccountObject(event, apiAction, async (event: H3Event, apiAction: string, session: {{sessionType}}, caller: {{callerType}}) => {{{curlyOpen}}}{{else}}{{#if apiConfig.create.permission.owner}}
|
|
45
|
+
return await requireAccountObject(event, apiAction, async (event: H3Event, apiAction: string, session: {{sessionType}}, caller: {{callerType}}) => {{{curlyOpen}}}{{else}}{{#if apiConfig.create.permission.named}}
|
|
46
|
+
return await requireAccountObject(event, apiAction, async (event: H3Event, apiAction: string, session: {{sessionType}}, caller: {{callerType}}) => {{{curlyOpen}}}{{else}}{{#if apiConfig.create.permission.filter}}
|
|
47
|
+
return await requireAccountObject(event, apiAction, async (event: H3Event, apiAction: string, session: {{sessionType}}, caller: {{callerType}}) => {{{curlyOpen}}}{{else}}{{#if apiConfig.create.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 = event?.context?.params?.{{typeDef.idParam}};
|
|
16
53
|
if (!id) throw notFound({{name}}Repo.typeDef.idFieldName());
|
|
17
54
|
const obj: {{Name}}Type = await readBody(event);
|
|
18
55
|
if (!obj) {
|
|
19
|
-
throw badRequest();
|
|
56
|
+
throw badRequest(`apiAction=${apiAction} no object in request body`);
|
|
20
57
|
}
|
|
21
58
|
if ({{Name}}TypeDef.id(obj) !== id) {
|
|
22
59
|
const errs: MobilettoOrmValidationErrors = {};
|
|
23
60
|
errs[{{name}}Repo.typeDef.idFieldName()] = ["mismatch"];
|
|
24
61
|
throw validationError(errs);
|
|
25
|
-
} else {
|
|
26
|
-
{{#if
|
|
27
|
-
|
|
62
|
+
} else {{{curlyOpen}}}{{#if apiConfig.create.permission.named}}
|
|
63
|
+
await checkPermission(caller, apiAction, {{#if parentPaths}}parentRefs{{else}}{{{emptyObject}}}{{/if}}, obj, {{Name}}TypeDef);{{else}}{{#if apiConfig.create.permission.filter}}
|
|
64
|
+
if (!(await {{apiConfig.create.permission.filter.symbol}}.allowAction({{eventContextVar}}, caller, apiAction, obj, {{#if parentPaths}}parentRefs{{else}}{{{emptyObject}}}{{/if}}, {{Name}}TypeDef))) {
|
|
65
|
+
throw validationError({ global: ["filter"] });
|
|
66
|
+
}{{/if}}{{/if}}{{#if apiConfig.create.hasValidate}}
|
|
67
|
+
if (typeof {{Name}}TypeDef.apiConfig.create?.validate === "function") {
|
|
68
|
+
const validated = await {{Name}}TypeDef.apiConfig.create.validate(caller, obj, {{eventContextVar}});
|
|
28
69
|
if (validated === true || (typeof validated === "object" && Object.keys(validated).length === 0)) {
|
|
29
70
|
// successfully validated
|
|
30
71
|
} else if (typeof validated === "object" && Object.keys(validated).length > 0) {
|
|
@@ -33,10 +74,16 @@ export default defineEventHandler(async (event: H3Event) => {
|
|
|
33
74
|
throw validationError({global: ["validation"]});
|
|
34
75
|
}
|
|
35
76
|
}
|
|
36
|
-
{{/if}}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
});{{
|
|
77
|
+
{{/if}}{{#if parentPaths}}
|
|
78
|
+
checkRefs(obj, parentRefs, event);{{/if}}
|
|
79
|
+
return await {{name}}Repo.create(caller, obj);
|
|
80
|
+
}{{#if apiConfig.create.permission.admin}}
|
|
81
|
+
});{{else}}{{#if apiConfig.create.permission.owner}}
|
|
82
|
+
});{{else}}{{#if apiConfig.create.permission.named}}
|
|
83
|
+
});{{else}}{{#if apiConfig.create.permission.filter}}
|
|
84
|
+
});{{else}}{{#if apiConfig.create.permission.session}}
|
|
85
|
+
});{{/if}}{{/if}}{{/if}}{{/if}}{{/if}}
|
|
86
|
+
});
|
|
41
87
|
});
|
|
42
88
|
});
|
|
89
|
+
{{/if}}
|
|
@@ -1,39 +1,128 @@
|
|
|
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
|
-
import {
|
|
5
|
+
import { defineEventHandler, H3Event, readBody } from "h3";
|
|
6
|
+
import pLimit from "p-limit";
|
|
7
|
+
import { filterErrors, validationError, notImplemented, notFound } from "{{errorFilterImport}}";
|
|
8
|
+
import {
|
|
9
|
+
requireDefaultAccess,
|
|
10
|
+
requireAccount,
|
|
11
|
+
requireAccountObject,
|
|
12
|
+
requireAdminAccountObject,
|
|
13
|
+
} from "~/filter/sessionFilter.js";
|
|
14
|
+
import { type {{callerType}}{{#or apiConfig.search.permission.owner apiConfig.search.permission.filter}}, {{callerType}}Def{{/or}} } from "{{typeDefPackage}}";
|
|
15
|
+
import { {{name}}Repository } from "~/repo/{{name}}Repo.js";
|
|
16
|
+
import { checkPermission } from "~/utils/auth.js";{{#if apiConfig.search.permission.filter}}
|
|
17
|
+
import { {{apiConfig.search.permission.filter.symbol}} } from "{{apiConfig.search.permission.filter.package}}";{{/if}}
|
|
18
|
+
{{#if parentPaths}}{{#each parentPaths}}import { {{typeName}}Repository } from "~/repo/{{typeName}}Repo.js";
|
|
19
|
+
{{/each}}
|
|
20
|
+
import { type MobilettoOrmObject, type MobilettoDbFindOpts, type MobilettoOrmFindOpts, FIND_NOREDACT } from "mobiletto-orm-typedef";{{else}}
|
|
21
|
+
import { type MobilettoDbFindOpts, type MobilettoOrmFindOpts } from "mobiletto-orm-typedef";{{/if}}{{#if apiConfig.search.disabled}}
|
|
22
|
+
|
|
23
|
+
export default defineEventHandler((_event: H3Event) => {
|
|
24
|
+
// apiConfig.search.disabled was set; endpoint is disabled
|
|
25
|
+
throw notImplemented(`disabled: {{name}}::search`);
|
|
26
|
+
});
|
|
27
|
+
{{else}}
|
|
28
|
+
{{#nor apiConfig.search.permission.admin apiConfig.search.permission.owner apiConfig.search.permission.named apiConfig.search.permission.filter}}
|
|
29
|
+
import { anonymousCaller } from "mobiletto-orm";{{/nor}}
|
|
30
|
+
|
|
31
|
+
{{#unless typeDef.singleton}}{{#unless parentPaths}}
|
|
32
|
+
import { type MobilettoOrmObject } from "mobiletto-orm-typedef";{{/unless}}{{/unless}}
|
|
33
|
+
import { {{Name}}TypeDef, type {{sessionType}} } from "{{typeDefPackage}}";
|
|
34
|
+
import { FindApiOpts } from "{{findApiOptsPackage}}";
|
|
35
|
+
[[[include 'refs.ts']]]
|
|
36
|
+
const apiAction = "{{name}}.search";{{#nor apiConfig.search.permission.admin apiConfig.search.permission.owner apiConfig.search.permission.named apiConfig.search.permission.filter}}
|
|
37
|
+
const caller: {{callerType}} = anonymousCaller();{{/nor}}
|
|
8
38
|
|
|
9
39
|
export default defineEventHandler(async (event: H3Event) => {
|
|
10
|
-
return await filterErrors(event,
|
|
11
|
-
|
|
12
|
-
return await
|
|
13
|
-
return await
|
|
14
|
-
|
|
40
|
+
return await filterErrors(event, apiAction, async (event: H3Event) => {{{curlyOpen}}}
|
|
41
|
+
return await requireDefaultAccess(event, apiAction, async (event: H3Event, apiAction: string) => {{{curlyOpen}}}{{#if apiConfig.search.permission.admin}}
|
|
42
|
+
return await requireAdminAccountObject(event, apiAction, async (event: H3Event, apiAction: string, session: {{sessionType}}, caller: {{callerType}}) => {{{curlyOpen}}}{{else}}{{#if apiConfig.search.permission.owner}}
|
|
43
|
+
return await requireAccountObject(event, apiAction, async (event: H3Event, apiAction: string, session: {{sessionType}}, caller: {{callerType}}) => {{{curlyOpen}}}{{else}}{{#if apiConfig.search.permission.named}}
|
|
44
|
+
return await requireAccountObject(event, apiAction, async (event: H3Event, apiAction: string, session: {{sessionType}}, caller: {{callerType}}) => {{{curlyOpen}}}{{else}}{{#if apiConfig.search.permission.filter}}
|
|
45
|
+
return await requireAccountObject(event, apiAction, async (event: H3Event, apiAction: string, session: {{sessionType}}, caller: {{callerType}}) => {{{curlyOpen}}}{{else}}{{#if apiConfig.search.permission.session}}
|
|
46
|
+
return await requireAccount(event, apiAction, async (event: H3Event, apiAction: string, session: {{sessionType}}) => {{{curlyOpen}}}{{/if}}{{/if}}{{/if}}{{/if}}{{/if}}
|
|
47
|
+
{{#if parentPaths}}const parentRefs = await resolveRefs(caller, event);
|
|
48
|
+
{{/if}}const opts: FindApiOpts = {{#if typeDef.singleton}}{}; // {{name}} is a singleton, options make no sense{{else}}(await readBody(event)) || {};{{#if apiConfig.search.hasValidate}}{{/if}}
|
|
15
49
|
if (typeof {{Name}}TypeDef.apiConfig.search?.validate === "function") {
|
|
16
|
-
const validated = await {{Name}}TypeDef.apiConfig.search.validate(
|
|
50
|
+
const validated = await {{Name}}TypeDef.apiConfig.search.validate(caller, null, {{eventContextVar}}, opts);
|
|
17
51
|
if (validated === true || (typeof validated === "object" && Object.keys(validated).length === 0)) {
|
|
18
52
|
// successfully validated
|
|
19
53
|
} else if (typeof validated === "object" && Object.keys(validated).length > 0) {
|
|
20
54
|
throw validationError(validated);
|
|
21
55
|
} else {
|
|
22
|
-
throw validationError({global: ["validation"]});
|
|
56
|
+
throw validationError({ global: ["validation"] });
|
|
23
57
|
}
|
|
24
58
|
}{{/if}}
|
|
25
|
-
const {{name}}Repo = {{name}}Repository(
|
|
26
|
-
|
|
59
|
+
const {{name}}Repo = {{name}}Repository({{eventContextVar}});
|
|
60
|
+
if ({{name}}Repo.initialize) await {{name}}Repo.initialize();{{#if apiConfig.search.permission.named}}
|
|
61
|
+
await checkPermission(caller, apiAction, {{#if parentPaths}}parentRefs{{else}}{{{emptyObject}}}{{/if}}, {{#if parentRefField}}parentRefs.{{parentRefField}}{{else}}null{{/if}}, {{Name}}TypeDef);{{else}}{{#if apiConfig.search.permission.filter}}
|
|
62
|
+
if (!(await {{apiConfig.search.permission.filter.symbol}}.allowSearch({{eventContextVar}}, caller, apiAction, opts, {{#if parentPaths}}parentRefs{{else}}{{{emptyObject}}}{{/if}}, {{Name}}TypeDef))) {
|
|
63
|
+
return { objects: [], object: null };
|
|
64
|
+
}{{/if}}{{/if}}{{#if typeDef.singleton}}
|
|
65
|
+
const singleton = await {{name}}Repo.safeFindById(caller, "{{typeDef.singleton}}");
|
|
27
66
|
return singleton ? [ singleton ] : [];{{else}}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
67
|
+
let results;
|
|
68
|
+
const db: MobilettoDbFindOpts | undefined = typeof opts.db === "object" ? opts.db : undefined;
|
|
69
|
+
if (db) {
|
|
70
|
+
if (typeof {{name}}Repo.findByDb !== "function") {
|
|
71
|
+
throw notImplemented(`apiAction=${apiAction} {{name}}Repo.findByDb not a function`);
|
|
72
|
+
}{{#if apiConfig.search.permission.filter}}
|
|
73
|
+
await {{apiConfig.search.permission.filter.symbol}}.adjustSearchOptions({{eventContextVar}}, caller, apiAction, db, {{Name}}TypeDef);{{/if}}
|
|
74
|
+
results = await {{name}}Repo.findByDb!(caller, db)
|
|
32
75
|
} else {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
76
|
+
const predicate = opts.textSearch && opts.textSearch.trim().length > 0
|
|
77
|
+
? (obj: MobilettoOrmObject) => {{Name}}TypeDef.textMatch(obj, opts.textSearch || "")
|
|
78
|
+
: undefined;
|
|
79
|
+
const requestedOpts: MobilettoOrmFindOpts = opts.opts || {};
|
|
80
|
+
const searchOpts: MobilettoOrmFindOpts = predicate ? Object.assign({}, requestedOpts, { predicate }) : requestedOpts;{{#if apiConfig.search.permission.filter}}
|
|
81
|
+
await {{apiConfig.search.permission.filter.symbol}}.adjustSearchOptions({{eventContextVar}}, caller, apiAction, searchOpts, {{Name}}TypeDef);{{/if}}{{#if parentRefField}}
|
|
82
|
+
opts.indexes ||= {};{{/if}}
|
|
83
|
+
if (opts.field && opts.value) {
|
|
84
|
+
opts.indexes ||= {};
|
|
85
|
+
opts.indexes[opts.field] = opts.value;
|
|
86
|
+
}
|
|
87
|
+
if (opts.indexes) {
|
|
88
|
+
{{#if parentRefField}}opts.indexes.{{parentRefField}} = {{parentRef.typeName}}Repository({{eventContextVar}}).typeDef.id(parentRefs.{{parentRefField}}) || "";{{/if}}
|
|
89
|
+
results = await {{name}}Repo.safeFindByIndex(caller, opts.indexes, searchOpts);
|
|
90
|
+
} else if (opts.field && opts.value) {
|
|
91
|
+
results = await {{name}}Repo.safeFindBy(caller, opts.field, opts.value, searchOpts);
|
|
92
|
+
} else {
|
|
93
|
+
results = await {{name}}Repo.find(caller, searchOpts);
|
|
94
|
+
}{{#if apiConfig.search.permission.owner}}
|
|
95
|
+
const callerId = {{callerType}}Def.id(caller);
|
|
96
|
+
if (results.objects) {
|
|
97
|
+
results.objects = results.objects.filter((r) => caller.admin || r.{{apiConfig.search.permission.owner}} === callerId);
|
|
98
|
+
}
|
|
99
|
+
if (!caller.admin && results.object && results.object.{{apiConfig.search.permission.owner}} !== callerId) {
|
|
100
|
+
results.object = undefined;
|
|
101
|
+
}{{/if}}{{#if apiConfig.search.permission.filter}}
|
|
102
|
+
if (results.objects) {
|
|
103
|
+
const limit = pLimit({{#if apiConfig.search.permission.filter.concurrency}}{{apiConfig.search.permission.filter.concurrency}}{{else}}4{{/if}});
|
|
104
|
+
const checks: boolean[] = await Promise.all(
|
|
105
|
+
results.objects.map(r =>
|
|
106
|
+
limit(() =>
|
|
107
|
+
caller.admin
|
|
108
|
+
? Promise.resolve(true)
|
|
109
|
+
: {{apiConfig.search.permission.filter.symbol}}.allowResult({{eventContextVar}}, caller, apiAction, opts, r, {{#if parentPaths}}parentRefs{{else}}{{{emptyObject}}}{{/if}}, {{Name}}TypeDef)
|
|
110
|
+
)
|
|
111
|
+
)
|
|
112
|
+
);
|
|
113
|
+
results.objects = results.objects.filter((_, i) => checks[i]);
|
|
114
|
+
}
|
|
115
|
+
if (!caller.admin && results.object && !(await {{apiConfig.search.permission.filter.symbol}}.allowResult({{eventContextVar}}, caller, apiAction, opts, results.object, {{#if parentPaths}}parentRefs{{else}}{{{emptyObject}}}{{/if}}, {{Name}}TypeDef))) {
|
|
116
|
+
results.object = undefined;
|
|
117
|
+
}{{/if}}
|
|
118
|
+
}
|
|
119
|
+
return results;{{/if}}{{#if apiConfig.search.permission.admin}}
|
|
120
|
+
});{{else}}{{#if apiConfig.search.permission.owner}}
|
|
121
|
+
});{{else}}{{#if apiConfig.search.permission.named}}
|
|
122
|
+
});{{else}}{{#if apiConfig.search.permission.filter}}
|
|
123
|
+
});{{else}}{{#if apiConfig.search.permission.session}}
|
|
124
|
+
});{{/if}}{{/if}}{{/if}}{{/if}}{{/if}}
|
|
125
|
+
});
|
|
38
126
|
});
|
|
39
127
|
});
|
|
128
|
+
{{/if}}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
+
{{#if disclaimer}}// {{{disclaimer}}}{{#if header}}
|
|
3
|
+
{{{header}}}{{/if}}{{/if}}
|
|
4
|
+
|
|
5
|
+
import { defineEventHandler, H3Event, readBody } from "h3";
|
|
6
|
+
import { badRequest, filterErrors, validationError, notImplemented, notFound } from "{{errorFilterImport}}";
|
|
7
|
+
import {
|
|
8
|
+
requireDefaultAccess,
|
|
9
|
+
requireAccount,
|
|
10
|
+
requireAccountObject,
|
|
11
|
+
requireAdminAccountObject,
|
|
12
|
+
} from "~/filter/sessionFilter.js";
|
|
13
|
+
import { type {{callerType}} } from "{{typeDefPackage}}";
|
|
14
|
+
import { {{name}}Repository } from "~/repo/{{name}}Repo.js";
|
|
15
|
+
import { checkPermission } from "~/utils/auth.js";{{#if apiConfig.create.permission.filter}}
|
|
16
|
+
import { {{apiConfig.create.permission.filter.symbol}} } from "{{apiConfig.create.permission.filter.package}}";{{/if}}
|
|
17
|
+
{{#if parentPaths}}{{#each parentPaths}}import { {{typeName}}Repository } from "~/repo/{{typeName}}Repo.js";
|
|
18
|
+
{{/each}}
|
|
19
|
+
import { type MobilettoOrmObject, FIND_NOREDACT } from "mobiletto-orm-typedef";{{/if}}{{#if apiConfig.create.disabled}}
|
|
20
|
+
|
|
21
|
+
export default defineEventHandler((_event: H3Event) => {
|
|
22
|
+
// apiConfig.create.disabled was set; endpoint is disabled
|
|
23
|
+
throw notImplemented(`disabled: {{name}}::create`);
|
|
24
|
+
});
|
|
25
|
+
{{else}}
|
|
26
|
+
{{#if parentPaths}}{{#nor apiConfig.create.permission.admin apiConfig.create.permission.owner apiConfig.create.permission.named apiConfig.create.permission.filter}}
|
|
27
|
+
import { anonymousCaller } from "mobiletto-orm";{{/nor}}{{/if}}
|
|
28
|
+
|
|
29
|
+
import { {{#ne callerTypeName Name}}type {{../Name}}Type, {{/ne}}{{Name}}TypeDef{{#ne sessionType TypeNameType}}, type {{../sessionType}}{{/ne}} } from "{{typeDefPackage}}";
|
|
30
|
+
[[[include 'refs.ts']]]
|
|
31
|
+
const apiAction = "{{name}}.create";{{#nor apiConfig.create.permission.admin apiConfig.create.permission.owner apiConfig.create.permission.named apiConfig.create.permission.filter}}
|
|
32
|
+
const caller: {{callerType}} = anonymousCaller();{{/nor}}
|
|
33
|
+
|
|
34
|
+
export default defineEventHandler(async (event: H3Event) => {
|
|
35
|
+
return await filterErrors(event, apiAction, async (event: H3Event) => {{{curlyOpen}}}
|
|
36
|
+
return await requireDefaultAccess(event, apiAction, async (event: H3Event, apiAction: string) => {{{curlyOpen}}}{{#if apiConfig.create.permission.admin}}
|
|
37
|
+
return await requireAdminAccountObject(event, apiAction, async (event: H3Event, apiAction: string, session: {{sessionType}}, caller: {{callerType}}) => {{{curlyOpen}}}{{else}}{{#if apiConfig.create.permission.owner}}
|
|
38
|
+
return await requireAccountObject(event, apiAction, async (event: H3Event, apiAction: string, session: {{sessionType}}, caller: {{callerType}}) => {{{curlyOpen}}}{{else}}{{#if apiConfig.create.permission.named}}
|
|
39
|
+
return await requireAccountObject(event, apiAction, async (event: H3Event, apiAction: string, session: {{sessionType}}, caller: {{callerType}}) => {{{curlyOpen}}}{{else}}{{#if apiConfig.create.permission.filter}}
|
|
40
|
+
return await requireAccountObject(event, apiAction, async (event: H3Event, apiAction: string, session: {{sessionType}}, caller: {{callerType}}) => {{{curlyOpen}}}{{else}}{{#if apiConfig.create.permission.session}}
|
|
41
|
+
return await requireAccount(event, apiAction, async (event: H3Event, apiAction: string, session: {{sessionType}}) => {{{curlyOpen}}}{{/if}}{{/if}}{{/if}}{{/if}}{{/if}}
|
|
42
|
+
{{#if parentPaths}}const parentRefs = await resolveRefs(caller, event);
|
|
43
|
+
{{/if}}const {{name}}Repo = {{name}}Repository({{eventContextVar}});
|
|
44
|
+
if ({{name}}Repo.initialize) await {{name}}Repo.initialize();
|
|
45
|
+
const obj: {{Name}}Type = await readBody(event);
|
|
46
|
+
if (!obj) {
|
|
47
|
+
throw badRequest(`apiAction=${apiAction} no object in request body`);
|
|
48
|
+
}
|
|
49
|
+
{{#if apiConfig.create.permission.named}}
|
|
50
|
+
await checkPermission(caller, apiAction, {{#if parentPaths}}parentRefs{{else}}{{{emptyObject}}}{{/if}}, obj, {{Name}}TypeDef);{{else}}{{#if apiConfig.create.permission.filter}}
|
|
51
|
+
if (!(await {{apiConfig.create.permission.filter.symbol}}.allowAction({{eventContextVar}}, caller, apiAction, obj, {{#if parentPaths}}parentRefs{{else}}{{{emptyObject}}}{{/if}}, {{Name}}TypeDef))) {
|
|
52
|
+
throw validationError({ global: ["filter"] });
|
|
53
|
+
}{{/if}}{{/if}}{{#if apiConfig.create.hasValidate}}
|
|
54
|
+
if (typeof {{Name}}TypeDef.apiConfig.create?.validate === "function") {
|
|
55
|
+
const validated = await {{Name}}TypeDef.apiConfig.create.validate(caller, obj, {{eventContextVar}});
|
|
56
|
+
if (validated === true || (typeof validated === "object" && Object.keys(validated).length === 0)) {
|
|
57
|
+
// successfully validated
|
|
58
|
+
} else if (typeof validated === "object" && Object.keys(validated).length > 0) {
|
|
59
|
+
throw validationError(validated);
|
|
60
|
+
} else {
|
|
61
|
+
throw validationError({global: ["validation"]});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
{{/if}}{{#if parentPaths}}
|
|
65
|
+
checkRefs(obj, parentRefs, event);{{/if}}
|
|
66
|
+
return await {{name}}Repo.create(caller, obj);{{#if apiConfig.create.permission.admin}}
|
|
67
|
+
});{{else}}{{#if apiConfig.create.permission.owner}}
|
|
68
|
+
});{{else}}{{#if apiConfig.create.permission.named}}
|
|
69
|
+
});{{else}}{{#if apiConfig.create.permission.filter}}
|
|
70
|
+
});{{else}}{{#if apiConfig.create.permission.session}}
|
|
71
|
+
});{{/if}}{{/if}}{{/if}}{{/if}}{{/if}}
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
{{/if}}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{{#if parentPaths}}
|
|
2
|
+
|
|
3
|
+
/* eslint-disable camelcase */
|
|
4
|
+
|
|
5
|
+
const resolveRefs = async (caller: {{callerType}}, event: H3Event): Promise<Record<string, MobilettoOrmObject>> => {
|
|
6
|
+
{{#each parentPaths}}
|
|
7
|
+
const {{typeName}}Repo = {{typeName}}Repository({{../eventContextVar}});
|
|
8
|
+
if ({{typeName}}Repo.initialize) await {{typeName}}Repo.initialize();
|
|
9
|
+
const {{typeDef.idParam}} = event?.context?.params?.{{typeDef.idParam}};
|
|
10
|
+
if (!{{typeDef.idParam}}) throw notFound("{{typeName}}." + {{typeName}}Repo.typeDef.idFieldName());
|
|
11
|
+
{{/each}}
|
|
12
|
+
|
|
13
|
+
const refs: Record<string, MobilettoOrmObject> = {};{{#each parentPaths}}
|
|
14
|
+
const {{refField}}Local = await {{typeName}}Repo.safeFindById(caller, {{typeDef.idParam}}, FIND_NOREDACT){{#each typeDef.alternateLookupFields}} ||
|
|
15
|
+
await {{../typeName}}Repo.safeFindFirstBy(caller, "{{this}}", {{../typeDef.idParam}}, FIND_NOREDACT){{/each}};
|
|
16
|
+
if (!{{refField}}Local) throw notFound({{typeDef.idParam}});
|
|
17
|
+
refs.{{refField}} = {{refField}}Local;
|
|
18
|
+
{{/each}}
|
|
19
|
+
return refs;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const checkRefs = (obj: MobilettoOrmObject, expected: Record<string, MobilettoOrmObject>, event: H3Event): void => {{{curlyOpen}}}{{#each parentPaths}}
|
|
23
|
+
const {{typeName}}Repo = {{typeName}}Repository({{../eventContextVar}});
|
|
24
|
+
if (!obj.{{refField}} || obj.{{refField}} !== {{typeName}}Repo.typeDef.id(expected.{{refField}})) throw validationError({ {{refField}}: ["mismatch"] });{{/each}}
|
|
25
|
+
};
|
|
26
|
+
{{/if}}
|
|
@@ -12,6 +12,10 @@ export const validationError = (errors: MobilettoOrmValidationErrors) =>
|
|
|
12
12
|
data: JSON.stringify(errors),
|
|
13
13
|
});
|
|
14
14
|
|
|
15
|
+
export const unauthorized = () => {
|
|
16
|
+
return createError({ statusCode: 401 });
|
|
17
|
+
};
|
|
18
|
+
|
|
15
19
|
export const forbidden = () => {
|
|
16
20
|
return createError({ statusCode: 403 });
|
|
17
21
|
};
|
|
@@ -31,6 +35,12 @@ export const serverError = () =>
|
|
|
31
35
|
statusMessage: "server error",
|
|
32
36
|
});
|
|
33
37
|
|
|
38
|
+
export const notImplemented = () =>
|
|
39
|
+
createError({
|
|
40
|
+
statusCode: 501,
|
|
41
|
+
statusMessage: "not implemented",
|
|
42
|
+
});
|
|
43
|
+
|
|
34
44
|
export const conflict = () => createError({ statusCode: 409, statusMessage: "conflict" });
|
|
35
45
|
|
|
36
46
|
export const filterErrors = async (event: H3Event, logPrefix: string, fn: (event: H3Event) => Promise<unknown>) => {
|
|
@@ -1,56 +1,156 @@
|
|
|
1
|
-
/* eslint-disable import/no-duplicates */
|
|
2
1
|
{{#if disclaimer}}// {{{disclaimer}}}{{#if header}}
|
|
3
2
|
{{{header}}}{{/if}}{{/if}}
|
|
4
3
|
|
|
5
|
-
import { H3Event, deleteCookie } from "h3";
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
4
|
+
import { H3Event, deleteCookie, getRequestHeader } from "h3";
|
|
5
|
+
import { parse } from "cookie-es";
|
|
6
|
+
import { MobilettoApiAction } from "mobiletto-orm-typedef";
|
|
7
|
+
import { resolverCaller } from "mobiletto-orm";
|
|
8
|
+
{{#eq sessionHeaderImport sessionCookieNameImport typeDefPackage}}import {
|
|
9
|
+
{{../SESSION_HEADER}},
|
|
10
|
+
{{../SESSION_COOKIE_NAME}},
|
|
11
|
+
type {{../AccountType}},
|
|
12
|
+
type {{../SessionType}}
|
|
13
|
+
} from "{{../typeDefPackage}}";
|
|
14
|
+
{{else}}{{#eq sessionHeaderImport sessionCookieNameImport}}import { {{../SESSION_HEADER}}, {{../SESSION_COOKIE_NAME}} } from "{{../sessionHeaderImport}}";
|
|
15
|
+
import type { {{../AccountType}}, {{../SessionType}} } from "{{../typeDefPackage}}";
|
|
16
|
+
{{else}}import { {{../SESSION_HEADER}} } from "{{../sessionHeaderImport}}";
|
|
17
|
+
import { {{../SESSION_COOKIE_NAME}} } from "{{../sessionCookieNameImport}}";
|
|
18
|
+
import type { {{../AccountType}}, {{../SessionType}} } from "{{../typeDefPackage}}";
|
|
19
|
+
{{/eq}}{{/eq}}
|
|
9
20
|
import { {{cookieOptions}} } from "{{cookieOptionsImport}}";
|
|
10
21
|
import { forbidden } from "{{errorFilterImport}}";
|
|
11
22
|
import { {{sessionRepository}} } from "{{sessionRepositoryImport}}";
|
|
12
23
|
import { {{accountRepository}} } from "{{accountRepositoryImport}}";
|
|
13
24
|
|
|
25
|
+
const caller: {{AccountType}} = resolverCaller();
|
|
26
|
+
|
|
27
|
+
export const optionalAccount = async (
|
|
28
|
+
event: H3Event,
|
|
29
|
+
apiAction: string,
|
|
30
|
+
fn: (event: H3Event, apiAction: string, session: SessionType | null) => Promise<unknown>,
|
|
31
|
+
) => {
|
|
32
|
+
let token = getRequestHeader(event, {{SESSION_HEADER}});
|
|
33
|
+
if (!token) {
|
|
34
|
+
const cookieHeader = getRequestHeader(event, "cookie");
|
|
35
|
+
if (cookieHeader) {
|
|
36
|
+
const cookie = parse(cookieHeader);
|
|
37
|
+
token = cookie[{{SESSION_COOKIE_NAME}}];
|
|
38
|
+
} else {
|
|
39
|
+
return await fn(event, apiAction, null);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const session: SessionType | null = await sessionRepository({{eventContextVar}}).safeFindById(
|
|
43
|
+
caller,
|
|
44
|
+
token,
|
|
45
|
+
);
|
|
46
|
+
return await fn(event, apiAction, session);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export const optionalAccountObject = async (
|
|
50
|
+
event: H3Event,
|
|
51
|
+
apiAction: string,
|
|
52
|
+
fn: (event: H3Event, apiAction: string, session: SessionType | null, account: {{AccountType}} | null) => Promise<unknown>,
|
|
53
|
+
) => {
|
|
54
|
+
return await optionalAccount(event, apiAction, async (event, apiAction, session) => {
|
|
55
|
+
if (session == null || !session.account) return await fn(event, apiAction, null, null);
|
|
56
|
+
const account: {{AccountType}} | null = await {{accountRepository}}({{eventContextVar}}).safeFindById(
|
|
57
|
+
caller,
|
|
58
|
+
session.account,
|
|
59
|
+
);
|
|
60
|
+
if (!account) return await fn(event, apiAction, null, null);
|
|
61
|
+
return await fn(event, apiAction, session, account);
|
|
62
|
+
});
|
|
63
|
+
};
|
|
64
|
+
|
|
14
65
|
export const requireAccount = async (
|
|
15
66
|
event: H3Event,
|
|
16
|
-
|
|
17
|
-
fn: (event: H3Event, session: {{SessionType}}) => Promise<unknown>,
|
|
67
|
+
apiAction: string,
|
|
68
|
+
fn: (event: H3Event, apiAction: string, session: {{SessionType}}) => Promise<unknown>,
|
|
18
69
|
) => {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
70
|
+
let token = getRequestHeader(event, {{SESSION_HEADER}});
|
|
71
|
+
if (!token) {
|
|
72
|
+
const cookieHeader = getRequestHeader(event, "cookie");
|
|
73
|
+
if (cookieHeader) {
|
|
74
|
+
const cookie = parse(cookieHeader);
|
|
75
|
+
token = cookie[{{SESSION_COOKIE_NAME}}];
|
|
76
|
+
} else {
|
|
77
|
+
deleteCookie(event, {{SESSION_COOKIE_NAME}}, await cookieOptions(event));
|
|
78
|
+
throw forbidden("requireAccount: no token");
|
|
79
|
+
}
|
|
22
80
|
}
|
|
23
|
-
const
|
|
24
|
-
const session: {{SessionType}} | null = await {{sessionRepository}}().safeFindById(token);
|
|
81
|
+
const session: {{SessionType}} | null = await {{sessionRepository}}({{eventContextVar}}).safeFindById(caller, token);
|
|
25
82
|
if (!session) {
|
|
26
|
-
deleteCookie(event, {{SESSION_COOKIE_NAME}}, await {{cookieOptions}}());
|
|
27
|
-
throw forbidden();
|
|
83
|
+
deleteCookie(event, {{SESSION_COOKIE_NAME}}, await {{cookieOptions}}(event));
|
|
84
|
+
throw forbidden(`requireAccount: session not found for caller with token=${token}`);
|
|
28
85
|
}
|
|
29
|
-
return await fn(event, session);
|
|
86
|
+
return await fn(event, apiAction, session);
|
|
30
87
|
};
|
|
31
88
|
|
|
32
89
|
export const requireAccountObject = async (
|
|
33
90
|
event: H3Event,
|
|
34
|
-
|
|
35
|
-
fn: (event: H3Event, session: {{SessionType}}, account: {{AccountType}}) => Promise<unknown>,
|
|
91
|
+
apiAction: string,
|
|
92
|
+
fn: (event: H3Event, apiAction: string, session: {{SessionType}}, account: {{AccountType}}) => Promise<unknown>,
|
|
36
93
|
) => {
|
|
37
|
-
return await requireAccount(event,
|
|
38
|
-
|
|
94
|
+
return await requireAccount(event, apiAction, async (event, apiAction, session) => {
|
|
95
|
+
if (!session.account) throw forbidden(`apiAction=${apiAction} no session.account`);
|
|
96
|
+
const account: {{AccountType}} | null = await {{accountRepository}}({{eventContextVar}}).safeFindById(caller, session.account);
|
|
39
97
|
if (!account) throw forbidden();
|
|
40
|
-
return await fn(event, session, account);
|
|
98
|
+
return await fn(event, apiAction, session, account);
|
|
41
99
|
});
|
|
42
100
|
};
|
|
43
101
|
|
|
44
102
|
export const requireAdminAccountObject = async (
|
|
45
103
|
event: H3Event,
|
|
46
|
-
|
|
47
|
-
fn: (event: H3Event, session: {{SessionType}}, account: {{AccountType}}) => Promise<unknown>,
|
|
104
|
+
apiAction: string,
|
|
105
|
+
fn: (event: H3Event, apiAction: string, session: {{SessionType}}, account: {{AccountType}}) => Promise<unknown>,
|
|
48
106
|
) => {
|
|
49
|
-
return await requireAccountObject(event,
|
|
107
|
+
return await requireAccountObject(event, apiAction, async (event, apiAction: string, session: {{SessionType}}, account: {{AccountType}}) => {
|
|
50
108
|
if (account.admin === true) {
|
|
51
|
-
return await fn(event, session, account);
|
|
109
|
+
return await fn(event, apiAction, session, account);
|
|
52
110
|
} else {
|
|
53
|
-
throw forbidden();
|
|
111
|
+
throw forbidden(`requireAdminAccountObject: caller not admin`);
|
|
54
112
|
}
|
|
55
113
|
});
|
|
56
114
|
};
|
|
115
|
+
|
|
116
|
+
export type AnyFunction = (...args: unknown[]) => unknown;
|
|
117
|
+
export type RequireApiSessionType = {
|
|
118
|
+
func: null | AnyFunction;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
export const REQUIRE_API_SESSION: RequireApiSessionType = {
|
|
122
|
+
func: null,
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
export const requireDefaultAccess = async (
|
|
126
|
+
event: H3Event,
|
|
127
|
+
apiAction: string,
|
|
128
|
+
fn: (event: H3Event, apiAction: string) => Promise<unknown>,
|
|
129
|
+
) => {
|
|
130
|
+
if (REQUIRE_API_SESSION.func) {
|
|
131
|
+
return REQUIRE_API_SESSION.func(event, apiAction, fn);
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
export type ApiPermission = {
|
|
136
|
+
targetType: string;
|
|
137
|
+
action: MobilettoApiAction;
|
|
138
|
+
custom?: string;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
/*
|
|
142
|
+
* Permission strings come in two flavors:
|
|
143
|
+
* Two parts for standard actions: targetType.action, for example shopProduct.create or shopAsset.search
|
|
144
|
+
* Three parts for custom actions: targetType.custom.action, for example shopProduct.audit.action or shop.catalog_view:public.action
|
|
145
|
+
*/
|
|
146
|
+
export const parsePermission = (p: string): ApiPermission => {
|
|
147
|
+
if (!p || p.length === 0) throw new Error("parsePermission: null or empty argument");
|
|
148
|
+
const lastDot = p.lastIndexOf(".");
|
|
149
|
+
if (lastDot <= 0 || lastDot === p.length - 1) throw new Error(`parsePermission: invalid argument: ${p}`);
|
|
150
|
+
const action = p.substring(lastDot + 1) as MobilettoApiAction;
|
|
151
|
+
const firstDot = p.indexOf(".");
|
|
152
|
+
const targetType = p.substring(0, firstDot);
|
|
153
|
+
return firstDot === lastDot
|
|
154
|
+
? { targetType, action }
|
|
155
|
+
: { targetType, action, custom: p.substring(firstDot + targetType.length + 1, lastDot) };
|
|
156
|
+
};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "http://json-schema.org/draft-07/schema#",{{#if disclaimer}}
|
|
3
|
+
"$comment": "{{{ disclaimer }}}",{{/if}}
|
|
4
|
+
"type": "object",
|
|
5
|
+
"title": "{{ Name }}",
|
|
6
|
+
"description": "JSON Schema for {{ Name }}{{#if typeDef.description}} - {{typeDef.description}}{{/if}}",
|
|
7
|
+
"allOf": [
|
|
8
|
+
{
|
|
9
|
+
"$ref": "#/$defs/MobilettoOrmObject"
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
"type": "object",
|
|
13
|
+
"properties": {
|
|
14
|
+
{{#each fields}} "{{name}}": {{#if typeIsString}}{
|
|
15
|
+
"type": "string"{{#if min}},
|
|
16
|
+
"minLength": {{min}}{{/if}}{{#if max}},
|
|
17
|
+
"maxLength": {{max}}{{/if}}{{#if isEnum}},
|
|
18
|
+
"enum": {{{enumValues}}}{{/if}}{{#if defaultValue}},
|
|
19
|
+
"default": {{{defaultValue}}}{{/if}}
|
|
20
|
+
}{{else}}{{#if typeIsNumber}}{
|
|
21
|
+
"type": "number"{{#if minValue}},
|
|
22
|
+
"minimum": {{minValue}}{{/if}}{{#if maxValue}},
|
|
23
|
+
"maximum": {{maxValue}}{{/if}}{{#if defaultValue}},
|
|
24
|
+
"default": {{{defaultValue}}}{{/if}}
|
|
25
|
+
}{{else}}{{#if typeIsBoolean}}{
|
|
26
|
+
"type": "boolean"{{#if defaultValue}},
|
|
27
|
+
"default": {{{defaultValue}}}{{/if}}
|
|
28
|
+
}{{else}}{{#if typeIsArray}}{
|
|
29
|
+
"type": "array",
|
|
30
|
+
"items": { "type": "{{valueType}}" }
|
|
31
|
+
}{{else}}{
|
|
32
|
+
"type": "string",
|
|
33
|
+
"description": "Unsupported type: {{type}}"
|
|
34
|
+
}{{/if}}{{/if}}{{/if}}{{/if}}{{#unless @last}},{{/unless}}
|
|
35
|
+
{{/each}}
|
|
36
|
+
},
|
|
37
|
+
"required": [{{#each requiredFields}}"{{this}}"{{#unless @last}}, {{/unless}}{{/each}}]
|
|
38
|
+
}
|
|
39
|
+
],
|
|
40
|
+
"$defs": {
|
|
41
|
+
"MobilettoOrmObject": {
|
|
42
|
+
"type": "object",
|
|
43
|
+
"properties": {
|
|
44
|
+
"id": {
|
|
45
|
+
"type": "string",
|
|
46
|
+
"description": "Unique identifier"
|
|
47
|
+
},
|
|
48
|
+
"ctime": {
|
|
49
|
+
"type": "integer",
|
|
50
|
+
"description": "Creation timestamp",
|
|
51
|
+
"exclusiveMinimum": 0
|
|
52
|
+
},
|
|
53
|
+
"mtime": {
|
|
54
|
+
"type": "integer",
|
|
55
|
+
"description": "Modification timestamp",
|
|
56
|
+
"exclusiveMinimum": 0
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
"required": ["id"]
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|