instant-api
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -22,5 +22,10 @@ var http = require('http') | ||
// allow any origin | ||
credentials: true, | ||
credentials: !!options.corsAllowedDomains, | ||
origin: function (origin, callback) { | ||
callback(null, origin) | ||
var originDomain = origin.replace(/^[^:]+:\/\//, '') | ||
var corsOrigin = origin | ||
if (options.corsAllowedDomains) { | ||
corsOrigin = options.corsAllowedDomains.includes(originDomain) ? origin : null | ||
} | ||
callback(corsOrigin ? null : `Invalid origin found: ${originDomain}`, corsOrigin) | ||
} | ||
@@ -27,0 +32,0 @@ }) |
{ | ||
"name": "instant-api", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Like instant soup but API. JSON-RPC2 flavor with Websockets and HTTP.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/archilogic-com/instant-api", |
@@ -20,18 +20,18 @@ # Instant API | ||
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) | ||
} | ||
@@ -57,3 +57,3 @@ ``` | ||
}).catch(console.error) | ||
// ... from a browser using Websockets | ||
@@ -83,2 +83,17 @@ var ws = new WebSocket('ws://localhost:3000') | ||
[**🚨 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) | ||
@@ -85,0 +100,0 @@ ``` |
20454
469
102