Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

interfacer

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

interfacer - npm Package Compare versions

Comparing version 0.0.8 to 0.0.9

3

lib/configComposers.js

@@ -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 @@ };

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc