2.2.3 • Published 5 years ago

hook-schema v2.2.3

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

Hook Schema

A package for adding no operation (noop) hooks to incomplete hook objects

Installation

npm install --save hook-schema

or

yarn add hook-schema

Examples

const { join } = require('path');

const fromSchema = require('hook-schema');
const { readFile } = require('mz/fs');

const { withHooks } = fromSchema({
  readFile: null,
  parseJson: null,
});

/**
 * A simple JSON reading method with hook-schema added for extensibility.
 * hook-schema is more useful in more complicated logic but its ability to
 * decouple different logic.
 */
async function readJson(filePath, partialHooks) {
  const context = {
    path: filePath,
    encoding: 'utf8',
  };
  const hooks = withHooks(partialHooks);

  await hooks.before.readFile(context);
  context.contents = await readFile(context.path, context.encoding);
  await hooks.after.readFile(context);

  await hooks.before.parseJson(context);
  context.json = JSON.parse(context.contents);
  await hooks.after.parseJson(context);

  return context.json;
}

/**
 * Harnesses the utility of hook-schema adding the ability to log messages at
 * each step of the readJson function without adding the code directly into readJson.
 */
async function loggedReadJson(filePath) {
  const hooks = {
    before: {
      async readFile({ path }) {
        console.log(`Reading ${path}...`);
      },
      async parseJson({ path }) {
        console.log(`Parsing ${path}...`);
      },
    },
  };
  return await readJson(filePath, hooks);
}

async function run() {
  await loggedReadJson(join(__dirname, 'package.json'));
}
run();

This documentation was generated using writeme

2.2.3

5 years ago

2.2.2

5 years ago

2.2.1

5 years ago

2.2.0

5 years ago

2.1.0

5 years ago

2.0.0

5 years ago

1.4.0

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.3

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

1.0.0-alpha.0

5 years ago