interfacer
Advanced tools
Comparing version 0.0.8 to 0.0.9
@@ -17,3 +17,4 @@ 'use strict'; | ||
request: callConfig.request || localConfig.request || globalConfig.request || {}, | ||
flatMethod: callConfig.flatMethod || localConfig.flatMethod || globalConfig.flatMethod | ||
flatMethod: callConfig.flatMethod || localConfig.flatMethod || globalConfig.flatMethod, | ||
makeBody: callConfig.makeBody || localConfig.makeBody || globalConfig.makeBody | ||
}; | ||
@@ -20,0 +21,0 @@ }; |
@@ -27,3 +27,3 @@ 'use strict'; | ||
method: request.method, | ||
body: (0, _stringify2.default)(request.body), | ||
body: (0, _stringify2.default)(request.makeBody ? request.makeBody(request.body) : request.body), | ||
headers: (0, _extends3.default)({ | ||
@@ -30,0 +30,0 @@ 'Content-Type': 'application/json' |
{ | ||
"name": "interfacer", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "Module for working with REST API", | ||
@@ -73,13 +73,11 @@ "main": "lib/index.js", | ||
"sinon-chai": "^2.8.0", | ||
"tmp": "0.0.28" | ||
"tmp": "0.0.28", | ||
"flow-bin": "^0.43.0", | ||
"nock": "^9.0.11" | ||
}, | ||
"dependencies": { | ||
"bluebird": "^3.5.0", | ||
"flow-bin": "^0.43.0", | ||
"isomorphic-fetch": "^2.2.1", | ||
"mobx": "^3.1.8", | ||
"mobx-react": "^4.1.5", | ||
"nock": "^9.0.11", | ||
"request": "^2.81.0" | ||
} | ||
} |
# Interfacer.js | ||
Interfacer.js is a convenient module for work with RESTful API from Client. | ||
Interfacer.js is a convenient module for working with RESTful API from Client. | ||
@@ -8,4 +8,4 @@ Key Features: | ||
* Highly customizable | ||
* Memoization | ||
* Simple IO error handling, that fits your system | ||
* Memoization by default | ||
* Simple error handling, that fits your system | ||
* Customizable query construction | ||
@@ -24,5 +24,5 @@ | ||
The most advanced feature Interfacer.js provides is 3 level of configuration (Application, Interface, Call). In configuration you can define things like `defaultError`, `domain`, `protocol`, and custom `queryparser`. Each level of configuration overrides previous (more global ones), so you can change everything, mid-action, on the fly if you need to. | ||
The most advanced feature, Interfacer.js provides, are 3 levels of configuration (Application level, Single Interface/Collection level, Request level). In every configuration level you can define things like `defaultError`, `baseUrl`, custom `querybuilder` and more. Each level of configuration overrides previous (more global ones), so you can change everything, mid-action, on the fly if you need to. | ||
Don't panic when you see all them references to Redux like `dispatch`. Their's purpose is purely illustrative. you can provide any kind of function, that handles your data. | ||
Don't panic as you'll see all them references to Redux like `dispatch`. Their's purpose is purely illustrative. you can provide any kind of function, that handles your data. | ||
@@ -35,6 +35,3 @@ ### Application Level | ||
const globalConfig = { | ||
protocol: 'https', | ||
domain: 'localhost', | ||
port: 8000, | ||
baseUrl: 'http://localhost:8080/api', | ||
defaultError: new Error('Something broke'), | ||
@@ -46,6 +43,6 @@ errorHandler: ({error, message}) => dispatch({ type: 'API_ERROR', payload: message }) | ||
``` | ||
All of above settings will apply to every interface you create by this `createInterface` function, unless overwritten by later configurations in more "local" level. | ||
All of above settings will apply to every interface instance you create with this `createInterface` function, unless overwritten by later configurations in more "local" level. | ||
### Interface level | ||
Also can be perceived as "collection level". In for example Redux I'd recommend to have one interface for each collection reducer. | ||
Also can be perceived as "collection level". In for example Redux I'd recommend to have one interface for each collection reducer. From server perspective, there is one interface per resource. | ||
@@ -55,5 +52,6 @@ ```js | ||
defaultError: new Error('Articles API error'), | ||
constructFields: FieldsConstructor, | ||
querybuilder: myCustomQueryBuilder, | ||
headers: { 'Content-Type': 'text/html' } | ||
request: { mode: 'cors' }, | ||
flatMethod: parseXMLFunction, | ||
errorHandler: err => dispatch({ type: 'API_ERROR', payload: err }) | ||
@@ -64,9 +62,9 @@ } | ||
``` | ||
On Interface level you always specify `subdomain`. That is route that will be appended to `domain` in all requests you do, with this interface. | ||
On Interface level you always specify `resource`. That is route that will be appended to `baseUrl` in all requests you do, with this interface. `resource` string is always passed to `createInterface` function as a first paramtere. | ||
Obviously you can (and will) have many different Interfaces. | ||
### Call level | ||
This final and most local level references to a certain calls you make with your interface. `.send()` triggers the fetch and returns a promise. | ||
### Request level | ||
This final and most local level references to a certain requests you make with your interface. Each call returns a promise with your flattened (see `flatMethod`) response. | ||
```js | ||
const reqOptions = { | ||
const requestOptions = { | ||
query: { fields: ['title', 'author']}, | ||
@@ -77,6 +75,6 @@ defaultError: new Error('Articles Collection failed to fetch') | ||
articleInterface | ||
.getCollection(reqOptions) | ||
.then(data => | ||
dispatch({ type: 'RECIEVE_ARTICLES', payload: data.payload }) | ||
); | ||
.getCollection(requestOptions) | ||
.then(data => | ||
dispatch({ type: 'RECIEVE_ARTICLES', payload: data }) | ||
); | ||
``` | ||
@@ -89,27 +87,22 @@ | ||
### **`get`** | ||
args: | ||
```js | ||
id :string | number, options :Object = {} | ||
(id :string | number, requestConfig? :Object) => Promise<Response> | ||
``` | ||
### **`getCollection`** | ||
args: | ||
```js | ||
options :Object = {} | ||
(requestConfig? :Object) => Promise<Response> | ||
``` | ||
### **`create`** | ||
args: | ||
```js | ||
body :Object, options? :Object | ||
(body :Object, requestConfig? :Object) => Promise<Response> | ||
``` | ||
### **`remove`** | ||
args: | ||
```js | ||
id :ID, options? :Object | ||
(id :ID, requestConfig? :Object) => Promise<Response> | ||
``` | ||
### **`update`** | ||
args: | ||
```js | ||
id :string, body :Object, options? :Object | ||
(id :string, body :Object, requestConfig? :Object) => Promise<Response> | ||
``` | ||
@@ -126,21 +119,21 @@ ## API Reference | ||
| `headers` | Object containting headers your request should have | `object` | `"Content-Type": "application/json"` | | ||
|`flatMethod`| Function that will be used on raw response from the fetch| `function` | `.json()`| | ||
| `request` | This object will be added to request options. It's the same as pasting an object into second argument of `fetch` function | `{}` | | ||
| `query` | Object that gets passed to queryparser fn | `object` | `null` | | ||
| `port` | Number that gets appended to your domain | `number` or `string` or `nothing` | `null` | | ||
| `domain` | baseUrl that your API runs on | `string` | `localhost` | | ||
| `protocol` | Gets prepended to your `domain` | `http` or `https` | `http` | | ||
| `queryparser` | Function that transform `query` object into a query string | `function`| `queryparser` | ||
| `query` | Object that gets passed to queryparser fn | `object` | `{}` | | ||
| `baseUrl` | baseUrl that your API runs on | `string` | `"/"` | | ||
| `querybuilder` | Function that transform `query` object into a query string | `function`| `querybuilder` | ||
| `makeBody` | Function that'll be run on body, right before request is made. | `function` | `body => body` | ||
## URL Queries | ||
You can pass your own `queryparser` into any config, but you can also use default one. Annotation of `queryparser` looks like this | ||
You can pass your own `querybuilder` into any config, but you can also use default one. Annotation of `querybuilder` looks like this | ||
```js | ||
queryparser(query :Object) => string | ||
querybuilder(query :Object) => string | ||
``` | ||
Default `queryparser` builds queries like this... | ||
Default `querybuilder` builds queries like this... | ||
```js | ||
queryparser({ | ||
filters: 'over18', | ||
fields: ['title', 'years old'] | ||
querybuilder({ | ||
filters: 'over18', | ||
fields: ['title', 'years old'] | ||
}); | ||
@@ -150,5 +143,1 @@ // -> 'filters=over18&fields=title&fields=years%20old' | ||
``` | ||
## Thank you | ||
Thank you for using this package. If you have any issues, questions or suggestions, create an issue please, I'll be happy to answer it. |
@@ -24,4 +24,5 @@ // @flow | ||
flatMethod: callConfig.flatMethod || localConfig.flatMethod || globalConfig.flatMethod, | ||
makeBody: callConfig.makeBody || localConfig.makeBody || globalConfig.makeBody, | ||
}); | ||
export default composeConfigs; |
@@ -11,3 +11,4 @@ // @flow | ||
method: request.method, | ||
body: JSON.stringify(request.body), | ||
body: JSON.stringify(request.makeBody ? | ||
request.makeBody(request.body) : request.body), | ||
headers: { | ||
@@ -14,0 +15,0 @@ 'Content-Type': 'application/json', |
@@ -42,2 +42,3 @@ // @flow | ||
flatMethod?: Function, | ||
makeBody?: Function | ||
}; | ||
@@ -64,2 +65,3 @@ | ||
body?: Object, | ||
makeBody: Function, | ||
flatMethod: Function, | ||
@@ -66,0 +68,0 @@ }; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
186059
3
394
49
18
134
- Removedflow-bin@^0.43.0
- Removedmobx@^3.1.8
- Removedmobx-react@^4.1.5
- Removednock@^9.0.11
- Removedassertion-error@1.1.0(transitive)
- Removedcall-bind@1.0.7(transitive)
- Removedchai@4.5.0(transitive)
- Removedcheck-error@1.0.3(transitive)
- Removeddebug@3.2.7(transitive)
- Removeddeep-eql@4.1.4(transitive)
- Removeddeep-equal@1.1.2(transitive)
- Removeddefine-data-property@1.1.4(transitive)
- Removeddefine-properties@1.2.1(transitive)
- Removedes-define-property@1.0.0(transitive)
- Removedes-errors@1.3.0(transitive)
- Removedflow-bin@0.43.1(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedfunctions-have-names@1.2.3(transitive)
- Removedget-func-name@2.0.2(transitive)
- Removedget-intrinsic@1.2.4(transitive)
- Removedgopd@1.0.1(transitive)
- Removedhas-property-descriptors@1.0.2(transitive)
- Removedhas-proto@1.0.3(transitive)
- Removedhas-symbols@1.0.3(transitive)
- Removedhas-tostringtag@1.0.2(transitive)
- Removedhasown@2.0.2(transitive)
- Removedhoist-non-react-statics@2.5.5(transitive)
- Removedis-arguments@1.1.1(transitive)
- Removedis-date-object@1.0.5(transitive)
- Removedis-regex@1.1.4(transitive)
- Removedjs-tokens@4.0.0(transitive)
- Removedlodash@4.17.21(transitive)
- Removedloose-envify@1.4.0(transitive)
- Removedloupe@2.3.7(transitive)
- Removedminimist@1.2.8(transitive)
- Removedmkdirp@0.5.6(transitive)
- Removedmobx@3.6.2(transitive)
- Removedmobx-react@4.4.3(transitive)
- Removedms@2.1.3(transitive)
- Removednock@9.6.1(transitive)
- Removedobject-assign@4.1.1(transitive)
- Removedobject-inspect@1.13.3(transitive)
- Removedobject-is@1.1.6(transitive)
- Removedobject-keys@1.1.1(transitive)
- Removedpathval@1.1.1(transitive)
- Removedprop-types@15.8.1(transitive)
- Removedpropagate@1.0.0(transitive)
- Removedqs@6.13.1(transitive)
- Removedreact@16.14.0(transitive)
- Removedreact-is@16.13.1(transitive)
- Removedregexp.prototype.flags@1.5.3(transitive)
- Removedsemver@5.7.2(transitive)
- Removedset-function-length@1.2.2(transitive)
- Removedset-function-name@2.0.2(transitive)
- Removedside-channel@1.0.6(transitive)
- Removedtype-detect@4.1.0(transitive)