@interopio/search-api
Version:
Glue42 Search API
91 lines (65 loc) • 3.02 kB
text/typescript
import { IOConnectCore } from "@interopio/core";
import { nanoid } from "nanoid/non-secure";
import { IOConnectSearch } from "../../search";
import { ProviderController } from "../controllers/provider";
export class ProviderModel {
constructor(
private readonly myData: IOConnectSearch.ProviderData,
private readonly controller: ProviderController,
private readonly logger: IOConnectCore.Logger.API
) {}
private get id(): string {
return this.myData.id;
}
private get name(): string {
return this.myData.name;
}
private get appName(): string | undefined {
return this.myData.appName;
}
private get types(): IOConnectSearch.SearchType[] | undefined {
return this.myData.types;
}
public get myProviderData(): IOConnectSearch.ProviderData {
return Object.assign({}, this.myData);
}
public exposeFacade(): IOConnectSearch.Provider {
const providerFacade: IOConnectSearch.Provider = {
interopId: this.myData.interopId,
id: this.id,
name: this.name,
appName: this.appName,
types: this.types,
onQuery: this.onQuery.bind(this),
onQueryCancel: this.onQueryCancel.bind(this),
unregister: this.unregister.bind(this)
};
return Object.freeze(providerFacade);
}
private onQuery(callback: (query: IOConnectSearch.ProviderQuery) => void): IOConnectSearch.UnsubscribeFunction {
if (typeof callback !== "function") {
throw new Error("onQuery requires a callback of type function");
}
const commandId = nanoid(10);
this.logger.info(`[${commandId}] received a valid onQuery request, forwarding to the controller.`);
const unsubscribe = this.controller.processProviderOnQuery({ callback, id: this.id, commandId });
this.logger.info(`[${commandId}] the onQuery request was completed.`);
return unsubscribe;
}
private onQueryCancel(callback: (query: { id: string; }) => void): IOConnectSearch.UnsubscribeFunction {
if (typeof callback !== "function") {
throw new Error("onQuery requires a callback of type function");
}
const commandId = nanoid(10);
this.logger.info(`[${commandId}] received a valid onQueryCancel request, forwarding to the controller.`);
const unsubscribe = this.controller.processProviderOnQueryCancel({ callback, id: this.id, commandId });
this.logger.info(`[${commandId}] the onQueryCancel request was completed.`);
return unsubscribe;
}
private async unregister(): Promise<void> {
const commandId = nanoid(10);
this.logger.info(`[${commandId}] received a valid unregister request, forwarding to the controller.`);
await this.controller.processProviderUnregister({ id: this.id, commandId });
this.logger.info(`[${commandId}] the unregister request was completed.`);
}
}