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

Commit 2640134

Browse files
committed
feat: support setting AbortSignal
1 parent 356eb8c commit 2640134

File tree

5 files changed

+47
-0
lines changed

5 files changed

+47
-0
lines changed

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"@types/node-fetch": "^2.5.7",
3838
"genversion": "^3.0.1",
3939
"jest": "^26.4.1",
40+
"node-abort-controller": "^3.0.0",
4041
"npm-run-all": "^4.1.5",
4142
"prettier": "^2.0.5",
4243
"rimraf": "^3.0.2",

src/lib/PostgrestTransformBuilder.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ export default class PostgrestTransformBuilder<T> extends PostgrestBuilder<T> {
8585
return this
8686
}
8787

88+
/**
89+
* Sets the AbortSignal for the fetch request.
90+
*/
91+
abortSignal(signal: AbortSignal): this {
92+
this.signal = signal
93+
return this
94+
}
95+
8896
/**
8997
* Retrieves only one row from the result. Result must be one row (e.g. using
9098
* `limit`), otherwise this will result in an error.

src/lib/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export abstract class PostgrestBuilder<T> implements PromiseLike<PostgrestRespon
5555
protected schema?: string
5656
protected body?: Partial<T> | Partial<T>[]
5757
protected shouldThrowOnError = false
58+
protected signal?: AbortSignal
5859

5960
constructor(builder: PostgrestBuilder<T>) {
6061
Object.assign(this, builder)
@@ -94,6 +95,7 @@ export abstract class PostgrestBuilder<T> implements PromiseLike<PostgrestRespon
9495
method: this.method,
9596
headers: this.headers,
9697
body: JSON.stringify(this.body),
98+
signal: this.signal,
9799
}).then(async (res) => {
98100
let error = null
99101
let data = null

test/transforms.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { PostgrestClient } from '../src/index'
22

3+
import { AbortController } from 'node-abort-controller'
4+
35
const postgrest = new PostgrestClient('http://localhost:3000')
46

57
test('order', async () => {
@@ -78,3 +80,24 @@ test('csv', async () => {
7880
}
7981
`)
8082
})
83+
84+
test('abort signal', async () => {
85+
const ac = new AbortController()
86+
ac.abort()
87+
const res = await postgrest.from('users').select().abortSignal(ac.signal)
88+
expect(res).toMatchInlineSnapshot(`
89+
Object {
90+
"body": null,
91+
"count": null,
92+
"data": null,
93+
"error": Object {
94+
"code": "",
95+
"details": "",
96+
"hint": "",
97+
"message": "FetchError: The user aborted a request.",
98+
},
99+
"status": 400,
100+
"statusText": "Bad Request",
101+
}
102+
`)
103+
})

0 commit comments

Comments
 (0)