Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

lightrail-client

Package Overview
Dependencies
Maintainers
4
Versions
81
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lightrail-client - npm Package Compare versions

Comparing version 0.0.9 to 1.0.0

4

dist/contacts.d.ts

@@ -12,6 +12,2 @@ import { Pagination } from "./model/Pagination";

}>;
/**
* This assumes email is unique, which I think we're moving to.
*/
export declare function getContactByEmail(email: string): Promise<Contact>;
export declare function getContactById(contactId: string): Promise<Contact>;

@@ -18,0 +14,0 @@ export declare function getContactByUserSuppliedId(userSuppliedId: string): Promise<Contact>;

@@ -39,15 +39,2 @@ "use strict";

exports.getContacts = getContacts;
/**
* This assumes email is unique, which I think we're moving to.
*/
function getContactByEmail(email) {
return __awaiter(this, void 0, void 0, function* () {
const resp = yield this.getContacts({ email });
if (resp.contacts.length > 0) {
return resp.contacts[0];
}
return null;
});
}
exports.getContactByEmail = getContactByEmail;
function getContactById(contactId) {

@@ -54,0 +41,0 @@ return __awaiter(this, void 0, void 0, function* () {

30

dist/index.d.ts

@@ -10,7 +10,27 @@ /// <reference types="superagent" />

import { LightrailRequestError } from "./LightrailRequestError";
export declare let apiKey: string;
export declare let restRoot: string;
export declare let logRequests: boolean;
export declare function configure(options: LightrailOptions): void;
export { LightrailOptions, LightrailRequestError, cards, contacts, model, params, programs };
/**
* These values are all configurable from configure().
*/
export declare const configuration: LightrailOptions;
/**
* Configure the Lightrail client.
*/
export declare function configure(options: Partial<LightrailOptions>): void;
export declare function request(method: string, path: string): superagent.Request;
export { LightrailOptions, LightrailRequestError, cards, contacts, model, params, programs };
/**
* Generate a shopper token that can be used to make Lightrail calls
* restricted to that particular shopper. The shopper can be defined by the
* contactId, userSuppliedId, or shopperId.
*
* eg: `generateShopperToken({shopperId: "user-12345"});`
*
* @param contact an object that defines one of: contactId, userSuppliedId or shopperId
* @param validityInSeconds the number of seconds the shopper token will be valid for
* @returns the shopper token
*/
export declare function generateShopperToken(contact: {
contactId?: string;
userSuppliedId?: string;
shopperId?: string;
}, validityInSeconds?: number): string;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const jsonwebtoken = require("jsonwebtoken");
const superagent = require("superagent");

@@ -17,6 +18,14 @@ const superagentLogger = require("superagent-logger");

exports.LightrailRequestError = LightrailRequestError_1.LightrailRequestError;
// These values are all configurable from configure().
exports.apiKey = null;
exports.restRoot = "https://api.lightrail.com/v1/";
exports.logRequests = false;
/**
* These values are all configurable from configure().
*/
exports.configuration = {
apiKey: null,
restRoot: "https://api.lightrail.com/v1/",
sharedSecret: null,
logRequests: false
};
/**
* Configure the Lightrail client.
*/
function configure(options) {

@@ -27,12 +36,30 @@ if (!options) {

if (options.hasOwnProperty("apiKey")) {
exports.apiKey = options.apiKey;
if (typeof options.apiKey !== "string") {
throw new Error("apiKey must be a string");
}
exports.configuration.apiKey = options.apiKey;
}
if (options.hasOwnProperty("restRoot")) {
exports.restRoot = options.restRoot;
if (!exports.restRoot.endsWith("/")) {
exports.restRoot = exports.restRoot + "/";
if (typeof options.restRoot !== "string") {
throw new Error("restRoot must be a string");
}
exports.configuration.restRoot = options.restRoot;
if (!exports.configuration.restRoot.endsWith("/")) {
exports.configuration.restRoot = exports.configuration.restRoot + "/";
}
}
if (options.hasOwnProperty("sharedSecret")) {
if (typeof options.sharedSecret !== "string") {
throw new Error("sharedSecret must be a string");
}
exports.configuration.sharedSecret = options.sharedSecret;
}
if (options.hasOwnProperty("logRequests")) {
exports.logRequests = options.logRequests;
if (typeof options.logRequests !== "boolean") {
throw new Error("logRequests must be a boolean");
}
if (options.logRequests && !superagentLogger) {
throw new Error("logRequests is set to true but optional dependency superagent-logger was not found");
}
exports.configuration.logRequests = options.logRequests;
}

@@ -42,14 +69,12 @@ }

function request(method, path) {
if (!exports.configuration.apiKey) {
throw new Error("apiKey not set");
}
// We can do some fancy things with superagent here like request rate
// throttling or automatic retry on particular codes.
let r = superagent(method, exports.restRoot + path)
.set("Authorization", `Bearer ${exports.apiKey}`)
let r = superagent(method, exports.configuration.restRoot + path)
.set("Authorization", `Bearer ${exports.configuration.apiKey}`)
.ok(() => true);
if (exports.logRequests) {
if (superagentLogger) {
r = r.use(superagentLogger);
}
else {
console.error("logRequests is set to true but optional dependency superagent-logger was not found");
}
if (exports.configuration.logRequests && superagentLogger) {
r = r.use(superagentLogger);
}

@@ -59,1 +84,50 @@ return r;

exports.request = request;
/**
* Generate a shopper token that can be used to make Lightrail calls
* restricted to that particular shopper. The shopper can be defined by the
* contactId, userSuppliedId, or shopperId.
*
* eg: `generateShopperToken({shopperId: "user-12345"});`
*
* @param contact an object that defines one of: contactId, userSuppliedId or shopperId
* @param validityInSeconds the number of seconds the shopper token will be valid for
* @returns the shopper token
*/
function generateShopperToken(contact, validityInSeconds = 43200) {
if (!exports.configuration.apiKey) {
throw new Error("apiKey not set");
}
if (!exports.configuration.sharedSecret) {
throw new Error("sharedSecret not set");
}
if (!contact.contactId && !contact.userSuppliedId && !contact.shopperId) {
throw new Error("one of contact.contactId, contact.userSuppliedId or contact.shopperId must be set");
}
if (typeof validityInSeconds !== "number" || validityInSeconds <= 0) {
throw new Error("validityInSeconds must be a number > 0");
}
const merchantJwtPayload = jsonwebtoken.decode(exports.configuration.apiKey);
if (!merchantJwtPayload || !merchantJwtPayload.g || !merchantJwtPayload.g.gui) {
throw new Error("apiKey not valid");
}
const nowInSeconds = Date.now() / 1000;
return jsonwebtoken.sign({
g: {
gui: merchantJwtPayload.g.gui,
coi: contact.contactId || undefined,
cui: contact.userSuppliedId || undefined,
shi: contact.shopperId || undefined
},
iss: "MERCHANT",
iat: nowInSeconds,
exp: nowInSeconds + validityInSeconds
}, exports.configuration.sharedSecret, {
header: {
alg: "HS256",
typ: "JWT",
ver: 3,
vav: 1
}
});
}
exports.generateShopperToken = generateShopperToken;

@@ -0,5 +1,22 @@

/**
* Options to configure the Lightrail client.
*/
export interface LightrailOptions {
apiKey?: string;
restRoot?: string;
logRequests?: boolean;
/**
* The Lightrail API key as retrieved from the web app.
*/
apiKey: string;
/**
* The REST root URL. Usually this is only set for testing.
*/
restRoot: string;
/**
* The shared secret as set in the web app.
*/
sharedSecret: string;
/**
* Whether to log requests using superagent-logger. If `true` then
* superagent-logger must be available.
*/
logRequests: boolean;
}
{
"name": "lightrail-client",
"version": "0.0.9",
"version": "1.0.0",
"description": "A Javascript and Typescript client for Lightrail",

@@ -25,3 +25,5 @@ "main": "dist/index.js",

"contributors": [
"Jeffery Grajkowski <pushplay@gmail.com> (https://github.com/pushplay/)"
"Jeffery Grajkowski <pushplay@gmail.com> (https://github.com/pushplay)",
"Tana Jukes <tana.jukes@gmail.com> (https://github.com/tjukes)",
"Tim Jordison (https://github.com/Tim-Jordison)"
],

@@ -35,14 +37,16 @@ "license": "MIT",

"@types/chai": "^4.0.4",
"@types/mocha": "^2.2.43",
"@types/node": "^8.0.28",
"@types/superagent": "^3.5.5",
"@types/mocha": "^2.2.44",
"@types/node": "^8.0.52",
"@types/superagent": "^3.5.6",
"chai": "^4.1.2",
"mocha": "^3.5.3",
"mocha": "^4.0.1",
"rimraf": "^2.6.2",
"ts-node": "^3.3.0",
"tslint": "^5.7.0",
"typescript": "^2.5.2"
"tslint": "^5.8.0",
"typescript": "^2.6.1"
},
"dependencies": {
"superagent": "^3.6.0"
"@types/jsonwebtoken": "^7.2.3",
"jsonwebtoken": "^8.1.0",
"superagent": "^3.8.1"
},

@@ -49,0 +53,0 @@ "optionalDependencies": {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc