mobiletto-orm-typedef-gen 2.2.21 → 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.
- package/README.md +11 -1
- package/lib/cjs/generate.d.ts +2 -1
- package/lib/cjs/generate.js +8 -14
- package/lib/cjs/generators/client.d.ts +8 -0
- package/lib/cjs/generators/client.js +73 -0
- package/lib/cjs/generators/server.d.ts +9 -0
- package/lib/cjs/generators/server.js +33 -0
- package/lib/cjs/generators/tsType.js +1 -1
- package/lib/cjs/generators/yup.js +1 -1
- package/lib/cjs/index.d.ts +2 -0
- package/lib/cjs/index.js +2 -0
- package/lib/cjs/templates/api/id.delete.ts.hbs +66 -0
- package/lib/cjs/templates/api/id.get.ts.hbs +37 -0
- package/lib/cjs/templates/api/id.patch.ts.hbs +37 -0
- package/lib/cjs/templates/api/id.put.ts.hbs +35 -0
- package/lib/cjs/templates/api/index.post.ts.hbs +41 -0
- package/lib/cjs/templates/component/admin.vue.hbs +405 -0
- package/lib/cjs/templates/component/adminHelper.ts.hbs +65 -0
- package/lib/cjs/templates/component/componentHelper.d.ts +3 -0
- package/lib/cjs/templates/component/componentHelper.js +53 -0
- package/lib/cjs/templates/service/service.ts.hbs +59 -0
- package/lib/cjs/templates/service/serviceHelper.ts.hbs +97 -0
- package/lib/cjs/templates/service/singletonService.ts.hbs +50 -0
- package/lib/cjs/templates/store/singletonStore.ts.hbs +73 -0
- package/lib/cjs/templates/store/store.ts.hbs +94 -0
- package/lib/cjs/templates/store/storeHelper.d.ts +15 -0
- package/lib/cjs/templates/store/storeHelper.js +15 -0
- package/lib/cjs/templates/store/storeHelper.ts.hbs +34 -0
- package/lib/esm/generate.d.ts +2 -1
- package/lib/esm/generate.js +8 -14
- package/lib/esm/generators/client.d.ts +8 -0
- package/lib/esm/generators/client.js +73 -0
- package/lib/esm/generators/server.d.ts +9 -0
- package/lib/esm/generators/server.js +33 -0
- package/lib/esm/generators/tsType.js +1 -1
- package/lib/esm/generators/yup.js +1 -1
- package/lib/esm/index.d.ts +2 -0
- package/lib/esm/index.js +2 -0
- package/lib/esm/templates/api/id.delete.ts.hbs +66 -0
- package/lib/esm/templates/api/id.get.ts.hbs +37 -0
- package/lib/esm/templates/api/id.patch.ts.hbs +37 -0
- package/lib/esm/templates/api/id.put.ts.hbs +35 -0
- package/lib/esm/templates/api/index.post.ts.hbs +41 -0
- package/lib/esm/templates/component/admin.vue.hbs +405 -0
- package/lib/esm/templates/component/adminHelper.ts.hbs +65 -0
- package/lib/esm/templates/component/componentHelper.d.ts +3 -0
- package/lib/esm/templates/component/componentHelper.js +53 -0
- package/lib/esm/templates/service/service.ts.hbs +59 -0
- package/lib/esm/templates/service/serviceHelper.ts.hbs +97 -0
- package/lib/esm/templates/service/singletonService.ts.hbs +50 -0
- package/lib/esm/templates/store/singletonStore.ts.hbs +73 -0
- package/lib/esm/templates/store/store.ts.hbs +94 -0
- package/lib/esm/templates/store/storeHelper.d.ts +15 -0
- package/lib/esm/templates/store/storeHelper.js +15 -0
- package/lib/esm/templates/store/storeHelper.ts.hbs +34 -0
- package/package.json +4 -3
- /package/lib/cjs/templates/{tsType.ts.hbs → type/tsType.ts.hbs} +0 -0
- /package/lib/cjs/templates/{yup.ts.hbs → validation/yup.ts.hbs} +0 -0
- /package/lib/esm/templates/{tsType.ts.hbs → type/tsType.ts.hbs} +0 -0
- /package/lib/esm/templates/{yup.ts.hbs → validation/yup.ts.hbs} +0 -0
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
{{#if disclaimer}}<!-- {{{ disclaimer }}}{{#if header}}
|
|
2
|
+
{{{header}}}{{/if}} -->{{/if}}
|
|
3
|
+
|
|
4
|
+
<template>
|
|
5
|
+
<v-container>
|
|
6
|
+
<v-row>
|
|
7
|
+
<v-col>
|
|
8
|
+
<h2>{{{curlyOpen}}}{{{curlyOpen}}} adminTitle() {{{curlyClose}}}{{{curlyClose}}}</h2>
|
|
9
|
+
<b v-if="messageExists('title_{{name}}_admin_details', messages)">
|
|
10
|
+
{{{curlyOpen}}}{{{curlyOpen}}} messages.title_{{name}}_admin_details {{{curlyClose}}}{{{curlyClose}}}
|
|
11
|
+
</b>
|
|
12
|
+
</v-col>
|
|
13
|
+
</v-row>
|
|
14
|
+
<div>
|
|
15
|
+
<v-row v-if="addingObject">
|
|
16
|
+
<v-col>
|
|
17
|
+
<OrmForm
|
|
18
|
+
v-if="allRefsLoaded() && {{name}}TypeDef"
|
|
19
|
+
form-name="add_{{name}}_form"
|
|
20
|
+
:type-def="{{name}}TypeDef"
|
|
21
|
+
:type-name-message="typeNameMessage"
|
|
22
|
+
:thing="addObject"
|
|
23
|
+
:fields="{{name}}TypeDefFields"
|
|
24
|
+
:create="true"
|
|
25
|
+
:read-only-object="() => false"
|
|
26
|
+
:server-errors="create{{Name}}ServerErrors"
|
|
27
|
+
:label-prefixes="labelPfx"
|
|
28
|
+
:hint-suffixes="['_description']"{{#if typeDef.singleton}}
|
|
29
|
+
cancel-button-message=""{{/if}}
|
|
30
|
+
@submitted="onAddSubmitted"
|
|
31
|
+
@update="onAddUpdated"{{#unless typeDef.singleton}}
|
|
32
|
+
@cancel="onAddCancel"{{/unless}}
|
|
33
|
+
/>
|
|
34
|
+
</v-col>
|
|
35
|
+
</v-row>
|
|
36
|
+
<v-row v-else-if="Object.keys(editingObject).length > 0">
|
|
37
|
+
<v-col>
|
|
38
|
+
<OrmForm
|
|
39
|
+
v-if="allRefsLoaded() && {{name}}TypeDef"
|
|
40
|
+
form-name="edit_{{name}}_form"
|
|
41
|
+
:type-def="{{name}}TypeDef"
|
|
42
|
+
type-name-message="typeNameMessage"
|
|
43
|
+
:thing="editingObject"
|
|
44
|
+
:fields="{{name}}TypeDefFields"
|
|
45
|
+
:create="false"
|
|
46
|
+
:read-only-object="() => false"
|
|
47
|
+
:server-errors="edit{{Name}}ServerErrors"
|
|
48
|
+
:label-prefixes="labelPfx"
|
|
49
|
+
:hint-suffixes="['_description']"
|
|
50
|
+
@submitted="onEditSubmitted"
|
|
51
|
+
@update="onEditUpdated"
|
|
52
|
+
@cancel="onEditCancel"
|
|
53
|
+
/>
|
|
54
|
+
</v-col>
|
|
55
|
+
</v-row>{{#unless typeDef.singleton}}
|
|
56
|
+
<v-row v-else-if="allRefsLoaded() && {{name}}TypeDef">
|
|
57
|
+
<v-col>
|
|
58
|
+
<v-container>
|
|
59
|
+
<v-row v-if="({{name}}List && {{name}}List.length > 0) || searched">
|
|
60
|
+
<v-col>
|
|
61
|
+
<div>
|
|
62
|
+
<v-form @submit.prevent="searchObjects">
|
|
63
|
+
<div class="form-group">
|
|
64
|
+
<v-text-field
|
|
65
|
+
v-model="searchTerms"
|
|
66
|
+
:label="messages.label_search"
|
|
67
|
+
:disabled="libraryStore.libraryBusy"
|
|
68
|
+
type="text"
|
|
69
|
+
name="searchTerms"
|
|
70
|
+
class="form-control"
|
|
71
|
+
@keyup.enter="searchObjects"
|
|
72
|
+
/>
|
|
73
|
+
<v-btn class="btn btn-primary" :disabled="{{name}}Store.{{name}}Busy" @click.stop="searchObjects">
|
|
74
|
+
<Icon name="material-symbols:search" />
|
|
75
|
+
</v-btn>
|
|
76
|
+
</div>
|
|
77
|
+
</v-form>
|
|
78
|
+
</div>
|
|
79
|
+
</v-col>
|
|
80
|
+
</v-row>
|
|
81
|
+
<v-row>
|
|
82
|
+
<v-col>
|
|
83
|
+
<table v-if="{{name}}List && {{name}}List.length > 0">
|
|
84
|
+
<thead>
|
|
85
|
+
<tr>
|
|
86
|
+
<th v-for="(tableField, tableFieldIndex) in tableFields" :key="tableFieldIndex">
|
|
87
|
+
{{{curlyOpen}}}{{{curlyOpen}}} tableFieldMessages[tableField] {{{curlyClose}}}{{{curlyClose}}}
|
|
88
|
+
</th>
|
|
89
|
+
<th v-if="Object.keys(actionConfigs).length > 0">
|
|
90
|
+
<Icon name="material-symbols:target" />
|
|
91
|
+
</th>
|
|
92
|
+
<th></th>
|
|
93
|
+
<th></th>
|
|
94
|
+
</tr>
|
|
95
|
+
</thead>
|
|
96
|
+
<tbody>
|
|
97
|
+
<tr v-for="(obj, objIndex) in {{name}}List" :key="objIndex">
|
|
98
|
+
<td v-for="(fieldName, fieldIndex) in tableFields" :key="fieldIndex">
|
|
99
|
+
<OrmFieldDisplay v-if="{{name}}TypeDef.fields[fieldName] || fieldName.startsWith('_meta')" :field="fieldName.startsWith('_meta') ? metaField(fieldName) : {{name}}TypeDef.fields[fieldName]" :value="deepGet(obj, fieldName)" />
|
|
100
|
+
</td>
|
|
101
|
+
<td v-if="Object.keys(actionConfigs).length > 0">
|
|
102
|
+
<div v-for="(action, actionIndex) in Object.keys(actionConfigs)" :key="actionIndex">
|
|
103
|
+
<NuxtLink
|
|
104
|
+
v-if="actionEnabled(obj, action)"
|
|
105
|
+
:to="{ path: `${actionConfig(action).path}/${deepGet(obj, {{name}}TypeDef.idField(obj) as string)}` }"
|
|
106
|
+
>
|
|
107
|
+
<v-btn>
|
|
108
|
+
{{{curlyOpen}}}{{{curlyOpen}}} messages[actionConfig(action).message] {{{curlyClose}}}{{{curlyClose}}}
|
|
109
|
+
</v-btn>
|
|
110
|
+
</NuxtLink>
|
|
111
|
+
</div>
|
|
112
|
+
</td>
|
|
113
|
+
<td>
|
|
114
|
+
<v-btn v-if="canEdit(obj, {{name}}List)" :disabled="{{name}}Store.{{name}}Busy" @click.stop="showEditOrm(obj)">
|
|
115
|
+
<Icon name="material-symbols:edit" />
|
|
116
|
+
</v-btn>
|
|
117
|
+
</td>
|
|
118
|
+
<td>
|
|
119
|
+
<v-btn v-if="canDelete(obj, {{name}}List)" :disabled="{{name}}Store.{{name}}Busy" @click.stop="delObject(obj)">
|
|
120
|
+
<Icon name="material-symbols:delete" />
|
|
121
|
+
</v-btn>
|
|
122
|
+
</td>
|
|
123
|
+
</tr>
|
|
124
|
+
</tbody>
|
|
125
|
+
</table>
|
|
126
|
+
</v-col>
|
|
127
|
+
</v-row>
|
|
128
|
+
<v-row>
|
|
129
|
+
<v-col>
|
|
130
|
+
<v-btn class="btn btn-primary" :disabled="{{name}}Store.{{name}}Busy" @click.stop="showAddOrm">
|
|
131
|
+
<Icon name="material-symbols:add" />
|
|
132
|
+
</v-btn>
|
|
133
|
+
</v-col>
|
|
134
|
+
</v-row>
|
|
135
|
+
</v-container>
|
|
136
|
+
</v-col>
|
|
137
|
+
</v-row>{{/unless}}
|
|
138
|
+
<v-row v-else>
|
|
139
|
+
<v-col>
|
|
140
|
+
<Icon name="material-symbols:clock-outline" />
|
|
141
|
+
</v-col>
|
|
142
|
+
</v-row>
|
|
143
|
+
</div>
|
|
144
|
+
</v-container>
|
|
145
|
+
</template>
|
|
146
|
+
|
|
147
|
+
<script setup lang="ts">
|
|
148
|
+
import { ref, watch, Ref } from "vue";
|
|
149
|
+
import { storeToRefs } from "pinia";
|
|
150
|
+
import {
|
|
151
|
+
MobilettoOrmFieldDefConfig,
|
|
152
|
+
MobilettoOrmObject,
|
|
153
|
+
MobilettoOrmTypeDef,
|
|
154
|
+
MobilettoOrmValidationErrors,
|
|
155
|
+
metaField
|
|
156
|
+
} from "mobiletto-orm-typedef";
|
|
157
|
+
import { {{Name}}Type, {{Name}}TypeDef } from "{{typeDefPackage}}";
|
|
158
|
+
import { findMessage, messageExists, parseMessage } from "hokey-runtime";
|
|
159
|
+
import { useSessionStore } from "{{sessionStoreImport}}";
|
|
160
|
+
import { use{{Name}}Store } from "~/stores/model/{{name}}Store";{{#if typeDef.refTypes}}{{#each typeDef.refTypes}}
|
|
161
|
+
import { use{{RefType}}Store } from "~/stores/model/{{refType}}Store";{{/each}}{{/if}}
|
|
162
|
+
import { deepUpdate{{#unless typeDef.singleton}}, normalizeMsg{{/unless}} } from "{{componentHelperPath}}/componentHelper.js";
|
|
163
|
+
{{#unless typeDef.singleton}}
|
|
164
|
+
type ActionConfig = {
|
|
165
|
+
path: string;
|
|
166
|
+
message: string;
|
|
167
|
+
when: (obj: MobilettoOrmObject) => boolean;
|
|
168
|
+
};{{/unless}}
|
|
169
|
+
const props = withDefaults(
|
|
170
|
+
defineProps<{
|
|
171
|
+
labelPrefixes: string[];
|
|
172
|
+
typeNameMessage: string,{{#unless typeDef.singleton}}
|
|
173
|
+
actionConfigs: Record<string, ActionConfig>;
|
|
174
|
+
canEdit: (obj: MobilettoOrmObject, objList: MobilettoOrmObject[]) => boolean;
|
|
175
|
+
canDelete: (obj: MobilettoOrmObject, objList: MobilettoOrmObject[]) => boolean;
|
|
176
|
+
deleteConfirmationMessage: string;{{/unless}}
|
|
177
|
+
}>(),{
|
|
178
|
+
labelPrefixes: () => ["label_", ""],
|
|
179
|
+
typeNameMessage: () => "typename_{{name}}",{{#unless typeDef.singleton}}
|
|
180
|
+
actionConfigs: () => ({}),
|
|
181
|
+
canEdit: () => true,
|
|
182
|
+
canDelete: () => true,{{/unless}}
|
|
183
|
+
},
|
|
184
|
+
);
|
|
185
|
+
|
|
186
|
+
const labelPfx: Ref<string[]> = ref(["admin_label_{{name}}_", "label_", ""]);
|
|
187
|
+
if (props.labelPrefixes) {
|
|
188
|
+
props.labelPrefixes.forEach((p: string) => {
|
|
189
|
+
if (!labelPfx.value.includes(p)) labelPfx.value.unshift(p);
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const sessionStore = useSessionStore();
|
|
194
|
+
const { {{localeMessages}} } = storeToRefs(sessionStore);
|
|
195
|
+
const messages = {{localeMessages}};
|
|
196
|
+
|
|
197
|
+
const adminTitle = () => messageExists("admin_title_{{name}}_administration", messages.value)
|
|
198
|
+
? messages.value.admin_title_{{name}}_administration
|
|
199
|
+
: parseMessage("admin_title_site_administration", messages.value, { title: findMessage(props.typeNameMessage, messages.value, [""])});
|
|
200
|
+
|
|
201
|
+
const addingObject = ref(false);
|
|
202
|
+
const addObject = ref({} as {{Name}}Type);
|
|
203
|
+
const create{{Name}}ServerErrors = ref({} as MobilettoOrmValidationErrors);
|
|
204
|
+
|
|
205
|
+
const editingObject = ref({} as {{Name}}Type);
|
|
206
|
+
const edit{{Name}}ServerErrors = ref({} as MobilettoOrmValidationErrors);{{#unless typeDef.singleton}}
|
|
207
|
+
const delete{{Name}}ServerErrors = ref({} as MobilettoOrmValidationErrors);
|
|
208
|
+
|
|
209
|
+
const pageNumber = ref(1);
|
|
210
|
+
const pageSize = ref(20);
|
|
211
|
+
const searchTerms = ref("");
|
|
212
|
+
const searched = ref(false);
|
|
213
|
+
const lastQuery = ref({});
|
|
214
|
+
|
|
215
|
+
const tableFields: Ref<string[]> = ref([]);
|
|
216
|
+
|
|
217
|
+
const tableFieldMessages: Ref<Record<string, string>> = ref({});
|
|
218
|
+
const initTableFieldMessages = (tableFields: string[]) => {
|
|
219
|
+
const defaultTableFieldMessages: Record<string, string> = {};
|
|
220
|
+
tableFields.forEach((f: string) => {
|
|
221
|
+
defaultTableFieldMessages[f] = findMessage(normalizeMsg(f), messages.value, props.labelPrefixes);
|
|
222
|
+
});
|
|
223
|
+
tableFieldMessages.value = defaultTableFieldMessages;
|
|
224
|
+
};{{/unless}}
|
|
225
|
+
|
|
226
|
+
const {{name}}Store = use{{Name}}Store();
|
|
227
|
+
{{#if typeDef.singleton}}const { found } = storeToRefs({{name}}Store);
|
|
228
|
+
{{else}}const { {{name}}List } = storeToRefs({{name}}Store);{{/if}}
|
|
229
|
+
const {{name}}TypeDef: Ref<MobilettoOrmTypeDef | null> = ref(null);
|
|
230
|
+
const {{name}}TypeDefFields: Ref<MobilettoOrmFieldDefConfig[] | undefined> = ref(undefined);
|
|
231
|
+
{{#if typeDef.singleton}}
|
|
232
|
+
const findSingleton{{Name}}ServerErrors = ref({} as MobilettoOrmValidationErrors);
|
|
233
|
+
{{else}}
|
|
234
|
+
const searchQuery = () => ({ textSearch: searchTerms.value });
|
|
235
|
+
|
|
236
|
+
const searchObjects = () => {
|
|
237
|
+
const query = searchQuery();
|
|
238
|
+
if (lastQuery.value && JSON.stringify(lastQuery.value) === JSON.stringify(query)) {
|
|
239
|
+
// not sending duplicate search
|
|
240
|
+
} else {
|
|
241
|
+
searched.value = true;
|
|
242
|
+
lastQuery.value = Object.assign({}, query);
|
|
243
|
+
{{name}}Store.search(query);
|
|
244
|
+
}
|
|
245
|
+
};{{/if}}{{#unless typeDef.singleton}}
|
|
246
|
+
|
|
247
|
+
const navigating = ref(false);{{/unless}}
|
|
248
|
+
{{#if typeDef.refTypes}}
|
|
249
|
+
const initTypeDef = () => {
|
|
250
|
+
const typeDef = {{Name}}TypeDef.extend({
|
|
251
|
+
fields: {
|
|
252
|
+
{{#each typeDef.refTypes}}{{#each fieldPaths}}
|
|
253
|
+
{{this}}: { ...ref{{../RefType}}.value },
|
|
254
|
+
{{/each}}{{/each}}
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
{{name}}TypeDefFields.value = typeDef.tabIndexedFields();
|
|
258
|
+
{{name}}TypeDef.value = typeDef;{{#unless typeDef.singleton}}
|
|
259
|
+
tableFields.value = {{name}}TypeDef.value.tableFields && Array.isArray({{name}}TypeDef.value.tableFields)
|
|
260
|
+
? {{name}}TypeDef.value.tableFields
|
|
261
|
+
: {{name}}TypeDef.value.primary
|
|
262
|
+
? [{{name}}TypeDef.value.primary, "ctime", "mtime"]
|
|
263
|
+
: ["id", "ctime", "mtime"];
|
|
264
|
+
initTableFieldMessages(tableFields.value);{{/unless}}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const allRefs: Ref<Boolean>[] = [];
|
|
268
|
+
const allRefsLoaded = () => allRefs.length === {{typeDef.refTypes.length}} && allRefs.filter(r => r.value === true).length === {{typeDef.refTypes.length}};{{#each typeDef.refTypes}}
|
|
269
|
+
const ref{{RefType}} = ref({} as MobilettoOrmFieldDefConfig);
|
|
270
|
+
const ref{{RefType}}Loaded = ref(false);
|
|
271
|
+
allRefs.push(ref{{RefType}}Loaded);
|
|
272
|
+
const {{refType}}Store = use{{RefType}}Store();
|
|
273
|
+
const { {{refType}}List } = storeToRefs({{refType}}Store);
|
|
274
|
+
|
|
275
|
+
watch({{refType}}List, (newList) => {
|
|
276
|
+
if (newList && newList.length === 0) {
|
|
277
|
+
if (navigating.value) return;
|
|
278
|
+
navigating.value = true;
|
|
279
|
+
navigateTo("/admin/{{refType}}/setup");
|
|
280
|
+
} else if (newList && newList.length > 0) {
|
|
281
|
+
ref{{RefType}}.value.values = newList.map((s) => s.name);
|
|
282
|
+
ref{{RefType}}.value.labels = newList.map((s) => s.name);
|
|
283
|
+
ref{{RefType}}.value.items = newList.map((s) => ({
|
|
284
|
+
label: s.name,
|
|
285
|
+
value: s.name,
|
|
286
|
+
title: s.name,
|
|
287
|
+
rawLabel: true,
|
|
288
|
+
}));
|
|
289
|
+
{{#each fieldPaths}}
|
|
290
|
+
addObject.value.{{this}} = ref{{../RefType}}.value.values as any;
|
|
291
|
+
{{/each}}
|
|
292
|
+
ref{{RefType}}Loaded.value = true;
|
|
293
|
+
if (allRefsLoaded()) {
|
|
294
|
+
initTypeDef()
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
{{/each}}{{else}}
|
|
299
|
+
const allRefsLoaded = () => true;
|
|
300
|
+
{{name}}TypeDef.value = {{Name}}TypeDef;
|
|
301
|
+
{{name}}TypeDefFields.value = {{Name}}TypeDef.tabIndexedFields();{{#unless typeDef.singleton}}
|
|
302
|
+
tableFields.value = {{name}}TypeDef.value.tableFields && Array.isArray({{name}}TypeDef.value.tableFields)
|
|
303
|
+
? {{name}}TypeDef.value.tableFields
|
|
304
|
+
: {{name}}TypeDef.value.primary
|
|
305
|
+
? [{{name}}TypeDef.value.primary, "ctime", "mtime"]
|
|
306
|
+
: ["id", "ctime", "mtime"];
|
|
307
|
+
initTableFieldMessages(tableFields.value);{{/unless}}
|
|
308
|
+
{{/if}}
|
|
309
|
+
|
|
310
|
+
const onAddUpdated = (update: { field: string; value: any }) => {
|
|
311
|
+
deepUpdate(addObject.value, update.field, update.value);
|
|
312
|
+
};
|
|
313
|
+
const onAddSubmitted = async (obj: MobilettoOrmObject) => {
|
|
314
|
+
await {{name}}Store
|
|
315
|
+
.create(obj as {{Name}}Type, create{{Name}}ServerErrors);
|
|
316
|
+
addingObject.value = false;
|
|
317
|
+
{{#if typeDef.singleton}}
|
|
318
|
+
return await {{name}}Store.lookup(findSingleton{{Name}}ServerErrors);
|
|
319
|
+
{{else}}
|
|
320
|
+
return await {{name}}Store.search();
|
|
321
|
+
{{/if}}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
const showAddOrm = () => {
|
|
325
|
+
addingObject.value = true;
|
|
326
|
+
};{{#unless typeDef.singleton}}
|
|
327
|
+
const onAddCancel = () => {
|
|
328
|
+
addingObject.value = {{#if typeDef.singleton}}true{{else}}false{{/if}};
|
|
329
|
+
};{{/unless}}
|
|
330
|
+
|
|
331
|
+
const onEditUpdated = (update: { field: string; value: any }) => {
|
|
332
|
+
if (editingObject.value) {
|
|
333
|
+
deepUpdate(editingObject.value, update.field, update.value);
|
|
334
|
+
}
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
const onEditSubmitted = async (obj: MobilettoOrmObject) => {
|
|
338
|
+
await {{name}}Store
|
|
339
|
+
.update(obj as {{Name}}Type, edit{{Name}}ServerErrors);
|
|
340
|
+
editingObject.value = {} as {{Name}}Type;{{#if typeDef.singleton}}
|
|
341
|
+
return await {{name}}Store.lookup(findSingleton{{Name}}ServerErrors);{{else}}
|
|
342
|
+
return await {{name}}Store.search();{{/if}}
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
const showEditOrm = (obj: MobilettoOrmObject) => {
|
|
346
|
+
if (Object.keys(editingObject.value).length > 0) {
|
|
347
|
+
// already editing something else
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
if (obj) {
|
|
351
|
+
const id = {{Name}}TypeDef.id(obj);
|
|
352
|
+
if (id && id.length > 0) {
|
|
353
|
+
editingObject.value = JSON.parse(JSON.stringify(obj)) as {{Name}}Type;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
};
|
|
357
|
+
const onEditCancel = () => {
|
|
358
|
+
{{#if typeDef.singleton}}
|
|
359
|
+
editingObject.value = JSON.parse(JSON.stringify(found)) as {{Name}}Type;
|
|
360
|
+
{{else}}
|
|
361
|
+
editingObject.value = {} as {{Name}}Type;
|
|
362
|
+
{{/if}}
|
|
363
|
+
};
|
|
364
|
+
{{#if typeDef.singleton}}
|
|
365
|
+
watch(found, (newFound: MobilettoOrmObject) => {
|
|
366
|
+
if (newFound && {{Name}}TypeDef.id(newFound)) {
|
|
367
|
+
showEditOrm(newFound)
|
|
368
|
+
} else {
|
|
369
|
+
showAddOrm();
|
|
370
|
+
}
|
|
371
|
+
});
|
|
372
|
+
watch(findSingleton{{Name}}ServerErrors, (newErrors) => {
|
|
373
|
+
console.log(`findSingleton errors: ${JSON.stringify(newErrors)}`);
|
|
374
|
+
});{{else}}
|
|
375
|
+
const actionConfig = (action: string) => {
|
|
376
|
+
return props.actionConfigs[action];
|
|
377
|
+
};
|
|
378
|
+
|
|
379
|
+
const actionEnabled = (obj: MobilettoOrmObject, action: string) => {
|
|
380
|
+
const cfg = props.actionConfigs[action];
|
|
381
|
+
if (!cfg.when || typeof cfg.when !== "function") {
|
|
382
|
+
return true;
|
|
383
|
+
}
|
|
384
|
+
if (typeof cfg.when === "function") {
|
|
385
|
+
return cfg.when(obj) === true;
|
|
386
|
+
}
|
|
387
|
+
return true;
|
|
388
|
+
};
|
|
389
|
+
watch({{name}}List, (newList) => {
|
|
390
|
+
if (newList && Array.isArray(newList) && newList.length === 0 && searchTerms.value && searchTerms.value.length === 0) {
|
|
391
|
+
if (navigating.value) return;
|
|
392
|
+
navigating.value = true;
|
|
393
|
+
navigateTo("/admin/{{name}}/setup");
|
|
394
|
+
}
|
|
395
|
+
});{{/if}}
|
|
396
|
+
{{#if typeDef.singleton}}
|
|
397
|
+
{{name}}Store.lookup(findSingleton{{Name}}ServerErrors){{#if typeDef.refTypes}}.then(() => {{{curlyOpen}}}{{#each typeDef.refTypes}}
|
|
398
|
+
{{refType}}Store.search();{{/each}}
|
|
399
|
+
}){{/if}};
|
|
400
|
+
{{else}}
|
|
401
|
+
{{name}}Store.search(){{#if typeDef.refTypes}}.then(() => {{{curlyOpen}}}{{#each typeDef.refTypes}}
|
|
402
|
+
{{refType}}Store.search();{{/each}}
|
|
403
|
+
}){{/if}};
|
|
404
|
+
{{/if}}
|
|
405
|
+
</script>
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{{#if disclaimer}}// {{{ disclaimer }}}{{#if header}}
|
|
2
|
+
{{{header}}}{{/if}}{{/if}}
|
|
3
|
+
|
|
4
|
+
import { MobilettoOrmValidationErrors } from "mobiletto-orm-typedef";
|
|
5
|
+
import { errMatch, errMatchStart, normalizeMsg, fieldErrorMessage } from "hokey-runtime";
|
|
6
|
+
|
|
7
|
+
export const typeDefFieldErrorMessage = (
|
|
8
|
+
field: string | string[],
|
|
9
|
+
messages: Record<string, string>,
|
|
10
|
+
labelPrefixes: string[],
|
|
11
|
+
clientErrors: MobilettoOrmValidationErrors,
|
|
12
|
+
serverErrors: MobilettoOrmValidationErrors,
|
|
13
|
+
submitted?: boolean,
|
|
14
|
+
objPath?: string,
|
|
15
|
+
): string => {
|
|
16
|
+
if (submitted === false) {
|
|
17
|
+
return "";
|
|
18
|
+
}
|
|
19
|
+
const fields = Array.isArray(field) ? field : [field];
|
|
20
|
+
const fieldName = fields[0];
|
|
21
|
+
for (const f of fields) {
|
|
22
|
+
const clientErrFields = Object.keys(clientErrors);
|
|
23
|
+
const serverErrFields = Object.keys(serverErrors);
|
|
24
|
+
let errMsg = clientErrFields.find(errMatch(f));
|
|
25
|
+
if (errMsg) {
|
|
26
|
+
errMsg = errMsg + "_" + clientErrors[errMsg][0];
|
|
27
|
+
} else {
|
|
28
|
+
errMsg = serverErrFields.find(errMatch(f)) || undefined;
|
|
29
|
+
if (errMsg) {
|
|
30
|
+
errMsg = errMsg + "_" + serverErrors[errMsg][0];
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (!errMsg && objPath && objPath.length > 0) {
|
|
34
|
+
errMsg = clientErrFields.find(errMatchStart(objPath));
|
|
35
|
+
if (errMsg) {
|
|
36
|
+
errMsg = errMsg + "_" + clientErrors[errMsg][0];
|
|
37
|
+
} else {
|
|
38
|
+
errMsg = serverErrFields.find(errMatchStart(objPath));
|
|
39
|
+
if (errMsg) {
|
|
40
|
+
errMsg = errMsg + "_" + serverErrors[errMsg][0];
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (errMsg) {
|
|
45
|
+
errMsg = normalizeMsg(errMsg);
|
|
46
|
+
const labelPfx = labelPrefixes.includes("") ? labelPrefixes : [...labelPrefixes, ""];
|
|
47
|
+
const fld = normalizeMsg(fieldName);
|
|
48
|
+
return fieldErrorMessage(fld, errMsg, messages, labelPfx);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return "";
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
55
|
+
export const deepUpdate = (obj: any, fieldPath: string, value: any) => {
|
|
56
|
+
let thing = obj;
|
|
57
|
+
const parts = fieldPath.split(".");
|
|
58
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
59
|
+
if (typeof thing[parts[i]] === "undefined") {
|
|
60
|
+
thing[parts[i]] = {};
|
|
61
|
+
}
|
|
62
|
+
thing = thing[parts[i]];
|
|
63
|
+
}
|
|
64
|
+
thing[parts[parts.length - 1]] = value;
|
|
65
|
+
};
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { MobilettoOrmValidationErrors } from "mobiletto-orm-typedef";
|
|
2
|
+
export declare const typeDefFieldErrorMessage: (field: string | string[], messages: Record<string, string>, labelPrefixes: string[], clientErrors: MobilettoOrmValidationErrors, serverErrors: MobilettoOrmValidationErrors, submitted?: boolean, objPath?: string) => string;
|
|
3
|
+
export declare const deepUpdate: (obj: any, fieldPath: string, value: any) => void;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { errMatch, errMatchStart, normalizeMsg, fieldErrorMessage } from "hokey-runtime";
|
|
2
|
+
export const typeDefFieldErrorMessage = (field, messages, labelPrefixes, clientErrors, serverErrors, submitted, objPath) => {
|
|
3
|
+
if (submitted === false) {
|
|
4
|
+
return "";
|
|
5
|
+
}
|
|
6
|
+
const fields = Array.isArray(field) ? field : [field];
|
|
7
|
+
const fieldName = fields[0];
|
|
8
|
+
for (const f of fields) {
|
|
9
|
+
const clientErrFields = Object.keys(clientErrors);
|
|
10
|
+
const serverErrFields = Object.keys(serverErrors);
|
|
11
|
+
let errMsg = clientErrFields.find(errMatch(f));
|
|
12
|
+
if (errMsg) {
|
|
13
|
+
errMsg = errMsg + "_" + clientErrors[errMsg][0];
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
errMsg = serverErrFields.find(errMatch(f)) || undefined;
|
|
17
|
+
if (errMsg) {
|
|
18
|
+
errMsg = errMsg + "_" + serverErrors[errMsg][0];
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
if (!errMsg && objPath && objPath.length > 0) {
|
|
22
|
+
errMsg = clientErrFields.find(errMatchStart(objPath));
|
|
23
|
+
if (errMsg) {
|
|
24
|
+
errMsg = errMsg + "_" + clientErrors[errMsg][0];
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
errMsg = serverErrFields.find(errMatchStart(objPath));
|
|
28
|
+
if (errMsg) {
|
|
29
|
+
errMsg = errMsg + "_" + serverErrors[errMsg][0];
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (errMsg) {
|
|
34
|
+
errMsg = normalizeMsg(errMsg);
|
|
35
|
+
const labelPfx = labelPrefixes.includes("") ? labelPrefixes : [...labelPrefixes, ""];
|
|
36
|
+
const fld = normalizeMsg(fieldName);
|
|
37
|
+
return fieldErrorMessage(fld, errMsg, messages, labelPfx);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return "";
|
|
41
|
+
};
|
|
42
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
43
|
+
export const deepUpdate = (obj, fieldPath, value) => {
|
|
44
|
+
let thing = obj;
|
|
45
|
+
const parts = fieldPath.split(".");
|
|
46
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
47
|
+
if (typeof thing[parts[i]] === "undefined") {
|
|
48
|
+
thing[parts[i]] = {};
|
|
49
|
+
}
|
|
50
|
+
thing = thing[parts[i]];
|
|
51
|
+
}
|
|
52
|
+
thing[parts[parts.length - 1]] = value;
|
|
53
|
+
};
|
|
@@ -0,0 +1,59 @@
|
|
|
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
|
+
search{{ Name }},
|
|
12
|
+
find{{ Name }},
|
|
13
|
+
create{{ Name }},
|
|
14
|
+
update{{ Name }},
|
|
15
|
+
delete{{ Name }},
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
function search{{ Name }}(query?: MobilettoOrmFindApiOpts): Promise<{{ Name }}Type[]> {
|
|
19
|
+
return $fetch("/api/model/{{ name }}", a.authPostJson(query)).then(a.handleJsonResponse<{{ Name }}Type[]>);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function find{{ Name }}(
|
|
23
|
+
id: string,
|
|
24
|
+
serverErrors: Ref<MobilettoOrmValidationErrors>,
|
|
25
|
+
): Promise<{{ Name }}Type> {
|
|
26
|
+
return $fetch(`/api/model/{{ name }}/${id}`, a.authGet())
|
|
27
|
+
.then(a.handleJsonResponse<{{ Name }}Type>)
|
|
28
|
+
.catch(a.handleErrors(serverErrors)) as Promise<{{Name}}Type>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function create{{ Name }}(
|
|
32
|
+
{{ name }}: {{ Name }}Type,
|
|
33
|
+
serverErrors: Ref<MobilettoOrmValidationErrors>,
|
|
34
|
+
): Promise<{{ Name }}Type> {
|
|
35
|
+
const id = {{ Name }}TypeDef.id({{ name }});
|
|
36
|
+
return $fetch(`/api/model/{{ name }}/${{{curlyOpen}}}id{{{curlyClose}}}`, a.authPutJson({{ name }}))
|
|
37
|
+
.then(a.handleJsonResponse<{{ Name }}Type>)
|
|
38
|
+
.catch(a.handleErrors(serverErrors)) as Promise<{{Name}}Type>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function update{{ Name }}(
|
|
42
|
+
{{ name }}: {{ Name }}Type,
|
|
43
|
+
serverErrors: Ref<MobilettoOrmValidationErrors>,
|
|
44
|
+
): Promise<{{ Name }}Type> {
|
|
45
|
+
const id = {{ Name }}TypeDef.id({{ name }});
|
|
46
|
+
return $fetch(`/api/model/{{ name }}/${{{curlyOpen}}}id{{{curlyClose}}}`, a.authPatchJson({{ name }}))
|
|
47
|
+
.then(a.handleJsonResponse<{{ Name }}Type>)
|
|
48
|
+
.catch(a.handleErrors(serverErrors)) as Promise<{{Name}}Type>;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function delete{{ Name }}(
|
|
52
|
+
id: string,
|
|
53
|
+
serverErrors: Ref<MobilettoOrmValidationErrors>,
|
|
54
|
+
purge?: boolean
|
|
55
|
+
): Promise<MobilettoOrmObject | MobilettoOrmPurgeResults> {
|
|
56
|
+
return $fetch(`/api/model/{{ name }}/${id}/${purge ? `?purge=${purge}` : ""}`, a.authDelete())
|
|
57
|
+
.then(a.handleJsonResponse<MobilettoOrmObject | MobilettoOrmPurgeResults>)
|
|
58
|
+
.catch(a.handleErrors(serverErrors)) as Promise<{{Name}}Type>;
|
|
59
|
+
}
|
|
@@ -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
|
+
};
|