@bootpay/backend-js
Version:
Bootpay Server Side Package for Node.js
150 lines (149 loc) • 5.4 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BootpayBackendNodejsResource = void 0;
const axios_1 = __importDefault(require("axios"));
class BootpayBackendNodejsResource {
constructor() {
this.apiVersion = '5.0.0';
this.sdkVersion = '2.3.0';
this.mode = 'production';
this.$http = axios_1.default.create({
timeout: 60000
});
this.$token = undefined;
this.bootpayConfiguration = {
application_id: '',
private_key: '',
mode: 'production'
};
this.API_ENTRYPOINTS = {
development: 'https://dev-api.bootpay.co.kr/v2',
stage: 'https://stage-api.bootpay.co.kr/v2',
production: 'https://api.bootpay.co.kr/v2'
};
this.$http.interceptors.response.use((response) => {
if (response.request !== undefined && response.headers !== undefined) {
return response.data;
}
else {
// 오류를 리턴
return response.data;
}
}, (error) => {
if (error.response !== undefined) {
return Promise.reject(error.response.data);
}
else {
return Promise.reject({
error_code: -100,
message: `Request Rest Api Failed to Bootpay Server, ${error.message}`
});
}
});
// @ts-expect-error
this.$http.interceptors.request.use((config) => {
if (config.headers !== undefined) {
if (this.$token !== undefined) {
config.headers.authorization = `Bearer ${this.$token}`;
}
config.headers['Content-Type'] = 'application/json';
config.headers['Accept'] = 'application/json';
config.headers['BOOTPAY-SDK-VERSION'] = this.sdkVersion;
config.headers['BOOTPAY-API-VERSION'] = this.apiVersion;
config.headers['BOOTPAY-SDK-TYPE'] = 301;
}
return config;
}, (error) => {
return Promise.reject(error);
});
}
/**
* Environments
* Comment by GOSOMI
* @date: 2022-04-12
* @param configuration: BootpayConfiguration
* @returns void
*/
setConfiguration(configuration) {
if (configuration.mode === undefined) {
configuration.mode = 'production';
}
this.bootpayConfiguration = configuration;
}
/**
* SET API Version
* Comment by GOSOMI
* @date: 2022-07-29
*/
setApiVersion(version) {
this.apiVersion = version;
}
/**
* Set Access Token
* Comment by GOSOMI
* @date: 2022-04-12
*/
setToken(token) {
this.$token = token;
}
entrypoints(url) {
return [this.API_ENTRYPOINTS[this.bootpayConfiguration.mode === undefined ? 'production' : this.bootpayConfiguration.mode], url].join('/');
}
get(url, config) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.$http.get(this.entrypoints(url), config);
return Promise.resolve(response);
}
catch (e) {
return Promise.reject(e);
}
});
}
post(url, data, config) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.$http.post(this.entrypoints(url), data, config);
return Promise.resolve(response);
}
catch (e) {
return Promise.reject(e);
}
});
}
put(url, data, config) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.$http.put(this.entrypoints(url), data, config);
return Promise.resolve(response);
}
catch (e) {
return Promise.reject(e);
}
});
}
delete(url, config) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.$http.delete(this.entrypoints(url), config);
return Promise.resolve(response);
}
catch (e) {
return Promise.reject(e);
}
});
}
}
exports.BootpayBackendNodejsResource = BootpayBackendNodejsResource;