Skip to content

Commit

Permalink
feat: error handling (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cuong Tran authored Aug 9, 2020
1 parent 0d5d50b commit 176a8d0
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 153 deletions.
42 changes: 0 additions & 42 deletions .idea/codeStyles/Project.xml

This file was deleted.

5 changes: 0 additions & 5 deletions .idea/codeStyles/codeStyleConfig.xml

This file was deleted.

12 changes: 0 additions & 12 deletions .idea/hera.iml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/inspectionProfiles/Project_Default.xml

This file was deleted.

8 changes: 0 additions & 8 deletions .idea/modules.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

44 changes: 0 additions & 44 deletions .idea/workspace.xml

This file was deleted.

70 changes: 49 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
[![Hits-of-Code](https://hitsofcode.com/github/103cuong/hera)](https://hitsofcode.com/view/github/103cuong/hera)
[![GitHub](https://img.shields.io/github/license/103cuong/hera.svg)](https://github.com/103cuong/hera/blob/master/LICENSE)

## Installation
## 🧰 installation

```sh
yarn add hera-js
```

## Usage
## 🌳 usage

```ts
import { hera } from 'hera-js';

hera({
const { data } = await hera({
option: {
url: 'https://example.com'
},
Expand All @@ -39,12 +39,10 @@ hera({
});
```

## 👻 Special 🚧

hera allows passing parameters as objects.
**👻 passing parameters as objects**

```ts
hera({
const { data } = await hera({
option: {
url: 'https://example.com'
},
Expand All @@ -61,18 +59,48 @@ hera({
`,
variables: {
info: {
name: 'Cuong Duy Nguyen',
name: 'Cuong Tran',
age: 22,
address: 'Ho Chi Minh',
job: 'Software Engineer'
address: 'Sai Gon / Vietnam',
job: 'software engineer'
}
}
});
```

## API
**🐛 error handling**

```ts
const { data, errors } = await hera({
option: {
url: 'https://example.com'
},
query: `
query {
user(id: $id) {
id
name
age
}
}
`,
variables: {
id: 1
}
});
```

## 🚀 API

```ts
hera({
option: Option;
query: string;
variables?: any;
}) : Promise<{ data: any; errors: any[] }>
```

### options
### 📝 options

```ts
{
Expand All @@ -86,11 +114,11 @@ hera({
}
```

### query
### 📒 query

> `query` is query or mutation in Graphql
**Graphql's query**
**graphql's query**

```ts
query: `
Expand All @@ -104,7 +132,7 @@ query: `
`
```

**Graphql's mutation**
**graphql's mutation**

```ts
query: `
Expand All @@ -120,7 +148,7 @@ query: `
`
```

### variables
### 💉 variables

> variables is used to pass values to query's variables
Expand All @@ -138,10 +166,10 @@ query: `
`,
variables: {
info: {
name: 'Cuong Duy Nguyen',
age: 21,
address: 'Ho Chi Minh',
job: 'Software Engineer'
name: 'Cuong Tran',
age: 22,
address: 'Sai Gon / Vietnam',
job: 'software engineer'
}
}
```
Expand All @@ -152,4 +180,4 @@ variables: {

## License

MIT © [103cuong](https://github.com/103cuong)
MIT © [Cuong Tran](https://github.com/103cuong)
9 changes: 4 additions & 5 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { hera } from '../src';

test('hera test', async() => {
const data = await hera({
const { data } = await hera({
option: {
url: 'https://graphqlzero.almansi.me/api'
},
Expand All @@ -18,8 +18,7 @@ test('hera test', async() => {
id: 1
}
});
const { post } = data;
expect(post.id).toEqual('1');
expect(typeof post.title).toEqual('string');
expect(typeof post.body).toEqual('string');
expect(data.post.id).toEqual('1');
expect(typeof data.post.title).toEqual('string');
expect(typeof data.post.body).toEqual('string');
});
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "@103cuong/hera-js",
"version": "0.2.1",
"version": "0.3.0",
"description": "👩🏼‍💻 Simple and lightweight GraphQL client.",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"repository": "https://github.com/103cuong/hera.git",
"author": "Cuong Duy Nguyen",
"author": "Cuong Tran",
"keywords": [
"hera",
"hera-js",
Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const hera = ({
query: string;
variables?: any;
option: Option;
}): Promise<any> => axios.post(
}): Promise<{ data: any; errors: any[] }> => axios.post(
option.url,
{
query: formatQuery(query, variables),
Expand All @@ -36,6 +36,7 @@ const hera = ({
headers: option.headers,
},
)
.then((res: AxiosResponse) => res.data.data);
.then((res: AxiosResponse) => res.data)
.catch(error => ({ data: null, errors: [error] }));

export { hera };

0 comments on commit 176a8d0

Please sign in to comment.