1- import { AdminForthFilterOperators , AdminForthPlugin , parseBody , Filters } from "adminforth" ;
1+ import { AdminForthFilterOperators , AdminForthPlugin , Filters } from "adminforth" ;
22import type { IAdminForth , IHttpServer , AdminForthComponentDeclaration , AdminForthResource } from "adminforth" ;
33import { suggestIfTypo , filtersTools } from "adminforth" ;
44import type { PluginOptions } from './types.js' ;
@@ -32,7 +32,7 @@ const getImageGenerationPromptsBodySchema = z.object({
3232
3333const createJobBodySchema = z . object ( {
3434 actionType : z . string ( ) . nullish ( ) ,
35- recordId : z . any ( ) . optional ( ) ,
35+ recordId : z . union ( [ z . string ( ) , z . number ( ) ] ) . nullish ( ) ,
3636 customPrompt : z . string ( ) . nullish ( ) ,
3737 filterFilledFields : z . boolean ( ) . nullish ( ) ,
3838 sessionIds : z . array ( z . string ( ) ) . nullish ( ) ,
@@ -163,7 +163,7 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
163163 return prompt ;
164164 }
165165
166- private async analyze_image ( jobId : string , recordId : string , adminUser : any , headers : Record < string , string | string [ ] | undefined > , customPrompt ? : string , filterFilledFields : boolean = true ) {
166+ private async analyze_image ( jobId : string , recordId : string | number , adminUser : any , headers : Record < string , string | string [ ] | undefined > , customPrompt ? : string , filterFilledFields : boolean = true ) {
167167 // TODO: need to do correct check of rate limit
168168 // if (this.options.rateLimits && this.options.rateLimits.fillFieldsFromImages && await this.checkRateLimit("fillFieldsFromImages" ,this.options.rateLimits.fillFieldsFromImages, headers)) {
169169 // jobs.set(jobId, { status: 'failed', error: "Rate limit exceeded" });
@@ -248,7 +248,7 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
248248
249249 }
250250
251- private async analyzeNoImages ( jobId : string , recordId : string , adminUser : any , headers : Record < string , string | string [ ] | undefined > , customPrompt ? : string , filterFilledFields : boolean = true ) {
251+ private async analyzeNoImages ( jobId : string , recordId : string | number , adminUser : any , headers : Record < string , string | string [ ] | undefined > , customPrompt ? : string , filterFilledFields : boolean = true ) {
252252 // TODO: need to do correct check of rate limit
253253 // if (this.options.rateLimits && this.options.rateLimits.fillPlainFields && await this.checkRateLimit("fillPlainFields" ,this.options.rateLimits.fillPlainFields, headers)) {
254254 // jobs.set(jobId, { status: 'failed', error: "Rate limit exceeded" });
@@ -312,7 +312,7 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
312312 }
313313 }
314314
315- private async initialImageGenerate ( jobId : string , recordId : string , adminUser : any , headers : Record < string , string | string [ ] | undefined > , customPrompt ? : string , filterFilledFields : boolean = true ) {
315+ private async initialImageGenerate ( jobId : string , recordId : string | number , adminUser : any , headers : Record < string , string | string [ ] | undefined > , customPrompt ? : string , filterFilledFields : boolean = true ) {
316316 // TODO: need to do correct check of rate limit
317317 // if (this.options.rateLimits && this.options.rateLimits.generateImages && await this.checkRateLimit("generateImages" ,this.options.rateLimits.generateImages, headers)) {
318318 // jobs.set(jobId, { status: 'failed', error: "Rate limit exceeded" });
@@ -424,7 +424,7 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
424424 }
425425 }
426426
427- private async regenerateImage ( jobId : string , recordId : string , fieldName : string , prompt : string , adminUser : any , headers : Record < string , string | string [ ] | undefined > ) {
427+ private async regenerateImage ( jobId : string , recordId : string | number , fieldName : string , prompt : string , adminUser : any , headers : Record < string , string | string [ ] | undefined > ) {
428428 const Id = recordId ;
429429 let isError = false ;
430430 // TODO: need to do correct check of rate limit
@@ -832,10 +832,9 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
832832 server . endpoint ( {
833833 method : 'POST' ,
834834 path : `/plugin/${ this . pluginInstanceId } /get_records` ,
835+ request_schema : getRecordsBodySchema ,
835836 handler : async ( { body, response } ) => {
836- const parsed = parseBody ( getRecordsBodySchema , body , response ) ;
837- if ( 'error' in parsed ) return parsed . error ;
838- const data = parsed . data ;
837+ const data = body as z . infer < typeof getRecordsBodySchema > ;
839838 if ( ! Array . isArray ( data . record ) ) {
840839 return { records : [ ] } ;
841840 }
@@ -860,10 +859,9 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
860859 server . endpoint ( {
861860 method : 'POST' ,
862861 path : `/plugin/${ this . pluginInstanceId } /get_old_data` ,
862+ request_schema : getOldDataBodySchema ,
863863 handler : async ( { body, response } ) => {
864- const parsed = parseBody ( getOldDataBodySchema , body , response ) ;
865- if ( 'error' in parsed ) return parsed . error ;
866- const data = parsed . data ;
864+ const data = body as z . infer < typeof getOldDataBodySchema > ;
867865 const recordId = data . recordId ;
868866 if ( recordId === undefined || recordId === null ) {
869867 return { ok : false , error : "Missing recordId" } ;
@@ -883,10 +881,9 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
883881 server . endpoint ( {
884882 method : 'POST' ,
885883 path : `/plugin/${ this . pluginInstanceId } /get_images` ,
884+ request_schema : getImagesBodySchema ,
886885 handler : async ( { body, response } ) => {
887- const parsed = parseBody ( getImagesBodySchema , body , response ) ;
888- if ( 'error' in parsed ) return parsed . error ;
889- const data = parsed . data ;
886+ const data = body as z . infer < typeof getImagesBodySchema > ;
890887 let images = [ ] ;
891888 if ( data . record ) {
892889 for ( const record of data . record ) {
@@ -905,10 +902,9 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
905902 server . endpoint ( {
906903 method : 'POST' ,
907904 path : `/plugin/${ this . pluginInstanceId } /update_fields` ,
905+ request_schema : updateFieldsBodySchema ,
908906 handler : async ( { body, adminUser, headers, response } ) => {
909- const parsed = parseBody ( updateFieldsBodySchema , body , response ) ;
910- if ( 'error' in parsed ) return parsed . error ;
911- const data = parsed . data ;
907+ const data = body as z . infer < typeof updateFieldsBodySchema > ;
912908 let isAllowedToSave : any = { ok : true , error : '' } ;
913909 if ( this . options . isAllowedToSave ) {
914910 isAllowedToSave = await this . options . isAllowedToSave ( { record : { } , adminUser : adminUser , resource : this . resourceConfig } ) ;
@@ -1015,10 +1011,9 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
10151011 server . endpoint ( {
10161012 method : 'POST' ,
10171013 path : `/plugin/${ this . pluginInstanceId } /get_image_generation_prompts` ,
1014+ request_schema : getImageGenerationPromptsBodySchema ,
10181015 handler : async ( { body, headers, response } ) => {
1019- const parsed = parseBody ( getImageGenerationPromptsBodySchema , body , response ) ;
1020- if ( 'error' in parsed ) return parsed . error ;
1021- const data = parsed . data ;
1016+ const data = body as z . infer < typeof getImageGenerationPromptsBodySchema > ;
10221017 const Id = data . recordId || [ ] ;
10231018 const customPrompt = data . customPrompt || null ;
10241019 const record = await this . adminforth . resource ( this . resourceConfig . resourceId ) . get ( [ Filters . EQ ( this . resourceConfig . columns . find ( c => c . primaryKey ) ?. name , Id ) ] ) ;
@@ -1044,10 +1039,9 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
10441039 server . endpoint ( {
10451040 method : 'POST' ,
10461041 path : `/plugin/${ this . pluginInstanceId } /create-job` ,
1042+ request_schema : createJobBodySchema ,
10471043 handler : async ( { body, adminUser, headers, response } ) => {
1048- const parsed = parseBody ( createJobBodySchema , body , response ) ;
1049- if ( 'error' in parsed ) return parsed . error ;
1050- const data = parsed . data ;
1044+ const data = body as z . infer < typeof createJobBodySchema > ;
10511045 const { actionType, recordId, customPrompt, filterFilledFields, sessionIds } = data ;
10521046 if ( this . options . rateLimits ) {
10531047 if ( sessionIds && sessionIds . length > 0 ) {
@@ -1106,10 +1100,9 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
11061100 server . endpoint ( {
11071101 method : 'POST' ,
11081102 path : `/plugin/${ this . pluginInstanceId } /get-job-status` ,
1103+ request_schema : jobStatusBodySchema ,
11091104 handler : async ( { body, adminUser, headers, response } ) => {
1110- const parsed = parseBody ( jobStatusBodySchema , body , response ) ;
1111- if ( 'error' in parsed ) return parsed . error ;
1112- const data = parsed . data ;
1105+ const data = body as z . infer < typeof jobStatusBodySchema > ;
11131106 const jobId = data . jobId ;
11141107 if ( ! jobId ) {
11151108 response . setStatus ( 400 ) ;
@@ -1147,10 +1140,9 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
11471140 server . endpoint ( {
11481141 method : 'POST' ,
11491142 path : `/plugin/${ this . pluginInstanceId } /update-rate-limits` ,
1143+ request_schema : updateRateLimitsBodySchema ,
11501144 handler : async ( { body, adminUser, headers, response } ) => {
1151- const parsed = parseBody ( updateRateLimitsBodySchema , body , response ) ;
1152- if ( 'error' in parsed ) return parsed . error ;
1153- const data = parsed . data ;
1145+ const data = body as z . infer < typeof updateRateLimitsBodySchema > ;
11541146 const actionType = data . actionType ;
11551147 const sessionId = randomUUID ( ) ;
11561148 this . sessionIds . add ( sessionId ) ;
@@ -1178,10 +1170,9 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
11781170 server . endpoint ( {
11791171 method : 'POST' ,
11801172 path : `/plugin/${ this . pluginInstanceId } /compile_old_image_link` ,
1173+ request_schema : compileOldImageLinkBodySchema ,
11811174 handler : async ( { body, adminUser, headers, response } ) => {
1182- const parsed = parseBody ( compileOldImageLinkBodySchema , body , response ) ;
1183- if ( 'error' in parsed ) return parsed . error ;
1184- const data = parsed . data ;
1175+ const data = body as z . infer < typeof compileOldImageLinkBodySchema > ;
11851176 const image = data . image ;
11861177 const columnName = data . columnName ;
11871178 if ( ! image ) {
@@ -1211,10 +1202,8 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
12111202 server . endpoint ( {
12121203 method : 'POST' ,
12131204 path : `/plugin/${ this . pluginInstanceId } /get_filtered_ids` ,
1205+ request_schema : getFilteredIdsBodySchema ,
12141206 handler : async ( { body, adminUser, headers, query, cookies, requestUrl, response } ) => {
1215- const parsed = parseBody ( getFilteredIdsBodySchema , body , response ) ;
1216- if ( 'error' in parsed ) return parsed . error ;
1217- const data = parsed . data ;
12181207 const resource = this . resourceConfig ;
12191208
12201209 for ( const hook of resource . hooks ?. list ?. beforeDatasourceRequest || [ ] ) {
0 commit comments