@@ -7,63 +7,63 @@ import * as vscode from 'vscode';
77import { fetch , RequestError , RequestRateLimitError , RequestInvalidTokenError , RequestNotFoundError , throttledReportNetworkError } from './util/fetch' ;
88
99interface 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+ }
1515
1616const 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+ } ;
2424} ;
2525
2626const 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' ) ;
4343} ;
4444
4545export 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 ) ;
4949} ;
5050
5151export 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 ) ;
5555} ;
5656
5757export 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+ } ) ;
6969} ;
0 commit comments