@interopio/search-api
Version:
Glue42 Search API
84 lines (63 loc) • 3.13 kB
text/typescript
import { IOConnectCore } from "@interopio/core";
import { nanoid } from "nanoid/non-secure";
import { IOConnectSearch } from "../../search";
import { ProviderController } from "../controllers/provider";
import { nonEmptyStringDecoder, queryResultDecoder } from "../shared/decoders";
import { QueryIdentification } from "../shared/types";
import { extractErrorMsg } from "../shared/utils";
export class ProviderQueryModel {
constructor(
private readonly myData: IOConnectSearch.QueryConfig,
private readonly controller: ProviderController,
private readonly logger: IOConnectCore.Logger.API,
private readonly identification: QueryIdentification
) {}
private get id(): string {
return this.identification.queryId;
}
private get search(): string {
return this.myData.search;
}
private get providers(): IOConnectSearch.ProviderData[] | undefined {
return this.myData.providers;
}
private get types(): IOConnectSearch.SearchType[] | undefined {
return this.myData.types;
}
private get providerLimits(): IOConnectSearch.ProviderLimits | undefined {
return this.myData.providerLimits;
}
public get myQueryData(): IOConnectSearch.QueryConfig {
return Object.assign({}, this.myData);
}
public exposeFacade(): IOConnectSearch.ProviderQuery {
const providerQueryFacade: IOConnectSearch.ProviderQuery = {
id: this.id,
search: this.search,
providers: this.providers,
types: this.types,
providerLimits: this.providerLimits,
sendResult: this.sendResult.bind(this),
error: this.error.bind(this),
done: this.done.bind(this)
};
return Object.freeze(providerQueryFacade);
}
private sendResult(result: IOConnectSearch.QueryResult): void {
queryResultDecoder.runWithException(result);
const commandId = nanoid(10);
this.logger.trace(`[${commandId}] Received a valid result, forwarding to the controller`);
return this.controller.processProviderQueryResult({ identification: this.identification, result, commandId });
}
private error(error: string): void {
const commandId = nanoid(10);
nonEmptyStringDecoder.runWithException(error);
this.logger.trace(`[${commandId}] Received a valid error, forwarding to the controller`);
this.controller.processProviderQueryError({ identification: this.identification, error, commandId }).catch((error) => this.logger.warn(`Error processing the error signal for this provider: ${this.id}, error: ${extractErrorMsg(error)}`));
}
private done(): void {
const commandId = nanoid(10);
this.logger.trace(`[${commandId}] Received a valid done, forwarding to the controller`);
this.controller.processProviderQueryDone({ identification: this.identification, commandId }).catch((error) => this.logger.warn(`Error processing the done signal for this provider: ${this.identification.providerId}, error: ${extractErrorMsg(error)}`));
}
}