From 176a8d0ba114692f8a6bc9812d78975fc097f1ac Mon Sep 17 00:00:00 2001 From: Cuong Tran <103cuong@gmail.com> Date: Sun, 9 Aug 2020 12:28:39 +0700 Subject: [PATCH] feat: error handling (#22) --- .idea/codeStyles/Project.xml | 42 ------------ .idea/codeStyles/codeStyleConfig.xml | 5 -- .idea/hera.iml | 12 ---- .idea/inspectionProfiles/Project_Default.xml | 6 -- .idea/modules.xml | 8 --- .idea/vcs.xml | 6 -- .idea/workspace.xml | 44 ------------ README.md | 70 ++++++++++++++------ __tests__/index.test.ts | 9 ++- package.json | 4 +- src/index.ts | 5 +- 11 files changed, 58 insertions(+), 153 deletions(-) delete mode 100644 .idea/codeStyles/Project.xml delete mode 100644 .idea/codeStyles/codeStyleConfig.xml delete mode 100644 .idea/hera.iml delete mode 100644 .idea/inspectionProfiles/Project_Default.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml delete mode 100644 .idea/workspace.xml diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml deleted file mode 100644 index 51dedef..0000000 --- a/.idea/codeStyles/Project.xml +++ /dev/null @@ -1,42 +0,0 @@ - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml deleted file mode 100644 index 79ee123..0000000 --- a/.idea/codeStyles/codeStyleConfig.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/hera.iml b/.idea/hera.iml deleted file mode 100644 index 24643cc..0000000 --- a/.idea/hera.iml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml deleted file mode 100644 index df7825d..0000000 --- a/.idea/inspectionProfiles/Project_Default.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 907aa31..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml deleted file mode 100644 index 102c473..0000000 --- a/.idea/workspace.xml +++ /dev/null @@ -1,44 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - 1596790765613 - - - - - - \ No newline at end of file diff --git a/README.md b/README.md index d25bcf0..b0f885f 100644 --- a/README.md +++ b/README.md @@ -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' }, @@ -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' }, @@ -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 { @@ -86,11 +114,11 @@ hera({ } ``` -### query +### 📒 query > `query` is query or mutation in Graphql -**Graphql's query** +**graphql's query** ```ts query: ` @@ -104,7 +132,7 @@ query: ` ` ``` -**Graphql's mutation** +**graphql's mutation** ```ts query: ` @@ -120,7 +148,7 @@ query: ` ` ``` -### variables +### 💉 variables > variables is used to pass values to query's variables @@ -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' } } ``` @@ -152,4 +180,4 @@ variables: { ## License -MIT © [103cuong](https://github.com/103cuong) +MIT © [Cuong Tran](https://github.com/103cuong) diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index 2f355ed..7196850 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -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' }, @@ -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'); }); \ No newline at end of file diff --git a/package.json b/package.json index 1f2b362..526d5ab 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.ts b/src/index.ts index efc22ca..4690d1e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -27,7 +27,7 @@ const hera = ({ query: string; variables?: any; option: Option; -}): Promise => axios.post( +}): Promise<{ data: any; errors: any[] }> => axios.post( option.url, { query: formatQuery(query, variables), @@ -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 };