@portone/server-sdk
Version:
PortOne JavaScript SDK for server-side usage
144 lines (143 loc) • 4.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.BillingKeyClient = BillingKeyClient;
exports.IssueBillingKeyError = exports.GetBillingKeyInfosError = exports.GetBillingKeyInfoError = exports.DeleteBillingKeyError = void 0;
var _BillingKeyError = require("./BillingKeyError.cjs");
var _client = require("../../../client.cjs");
function BillingKeyClient(init) {
const baseUrl = init.baseUrl ?? "https://api.portone.io";
const secret = init.secret;
return {
getBillingKeyInfos: async options => {
const page = options?.page;
const sort = options?.sort;
const filter = options?.filter;
const requestBody = JSON.stringify({
page,
sort,
filter
});
const query = [["requestBody", requestBody]].flatMap(([key, value]) => value == null ? [] : `${key}=${encodeURIComponent(value)}`).join("&");
const response = await fetch(new URL(`/billing-keys?${query}`, baseUrl), {
method: "GET",
headers: {
Authorization: `PortOne ${secret}`,
"User-Agent": _client.USER_AGENT
}
});
if (!response.ok) {
throw new GetBillingKeyInfosError(await response.json());
}
return response.json();
},
issueBillingKey: async options => {
const {
storeId,
method,
channelKey,
channelGroupId,
customer,
customData,
bypass,
noticeUrls
} = options;
const requestBody = JSON.stringify({
storeId: storeId ?? init.storeId,
method,
channelKey,
channelGroupId,
customer,
customData,
bypass,
noticeUrls
});
const response = await fetch(new URL("/billing-keys", baseUrl), {
method: "POST",
headers: {
Authorization: `PortOne ${secret}`,
"User-Agent": _client.USER_AGENT
},
body: requestBody
});
if (!response.ok) {
throw new IssueBillingKeyError(await response.json());
}
return response.json();
},
getBillingKeyInfo: async options => {
const {
billingKey,
storeId
} = options;
const query = [["storeId", storeId]].flatMap(([key, value]) => value == null ? [] : `${key}=${encodeURIComponent(value)}`).join("&");
const response = await fetch(new URL(`/billing-keys/${encodeURIComponent(billingKey)}?${query}`, baseUrl), {
method: "GET",
headers: {
Authorization: `PortOne ${secret}`,
"User-Agent": _client.USER_AGENT
}
});
if (!response.ok) {
throw new GetBillingKeyInfoError(await response.json());
}
return response.json();
},
deleteBillingKey: async options => {
const {
billingKey,
storeId,
reason
} = options;
const query = [["storeId", storeId], ["reason", reason]].flatMap(([key, value]) => value == null ? [] : `${key}=${encodeURIComponent(value)}`).join("&");
const response = await fetch(new URL(`/billing-keys/${encodeURIComponent(billingKey)}?${query}`, baseUrl), {
method: "DELETE",
headers: {
Authorization: `PortOne ${secret}`,
"User-Agent": _client.USER_AGENT
}
});
if (!response.ok) {
throw new DeleteBillingKeyError(await response.json());
}
return response.json();
}
};
}
class GetBillingKeyInfosError extends _BillingKeyError.BillingKeyError {
/** @ignore */
constructor(data) {
super(data);
Object.setPrototypeOf(this, GetBillingKeyInfosError.prototype);
this.name = "GetBillingKeyInfosError";
}
}
exports.GetBillingKeyInfosError = GetBillingKeyInfosError;
class IssueBillingKeyError extends _BillingKeyError.BillingKeyError {
/** @ignore */
constructor(data) {
super(data);
Object.setPrototypeOf(this, IssueBillingKeyError.prototype);
this.name = "IssueBillingKeyError";
}
}
exports.IssueBillingKeyError = IssueBillingKeyError;
class GetBillingKeyInfoError extends _BillingKeyError.BillingKeyError {
/** @ignore */
constructor(data) {
super(data);
Object.setPrototypeOf(this, GetBillingKeyInfoError.prototype);
this.name = "GetBillingKeyInfoError";
}
}
exports.GetBillingKeyInfoError = GetBillingKeyInfoError;
class DeleteBillingKeyError extends _BillingKeyError.BillingKeyError {
/** @ignore */
constructor(data) {
super(data);
Object.setPrototypeOf(this, DeleteBillingKeyError.prototype);
this.name = "DeleteBillingKeyError";
}
}
exports.DeleteBillingKeyError = DeleteBillingKeyError;