lightrail-client
Version:
A Javascript and Typescript client for Lightrail
197 lines (196 loc) • 7.62 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.getContactId = exports.deleteContact = exports.detachContactFromValue = exports.attachContactToValue = exports.updateContact = exports.listContactsValues = exports.listContacts = exports.getContact = exports.createContact = void 0;
const lightrail = require("./");
const requestUtils_1 = require("./requestUtils");
const LightrailRequestError_1 = require("./LightrailRequestError");
/**
* See: https://apidocs.lightrail.com/#operation/CreateContact
*
* Example:
* ```js
* const contact = await Lightrail.contacts.createContact({
* id: "abcdefg",
* email: "ex@example.com"
* });
* ```
*/
function createContact(params) {
return __awaiter(this, void 0, void 0, function* () {
if (!params) {
throw new Error("params not set");
}
else if (!params.id) {
throw new Error("params.id not set");
}
const resp = yield lightrail.request("POST", "contacts").send(params);
if (requestUtils_1.isSuccessStatus(resp.status)) {
return requestUtils_1.formatResponse(resp);
}
throw new LightrailRequestError_1.LightrailRequestError(resp);
});
}
exports.createContact = createContact;
/**
* See: https://apidocs.lightrail.com/#operation/GetaContact
*
* Example:
* ```js
* const contact = await Lightrail.contacts.getContact("abcdefg");
* ```
*/
function getContact(contact) {
return __awaiter(this, void 0, void 0, function* () {
const contactId = getContactId(contact);
const resp = yield lightrail.request("GET", `contacts/${encodeURIComponent(contactId)}`);
if (requestUtils_1.isSuccessStatus(resp.status) || resp.status === 404) {
return requestUtils_1.formatResponse(resp);
}
throw new LightrailRequestError_1.LightrailRequestError(resp);
});
}
exports.getContact = getContact;
/**
* See: https://apidocs.lightrail.com/#operation/ListContacts
*
* Example:
* ```js
* const contacts = await Lightrail.contacts.listContacts({limit: 5});
* const contactsLimited = await Lightrail.contacts.listContacts({limit: 5});
* ```
*/
function listContacts(params) {
return __awaiter(this, void 0, void 0, function* () {
const resp = yield lightrail.request("GET", "contacts").query(requestUtils_1.formatFilterParams(params));
if (requestUtils_1.isSuccessStatus(resp.status)) {
return requestUtils_1.formatResponse(resp);
}
throw new LightrailRequestError_1.LightrailRequestError(resp);
});
}
exports.listContacts = listContacts;
/**
* See: https://apidocs.lightrail.com/#operation/ListaContactsValues
*
* Example:
* ```js
* const values = await Lightrail.contacts.listContactsValues("abcdefg");
* const valuesLimited = await Lightrail.contacts.listContactsValues("abcdefg", {limit: 5});
* ```
*/
function listContactsValues(contact, params) {
return __awaiter(this, void 0, void 0, function* () {
const contactId = getContactId(contact);
const resp = yield lightrail.request("GET", `contacts/${encodeURIComponent(contactId)}/values`).query(params);
if (requestUtils_1.isSuccessStatus(resp.status)) {
return requestUtils_1.formatResponse(resp);
}
throw new LightrailRequestError_1.LightrailRequestError(resp);
});
}
exports.listContactsValues = listContactsValues;
/**
* See: https://apidocs.lightrail.com/#operation/UpdateContact
*
* Example:
* ```js
* const updatedContact = await Lightrail.contacts.updateContact("abcdefg", {email: "new.ex@example.com"});
* ```
*/
function updateContact(contact, params) {
return __awaiter(this, void 0, void 0, function* () {
const contactId = getContactId(contact);
const resp = yield lightrail.request("PATCH", `contacts/${encodeURIComponent(contactId)}`).send(params);
if (requestUtils_1.isSuccessStatus(resp.status)) {
return requestUtils_1.formatResponse(resp);
}
throw new LightrailRequestError_1.LightrailRequestError(resp);
});
}
exports.updateContact = updateContact;
/**
* See: https://apidocs.lightrail.com/#operation/AttachContactToValue
*
* Example:
* ```js
* const valueAttachedById = await Lightrail.contacts.attachContactToValue("abcdefg", {valueId: "hijklmnop"});
* const valueAttachedByCode = await Lightrail.contacts.attachContactToValue("abcdefg", {code: "PROMOCODE"});
* ```
*/
function attachContactToValue(contact, params) {
return __awaiter(this, void 0, void 0, function* () {
const contactId = getContactId(contact);
const resp = yield lightrail.request("POST", `contacts/${encodeURIComponent(contactId)}/values/attach`).send(params);
if (requestUtils_1.isSuccessStatus(resp.status)) {
return requestUtils_1.formatResponse(resp);
}
throw new LightrailRequestError_1.LightrailRequestError(resp);
});
}
exports.attachContactToValue = attachContactToValue;
/**
* See: https://apidocs.lightrail.com/#operation/DetachContactFromValue
*
* Example:
* ```js
* const valueDetached = await Lightrail.contacts.detachContactFromValue("abcdefg", {valueId: "hijklmnop"});
* ```
*/
function detachContactFromValue(contact, params) {
return __awaiter(this, void 0, void 0, function* () {
const contactId = getContactId(contact);
const resp = yield lightrail.request("POST", `contacts/${encodeURIComponent(contactId)}/values/detach`).send(params);
if (requestUtils_1.isSuccessStatus(resp.status)) {
return requestUtils_1.formatResponse(resp);
}
throw new LightrailRequestError_1.LightrailRequestError(resp);
});
}
exports.detachContactFromValue = detachContactFromValue;
/**
* See: https://apidocs.lightrail.com/#operation/DeleteContact
*
* Example:
* ```js
* await Lightrail.contacts.deleteContact("abcdefg"});
* ```
*/
function deleteContact(contact) {
return __awaiter(this, void 0, void 0, function* () {
const contactId = getContactId(contact);
const resp = yield lightrail.request("DELETE", `contacts/${encodeURIComponent(contactId)}`);
if (requestUtils_1.isSuccessStatus(resp.status) || resp.status === 404) {
return requestUtils_1.formatResponse(resp);
}
throw new LightrailRequestError_1.LightrailRequestError(resp);
});
}
exports.deleteContact = deleteContact;
/**
* @internal
* Get contactId from the string (as the ID itself) or Contact object.
*/
function getContactId(contact) {
if (!contact) {
throw new Error("contact not set");
}
else if (typeof contact === "string") {
return contact;
}
else if (contact.id) {
return contact.id;
}
else {
throw new Error("contact must be a string for contactId or a Contact object");
}
}
exports.getContactId = getContactId;