import { Request, Response } from "express" import { Build, GenericController, GenericService } from "./GenericController" import { handleError, Log } from "./http" import { ErrorMessage } from "./metadata" import { resources, StringMap } from "./resources" import { buildArray, Filter, format, fromRequest, getMetadataFunc, getParameters, initializeConfig, jsonResult, SearchConfig, SearchResult } from "./search" export interface Service extends GenericService { search: (s: S, limit: number, page?: number | string, fields?: string[]) => Promise> } export class LowcodeController extends GenericController { config?: SearchConfig csv?: boolean dates?: string[] numbers?: string[] excluding?: string array?: string[] constructor( public lowCodeService: Service, config?: SearchConfig, build?: Build, validate?: (obj: T, resource?: StringMap, patch?: boolean) => Promise, dates?: string[], numbers?: string[], ) { super(lowCodeService, build, validate) this.search = this.search.bind(this) this.config = initializeConfig(config) if (this.config) { this.csv = this.config.csv this.excluding = this.config.excluding } const m = getMetadataFunc(lowCodeService, dates, numbers) if (m) { this.dates = m.dates this.numbers = m.numbers } } search(req: Request, res: Response) { const s = fromRequest(req, buildArray(this.array, resources.fields, this.excluding)) const l = getParameters(s) const s2 = format(s, this.dates, this.numbers) this.lowCodeService .search(s2, l.limit, l.pageOrNextPageToken, l.fields) .then((result) => jsonResult(res, result, this.csv, l.fields, this.config)) .catch((err) => handleError(err, res)) } } export { LowcodeController as LowcodeHandler } export class Controller extends GenericController { config?: SearchConfig csv?: boolean dates?: string[] numbers?: string[] excluding?: string array?: string[] constructor( log: Log, public lowCodeService: Service, build?: Build, validate?: (obj: T, resource?: StringMap, patch?: boolean) => Promise, config?: SearchConfig, dates?: string[], numbers?: string[], ) { super(lowCodeService, build, validate) this.search = this.search.bind(this) this.config = initializeConfig(config) if (this.config) { this.csv = this.config.csv this.excluding = this.config.excluding } const m = getMetadataFunc(lowCodeService, dates, numbers) if (m) { this.dates = m.dates this.numbers = m.numbers } } search(req: Request, res: Response) { const s = fromRequest(req, buildArray(this.array, resources.fields, this.excluding)) const l = getParameters(s) const s2 = format(s, this.dates, this.numbers) this.lowCodeService .search(s2, l.limit, l.pageOrNextPageToken, l.fields) .then((result) => jsonResult(res, result, this.csv, l.fields, this.config)) .catch((err) => handleError(err, res)) } }