
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
@janiscommerce/api
Advanced tools
A package for managing API from any origin.
npm install @janiscommerce/api
new APIDispatcher( object ) Construct an API
async .dispatch()
This method dispatch the api instance.
Returns an object with code
and the body
.
const { APIDispatcher } = require('@janiscommerce/api');
const dispatcher = new APIDispatcher({
endpoint: 'api/pets',
method: 'get', // this is the default verb
data: { status: 'active' },
headers: { 'Content-Type': 'application/json' },
cookies: { 'my-cookie': 123 }
});
const response = await dispatcher.dispatch();
console.log(response);
/**
expected output:
{
code: 200,
body: [
{
id: 1,
type: 'dog',
breed: 'pug',
name: 'Batman'
}, {
id: 2,
type: 'dog',
breed: 'chihuahua',
name: 'Chico'
}
]
}
*/
You should extend your apis from this module.
pathParameters (getter). Returns the path parameters of the request.
headers (getter). Returns the the headers of the request.
cookies (getter). Returns the the cookies of the request.
setCode(code). Set a response httpCode. code
must be a integer.
setHeader(headerName, headerValue). Set an individual response header. headerName
must be a string.
setHeaders(headers). Set response headers. headers
must be an object with "key-value" headers.
setCookie(cookieName, cookieValue). Set an individual response cookie. cookieName
must be a string.
setCookies(cookies). Set response cookies. cookies
must be an object with "key-value" cookies.
setBody(body). Set the response body.
const { API } = require('@janiscommerce/api');
class MyApi extends API {
/**
* Optional method for struct validation (qs or requestBody)
*/
get struct() {
return {
id: 'number',
name: 'string'
};
}
/**
* Optional method for extra validation
*/
async validate() {
if(this.data.id > 10)
throw new Error('Weird validation fail'); // this will response a 400 error
if(!existsInMyDB(this.data.id)) {
this.setCode(404); // set a custom http resposne code
throw new Error('resource not found'); // this will response a 404 error
}
}
/**
* Required method for api process
*/
async process() {
if(!saveInMyDB(this.data))
throw new Error('internal save error'); // this will response a 500 error
if(!saveOtherThingInMyDB(this.data)) {
this.setCode(504); // set a custom http resposne code
throw new Error('internal save error');
}
this
.setHeader('my-header-1', 'foo')
.setHeaders({ 'my-header-2': 'foo', 'my-header-3': 'foo' })
.setCookie('my-cookie-1', 'bar')
.setCookies({ 'my-cookie-2': 'bar', 'my-cookie-3': 'bar' })
.setBody({
'response-body': 123
});
}
}
module.exports = MyApi;
[1.4.0] - 2019-06-18
husky
added for pre-commitDispatcher
renamedDispatcher
prepare() better validationAPI
response default values for code
and body
lib
folder for package contenteslint
configsFAQs
A package for managing API from any origin
The npm package @janiscommerce/api receives a total of 420 weekly downloads. As such, @janiscommerce/api popularity was classified as not popular.
We found that @janiscommerce/api demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.