diff --git a/.travis.yml b/.travis.yml index ecd1cb6..95f517d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,6 +11,8 @@ branches: install: - yarn + +script: - yarn test cache: diff --git a/README.md b/README.md index 4aeebd1..9f5ea28 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ yarn add hera-js import { hera } from 'hera-js'; const { data } = await hera({ - option: { + options: { url: 'https://example.com' }, query: ` @@ -43,7 +43,7 @@ const { data } = await hera({ ```ts const { data } = await hera({ - option: { + options: { url: 'https://example.com' }, query: ` @@ -72,7 +72,7 @@ const { data } = await hera({ ```ts const { data, errors } = await hera({ - option: { + options: { url: 'https://example.com' }, query: ` @@ -90,13 +90,45 @@ const { data, errors } = await hera({ }); ``` +**🌏 global options** + +> You can specify config defaults that will be applied to every request. + +```ts +import { hera, globalOptions } from 'hera-js'; + +globalOptions.url = 'https://example.com'; + +const { data } = await hera({ + query: ` + mutation { + createUser(info: $info) { + id + name + age + address + job + } + } + `, + variables: { + info: { + name: 'Cuong Tran', + age: 22, + address: 'Sai Gon / Vietnam', + job: 'software engineer' + } + } +}); +``` + ## 🚀 API ```ts hera({ - option: Option; query: string; variables?: any; + options?: Options; }) : Promise<{ data: any; errors: any[] }> ``` diff --git a/__tests__/globalOptions.test.ts b/__tests__/globalOptions.test.ts new file mode 100644 index 0000000..5127e8d --- /dev/null +++ b/__tests__/globalOptions.test.ts @@ -0,0 +1,23 @@ +import { hera, globalOptions } from '../src'; + +globalOptions.url = 'https://graphqlzero.almansi.me/api'; + +test('hera test', async() => { + const { data } = await hera({ + query: ` + query { + post(id: $id) { + id + title + body + } + } + `, + variables: { + id: 1 + } + }); + 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/__tests__/index.test.ts b/__tests__/index.test.ts index 7196850..1b9f5d9 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -2,7 +2,7 @@ import { hera } from '../src'; test('hera test', async() => { const { data } = await hera({ - option: { + options: { url: 'https://graphqlzero.almansi.me/api' }, query: ` diff --git a/package.json b/package.json index 1fc53f7..13b7ea9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@103cuong/hera-js", - "version": "0.3.1", + "version": "0.4.0", "description": "👩🏼‍💻 Simple and lightweight GraphQL client.", "main": "./lib/index.js", "types": "./lib/index.d.ts", diff --git a/src/index.ts b/src/index.ts index b71b6ae..cbddaf4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import axios, { AxiosResponse, AxiosRequestConfig } from 'axios'; +import axios from 'axios'; function formatVariables(variables: any): any { let formattedVariables = JSON.stringify(variables); @@ -14,29 +14,43 @@ function formatQuery(query: string, variables: any): string { return formattedQuery; } -interface Option { - url: string; - headers?: AxiosRequestConfig['headers']; +interface Options { + url?: string; + headers?: any; } -const hera = ({ + +const globalOptions: Options = {}; + +const hera = async ({ query, variables, - option, + options = globalOptions, }: { query: string; variables?: any; - option: Option; -}): Promise<{ data: any; errors: any[] }> => axios.post( - option.url, - { - query: variables ? formatQuery(query, variables): query, - }, - { - headers: option.headers, - }, -) - .then((res: AxiosResponse) => res.data) - .catch(error => ({ data: null, errors: [error] })); + options?: Options; +}): Promise<{ data: any; errors: any[] }> => { + if (!options.url) { + return { + data: null, + errors: [new Error('options.url must be provided')], + }; + } + try { + const res = await axios.post( + options.url, + { + query: variables ? formatQuery(query, variables) : query, + }, + { + headers: options.headers, + }, + ); + return res.data; + } catch (error) { + return { data: null, errors: [error] }; + } +}; -export { hera }; +export { hera, globalOptions };