lightrail-client
Version:
A Javascript and Typescript client for Lightrail
150 lines (149 loc) • 5.61 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.getCurrencyCode = exports.deleteCurrency = exports.updateCurrency = exports.getCurrency = exports.listCurrencies = exports.createCurrency = void 0;
const requestUtils_1 = require("./requestUtils");
const LightrailRequestError_1 = require("./LightrailRequestError");
const lightrail = require("./index");
/**
* See: https://apidocs.lightrail.com/#operation/CreateCurrency
*
* Example:
* ```js
* const currency = await Lightrail.currencies.createCurrency({
* code: "USD",
* symbol: "$",
* name: "US Dollar",
* decimalPlaces: 2
* });
* ```
*/
function createCurrency(params) {
return __awaiter(this, void 0, void 0, function* () {
if (!params) {
throw new Error("missing currency");
}
else {
requestUtils_1.validateRequiredParams(["code", "name", "symbol", "decimalPlaces"], params);
}
const resp = yield lightrail.request("POST", "currencies").send(params);
if (requestUtils_1.isSuccessStatus(resp.status)) {
return requestUtils_1.formatResponse(resp);
}
throw new LightrailRequestError_1.LightrailRequestError(resp);
});
}
exports.createCurrency = createCurrency;
/**
* See: https://apidocs.lightrail.com/#operation/ListCurrencies
*
* Example:
* ```js
* const currencies = await Lightrail.currencies.listCurrencies();
* ```
*/
function listCurrencies() {
return __awaiter(this, void 0, void 0, function* () {
const resp = yield lightrail.request("GET", `currencies`);
if (requestUtils_1.isSuccessStatus(resp.status)) {
return requestUtils_1.formatResponse(resp);
}
throw new LightrailRequestError_1.LightrailRequestError(resp);
});
}
exports.listCurrencies = listCurrencies;
/**
* See: https://apidocs.lightrail.com/#operation/GetCurrency
*
* Example:
* ```js
* const usd = await Lightrail.currencies.getCurrency("USD");
* ```
*/
function getCurrency(currency) {
return __awaiter(this, void 0, void 0, function* () {
const currencyCode = getCurrencyCode(currency);
if (!currencyCode) {
throw new Error("currencyCode missing from getCurrency(currency)!");
}
const resp = yield lightrail.request("GET", `currencies/${encodeURIComponent(currencyCode)}`);
if (requestUtils_1.isSuccessStatus(resp.status) || resp.status === 404) {
return requestUtils_1.formatResponse(resp);
}
throw new LightrailRequestError_1.LightrailRequestError(resp);
});
}
exports.getCurrency = getCurrency;
/**
* See: https://apidocs.lightrail.com/#operation/UpdateCurrency
*
* Example:
* ```js
* const updatedUsd = await Lightrail.currencies.updateCurrency("USD", {name: "Freedom Dollars"});
* ```
*/
function updateCurrency(currency, params) {
return __awaiter(this, void 0, void 0, function* () {
const currencyCode = getCurrencyCode(currency);
if (!currencyCode) {
throw new Error("currencyCode missing from updateCurrency(currency, params)!");
}
if (!params) {
throw new Error("params sent to updateCurrency(currency, params)");
}
const resp = yield lightrail.request("PATCH", `currencies/${encodeURIComponent(currencyCode)}`).send(params);
if (requestUtils_1.isSuccessStatus(resp.status)) {
return requestUtils_1.formatResponse(resp);
}
throw new LightrailRequestError_1.LightrailRequestError(resp);
});
}
exports.updateCurrency = updateCurrency;
/**
* See: https://apidocs.lightrail.com/#operation/DeleteCurrency
*
* This will only be possible if the currency has not been used because
* Transactions cannot be deleted and Transactions must have a valid currency.
*
* Example:
* ```js
* await Lightrail.currencies.deleteCurrency("USD");
* ```
*/
function deleteCurrency(currency) {
return __awaiter(this, void 0, void 0, function* () {
const currencyCode = getCurrencyCode(currency);
const resp = yield lightrail.request("DELETE", `currencies/${encodeURIComponent(currencyCode)}`);
if (requestUtils_1.isSuccessStatus(resp.status) || resp.status === 404) {
return requestUtils_1.formatResponse(resp);
}
throw new LightrailRequestError_1.LightrailRequestError(resp);
});
}
exports.deleteCurrency = deleteCurrency;
/**
* Get currency code from the string (as the ID itself) or Currency object.
*/
function getCurrencyCode(currency) {
if (currency == null) {
throw new Error("currency not set");
}
else if (typeof currency === "string") {
return currency;
}
else if (currency.code) {
return currency.code;
}
else {
throw new Error("currency must be a string or Currency object");
}
}
exports.getCurrencyCode = getCurrencyCode;