Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/archilogic-com/instant-api
Like instant soup but API. JSON-RPC2 flavor with Websockets and HTTP.
https://github.com/archilogic-com/instant-api
Last synced: 2 days ago
JSON representation
Like instant soup but API. JSON-RPC2 flavor with Websockets and HTTP.
- Host: GitHub
- URL: https://github.com/archilogic-com/instant-api
- Owner: archilogic-com
- License: mit
- Created: 2017-06-03T11:31:28.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-12-23T15:21:46.000Z (almost 7 years ago)
- Last Synced: 2024-10-12T17:38:22.458Z (29 days ago)
- Language: JavaScript
- Homepage:
- Size: 19.5 KB
- Stars: 9
- Watchers: 6
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Instant API
Like instant soup but API. JSON-RPC2 flavor with Websockets and HTTP.**💾 Install**
```
npm i -s instant-api
```**📡 Expose task 'makeSoup' at port 3000**
```javascript
var tasks = {
'makeSoup': require('./tasks/make-soup')
}
require('instant-api')(tasks ,{ port: process.env.PORT || 3000 })
```**🤖 tasks/make-soup.js**
```javascript
module.exports = function (rpc) {// use parameters
console.log(rpc.params)// return result
rpc.sendResult('Done. Enjoy!')// return param error
//rpc.sendParamsError('Missing parameter ...')// return custom error
//rpc.sendError('Splash')// use in promise chains
// rawQuery(query).then(rpc.sendResult).catch(rpc.sendError)}
```**📣 Call task...**
```javascript
var message = {
method: 'makeSoup',
params: { size: 'medium' },
jsonrpc: '2.0',
id: Math.round(Math.random()*1e20)
}// ... from a browser using HTTP
fetch('http://localhost:3000', {
method: 'POST', body: JSON.stringify( message )
}).then(function(response){
return response.json()
}).then(function(body){
console.log(body.result)
}).catch(console.error)// ... from a browser using Websockets
var ws = new WebSocket('ws://localhost:3000')
ws.onopen = function () {
ws.send( JSON.stringify(message) )
}
ws.onmessage = function (event) {
console.log(JSON.parse(event.data))
}// ... from another server
// npm install --save request
require('request').post({
url: 'http://localhost:3000',
json: message
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
console.log(body.result)
} else {
console.error(error || body)
}
})```
[**🚨 Cross origin settings**](example/index.js)
By default, [CORS](https://en.wikipedia.org/wiki/Cross-origin_resource_sharing) is enabled but does not permit transmitting credentials.
You can specify allowed CORS domains which will also be able to send credentials:```javascript
var tasks = {
'makeSoup': require('./tasks/make-soup')
}
require('instant-api')(tasks ,{
port: process.env.PORT || 3000,
corsAllowedDomains: [ 'example.org', 'test.example.org' ]
})
```[**🕹 Run example**](example/index.js)
```
npm run example
```