import { Client, Payload } from "./client"; export abstract class ServiceClient { client: Client; constructor(client: Client) { this.client = client; } protected abstract getServiceName(): string; protected async actionCall(folder: string, actionName: string, payload: Payload): Promise; protected async actionCall(actionName: string, payload: Payload): Promise protected async actionCall(...args: any[]): Promise { let apiPath: string = ''; if (args.length === 3) { const [folder, actionName, payload] = args; apiPath = `/service/registry/${this.getServiceName()}/${folder}/${actionName}`; } else { const [actionName, payload] = args; apiPath = `/service/registry/${this.getServiceName()}/${actionName}`; } const uri = new URL(this.client.config.endpoint + apiPath); const apiHeaders: { [header: string]: string } = { 'content-type': 'application/json', } return await this.client.call( 'post', uri, apiHeaders, args.length === 3 ? args[2] : args[1], ); } protected httpGet(url: string, payload?: Payload): Promise { return this.client.call( 'get', new URL(this.client.config.endpoint + url + (payload ? '?' + new URLSearchParams(payload as any) : '')) ); } protected post(url: string, payload: Payload): Promise { const apiHeaders: { [header: string]: string } = { 'content-type': 'application/json', } return this.client.call( 'post', new URL(this.client.config.endpoint + url), apiHeaders, payload ); } }