@adminforth/rich-editor 1.0.14 → 1.0.16
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/dist/index.js +8 -6
- package/index.ts +10 -6
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -7,7 +7,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import { AdminForthPlugin, Filters,
|
|
10
|
+
import { AdminForthPlugin, Filters, RateLimiter } from "adminforth";
|
|
11
11
|
import * as cheerio from 'cheerio';
|
|
12
12
|
// options:
|
|
13
13
|
// attachments: {
|
|
@@ -31,6 +31,7 @@ export default class RichEditorPlugin extends AdminForthPlugin {
|
|
|
31
31
|
return __awaiter(this, void 0, void 0, function* () {
|
|
32
32
|
var _a, _b, _c;
|
|
33
33
|
_super.modifyResourceConfig.call(this, adminforth, resourceConfig);
|
|
34
|
+
this.adminforth = adminforth;
|
|
34
35
|
const c = resourceConfig.columns.find(c => c.name === this.options.htmlFieldName);
|
|
35
36
|
if (!c) {
|
|
36
37
|
throw new Error(`Column ${this.options.htmlFieldName} not found in resource ${resourceConfig.label}`);
|
|
@@ -127,11 +128,11 @@ export default class RichEditorPlugin extends AdminForthPlugin {
|
|
|
127
128
|
// field was not changed, do nothing
|
|
128
129
|
return { ok: true };
|
|
129
130
|
}
|
|
130
|
-
const
|
|
131
|
+
const existingAparts = yield adminforth.resource(this.options.attachments.attachmentResource).list([
|
|
131
132
|
Filters.EQ(this.options.attachments.attachmentRecordIdFieldName, recordId),
|
|
132
133
|
Filters.EQ(this.options.attachments.attachmentResourceIdFieldName, resourceConfig.resourceId)
|
|
133
134
|
]);
|
|
134
|
-
const existingS3Paths =
|
|
135
|
+
const existingS3Paths = existingAparts.map((a) => a[this.options.attachments.attachmentFieldName]);
|
|
135
136
|
const newS3Paths = getAttachmentPathes(record[this.options.htmlFieldName]);
|
|
136
137
|
process.env.HEAVY_DEBUG && console.log('📸 Existing s3Paths (from db)', existingS3Paths);
|
|
137
138
|
process.env.HEAVY_DEBUG && console.log('📸 Found new s3Paths (from text)', newS3Paths);
|
|
@@ -147,11 +148,11 @@ export default class RichEditorPlugin extends AdminForthPlugin {
|
|
|
147
148
|
}));
|
|
148
149
|
// after delete we need to delete all attachments
|
|
149
150
|
resourceConfig.hooks.delete.afterSave.push((_f) => __awaiter(this, [_f], void 0, function* ({ record, adminUser }) {
|
|
150
|
-
const
|
|
151
|
+
const existingAparts = yield adminforth.resource(this.options.attachments.attachmentResource).list([
|
|
151
152
|
Filters.EQ(this.options.attachments.attachmentRecordIdFieldName, record[editorRecordPkField.name]),
|
|
152
153
|
Filters.EQ(this.options.attachments.attachmentResourceIdFieldName, resourceConfig.resourceId)
|
|
153
154
|
]);
|
|
154
|
-
const existingS3Paths =
|
|
155
|
+
const existingS3Paths = existingAparts.map((a) => a[this.options.attachments.attachmentFieldName]);
|
|
155
156
|
process.env.HEAVY_DEBUG && console.log('📸 Found s3Paths to delete', existingS3Paths);
|
|
156
157
|
yield deleteAttachmentRecords(adminforth, this.options, existingS3Paths, adminUser);
|
|
157
158
|
return { ok: true };
|
|
@@ -160,6 +161,7 @@ export default class RichEditorPlugin extends AdminForthPlugin {
|
|
|
160
161
|
});
|
|
161
162
|
}
|
|
162
163
|
validateConfigAfterDiscover(adminforth, resourceConfig) {
|
|
164
|
+
this.adminforth = adminforth;
|
|
163
165
|
// optional method where you can safely check field types after database discovery was performed
|
|
164
166
|
if (this.options.completion && this.options.completion.provider !== 'openai-chat-gpt') {
|
|
165
167
|
throw new Error(`Invalid provider ${this.options.completion.provider}`);
|
|
@@ -208,7 +210,7 @@ export default class RichEditorPlugin extends AdminForthPlugin {
|
|
|
208
210
|
const { record } = body;
|
|
209
211
|
if ((_b = this.options.completion.rateLimit) === null || _b === void 0 ? void 0 : _b.limit) {
|
|
210
212
|
// rate limit
|
|
211
|
-
const { error } = RateLimiter.checkRateLimit(this.pluginInstanceId, (_c = this.options.completion.rateLimit) === null || _c === void 0 ? void 0 : _c.limit,
|
|
213
|
+
const { error } = RateLimiter.checkRateLimit(this.pluginInstanceId, (_c = this.options.completion.rateLimit) === null || _c === void 0 ? void 0 : _c.limit, this.adminforth.auth.getClientIp(headers));
|
|
212
214
|
if (error) {
|
|
213
215
|
return {
|
|
214
216
|
completion: [],
|
package/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
|
|
2
2
|
import type { IAdminForth, IHttpServer, AdminForthResource, AdminUser, AfterSaveFunction } from "adminforth";
|
|
3
3
|
import type { PluginOptions } from './types.js';
|
|
4
|
-
import { AdminForthPlugin, Filters,
|
|
4
|
+
import { AdminForthPlugin, Filters, RateLimiter } from "adminforth";
|
|
5
5
|
import * as cheerio from 'cheerio';
|
|
6
6
|
|
|
7
7
|
|
|
@@ -22,6 +22,8 @@ export default class RichEditorPlugin extends AdminForthPlugin {
|
|
|
22
22
|
activationOrder: number = 100000;
|
|
23
23
|
|
|
24
24
|
attachmentResource: AdminForthResource = undefined;
|
|
25
|
+
|
|
26
|
+
adminforth: IAdminForth;
|
|
25
27
|
|
|
26
28
|
constructor(options: PluginOptions) {
|
|
27
29
|
super(options, import.meta.url);
|
|
@@ -30,6 +32,7 @@ export default class RichEditorPlugin extends AdminForthPlugin {
|
|
|
30
32
|
|
|
31
33
|
async modifyResourceConfig(adminforth: IAdminForth, resourceConfig: AdminForthResource) {
|
|
32
34
|
super.modifyResourceConfig(adminforth, resourceConfig);
|
|
35
|
+
this.adminforth = adminforth;
|
|
33
36
|
|
|
34
37
|
const c = resourceConfig.columns.find(c => c.name === this.options.htmlFieldName);
|
|
35
38
|
if (!c) {
|
|
@@ -161,11 +164,11 @@ export default class RichEditorPlugin extends AdminForthPlugin {
|
|
|
161
164
|
// field was not changed, do nothing
|
|
162
165
|
return { ok: true };
|
|
163
166
|
}
|
|
164
|
-
const
|
|
167
|
+
const existingAparts = await adminforth.resource(this.options.attachments.attachmentResource).list([
|
|
165
168
|
Filters.EQ(this.options.attachments.attachmentRecordIdFieldName, recordId),
|
|
166
169
|
Filters.EQ(this.options.attachments.attachmentResourceIdFieldName, resourceConfig.resourceId)
|
|
167
170
|
]);
|
|
168
|
-
const existingS3Paths =
|
|
171
|
+
const existingS3Paths = existingAparts.map((a: any) => a[this.options.attachments.attachmentFieldName]);
|
|
169
172
|
const newS3Paths = getAttachmentPathes(record[this.options.htmlFieldName]);
|
|
170
173
|
process.env.HEAVY_DEBUG && console.log('📸 Existing s3Paths (from db)', existingS3Paths)
|
|
171
174
|
process.env.HEAVY_DEBUG && console.log('📸 Found new s3Paths (from text)', newS3Paths);
|
|
@@ -187,13 +190,13 @@ export default class RichEditorPlugin extends AdminForthPlugin {
|
|
|
187
190
|
// after delete we need to delete all attachments
|
|
188
191
|
(resourceConfig.hooks.delete.afterSave as Array<AfterSaveFunction>).push(
|
|
189
192
|
async ({ record, adminUser }: { record: any, adminUser: AdminUser }) => {
|
|
190
|
-
const
|
|
193
|
+
const existingAparts = await adminforth.resource(this.options.attachments.attachmentResource).list(
|
|
191
194
|
[
|
|
192
195
|
Filters.EQ(this.options.attachments.attachmentRecordIdFieldName, record[editorRecordPkField.name]),
|
|
193
196
|
Filters.EQ(this.options.attachments.attachmentResourceIdFieldName, resourceConfig.resourceId)
|
|
194
197
|
]
|
|
195
198
|
);
|
|
196
|
-
const existingS3Paths =
|
|
199
|
+
const existingS3Paths = existingAparts.map((a: any) => a[this.options.attachments.attachmentFieldName]);
|
|
197
200
|
process.env.HEAVY_DEBUG && console.log('📸 Found s3Paths to delete', existingS3Paths);
|
|
198
201
|
await deleteAttachmentRecords(adminforth, this.options, existingS3Paths, adminUser);
|
|
199
202
|
|
|
@@ -204,6 +207,7 @@ export default class RichEditorPlugin extends AdminForthPlugin {
|
|
|
204
207
|
}
|
|
205
208
|
|
|
206
209
|
validateConfigAfterDiscover(adminforth: IAdminForth, resourceConfig: AdminForthResource) {
|
|
210
|
+
this.adminforth = adminforth;
|
|
207
211
|
// optional method where you can safely check field types after database discovery was performed
|
|
208
212
|
if (this.options.completion && this.options.completion.provider !== 'openai-chat-gpt') {
|
|
209
213
|
throw new Error(`Invalid provider ${this.options.completion.provider}`);
|
|
@@ -265,7 +269,7 @@ export default class RichEditorPlugin extends AdminForthPlugin {
|
|
|
265
269
|
const { error } = RateLimiter.checkRateLimit(
|
|
266
270
|
this.pluginInstanceId,
|
|
267
271
|
this.options.completion.rateLimit?.limit,
|
|
268
|
-
|
|
272
|
+
this.adminforth.auth.getClientIp(headers),
|
|
269
273
|
);
|
|
270
274
|
if (error) {
|
|
271
275
|
return {
|