# absolvent/absolvent-api-javascript-client

## usage (version 3.0.0)

### add to package.json

```json
"dependencies": {
  "absolvent-api-javascript-client": "ssh://git@github.com/absolvent/absolvent-api-javascript-client#3.0.0"
}
```

### in code

- import api in some root component and initialize apiHandle object

```js
import api from 'absolvent-api-javascript-client/api';

const initialProps = {
  apiBaseUri: 'http://localhost:8000/', // this should come from backend
  token: 'abcdef', // this should come from backend
};

const apiHandle = api.createFromFetch(initialProps, window.fetch);
```
- GET example

```js
apiHandle.request('some/path', { foo: 'someQueryParam' }, 'GET', null)
  .then(response => {
    this.setState({
      data: response.data
    });
  });
}
```

- POST example (for example to send state)

```js
apiHandle.request('/other/path', this.state, 'POST', null)
.then(response => {
  if (response.success) {
    this.setState({
      isSuccess: true
    });
  }
});
 ```

- use requestNoCache to avoid cache problems with IE11

```js
apiHandle.requestNoCache('/other/path', this.state, 'POST', null)
.then(response => {
  if (response.success) {
    this.setState({
      isSuccess: true
    });
  }
});
 ```
