1.0.1 • Published 6 months ago

@hijacker/core v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

Hijacker

npm Build Status Coverage Status

Hijacker is a develoment too meant to proxy to an existing API while being able to mock and modify specific requests via rules.

Get Started

Install

Locally

npm i -D @hijacker/core

Globally

npm i -g @hijacker/core

Create Config

Create config file named hijacker.config.js with the following:

module.exports = {
  port: 3000,
  baseRule: {
    baseUrl: '<YOUR_API_URL>'
  },
  rules: []
}

View Config for more information.

Run

Add a hijacker script to your package.json file: "hijacker": "hijacker".

Then run the following command in the directory with your config file:

npm run hijacker

Your hijacker instance should now be running at http://localhost:3000 with the interface available at http://localhost:3000/hijacker.

Config

Config

PropertyDescriptonTypeRequiredDefault
portPort that you want the hijacker instance to run onnumberyes
baseRuleBase rule that other rules will inhert from. See Base RulePartial<Rule>yes
rulesArray of hijacker rules. See RulePartial<Rule>[]yes
loggerLogger configuration options. See LoggerLoggerOptionsno

Rule

A tells hijacker how to handle certain requests. If no matching rule is found, hijacker will proxy the request to the API server.

PropertyDescriptionTypeRequiredDefault
nameRule name. Used for display purposes onlystringno
disabledIf rule is disabled it can't be matchedbooleannofalse
typeRule type. Used to determine which RuleType to use for matching and handling the requeststringrest
baseUrlBase URL for API you want to point hijacker at for specific rulestringno

Base Rule

The base rule allows you set default values for the

Rule Types

By default hijacker includes Rest and a GraphQL rule types. Each rule type can have additional properties that build upon the base rule properties. Each rule will be merged with the Base Rule and use it's values unless overridden in the rule.

The base rule has the same values as Rules. The only difference is that baseUrl is required so that requests that have no matching rule will be able to be proxied.

Rest Rule

The rest rule type is meant for REST APIs, and will be used for rule's where type is set to rest. The rest rule type has the same properties as the Rule, as well as the following:

PropertyDescriptionTypeRequiredDefault
skipApibooleannofalse
methodHTTP method of the request to match on.GET | HEAD | POST | PUT | DELETE | OPTIONS | TRACE | PATCH | ALLnoALL
bodyResponse body to send back when a rule matchesanynoundefined
pathURL path of request to match on.stringyes
statusCodeStatus code you would like hijacker return for a requestnumberno
routeToPath to redirect request to at the API urlstringno

GraphQL Rule

The GraphQL rule type expands on the Rest Rule but adds matching that works for GraphQL queries. Right now the GraphQL rule type only matches on the operation name.

PropertyDescriptionTypeRequiredDefault
operationNameOperation to match in graphql querystringyes

Example:

query TestQuery {
  posts {
    name
    id
  }
}

For the above query, TestQuery is the operationName that should be used to match query in a rule.

Logger

Settings for built in logger. This may eventually get changed to accepting a custom logger. | Property | Description | Type | Required | Default | | -------- | ----------- | ---- | -------- | ------- | | level | Max log level that you wan't logged to the console. | SILLY | DEBUG | HTTP | INFO | WARN | ERROR | NONE | no | INFO |

Extending Hijacker

Hijacker was made with extensibility in mind. There are a two ways Hijacker can be extended: custom rule types and hooks.

Plugins

Plugins are how hooks and rule types are packaged up to be added to a config for a hijacker user to use.

PropertyDescriptionTypeRequiredDefault
nameName of pluginstringyes
initPluginFunction called to initialize plugin and pass HijackerContext(context: HijackerContext) => voidno
hooksArray of hook names that the plugin allows to be hooked intostring[]no
handlersList of hook handlers for pluginRecord<string, function>no
ruleTypesArray of custom rule typesRuleType[]no

Custom Rule Types

It is possible to create custom rule types, that allow for request matching and handling. You can view RestRule and GraphqlRule for examples.

PropertyDescriptionTypeRequiredDefault
typeRule type used to match rulesstringyes
createRuleCreate rule object used for the handler(rule: Partial<Rule<T>>) => Rule<T>yes
isMatchUsed to determine if request matches a rule(request: HijackerRequest, rule: Rule<T>) => booleanyes
handlerRequest handler for rule type(request: Request<T>, baseRule: Partial<Rule<T>>, context: HijackerContext) => Promise<HijackerResponse>yes

Hooks

Hooks allow plugins to listen and modify objects at specific points in the request lifecycle. Hook handlers are passed an object that they can modify and return an object of the same shape. Right now Hijacker has hooks for the following events:

Hook NameTypeDescriptionSynchronous
HIJACKER_STARTConfigExecuted when hijacker starts up and allows modifying the hijacker configyes
HIJACKER_REQUESTHijackerRequstBegining of request. Called before rule matchedno
HIJACKER_RESPONSEHijackerResponseCalled after request handler, before response returned to clientno

Plugins are able to add additional hooks using the HookManager which allow other plugins to modify their functionality.