Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/fakoua/soxa
Browse files Browse the repository at this point in the history
  • Loading branch information
fakoua committed Jun 17, 2020
2 parents 4cd468f + 7f7023d commit 1a10d8d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-16.04
steps:
- uses: actions/checkout@master
- uses: denolib/setup-deno@v1.1.0
- uses: denolib/setup-deno@master
with:
deno-version: 0.36.0
- run: deno -A test.ts
deno-version: 1.x
- run: deno test -A
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: python

install:
- curl -L https://deno.land/x/install/install.sh | sh -s "v0.25.0"
- curl -L https://deno.land/x/install/install.sh | sh
- export PATH="$HOME/.deno/bin:$PATH"

script:
- deno run -A test.ts
- deno test -A
38 changes: 21 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# soxa

[![Build Status](https://api.travis-ci.com/fakoua/soxa.svg?branch=master)](https://travis-ci.com/fakoua/soxa)
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Ffakoua%2Fsoxa.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Ffakoua%2Fsoxa?ref=badge_shield)
[![Build Status](https://github.com/fakoua/soxa/workflows/CI/badge.svg?branch=master&event=push)](https://github.com/fakoua/soxa/actions)


Expand All @@ -19,15 +20,15 @@ Promise based HTTP client for deno

Using deno:

```js
```ts
import { soxa } from 'https://deno.land/x/soxa/mod.ts'
```

## Example

Performing a `GET` request (Promise)

```js
```ts
import { soxa } from 'https://deno.land/x/soxa/mod.ts'

// soxa.get(url, config)
Expand Down Expand Up @@ -57,14 +58,14 @@ soxa.get('https://jsonplaceholder.typicode.com/todos/1')

Performing a `GET` request (Await/Async)

```js
```ts
let result = await soxa.get('https://jsonplaceholder.typicode.com/todos/1');
console.log(result.data)
```

Performing a `POST` request

```js
```ts
let response = await soxa.post('https://jsonplaceholder.typicode.com/posts', {
"title": "Hello Soxa",
"id": 14
Expand All @@ -83,7 +84,7 @@ let response = await soxa.post('https://jsonplaceholder.typicode.com/posts', {},

## URL Examples

```js
```ts
await soxa.get('http://example.com'); // http://example.com
await soxa.get('http://example.com', { params: { q: 'hello' } }); // http://example.com?q=hello
await soxa.get('http://example.com', { params: { q: 'hello', id: 12 } }); // http://example.com?q=hello&id=12
Expand All @@ -104,7 +105,7 @@ await soxa.get('/folder', config); // http://example.com/folder?q=hello

These are the available config options for making requests. Only the `url` is required. Requests will default to `GET` if `method` is not specified.

```js
```ts
{
// `baseURL` will be prepended to `url` unless `url` is absolute.
// It can be convenient to set `baseURL` for an instance of soxa to pass relative URLs
Expand Down Expand Up @@ -246,7 +247,7 @@ soxa.post(url, {} ,config)

The response for a request contains the following information.

```js
```ts
{
// `data` is the response that was provided by the server
data: {},
Expand All @@ -273,7 +274,7 @@ The response for a request contains the following information.

When using `then`, you will receive the response as follows:

```js
```ts
soxa.get('/user/12345')
.then(function (response) {
console.log(response.data);
Expand All @@ -292,7 +293,7 @@ You can specify config defaults that will be applied to every request.

### Global soxa defaults

```js
```ts
soxa.defaults.baseURL = 'https://api.example.com';
soxa.defaults.headers.common['Authorization'] = AUTH_TOKEN;
soxa.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
Expand All @@ -302,7 +303,7 @@ soxa.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'

You can intercept requests or responses before they are handled by `then` or `catch`.

```js
```ts
// Add a request interceptor
soxa.interceptors.request.use(function (config) {
// Do something before request is sent
Expand All @@ -326,21 +327,21 @@ soxa.interceptors.response.use(function (response) {

If you need to remove an interceptor later you can.

```js
```ts
const myInterceptor = soxa.interceptors.request.use(function () {/*...*/});
soxa.interceptors.request.eject(myInterceptor);
```

You can add interceptors to a custom instance of soxa.

```js
```ts
const instance = soxa.create();
instance.interceptors.request.use(function () {/*...*/});
```

## Handling Errors

```js
```ts
soxa.get('/user/12345')
.catch(function (error) {
if (error.response) {
Expand All @@ -364,7 +365,7 @@ soxa.get('/user/12345')

Using the `validateStatus` config option, you can define HTTP code(s) that should throw an error.

```js
```ts
soxa.get('/user/12345', {
validateStatus: function (status) {
return status < 500; // Reject only if the status code is greater than or equal to 500
Expand All @@ -374,7 +375,7 @@ soxa.get('/user/12345', {

Using `toJSON` you get an object with more information about the HTTP error.

```js
```ts
soxa.get('/user/12345')
.catch(function (error) {
console.log(error.toJSON());
Expand All @@ -389,7 +390,7 @@ You can cancel a request using a *cancel token*.
You can create a cancel token using the `CancelToken.source` factory as shown below:

```js
```ts
const CancelToken = soxa.CancelToken;
const source = CancelToken.source();

Expand All @@ -415,7 +416,7 @@ source.cancel('Operation canceled by the user.');

You can also create a cancel token by passing an executor function to the `CancelToken` constructor:

```js
```ts
const CancelToken = soxa.CancelToken;
let cancel;

Expand Down Expand Up @@ -443,3 +444,6 @@ soxa is heavily inspired by the [axios](https://github.com/axios/axios) with new
## License

[MIT](LICENSE)


[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Ffakoua%2Fsoxa.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Ffakoua%2Fsoxa?ref=badge_large)

0 comments on commit 1a10d8d

Please sign in to comment.