@janiscommerce/api-view v1.3.0
API View
A package for managing API View from any origin.
Installation
npm install @janiscommerce/api-viewDispatcher
new Dispatcher( object ) Construct a Dispatcher with the request information
async .dispatch() This method dispatch the api instance. Returns an object with
codeand thebody.
API
You should extend your apis from this module.
pathParameters (getter). Returns the path parameters of the request. The
entityandentityIdif present.headers (getter). Returns the the headers of the request.
cookies (getter). Returns the the cookies of the request.
setCode(code). Set a response httpCode.
codemust be a integer.setHeader(headerName, headerValue). Set an individual response header.
headerNamemust be a string.setHeaders(headers). Set response headers.
headersmust be an object with "key-value" headers.setCookie(cookieName, cookieValue). Set an individual response cookie.
cookieNamemust be a string.setCookies(cookies). Set response cookies.
cookiesmust be an object with "key-value" cookies.setBody(body). Set the response body.
Usage
- How to process a
browserequest
const { Dispatcher } = require('@janiscommerce/api-view');
const dispatcher = new Dispatcher({
entity: 'product',
action: 'browse',
method: 'data'
});
const result = await dispatcher.dispatch();
console.log(result);
/**
expected output:
{
code: 200,
body: [
{
id: 5928,
name: 'Great product',
price: 45.1,
category: 'Beverages'
}, {
id: 5929,
name: 'Another great product',
price: 32.0,
category: 'groceries'
}
]
}
*/- How to process an
editrequest
const { Dispatcher } = require('@janiscommerce/api-view');
const dispatcher = new Dispatcher({
entity: 'product',
action: 'edit',
method: 'data',
entity: 5928
});
const result = await dispatcher.dispatch();
console.log(result);
/**
expected output:
{
code: 200,
body: {
id: 5928,
name: 'Great product',
price: 45.1,
category: 'Beverages'
}
}
*/