mobiletto-orm-typedef-gen 2.2.22 → 2.3.1

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.
Files changed (60) hide show
  1. package/README.md +11 -1
  2. package/lib/cjs/generate.d.ts +2 -1
  3. package/lib/cjs/generate.js +5 -0
  4. package/lib/cjs/generators/client.d.ts +8 -0
  5. package/lib/cjs/generators/client.js +73 -0
  6. package/lib/cjs/generators/server.d.ts +9 -0
  7. package/lib/cjs/generators/server.js +33 -0
  8. package/lib/cjs/generators/tsType.js +1 -1
  9. package/lib/cjs/generators/yup.js +1 -1
  10. package/lib/cjs/index.d.ts +2 -0
  11. package/lib/cjs/index.js +2 -0
  12. package/lib/cjs/templates/api/id.delete.ts.hbs +66 -0
  13. package/lib/cjs/templates/api/id.get.ts.hbs +37 -0
  14. package/lib/cjs/templates/api/id.patch.ts.hbs +37 -0
  15. package/lib/cjs/templates/api/id.put.ts.hbs +35 -0
  16. package/lib/cjs/templates/api/index.post.ts.hbs +41 -0
  17. package/lib/cjs/templates/component/admin.vue.hbs +405 -0
  18. package/lib/cjs/templates/component/adminHelper.ts.hbs +65 -0
  19. package/lib/cjs/templates/component/componentHelper.d.ts +3 -0
  20. package/lib/cjs/templates/component/componentHelper.js +53 -0
  21. package/lib/cjs/templates/service/service.ts.hbs +59 -0
  22. package/lib/cjs/templates/service/serviceHelper.ts.hbs +97 -0
  23. package/lib/cjs/templates/service/singletonService.ts.hbs +50 -0
  24. package/lib/cjs/templates/store/singletonStore.ts.hbs +73 -0
  25. package/lib/cjs/templates/store/store.ts.hbs +94 -0
  26. package/lib/cjs/templates/store/storeHelper.d.ts +15 -0
  27. package/lib/cjs/templates/store/storeHelper.js +15 -0
  28. package/lib/cjs/templates/store/storeHelper.ts.hbs +34 -0
  29. package/lib/esm/generate.d.ts +2 -1
  30. package/lib/esm/generate.js +5 -0
  31. package/lib/esm/generators/client.d.ts +8 -0
  32. package/lib/esm/generators/client.js +73 -0
  33. package/lib/esm/generators/server.d.ts +9 -0
  34. package/lib/esm/generators/server.js +33 -0
  35. package/lib/esm/generators/tsType.js +1 -1
  36. package/lib/esm/generators/yup.js +1 -1
  37. package/lib/esm/index.d.ts +2 -0
  38. package/lib/esm/index.js +2 -0
  39. package/lib/esm/templates/api/id.delete.ts.hbs +66 -0
  40. package/lib/esm/templates/api/id.get.ts.hbs +37 -0
  41. package/lib/esm/templates/api/id.patch.ts.hbs +37 -0
  42. package/lib/esm/templates/api/id.put.ts.hbs +35 -0
  43. package/lib/esm/templates/api/index.post.ts.hbs +41 -0
  44. package/lib/esm/templates/component/admin.vue.hbs +405 -0
  45. package/lib/esm/templates/component/adminHelper.ts.hbs +65 -0
  46. package/lib/esm/templates/component/componentHelper.d.ts +3 -0
  47. package/lib/esm/templates/component/componentHelper.js +53 -0
  48. package/lib/esm/templates/service/service.ts.hbs +59 -0
  49. package/lib/esm/templates/service/serviceHelper.ts.hbs +97 -0
  50. package/lib/esm/templates/service/singletonService.ts.hbs +50 -0
  51. package/lib/esm/templates/store/singletonStore.ts.hbs +73 -0
  52. package/lib/esm/templates/store/store.ts.hbs +94 -0
  53. package/lib/esm/templates/store/storeHelper.d.ts +15 -0
  54. package/lib/esm/templates/store/storeHelper.js +15 -0
  55. package/lib/esm/templates/store/storeHelper.ts.hbs +34 -0
  56. package/package.json +4 -3
  57. /package/lib/cjs/templates/{tsType.ts.hbs → type/tsType.ts.hbs} +0 -0
  58. /package/lib/cjs/templates/{yup.ts.hbs → validation/yup.ts.hbs} +0 -0
  59. /package/lib/esm/templates/{tsType.ts.hbs → type/tsType.ts.hbs} +0 -0
  60. /package/lib/esm/templates/{yup.ts.hbs → validation/yup.ts.hbs} +0 -0
@@ -0,0 +1,97 @@
1
+ {{#if disclaimer}}// {{{ disclaimer }}}{{#if header}}
2
+ {{{header}}}{{/if}}{{/if}}
3
+
4
+ import { MobilettoOrmValidationErrors } from "mobiletto-orm";
5
+ import { {{sessionHeader}} } from "{{sessionHeaderImport}}";
6
+ import { {{sessionCookie}} } from "{{sessionCookieImport}}";
7
+
8
+ export function authHeader() {
9
+ const session = {{sessionCookie}}();
10
+ if (session) {
11
+ const headers: Record<string, string> = {};
12
+ headers[{{sessionHeader}}] = session.value;
13
+ return headers;
14
+ } else {
15
+ return {};
16
+ }
17
+ }
18
+
19
+ export function authHead(headers = null) {
20
+ return authReq("HEAD", headers);
21
+ }
22
+ export function authGet(headers = null) {
23
+ return authReq("GET", headers);
24
+ }
25
+ export function authDelete(headers = null) {
26
+ return authReq("DELETE", headers);
27
+ }
28
+
29
+ export type OptionalHeaders = Record<string, string> | null;
30
+
31
+ export function authReq(method: string, headers: OptionalHeaders = null) {
32
+ return {
33
+ method,
34
+ headers: headers ? Object.assign({}, headers, authHeader()) : authHeader(),
35
+ };
36
+ }
37
+
38
+ export function authPostJson<T>(obj: T, headers: OptionalHeaders = null) {
39
+ return authDataJson<T>(obj, "POST", headers);
40
+ }
41
+ export function authPutJson<T>(obj: T, headers: OptionalHeaders = null) {
42
+ return authDataJson<T>(obj, "PUT", headers);
43
+ }
44
+ export function authPatchJson<T>(obj: T, headers: OptionalHeaders = null) {
45
+ return authDataJson<T>(obj, "PATCH", headers);
46
+ }
47
+
48
+ export function authDataJson<T>(obj: T, method: string, headers: OptionalHeaders = null) {
49
+ return {
50
+ method,
51
+ headers: headers
52
+ ? Object.assign({}, headers, authHeader(), {
53
+ "Content-Type": "application/json",
54
+ })
55
+ : authHeader(),
56
+ body: JSON.stringify(obj),
57
+ };
58
+ }
59
+
60
+ export function handleJsonResponse<T>(response: Response): Promise<T> {
61
+ if (response && typeof response.text === "function") {
62
+ return response.text().then((text) => {
63
+ let data;
64
+ try {
65
+ data = typeof text === "string" ? JSON.parse(text) : null;
66
+ } catch (e) {
67
+ // console.log(`handleJsonResponse: error parsing: ${text}`);
68
+ data = null;
69
+ }
70
+ if (!response.ok) {
71
+ const error = data || text || response.statusText;
72
+ return Promise.reject(error);
73
+ }
74
+ // console.log(`handleJsonResponse returning: ${JSON.stringify(data, null, 2)}`)
75
+ return data;
76
+ });
77
+ } else {
78
+ return Promise.resolve(response as T);
79
+ }
80
+ }
81
+
82
+ export const handleErrors = (serverErrors?: Ref<MobilettoOrmValidationErrors>) => (e) => {
83
+ if (!e.statusCode || !serverErrors) throw e;
84
+ if (e.statusCode === 403) {
85
+ serverErrors.value.global = ["forbidden"];
86
+ } else if (e.statusCode === 404) {
87
+ if (typeof e.data?.data === "string" && e.data.data.length > 0) {
88
+ serverErrors.value[e.data.data] = ["notFound"];
89
+ }
90
+ } else if (e.statusCode === 422) {
91
+ if (typeof e.data?.data === "string" && e.data.data.length > 0) {
92
+ serverErrors.value = JSON.parse(e.data.data) as MobilettoOrmValidationErrors;
93
+ }
94
+ } else {
95
+ throw e;
96
+ }
97
+ };
@@ -0,0 +1,50 @@
1
+ {{#if disclaimer}}// {{{ disclaimer }}}{{#if header}}
2
+ {{{header}}}{{/if}}{{/if}}
3
+
4
+ import { Ref } from "vue";
5
+ import { MobilettoOrmObject, MobilettoOrmPurgeResults } from "mobiletto-orm-typedef";
6
+ import { MobilettoOrmValidationErrors } from "mobiletto-orm";
7
+ import { {{ Name }}Type, {{ Name }}TypeDef } from "{{typeDefPackage}}";
8
+ import * as a from "{{utilsPath}}/serviceHelper.js";
9
+
10
+ export const {{ name }}Service = {
11
+ find{{ Name }},
12
+ create{{ Name }},
13
+ update{{ Name }},
14
+ delete{{ Name }},
15
+ };
16
+
17
+ function find{{ Name }}(
18
+ serverErrors?: Ref<MobilettoOrmValidationErrors>,
19
+ ): Promise<{{ Name }}Type> {
20
+ return $fetch(`/api/model/{{ name }}/{{ typeDef.singleton }}`, a.authGet())
21
+ .then(a.handleJsonResponse<{{ Name }}Type>)
22
+ .catch(a.handleErrors(serverErrors)) as Promise<{{Name}}Type>;
23
+ }
24
+
25
+ function create{{ Name }}(
26
+ {{ name }}: {{ Name }}Type,
27
+ serverErrors: Ref<MobilettoOrmValidationErrors>,
28
+ ): Promise<{{ Name }}Type> {
29
+ return $fetch("/api/model/{{ name }}/{{ typeDef.singleton }}", a.authPutJson({{ name }}))
30
+ .then(a.handleJsonResponse<{{ Name }}Type>)
31
+ .catch(a.handleErrors(serverErrors)) as Promise<{{Name}}Type>;
32
+ }
33
+
34
+ function update{{ Name }}(
35
+ {{ name }}: {{ Name }}Type,
36
+ serverErrors: Ref<MobilettoOrmValidationErrors>,
37
+ ): Promise<{{ Name }}Type> {
38
+ return $fetch("/api/model/{{ name }}/{{ typeDef.singleton }}", a.authPatchJson({{ name }}))
39
+ .then(a.handleJsonResponse<{{ Name }}Type>)
40
+ .catch(a.handleErrors(serverErrors)) as Promise<{{Name}}Type>;
41
+ }
42
+
43
+ function delete{{ Name }}(
44
+ serverErrors: Ref<MobilettoOrmValidationErrors>,
45
+ purge?: boolean
46
+ ): Promise<MobilettoOrmObject | MobilettoOrmPurgeResults> {
47
+ return $fetch(`/api/model/{{ name }}/{{ typeDef.singleton }}/${purge ? `?purge=${purge}` : ""}`, a.authDelete())
48
+ .then(a.handleJsonResponse<MobilettoOrmObject | MobilettoOrmPurgeResults>)
49
+ .catch(a.handleErrors(serverErrors)) as Promise<{{Name}}Type>;
50
+ }
@@ -0,0 +1,73 @@
1
+ {{#if disclaimer}}// {{{ disclaimer }}}{{#if header}}
2
+ {{{header}}}{{/if}}{{/if}}
3
+
4
+ import { Ref } from "vue";
5
+ import { defineStore } from "pinia";
6
+ import { MobilettoOrmValidationErrors } from "mobiletto-orm";
7
+ import { {{Name}}Type } from "{{typeDefPackage}}";
8
+ import { {{name}}Service } from "{{servicesImportPath}}/{{name}}Service";
9
+
10
+ export const use{{Name}}Store = defineStore("{{name}}", {
11
+ state: () => ({
12
+ finding: false,
13
+ found: null as {{Name}}Type | null,
14
+ creating: false,
15
+ created: null as {{Name}}Type | null,
16
+ updating: false,
17
+ updated: null as {{Name}}Type | null,
18
+ deleting: false,
19
+ deleted: null as boolean | null,
20
+ }),
21
+ getters: {
22
+ {{name}}Busy: (state) => state.finding || state.creating || state.updating || state.deleting,
23
+ },
24
+ actions: {
25
+ async lookup(serverErrors?: Ref<MobilettoOrmValidationErrors>): Promise<{{Name}}Type> {
26
+ this.found = null;
27
+ this.finding = true;
28
+ try {
29
+ return await {{name}}Service.find{{Name}}(serverErrors);
30
+ } finally {
31
+ this.finding = false;
32
+ }
33
+ },
34
+ async search(): Promise<{{Name}}Type[]> {
35
+ return [await this.lookup()];
36
+ },
37
+ async create(object: {{Name}}Type, serverErrors: Ref<MobilettoOrmValidationErrors>): Promise<{{Name}}Type> {
38
+ try {
39
+ this.created = null;
40
+ this.creating = true;
41
+ this.created = await {{name}}Service.create{{Name}}(object, serverErrors);
42
+ return this.created;
43
+ } finally {
44
+ this.creating = false;
45
+ }
46
+ },
47
+ async update(object: {{Name}}Type, serverErrors: Ref<MobilettoOrmValidationErrors>): Promise<{{Name}}Type> {
48
+ try {
49
+ this.updated = null;
50
+ this.updating = true;
51
+ this.updated = await {{name}}Service.update{{Name}}(object, serverErrors);
52
+ return this.updated;
53
+ } finally {
54
+ this.updating = false;
55
+ }
56
+ },
57
+ async delete(serverErrors: Ref<MobilettoOrmValidationErrors>, purge?: boolean): Promise<boolean> {
58
+ try {
59
+ this.deleted = null;
60
+ this.deleting = true;
61
+ const deleteResult = await {{name}}Service.delete{{Name}}(serverErrors, !!purge);
62
+ if (deleteResult) {
63
+ this.deleted = true;
64
+ } else {
65
+ this.deleted = false;
66
+ }
67
+ return this.deleted;
68
+ } finally {
69
+ this.deleting = false;
70
+ }
71
+ },
72
+ },
73
+ });
@@ -0,0 +1,94 @@
1
+ {{#if disclaimer}}// {{{ disclaimer }}}{{#if header}}
2
+ {{{header}}}{{/if}}{{/if}}
3
+
4
+ import { Ref } from "vue";
5
+ import { defineStore } from "pinia";
6
+ import { MobilettoOrmValidationErrors } from "mobiletto-orm";
7
+ import { {{Name}}Type, {{Name}}TypeDef } from "{{typeDefPackage}}";
8
+ import { {{name}}Service } from "{{servicesImportPath}}/{{name}}Service";
9
+ import { MobilettoOrmFindApiOpts, updateOrmList } from "{{utilsImportPath}}/storeHelper.js";
10
+
11
+ const updateList = (list: {{Name}}Type[] | null, id: string, opts?: { object?: {{Name}}Type; remove?: boolean }) => {
12
+ return updateOrmList<{{Name}}Type>({{Name}}TypeDef, list, id, opts);
13
+ };
14
+
15
+ export const use{{Name}}Store = defineStore("{{name}}", {
16
+ state: () => ({
17
+ finding: false,
18
+ found: null as {{Name}}Type | null,
19
+ creating: false,
20
+ created: null as {{Name}}Type | null,
21
+ updating: false,
22
+ updated: null as {{Name}}Type | null,
23
+ deleting: false,
24
+ deleted: null as boolean | null,
25
+ searching: false,
26
+ {{name}}List: null as {{Name}}Type[] | null,
27
+ }),
28
+ getters: {
29
+ {{name}}Busy: (state) => state.finding || state.creating || state.updating || state.deleting || state.searching,
30
+ },
31
+ actions: {
32
+ async lookup(id: string, serverErrors: Ref<MobilettoOrmValidationErrors>): Promise<{{Name}}Type> {
33
+ this.found = null;
34
+ this.finding = true;
35
+ try {
36
+ this.found = await {{name}}Service.find{{Name}}(id, serverErrors);
37
+ if (this.found) {
38
+ updateList(this.{{name}}List, {{Name}}TypeDef.id(this.found), {object: this.found});
39
+ }
40
+ return this.found;
41
+ } finally {
42
+ this.finding = false;
43
+ }
44
+ },
45
+ async search(query?: MobilettoOrmFindApiOpts): Promise<{{Name}}Type[]> {
46
+ try {
47
+ this.searching = true;
48
+ this.{{name}}List = await {{name}}Service.search{{Name}}(query);
49
+ return this.{{name}}List || [];
50
+ } finally {
51
+ this.searching = false;
52
+ }
53
+ },
54
+ async create(object: {{Name}}Type, serverErrors: Ref<MobilettoOrmValidationErrors>): Promise<{{Name}}Type> {
55
+ try {
56
+ this.created = null;
57
+ this.creating = true;
58
+ this.created = await {{name}}Service.create{{Name}}(object, serverErrors);
59
+ return this.created;
60
+ } finally {
61
+ this.creating = false;
62
+ }
63
+ },
64
+ async update(object: {{Name}}Type, serverErrors: Ref<MobilettoOrmValidationErrors>): Promise<{{Name}}Type> {
65
+ try {
66
+ this.updated = null;
67
+ this.updating = true;
68
+ this.updated = await {{name}}Service.update{{Name}}(object, serverErrors);
69
+ if (this.updated) {
70
+ updateList(this.{{name}}List, {{Name}}TypeDef.id(this.updated), {object: this.updated});
71
+ }
72
+ return this.updated;
73
+ } finally {
74
+ this.updating = false;
75
+ }
76
+ },
77
+ async delete(id: string, serverErrors: Ref<MobilettoOrmValidationErrors>, purge?: boolean): Promise<boolean> {
78
+ try {
79
+ this.deleted = null;
80
+ this.deleting = true;
81
+ const deleteResult = await {{name}}Service.delete{{Name}}(id, serverErrors, !!purge);
82
+ if (deleteResult) {
83
+ updateList(this.{{name}}List, id, { remove: true });
84
+ this.deleted = true;
85
+ } else {
86
+ this.deleted = false;
87
+ }
88
+ return this.deleted;
89
+ } finally {
90
+ this.deleting = false;
91
+ }
92
+ },
93
+ },
94
+ });
@@ -0,0 +1,15 @@
1
+ import { MobilettoOrmObject, MobilettoOrmTypeDef } from "mobiletto-orm-typedef";
2
+ export type MobilettoOrmFindApiOpts = {
3
+ field?: string;
4
+ value?: string;
5
+ textSearch?: string;
6
+ opts?: {
7
+ first?: boolean;
8
+ removed?: boolean;
9
+ noRedact?: boolean;
10
+ };
11
+ };
12
+ export declare const updateOrmList: <T extends MobilettoOrmObject>(typeDef: MobilettoOrmTypeDef, list: T[] | null, id: string, opts?: {
13
+ object?: T | undefined;
14
+ remove?: boolean | undefined;
15
+ } | undefined) => void;
@@ -0,0 +1,15 @@
1
+ export const updateOrmList = (typeDef, list, id, opts) => {
2
+ if (!opts)
3
+ return;
4
+ if (list) {
5
+ const foundIndex = list.findIndex((e) => typeDef.id(e) === id);
6
+ if (foundIndex && foundIndex >= 0) {
7
+ if (opts && opts.remove === true) {
8
+ list.splice(foundIndex, 1);
9
+ }
10
+ else if (opts && opts.object) {
11
+ list.splice(foundIndex, 1, opts.object);
12
+ }
13
+ }
14
+ }
15
+ };
@@ -0,0 +1,34 @@
1
+ {{#if disclaimer}}// {{{ disclaimer }}}{{#if header}}
2
+ {{{header}}}{{/if}}{{/if}}
3
+
4
+ import { MobilettoOrmObject, MobilettoOrmTypeDef } from "mobiletto-orm-typedef";
5
+
6
+ export type MobilettoOrmFindApiOpts = {
7
+ field?: string;
8
+ value?: string;
9
+ textSearch?: string;
10
+ opts?: {
11
+ first?: boolean;
12
+ removed?: boolean;
13
+ noRedact?: boolean;
14
+ };
15
+ };
16
+
17
+ export const updateOrmList = <T extends MobilettoOrmObject>(
18
+ typeDef: MobilettoOrmTypeDef,
19
+ list: T[] | null,
20
+ id: string,
21
+ opts?: { object?: T; remove?: boolean },
22
+ ) => {
23
+ if (!opts) return;
24
+ if (list) {
25
+ const foundIndex = list.findIndex((e) => typeDef.id(e) === id);
26
+ if (foundIndex && foundIndex >= 0) {
27
+ if (opts && opts.remove === true) {
28
+ list.splice(foundIndex, 1);
29
+ } else if (opts && opts.object) {
30
+ list.splice(foundIndex, 1, opts.object);
31
+ }
32
+ }
33
+ }
34
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mobiletto-orm-typedef-gen",
3
- "version": "2.2.22",
3
+ "version": "2.3.1",
4
4
  "type": "module",
5
5
  "description": "generate various files based on mobiletto-orm-typedef definitions",
6
6
  "keywords": [
@@ -27,7 +27,7 @@
27
27
  "license": "Apache-2.0",
28
28
  "scripts": {
29
29
  "test": "mocha ./test/*.spec.js",
30
- "tsc": "tsc --module es2020 -p tsconfig.json && tsc --module es2020 -p tsconfig-cjs.json && copyfiles --flat --error src/templates/*.hbs lib/esm/templates/ && copyfiles --flat --error src/templates/*.hbs lib/cjs/templates/",
30
+ "tsc": "tsc --module es2020 -p tsconfig.json && tsc --module es2020 -p tsconfig-cjs.json && cd src && copyfiles \"./templates/**/*.hbs\" ../lib/cjs/ && copyfiles \"./templates/**/*.hbs\" ../lib/esm/",
31
31
  "lint": "npx eslint src test",
32
32
  "lint:fix": "npm run lint -- --fix",
33
33
  "prettier": "npx prettier src test --check",
@@ -52,7 +52,8 @@
52
52
  "eslint": "^8.44.0",
53
53
  "eslint-config-prettier": "^8.8.0",
54
54
  "handlebars": "^4.7.7",
55
- "mobiletto-orm-typedef": "^2.3.19",
55
+ "hokey-runtime": "^1.0.4",
56
+ "mobiletto-orm-typedef": "^2.3.20",
56
57
  "mocha": "^10.2.0",
57
58
  "prettier": "^3.0.0",
58
59
  "tslint-config-prettier": "^1.18.0",