Skip to content

Commit 53700d3

Browse files
committed
readme, accept existing interpreter object
1 parent 5655245 commit 53700d3

File tree

3 files changed

+68
-3
lines changed

3 files changed

+68
-3
lines changed

README.md

+64
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,70 @@ JSONScript also supports sequential evaluation, conditionals, data manipulation,
9797
See [JSONScript Language](https://github.com/JSONScript/jsonscript/blob/master/LANGUAGE.md) for more information.
9898

9999

100+
## API
101+
102+
##### jsonscriptProxy(Object options [, Object js]) -> Function
103+
104+
Create express route handling function to process JSONScript. The second optional parameter is the existing instance of JSONScript interpreter, if it is not passed a new one will be created.
105+
106+
Both the `script` and the `data` instance should be properties of the request body:
107+
108+
```javascript
109+
{
110+
"script": {
111+
// JSONScript, can be an array
112+
},
113+
"data": {
114+
// data instance that can be used from the script,
115+
// can be array
116+
}
117+
}
118+
```
119+
120+
121+
## Options
122+
123+
See [options schema](https://github.com/JSONScript/jsonscript-proxy/blob/master/config_schema.json).
124+
125+
Defaults:
126+
127+
```javascript
128+
{
129+
services: {}, // must be specified and have at least one property
130+
processResponse: undefined,
131+
jsonscript: { strict: true },
132+
Promise: undefined
133+
}
134+
```
135+
136+
- _services_: the required map of service definitions that are exposed to JSONScript as executors. Each property name will be used as an executor map. See [Service definitions](#service-definitions).
137+
- _processResponse_: the default response processing function, can be overridden for a particular service. The possible values:
138+
- `"body"` - return only response body if status code is < 300, throw an exception otherwise.
139+
- function - custom function to process the response object, can throw an exception or return the object to be used as the result.
140+
- _jsonscript_: options passed to JSONScript interpreter [jsonscript-js](https://github.com/JSONScript/jsonscript-js).
141+
- _Promise_: an optional Promise class, the native Promise is used by default.
142+
143+
144+
## Service definitions
145+
146+
`services` properties in options object should contain a map of services:
147+
148+
```javascript
149+
{
150+
service1: {
151+
basePath: '...',
152+
processResponce: undefined
153+
},
154+
service2: {
155+
// ...
156+
},
157+
// ...
158+
}
159+
```
160+
161+
`basePath` will be prepended for the path in the call to the service, `processResponse`, if specified, will be used to process responses from the service.
162+
163+
100164
## License
101165

102166
[MIT](https://github.com/JSONScript/jsonscript-proxy/blob/master/LICENSE)

config_schema.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"description": "options passed to JSONScript interpreter",
2121
"type": "object"
2222
},
23-
"processResponse": { "$ref": "#/definitions/processResponse" }
23+
"processResponse": { "$ref": "#/definitions/processResponse" },
24+
"Promise": { "typeof": "function" }
2425
},
2526
"definitions": {
2627
"service": {

index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ module.exports = jsonscriptProxy;
1212

1313

1414
var METHODS = ['get', 'post', 'put', 'delete'];
15-
function jsonscriptProxy(options) {
15+
function jsonscriptProxy(options, js) {
1616
if (!validateOptions(options)) {
1717
console.log('Invalid options:\n', validateOptions.errors);
1818
throw new Error('Invalid options');
1919
}
2020

21-
var js = JSONScript(options.jsonscript || { strict: true });
21+
js = js || new JSONScript(options.jsonscript || { strict: true });
2222
for (var key in options.services)
2323
js.addExecutor(key, getExecutor(options.services[key]));
2424
evaluator.js = js;

0 commit comments

Comments
 (0)