Skip to content
This repository was archived by the owner on Oct 9, 2025. It is now read-only.

Commit 76a2926

Browse files
authored
Merge pull request #222 from jacobwgillespie/fetch
feat: allow providing custom fetch implementation
2 parents f6683f1 + ffd0e23 commit 76a2926

File tree

5 files changed

+46
-11
lines changed

5 files changed

+46
-11
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![Package](https://img.shields.io/npm/v/@supabase/postgrest-js)](https://www.npmjs.com/package/@supabase/postgrest-js)
55
[![License: MIT](https://img.shields.io/npm/l/@supabase/postgrest-js)](#license)
66

7-
Isomorphic JavaScript client for [PostgREST](https://postgrest.org). The goal of this library is to make an "ORM-like" restful interface.
7+
Isomorphic JavaScript client for [PostgREST](https://postgrest.org). The goal of this library is to make an "ORM-like" restful interface.
88

99
Full documentation can be found [here](https://supabase.github.io/postgrest-js/).
1010

@@ -30,6 +30,17 @@ const postgrest = new PostgrestClient(REST_URL)
3030
- update(): https://supabase.io/docs/reference/javascript/update
3131
- delete(): https://supabase.io/docs/reference/javascript/delete
3232

33+
#### Custom `fetch` implementation
34+
35+
`postgrest-js` uses the [`cross-fetch`](https://www.npmjs.com/package/cross-fetch) library to make HTTP requests, but an alternative `fetch` implementation can be provided as an option. This is most useful in environments where `cross-fetch` is not compatible, for instance Cloudflare Workers:
36+
37+
```js
38+
import { PostgrestClient } from '@supabase/postgrest-js'
39+
40+
const REST_URL = 'http://localhost:3000'
41+
const postgrest = new PostgrestClient(REST_URL, { fetch: fetch })
42+
```
43+
3344
## License
3445

3546
This repo is licensed under MIT License.

src/PostgrestClient.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import PostgrestQueryBuilder from './lib/PostgrestQueryBuilder'
22
import PostgrestRpcBuilder from './lib/PostgrestRpcBuilder'
33
import PostgrestFilterBuilder from './lib/PostgrestFilterBuilder'
44
import { DEFAULT_HEADERS } from './lib/constants'
5+
import { Fetch } from './lib/types'
56

67
export default class PostgrestClient {
78
url: string
89
headers: { [key: string]: string }
910
schema?: string
11+
fetch?: Fetch
1012

1113
/**
1214
* Creates a PostgREST client.
@@ -17,11 +19,16 @@ export default class PostgrestClient {
1719
*/
1820
constructor(
1921
url: string,
20-
{ headers = {}, schema }: { headers?: { [key: string]: string }; schema?: string } = {}
22+
{
23+
headers = {},
24+
schema,
25+
fetch,
26+
}: { headers?: { [key: string]: string }; schema?: string; fetch?: Fetch } = {}
2127
) {
2228
this.url = url
2329
this.headers = { ...DEFAULT_HEADERS, ...headers }
2430
this.schema = schema
31+
this.fetch = fetch
2532
}
2633

2734
/**
@@ -41,7 +48,11 @@ export default class PostgrestClient {
4148
*/
4249
from<T = any>(table: string): PostgrestQueryBuilder<T> {
4350
const url = `${this.url}/${table}`
44-
return new PostgrestQueryBuilder<T>(url, { headers: this.headers, schema: this.schema })
51+
return new PostgrestQueryBuilder<T>(url, {
52+
headers: this.headers,
53+
schema: this.schema,
54+
fetch: this.fetch,
55+
})
4556
}
4657

4758
/**
@@ -67,6 +78,7 @@ export default class PostgrestClient {
6778
return new PostgrestRpcBuilder<T>(url, {
6879
headers: this.headers,
6980
schema: this.schema,
81+
fetch: this.fetch,
7082
}).rpc(params, { head, count })
7183
}
7284
}

src/lib/PostgrestQueryBuilder.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
import { PostgrestBuilder } from './types'
1+
import { Fetch, PostgrestBuilder } from './types'
22
import PostgrestFilterBuilder from './PostgrestFilterBuilder'
33

44
export default class PostgrestQueryBuilder<T> extends PostgrestBuilder<T> {
55
constructor(
66
url: string,
7-
{ headers = {}, schema }: { headers?: { [key: string]: string }; schema?: string } = {}
7+
{
8+
headers = {},
9+
schema,
10+
fetch,
11+
}: { headers?: { [key: string]: string }; schema?: string; fetch?: Fetch } = {}
812
) {
9-
super({} as PostgrestBuilder<T>)
13+
super(({ fetch } as unknown) as PostgrestBuilder<T>)
1014
this.url = new URL(url)
1115
this.headers = { ...headers }
1216
this.schema = schema

src/lib/PostgrestRpcBuilder.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
import { PostgrestBuilder } from './types'
1+
import { Fetch, PostgrestBuilder } from './types'
22
import PostgrestFilterBuilder from './PostgrestFilterBuilder'
33

44
export default class PostgrestRpcBuilder<T> extends PostgrestBuilder<T> {
55
constructor(
66
url: string,
7-
{ headers = {}, schema }: { headers?: { [key: string]: string }; schema?: string } = {}
7+
{
8+
headers = {},
9+
schema,
10+
fetch,
11+
}: { headers?: { [key: string]: string }; schema?: string; fetch?: Fetch } = {}
812
) {
9-
super({} as PostgrestBuilder<T>)
13+
super(({ fetch } as unknown) as PostgrestBuilder<T>)
1014
this.url = new URL(url)
1115
this.headers = { ...headers }
1216
this.schema = schema

src/lib/types.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import fetch from 'cross-fetch'
1+
import crossFetch from 'cross-fetch'
2+
3+
export type Fetch = typeof fetch
24

35
/**
46
* Error format
@@ -56,9 +58,11 @@ export abstract class PostgrestBuilder<T> implements PromiseLike<PostgrestRespon
5658
protected body?: Partial<T> | Partial<T>[]
5759
protected shouldThrowOnError = false
5860
protected signal?: AbortSignal
61+
protected fetch: Fetch
5962

6063
constructor(builder: PostgrestBuilder<T>) {
6164
Object.assign(this, builder)
65+
this.fetch = builder.fetch || crossFetch
6266
}
6367

6468
/**
@@ -91,7 +95,7 @@ export abstract class PostgrestBuilder<T> implements PromiseLike<PostgrestRespon
9195
this.headers['Content-Type'] = 'application/json'
9296
}
9397

94-
let res = fetch(this.url.toString(), {
98+
let res = this.fetch(this.url.toString(), {
9599
method: this.method,
96100
headers: this.headers,
97101
body: JSON.stringify(this.body),

0 commit comments

Comments
 (0)