autharchy v1.5.1
autharchy
proxied/saved/mocked environment to test and develop microservices
Install
You can install it globally
npm -g autharchyor as a dependency
npm -D autharchyCompanions
webapp
Browse the recent requests
server
Allow other to access your local services
Configuration
mkdir .autharchy
cd .autharchy
echo module.exports = {} > config.jsMain config
The file .autharchy/config.js will eventually allow to set default configuration for all the services. Currently it's not used.
Service
Create a folder for each service, with a config.js inside
Example
.autharchy/foo/config.js
module.exports = {
type: 'REST',
remote: 'https://example.com/api',
local: {
ip: '127.0.0.1',
port: 5000,
}
}type (required)
The type of the service. The provided types are REST and graphql
Custom types can be added.
name
It defaults to the folder name.
It's used to determine the collection's name ${type}-${name}
In the example the name is 'foo' and the collection name is 'REST-foo'
remote
The url of the service that you want to proxy
It is not required if you are mocking all the endpoints from the database or a pre or post function.
local
Where to bind the local service.
local.ipdefaults to'0.0.0.0local.portdefaults to80
pre
(context) => context
it's executed just after matching the endpoint.
It can be used to modify the request that will be used to search on the database or to be sent to the proxy
It also can set a response, or modify the configuration.
Please refer to the service type documentation to further instructions regarding the context and the response
toQuery
[(context) => ({ some mongo query }), ...] | false
Requests are cached so after a first fetch you don't need the remote service to be up and running.
You can use pre to set context.conf.toQuery to false to disable the cache search
Otherwise it will loop the array running query after query until it returns a response.
Please refer to the service type documentation to further instructions regarding the defaults and the context schema regarding the request.
toDocument
(context) => document
You can use pre to set context.conf.toDocument to false to skip inserting the request and response in the database
Please refer to the service type documentation to further instructions regarding the defaults and the context schema regarding the request.
post
(context) => context
it's executed just before sending the response.
It can be used to modify the response that will be sent
Please refer to the service type documentation to further instructions regarding the context and the response
skipLogs
Requests are also logged, you can disable that setting skipLogs thuthy.
REST
Endpoints
If you want an specific configuration for a endpoint, you need to create a file whose name is the http method (or all), and the file path is the endpoint/ + the endpoint path
For instance: This file .autharchy/foo/endpoints/some/endpoint/GET.js will override .autharchy/foo/config.js when the request is a GET to /some/endpoint
You can use parameters between brackets.
For instance this file .autharchy/foo/endpoints/some/endpoint/[id]/all.js will override .autharchy/foo/config.js when a POST is sent to /some/endpoint/bar or when a GET is sent to /some/endpoint/buz
context
{
"request": {
"path": "",
"params": {},
"headers": {},
"method": "",
"body": {},
"query": {}
},
"conf": {
// ...foo/config.js,
// ...foo/endoints/path/METHOD.js
},
"response": {
"status": 200,
"headers": {},
"body": {}
}
}graphql
Queries
If you want to override .autharchy/foo/config.js in some queries you can create any file at any path inside .autharchy/foo/queries/
For instance
.autharchy/foo/queries/bar.js
module.exports = {
match: (context) => true // false
}It will loop over the files under queries/ until some match returns truthy
First one that matches, will override the configuration
context
{
"request": {
"headers": {},
"method": "",
"body": {},
"query": {}
},
"conf": {
// ...foo/config.js,
// ...foo/queries/baz.js
},
"response": {
// the graphql response
}
}