@@ -7,63 +7,63 @@ import * as vscode from 'vscode';
7
7
import { fetch , RequestError , RequestRateLimitError , RequestInvalidTokenError , RequestNotFoundError , throttledReportNetworkError } from './util/fetch' ;
8
8
9
9
interface UriState {
10
- owner : string ;
11
- repo : string ;
12
- branch : string ;
13
- path : string ;
14
- } ;
10
+ owner : string ;
11
+ repo : string ;
12
+ branch : string ;
13
+ path : string ;
14
+ }
15
15
16
16
const parseUri = ( uri : vscode . Uri ) : UriState => {
17
- const [ owner , repo , branch ] = ( uri . authority || '' ) . split ( '+' ) . filter ( Boolean ) ;
18
- return {
19
- owner,
20
- repo,
21
- branch,
22
- path : uri . path ,
23
- } ;
17
+ const [ owner , repo , branch ] = ( uri . authority || '' ) . split ( '+' ) . filter ( Boolean ) ;
18
+ return {
19
+ owner,
20
+ repo,
21
+ branch,
22
+ path : uri . path ,
23
+ } ;
24
24
} ;
25
25
26
26
const handleRequestError = ( error : RequestError ) => {
27
- if ( error instanceof RequestRateLimitError ) {
28
- if ( ! error . token ) {
29
- throw vscode . FileSystemError . NoPermissions ( 'API Rate Limit Exceeded, Please Offer an OAuth Token.' ) ;
30
- }
31
- throw vscode . FileSystemError . NoPermissions ( 'API Rate Limit Exceeded, Please Change Another OAuth Token.' ) ;
32
- }
33
- if ( error instanceof RequestInvalidTokenError ) {
34
- throw vscode . FileSystemError . NoPermissions ( 'Current OAuth Token Is Invalid, Please Change Another One.' ) ;
35
- }
36
- if ( error instanceof RequestNotFoundError ) {
37
- throw vscode . FileSystemError . NoPermissions ( 'Current OAuth Token Is Invalid, Please Change Another One.' ) ;
38
- }
39
- if ( error instanceof RequestNotFoundError ) {
40
- throw vscode . FileSystemError . FileNotFound ( 'GitHub Resource Not Found' ) ;
41
- }
42
- throw vscode . FileSystemError . Unavailable ( error . message || 'Unknown Error Occurred When Request To GitHub' ) ;
27
+ if ( error instanceof RequestRateLimitError ) {
28
+ if ( ! error . token ) {
29
+ throw vscode . FileSystemError . NoPermissions ( 'API Rate Limit Exceeded, Please Offer an OAuth Token.' ) ;
30
+ }
31
+ throw vscode . FileSystemError . NoPermissions ( 'API Rate Limit Exceeded, Please Change Another OAuth Token.' ) ;
32
+ }
33
+ if ( error instanceof RequestInvalidTokenError ) {
34
+ throw vscode . FileSystemError . NoPermissions ( 'Current OAuth Token Is Invalid, Please Change Another One.' ) ;
35
+ }
36
+ if ( error instanceof RequestNotFoundError ) {
37
+ throw vscode . FileSystemError . NoPermissions ( 'Current OAuth Token Is Invalid, Please Change Another One.' ) ;
38
+ }
39
+ if ( error instanceof RequestNotFoundError ) {
40
+ throw vscode . FileSystemError . FileNotFound ( 'GitHub Resource Not Found' ) ;
41
+ }
42
+ throw vscode . FileSystemError . Unavailable ( error . message || 'Unknown Error Occurred When Request To GitHub' ) ;
43
43
} ;
44
44
45
45
export const readGitHubDirectory = ( uri : vscode . Uri ) => {
46
- const state : UriState = parseUri ( uri ) ;
47
- return fetch ( `https://api.github.com/repos/${ state . owner } /${ state . repo } /git/trees/${ state . branch } ${ state . path . replace ( / ^ \/ / , ':' ) } ` )
48
- . catch ( handleRequestError )
46
+ const state : UriState = parseUri ( uri ) ;
47
+ return fetch ( `https://api.github.com/repos/${ state . owner } /${ state . repo } /git/trees/${ state . branch } ${ state . path . replace ( / ^ \/ / , ':' ) } ` )
48
+ . catch ( handleRequestError ) ;
49
49
} ;
50
50
51
51
export const readGitHubFile = ( uri : vscode . Uri , fileSha : string ) => {
52
- const state : UriState = parseUri ( uri ) ;
53
- return fetch ( `https://api.github.com/repos/${ state . owner } /${ state . repo } /git/blobs/${ fileSha } ` )
54
- . catch ( handleRequestError ) ;
52
+ const state : UriState = parseUri ( uri ) ;
53
+ return fetch ( `https://api.github.com/repos/${ state . owner } /${ state . repo } /git/blobs/${ fileSha } ` )
54
+ . catch ( handleRequestError ) ;
55
55
} ;
56
56
57
57
export const validateToken = ( token : string ) => {
58
- const authHeaders = token ? { Authorization : `token ${ token } ` } : { } ;
59
- return self . fetch ( `https://api.github.com` , { headers : { ...authHeaders } } ) . then ( response => ( {
60
- token : ! ! token , // if the token is not empty
61
- valid : response . status !== 401 ? true : false , // if the request is valid
62
- limit : + response . headers . get ( 'X-RateLimit-Limit' ) || 0 , // limit count
63
- remaining : + response . headers . get ( 'X-RateLimit-Remaining' ) || 0 , // remains request count
64
- reset : + response . headers . get ( 'X-RateLimit-Reset' ) || 0 , // reset time
65
- } ) ) . catch ( ( ) => {
66
- throttledReportNetworkError ( ) ;
67
- throw new RequestError ( 'Request Failed, Maybe an Network Error' , token ) ;
68
- } ) ;
58
+ const authHeaders = token ? { Authorization : `token ${ token } ` } : { } ;
59
+ return self . fetch ( `https://api.github.com` , { headers : { ...authHeaders } } ) . then ( response => ( {
60
+ token : ! ! token , // if the token is not empty
61
+ valid : response . status !== 401 ? true : false , // if the request is valid
62
+ limit : + response . headers . get ( 'X-RateLimit-Limit' ) || 0 , // limit count
63
+ remaining : + response . headers . get ( 'X-RateLimit-Remaining' ) || 0 , // remains request count
64
+ reset : + response . headers . get ( 'X-RateLimit-Reset' ) || 0 , // reset time
65
+ } ) ) . catch ( ( ) => {
66
+ throttledReportNetworkError ( ) ;
67
+ throw new RequestError ( 'Request Failed, Maybe an Network Error' , token ) ;
68
+ } ) ;
69
69
} ;
0 commit comments