lightrail-client
Version:
A Javascript and Typescript client for Lightrail
237 lines (236 loc) • 8.55 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.getIssuanceId = exports.getProgramId = exports.getIssuance = exports.listIssuances = exports.createIssuance = exports.deleteProgram = exports.updateProgram = exports.getProgram = exports.listPrograms = exports.createProgram = void 0;
const lightrail = require("./");
const LightrailRequestError_1 = require("./LightrailRequestError");
const requestUtils_1 = require("./requestUtils");
/**
* See: https://apidocs.lightrail.com/#operation/CreateProgram
*
* Example:
* ```js
* const program = await Lightrail.programs.createProgram({
* id: "abcdefg",
* currency: "USD",
* name: "Gift Cards",
* minInitialBalance: 250,
* maxInitialBalance: 50000
* });
* ```
*/
function createProgram(params) {
return __awaiter(this, void 0, void 0, function* () {
if (!params) {
throw new Error("params not set");
}
else {
requestUtils_1.validateRequiredParams(["id"], params);
}
const resp = yield lightrail.request("POST", "programs").send(params);
if (requestUtils_1.isSuccessStatus(resp.status)) {
return requestUtils_1.formatResponse(resp);
}
throw new LightrailRequestError_1.LightrailRequestError(resp);
});
}
exports.createProgram = createProgram;
/**
* See: https://apidocs.lightrail.com/#operation/ListPrograms
*
* Example:
* ```js
* const programs = await Lightrail.programs.listPrograms();
* const programsLimited = await Lightrail.programs.listPrograms({limit: 5});
* ```
*/
function listPrograms(params) {
return __awaiter(this, void 0, void 0, function* () {
const resp = yield lightrail.request("GET", "programs").query(requestUtils_1.formatFilterParams(params));
if (requestUtils_1.isSuccessStatus(resp.status)) {
return requestUtils_1.formatResponse(resp);
}
throw new LightrailRequestError_1.LightrailRequestError(resp);
});
}
exports.listPrograms = listPrograms;
/**
* See: https://apidocs.lightrail.com/#operation/GetaProgram
*
* Example:
* ```js
* const program = await Lightrail.programs.getProgram("abcdefg");
* ```
*/
function getProgram(program) {
return __awaiter(this, void 0, void 0, function* () {
const programId = getProgramId(program);
const resp = yield lightrail.request("GET", `programs/${encodeURIComponent(programId)}`);
if (requestUtils_1.isSuccessStatus(resp.status) || resp.status === 404) {
return requestUtils_1.formatResponse(resp);
}
throw new LightrailRequestError_1.LightrailRequestError(resp);
});
}
exports.getProgram = getProgram;
/**
* See: https://apidocs.lightrail.com/#operation/UpdateProgram
*
* Example:
* ```js
* const program = await Lightrail.programs.updateProgram("abcdefg", {name: "Awesome Gift Cards"});
* ```
*/
function updateProgram(program, params) {
return __awaiter(this, void 0, void 0, function* () {
const programId = getProgramId(program);
if (!params) {
throw new Error("params not set");
}
const resp = yield lightrail.request("PATCH", `programs/${encodeURIComponent(programId)}`).send(params);
if (requestUtils_1.isSuccessStatus(resp.status)) {
return requestUtils_1.formatResponse(resp);
}
throw new LightrailRequestError_1.LightrailRequestError(resp);
});
}
exports.updateProgram = updateProgram;
/**
* See: https://apidocs.lightrail.com/#operation/DeleteProgram
*
* Example:
* ```js
* await Lightrail.programs.deleteProgram("abcdefg");
* ```
*/
function deleteProgram(program) {
return __awaiter(this, void 0, void 0, function* () {
const programId = getProgramId(program);
const resp = yield lightrail.request("DELETE", `programs/${encodeURIComponent(programId)}`);
if (requestUtils_1.isSuccessStatus(resp.status) || resp.status === 404) {
return requestUtils_1.formatResponse(resp);
}
throw new LightrailRequestError_1.LightrailRequestError(resp);
});
}
exports.deleteProgram = deleteProgram;
/**
* See: https://apidocs.lightrail.com/#operation/CreateIssuance
*
* Example:
* ```js
* const issuance = await Lightrail.programs.createIssuance("abcdefg", {
* id: "hijklmnop",
* name: "Some cards",
* count: 500,
* generateCode: {},
* balance: 5000
* });
* ```
*/
function createIssuance(program, params) {
return __awaiter(this, void 0, void 0, function* () {
const programId = getProgramId(program);
if (!params) {
throw new Error("createIssuance(p) params not set");
}
else {
requestUtils_1.validateRequiredParams(["id"], params);
}
const resp = yield lightrail.request("POST", `programs/${encodeURIComponent(programId)}/issuances`).send(params);
if (requestUtils_1.isSuccessStatus(resp.status)) {
return requestUtils_1.formatResponse(resp);
}
throw new LightrailRequestError_1.LightrailRequestError(resp);
});
}
exports.createIssuance = createIssuance;
/**
* See: https://apidocs.lightrail.com/#operation/ListIssuances
*
* Example:
* ```js
* const issuances = await Lightrail.programs.listIssuances("abcdefg");
* const issuancesLimited = await Lightrail.programs.listIssuances("abcdefg", {limit: 5});
* ```
*/
function listIssuances(program, params) {
return __awaiter(this, void 0, void 0, function* () {
const programId = getProgramId(program);
const resp = yield lightrail.request("GET", `programs/${encodeURIComponent(programId)}/issuances`).query(requestUtils_1.formatFilterParams(params));
if (requestUtils_1.isSuccessStatus(resp.status)) {
return requestUtils_1.formatResponse(resp);
}
throw new LightrailRequestError_1.LightrailRequestError(resp);
});
}
exports.listIssuances = listIssuances;
/**
* See: https://apidocs.lightrail.com/#operation/GetanIssuance
*
* Example:
* ```js
* const issuance = await Lightrail.programs.getIssuance("abcdefg", "hijklmnop");
* ```
*/
function getIssuance(program, issuance) {
return __awaiter(this, void 0, void 0, function* () {
const programId = getProgramId(program);
const issuanceId = getIssuanceId(issuance);
if (!issuanceId) {
throw new Error("issuanceId in getIssuance(program, issuanceId) is no set!");
}
const resp = yield lightrail.request("GET", `programs/${encodeURIComponent(programId)}/issuances/${encodeURIComponent(issuanceId)}`);
if (requestUtils_1.isSuccessStatus(resp.status) || resp.status === 404) {
return requestUtils_1.formatResponse(resp);
}
throw new LightrailRequestError_1.LightrailRequestError(resp);
});
}
exports.getIssuance = getIssuance;
/**
* @internal
* Get programId from the string (as the ID itself) or Program object.
*/
function getProgramId(program) {
if (!program) {
throw new Error("program not set");
}
else if (typeof program === "string") {
return program;
}
else if (program.id) {
return program.id;
}
else {
throw new Error("program must be a string for programId or a Program object");
}
}
exports.getProgramId = getProgramId;
/**
* @internal
* Get issuanceId from the string (as the ID itself) or Issuance object.
*/
function getIssuanceId(issuance) {
if (!issuance) {
throw new Error("issuance not set");
}
else if (typeof issuance === "string") {
return issuance;
}
else if (issuance.id) {
return issuance.id;
}
else {
throw new Error("issuance must be a string for issuanceId or a Issuance object");
}
}
exports.getIssuanceId = getIssuanceId;