@adminforth/rich-editor 2.0.18 → 2.1.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/build.log CHANGED
@@ -10,5 +10,5 @@ custom/package.json
10
10
  custom/quillEditor.vue
11
11
  custom/tsconfig.json
12
12
 
13
- sent 35,535 bytes received 115 bytes 71,300.00 bytes/sec
14
- total size is 35,118 speedup is 0.99
13
+ sent 35,541 bytes received 115 bytes 71,312.00 bytes/sec
14
+ total size is 35,118 speedup is 0.98
package/dist/index.d.ts CHANGED
@@ -14,5 +14,6 @@ export default class RichEditorPlugin extends AdminForthPlugin {
14
14
  validateConfigAfterDiscover(adminforth: IAdminForth, resourceConfig: AdminForthResource): void;
15
15
  instanceUniqueRepresentation(pluginOptions: any): string;
16
16
  generateRecordContext(record: any, maxFields: number, inputFieldLimit: number, partsCount: number): string;
17
+ private parseBody;
17
18
  setupEndpoints(server: IHttpServer): void;
18
19
  }
package/dist/index.js CHANGED
@@ -9,6 +9,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  };
10
10
  import { AdminForthPlugin, Filters, RateLimiter } from "adminforth";
11
11
  import * as cheerio from 'cheerio';
12
+ import { z } from "zod";
13
+ const doCompleteBodySchema = z.object({
14
+ record: z.record(z.string(), z.unknown()),
15
+ }).strict();
12
16
  // options:
13
17
  // attachments: {
14
18
  // attachmentResource: 'description_images',
@@ -214,13 +218,28 @@ export default class RichEditorPlugin extends AdminForthPlugin {
214
218
  });
215
219
  return JSON.stringify(Object.fromEntries(fields));
216
220
  }
221
+ parseBody(schema, body, response) {
222
+ const parsed = schema.safeParse(body !== null && body !== void 0 ? body : {});
223
+ if (!parsed.success) {
224
+ response.setStatus(400, '');
225
+ return {
226
+ ok: false,
227
+ error: { error: 'Request body validation failed', details: parsed.error.issues },
228
+ };
229
+ }
230
+ return { ok: true, data: parsed.data };
231
+ }
217
232
  setupEndpoints(server) {
218
233
  server.endpoint({
219
234
  method: 'POST',
220
235
  path: `/plugin/${this.pluginInstanceId}/doComplete`,
221
- handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body, headers }) {
236
+ handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body, headers, response }) {
222
237
  var _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
223
- const { record } = body;
238
+ const parsed = this.parseBody(doCompleteBodySchema, body, response);
239
+ if ('error' in parsed)
240
+ return parsed.error;
241
+ const data = parsed.data;
242
+ const { record } = data;
224
243
  if (!record) {
225
244
  return { completion: [] };
226
245
  }
@@ -265,6 +284,11 @@ export default class RichEditorPlugin extends AdminForthPlugin {
265
284
  maxTokens: (_o = (_m = (_l = this.options.completion) === null || _l === void 0 ? void 0 : _l.expert) === null || _m === void 0 ? void 0 : _m.maxTokens) !== null && _o !== void 0 ? _o : 50,
266
285
  });
267
286
  let suggestion = respContent;
287
+ if (!suggestion) {
288
+ return {
289
+ completion: []
290
+ };
291
+ }
268
292
  if (suggestion.startsWith(currentVal)) {
269
293
  suggestion = suggestion.slice(currentVal.length);
270
294
  }
package/index.ts CHANGED
@@ -3,6 +3,11 @@ import type { IAdminForth, IHttpServer, AdminForthResource, AdminUser, IAdminFor
3
3
  import type { PluginOptions } from './types.js';
4
4
  import { AdminForthPlugin, Filters, RateLimiter } from "adminforth";
5
5
  import * as cheerio from 'cheerio';
6
+ import { z } from "zod";
7
+
8
+ const doCompleteBodySchema = z.object({
9
+ record: z.record(z.string(), z.unknown()),
10
+ }).strict();
6
11
 
7
12
 
8
13
  // options:
@@ -270,12 +275,31 @@ export default class RichEditorPlugin extends AdminForthPlugin {
270
275
  return JSON.stringify(Object.fromEntries(fields));
271
276
  }
272
277
 
278
+ private parseBody<T>(
279
+ schema: z.ZodType<T>,
280
+ body: unknown,
281
+ response: { setStatus: (code: number, message: string) => void },
282
+ ): { ok: true; data: T } | { ok: false; error: { error: string; details: unknown } } {
283
+ const parsed = schema.safeParse(body ?? {});
284
+ if (!parsed.success) {
285
+ response.setStatus(400, '');
286
+ return {
287
+ ok: false,
288
+ error: { error: 'Request body validation failed', details: parsed.error.issues },
289
+ };
290
+ }
291
+ return { ok: true, data: parsed.data };
292
+ }
293
+
273
294
  setupEndpoints(server: IHttpServer) {
274
295
  server.endpoint({
275
296
  method: 'POST',
276
297
  path: `/plugin/${this.pluginInstanceId}/doComplete`,
277
- handler: async ({ body, headers }) => {
278
- const { record } = body;
298
+ handler: async ({ body, headers, response }) => {
299
+ const parsed = this.parseBody(doCompleteBodySchema, body, response);
300
+ if ('error' in parsed) return parsed.error;
301
+ const data = parsed.data;
302
+ const { record } = data;
279
303
 
280
304
  if (!record) {
281
305
  return { completion: [] };
@@ -333,6 +357,11 @@ export default class RichEditorPlugin extends AdminForthPlugin {
333
357
  maxTokens: this.options.completion?.expert?.maxTokens ?? 50,
334
358
  });
335
359
  let suggestion = respContent
360
+ if (!suggestion) {
361
+ return {
362
+ completion: []
363
+ };
364
+ }
336
365
  if (suggestion.startsWith(currentVal)) {
337
366
  suggestion = suggestion.slice(currentVal.length);
338
367
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adminforth/rich-editor",
3
- "version": "2.0.18",
3
+ "version": "2.1.1",
4
4
  "description": "Rich editor plugin for adminforth",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -23,14 +23,15 @@
23
23
  "author": "DevForth (https://devforth.io)",
24
24
  "license": "MIT",
25
25
  "dependencies": {
26
- "cheerio": "^1.0.0"
26
+ "cheerio": "^1.0.0",
27
+ "zod": "^4.3.6"
27
28
  },
28
29
  "peerDependencies": {
29
- "adminforth": "^3.6.21"
30
+ "adminforth": "^3.7.1"
30
31
  },
31
32
  "devDependencies": {
32
33
  "@types/node": "^22.10.7",
33
- "adminforth": "^3.6.21",
34
+ "adminforth": "^3.7.1",
34
35
  "semantic-release": "^24.2.1",
35
36
  "semantic-release-slack-bot": "^4.0.2",
36
37
  "typescript": "^5.7.3"