Skip to content

feat: allow providing custom fetch implementation #222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![Package](https://img.shields.io/npm/v/@supabase/postgrest-js)](https://www.npmjs.com/package/@supabase/postgrest-js)
[![License: MIT](https://img.shields.io/npm/l/@supabase/postgrest-js)](#license)

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

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

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

#### Custom `fetch` implementation

`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:

```js
import { PostgrestClient } from '@supabase/postgrest-js'

const REST_URL = 'http://localhost:3000'
const postgrest = new PostgrestClient(REST_URL, { fetch: fetch })
```

## License

This repo is licensed under MIT License.
Expand Down
16 changes: 14 additions & 2 deletions src/PostgrestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import PostgrestQueryBuilder from './lib/PostgrestQueryBuilder'
import PostgrestRpcBuilder from './lib/PostgrestRpcBuilder'
import PostgrestFilterBuilder from './lib/PostgrestFilterBuilder'
import { DEFAULT_HEADERS } from './lib/constants'
import { Fetch } from './lib/types'

export default class PostgrestClient {
url: string
headers: { [key: string]: string }
schema?: string
fetch?: Fetch

/**
* Creates a PostgREST client.
Expand All @@ -17,11 +19,16 @@ export default class PostgrestClient {
*/
constructor(
url: string,
{ headers = {}, schema }: { headers?: { [key: string]: string }; schema?: string } = {}
{
headers = {},
schema,
fetch,
}: { headers?: { [key: string]: string }; schema?: string; fetch?: Fetch } = {}
) {
this.url = url
this.headers = { ...DEFAULT_HEADERS, ...headers }
this.schema = schema
this.fetch = fetch
}

/**
Expand All @@ -41,7 +48,11 @@ export default class PostgrestClient {
*/
from<T = any>(table: string): PostgrestQueryBuilder<T> {
const url = `${this.url}/${table}`
return new PostgrestQueryBuilder<T>(url, { headers: this.headers, schema: this.schema })
return new PostgrestQueryBuilder<T>(url, {
headers: this.headers,
schema: this.schema,
fetch: this.fetch,
})
}

/**
Expand All @@ -67,6 +78,7 @@ export default class PostgrestClient {
return new PostgrestRpcBuilder<T>(url, {
headers: this.headers,
schema: this.schema,
fetch: this.fetch,
}).rpc(params, { head, count })
}
}
10 changes: 7 additions & 3 deletions src/lib/PostgrestQueryBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { PostgrestBuilder } from './types'
import { Fetch, PostgrestBuilder } from './types'
import PostgrestFilterBuilder from './PostgrestFilterBuilder'

export default class PostgrestQueryBuilder<T> extends PostgrestBuilder<T> {
constructor(
url: string,
{ headers = {}, schema }: { headers?: { [key: string]: string }; schema?: string } = {}
{
headers = {},
schema,
fetch,
}: { headers?: { [key: string]: string }; schema?: string; fetch?: Fetch } = {}
) {
super({} as PostgrestBuilder<T>)
super(({ fetch } as unknown) as PostgrestBuilder<T>)
this.url = new URL(url)
this.headers = { ...headers }
this.schema = schema
Expand Down
10 changes: 7 additions & 3 deletions src/lib/PostgrestRpcBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { PostgrestBuilder } from './types'
import { Fetch, PostgrestBuilder } from './types'
import PostgrestFilterBuilder from './PostgrestFilterBuilder'

export default class PostgrestRpcBuilder<T> extends PostgrestBuilder<T> {
constructor(
url: string,
{ headers = {}, schema }: { headers?: { [key: string]: string }; schema?: string } = {}
{
headers = {},
schema,
fetch,
}: { headers?: { [key: string]: string }; schema?: string; fetch?: Fetch } = {}
) {
super({} as PostgrestBuilder<T>)
super(({ fetch } as unknown) as PostgrestBuilder<T>)
this.url = new URL(url)
this.headers = { ...headers }
this.schema = schema
Expand Down
8 changes: 6 additions & 2 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import fetch from 'cross-fetch'
import crossFetch from 'cross-fetch'

export type Fetch = typeof fetch

/**
* Error format
Expand Down Expand Up @@ -56,9 +58,11 @@ export abstract class PostgrestBuilder<T> implements PromiseLike<PostgrestRespon
protected body?: Partial<T> | Partial<T>[]
protected shouldThrowOnError = false
protected signal?: AbortSignal
protected fetch: Fetch

constructor(builder: PostgrestBuilder<T>) {
Object.assign(this, builder)
this.fetch = builder.fetch || crossFetch
}

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

let res = fetch(this.url.toString(), {
let res = this.fetch(this.url.toString(), {
method: this.method,
headers: this.headers,
body: JSON.stringify(this.body),
Expand Down