mobiletto-orm-typedef-gen 2.3.5 → 2.3.8
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/lib/cjs/generate.d.ts +1 -0
- package/lib/cjs/generate.js +1 -0
- package/lib/cjs/generators/client.js +1 -3
- package/lib/cjs/generators/server.d.ts +5 -3
- package/lib/cjs/generators/server.js +60 -11
- package/lib/cjs/templates/api/id.patch.ts.hbs +42 -35
- package/lib/cjs/templates/api/id.put.ts.hbs +40 -33
- package/lib/cjs/templates/api/index.post.ts.hbs +37 -39
- package/lib/cjs/templates/filter/errorFilter.ts.hbs +51 -0
- package/lib/cjs/templates/filter/sessionFilter.ts.hbs +55 -0
- package/lib/esm/generate.d.ts +1 -0
- package/lib/esm/generate.js +1 -0
- package/lib/esm/generators/client.js +1 -3
- package/lib/esm/generators/server.d.ts +5 -3
- package/lib/esm/generators/server.js +60 -11
- package/lib/esm/templates/api/id.patch.ts.hbs +42 -35
- package/lib/esm/templates/api/id.put.ts.hbs +40 -33
- package/lib/esm/templates/api/index.post.ts.hbs +37 -39
- package/lib/esm/templates/filter/errorFilter.ts.hbs +51 -0
- package/lib/esm/templates/filter/sessionFilter.ts.hbs +55 -0
- package/package.json +1 -1
package/lib/cjs/generate.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { MobilettoOrmTypeDef, MobilettoOrmTypeDefConfig } from "mobiletto-orm-typedef";
|
|
2
|
+
export declare const DUMMY_TYPEDEF: MobilettoOrmTypeDef;
|
|
2
3
|
export declare const STANDARD_AUTOGEN_FILE_DISCLAIMER = "DO NOT EDIT THIS FILE. AUTO-GENERATED BY mobiletto-orm-typedef-gen";
|
|
3
4
|
export type TypeDeclaration = {
|
|
4
5
|
name: string;
|
package/lib/cjs/generate.js
CHANGED
|
@@ -4,6 +4,7 @@ import Handlebars from "handlebars";
|
|
|
4
4
|
import { dirname } from "path";
|
|
5
5
|
import { fileURLToPath } from "url";
|
|
6
6
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
7
|
+
export const DUMMY_TYPEDEF = new MobilettoOrmTypeDef({ typeName: "dummy", fields: {} });
|
|
7
8
|
export const STANDARD_AUTOGEN_FILE_DISCLAIMER = "DO NOT EDIT THIS FILE. AUTO-GENERATED BY mobiletto-orm-typedef-gen";
|
|
8
9
|
const findDecls = (name, fields) => {
|
|
9
10
|
return _findDecls(name, fields, []);
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { generate } from "../generate.js";
|
|
3
|
-
const DUMMY_TYPEDEF = new MobilettoOrmTypeDef({ typeName: "dummy", fields: {} });
|
|
1
|
+
import { DUMMY_TYPEDEF, generate } from "../generate.js";
|
|
4
2
|
export const generateService = (typeDef, typeDefPackage, opts, utilsPath) => {
|
|
5
3
|
utilsPath || (utilsPath = `~/utils/model`);
|
|
6
4
|
opts || (opts = {});
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { MobilettoOrmTypeDef, MobilettoOrmTypeDefConfig } from "mobiletto-orm-typedef";
|
|
2
2
|
import { GenerateOptions } from "../generate.js";
|
|
3
|
-
export declare const
|
|
3
|
+
export declare const genApiEndpoint: (apiTemplate: string, typeDef: MobilettoOrmTypeDef | MobilettoOrmTypeDefConfig, typeDefPackage: string, opts?: GenerateOptions, ctx?: Record<string, string>) => string;
|
|
4
4
|
export declare const generateApiIdGet: (typeDef: MobilettoOrmTypeDef | MobilettoOrmTypeDefConfig, typeDefPackage: string, opts?: GenerateOptions) => string;
|
|
5
5
|
export declare const generateApiIdPut: (typeDef: MobilettoOrmTypeDef | MobilettoOrmTypeDefConfig, typeDefPackage: string, opts?: GenerateOptions) => string;
|
|
6
6
|
export declare const generateApiIdPatch: (typeDef: MobilettoOrmTypeDef | MobilettoOrmTypeDefConfig, typeDefPackage: string, opts?: GenerateOptions) => string;
|
|
7
7
|
export declare const generateApiIdDelete: (typeDef: MobilettoOrmTypeDef | MobilettoOrmTypeDefConfig, typeDefPackage: string, opts?: GenerateOptions) => string;
|
|
8
|
-
export declare const generateApiIndexPost: (typeDef: MobilettoOrmTypeDef | MobilettoOrmTypeDefConfig, typeDefPackage: string, opts?: GenerateOptions) => string;
|
|
9
|
-
export declare const generateApi: (typeDef: MobilettoOrmTypeDef | MobilettoOrmTypeDefConfig, typeDefPackage: string, opts?: GenerateOptions) => Record<string, string>;
|
|
8
|
+
export declare const generateApiIndexPost: (typeDef: MobilettoOrmTypeDef | MobilettoOrmTypeDefConfig, typeDefPackage: string, opts?: GenerateOptions, utilsImportPath?: string) => string;
|
|
9
|
+
export declare const generateApi: (typeDef: MobilettoOrmTypeDef | MobilettoOrmTypeDefConfig, typeDefPackage: string, opts?: GenerateOptions, utilsImportPath?: string) => Record<string, string>;
|
|
10
|
+
export declare const generateErrorFilter: (typeDefPackage: string, opts?: GenerateOptions, logger?: string, loggerImport?: string) => string;
|
|
11
|
+
export declare const generateSessionFilter: (typeDefPackage: string, opts?: GenerateOptions, AccountType?: string, SessionType?: string, SESSION_HEADER?: string, sessionHeaderImport?: string, SESSION_COOKIE_NAME?: string, sessionCookieNameImport?: string, cookieOptions?: string, cookieOptionsImport?: string, errorFilterImport?: string, sessionRepository?: string, sessionRepositoryImport?: string, accountRepository?: string, accountRepositoryImport?: string) => string;
|
|
@@ -1,29 +1,30 @@
|
|
|
1
|
-
import { generate } from "../generate.js";
|
|
1
|
+
import { DUMMY_TYPEDEF, generate } from "../generate.js";
|
|
2
2
|
import * as fs from "fs";
|
|
3
|
-
export const
|
|
3
|
+
export const genApiEndpoint = (apiTemplate, typeDef, typeDefPackage, opts, ctx) => {
|
|
4
4
|
opts || (opts = {});
|
|
5
5
|
opts.prepareContext = (typeDef, ctx) => {
|
|
6
6
|
return ctx;
|
|
7
7
|
};
|
|
8
|
-
|
|
8
|
+
const cx = ctx ? Object.assign({}, ctx, { typeDefPackage }) : { typeDefPackage };
|
|
9
|
+
return generate(typeDef, `templates/api/${apiTemplate}.ts.hbs`, opts, cx);
|
|
9
10
|
};
|
|
10
11
|
export const generateApiIdGet = (typeDef, typeDefPackage, opts) => {
|
|
11
|
-
return
|
|
12
|
+
return genApiEndpoint("id.get", typeDef, typeDefPackage, opts);
|
|
12
13
|
};
|
|
13
14
|
export const generateApiIdPut = (typeDef, typeDefPackage, opts) => {
|
|
14
|
-
return
|
|
15
|
+
return genApiEndpoint("id.put", typeDef, typeDefPackage, opts);
|
|
15
16
|
};
|
|
16
17
|
export const generateApiIdPatch = (typeDef, typeDefPackage, opts) => {
|
|
17
|
-
return
|
|
18
|
+
return genApiEndpoint("id.patch", typeDef, typeDefPackage, opts);
|
|
18
19
|
};
|
|
19
20
|
export const generateApiIdDelete = (typeDef, typeDefPackage, opts) => {
|
|
20
|
-
return
|
|
21
|
+
return genApiEndpoint("id.delete", typeDef, typeDefPackage, opts);
|
|
21
22
|
};
|
|
22
|
-
export const generateApiIndexPost = (typeDef, typeDefPackage, opts) => {
|
|
23
|
-
return
|
|
23
|
+
export const generateApiIndexPost = (typeDef, typeDefPackage, opts, utilsImportPath) => {
|
|
24
|
+
return genApiEndpoint("index.post", typeDef, typeDefPackage, opts, utilsImportPath ? { utilsImportPath } : undefined);
|
|
24
25
|
};
|
|
25
26
|
const apiOutfile = (opts, tsFile) => opts.outfile ? Object.assign({}, opts, { outfile: `${opts.outfile}/${tsFile}` }) : opts;
|
|
26
|
-
export const generateApi = (typeDef, typeDefPackage, opts) => {
|
|
27
|
+
export const generateApi = (typeDef, typeDefPackage, opts, utilsImportPath) => {
|
|
27
28
|
opts || (opts = {});
|
|
28
29
|
if (opts.outfile) {
|
|
29
30
|
const stat = fs.statSync(opts.outfile, { throwIfNoEntry: false });
|
|
@@ -36,6 +37,54 @@ export const generateApi = (typeDef, typeDefPackage, opts) => {
|
|
|
36
37
|
create: generateApiIdPut(typeDef, typeDefPackage, apiOutfile(opts, "[id].put.ts")),
|
|
37
38
|
update: generateApiIdPatch(typeDef, typeDefPackage, apiOutfile(opts, "[id].patch.ts")),
|
|
38
39
|
delete: generateApiIdDelete(typeDef, typeDefPackage, apiOutfile(opts, "[id].delete.ts")),
|
|
39
|
-
search: generateApiIndexPost(typeDef, typeDefPackage, apiOutfile(opts, "index.post.ts")),
|
|
40
|
+
search: generateApiIndexPost(typeDef, typeDefPackage, apiOutfile(opts, "index.post.ts"), utilsImportPath),
|
|
40
41
|
};
|
|
41
42
|
};
|
|
43
|
+
export const generateErrorFilter = (typeDefPackage, opts, logger, loggerImport) => {
|
|
44
|
+
logger || (logger = "logger");
|
|
45
|
+
loggerImport || (loggerImport = "~/server/utils/logger");
|
|
46
|
+
opts || (opts = {});
|
|
47
|
+
opts.prepareContext = (typeDef, ctx) => {
|
|
48
|
+
return ctx;
|
|
49
|
+
};
|
|
50
|
+
return generate(DUMMY_TYPEDEF, `templates/filter/errorFilter.ts.hbs`, opts, {
|
|
51
|
+
typeDefPackage,
|
|
52
|
+
logger,
|
|
53
|
+
loggerImport,
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
export const generateSessionFilter = (typeDefPackage, opts, AccountType, SessionType, SESSION_HEADER, sessionHeaderImport, SESSION_COOKIE_NAME, sessionCookieNameImport, cookieOptions, cookieOptionsImport, errorFilterImport, sessionRepository, sessionRepositoryImport, accountRepository, accountRepositoryImport) => {
|
|
57
|
+
AccountType || (AccountType = "AccountType");
|
|
58
|
+
SessionType || (SessionType = "SessionType");
|
|
59
|
+
SESSION_HEADER || (SESSION_HEADER = "SESSION_HEADER");
|
|
60
|
+
sessionHeaderImport || (sessionHeaderImport = "~/utils/auth");
|
|
61
|
+
SESSION_COOKIE_NAME || (SESSION_COOKIE_NAME = "SESSION_COOKIE_NAME");
|
|
62
|
+
sessionCookieNameImport || (sessionCookieNameImport = "~/utils/auth");
|
|
63
|
+
cookieOptions || (cookieOptions = "cookieOptions");
|
|
64
|
+
cookieOptionsImport || (cookieOptionsImport = "~/server/utils/cookie");
|
|
65
|
+
errorFilterImport || (errorFilterImport = "~/server/utils/filter/errorFilter");
|
|
66
|
+
sessionRepository || (sessionRepository = "sessionRepository");
|
|
67
|
+
sessionRepositoryImport || (sessionRepositoryImport = "~/server/utils/repo/sessionRepo");
|
|
68
|
+
accountRepository || (accountRepository = "accountRepository");
|
|
69
|
+
accountRepositoryImport || (accountRepositoryImport = "~/server/utils/repo/accountRepo");
|
|
70
|
+
opts || (opts = {});
|
|
71
|
+
opts.prepareContext = (typeDef, ctx) => {
|
|
72
|
+
return ctx;
|
|
73
|
+
};
|
|
74
|
+
return generate(DUMMY_TYPEDEF, `templates/filter/sessionFilter.ts.hbs`, opts, {
|
|
75
|
+
typeDefPackage,
|
|
76
|
+
AccountType,
|
|
77
|
+
SessionType,
|
|
78
|
+
SESSION_HEADER,
|
|
79
|
+
sessionHeaderImport,
|
|
80
|
+
SESSION_COOKIE_NAME,
|
|
81
|
+
sessionCookieNameImport,
|
|
82
|
+
cookieOptions,
|
|
83
|
+
cookieOptionsImport,
|
|
84
|
+
errorFilterImport,
|
|
85
|
+
sessionRepository,
|
|
86
|
+
sessionRepositoryImport,
|
|
87
|
+
accountRepository,
|
|
88
|
+
accountRepositoryImport,
|
|
89
|
+
});
|
|
90
|
+
};
|
|
@@ -1,37 +1,44 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
-
{{#if disclaimer}}//
|
|
3
|
-
|
|
4
|
-
{{{header}}}{{/if}}{{/if}}
|
|
2
|
+
{{#if disclaimer}}// {{{disclaimer}}}{{#if header}}
|
|
3
|
+
{{{header}}}{{/if}}{{/if}}
|
|
5
4
|
|
|
6
|
-
import { H3Event } from "h3";
|
|
7
|
-
{
|
|
8
|
-
{{Name}}TypeDef } from "{{typeDefPackage}}";
|
|
9
|
-
|
|
10
|
-
{
|
|
11
|
-
|
|
12
|
-
{{{curlyOpen}}}{{else
|
|
13
|
-
return await requireAccountObject(event, "{{name}}.update", async (event: H3Event, session, account) =>
|
|
14
|
-
{{{curlyOpen}}}{{
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
{{
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
{{
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
5
|
+
import { H3Event } from "h3";
|
|
6
|
+
import { MobilettoOrmValidationErrors } from "mobiletto-orm-typedef";
|
|
7
|
+
import { {{Name}}Type, {{Name}}TypeDef } from "{{typeDefPackage}}";
|
|
8
|
+
|
|
9
|
+
export default defineEventHandler(async (event: H3Event) => {
|
|
10
|
+
return await filterErrors(event, "{{name}}.update", async (event: H3Event) => {{{curlyOpen}}}{{#if typeDef.apiConfig.update.permission.admin}}
|
|
11
|
+
return await requireAdminAccountObject(event, "{{name}}.update", async (event: H3Event, session, account) => {{{curlyOpen}}}{{else}}{{#if typeDef.apiConfig.update.permission.owner}}
|
|
12
|
+
return await requireAccountObject(event, "{{name}}.update", async (event: H3Event, session, account) => {{{curlyOpen}}}{{else}}{{#if typeDef.apiConfig.update.permission.session}}
|
|
13
|
+
return await requireAccount(event, "{{name}}.update", async (event: H3Event, session) => {{{curlyOpen}}}{{/if}}{{/if}}{{/if}}
|
|
14
|
+
const {{name}}Repo = {{name}}Repository();
|
|
15
|
+
const id = event?.context?.params?.id;
|
|
16
|
+
if (!id) throw notFound({{name}}Repo.typeDef.idFieldName());
|
|
17
|
+
const obj: {{Name}}Type = await readBody(event);
|
|
18
|
+
if (!obj) {
|
|
19
|
+
throw badRequest();
|
|
20
|
+
}
|
|
21
|
+
if ({{Name}}TypeDef.id(obj) !== id) {
|
|
22
|
+
const errs: MobilettoOrmValidationErrors = {};
|
|
23
|
+
errs[{{name}}Repo.typeDef.idFieldName()] = ["mismatch"];
|
|
24
|
+
throw validationError(errs);
|
|
25
|
+
} else {
|
|
26
|
+
{{#if typeDef.apiConfig.update.permission.owner}}
|
|
27
|
+
if (!account.admin && obj.account && obj.account !== session.account) throw forbidden();
|
|
28
|
+
{{/if}}{{#if typeDef.apiConfig.update.hasValidate}}if (typeof {{Name}}TypeDef.apiConfig.update?.validate === "function") {
|
|
29
|
+
const validated = await {{Name}}TypeDef.apiConfig.update.validate(account, obj);
|
|
30
|
+
if (validated === true || (typeof validated === "object" && Object.keys(validated).length === 0)) {
|
|
31
|
+
// successfully validated
|
|
32
|
+
} else if (typeof validated === "object" && Object.keys(validated).length > 0) {
|
|
33
|
+
throw validationError(validated);
|
|
34
|
+
} else {
|
|
35
|
+
throw validationError({global: ["validation"]});
|
|
36
|
+
}
|
|
37
|
+
}{{/if}}
|
|
38
|
+
return await {{name}}Repo.update(obj);
|
|
39
|
+
}{{#if typeDef.apiConfig.update.permission.admin}}
|
|
40
|
+
});{{else}}{{#if typeDef.apiConfig.update.permission.owner}}
|
|
41
|
+
});{{else}}{{#if typeDef.apiConfig.update.permission.session}}
|
|
42
|
+
});{{/if}}{{/if}}{{/if}}
|
|
43
|
+
});
|
|
44
|
+
});
|
|
@@ -1,35 +1,42 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
-
{{#if disclaimer}}//
|
|
3
|
-
|
|
4
|
-
{{{header}}}{{/if}}{{/if}}
|
|
2
|
+
{{#if disclaimer}}// {{{disclaimer}}}{{#if header}}
|
|
3
|
+
{{{header}}}{{/if}}{{/if}}
|
|
5
4
|
|
|
6
|
-
import { H3Event } from "h3";
|
|
7
|
-
{
|
|
8
|
-
{{Name}}TypeDef } from "{{typeDefPackage}}";
|
|
9
|
-
|
|
10
|
-
{
|
|
11
|
-
|
|
12
|
-
{{{curlyOpen}}}{{else
|
|
13
|
-
return await requireAccountObject(event, "{{name}}.create", async (event: H3Event, session, account) =>
|
|
14
|
-
{{{curlyOpen}}}{{
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
{{
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
{{
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
5
|
+
import { H3Event } from "h3";
|
|
6
|
+
import { MobilettoOrmValidationErrors } from "mobiletto-orm-typedef";
|
|
7
|
+
import { {{Name}}Type, {{Name}}TypeDef } from "{{typeDefPackage}}";
|
|
8
|
+
|
|
9
|
+
export default defineEventHandler(async (event: H3Event) => {
|
|
10
|
+
return await filterErrors(event, "{{name}}.create", async (event: H3Event) => {{{curlyOpen}}}{{#if typeDef.apiConfig.create.permission.admin}}
|
|
11
|
+
return await requireAdminAccountObject(event, "{{name}}.create", async (event: H3Event, session, account) => {{{curlyOpen}}}{{else}}{{#if typeDef.apiConfig.create.permission.owner}}
|
|
12
|
+
return await requireAccountObject(event, "{{name}}.create", async (event: H3Event, session, account) => {{{curlyOpen}}}{{else}}{{#if typeDef.apiConfig.create.permission.session}}
|
|
13
|
+
return await requireAccount(event, "{{name}}.create", async (event: H3Event, session) => {{{curlyOpen}}}{{/if}}{{/if}}{{/if}}
|
|
14
|
+
const {{name}}Repo = {{name}}Repository();
|
|
15
|
+
const id = event?.context?.params?.id;
|
|
16
|
+
if (!id) throw notFound({{name}}Repo.typeDef.idFieldName());
|
|
17
|
+
const obj: {{Name}}Type = await readBody(event);
|
|
18
|
+
if (!obj) {
|
|
19
|
+
throw badRequest();
|
|
20
|
+
}
|
|
21
|
+
if ({{Name}}TypeDef.id(obj) !== id) {
|
|
22
|
+
const errs: MobilettoOrmValidationErrors = {};
|
|
23
|
+
errs[{{name}}Repo.typeDef.idFieldName()] = ["mismatch"];
|
|
24
|
+
throw validationError(errs);
|
|
25
|
+
} else {
|
|
26
|
+
{{#if typeDef.apiConfig.create.hasValidate}}if (typeof {{Name}}TypeDef.apiConfig.create?.validate === "function") {
|
|
27
|
+
const validated = await {{Name}}TypeDef.apiConfig.create.validate(account, obj);
|
|
28
|
+
if (validated === true || (typeof validated === "object" && Object.keys(validated).length === 0)) {
|
|
29
|
+
// successfully validated
|
|
30
|
+
} else if (typeof validated === "object" && Object.keys(validated).length > 0) {
|
|
31
|
+
throw validationError(validated);
|
|
32
|
+
} else {
|
|
33
|
+
throw validationError({global: ["validation"]});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
{{/if}}return await {{name}}Repo.create(obj);
|
|
37
|
+
}{{#if typeDef.apiConfig.create.permission.admin}}
|
|
38
|
+
});{{else}}{{#if typeDef.apiConfig.create.permission.owner}}
|
|
39
|
+
});{{else}}{{#if typeDef.apiConfig.create.permission.session}}
|
|
40
|
+
});{{/if}}{{/if}}{{/if}}
|
|
41
|
+
});
|
|
42
|
+
});
|
|
@@ -1,41 +1,39 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
-
{{#if disclaimer}}//
|
|
3
|
-
|
|
4
|
-
{{{header}}}{{/if}}{{/if}}
|
|
2
|
+
{{#if disclaimer}}// {{{disclaimer}}}{{#if header}}
|
|
3
|
+
{{{header}}}{{/if}}{{/if}}
|
|
5
4
|
|
|
6
|
-
import { H3Event } from "h3";
|
|
7
|
-
{{Name}}TypeDef } from "{{typeDefPackage}}";
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
{
|
|
11
|
-
|
|
12
|
-
{{{curlyOpen}}}{{else
|
|
13
|
-
return await requireAccountObject(event, "{{name}}.search", async (event: H3Event, session, account) =>
|
|
14
|
-
{{{curlyOpen}}}{{
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
const
|
|
28
|
-
{{
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
{{
|
|
37
|
-
|
|
38
|
-
});{{
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}); });
|
|
5
|
+
import { H3Event } from "h3";
|
|
6
|
+
import { {{Name}}TypeDef } from "{{typeDefPackage}}";
|
|
7
|
+
import { MobilettoOrmFindApiOpts } from "{{utilsImportPath}}/storeHelper.ts";
|
|
8
|
+
|
|
9
|
+
export default defineEventHandler(async (event: H3Event) => {
|
|
10
|
+
return await filterErrors(event, "{{name}}.search", async (event: H3Event) => {{{curlyOpen}}}{{#if typeDef.apiConfig.search.permission.admin}}
|
|
11
|
+
return await requireAdminAccountObject(event, "{{name}}.search", async (event: H3Event, session, account) => {{{curlyOpen}}}{{else}}{{#if typeDef.apiConfig.search.permission.owner}}
|
|
12
|
+
return await requireAccountObject(event, "{{name}}.search", async (event: H3Event, session, account) => {{{curlyOpen}}}{{else}}{{#if typeDef.apiConfig.search.permission.session}}
|
|
13
|
+
return await requireAccount(event, "{{name}}.search", async (event: H3Event, session) => {{{curlyOpen}}}{{/if}}{{/if}}{{/if}}
|
|
14
|
+
const opts: MobilettoOrmFindApiOpts = {{#if typeDef.singleton}}{}; // {{name}} is a singleton, options make no sense{{else}}(await readBody(event)) || {};{{#if typeDef.apiConfig.search.hasValidate}}{{/if}}
|
|
15
|
+
if (typeof {{Name}}TypeDef.apiConfig.search?.validate === "function") {
|
|
16
|
+
const validated = await {{Name}}TypeDef.apiConfig.search.validate(account, null, opts);
|
|
17
|
+
if (validated === true || (typeof validated === "object" && Object.keys(validated).length === 0)) {
|
|
18
|
+
// successfully validated
|
|
19
|
+
} else if (typeof validated === "object" && Object.keys(validated).length > 0) {
|
|
20
|
+
throw validationError(validated);
|
|
21
|
+
} else {
|
|
22
|
+
throw validationError({global: ["validation"]});
|
|
23
|
+
}
|
|
24
|
+
}{{/if}}
|
|
25
|
+
const {{name}}Repo = {{name}}Repository();{{#if typeDef.singleton}}
|
|
26
|
+
const singleton = await {{name}}Repo.safeFindById("{{typeDef.singleton}}");
|
|
27
|
+
return singleton ? [ singleton ] : [];{{else}}
|
|
28
|
+
if (opts.field && opts.value) {
|
|
29
|
+
return await {{name}}Repo.safeFindBy(opts.field, opts.value, opts.opts || {});
|
|
30
|
+
} else if (opts.textSearch && {{Name}}TypeDef.textSearchFields.length > 0) {
|
|
31
|
+
return await {{name}}Repo.find({ predicate: (obj) => {{Name}}TypeDef.textMatch(obj, opts.textSearch || "") });
|
|
32
|
+
} else {
|
|
33
|
+
return await {{name}}Repo.find(opts.opts || {});
|
|
34
|
+
}{{/if}}{{#if typeDef.apiConfig.search.permission.admin}}
|
|
35
|
+
});{{else}}{{#if typeDef.apiConfig.search.permission.owner}}
|
|
36
|
+
});{{else}}{{#if typeDef.apiConfig.search.permission.session}}
|
|
37
|
+
});{{/if}}{{/if}}{{/if}}
|
|
38
|
+
});
|
|
39
|
+
});
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{{#if disclaimer}}// {{{disclaimer}}}{{#if header}}
|
|
2
|
+
{{{header}}}{{/if}}{{/if}}
|
|
3
|
+
|
|
4
|
+
import { MobilettoOrmNotFoundError, MobilettoOrmValidationError, MobilettoOrmValidationErrors } from "mobiletto-orm";
|
|
5
|
+
import { H3Error, H3Event, createError } from "h3";
|
|
6
|
+
import { {{logger}} } from "{{loggerImport}}";
|
|
7
|
+
|
|
8
|
+
export const validationError = (errors: MobilettoOrmValidationErrors) =>
|
|
9
|
+
createError({
|
|
10
|
+
statusCode: 422,
|
|
11
|
+
statusMessage: "validation error",
|
|
12
|
+
data: JSON.stringify(errors),
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
export const forbidden = () => {
|
|
16
|
+
return createError({ statusCode: 403 });
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const badRequest = () => createError({ statusCode: 400, statusMessage: "bad request" });
|
|
20
|
+
|
|
21
|
+
export const notFound = (id: string) =>
|
|
22
|
+
createError({
|
|
23
|
+
statusCode: 404,
|
|
24
|
+
statusMessage: "not found",
|
|
25
|
+
data: id,
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
export const serverError = () =>
|
|
29
|
+
createError({
|
|
30
|
+
statusCode: 500,
|
|
31
|
+
statusMessage: "server error",
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
export const conflict = () => createError({ statusCode: 409, statusMessage: "conflict" });
|
|
35
|
+
|
|
36
|
+
export const filterErrors = async (event: H3Event, logPrefix: string, fn: (event: H3Event) => Promise<unknown>) => {
|
|
37
|
+
try {
|
|
38
|
+
return await fn(event);
|
|
39
|
+
} catch (e) {
|
|
40
|
+
if (e instanceof H3Error) {
|
|
41
|
+
throw e;
|
|
42
|
+
} else if (e instanceof MobilettoOrmValidationError) {
|
|
43
|
+
throw validationError(e.errors);
|
|
44
|
+
} else if (e instanceof MobilettoOrmNotFoundError) {
|
|
45
|
+
throw notFound("id");
|
|
46
|
+
} else {
|
|
47
|
+
{{logger}}.error(`${logPrefix}: server error: ${JSON.stringify(e)}`);
|
|
48
|
+
throw serverError();
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{{#if disclaimer}}// {{{disclaimer}}}{{#if header}}
|
|
2
|
+
{{{header}}}{{/if}}{{/if}}
|
|
3
|
+
|
|
4
|
+
import { H3Event, deleteCookie } from "h3";
|
|
5
|
+
import { {{AccountType}}, {{SessionType}} } from "{{typeDefPackage}}";
|
|
6
|
+
import { {{SESSION_HEADER}} } from "{{sessionHeaderImport}}";
|
|
7
|
+
import { {{SESSION_COOKIE_NAME}} } from "{{sessionCookieNameImport}}";
|
|
8
|
+
import { {{cookieOptions}} } from "{{cookieOptionsImport}}";
|
|
9
|
+
import { forbidden } from "{{errorFilterImport}}";
|
|
10
|
+
import { {{sessionRepository}} } from "{{sessionRepositoryImport}}";
|
|
11
|
+
import { {{accountRepository}} } from "{{accountRepositoryImport}}";
|
|
12
|
+
|
|
13
|
+
export const requireAccount = async (
|
|
14
|
+
event: H3Event,
|
|
15
|
+
_logPrefix: string,
|
|
16
|
+
fn: (event: H3Event, session: {{SessionType}}) => Promise<unknown>,
|
|
17
|
+
) => {
|
|
18
|
+
if (!event?.node?.req?.headers[{{SESSION_HEADER}}]) {
|
|
19
|
+
deleteCookie(event, {{SESSION_COOKIE_NAME}}, await {{cookieOptions}}());
|
|
20
|
+
throw forbidden();
|
|
21
|
+
}
|
|
22
|
+
const token = event?.node?.req?.headers[{{SESSION_HEADER}}];
|
|
23
|
+
const session: {{SessionType}} | null = await {{sessionRepository}}().safeFindById(token);
|
|
24
|
+
if (!session) {
|
|
25
|
+
deleteCookie(event, {{SESSION_COOKIE_NAME}}, await {{cookieOptions}}());
|
|
26
|
+
throw forbidden();
|
|
27
|
+
}
|
|
28
|
+
return await fn(event, session);
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export const requireAccountObject = async (
|
|
32
|
+
event: H3Event,
|
|
33
|
+
logPrefix: string,
|
|
34
|
+
fn: (event: H3Event, session: {{SessionType}}, account: {{AccountType}}) => Promise<unknown>,
|
|
35
|
+
) => {
|
|
36
|
+
return await requireAccount(event, logPrefix, async (event, session) => {
|
|
37
|
+
const account: {{AccountType}} | null = await {{accountRepository}}().safeFindById(session.account);
|
|
38
|
+
if (!account) throw forbidden();
|
|
39
|
+
return await fn(event, session, account);
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const requireAdminAccountObject = async (
|
|
44
|
+
event: H3Event,
|
|
45
|
+
logPrefix: string,
|
|
46
|
+
fn: (event: H3Event, session: {{SessionType}}, account: {{AccountType}}) => Promise<unknown>,
|
|
47
|
+
) => {
|
|
48
|
+
return await requireAccountObject(event, logPrefix, async (event, session: {{SessionType}}, account: {{AccountType}}) => {
|
|
49
|
+
if (account.admin === true) {
|
|
50
|
+
return await fn(event, session, account);
|
|
51
|
+
} else {
|
|
52
|
+
throw forbidden();
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
};
|
package/lib/esm/generate.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { MobilettoOrmTypeDef, MobilettoOrmTypeDefConfig } from "mobiletto-orm-typedef";
|
|
2
|
+
export declare const DUMMY_TYPEDEF: MobilettoOrmTypeDef;
|
|
2
3
|
export declare const STANDARD_AUTOGEN_FILE_DISCLAIMER = "DO NOT EDIT THIS FILE. AUTO-GENERATED BY mobiletto-orm-typedef-gen";
|
|
3
4
|
export type TypeDeclaration = {
|
|
4
5
|
name: string;
|
package/lib/esm/generate.js
CHANGED
|
@@ -4,6 +4,7 @@ import Handlebars from "handlebars";
|
|
|
4
4
|
import { dirname } from "path";
|
|
5
5
|
import { fileURLToPath } from "url";
|
|
6
6
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
7
|
+
export const DUMMY_TYPEDEF = new MobilettoOrmTypeDef({ typeName: "dummy", fields: {} });
|
|
7
8
|
export const STANDARD_AUTOGEN_FILE_DISCLAIMER = "DO NOT EDIT THIS FILE. AUTO-GENERATED BY mobiletto-orm-typedef-gen";
|
|
8
9
|
const findDecls = (name, fields) => {
|
|
9
10
|
return _findDecls(name, fields, []);
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { generate } from "../generate.js";
|
|
3
|
-
const DUMMY_TYPEDEF = new MobilettoOrmTypeDef({ typeName: "dummy", fields: {} });
|
|
1
|
+
import { DUMMY_TYPEDEF, generate } from "../generate.js";
|
|
4
2
|
export const generateService = (typeDef, typeDefPackage, opts, utilsPath) => {
|
|
5
3
|
utilsPath || (utilsPath = `~/utils/model`);
|
|
6
4
|
opts || (opts = {});
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { MobilettoOrmTypeDef, MobilettoOrmTypeDefConfig } from "mobiletto-orm-typedef";
|
|
2
2
|
import { GenerateOptions } from "../generate.js";
|
|
3
|
-
export declare const
|
|
3
|
+
export declare const genApiEndpoint: (apiTemplate: string, typeDef: MobilettoOrmTypeDef | MobilettoOrmTypeDefConfig, typeDefPackage: string, opts?: GenerateOptions, ctx?: Record<string, string>) => string;
|
|
4
4
|
export declare const generateApiIdGet: (typeDef: MobilettoOrmTypeDef | MobilettoOrmTypeDefConfig, typeDefPackage: string, opts?: GenerateOptions) => string;
|
|
5
5
|
export declare const generateApiIdPut: (typeDef: MobilettoOrmTypeDef | MobilettoOrmTypeDefConfig, typeDefPackage: string, opts?: GenerateOptions) => string;
|
|
6
6
|
export declare const generateApiIdPatch: (typeDef: MobilettoOrmTypeDef | MobilettoOrmTypeDefConfig, typeDefPackage: string, opts?: GenerateOptions) => string;
|
|
7
7
|
export declare const generateApiIdDelete: (typeDef: MobilettoOrmTypeDef | MobilettoOrmTypeDefConfig, typeDefPackage: string, opts?: GenerateOptions) => string;
|
|
8
|
-
export declare const generateApiIndexPost: (typeDef: MobilettoOrmTypeDef | MobilettoOrmTypeDefConfig, typeDefPackage: string, opts?: GenerateOptions) => string;
|
|
9
|
-
export declare const generateApi: (typeDef: MobilettoOrmTypeDef | MobilettoOrmTypeDefConfig, typeDefPackage: string, opts?: GenerateOptions) => Record<string, string>;
|
|
8
|
+
export declare const generateApiIndexPost: (typeDef: MobilettoOrmTypeDef | MobilettoOrmTypeDefConfig, typeDefPackage: string, opts?: GenerateOptions, utilsImportPath?: string) => string;
|
|
9
|
+
export declare const generateApi: (typeDef: MobilettoOrmTypeDef | MobilettoOrmTypeDefConfig, typeDefPackage: string, opts?: GenerateOptions, utilsImportPath?: string) => Record<string, string>;
|
|
10
|
+
export declare const generateErrorFilter: (typeDefPackage: string, opts?: GenerateOptions, logger?: string, loggerImport?: string) => string;
|
|
11
|
+
export declare const generateSessionFilter: (typeDefPackage: string, opts?: GenerateOptions, AccountType?: string, SessionType?: string, SESSION_HEADER?: string, sessionHeaderImport?: string, SESSION_COOKIE_NAME?: string, sessionCookieNameImport?: string, cookieOptions?: string, cookieOptionsImport?: string, errorFilterImport?: string, sessionRepository?: string, sessionRepositoryImport?: string, accountRepository?: string, accountRepositoryImport?: string) => string;
|
|
@@ -1,29 +1,30 @@
|
|
|
1
|
-
import { generate } from "../generate.js";
|
|
1
|
+
import { DUMMY_TYPEDEF, generate } from "../generate.js";
|
|
2
2
|
import * as fs from "fs";
|
|
3
|
-
export const
|
|
3
|
+
export const genApiEndpoint = (apiTemplate, typeDef, typeDefPackage, opts, ctx) => {
|
|
4
4
|
opts || (opts = {});
|
|
5
5
|
opts.prepareContext = (typeDef, ctx) => {
|
|
6
6
|
return ctx;
|
|
7
7
|
};
|
|
8
|
-
|
|
8
|
+
const cx = ctx ? Object.assign({}, ctx, { typeDefPackage }) : { typeDefPackage };
|
|
9
|
+
return generate(typeDef, `templates/api/${apiTemplate}.ts.hbs`, opts, cx);
|
|
9
10
|
};
|
|
10
11
|
export const generateApiIdGet = (typeDef, typeDefPackage, opts) => {
|
|
11
|
-
return
|
|
12
|
+
return genApiEndpoint("id.get", typeDef, typeDefPackage, opts);
|
|
12
13
|
};
|
|
13
14
|
export const generateApiIdPut = (typeDef, typeDefPackage, opts) => {
|
|
14
|
-
return
|
|
15
|
+
return genApiEndpoint("id.put", typeDef, typeDefPackage, opts);
|
|
15
16
|
};
|
|
16
17
|
export const generateApiIdPatch = (typeDef, typeDefPackage, opts) => {
|
|
17
|
-
return
|
|
18
|
+
return genApiEndpoint("id.patch", typeDef, typeDefPackage, opts);
|
|
18
19
|
};
|
|
19
20
|
export const generateApiIdDelete = (typeDef, typeDefPackage, opts) => {
|
|
20
|
-
return
|
|
21
|
+
return genApiEndpoint("id.delete", typeDef, typeDefPackage, opts);
|
|
21
22
|
};
|
|
22
|
-
export const generateApiIndexPost = (typeDef, typeDefPackage, opts) => {
|
|
23
|
-
return
|
|
23
|
+
export const generateApiIndexPost = (typeDef, typeDefPackage, opts, utilsImportPath) => {
|
|
24
|
+
return genApiEndpoint("index.post", typeDef, typeDefPackage, opts, utilsImportPath ? { utilsImportPath } : undefined);
|
|
24
25
|
};
|
|
25
26
|
const apiOutfile = (opts, tsFile) => opts.outfile ? Object.assign({}, opts, { outfile: `${opts.outfile}/${tsFile}` }) : opts;
|
|
26
|
-
export const generateApi = (typeDef, typeDefPackage, opts) => {
|
|
27
|
+
export const generateApi = (typeDef, typeDefPackage, opts, utilsImportPath) => {
|
|
27
28
|
opts || (opts = {});
|
|
28
29
|
if (opts.outfile) {
|
|
29
30
|
const stat = fs.statSync(opts.outfile, { throwIfNoEntry: false });
|
|
@@ -36,6 +37,54 @@ export const generateApi = (typeDef, typeDefPackage, opts) => {
|
|
|
36
37
|
create: generateApiIdPut(typeDef, typeDefPackage, apiOutfile(opts, "[id].put.ts")),
|
|
37
38
|
update: generateApiIdPatch(typeDef, typeDefPackage, apiOutfile(opts, "[id].patch.ts")),
|
|
38
39
|
delete: generateApiIdDelete(typeDef, typeDefPackage, apiOutfile(opts, "[id].delete.ts")),
|
|
39
|
-
search: generateApiIndexPost(typeDef, typeDefPackage, apiOutfile(opts, "index.post.ts")),
|
|
40
|
+
search: generateApiIndexPost(typeDef, typeDefPackage, apiOutfile(opts, "index.post.ts"), utilsImportPath),
|
|
40
41
|
};
|
|
41
42
|
};
|
|
43
|
+
export const generateErrorFilter = (typeDefPackage, opts, logger, loggerImport) => {
|
|
44
|
+
logger || (logger = "logger");
|
|
45
|
+
loggerImport || (loggerImport = "~/server/utils/logger");
|
|
46
|
+
opts || (opts = {});
|
|
47
|
+
opts.prepareContext = (typeDef, ctx) => {
|
|
48
|
+
return ctx;
|
|
49
|
+
};
|
|
50
|
+
return generate(DUMMY_TYPEDEF, `templates/filter/errorFilter.ts.hbs`, opts, {
|
|
51
|
+
typeDefPackage,
|
|
52
|
+
logger,
|
|
53
|
+
loggerImport,
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
export const generateSessionFilter = (typeDefPackage, opts, AccountType, SessionType, SESSION_HEADER, sessionHeaderImport, SESSION_COOKIE_NAME, sessionCookieNameImport, cookieOptions, cookieOptionsImport, errorFilterImport, sessionRepository, sessionRepositoryImport, accountRepository, accountRepositoryImport) => {
|
|
57
|
+
AccountType || (AccountType = "AccountType");
|
|
58
|
+
SessionType || (SessionType = "SessionType");
|
|
59
|
+
SESSION_HEADER || (SESSION_HEADER = "SESSION_HEADER");
|
|
60
|
+
sessionHeaderImport || (sessionHeaderImport = "~/utils/auth");
|
|
61
|
+
SESSION_COOKIE_NAME || (SESSION_COOKIE_NAME = "SESSION_COOKIE_NAME");
|
|
62
|
+
sessionCookieNameImport || (sessionCookieNameImport = "~/utils/auth");
|
|
63
|
+
cookieOptions || (cookieOptions = "cookieOptions");
|
|
64
|
+
cookieOptionsImport || (cookieOptionsImport = "~/server/utils/cookie");
|
|
65
|
+
errorFilterImport || (errorFilterImport = "~/server/utils/filter/errorFilter");
|
|
66
|
+
sessionRepository || (sessionRepository = "sessionRepository");
|
|
67
|
+
sessionRepositoryImport || (sessionRepositoryImport = "~/server/utils/repo/sessionRepo");
|
|
68
|
+
accountRepository || (accountRepository = "accountRepository");
|
|
69
|
+
accountRepositoryImport || (accountRepositoryImport = "~/server/utils/repo/accountRepo");
|
|
70
|
+
opts || (opts = {});
|
|
71
|
+
opts.prepareContext = (typeDef, ctx) => {
|
|
72
|
+
return ctx;
|
|
73
|
+
};
|
|
74
|
+
return generate(DUMMY_TYPEDEF, `templates/filter/sessionFilter.ts.hbs`, opts, {
|
|
75
|
+
typeDefPackage,
|
|
76
|
+
AccountType,
|
|
77
|
+
SessionType,
|
|
78
|
+
SESSION_HEADER,
|
|
79
|
+
sessionHeaderImport,
|
|
80
|
+
SESSION_COOKIE_NAME,
|
|
81
|
+
sessionCookieNameImport,
|
|
82
|
+
cookieOptions,
|
|
83
|
+
cookieOptionsImport,
|
|
84
|
+
errorFilterImport,
|
|
85
|
+
sessionRepository,
|
|
86
|
+
sessionRepositoryImport,
|
|
87
|
+
accountRepository,
|
|
88
|
+
accountRepositoryImport,
|
|
89
|
+
});
|
|
90
|
+
};
|
|
@@ -1,37 +1,44 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
-
{{#if disclaimer}}//
|
|
3
|
-
|
|
4
|
-
{{{header}}}{{/if}}{{/if}}
|
|
2
|
+
{{#if disclaimer}}// {{{disclaimer}}}{{#if header}}
|
|
3
|
+
{{{header}}}{{/if}}{{/if}}
|
|
5
4
|
|
|
6
|
-
import { H3Event } from "h3";
|
|
7
|
-
{
|
|
8
|
-
{{Name}}TypeDef } from "{{typeDefPackage}}";
|
|
9
|
-
|
|
10
|
-
{
|
|
11
|
-
|
|
12
|
-
{{{curlyOpen}}}{{else
|
|
13
|
-
return await requireAccountObject(event, "{{name}}.update", async (event: H3Event, session, account) =>
|
|
14
|
-
{{{curlyOpen}}}{{
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
{{
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
{{
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
5
|
+
import { H3Event } from "h3";
|
|
6
|
+
import { MobilettoOrmValidationErrors } from "mobiletto-orm-typedef";
|
|
7
|
+
import { {{Name}}Type, {{Name}}TypeDef } from "{{typeDefPackage}}";
|
|
8
|
+
|
|
9
|
+
export default defineEventHandler(async (event: H3Event) => {
|
|
10
|
+
return await filterErrors(event, "{{name}}.update", async (event: H3Event) => {{{curlyOpen}}}{{#if typeDef.apiConfig.update.permission.admin}}
|
|
11
|
+
return await requireAdminAccountObject(event, "{{name}}.update", async (event: H3Event, session, account) => {{{curlyOpen}}}{{else}}{{#if typeDef.apiConfig.update.permission.owner}}
|
|
12
|
+
return await requireAccountObject(event, "{{name}}.update", async (event: H3Event, session, account) => {{{curlyOpen}}}{{else}}{{#if typeDef.apiConfig.update.permission.session}}
|
|
13
|
+
return await requireAccount(event, "{{name}}.update", async (event: H3Event, session) => {{{curlyOpen}}}{{/if}}{{/if}}{{/if}}
|
|
14
|
+
const {{name}}Repo = {{name}}Repository();
|
|
15
|
+
const id = event?.context?.params?.id;
|
|
16
|
+
if (!id) throw notFound({{name}}Repo.typeDef.idFieldName());
|
|
17
|
+
const obj: {{Name}}Type = await readBody(event);
|
|
18
|
+
if (!obj) {
|
|
19
|
+
throw badRequest();
|
|
20
|
+
}
|
|
21
|
+
if ({{Name}}TypeDef.id(obj) !== id) {
|
|
22
|
+
const errs: MobilettoOrmValidationErrors = {};
|
|
23
|
+
errs[{{name}}Repo.typeDef.idFieldName()] = ["mismatch"];
|
|
24
|
+
throw validationError(errs);
|
|
25
|
+
} else {
|
|
26
|
+
{{#if typeDef.apiConfig.update.permission.owner}}
|
|
27
|
+
if (!account.admin && obj.account && obj.account !== session.account) throw forbidden();
|
|
28
|
+
{{/if}}{{#if typeDef.apiConfig.update.hasValidate}}if (typeof {{Name}}TypeDef.apiConfig.update?.validate === "function") {
|
|
29
|
+
const validated = await {{Name}}TypeDef.apiConfig.update.validate(account, obj);
|
|
30
|
+
if (validated === true || (typeof validated === "object" && Object.keys(validated).length === 0)) {
|
|
31
|
+
// successfully validated
|
|
32
|
+
} else if (typeof validated === "object" && Object.keys(validated).length > 0) {
|
|
33
|
+
throw validationError(validated);
|
|
34
|
+
} else {
|
|
35
|
+
throw validationError({global: ["validation"]});
|
|
36
|
+
}
|
|
37
|
+
}{{/if}}
|
|
38
|
+
return await {{name}}Repo.update(obj);
|
|
39
|
+
}{{#if typeDef.apiConfig.update.permission.admin}}
|
|
40
|
+
});{{else}}{{#if typeDef.apiConfig.update.permission.owner}}
|
|
41
|
+
});{{else}}{{#if typeDef.apiConfig.update.permission.session}}
|
|
42
|
+
});{{/if}}{{/if}}{{/if}}
|
|
43
|
+
});
|
|
44
|
+
});
|
|
@@ -1,35 +1,42 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
-
{{#if disclaimer}}//
|
|
3
|
-
|
|
4
|
-
{{{header}}}{{/if}}{{/if}}
|
|
2
|
+
{{#if disclaimer}}// {{{disclaimer}}}{{#if header}}
|
|
3
|
+
{{{header}}}{{/if}}{{/if}}
|
|
5
4
|
|
|
6
|
-
import { H3Event } from "h3";
|
|
7
|
-
{
|
|
8
|
-
{{Name}}TypeDef } from "{{typeDefPackage}}";
|
|
9
|
-
|
|
10
|
-
{
|
|
11
|
-
|
|
12
|
-
{{{curlyOpen}}}{{else
|
|
13
|
-
return await requireAccountObject(event, "{{name}}.create", async (event: H3Event, session, account) =>
|
|
14
|
-
{{{curlyOpen}}}{{
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
{{
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
{{
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
5
|
+
import { H3Event } from "h3";
|
|
6
|
+
import { MobilettoOrmValidationErrors } from "mobiletto-orm-typedef";
|
|
7
|
+
import { {{Name}}Type, {{Name}}TypeDef } from "{{typeDefPackage}}";
|
|
8
|
+
|
|
9
|
+
export default defineEventHandler(async (event: H3Event) => {
|
|
10
|
+
return await filterErrors(event, "{{name}}.create", async (event: H3Event) => {{{curlyOpen}}}{{#if typeDef.apiConfig.create.permission.admin}}
|
|
11
|
+
return await requireAdminAccountObject(event, "{{name}}.create", async (event: H3Event, session, account) => {{{curlyOpen}}}{{else}}{{#if typeDef.apiConfig.create.permission.owner}}
|
|
12
|
+
return await requireAccountObject(event, "{{name}}.create", async (event: H3Event, session, account) => {{{curlyOpen}}}{{else}}{{#if typeDef.apiConfig.create.permission.session}}
|
|
13
|
+
return await requireAccount(event, "{{name}}.create", async (event: H3Event, session) => {{{curlyOpen}}}{{/if}}{{/if}}{{/if}}
|
|
14
|
+
const {{name}}Repo = {{name}}Repository();
|
|
15
|
+
const id = event?.context?.params?.id;
|
|
16
|
+
if (!id) throw notFound({{name}}Repo.typeDef.idFieldName());
|
|
17
|
+
const obj: {{Name}}Type = await readBody(event);
|
|
18
|
+
if (!obj) {
|
|
19
|
+
throw badRequest();
|
|
20
|
+
}
|
|
21
|
+
if ({{Name}}TypeDef.id(obj) !== id) {
|
|
22
|
+
const errs: MobilettoOrmValidationErrors = {};
|
|
23
|
+
errs[{{name}}Repo.typeDef.idFieldName()] = ["mismatch"];
|
|
24
|
+
throw validationError(errs);
|
|
25
|
+
} else {
|
|
26
|
+
{{#if typeDef.apiConfig.create.hasValidate}}if (typeof {{Name}}TypeDef.apiConfig.create?.validate === "function") {
|
|
27
|
+
const validated = await {{Name}}TypeDef.apiConfig.create.validate(account, obj);
|
|
28
|
+
if (validated === true || (typeof validated === "object" && Object.keys(validated).length === 0)) {
|
|
29
|
+
// successfully validated
|
|
30
|
+
} else if (typeof validated === "object" && Object.keys(validated).length > 0) {
|
|
31
|
+
throw validationError(validated);
|
|
32
|
+
} else {
|
|
33
|
+
throw validationError({global: ["validation"]});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
{{/if}}return await {{name}}Repo.create(obj);
|
|
37
|
+
}{{#if typeDef.apiConfig.create.permission.admin}}
|
|
38
|
+
});{{else}}{{#if typeDef.apiConfig.create.permission.owner}}
|
|
39
|
+
});{{else}}{{#if typeDef.apiConfig.create.permission.session}}
|
|
40
|
+
});{{/if}}{{/if}}{{/if}}
|
|
41
|
+
});
|
|
42
|
+
});
|
|
@@ -1,41 +1,39 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
-
{{#if disclaimer}}//
|
|
3
|
-
|
|
4
|
-
{{{header}}}{{/if}}{{/if}}
|
|
2
|
+
{{#if disclaimer}}// {{{disclaimer}}}{{#if header}}
|
|
3
|
+
{{{header}}}{{/if}}{{/if}}
|
|
5
4
|
|
|
6
|
-
import { H3Event } from "h3";
|
|
7
|
-
{{Name}}TypeDef } from "{{typeDefPackage}}";
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
{
|
|
11
|
-
|
|
12
|
-
{{{curlyOpen}}}{{else
|
|
13
|
-
return await requireAccountObject(event, "{{name}}.search", async (event: H3Event, session, account) =>
|
|
14
|
-
{{{curlyOpen}}}{{
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
const
|
|
28
|
-
{{
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
{{
|
|
37
|
-
|
|
38
|
-
});{{
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}); });
|
|
5
|
+
import { H3Event } from "h3";
|
|
6
|
+
import { {{Name}}TypeDef } from "{{typeDefPackage}}";
|
|
7
|
+
import { MobilettoOrmFindApiOpts } from "{{utilsImportPath}}/storeHelper.ts";
|
|
8
|
+
|
|
9
|
+
export default defineEventHandler(async (event: H3Event) => {
|
|
10
|
+
return await filterErrors(event, "{{name}}.search", async (event: H3Event) => {{{curlyOpen}}}{{#if typeDef.apiConfig.search.permission.admin}}
|
|
11
|
+
return await requireAdminAccountObject(event, "{{name}}.search", async (event: H3Event, session, account) => {{{curlyOpen}}}{{else}}{{#if typeDef.apiConfig.search.permission.owner}}
|
|
12
|
+
return await requireAccountObject(event, "{{name}}.search", async (event: H3Event, session, account) => {{{curlyOpen}}}{{else}}{{#if typeDef.apiConfig.search.permission.session}}
|
|
13
|
+
return await requireAccount(event, "{{name}}.search", async (event: H3Event, session) => {{{curlyOpen}}}{{/if}}{{/if}}{{/if}}
|
|
14
|
+
const opts: MobilettoOrmFindApiOpts = {{#if typeDef.singleton}}{}; // {{name}} is a singleton, options make no sense{{else}}(await readBody(event)) || {};{{#if typeDef.apiConfig.search.hasValidate}}{{/if}}
|
|
15
|
+
if (typeof {{Name}}TypeDef.apiConfig.search?.validate === "function") {
|
|
16
|
+
const validated = await {{Name}}TypeDef.apiConfig.search.validate(account, null, opts);
|
|
17
|
+
if (validated === true || (typeof validated === "object" && Object.keys(validated).length === 0)) {
|
|
18
|
+
// successfully validated
|
|
19
|
+
} else if (typeof validated === "object" && Object.keys(validated).length > 0) {
|
|
20
|
+
throw validationError(validated);
|
|
21
|
+
} else {
|
|
22
|
+
throw validationError({global: ["validation"]});
|
|
23
|
+
}
|
|
24
|
+
}{{/if}}
|
|
25
|
+
const {{name}}Repo = {{name}}Repository();{{#if typeDef.singleton}}
|
|
26
|
+
const singleton = await {{name}}Repo.safeFindById("{{typeDef.singleton}}");
|
|
27
|
+
return singleton ? [ singleton ] : [];{{else}}
|
|
28
|
+
if (opts.field && opts.value) {
|
|
29
|
+
return await {{name}}Repo.safeFindBy(opts.field, opts.value, opts.opts || {});
|
|
30
|
+
} else if (opts.textSearch && {{Name}}TypeDef.textSearchFields.length > 0) {
|
|
31
|
+
return await {{name}}Repo.find({ predicate: (obj) => {{Name}}TypeDef.textMatch(obj, opts.textSearch || "") });
|
|
32
|
+
} else {
|
|
33
|
+
return await {{name}}Repo.find(opts.opts || {});
|
|
34
|
+
}{{/if}}{{#if typeDef.apiConfig.search.permission.admin}}
|
|
35
|
+
});{{else}}{{#if typeDef.apiConfig.search.permission.owner}}
|
|
36
|
+
});{{else}}{{#if typeDef.apiConfig.search.permission.session}}
|
|
37
|
+
});{{/if}}{{/if}}{{/if}}
|
|
38
|
+
});
|
|
39
|
+
});
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{{#if disclaimer}}// {{{disclaimer}}}{{#if header}}
|
|
2
|
+
{{{header}}}{{/if}}{{/if}}
|
|
3
|
+
|
|
4
|
+
import { MobilettoOrmNotFoundError, MobilettoOrmValidationError, MobilettoOrmValidationErrors } from "mobiletto-orm";
|
|
5
|
+
import { H3Error, H3Event, createError } from "h3";
|
|
6
|
+
import { {{logger}} } from "{{loggerImport}}";
|
|
7
|
+
|
|
8
|
+
export const validationError = (errors: MobilettoOrmValidationErrors) =>
|
|
9
|
+
createError({
|
|
10
|
+
statusCode: 422,
|
|
11
|
+
statusMessage: "validation error",
|
|
12
|
+
data: JSON.stringify(errors),
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
export const forbidden = () => {
|
|
16
|
+
return createError({ statusCode: 403 });
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const badRequest = () => createError({ statusCode: 400, statusMessage: "bad request" });
|
|
20
|
+
|
|
21
|
+
export const notFound = (id: string) =>
|
|
22
|
+
createError({
|
|
23
|
+
statusCode: 404,
|
|
24
|
+
statusMessage: "not found",
|
|
25
|
+
data: id,
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
export const serverError = () =>
|
|
29
|
+
createError({
|
|
30
|
+
statusCode: 500,
|
|
31
|
+
statusMessage: "server error",
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
export const conflict = () => createError({ statusCode: 409, statusMessage: "conflict" });
|
|
35
|
+
|
|
36
|
+
export const filterErrors = async (event: H3Event, logPrefix: string, fn: (event: H3Event) => Promise<unknown>) => {
|
|
37
|
+
try {
|
|
38
|
+
return await fn(event);
|
|
39
|
+
} catch (e) {
|
|
40
|
+
if (e instanceof H3Error) {
|
|
41
|
+
throw e;
|
|
42
|
+
} else if (e instanceof MobilettoOrmValidationError) {
|
|
43
|
+
throw validationError(e.errors);
|
|
44
|
+
} else if (e instanceof MobilettoOrmNotFoundError) {
|
|
45
|
+
throw notFound("id");
|
|
46
|
+
} else {
|
|
47
|
+
{{logger}}.error(`${logPrefix}: server error: ${JSON.stringify(e)}`);
|
|
48
|
+
throw serverError();
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{{#if disclaimer}}// {{{disclaimer}}}{{#if header}}
|
|
2
|
+
{{{header}}}{{/if}}{{/if}}
|
|
3
|
+
|
|
4
|
+
import { H3Event, deleteCookie } from "h3";
|
|
5
|
+
import { {{AccountType}}, {{SessionType}} } from "{{typeDefPackage}}";
|
|
6
|
+
import { {{SESSION_HEADER}} } from "{{sessionHeaderImport}}";
|
|
7
|
+
import { {{SESSION_COOKIE_NAME}} } from "{{sessionCookieNameImport}}";
|
|
8
|
+
import { {{cookieOptions}} } from "{{cookieOptionsImport}}";
|
|
9
|
+
import { forbidden } from "{{errorFilterImport}}";
|
|
10
|
+
import { {{sessionRepository}} } from "{{sessionRepositoryImport}}";
|
|
11
|
+
import { {{accountRepository}} } from "{{accountRepositoryImport}}";
|
|
12
|
+
|
|
13
|
+
export const requireAccount = async (
|
|
14
|
+
event: H3Event,
|
|
15
|
+
_logPrefix: string,
|
|
16
|
+
fn: (event: H3Event, session: {{SessionType}}) => Promise<unknown>,
|
|
17
|
+
) => {
|
|
18
|
+
if (!event?.node?.req?.headers[{{SESSION_HEADER}}]) {
|
|
19
|
+
deleteCookie(event, {{SESSION_COOKIE_NAME}}, await {{cookieOptions}}());
|
|
20
|
+
throw forbidden();
|
|
21
|
+
}
|
|
22
|
+
const token = event?.node?.req?.headers[{{SESSION_HEADER}}];
|
|
23
|
+
const session: {{SessionType}} | null = await {{sessionRepository}}().safeFindById(token);
|
|
24
|
+
if (!session) {
|
|
25
|
+
deleteCookie(event, {{SESSION_COOKIE_NAME}}, await {{cookieOptions}}());
|
|
26
|
+
throw forbidden();
|
|
27
|
+
}
|
|
28
|
+
return await fn(event, session);
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export const requireAccountObject = async (
|
|
32
|
+
event: H3Event,
|
|
33
|
+
logPrefix: string,
|
|
34
|
+
fn: (event: H3Event, session: {{SessionType}}, account: {{AccountType}}) => Promise<unknown>,
|
|
35
|
+
) => {
|
|
36
|
+
return await requireAccount(event, logPrefix, async (event, session) => {
|
|
37
|
+
const account: {{AccountType}} | null = await {{accountRepository}}().safeFindById(session.account);
|
|
38
|
+
if (!account) throw forbidden();
|
|
39
|
+
return await fn(event, session, account);
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const requireAdminAccountObject = async (
|
|
44
|
+
event: H3Event,
|
|
45
|
+
logPrefix: string,
|
|
46
|
+
fn: (event: H3Event, session: {{SessionType}}, account: {{AccountType}}) => Promise<unknown>,
|
|
47
|
+
) => {
|
|
48
|
+
return await requireAccountObject(event, logPrefix, async (event, session: {{SessionType}}, account: {{AccountType}}) => {
|
|
49
|
+
if (account.admin === true) {
|
|
50
|
+
return await fn(event, session, account);
|
|
51
|
+
} else {
|
|
52
|
+
throw forbidden();
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
};
|