@janiscommerce/endpoint-resolver
v1.0.0
Published
A package to resolve a JANIS endpoint
Downloads
5,845
Readme
Endpoint resolver
A package to resolve a JANIS endpoint
Installation
npm install @janiscommerce/endpoint-resolver
API
constructor(schema[, environment = 'local'])
schema
: The schema object or file path (JSON only)environment
: The environment to fetch in the schema servers. Default is local.
async resolve(namespace, method)
namespace
: The namespace to fetch (x-janis-namespace
in the API schema)method
: The method to fetch (x-janis-method
in the API schema)
Resolves an object with the httpMethod
and url
properties, or rejects with an error explaining the motive.
Usage
const EndpointResolver = require('@janiscommerce/endpoint-resolver');
const endpointResolver = new EndpointResolver('path/to/schema');
const { httpMethod, url, error } = endpointResolver.resolve('some-namespace', 'some-method');
Examples
const EndpointResolver = require('@janiscommerce/endpoint-resolver');
const endpointResolver = new EndpointResolver('schemas/public.json');
// Or
// const endpointResolver = new EndpointResolver(mySchemaObject);
try {
const { httpMethod, url } = endpointResolver.resolve('user', 'list');
console.log(httpMethod); // GET
console.log(url); // https://example.com/user
} catch(e) {
handleError(e);
}