@adminforth/upload 2.3.5 → 2.3.7

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/build.log CHANGED
@@ -11,5 +11,5 @@ custom/preview.vue
11
11
  custom/tsconfig.json
12
12
  custom/uploader.vue
13
13
 
14
- sent 46,947 bytes received 134 bytes 94,162.00 bytes/sec
15
- total size is 46,465 speedup is 0.99
14
+ sent 47,144 bytes received 134 bytes 94,556.00 bytes/sec
15
+ total size is 46,662 speedup is 0.99
@@ -4,16 +4,27 @@
4
4
  "paths": {
5
5
  "@/*": [
6
6
  // "node_modules/adminforth/dist/spa/src/*"
7
- "../../../spa/src/*"
7
+ "../../../adminforth/spa/src/*"
8
8
  ],
9
9
  "*": [
10
10
  // "node_modules/adminforth/dist/spa/node_modules/*"
11
- "../../../spa/node_modules/*"
11
+ "../../../adminforth/spa/node_modules/*"
12
12
  ],
13
13
  "@@/*": [
14
14
  // "node_modules/adminforth/dist/spa/src/*"
15
15
  "."
16
16
  ]
17
17
  }
18
- }
18
+ },
19
+ "include": [
20
+ "./**/*.ts",
21
+ "./**/*.tsx",
22
+ "./**/*.vue",
23
+ "../**/*.ts",
24
+ "../**/*.tsx",
25
+ "../**/*.vue",
26
+ "../*.vue",
27
+ "../*.ts",
28
+ "../*.tsx"
29
+ ]
19
30
  }
@@ -4,16 +4,27 @@
4
4
  "paths": {
5
5
  "@/*": [
6
6
  // "node_modules/adminforth/dist/spa/src/*"
7
- "../../../spa/src/*"
7
+ "../../../adminforth/spa/src/*"
8
8
  ],
9
9
  "*": [
10
10
  // "node_modules/adminforth/dist/spa/node_modules/*"
11
- "../../../spa/node_modules/*"
11
+ "../../../adminforth/spa/node_modules/*"
12
12
  ],
13
13
  "@@/*": [
14
14
  // "node_modules/adminforth/dist/spa/src/*"
15
15
  "."
16
16
  ]
17
17
  }
18
- }
18
+ },
19
+ "include": [
20
+ "./**/*.ts",
21
+ "./**/*.tsx",
22
+ "./**/*.vue",
23
+ "../**/*.ts",
24
+ "../**/*.tsx",
25
+ "../**/*.vue",
26
+ "../*.vue",
27
+ "../*.ts",
28
+ "../*.tsx"
29
+ ]
19
30
  }
package/dist/index.js CHANGED
@@ -13,13 +13,15 @@ import { RateLimiter } from "adminforth";
13
13
  const ADMINFORTH_NOT_YET_USED_TAG = 'adminforth-candidate-for-cleanup';
14
14
  export default class UploadPlugin extends AdminForthPlugin {
15
15
  constructor(options) {
16
- var _a;
16
+ var _a, _b, _c;
17
17
  super(options, import.meta.url);
18
18
  this.options = options;
19
19
  // for calcualting average time
20
20
  this.totalCalls = 0;
21
21
  this.totalDuration = 0;
22
- this.rateLimiter = new RateLimiter((_a = this.options.generation.rateLimit) === null || _a === void 0 ? void 0 : _a.limit);
22
+ if ((_b = (_a = this.options.generation) === null || _a === void 0 ? void 0 : _a.rateLimit) === null || _b === void 0 ? void 0 : _b.limit) {
23
+ this.rateLimiter = new RateLimiter((_c = this.options.generation.rateLimit) === null || _c === void 0 ? void 0 : _c.limit);
24
+ }
23
25
  }
24
26
  instanceUniqueRepresentation(pluginOptions) {
25
27
  return `${pluginOptions.pathColumnName}`;
@@ -84,27 +86,6 @@ export default class UploadPlugin extends AdminForthPlugin {
84
86
  };
85
87
  // define components which will be imported from other components
86
88
  this.componentPath('imageGenerator.vue');
87
- const virtualColumn = {
88
- virtual: true,
89
- name: `uploader_${this.pluginInstanceId}`,
90
- components: {
91
- edit: {
92
- file: this.componentPath('uploader.vue'),
93
- meta: pluginFrontendOptions,
94
- },
95
- create: {
96
- file: this.componentPath('uploader.vue'),
97
- meta: pluginFrontendOptions,
98
- },
99
- },
100
- showIn: {
101
- create: true,
102
- edit: true,
103
- list: false,
104
- show: false,
105
- filter: false,
106
- },
107
- };
108
89
  if (!resourceConfig.columns[pathColumnIndex].components) {
109
90
  resourceConfig.columns[pathColumnIndex].components = {};
110
91
  }
@@ -120,32 +101,15 @@ export default class UploadPlugin extends AdminForthPlugin {
120
101
  meta: pluginFrontendOptions,
121
102
  };
122
103
  }
123
- // insert virtual column after path column if it is not already there
124
- const virtualColumnIndex = resourceConfig.columns.findIndex((column) => column.name === virtualColumn.name);
125
- if (virtualColumnIndex === -1) {
126
- resourceConfig.columns.splice(pathColumnIndex + 1, 0, virtualColumn);
127
- }
128
- // if showIn of path column has 'create' or 'edit' remove it but use it for virtual column
129
- if (pathColumn.showIn && (pathColumn.showIn.create !== undefined)) {
130
- virtualColumn.showIn = Object.assign(Object.assign({}, virtualColumn.showIn), { create: pathColumn.showIn.create });
131
- pathColumn.showIn = Object.assign(Object.assign({}, pathColumn.showIn), { create: false });
132
- }
133
- if (pathColumn.showIn && (pathColumn.showIn.edit !== undefined)) {
134
- virtualColumn.showIn = Object.assign(Object.assign({}, virtualColumn.showIn), { edit: pathColumn.showIn.edit });
135
- pathColumn.showIn = Object.assign(Object.assign({}, pathColumn.showIn), { edit: false });
136
- }
137
- virtualColumn.required = pathColumn.required;
138
- virtualColumn.label = pathColumn.label;
139
- virtualColumn.editingNote = pathColumn.editingNote;
104
+ resourceConfig.columns[pathColumnIndex].components.create = {
105
+ file: this.componentPath('uploader.vue'),
106
+ meta: pluginFrontendOptions,
107
+ };
108
+ resourceConfig.columns[pathColumnIndex].components.edit = {
109
+ file: this.componentPath('uploader.vue'),
110
+ meta: pluginFrontendOptions,
111
+ };
140
112
  // ** HOOKS FOR CREATE **//
141
- // add beforeSave hook to save virtual column to path column
142
- resourceConfig.hooks.create.beforeSave.push((_a) => __awaiter(this, [_a], void 0, function* ({ record }) {
143
- if (record[virtualColumn.name]) {
144
- record[pathColumnName] = record[virtualColumn.name];
145
- delete record[virtualColumn.name];
146
- }
147
- return { ok: true };
148
- }));
149
113
  // in afterSave hook, aremove tag adminforth-not-yet-used from the file
150
114
  resourceConfig.hooks.create.afterSave.push((_a) => __awaiter(this, [_a], void 0, function* ({ record }) {
151
115
  process.env.HEAVY_DEBUG && console.log('💾💾 after save ', record === null || record === void 0 ? void 0 : record.id);
@@ -196,17 +160,9 @@ export default class UploadPlugin extends AdminForthPlugin {
196
160
  return { ok: true };
197
161
  }));
198
162
  // ** HOOKS FOR EDIT **//
199
- // beforeSave
200
- resourceConfig.hooks.edit.beforeSave.push((_a) => __awaiter(this, [_a], void 0, function* ({ record }) {
201
- // null is when value is removed
202
- if (record[virtualColumn.name] || record[virtualColumn.name] === null) {
203
- record[pathColumnName] = record[virtualColumn.name];
204
- }
205
- return { ok: true };
206
- }));
207
163
  // add edit postSave hook to delete old file and remove tag from new file
208
164
  resourceConfig.hooks.edit.afterSave.push((_a) => __awaiter(this, [_a], void 0, function* ({ updates, oldRecord }) {
209
- if (updates[virtualColumn.name] || updates[virtualColumn.name] === null) {
165
+ if (updates[pathColumnName] || updates[pathColumnName] === null) {
210
166
  if (oldRecord[pathColumnName]) {
211
167
  // put tag to delete old file
212
168
  try {
@@ -217,7 +173,7 @@ export default class UploadPlugin extends AdminForthPlugin {
217
173
  console.error(`Error setting tag ${ADMINFORTH_NOT_YET_USED_TAG} to true for object ${oldRecord[pathColumnName]}. File will not be auto-cleaned up`, e);
218
174
  }
219
175
  }
220
- if (updates[virtualColumn.name] !== null) {
176
+ if (updates[pathColumnName] !== null) {
221
177
  // remove tag from new file
222
178
  // in this case we let it crash if it fails: this is a new file which just was uploaded.
223
179
  yield this.options.storageAdapter.markKeyForNotDeletation(updates[pathColumnName]);
@@ -305,9 +261,9 @@ export default class UploadPlugin extends AdminForthPlugin {
305
261
  method: 'POST',
306
262
  path: `/plugin/${this.pluginInstanceId}/generate_images`,
307
263
  handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body, adminUser, headers }) {
308
- var _b, _c;
264
+ var _b;
309
265
  const { prompt, recordId } = body;
310
- if ((_b = this.options.generation.rateLimit) === null || _b === void 0 ? void 0 : _b.limit) {
266
+ if (this.rateLimiter) {
311
267
  // rate limit
312
268
  // const { error } = RateLimiter.checkRateLimit(
313
269
  // this.pluginInstanceId,
@@ -322,7 +278,7 @@ export default class UploadPlugin extends AdminForthPlugin {
322
278
  if (this.options.generation.attachFiles) {
323
279
  // TODO - does it require additional allowed action to check this record id has access to get the image?
324
280
  // or should we mention in docs that user should do validation in method itself
325
- const record = yield this.adminforth.resource(this.resourceConfig.resourceId).get([Filters.EQ((_c = this.resourceConfig.columns.find((column) => column.primaryKey)) === null || _c === void 0 ? void 0 : _c.name, recordId)]);
281
+ const record = yield this.adminforth.resource(this.resourceConfig.resourceId).get([Filters.EQ((_b = this.resourceConfig.columns.find((column) => column.primaryKey)) === null || _b === void 0 ? void 0 : _b.name, recordId)]);
326
282
  if (!record) {
327
283
  return { error: `Record with id ${recordId} not found` };
328
284
  }
package/index.ts CHANGED
@@ -25,7 +25,9 @@ export default class UploadPlugin extends AdminForthPlugin {
25
25
  // for calcualting average time
26
26
  this.totalCalls = 0;
27
27
  this.totalDuration = 0;
28
+ if (this.options.generation?.rateLimit?.limit) {
28
29
  this.rateLimiter = new RateLimiter(this.options.generation.rateLimit?.limit)
30
+ }
29
31
  }
30
32
 
31
33
  instanceUniqueRepresentation(pluginOptions: any) : string {
@@ -88,28 +90,6 @@ export default class UploadPlugin extends AdminForthPlugin {
88
90
  // define components which will be imported from other components
89
91
  this.componentPath('imageGenerator.vue');
90
92
 
91
- const virtualColumn: AdminForthResourceColumn = {
92
- virtual: true,
93
- name: `uploader_${this.pluginInstanceId}`,
94
- components: {
95
- edit: {
96
- file: this.componentPath('uploader.vue'),
97
- meta: pluginFrontendOptions,
98
- },
99
- create: {
100
- file: this.componentPath('uploader.vue'),
101
- meta: pluginFrontendOptions,
102
- },
103
- },
104
- showIn: {
105
- create: true,
106
- edit: true,
107
- list: false,
108
- show: false,
109
- filter: false,
110
- },
111
- };
112
-
113
93
  if (!resourceConfig.columns[pathColumnIndex].components) {
114
94
  resourceConfig.columns[pathColumnIndex].components = {};
115
95
  }
@@ -128,38 +108,18 @@ export default class UploadPlugin extends AdminForthPlugin {
128
108
  };
129
109
  }
130
110
 
131
- // insert virtual column after path column if it is not already there
132
- const virtualColumnIndex = resourceConfig.columns.findIndex((column: any) => column.name === virtualColumn.name);
133
- if (virtualColumnIndex === -1) {
134
- resourceConfig.columns.splice(pathColumnIndex + 1, 0, virtualColumn);
135
- }
136
-
137
-
138
- // if showIn of path column has 'create' or 'edit' remove it but use it for virtual column
139
- if (pathColumn.showIn && (pathColumn.showIn.create !== undefined)) {
140
- virtualColumn.showIn = { ...virtualColumn.showIn, create: pathColumn.showIn.create };
141
- pathColumn.showIn = { ...pathColumn.showIn, create: false };
111
+ resourceConfig.columns[pathColumnIndex].components.create = {
112
+ file: this.componentPath('uploader.vue'),
113
+ meta: pluginFrontendOptions,
142
114
  }
143
115
 
144
- if (pathColumn.showIn && (pathColumn.showIn.edit !== undefined)) {
145
- virtualColumn.showIn = { ...virtualColumn.showIn, edit: pathColumn.showIn.edit };
146
- pathColumn.showIn = { ...pathColumn.showIn, edit: false };
116
+ resourceConfig.columns[pathColumnIndex].components.edit = {
117
+ file: this.componentPath('uploader.vue'),
118
+ meta: pluginFrontendOptions,
147
119
  }
148
120
 
149
- virtualColumn.required = pathColumn.required;
150
- virtualColumn.label = pathColumn.label;
151
- virtualColumn.editingNote = pathColumn.editingNote;
152
-
153
121
  // ** HOOKS FOR CREATE **//
154
122
 
155
- // add beforeSave hook to save virtual column to path column
156
- resourceConfig.hooks.create.beforeSave.push(async ({ record }: { record: any }) => {
157
- if (record[virtualColumn.name]) {
158
- record[pathColumnName] = record[virtualColumn.name];
159
- delete record[virtualColumn.name];
160
- }
161
- return { ok: true };
162
- });
163
123
 
164
124
  // in afterSave hook, aremove tag adminforth-not-yet-used from the file
165
125
  resourceConfig.hooks.create.afterSave.push(async ({ record }: { record: any }) => {
@@ -223,20 +183,12 @@ export default class UploadPlugin extends AdminForthPlugin {
223
183
 
224
184
  // ** HOOKS FOR EDIT **//
225
185
 
226
- // beforeSave
227
- resourceConfig.hooks.edit.beforeSave.push(async ({ record }: { record: any }) => {
228
- // null is when value is removed
229
- if (record[virtualColumn.name] || record[virtualColumn.name] === null) {
230
- record[pathColumnName] = record[virtualColumn.name];
231
- }
232
- return { ok: true };
233
- })
234
186
 
235
187
 
236
188
  // add edit postSave hook to delete old file and remove tag from new file
237
189
  resourceConfig.hooks.edit.afterSave.push(async ({ updates, oldRecord }: { updates: any, oldRecord: any }) => {
238
190
 
239
- if (updates[virtualColumn.name] || updates[virtualColumn.name] === null) {
191
+ if (updates[pathColumnName] || updates[pathColumnName] === null) {
240
192
  if (oldRecord[pathColumnName]) {
241
193
  // put tag to delete old file
242
194
  try {
@@ -246,7 +198,7 @@ export default class UploadPlugin extends AdminForthPlugin {
246
198
  console.error(`Error setting tag ${ADMINFORTH_NOT_YET_USED_TAG} to true for object ${oldRecord[pathColumnName]}. File will not be auto-cleaned up`, e);
247
199
  }
248
200
  }
249
- if (updates[virtualColumn.name] !== null) {
201
+ if (updates[pathColumnName] !== null) {
250
202
  // remove tag from new file
251
203
  // in this case we let it crash if it fails: this is a new file which just was uploaded.
252
204
  await this.options.storageAdapter.markKeyForNotDeletation(updates[pathColumnName]);
@@ -347,7 +299,7 @@ export default class UploadPlugin extends AdminForthPlugin {
347
299
  path: `/plugin/${this.pluginInstanceId}/generate_images`,
348
300
  handler: async ({ body, adminUser, headers }) => {
349
301
  const { prompt, recordId } = body;
350
- if (this.options.generation.rateLimit?.limit) {
302
+ if (this.rateLimiter) {
351
303
  // rate limit
352
304
  // const { error } = RateLimiter.checkRateLimit(
353
305
  // this.pluginInstanceId,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adminforth/upload",
3
- "version": "2.3.5",
3
+ "version": "2.3.7",
4
4
  "description": "Plugin for uploading files for adminforth",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",