lightrail-client
Version:
A Javascript and Typescript client for Lightrail
314 lines (313 loc) • 10.7 kB
JavaScript
;
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTransactionId = exports.getTransactionChain = exports.getTransaction = exports.listTransactions = exports.voidPending = exports.capturePending = exports.reverse = exports.transfer = exports.credit = exports.debit = exports.checkout = void 0;
const lightrail = require("./index");
const LightrailRequestError_1 = require("./LightrailRequestError");
const requestUtils_1 = require("./requestUtils");
/**
* See: https://apidocs.lightrail.com/#operation/Checkout
*
* Example:
* ```js
* const checkoutTx = await Lightrail.transactions.checkout({
* id: "abcdefg",
* currency: "USD",
* lineItems: [
* {
* productId: "pedals",
* unitPrice: 100000,
* taxRate: 0
* }
* ],
* sources: [
* {
* rail: "lightrail",
* code: "PROMOCODE"
* },
* {
* rail: "stripe",
* source: "tok_visa"
* }
* ]
* );
* ```
*/
function checkout(params) {
return __awaiter(this, void 0, void 0, function* () {
if (!params) {
throw new Error("checkout params not set");
}
else {
requestUtils_1.validateRequiredParams(["id", "currency"], params);
}
const resp = yield lightrail.request("POST", "transactions/checkout").send(params);
if (requestUtils_1.isSuccessStatus(resp.status)) {
return requestUtils_1.formatResponse(resp);
}
throw new LightrailRequestError_1.LightrailRequestError(resp);
});
}
exports.checkout = checkout;
/**
* See: https://apidocs.lightrail.com/#operation/Debit
*
* Example:
* ```js
* const debitTx = await Lightrail.transactions.debit({
* id: "abcdefg",
* currency: "USD",
* source: {
* rail: "lightrail",
* valueId: "hijklmnop"
* },
* amount: 1000
* );
* ```
*/
function debit(params) {
return __awaiter(this, void 0, void 0, function* () {
if (!params) {
throw new Error("debit params not set");
}
else {
requestUtils_1.validateRequiredParams(["id", "currency", "source"], params);
}
const resp = yield lightrail.request("POST", "transactions/debit").send(params);
if (requestUtils_1.isSuccessStatus(resp.status)) {
return requestUtils_1.formatResponse(resp);
}
throw new LightrailRequestError_1.LightrailRequestError(resp);
});
}
exports.debit = debit;
/**
* See: https://apidocs.lightrail.com/#operation/Credit
*
* Example:
* ```js
* const creditTx = await Lightrail.transactions.credit({
* id: "abcdefg",
* currency: "USD",
* destination: {
* rail: "lightrail",
* valueId: "hijklmnop"
* },
* amount: 1000
* );
* ```
*/
function credit(params) {
return __awaiter(this, void 0, void 0, function* () {
if (!params) {
throw new Error("credit params not set");
}
else {
requestUtils_1.validateRequiredParams(["id", "currency", "destination"], params);
}
const resp = yield lightrail.request("POST", "transactions/credit").send(params);
if (requestUtils_1.isSuccessStatus(resp.status)) {
return requestUtils_1.formatResponse(resp);
}
throw new LightrailRequestError_1.LightrailRequestError(resp);
});
}
exports.credit = credit;
/**
* See: https://apidocs.lightrail.com/#operation/Transfer
*
* Example:
* ```js
* const transferTx = await Lightrail.transactions.transfer({
* id: "abcdefg",
* currency: "USD",
* source: {
* rail: "lightrail",
* valueId: "hijklmnop"
* },
* destination: {
* rail: "lightrail",
* valueId: "qrstuv"
* },
* amount: 1000
* );
* ```
*/
function transfer(params) {
return __awaiter(this, void 0, void 0, function* () {
if (!params) {
throw new Error("transfer params not set");
}
else {
requestUtils_1.validateRequiredParams(["id", "currency", "source", "destination", "amount"], params);
}
const resp = yield lightrail.request("POST", "transactions/transfer").send(params);
if (requestUtils_1.isSuccessStatus(resp.status)) {
return requestUtils_1.formatResponse(resp);
}
throw new LightrailRequestError_1.LightrailRequestError(resp);
});
}
exports.transfer = transfer;
/**
* See: https://apidocs.lightrail.com/#operation/Reverse
*
* Example:
* ```js
* const reverseTx = await Lightrail.transactions.reverse("hijklmnop" {
* id: "abcdefg" // This is the ID of the reverse transaction created.
* );
* ```
*/
function reverse(transactionToReverse, params) {
return __awaiter(this, void 0, void 0, function* () {
if (!params) {
throw new Error("reverse params not set");
}
else {
requestUtils_1.validateRequiredParams(["id"], params);
}
const resp = yield lightrail.request("POST", `transactions/${encodeURIComponent(getTransactionId(transactionToReverse))}/reverse`).send(params);
if (requestUtils_1.isSuccessStatus(resp.status)) {
return requestUtils_1.formatResponse(resp);
}
throw new LightrailRequestError_1.LightrailRequestError(resp);
});
}
exports.reverse = reverse;
/**
* See: https://apidocs.lightrail.com/#operation/CapturePending
*
* Example:
* ```js
* const captureTx = await Lightrail.transactions.capturePending("hijklmnop" {
* id: "abcdefg" // This is the ID of the capture transaction created.
* );
* ```
*/
function capturePending(transactionToCapture, params) {
return __awaiter(this, void 0, void 0, function* () {
if (!params) {
throw new Error("capture params not set");
}
else {
requestUtils_1.validateRequiredParams(["id"], params);
}
const resp = yield lightrail.request("POST", `transactions/${encodeURIComponent(getTransactionId(transactionToCapture))}/capture`).send(params);
if (requestUtils_1.isSuccessStatus(resp.status)) {
return requestUtils_1.formatResponse(resp);
}
throw new LightrailRequestError_1.LightrailRequestError(resp);
});
}
exports.capturePending = capturePending;
/**
* See: https://apidocs.lightrail.com/#operation/VoidPending
*
* Example:
* ```js
* const voidTx = await Lightrail.transactions.voidPending("hijklmnop" {
* id: "abcdefg" // This is the ID of the void transaction created.
* );
* ```
*/
function voidPending(transactionToVoid, params) {
return __awaiter(this, void 0, void 0, function* () {
if (!params) {
throw new Error("void params not set");
}
else {
requestUtils_1.validateRequiredParams(["id"], params);
}
const resp = yield lightrail.request("POST", `transactions/${encodeURIComponent(getTransactionId(transactionToVoid))}/void`).send(params);
if (requestUtils_1.isSuccessStatus(resp.status)) {
return requestUtils_1.formatResponse(resp);
}
throw new LightrailRequestError_1.LightrailRequestError(resp);
});
}
exports.voidPending = voidPending;
/**
* See: https://apidocs.lightrail.com/#operation/ListTransactions
*
* Example:
* ```js
* const transactions = await Lightrail.transactions.listTransactions();
* const transactionsLimited = await Lightrail.transactions.listTransactions({limit: 5});
* ```
*/
function listTransactions(params) {
return __awaiter(this, void 0, void 0, function* () {
const resp = yield lightrail.request("GET", "transactions").query(requestUtils_1.formatFilterParams(params));
if (requestUtils_1.isSuccessStatus(resp.status)) {
return requestUtils_1.formatResponse(resp);
}
throw new LightrailRequestError_1.LightrailRequestError(resp);
});
}
exports.listTransactions = listTransactions;
/**
* See: https://apidocs.lightrail.com/#operation/GetaTransaction
*
* Example:
* ```js
* const transaction = await Lightrail.transactions.getTransaction("abcdefg");
* ```
*/
function getTransaction(transaction) {
return __awaiter(this, void 0, void 0, function* () {
const transactionId = getTransactionId(transaction);
const resp = yield lightrail.request("GET", `transactions/${encodeURIComponent(transactionId)}`);
if (requestUtils_1.isSuccessStatus(resp.status) || resp.status === 404) {
return requestUtils_1.formatResponse(resp);
}
throw new LightrailRequestError_1.LightrailRequestError(resp);
});
}
exports.getTransaction = getTransaction;
/**
* See: https://apidocs.lightrail.com/#operation/GetTransactionChain
*
* Example:
* ```js
* const transactions = await Lightrail.transactions.getTransactionChain("abcdefg");
* ```
*/
function getTransactionChain(transaction) {
return __awaiter(this, void 0, void 0, function* () {
const transactionId = getTransactionId(transaction);
const resp = yield lightrail.request("GET", `transactions/${encodeURIComponent(transactionId)}/chain`);
if (requestUtils_1.isSuccessStatus(resp.status) || resp.status === 404) {
return requestUtils_1.formatResponse(resp);
}
throw new LightrailRequestError_1.LightrailRequestError(resp);
});
}
exports.getTransactionChain = getTransactionChain;
/**
* @internal
* Get transactionId from the string (as the ID itself) or Transaction object.
*/
function getTransactionId(transaction) {
if (!transaction) {
throw new Error("transaction not set");
}
else if (typeof transaction === "string") {
return transaction;
}
else if (transaction.id) {
return transaction.id;
}
else {
throw new Error("transaction must be a string for transactionId or a Transaction object");
}
}
exports.getTransactionId = getTransactionId;