lightrail-client
Advanced tools
Comparing version 0.0.9 to 1.0.0
@@ -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* () { |
@@ -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": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
51967
1200
0
4
+ Added@types/jsonwebtoken@^7.2.3
+ Addedjsonwebtoken@^8.1.0
+ Added@types/jsonwebtoken@7.2.8(transitive)
+ Added@types/node@22.10.1(transitive)
+ Addedbuffer-equal-constant-time@1.0.1(transitive)
+ Addedecdsa-sig-formatter@1.0.11(transitive)
+ Addedjsonwebtoken@8.5.1(transitive)
+ Addedjwa@1.4.1(transitive)
+ Addedjws@3.2.2(transitive)
+ Addedlodash.includes@4.3.0(transitive)
+ Addedlodash.isboolean@3.0.3(transitive)
+ Addedlodash.isinteger@4.0.4(transitive)
+ Addedlodash.isnumber@3.0.3(transitive)
+ Addedlodash.isplainobject@4.0.6(transitive)
+ Addedlodash.isstring@4.0.1(transitive)
+ Addedlodash.once@4.1.1(transitive)
+ Addedsemver@5.7.2(transitive)
+ Addedundici-types@6.20.0(transitive)
Updatedsuperagent@^3.8.1