Skip to content

Commit

Permalink
feat: support global options (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cuong Tran committed Aug 9, 2020
1 parent 83d6a2e commit 1b53a96
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 25 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ branches:

install:
- yarn

script:
- yarn test

cache:
Expand Down
40 changes: 36 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ yarn add hera-js
import { hera } from 'hera-js';

const { data } = await hera({
option: {
options: {
url: 'https://example.com'
},
query: `
Expand All @@ -43,7 +43,7 @@ const { data } = await hera({

```ts
const { data } = await hera({
option: {
options: {
url: 'https://example.com'
},
query: `
Expand Down Expand Up @@ -72,7 +72,7 @@ const { data } = await hera({

```ts
const { data, errors } = await hera({
option: {
options: {
url: 'https://example.com'
},
query: `
Expand All @@ -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[] }>
```

Expand Down
23 changes: 23 additions & 0 deletions __tests__/globalOptions.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
2 changes: 1 addition & 1 deletion __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: `
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
52 changes: 33 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios, { AxiosResponse, AxiosRequestConfig } from 'axios';
import axios from 'axios';

function formatVariables(variables: any): any {
let formattedVariables = JSON.stringify(variables);
Expand All @@ -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 };

0 comments on commit 1b53a96

Please sign in to comment.