Skip to content

Commit f0d076e

Browse files
authored
Try fixing error "Illegal invocation" (#747)
* Try fixing error "Illegal invocation" * Changeset * Format
1 parent 6341510 commit f0d076e

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

Diff for: .changeset/smooth-beans-sin.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@gitbook/api': patch
3+
---
4+
5+
Fix use of the API client in some Cloudflare environment where it throws "Illegal invocation"

Diff for: packages/api/src/index.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class GitBookAPI extends Api<{
4242
/**
4343
* Service binding used to request the API.
4444
*/
45-
public readonly serviceBinding: GitBookAPIServiceBinding;
45+
public readonly serviceBinding: GitBookAPIServiceBinding | undefined;
4646

4747
constructor(
4848
options: {
@@ -74,9 +74,7 @@ export class GitBookAPI extends Api<{
7474
endpoint = GITBOOK_DEFAULT_ENDPOINT,
7575
authToken,
7676
userAgent = `${name}/${version}`,
77-
serviceBinding = {
78-
fetch,
79-
},
77+
serviceBinding,
8078
} = options;
8179

8280
const normalizedEndpoint = endpoint.endsWith('/') ? endpoint.slice(0, -1) : endpoint;
@@ -108,7 +106,11 @@ export class GitBookAPI extends Api<{
108106
delete init.referrerPolicy;
109107
}
110108

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));
112114

113115
if (!response.ok) {
114116
let error: string = response.statusText;

0 commit comments

Comments
 (0)