Skip to content

Commit bcfc83d

Browse files
committed
refactor: import parseBody from adminforth instead of redefining it
Use the shared parseBody helper exported from adminforth core rather than duplicating the same function in every plugin. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> AdminForth/1777/image
1 parent 081a6f4 commit bcfc83d

1 file changed

Lines changed: 4 additions & 20 deletions

File tree

index.ts

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import AdminForth, { AdminForthPlugin, Filters, suggestIfTypo, AdminForthDataTypes, RAMLock, filtersTools, AdminForthFilterOperators } from "adminforth";
1+
import AdminForth, { AdminForthPlugin, parseBody, Filters, suggestIfTypo, AdminForthDataTypes, RAMLock, filtersTools, AdminForthFilterOperators } from "adminforth";
22
import type { IAdminForth, IHttpServer, AdminForthComponentDeclaration, AdminForthResourceColumn, AdminForthResource, BeforeLoginConfirmationFunction, AdminForthConfigMenuItem, AdminUser } from "adminforth";
33
import type { PluginOptions, SupportedLanguage } from './types.js';
44
import { z } from "zod";
@@ -1250,22 +1250,6 @@ export default class I18nPlugin extends AdminForthPlugin {
12501250
}
12511251

12521252

1253-
private parseBody<T>(
1254-
schema: z.ZodType<T>,
1255-
body: unknown,
1256-
response: { setStatus: (code: number, message: string) => void },
1257-
): { ok: true; data: T } | { ok: false; error: { error: string; details: unknown } } {
1258-
const parsed = schema.safeParse(body ?? {});
1259-
if (!parsed.success) {
1260-
response.setStatus(400, '');
1261-
return {
1262-
ok: false,
1263-
error: { error: 'Request body validation failed', details: parsed.error.issues },
1264-
};
1265-
}
1266-
return { ok: true, data: parsed.data };
1267-
}
1268-
12691253
setupEndpoints(server: IHttpServer) {
12701254
server.endpoint({
12711255
method: 'GET',
@@ -1287,7 +1271,7 @@ export default class I18nPlugin extends AdminForthPlugin {
12871271
method: 'POST',
12881272
path: `/plugin/${this.pluginInstanceId}/update-field`,
12891273
handler: async ({ body, adminUser, headers, response }) => {
1290-
const parsed = this.parseBody(updateFieldBodySchema, body, response);
1274+
const parsed = parseBody(updateFieldBodySchema, body, response);
12911275
if ('error' in parsed) return parsed.error;
12921276
const data = parsed.data;
12931277
const { resourceId, recordId, field, value, reviewed } = data;
@@ -1353,7 +1337,7 @@ export default class I18nPlugin extends AdminForthPlugin {
13531337
path: `/plugin/${this.pluginInstanceId}/translate-selected-to-languages`,
13541338
noAuth: false,
13551339
handler: async ({ body, tr, adminUser, response }) => {
1356-
const parsed = this.parseBody(translateSelectedBodySchema, body, response);
1340+
const parsed = parseBody(translateSelectedBodySchema, body, response);
13571341
if ('error' in parsed) return parsed.error;
13581342
const data = parsed.data;
13591343
const selectedLanguages = data.selectedLanguages;
@@ -1380,7 +1364,7 @@ export default class I18nPlugin extends AdminForthPlugin {
13801364
method: 'POST',
13811365
path: `/plugin/${this.pluginInstanceId}/get_filtered_ids`,
13821366
handler: async ({ body, adminUser, headers, query, cookies, requestUrl, response }) => {
1383-
const parsed = this.parseBody(getFilteredIdsBodySchema, body, response);
1367+
const parsed = parseBody(getFilteredIdsBodySchema, body, response);
13841368
if ('error' in parsed) return parsed.error;
13851369
const data = parsed.data;
13861370
const resource = this.resourceConfig;

0 commit comments

Comments
 (0)