Skip to content

Commit

Permalink
Updates (Refactoring)
Browse files Browse the repository at this point in the history
  • Loading branch information
yulmwu committed Sep 2, 2022
1 parent 2fb69ea commit 90dcefe
Show file tree
Hide file tree
Showing 14 changed files with 116 additions and 157 deletions.
Binary file added .DS_Store
Binary file not shown.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
- [Gist API](./README.md#gist-api)
- [Docs](./README.md#docs)
- [Status codes](./README.md#status-codes)

# Gist API

```sh
npm i @tsukiroku/gist
```console
$ npm i @tsukiroku/gist
```

- [Gist API](./README.md#gist-api)
- [Docs](./README.md#docs)
- [Status codes](./README.md#status-codes)

---

# Docs
Expand All @@ -17,9 +17,9 @@ npm i @tsukiroku/gist
> **Note:** Account token required. See [Docs](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token)
```ts
import Gist from '@tsukiroku/gist';
import Gist from '@tsukiroku/gist'

const gist = new Gist('token');
const gist = new Gist('token')
```

<br>
Expand Down Expand Up @@ -59,7 +59,7 @@ await gist
// { secret: true }
)
.then((res) => console.log(`Gist created: ${res.data!.id}`))
.catch((err) => console.log(err));
.catch((err) => console.log(err))
```
<br>
Expand All @@ -76,7 +76,7 @@ await gist
await gist
.get('gist id')
.then((res) => console.log(`gist description: ${res.data!.description}`))
.catch((err) => console.log(err));
.catch((err) => console.log(err))
```

<br>
Expand Down Expand Up @@ -107,7 +107,7 @@ await gist
// { secret: true }
)
.then((res) => console.log(`Gist updated: ${res.status.code}`))
.catch((err) => console.log(err));
.catch((err) => console.log(err))
```

<br>
Expand All @@ -126,7 +126,7 @@ await gist
await gist
.delete(gist_id)
.then((res) => console.log(`Gist deleted, status: ${res.status.code}`))
.catch((err) => console.log(err));
.catch((err) => console.log(err))
```

<br>
Expand Down
4 changes: 2 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import Gist from './src/index';
import Gist from './src/index'

export default Gist;
export default Gist
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tsukiroku/gist",
"version": "1.0.73",
"version": "1.0.8",
"description": "Asynchronous gist API wrapper",
"main": "dist/index.js",
"scripts": {
Expand Down
Binary file added src/.DS_Store
Binary file not shown.
13 changes: 4 additions & 9 deletions src/gist/create.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import request from '../structures/request';
import { GistFile, GistOptions, GistResponse, ReqRet } from '../types';
import request from '../structures/request'
import { GistFile, GistOptions, GistResponse, ReqRet } from '../types'

export default async (
files: GistFile,
description: string,
token: string,
options?: GistOptions
): Promise<ReqRet<GistResponse>> =>
export default async (files: GistFile, description: string, token: string, options?: GistOptions): Promise<ReqRet<GistResponse>> =>
await request<GistResponse>('https://api.github.com/gists', token, 'POST', {
data: {
description: description,
Expand All @@ -15,4 +10,4 @@ export default async (
},
public: options?.secret ? true : false,
},
});
})
6 changes: 3 additions & 3 deletions src/gist/delete.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import request from '../structures/request';
import { ReqRet } from '../types';
import request from '../structures/request'
import { ReqRet } from '../types'

export default async (id: string, token: string): Promise<ReqRet<{}>> =>
await request<{}>(`https://api.github.com/gists/${id}`, token, 'DELETE', {})
Expand All @@ -8,4 +8,4 @@ export default async (id: string, token: string): Promise<ReqRet<{}>> =>
status: response.status,
})
)
.catch((err) => Promise.reject(err));
.catch((err) => Promise.reject(err))
18 changes: 5 additions & 13 deletions src/gist/get.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import request from '../structures/request';
import { GistResponse, ReqRet } from '../types';
import request from '../structures/request'
import { GistResponse, ReqRet } from '../types'

export default async (
id: string,
token: string
): Promise<ReqRet<GistResponse>> =>
await request<GistResponse>(
`https://api.github.com/gists/${id}`,
token,
'GET',
{}
)
export default async (id: string, token: string): Promise<ReqRet<GistResponse>> =>
await request<GistResponse>(`https://api.github.com/gists/${id}`, token, 'GET', {})
.then((response) =>
Promise.resolve({
data: response.data,
status: response.status,
})
)
.catch((err) => Promise.reject(err));
.catch((err) => Promise.reject(err))
10 changes: 5 additions & 5 deletions src/gist/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _create from './create';
import _delete from './delete';
import _get from './get';
import _update from './update';
import _create from './create'
import _delete from './delete'
import _get from './get'
import _update from './update'

export { _create, _delete, _get, _update };
export { _create, _delete, _get, _update }
14 changes: 4 additions & 10 deletions src/gist/update.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import request from '../structures/request';
import { GistFile, GistOptions, ReqRet } from '../types';
import request from '../structures/request'
import { GistFile, GistOptions, ReqRet } from '../types'

export default async (
id: string,
files: GistFile,
description: string,
token: string,
options?: GistOptions
): Promise<ReqRet<{}>> =>
export default async (id: string, files: GistFile, description: string, token: string, options?: GistOptions): Promise<ReqRet<{}>> =>
await request<{}>(`https://api.github.com/gists/${id}`, token, 'PATCH', {
data: {
description: description,
Expand All @@ -22,4 +16,4 @@ export default async (
status: response.status,
})
)
.catch((err) => Promise.reject(err));
.catch((err) => Promise.reject(err))
33 changes: 11 additions & 22 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,27 @@
import { GistFile, GistOptions, GistResponse, IGist, ReqRet } from './types';
import { _create, _delete, _get, _update } from './gist';
import { GistFile, GistOptions, GistResponse, IGist, ReqRet } from './types'
import { _create, _delete, _get, _update } from './gist'

export default class Gist implements IGist {
public readonly token: string;
public readonly token: string

constructor(token: string) {
if (!token || token === 'token') {
throw new Error('Token required');
}
this.token = token;
if (!token || token === 'token') throw new Error('Token required')
this.token = token
}

public create(
files: GistFile,
description: string,
options?: GistOptions
): Promise<ReqRet<GistResponse>> {
return _create(files, description, this.token, options);
public create(files: GistFile, description: string, options?: GistOptions): Promise<ReqRet<GistResponse>> {
return _create(files, description, this.token, options)
}

public delete(id: string): Promise<ReqRet<any>> {
return _delete(id, this.token);
return _delete(id, this.token)
}

public get(id: string): Promise<ReqRet<GistResponse>> {
return _get(id, this.token);
return _get(id, this.token)
}

public update(
id: string,
files: GistFile,
description: string,
options?: GistOptions
): Promise<ReqRet<any>> {
return _update(id, files, description, this.token, options);
public update(id: string, files: GistFile, description: string, options?: GistOptions): Promise<ReqRet<any>> {
return _update(id, files, description, this.token, options)
}
}
38 changes: 19 additions & 19 deletions src/structures/http_status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,43 @@ export enum HTTPStatus {
}

export interface HTTPStatusRes {
code: HTTPStatus;
data: string;
code: HTTPStatus
data: string
}

const t = (v: HTTPStatus): HTTPStatusRes => ({ code: v, data: HTTPStatus[v] });
const t = (v: HTTPStatus): HTTPStatusRes => ({ code: v, data: HTTPStatus[v] })

export default (status: number): HTTPStatusRes => {
switch (status) {
case 200:
return t(HTTPStatus.OK);
return t(HTTPStatus.OK)
case 201:
return t(HTTPStatus.CREATED);
return t(HTTPStatus.CREATED)
case 204:
return t(HTTPStatus.NO_CONTENT);
return t(HTTPStatus.NO_CONTENT)
case 304:
return t(HTTPStatus.NOT_MODIFIED);
return t(HTTPStatus.NOT_MODIFIED)
case 400:
return t(HTTPStatus.BAD_REQUEST);
return t(HTTPStatus.BAD_REQUEST)
case 401:
return t(HTTPStatus.UNAUTHORIZED);
return t(HTTPStatus.UNAUTHORIZED)
case 403:
return t(HTTPStatus.FORBIDDEN);
return t(HTTPStatus.FORBIDDEN)
case 404:
return t(HTTPStatus.NOT_FOUND);
return t(HTTPStatus.NOT_FOUND)
case 409:
return t(HTTPStatus.CONFLICT);
return t(HTTPStatus.CONFLICT)
case 422:
return t(HTTPStatus.VAILDATION_FAILED);
return t(HTTPStatus.VAILDATION_FAILED)
case 500:
return t(HTTPStatus.INTERNAL_SERVER_ERROR);
return t(HTTPStatus.INTERNAL_SERVER_ERROR)
case 502:
return t(HTTPStatus.BAD_GATEWAY);
return t(HTTPStatus.BAD_GATEWAY)
case 503:
return t(HTTPStatus.SERVICE_UNAVAILABLE);
return t(HTTPStatus.SERVICE_UNAVAILABLE)
case 504:
return t(HTTPStatus.GATEWAY_TIMEOUT);
return t(HTTPStatus.GATEWAY_TIMEOUT)
default:
return t(HTTPStatus.UNKNOWN);
return t(HTTPStatus.UNKNOWN)
}
};
}
15 changes: 5 additions & 10 deletions src/structures/request.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import axios, { AxiosRequestConfig, Method } from 'axios';
import { ReqRet } from '../types';
import st from './http_status';
import axios, { AxiosRequestConfig, Method } from 'axios'
import { ReqRet } from '../types'
import st from './http_status'

export default async <T>(
url: string,
token: string,
method: Method,
options: AxiosRequestConfig
): Promise<ReqRet<T>> =>
export default async <T>(url: string, token: string, method: Method, options: AxiosRequestConfig): Promise<ReqRet<T>> =>
await axios
.request<T>({
url,
Expand All @@ -25,4 +20,4 @@ export default async <T>(
status: st(response.status),
})
)
.catch((error) => Promise.reject(error));
.catch((error) => Promise.reject(error))
Loading

0 comments on commit 90dcefe

Please sign in to comment.