Socket
Socket
Sign inDemoInstall

before-after-hook

Package Overview
Dependencies
0
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.2.3 to 3.0.0

23

index.d.ts

@@ -28,6 +28,6 @@ type HookMethod<Options, Result> = (

type TypeStore =
| ({ [key in TypeStoreKeyLong]?: any } &
| ({ [key in TypeStoreKeyLong]?: unknown } &
{ [key in TypeStoreKeyShort]?: never })
| ({ [key in TypeStoreKeyLong]?: never } &
{ [key in TypeStoreKeyShort]?: any });
{ [key in TypeStoreKeyShort]?: unknown });
type GetType<

@@ -41,3 +41,3 @@ Store extends TypeStore,

? Store[ShortKey]
: any;
: unknown;

@@ -47,3 +47,3 @@ export interface HookCollection<

string,
{ Options: any; Result: any; Error: any }
{ Options: unknown; Result: unknown; Error: unknown }
>,

@@ -166,10 +166,3 @@ HookName extends keyof HooksType = keyof HooksType

interface Hook {
new <
HooksType extends Record<string, TypeStore> = Record<
string,
{ Options: any; Result: any; Error: any }
>
>(): HookCollection<HooksType>;
declare const Hook: {
/**

@@ -184,8 +177,4 @@ * Creates a collection of hooks

Singular: Singular;
}
};
export const Hook: Hook;
export const Collection: Collection;
export const Singular: Singular;
export default Hook;

@@ -1,11 +0,13 @@

var register = require("./lib/register");
var addHook = require("./lib/add");
var removeHook = require("./lib/remove");
// @ts-check
import { register } from "./lib/register.js";
import { addHook } from "./lib/add.js";
import { removeHook } from "./lib/remove.js";
// bind with array of arguments: https://stackoverflow.com/a/21792913
var bind = Function.bind;
var bindable = bind.bind(bind);
const bind = Function.bind;
const bindable = bind.bind(bind);
function bindApi(hook, state, name) {
var removeHookRef = bindable(removeHook, null).apply(
const removeHookRef = bindable(removeHook, null).apply(
null,

@@ -16,4 +18,4 @@ name ? [state, name] : [state]

hook.remove = removeHookRef;
["before", "error", "after", "wrap"].forEach(function (kind) {
var args = name ? [state, kind, name] : [state, kind];
["before", "error", "after", "wrap"].forEach((kind) => {
const args = name ? [state, kind, name] : [state, kind];
hook[kind] = hook.api[kind] = bindable(addHook, null).apply(null, args);

@@ -23,8 +25,8 @@ });

function HookSingular() {
var singularHookName = "h";
var singularHookState = {
function Singular() {
const singularHookName = Symbol("Singular");
const singularHookState = {
registry: {},
};
var singularHook = register.bind(null, singularHookState, singularHookName);
const singularHook = register.bind(null, singularHookState, singularHookName);
bindApi(singularHook, singularHookState, singularHookName);

@@ -34,8 +36,8 @@ return singularHook;

function HookCollection() {
var state = {
function Collection() {
const state = {
registry: {},
};
var hook = register.bind(null, state);
const hook = register.bind(null, state);
bindApi(hook, state);

@@ -46,20 +48,2 @@

var collectionHookDeprecationMessageDisplayed = false;
function Hook() {
if (!collectionHookDeprecationMessageDisplayed) {
console.warn(
'[before-after-hook]: "Hook()" repurposing warning, use "Hook.Collection()". Read more: https://git.io/upgrade-before-after-hook-to-1.4'
);
collectionHookDeprecationMessageDisplayed = true;
}
return HookCollection();
}
Hook.Singular = HookSingular.bind();
Hook.Collection = HookCollection.bind();
module.exports = Hook;
// expose constructors as a named property for TypeScript
module.exports.Hook = Hook;
module.exports.Singular = Hook.Singular;
module.exports.Collection = Hook.Collection;
export default { Singular, Collection };

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

module.exports = addHook;
// @ts-check
function addHook(state, kind, name, hook) {
var orig = hook;
export function addHook(state, kind, name, hook) {
const orig = hook;
if (!state.registry[name]) {

@@ -10,3 +10,3 @@ state.registry[name] = [];

if (kind === "before") {
hook = function (method, options) {
hook = (method, options) => {
return Promise.resolve()

@@ -19,11 +19,11 @@ .then(orig.bind(null, options))

if (kind === "after") {
hook = function (method, options) {
var result;
hook = (method, options) => {
let result;
return Promise.resolve()
.then(method.bind(null, options))
.then(function (result_) {
.then((result_) => {
result = result_;
return orig(result, options);
})
.then(function () {
.then(() => {
return result;

@@ -35,6 +35,6 @@ });

if (kind === "error") {
hook = function (method, options) {
hook = (method, options) => {
return Promise.resolve()
.then(method.bind(null, options))
.catch(function (error) {
.catch((error) => {
return orig(error, options);

@@ -41,0 +41,0 @@ });

@@ -1,4 +0,4 @@

module.exports = register;
// @ts-check
function register(state, name, method, options) {
export function register(state, name, method, options) {
if (typeof method !== "function") {

@@ -13,3 +13,3 @@ throw new Error("method for before hook must be a function");

if (Array.isArray(name)) {
return name.reverse().reduce(function (callback, name) {
return name.reverse().reduce((callback, name) => {
return register.bind(null, state, name, callback, options);

@@ -19,3 +19,3 @@ }, method)();

return Promise.resolve().then(function () {
return Promise.resolve().then(() => {
if (!state.registry[name]) {

@@ -25,3 +25,3 @@ return method(options);

return state.registry[name].reduce(function (method, registered) {
return state.registry[name].reduce((method, registered) => {
return registered.hook.bind(null, method, options);

@@ -28,0 +28,0 @@ }, method)();

@@ -1,4 +0,4 @@

module.exports = removeHook;
// @ts-check
function removeHook(state, name, method) {
export function removeHook(state, name, method) {
if (!state.registry[name]) {

@@ -8,4 +8,4 @@ return;

var index = state.registry[name]
.map(function (registered) {
const index = state.registry[name]
.map((registered) => {
return registered.orig;

@@ -12,0 +12,0 @@ })

{
"name": "before-after-hook",
"version": "2.2.3",
"type": "module",
"version": "3.0.0",
"description": "asynchronous before/error/after hooks for internal functionality",
"main": "index.js",
"exports": "./index.js",
"types": "./index.d.ts",
"files": [

@@ -11,20 +13,11 @@ "index.js",

],
"types": "./index.d.ts",
"scripts": {
"prebuild": "rimraf dist && mkdirp dist",
"build": "browserify index.js --standalone=Hook > dist/before-after-hook.js",
"postbuild": "uglifyjs dist/before-after-hook.js -mc > dist/before-after-hook.min.js",
"lint": "prettier --check '{lib,test,examples}/**/*' 'index.*' README.md package.json",
"lint:fix": "prettier --write '{lib,test,examples}/**/*' 'index.*' README.md package.json",
"pretest": "npm run -s lint",
"test": "npm run -s test:node | tap-spec",
"posttest": "npm run validate:ts",
"test:node": "node test",
"test:watch": "gaze 'clear && node test | tap-min' 'test/**/*.js' 'index.js' 'lib/**/*.js'",
"test:coverage": "istanbul cover test",
"test:coverage:upload": "istanbul-coveralls",
"validate:ts": "tsc --strict --target es6 index.d.ts",
"postvalidate:ts": "tsc --noEmit --strict --target es6 test/typescript-validate.ts",
"presemantic-release": "npm run build",
"semantic-release": "semantic-release"
"test": "npm run test:code && npm run test:tsc && npm run test:tsd && npm run lint",
"test:code": "c8 --100 ava test/*.test.js",
"test:tsc": "tsc --allowJs --noEmit --esModuleInterop --skipLibCheck --lib es2020 index.js",
"test:tsd": "tsd",
"lint": "prettier --check \"*.{js,json,ts,md}\" \".github/**/*.yml\"",
"lint:fix": "prettier --write \"*.{js,json,ts,md}\" \".github/**/*.yml\"",
"coverage": "c8 report --reporter html",
"postcoverage": "open-cli coverage/index.html"
},

@@ -39,18 +32,9 @@ "repository": "github:gr2m/before-after-hook",

"license": "Apache-2.0",
"dependencies": {},
"devDependencies": {
"browserify": "^16.0.0",
"gaze-cli": "^0.2.0",
"istanbul": "^0.4.0",
"istanbul-coveralls": "^1.0.3",
"mkdirp": "^1.0.3",
"ava": "^4.3.3",
"c8": "^7.12.0",
"prettier": "^2.0.0",
"rimraf": "^3.0.0",
"semantic-release": "^19.0.3",
"simple-mock": "^0.8.0",
"tap-min": "^2.0.0",
"tap-spec": "^5.0.0",
"tape": "^5.0.0",
"typescript": "^3.5.3",
"uglify-js": "^3.9.0"
"sinon": "^14.0.1",
"tsd": "^0.24.1",
"typescript": "^4.8.4"
},

@@ -57,0 +41,0 @@ "release": {

@@ -10,2 +10,30 @@ # before-after-hook

<table>
<tbody valign=top align=left>
<tr><th>
Browsers
</th><td width=100%>
Load <code>before-after-hook</code> directly from <a href="https://cdn.skypack.dev">cdn.skypack.dev</a>
```html
<script type="module">
import Hoom from "https://cdn.skypack.dev/before-after-hook";
</script>
```
</td></tr>
<tr><th>
Node
</th><td>
Install with <code>npm install before-after-hook</code>
```js
import GitHubProject from "before-after-hook";
```
</td></tr>
</tbody>
</table>
### Singular hook

@@ -18,6 +46,9 @@

// Create a hook
function getData(options) {
return hook(fetchFromDatabase, options)
.then(handleData)
.catch(handleGetError);
async function getData(options) {
try {
const result = await hook(fetchFromDatabase, options);
return handleData(result);
} catch (error) {
return handleGetError(error);
}
}

@@ -41,6 +72,9 @@

// Create a hook
function getData(options) {
return hookCollection("get", fetchFromDatabase, options)
.then(handleData)
.catch(handleGetError);
async function getData(options) {
try {
const result = await hookCollection("get", fetchFromDatabase, options);
return handleData(result);
} catch (error) {
return handleGetError(error);
}
}

@@ -95,10 +129,2 @@

## Install
```
npm install before-after-hook
```
Or download [the latest `before-after-hook.min.js`](https://github.com/gr2m/before-after-hook/releases/latest).
## API

@@ -232,3 +258,3 @@

"save",
function (record) {
(record) => {
return store.save(record);

@@ -255,3 +281,3 @@ },

["add", "save"],
function (record) {
(record) => {
return store.save(record);

@@ -280,6 +306,6 @@ },

"add",
function (record) {
(record) => {
return hookCollection(
"save",
function (record) {
(record) => {
return store.save(record);

@@ -379,3 +405,3 @@ },

```js
hookCollection.error("save", function (error, options) {
hookCollection.error("save", (error, options) => {
if (error.ignore) return;

@@ -423,3 +449,3 @@ throw error;

```js
hookCollection.after("save", function (result, options) {
hookCollection.after("save", (result, options) => {
if (result.updatedAt) {

@@ -469,3 +495,3 @@ app.emit("update", result);

```js
hookCollection.wrap("save", async function (saveInDatabase, options) {
hookCollection.wrap("save", async (saveInDatabase, options) => {
if (!record.name) {

@@ -540,3 +566,3 @@ throw new Error("name property is required");

```ts
import { Hook } from "before-after-hook";
import Hook from "before-after-hook";

@@ -543,0 +569,0 @@ type TOptions = { foo: string }; // type for options

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc