New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

easy-api

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

easy-api - npm Package Compare versions

Comparing version
0.0.4
to
0.0.5
+18
examples/google-first-answer.js
var easyApi = require('../src/easyApi')
var request = require('request-promise')
module.exports = function(port) {
var app = easyApi();
app.get('/google-first-answer', function(searchQuery) {
return request.get('http://google.com/search?q='+searchQuery).then(function(response ) {
var matches = response.match(/<h3 class="r"><a href="([^"]+)"/);
var firstUrl = matches[1]
return firstUrl.replace('/url?q=', '').replace(/&a.*/, '');
});
})
return app.start(port)
}
var easyApi = require('../src/easyApi')
var app = easyApi()
app.get(
'/some/object/:paramA/:paramB',
function(paramA, paramB) {
return Promise.resolve([paramA, paramB]);
}
)
app.post(
'/some/object',
function(label, content, catName) {
return Promise.resolve([label, content, catName]);
}
)
app.start(4537).then(function() {
console.log('Server started :)')
})
+12
-11

@@ -8,14 +8,15 @@ var easyApi = require('../src/easyApi')

return easyApi()
.get('/messages', function() {
return messages;
var app = easyApi();
app.get('/messages', function() {
return messages;
})
app.post('/messages', function(message) {
messages.push({
date: new Date,
content: message
})
.post('/messages', function(message) {
console.log("post message ! ", message)
messages.push({
date : new Date,
content : message
})
})
.start(port)
})
return app.start(port)
}
{
"name": "easy-api",
"version": "0.0.4",
"version": "0.0.5",
"description": "Super easy promise-based api declaration",

@@ -5,0 +5,0 @@ "main": "index.js",

+72
-32

@@ -7,2 +7,3 @@ # easy-api

## Install
```

@@ -14,47 +15,86 @@ npm install --save easy-api

Declaring route and using handlers is super easy
#### Server-side
```
var easyApi = require('easy-api')
easyApi()
.get(
'/some/object/:someParameter/:someOtherParameter',
function(someParameter, someOtherParameter) {
return doAnythingNotPromisified(someParameter, someOtherParameter);
// or...
return doAnythingPromisified(someParameter, someOtherParameter);
}
)
.post(
'/some/object',
function(label, content, catName) {
return createObjectHoweverYouWant(label, content, catName);
}
)
.start(4567)
```
No need to deal with request and response objects, just declare named parameters in the route and you will get them as parameter in the handler.
### Get
```
easyApi().get(
'/object/:foo/sub-object/:bar',
function(bar, foo) {
// ...
var app = easyApi()
app.get(
'/some/object/:paramA/:paramB',
function(paramA, paramB) {
return doAnythingNotPromisified(paramA, paramB);
// or...
return doAnythingPromisified(paramA, paramB);
}
)
app.post(
'/some/object',
function(label, content, catName) {
return createObjectHoweverYouWant(label, content, catName);
}
)
app.start(4567).then(function(host, port) {
console.log('Server started on host :', host + ':' + port)
})
```
Handler function can return either a promise or a value.
### Post
Currently, the value resolved by the promise (or returned by the function) is returned using JSON.stringify.
#### Client-side
##### Using [request](https://www.npmjs.com/package/request) (or [request-promise](https://www.npmjs.com/package/request-promise))
```
easyApi().post(
'/object/:foo/sub-object',
function(barName, foo) {
// ...
request.get({
url : '/some/object/valueA/valueB',
json : true
})
request.post({
uri: '/some/object',
json : true,
body : {
label : 'cat',
content : 'meow',
catName : 'Isis'
}
)
})
```
To get the correct value for barName parameter, it must be included in the payload in the call.
##### Using [XMLHttpRequest](https://developer.mozilla.org/en/docs/Web/API/XMLHttpRequest)
```
request('GET', '/some/object/valueA/valueB')
request('POST', '/some/object', {
label : 'cat',
content : 'meow',
catName : 'Isis'
})
function request(type, url, content) {
return new Promise(function(resolve, reject) {
var req = new XMLHttpRequest()
req.open(type.toUpperCase(), url, true)
var params = JSON.stringify(content)
req.setRequestHeader("Content-type", "application/json")
req.setRequestHeader("Content-length", params.length)
req.setRequestHeader("Connection", "close")
req.onreadystatechange = function(aEvt) {
if (req.readyState == 4) {
if (req.status == 200)
resolve(JSON.parse(req.responseText))
else
reject()
}
}
req.send(params)
})
}
```
## Tests
```
npm test
```
![build status](https://travis-ci.org/JohnBerlin/easy-api.svg "Build Status")

@@ -20,3 +20,2 @@ var express = require('express')

return new Promise(function(resolve) {
console.log(req.body, req.params, req.data)
var expectedParameters = parametersUtility.parseFunctionParameters(handler),

@@ -38,2 +37,3 @@ parameters = parametersUtility.buildParameters(

wrapHandler(handler, req).then(function(resolvedValue) {
res.set('Content-Type', 'application/json')
res.end(JSON.stringify(resolvedValue))

@@ -58,3 +58,3 @@ })

return new Promise(function(resolve) {
app.listen(port, resolve)
var server = app.listen(port, resolve)
})

@@ -61,0 +61,0 @@ }

@@ -5,3 +5,28 @@ var expect = require('chai').expect

describe.skip('examples', function() {
describe('examples', function() {
describe('readme-example', function() {
it('should work in order to help users :)', function() {
var readmeExample = require('../examples/readme-example');
return Promise.all([
request.get({
url : 'http://localhost:4537/some/object/valueA/valueB',
json : true
}),
request.post({
uri: 'http://localhost:4537/some/object',
json : true,
body : {
label : 'cat',
content : 'meow',
catName : 'Isis'
}
})
]).then(function(values) {
expect(values[0]).to.deep.equal(['valueA', 'valueB'])
expect(values[1]).to.deep.equal(['cat', 'meow', 'Isis'])
})
})
})
describe('message-server', function() {

@@ -19,8 +44,6 @@ it('should have a working API', function() {

return request.post({
url: baseUrl + '/messages',
headers: {
'Content-Type': 'application/json'
},
form: {
message: 'Oy !'
uri: baseUrl + '/messages',
json : true,
body : {
message : 'Oy !'
}

@@ -37,2 +60,23 @@ });

})
describe('google-first-answer', function() {
it('should have a working API', function() {
var messageServer = require('../examples/google-first-answer')
var port = 5332
var baseUrl = 'http://localhost:' + port
return messageServer(port).then(function() {
return request.get({
uri: baseUrl + '/google-first-answer',
json : true,
body : {
searchQuery : 'npm easy-api john-berlin'
}
}).then(function(url) {
expect(url).not.to.be.undefined
expect(url.indexOf('http')).to.equal(0)
})
})
})
})
})