Skip to content

Commit 90dcefe

Browse files
committed
Updates (Refactoring)
1 parent 2fb69ea commit 90dcefe

14 files changed

+116
-157
lines changed

.DS_Store

6 KB
Binary file not shown.

README.md

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
- [Gist API](./README.md#gist-api)
2-
- [Docs](./README.md#docs)
3-
- [Status codes](./README.md#status-codes)
4-
51
# Gist API
62

7-
```sh
8-
npm i @tsukiroku/gist
3+
```console
4+
$ npm i @tsukiroku/gist
95
```
106

7+
- [Gist API](./README.md#gist-api)
8+
- [Docs](./README.md#docs)
9+
- [Status codes](./README.md#status-codes)
10+
1111
---
1212

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

22-
const gist = new Gist('token');
22+
const gist = new Gist('token')
2323
```
2424

2525
<br>
@@ -59,7 +59,7 @@ await gist
5959
// { secret: true }
6060
)
6161
.then((res) => console.log(`Gist created: ${res.data!.id}`))
62-
.catch((err) => console.log(err));
62+
.catch((err) => console.log(err))
6363
```
6464
6565
<br>
@@ -76,7 +76,7 @@ await gist
7676
await gist
7777
.get('gist id')
7878
.then((res) => console.log(`gist description: ${res.data!.description}`))
79-
.catch((err) => console.log(err));
79+
.catch((err) => console.log(err))
8080
```
8181

8282
<br>
@@ -107,7 +107,7 @@ await gist
107107
// { secret: true }
108108
)
109109
.then((res) => console.log(`Gist updated: ${res.status.code}`))
110-
.catch((err) => console.log(err));
110+
.catch((err) => console.log(err))
111111
```
112112

113113
<br>
@@ -126,7 +126,7 @@ await gist
126126
await gist
127127
.delete(gist_id)
128128
.then((res) => console.log(`Gist deleted, status: ${res.status.code}`))
129-
.catch((err) => console.log(err));
129+
.catch((err) => console.log(err))
130130
```
131131

132132
<br>

index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import Gist from './src/index';
1+
import Gist from './src/index'
22

3-
export default Gist;
3+
export default Gist

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tsukiroku/gist",
3-
"version": "1.0.73",
3+
"version": "1.0.8",
44
"description": "Asynchronous gist API wrapper",
55
"main": "dist/index.js",
66
"scripts": {

src/.DS_Store

6 KB
Binary file not shown.

src/gist/create.ts

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
import request from '../structures/request';
2-
import { GistFile, GistOptions, GistResponse, ReqRet } from '../types';
1+
import request from '../structures/request'
2+
import { GistFile, GistOptions, GistResponse, ReqRet } from '../types'
33

4-
export default async (
5-
files: GistFile,
6-
description: string,
7-
token: string,
8-
options?: GistOptions
9-
): Promise<ReqRet<GistResponse>> =>
4+
export default async (files: GistFile, description: string, token: string, options?: GistOptions): Promise<ReqRet<GistResponse>> =>
105
await request<GistResponse>('https://api.github.com/gists', token, 'POST', {
116
data: {
127
description: description,
@@ -15,4 +10,4 @@ export default async (
1510
},
1611
public: options?.secret ? true : false,
1712
},
18-
});
13+
})

src/gist/delete.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import request from '../structures/request';
2-
import { ReqRet } from '../types';
1+
import request from '../structures/request'
2+
import { ReqRet } from '../types'
33

44
export default async (id: string, token: string): Promise<ReqRet<{}>> =>
55
await request<{}>(`https://api.github.com/gists/${id}`, token, 'DELETE', {})
@@ -8,4 +8,4 @@ export default async (id: string, token: string): Promise<ReqRet<{}>> =>
88
status: response.status,
99
})
1010
)
11-
.catch((err) => Promise.reject(err));
11+
.catch((err) => Promise.reject(err))

src/gist/get.ts

+5-13
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
1-
import request from '../structures/request';
2-
import { GistResponse, ReqRet } from '../types';
1+
import request from '../structures/request'
2+
import { GistResponse, ReqRet } from '../types'
33

4-
export default async (
5-
id: string,
6-
token: string
7-
): Promise<ReqRet<GistResponse>> =>
8-
await request<GistResponse>(
9-
`https://api.github.com/gists/${id}`,
10-
token,
11-
'GET',
12-
{}
13-
)
4+
export default async (id: string, token: string): Promise<ReqRet<GistResponse>> =>
5+
await request<GistResponse>(`https://api.github.com/gists/${id}`, token, 'GET', {})
146
.then((response) =>
157
Promise.resolve({
168
data: response.data,
179
status: response.status,
1810
})
1911
)
20-
.catch((err) => Promise.reject(err));
12+
.catch((err) => Promise.reject(err))

src/gist/index.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import _create from './create';
2-
import _delete from './delete';
3-
import _get from './get';
4-
import _update from './update';
1+
import _create from './create'
2+
import _delete from './delete'
3+
import _get from './get'
4+
import _update from './update'
55

6-
export { _create, _delete, _get, _update };
6+
export { _create, _delete, _get, _update }

src/gist/update.ts

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
import request from '../structures/request';
2-
import { GistFile, GistOptions, ReqRet } from '../types';
1+
import request from '../structures/request'
2+
import { GistFile, GistOptions, ReqRet } from '../types'
33

4-
export default async (
5-
id: string,
6-
files: GistFile,
7-
description: string,
8-
token: string,
9-
options?: GistOptions
10-
): Promise<ReqRet<{}>> =>
4+
export default async (id: string, files: GistFile, description: string, token: string, options?: GistOptions): Promise<ReqRet<{}>> =>
115
await request<{}>(`https://api.github.com/gists/${id}`, token, 'PATCH', {
126
data: {
137
description: description,
@@ -22,4 +16,4 @@ export default async (
2216
status: response.status,
2317
})
2418
)
25-
.catch((err) => Promise.reject(err));
19+
.catch((err) => Promise.reject(err))

src/index.ts

+11-22
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,27 @@
1-
import { GistFile, GistOptions, GistResponse, IGist, ReqRet } from './types';
2-
import { _create, _delete, _get, _update } from './gist';
1+
import { GistFile, GistOptions, GistResponse, IGist, ReqRet } from './types'
2+
import { _create, _delete, _get, _update } from './gist'
33

44
export default class Gist implements IGist {
5-
public readonly token: string;
5+
public readonly token: string
66

77
constructor(token: string) {
8-
if (!token || token === 'token') {
9-
throw new Error('Token required');
10-
}
11-
this.token = token;
8+
if (!token || token === 'token') throw new Error('Token required')
9+
this.token = token
1210
}
1311

14-
public create(
15-
files: GistFile,
16-
description: string,
17-
options?: GistOptions
18-
): Promise<ReqRet<GistResponse>> {
19-
return _create(files, description, this.token, options);
12+
public create(files: GistFile, description: string, options?: GistOptions): Promise<ReqRet<GistResponse>> {
13+
return _create(files, description, this.token, options)
2014
}
2115

2216
public delete(id: string): Promise<ReqRet<any>> {
23-
return _delete(id, this.token);
17+
return _delete(id, this.token)
2418
}
2519

2620
public get(id: string): Promise<ReqRet<GistResponse>> {
27-
return _get(id, this.token);
21+
return _get(id, this.token)
2822
}
2923

30-
public update(
31-
id: string,
32-
files: GistFile,
33-
description: string,
34-
options?: GistOptions
35-
): Promise<ReqRet<any>> {
36-
return _update(id, files, description, this.token, options);
24+
public update(id: string, files: GistFile, description: string, options?: GistOptions): Promise<ReqRet<any>> {
25+
return _update(id, files, description, this.token, options)
3726
}
3827
}

src/structures/http_status.ts

+19-19
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,43 @@ export enum HTTPStatus {
1717
}
1818

1919
export interface HTTPStatusRes {
20-
code: HTTPStatus;
21-
data: string;
20+
code: HTTPStatus
21+
data: string
2222
}
2323

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

2626
export default (status: number): HTTPStatusRes => {
2727
switch (status) {
2828
case 200:
29-
return t(HTTPStatus.OK);
29+
return t(HTTPStatus.OK)
3030
case 201:
31-
return t(HTTPStatus.CREATED);
31+
return t(HTTPStatus.CREATED)
3232
case 204:
33-
return t(HTTPStatus.NO_CONTENT);
33+
return t(HTTPStatus.NO_CONTENT)
3434
case 304:
35-
return t(HTTPStatus.NOT_MODIFIED);
35+
return t(HTTPStatus.NOT_MODIFIED)
3636
case 400:
37-
return t(HTTPStatus.BAD_REQUEST);
37+
return t(HTTPStatus.BAD_REQUEST)
3838
case 401:
39-
return t(HTTPStatus.UNAUTHORIZED);
39+
return t(HTTPStatus.UNAUTHORIZED)
4040
case 403:
41-
return t(HTTPStatus.FORBIDDEN);
41+
return t(HTTPStatus.FORBIDDEN)
4242
case 404:
43-
return t(HTTPStatus.NOT_FOUND);
43+
return t(HTTPStatus.NOT_FOUND)
4444
case 409:
45-
return t(HTTPStatus.CONFLICT);
45+
return t(HTTPStatus.CONFLICT)
4646
case 422:
47-
return t(HTTPStatus.VAILDATION_FAILED);
47+
return t(HTTPStatus.VAILDATION_FAILED)
4848
case 500:
49-
return t(HTTPStatus.INTERNAL_SERVER_ERROR);
49+
return t(HTTPStatus.INTERNAL_SERVER_ERROR)
5050
case 502:
51-
return t(HTTPStatus.BAD_GATEWAY);
51+
return t(HTTPStatus.BAD_GATEWAY)
5252
case 503:
53-
return t(HTTPStatus.SERVICE_UNAVAILABLE);
53+
return t(HTTPStatus.SERVICE_UNAVAILABLE)
5454
case 504:
55-
return t(HTTPStatus.GATEWAY_TIMEOUT);
55+
return t(HTTPStatus.GATEWAY_TIMEOUT)
5656
default:
57-
return t(HTTPStatus.UNKNOWN);
57+
return t(HTTPStatus.UNKNOWN)
5858
}
59-
};
59+
}

src/structures/request.ts

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
import axios, { AxiosRequestConfig, Method } from 'axios';
2-
import { ReqRet } from '../types';
3-
import st from './http_status';
1+
import axios, { AxiosRequestConfig, Method } from 'axios'
2+
import { ReqRet } from '../types'
3+
import st from './http_status'
44

5-
export default async <T>(
6-
url: string,
7-
token: string,
8-
method: Method,
9-
options: AxiosRequestConfig
10-
): Promise<ReqRet<T>> =>
5+
export default async <T>(url: string, token: string, method: Method, options: AxiosRequestConfig): Promise<ReqRet<T>> =>
116
await axios
127
.request<T>({
138
url,
@@ -25,4 +20,4 @@ export default async <T>(
2520
status: st(response.status),
2621
})
2722
)
28-
.catch((error) => Promise.reject(error));
23+
.catch((error) => Promise.reject(error))

0 commit comments

Comments
 (0)