From ffd0e235597342a226c3aa9c835ceae47963c90f Mon Sep 17 00:00:00 2001 From: Jacob Gillespie Date: Sat, 6 Nov 2021 10:15:53 -0400 Subject: [PATCH] feat: allow providing custom fetch implementation --- README.md | 13 ++++++++++++- src/PostgrestClient.ts | 16 ++++++++++++++-- src/lib/PostgrestQueryBuilder.ts | 10 +++++++--- src/lib/PostgrestRpcBuilder.ts | 10 +++++++--- src/lib/types.ts | 8 ++++++-- 5 files changed, 46 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 169cb27b..3bc67a54 100644 --- a/README.md +++ b/README.md @@ -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/). @@ -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. diff --git a/src/PostgrestClient.ts b/src/PostgrestClient.ts index 4171e9b8..1e55de27 100644 --- a/src/PostgrestClient.ts +++ b/src/PostgrestClient.ts @@ -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. @@ -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 } /** @@ -41,7 +48,11 @@ export default class PostgrestClient { */ from(table: string): PostgrestQueryBuilder { const url = `${this.url}/${table}` - return new PostgrestQueryBuilder(url, { headers: this.headers, schema: this.schema }) + return new PostgrestQueryBuilder(url, { + headers: this.headers, + schema: this.schema, + fetch: this.fetch, + }) } /** @@ -67,6 +78,7 @@ export default class PostgrestClient { return new PostgrestRpcBuilder(url, { headers: this.headers, schema: this.schema, + fetch: this.fetch, }).rpc(params, { head, count }) } } diff --git a/src/lib/PostgrestQueryBuilder.ts b/src/lib/PostgrestQueryBuilder.ts index 378170ad..9435eff1 100644 --- a/src/lib/PostgrestQueryBuilder.ts +++ b/src/lib/PostgrestQueryBuilder.ts @@ -1,12 +1,16 @@ -import { PostgrestBuilder } from './types' +import { Fetch, PostgrestBuilder } from './types' import PostgrestFilterBuilder from './PostgrestFilterBuilder' export default class PostgrestQueryBuilder extends PostgrestBuilder { 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) + super(({ fetch } as unknown) as PostgrestBuilder) this.url = new URL(url) this.headers = { ...headers } this.schema = schema diff --git a/src/lib/PostgrestRpcBuilder.ts b/src/lib/PostgrestRpcBuilder.ts index c25eff99..25c69c1f 100644 --- a/src/lib/PostgrestRpcBuilder.ts +++ b/src/lib/PostgrestRpcBuilder.ts @@ -1,12 +1,16 @@ -import { PostgrestBuilder } from './types' +import { Fetch, PostgrestBuilder } from './types' import PostgrestFilterBuilder from './PostgrestFilterBuilder' export default class PostgrestRpcBuilder extends PostgrestBuilder { 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) + super(({ fetch } as unknown) as PostgrestBuilder) this.url = new URL(url) this.headers = { ...headers } this.schema = schema diff --git a/src/lib/types.ts b/src/lib/types.ts index c943de8d..6742a1a4 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -1,4 +1,6 @@ -import fetch from 'cross-fetch' +import crossFetch from 'cross-fetch' + +export type Fetch = typeof fetch /** * Error format @@ -56,9 +58,11 @@ export abstract class PostgrestBuilder implements PromiseLike | Partial[] protected shouldThrowOnError = false protected signal?: AbortSignal + protected fetch: Fetch constructor(builder: PostgrestBuilder) { Object.assign(this, builder) + this.fetch = builder.fetch || crossFetch } /** @@ -91,7 +95,7 @@ export abstract class PostgrestBuilder implements PromiseLike