/* eslint-disable @typescript-eslint/no-explicit-any */ import { ApplicationExceptionFactory, UnknownException } from "pip-services4-commons-node"; import restify = require('restify-clients'); export class TestRestClient { private _rest: any; public constructor() { const url = 'http://localhost:3000'; this._rest = restify.createJsonClient({ url: url, version: '*'}); //requestTimeout: 2000 } protected async call(method: string, route: string, data?: any): Promise { method = method.toLowerCase(); return new Promise((resolve, reject) => { const action = (err, req, res, data) => { // Handling 204 codes if (res && res.statusCode == 204) resolve(null); else if (err == null) resolve(data); else { // Restore application exception if (data != null) err = ApplicationExceptionFactory.create(data).withCause(err); reject(err); } }; if (method == 'get') this._rest.get(route, action); else if (method == 'head') this._rest.head(route, action); else if (method == 'post') this._rest.post(route, data, action); else if (method == 'put') this._rest.put(route, data, action); else if (method == 'patch') this._rest.patch(route, data, action); else if (method == 'delete' || method == 'del') this._rest.del(route, action); else { const err = new UnknownException('UNSUPPORTED_METHOD', 'Method is not supported by Test REST client' ).withDetails('verb', method); reject(err); } }); } public async get(path: string): Promise { delete this._rest.headers['x-session-id']; return await this.call('get', path); } public async head(path: string): Promise { delete this._rest.headers['x-session-id']; return await this.call('head', path); } public async post(path: string, params: any): Promise { delete this._rest.headers['x-session-id']; return await this.call('post', path, params); } public async put(path: string, params: any): Promise { delete this._rest.headers['x-session-id']; return await this.call('put', path, params); } public async del(path: string): Promise { delete this._rest.headers['x-session-id']; return await this.call('del', path); } public async getAsUser(sessionId: string, path: string): Promise { this._rest.headers['x-session-id'] = sessionId; return await this.call('get', path); } public async headAsUser(sessionId: string, path: string): Promise { this._rest.headers['x-session-id'] = sessionId; return await this.call('head', path); } public async postAsUser(sessionId: string, path: string, params: any): Promise { this._rest.headers['x-session-id'] = sessionId; return await this.call('post', path, params); } public async putAsUser(sessionId: string, path: string, params: any): Promise { this._rest.headers['x-session-id'] = sessionId; return await this.call('put', path, params); } public async delAsUser(sessionId: string, path: string, params?: any): Promise { this._rest.headers['x-session-id'] = sessionId; return await this.call('del', path, params); } }