Skip to content

Commit

Permalink
🦄 Add first version with basic features (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cuong Duy Nguyen authored Dec 30, 2019
1 parent 9332acb commit 5a61684
Show file tree
Hide file tree
Showing 6 changed files with 601 additions and 1,042 deletions.
27 changes: 0 additions & 27 deletions .all-contributorsrc

This file was deleted.

53 changes: 46 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,58 @@ npm install -s giin
```ts
import { giin } from 'giin';
// const { giin } = require('giin');
giin({
option: {
url: 'https://example.com'
},
query: `
query {
user(id: $id) {
id
name
age
}
}
`,
variables: {
id: 1
}
});
```

## Contributors
## 👻 Special 🚧

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
giin allows passing parameters as objects.

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore -->
<table><tr><td align="center"><a href="http://cuongw.me"><img src="https://avatars0.githubusercontent.com/u/34389409?v=4" width="100px;" alt="Cuong Duy Nguyen"/><br /><sub><b>Cuong Duy Nguyen</b></sub></a><br /><a href="https://github.com/cuongw/giin/commits?author=cuongw" title="Code">💻</a> <a href="https://github.com/cuongw/giin/commits?author=cuongw" title="Documentation">📖</a> <a href="https://github.com/cuongw/giin/commits?author=cuongw" title="Tests">⚠️</a> <a href="#review-cuongw" title="Reviewed Pull Requests">👀</a></td></tr></table>
```ts
giin({
option: {
url: 'https://example.com'
},
query: `
query {
user(info: $info) {
id
name
sex
age
address
job
}
}
`,
variables: {
info: {
name: 'Cuong Duy Nguyen',
address: 'Ho Chi Minh'
}
}
});
```

<!-- ALL-CONTRIBUTORS-LIST:END -->
## Contributors

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
[![](https://sourcerer.io/fame/cuongw/cuongw/giin/images/0)](https://sourcerer.io/fame/cuongw/cuongw/giin/links/0)[![](https://sourcerer.io/fame/cuongw/cuongw/giin/images/1)](https://sourcerer.io/fame/cuongw/cuongw/giin/links/1)[![](https://sourcerer.io/fame/cuongw/cuongw/giin/images/2)](https://sourcerer.io/fame/cuongw/cuongw/giin/links/2)[![](https://sourcerer.io/fame/cuongw/cuongw/giin/images/3)](https://sourcerer.io/fame/cuongw/cuongw/giin/links/3)[![](https://sourcerer.io/fame/cuongw/cuongw/giin/images/4)](https://sourcerer.io/fame/cuongw/cuongw/giin/links/4)[![](https://sourcerer.io/fame/cuongw/cuongw/giin/images/5)](https://sourcerer.io/fame/cuongw/cuongw/giin/links/5)[![](https://sourcerer.io/fame/cuongw/cuongw/giin/images/6)](https://sourcerer.io/fame/cuongw/cuongw/giin/links/6)[![](https://sourcerer.io/fame/cuongw/cuongw/giin/images/7)](https://sourcerer.io/fame/cuongw/cuongw/giin/links/7)

## License

Expand Down
25 changes: 25 additions & 0 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { giin } from '../src';

test('giin Test', async() => {
const data = await giin({
option: {
url: 'https://graphqlzero.almansi.me/api'
},
query: `
query {
post(id: $id) {
id
title
body
}
}
`,
variables: {
id: 1
}
});
const { post } = data;
expect(post.id).toEqual('1');
expect(typeof post.title).toEqual('string');
expect(typeof post.body).toEqual('string');
});
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "giin",
"version": "0.5.5",
"version": "0.1.1",
"description": "🦄 Simple and lightweight GraphQL client.",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down Expand Up @@ -30,7 +30,6 @@
},
"devDependencies": {
"@types/jest": "^24.0.11",
"all-contributors-cli": "^6.3.1",
"jest": "^24.7.1",
"prettier": "^1.17.0",
"ts-jest": "^24.0.2",
Expand All @@ -39,5 +38,8 @@
"tslint": "^5.16.0",
"tslint-config-prettier": "^1.18.0",
"typescript": "^3.4.4"
},
"dependencies": {
"axios": "^0.19.0"
}
}
42 changes: 40 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,41 @@
export function giin(): string {
return 'giin';
// tslint:disable
import axios, { AxiosResponse, AxiosRequestConfig } from 'axios';

function formatVariables(variables: any): any {
let formatedVariables = JSON.stringify(variables);
formatedVariables = formatedVariables.replace(/"([^"]+)"\s*:\s*/g, '$1:');
return formatedVariables;
}
function formatQuery(query: string, variables: any): string {
let formatedQuery = query;
for (const key in variables) {
const regex = new RegExp(`\\$${key}`, 'g');
formatedQuery = formatedQuery.replace(regex, formatVariables(variables[key]));
}
return formatedQuery;
}

interface Option {
url: string;
headers?: AxiosRequestConfig['headers'];
}

export function giin({
query,
variables,
option,
}: {
query: string;
variables?: any;
option: Option;
}): Promise<any> {
return axios.post(
option.url,
{
query: formatQuery(query, variables),
},
{
headers: option.headers,
},
).then((res: AxiosResponse) => res.data.data);
}
Loading

0 comments on commit 5a61684

Please sign in to comment.