UNPKG

oklink-api

Version:

This is a non-official JS SDK API for calling OKLink's API product.

51 lines 1.94 kB
import { httpClient } from '../request/defaultHttpClient.js'; import { ApiResult } from './apiResult.js'; const DEFAULT_HEADERS = { 'Content-Type': 'application/json', }; export class ApiCaller { #config; #httpClient = httpClient; constructor({ config, httpClient }) { this.#config = config; if (httpClient) { this.#httpClient = httpClient; } } async call({ method = 'GET', path, params, data, timeout = 5000, }) { const requestUrl = `${this.#config.baseUrl}${this.#config.contextPath ?? ''}${path}`; let dataSerialize = data === null || data === undefined ? '' : JSON.stringify(data); if (dataSerialize === '{}') { dataSerialize = ''; } const authHeaders = { 'OK-ACCESS-KEY': this.#config.apiKey ?? '', }; try { const response = await this.#httpClient({ url: requestUrl, method, timeout, headers: { ...DEFAULT_HEADERS, ...authHeaders }, params, data, }); if (typeof response?.data?.code === 'string' && typeof response?.data?.msg === 'string') { return new ApiResult(response.data.code, response.data.msg, response.data.data); } else { throw new Error('Unknown API response format'); } // eslint-disable-next-line @typescript-eslint/no-explicit-any } catch (error) { if (typeof error.response?.data?.code === 'string' && typeof error.response?.data?.msg === 'string') { return new ApiResult(error.response.data.code, error.response.data.msg, error.response?.data); } else { throw error; } } } } //# sourceMappingURL=apiCaller.js.map