@adminforth/upload 2.3.4 → 2.3.6
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 +1 -1
- package/dist/index.js +8 -5
- package/index.ts +7 -3
- package/package.json +1 -1
package/build.log
CHANGED
package/dist/index.js
CHANGED
|
@@ -13,11 +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, _b, _c;
|
|
16
17
|
super(options, import.meta.url);
|
|
17
18
|
this.options = options;
|
|
18
19
|
// for calcualting average time
|
|
19
20
|
this.totalCalls = 0;
|
|
20
21
|
this.totalDuration = 0;
|
|
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
|
+
}
|
|
21
25
|
}
|
|
22
26
|
instanceUniqueRepresentation(pluginOptions) {
|
|
23
27
|
return `${pluginOptions.pathColumnName}`;
|
|
@@ -303,17 +307,16 @@ export default class UploadPlugin extends AdminForthPlugin {
|
|
|
303
307
|
method: 'POST',
|
|
304
308
|
path: `/plugin/${this.pluginInstanceId}/generate_images`,
|
|
305
309
|
handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body, adminUser, headers }) {
|
|
306
|
-
var _b
|
|
310
|
+
var _b;
|
|
307
311
|
const { prompt, recordId } = body;
|
|
308
|
-
if (
|
|
312
|
+
if (this.rateLimiter) {
|
|
309
313
|
// rate limit
|
|
310
314
|
// const { error } = RateLimiter.checkRateLimit(
|
|
311
315
|
// this.pluginInstanceId,
|
|
312
316
|
// this.options.generation.rateLimit?.limit,
|
|
313
317
|
// this.adminforth.auth.getClientIp(headers),
|
|
314
318
|
// );
|
|
315
|
-
|
|
316
|
-
if (!rateLimiter.consume(`${this.pluginInstanceId}-${this.adminforth.auth.getClientIp(headers)}`)) {
|
|
319
|
+
if (!(yield this.rateLimiter.consume(`${this.pluginInstanceId}-${this.adminforth.auth.getClientIp(headers)}`))) {
|
|
317
320
|
return { error: this.options.generation.rateLimit.errorMessage };
|
|
318
321
|
}
|
|
319
322
|
}
|
|
@@ -321,7 +324,7 @@ export default class UploadPlugin extends AdminForthPlugin {
|
|
|
321
324
|
if (this.options.generation.attachFiles) {
|
|
322
325
|
// TODO - does it require additional allowed action to check this record id has access to get the image?
|
|
323
326
|
// or should we mention in docs that user should do validation in method itself
|
|
324
|
-
const record = yield this.adminforth.resource(this.resourceConfig.resourceId).get([Filters.EQ((
|
|
327
|
+
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)]);
|
|
325
328
|
if (!record) {
|
|
326
329
|
return { error: `Record with id ${recordId} not found` };
|
|
327
330
|
}
|
package/index.ts
CHANGED
|
@@ -16,6 +16,8 @@ export default class UploadPlugin extends AdminForthPlugin {
|
|
|
16
16
|
|
|
17
17
|
resourceConfig: AdminForthResource;
|
|
18
18
|
|
|
19
|
+
rateLimiter: RateLimiter;
|
|
20
|
+
|
|
19
21
|
constructor(options: PluginOptions) {
|
|
20
22
|
super(options, import.meta.url);
|
|
21
23
|
this.options = options;
|
|
@@ -23,6 +25,9 @@ export default class UploadPlugin extends AdminForthPlugin {
|
|
|
23
25
|
// for calcualting average time
|
|
24
26
|
this.totalCalls = 0;
|
|
25
27
|
this.totalDuration = 0;
|
|
28
|
+
if (this.options.generation?.rateLimit?.limit) {
|
|
29
|
+
this.rateLimiter = new RateLimiter(this.options.generation.rateLimit?.limit)
|
|
30
|
+
}
|
|
26
31
|
}
|
|
27
32
|
|
|
28
33
|
instanceUniqueRepresentation(pluginOptions: any) : string {
|
|
@@ -344,15 +349,14 @@ export default class UploadPlugin extends AdminForthPlugin {
|
|
|
344
349
|
path: `/plugin/${this.pluginInstanceId}/generate_images`,
|
|
345
350
|
handler: async ({ body, adminUser, headers }) => {
|
|
346
351
|
const { prompt, recordId } = body;
|
|
347
|
-
if (this.
|
|
352
|
+
if (this.rateLimiter) {
|
|
348
353
|
// rate limit
|
|
349
354
|
// const { error } = RateLimiter.checkRateLimit(
|
|
350
355
|
// this.pluginInstanceId,
|
|
351
356
|
// this.options.generation.rateLimit?.limit,
|
|
352
357
|
// this.adminforth.auth.getClientIp(headers),
|
|
353
358
|
// );
|
|
354
|
-
|
|
355
|
-
if (!rateLimiter.consume(`${this.pluginInstanceId}-${this.adminforth.auth.getClientIp(headers)}`)) {
|
|
359
|
+
if (!await this.rateLimiter.consume(`${this.pluginInstanceId}-${this.adminforth.auth.getClientIp(headers)}`)) {
|
|
356
360
|
return { error: this.options.generation.rateLimit.errorMessage };
|
|
357
361
|
}
|
|
358
362
|
}
|