import * as Querystring from 'querystring' import { CATALOG_API_SHARED_KEY, CATALOG_API_URL } from '../lib/constants' import { Request, Utils, makeError } from '@oyst/utils' import { isNil, merge } from 'ramda' const { RequestError, ApiRequest } = Request const { removeUndefinedKeys } = Utils export interface IfindReference { merchant_id: string product_reference: string variation_reference?: string } export class CatalogAPI extends ApiRequest { constructor (init: any = {}) { super(merge({ api: 'catalog-api', endpoint: `${CATALOG_API_URL}/v1`, prefix: 'CAT', req: {}, sharedKey: CATALOG_API_SHARED_KEY, targetApi: 'catalog-api', type: 'http' }, init)) } public findByReferences (opts: IfindReference): Promise { return new Promise(async (resolve, reject) => { try { const result = await this.makeRequest({ url: `/merchants/${opts.merchant_id}/products/search?${ Querystring.stringify(removeUndefinedKeys({ product_reference: opts.product_reference, variation_reference: opts.variation_reference })) }` }) resolve(result.data.product) } catch (err) { reject(makeError(err)) } }) } public async findShipments (merchantId: string): Promise { const result = await this.makeRequest({ url: `/merchants/${merchantId}/shipments` }) return result.data.shipments } } export default new CatalogAPI()