@microcode/request-schema
``` npm install --save @microcode/request-schema ```
Last updated 3 months ago by jsvennevid .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @microcode/request-schema 
SYNC missed versions from official npm registry.

Installation

    npm install --save @microcode/request-schema

API

new Schema(methods) - Create a new schema, supporting a set of methods.

  • methods - What methods to support registration on.

Example:

    new Schema(['create','read','update','delete']);

Creates a new schema supporting CRUD.

schema.on(method, path, [filters], func) - Register a function on a specific method and path in the schema.

  • method - What method to register on
  • path - What path to register on
  • filters - Optional list of filters to run before the function
  • func - Function to execute

The function arguments are dynamic (determined when registered), and supports all extracted parameters in the path, along with two special parameters data and context.

  • For normal functions, context is used as the context for the function call.
  • Arrow functions requires context as a parameter since the context is already overridden.

You can register multiple functions on the same path. If a filter on a function fails (due to e.g. missing authorization), the next function will attempt to run.

Example:

    schema.on('read', '/objects/:object_id/attributes', new AuthCheckFilter(), (object_id, data, context) => { ... });

schema.run(method, path, data, context) - Run a registered method

Example:

    schema.run('read', '/objects/1234/attributes', {}, { user_id: 5763 });

Filters

Filters allow for sharing common functionality to verify and or modify a request. Example use cases could be authentication or request caching.

A filter inherits from the Filter class, and implements the method run. This method may return a promise.

The data passed into the filter method is a FilterData object, and it exposes the following attributes and methods.

  • method - Method type being called
  • data - Data being passed into the function
  • context - Context used in this call
  • resolve(value) - Call to resolve early (without calling function)
  • reject(err) - Call to reject filter (same as throwing an error)
  • on_resolve(func) - Register a callback that runs after the function has successfully executed. Parameters are:
    • result - Result returned from function
    • context - Context used in function

Resolve callbacks execute in the order they are registered, and will execute even if a filters resolves early. If a callback throws an exception, the following functions will NOT be executed.

Resolve callbacks may return a promise.

Results

Calling a function will generate a Result (unless an error occurs). This result contains the resulting value and also a map of meta values as registered by the different filters. This allows you to decorate results, if you e.g. want to keep track of a specific action taken by a filter.

Paths

  • Paths are separated by forward slashes. Example: /this/is/a/path
  • Parameters are supported by prefixing a path component with a colon. Example: /this/is/a/:parameter. Parameters are made available to the registered function.

License

This package uses the MIT license. Please read [LICENSE.md] for details.

Current Tags

  • 0.7.4                                ...           latest (3 months ago)

22 Versions

  • 0.7.4                                ...           3 months ago
  • 0.7.3                                ...           3 months ago
  • 0.7.2                                ...           3 months ago
  • 0.7.1                                ...           3 months ago
  • 0.7.0                                ...           3 months ago
  • 0.6.0                                ...           6 months ago
  • 0.4.0                                ...           7 months ago
  • 0.3.0                                ...           a year ago
  • 0.1.0                                ...           2 years ago
  • 0.0.14                                ...           2 years ago
  • 0.0.13                                ...           2 years ago
  • 0.0.12                                ...           2 years ago
  • 0.0.11                                ...           2 years ago
  • 0.0.9                                ...           2 years ago
  • 0.0.8                                ...           2 years ago
  • 0.0.7                                ...           2 years ago
  • 0.0.6                                ...           2 years ago
  • 0.0.5                                ...           2 years ago
  • 0.0.4                                ...           2 years ago
  • 0.0.3                                ...           2 years ago
  • 0.0.2                                ...           2 years ago
  • 0.0.1                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 2
Last Day 0
Last Week 0
Last Month 0
Dependencies (5)
Dev Dependencies (14)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |