File tree 2 files changed +12
-5
lines changed
2 files changed +12
-5
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ ' @gitbook/api ' : patch
3
+ ---
4
+
5
+ Fix use of the API client in some Cloudflare environment where it throws "Illegal invocation"
Original file line number Diff line number Diff line change @@ -42,7 +42,7 @@ export class GitBookAPI extends Api<{
42
42
/**
43
43
* Service binding used to request the API.
44
44
*/
45
- public readonly serviceBinding : GitBookAPIServiceBinding ;
45
+ public readonly serviceBinding : GitBookAPIServiceBinding | undefined ;
46
46
47
47
constructor (
48
48
options : {
@@ -74,9 +74,7 @@ export class GitBookAPI extends Api<{
74
74
endpoint = GITBOOK_DEFAULT_ENDPOINT ,
75
75
authToken,
76
76
userAgent = `${ name } /${ version } ` ,
77
- serviceBinding = {
78
- fetch,
79
- } ,
77
+ serviceBinding,
80
78
} = options ;
81
79
82
80
const normalizedEndpoint = endpoint . endsWith ( '/' ) ? endpoint . slice ( 0 , - 1 ) : endpoint ;
@@ -108,7 +106,11 @@ export class GitBookAPI extends Api<{
108
106
delete init . referrerPolicy ;
109
107
}
110
108
111
- const response = await this . serviceBinding . fetch ( input , init ) ;
109
+ // To avoid errors "TypeError: Illegal invocation: function called with incorrect this reference" on Cloudflare
110
+ // we always call `fetch` without passing it in an object.
111
+ const response = await ( this . serviceBinding
112
+ ? this . serviceBinding . fetch ( input , init )
113
+ : fetch ( input , init ) ) ;
112
114
113
115
if ( ! response . ok ) {
114
116
let error : string = response . statusText ;
You can’t perform that action at this time.
0 commit comments