Comparing version 0.4.10 to 0.5.1
{ | ||
"name": "abl-utils", | ||
"title": "ABL - utils", | ||
"version": "0.4.10", | ||
"version": "0.5.1", | ||
"license": "MIT", | ||
@@ -26,3 +26,3 @@ "description": "description", | ||
"abl-constants": "git+ssh://github.com/bucket-list/constants.git#v0.1.12", | ||
"abl-lang": "git+ssh://github.com/bucket-list/lang.git#v0.2.1", | ||
"abl-lang": "git+ssh://github.com/bucket-list/lang.git#v0.3.0", | ||
"debug": "2.2.0", | ||
@@ -29,0 +29,0 @@ "escape-string-regexp": "1.0.5", |
"use strict"; | ||
import {translate} from "abl-lang"; | ||
import {date} from "abl-constants/build/date"; | ||
@@ -14,71 +13,4 @@ | ||
function getText(displayName, key, user, fallback) { | ||
return translate(`error.server.${displayName.toLowerCase()}-${key}`, user) || `${displayName} ${fallback}`; | ||
} | ||
export function makeError(key, user, code = 400) { | ||
return _makeError(translate(`error.server.${key}`, user), code); | ||
return _makeError(translate(key, user), code); | ||
} | ||
export function checkModel(user) { | ||
return function checkModelInner(model) { | ||
if (!model) { | ||
throw _makeError(getText(this.constructor.displayName, "not-found", user, "Not Found"), 404); | ||
} else { | ||
return model; | ||
} | ||
}; | ||
} | ||
export function checkUser(user, condition = false) { | ||
return function checkUserInner(model) { | ||
if (user._id.toString() === model[this.constructor.realm]._id.toString() === condition) { | ||
throw makeError("access-denied", user, 403); | ||
} else { | ||
return model; | ||
} | ||
}; | ||
} | ||
export function checkOwner(user, condition = false) { | ||
return function checkOwnerInner(model) { | ||
if (user._id.toString() === model.owner.toString() === condition) { | ||
throw makeError("access-denied", user, 403); | ||
} else { | ||
return model; | ||
} | ||
}; | ||
} | ||
export function checkDefault(defaultContract) { | ||
return function checkDefaultInner(model, request) { | ||
const isDefault = request.params._id.toString() === defaultContract._id.toString(); | ||
if (isDefault && !request.body.default) { | ||
throw makeError("contract-must-have-default-contract", 400); | ||
} else { | ||
return model; | ||
} | ||
}; | ||
} | ||
export function checkActive(isAllowed = false) { | ||
return function checkActiveInner(model, request) { | ||
const isAdmin = !request.user.apiKeys[0].public; | ||
const isActive = model.status === this.constructor.statuses.active; | ||
if (!isActive && !(isAllowed && isAdmin)) { | ||
throw _makeError(getText(this.constructor.displayName, "not-active", request.user, "Is Not Active"), 400); | ||
} else { | ||
return model; | ||
} | ||
}; | ||
} | ||
export function checkPast(isAllowed = false, field = "startTime") { | ||
return function checkPastInner(model, request) { | ||
const isAdmin = !request.user.apiKeys[0].public; | ||
const isPast = model[field] <= date; // <= for tests | ||
if (isPast && !(isAllowed && isAdmin)) { | ||
throw makeError("event-has-passed", request.user); | ||
} | ||
}; | ||
} |
"use strict"; | ||
import crypto from "crypto"; | ||
import {getObject} from "abl-lang"; | ||
import {getObject} from "abl-lang/build/index"; | ||
@@ -6,0 +6,0 @@ export function getType(variable) { |
@@ -5,2 +5,3 @@ "use strict"; | ||
import {isType} from "./misc"; | ||
import {makeError} from "./error"; | ||
@@ -15,4 +16,6 @@ | ||
return user.payment.currency; | ||
} else if (user.location && user.location && user.location.countryCode && ["ca", "us"].includes(user.location.countryCode.toLowerCase())) { | ||
return `${user.location.countryCode.toLowerCase()}d`; | ||
} else { | ||
return user.location && user.location.countryCode.toLowerCase() === "ca" ? "cad" : "usd"; | ||
throw makeError("server.unrecognized-currency", user); | ||
} | ||
@@ -19,0 +22,0 @@ } |
@@ -12,3 +12,2 @@ "use strict"; | ||
const log = debug("utils:response"); | ||
@@ -29,3 +28,4 @@ function _send(request, response) { | ||
export function sendError(error, request, response, next) { | ||
log("sendError", error); | ||
const log = debug("utils:response"); | ||
log(error); | ||
void next; // eslint | ||
@@ -43,14 +43,8 @@ const send = _send(request, response); | ||
status: 400, | ||
message: translate(`error.mongo.${key}`, request.user) || translate("error.mongo.E11000", request.user) | ||
message: translate(`mongo.${key}`, request.user) || translate("mongo.E11000", request.user) | ||
}); | ||
} | ||
if (error.type === "StripeCardError" || error.type === "StripeInvalidRequest") { | ||
return send({ | ||
status: 400, | ||
message: error.message | ||
}); | ||
} | ||
if (!error.status) { | ||
if (process.env.NODE_ENV === "production") { | ||
return send(makeError("server-error", request.user, 500)); | ||
return send(makeError("server.error", request.user, 500)); | ||
} else { | ||
@@ -104,6 +98,3 @@ return send({ | ||
}) | ||
.catch(error => { | ||
log(error); | ||
return sendError(error, request, response); | ||
}) | ||
.catch(error => sendError(error, request, response)) | ||
.done(); | ||
@@ -116,9 +107,3 @@ }; | ||
method(request, response, next) | ||
.then(result => { | ||
log("result", result); | ||
}) | ||
.catch(error => { | ||
log(error); | ||
return sendError(error, request, response); | ||
}) | ||
.catch(error => sendError(error, request, response)) | ||
.done(); | ||
@@ -133,3 +118,2 @@ }; | ||
.catch(error => { | ||
log(error); | ||
switch (error.type) { | ||
@@ -144,6 +128,3 @@ case "StripeCardError": | ||
default: | ||
if (process.env.NODE_ENV === "production") { | ||
sendError(makeError("stripe-bad-request", request.user, 400), request, response); | ||
} | ||
return sendError(error, request, response); | ||
return sendError(process.env.NODE_ENV === "production" ? makeError("api.stripe-bad-request", request.user, 400) : error, request, response); | ||
} | ||
@@ -150,0 +131,0 @@ }) |
Git dependency
Supply chain riskContains a dependency which resolves to a remote git URL. Dependencies fetched from git URLs are not immutable and can be used to inject untrusted code or reduce the likelihood of a reproducible install.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
Git dependency
Supply chain riskContains a dependency which resolves to a remote git URL. Dependencies fetched from git URLs are not immutable and can be used to inject untrusted code or reduce the likelihood of a reproducible install.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
13
18097
458