import * as ifm from "typed-rest-client/Interfaces"; import * as rm from "typed-rest-client/RestClient"; import * as auctionModel from "../../models/auction.model"; import { validateRequest, validateResponse } from "../../validation/auction-validation"; /** * @const {string} - the auction endpoint constant */ export const AUCTION_ENDPOINT: string = "/auction"; /** * @const {number} - the http service not available status code constant */ export const NOT_AVAILABLE_SERVICE_STATUS_CODE: number = 404; /** * @class MsAuctionClient the client object should be used to call ms-auction * @example: let client: MsAuctionClient = new MsAuctionClient('https://nock'); * const healthResp = await client.Health(); */ export default class MsAuctionClient { private baseUrl: string; private restClient: rm.RestClient; /** * @constructor the client object should be used to call ms-auction * @param baseUrl the base url of ms-auction service * @param requestOptions */ constructor(baseUrl: string, requestOptions?: ifm.IRequestOptions) { this.baseUrl = baseUrl; this.restClient = new rm.RestClient( "auctionRestClient", this.baseUrl, undefined, requestOptions ); } public validateAuctionRequest(marketplaceRequest: auctionModel.IAuctionRequest): Error | null { return validateRequest(marketplaceRequest); } /** * Call marketplace endpoint and return a typed object * * @param {auctionModel.IAuctionRequest} marketplaceRequest * @returns {Promise} * @memberof MsFilterClient */ public async callAuction( marketplaceRequest: auctionModel.IAuctionRequest ): Promise { const restResp: rm.IRestResponse = await this.restClient.create( AUCTION_ENDPOINT, marketplaceRequest ); const errorDetails = []; const validateResponseErr = validateResponse(restResp.result); if (restResp.statusCode === NOT_AVAILABLE_SERVICE_STATUS_CODE) { errorDetails.push("[404] ms-auction return HTTP 404 error"); } if (validateResponseErr) { errorDetails.push(`[Validation] Response validation error => ${validateResponseErr.message}`); } if (errorDetails.length > 0) { throw new Error("ms-auction-client, error to call auction: " + errorDetails.join(" AND ")); } return restResp.result!; } }