UNPKG

@yuju/tosspayments-sdk

Version:

Toss Payments SDK for Node.js

46 lines (45 loc) 1.46 kB
import got, { HTTPError } from 'got'; import { TossPaymentsError } from './utils/tossPaymentsError.js'; import { encodeBase64 } from './utils/encodeBase64.js'; import { filterUndefined } from './utils/filterUndefined.js'; export class HttpClient { constructor(domain, basicAuth) { Object.defineProperty(this, "domain", { enumerable: true, configurable: true, writable: true, value: domain }); Object.defineProperty(this, "basicAuth", { enumerable: true, configurable: true, writable: true, value: basicAuth }); } async post(headers, path, body) { try { return await got.post(this.domain + path, filterUndefined({ json: body, headers: { ...headers, 'Authorization': 'basic ' + encodeBase64(this.basicAuth + ":"), }, })).json(); } catch (error) { if (error instanceof HTTPError) { throw new TossPaymentsError(error.response.statusCode, error.response.body); } throw HTTPError; } } async get(path, searchParams = {}) { return await got.get(this.domain + path, { searchParams, headers: { 'Authorization': 'basic ' + this.basicAuth, }, }).json(); } }