Skip to content

Commit 3a61484

Browse files
committed
Rename the unwrap API to throwOnError instead
The `unwrap` moniker was a bit of a misnomer, since it didn't actually unwrap the result, and this should make things a bit more clear. This was discussed in a bit more detail in #188.
1 parent 6209345 commit 3a61484

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

src/lib/types.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,20 @@ export abstract class PostgrestBuilder<T> implements PromiseLike<PostgrestRespon
5353
protected headers!: { [key: string]: string }
5454
protected schema?: string
5555
protected body?: Partial<T> | Partial<T>[]
56-
protected unwrapError?: boolean
56+
protected shouldThrowOnError?: boolean
5757

5858
constructor(builder: PostgrestBuilder<T>) {
5959
Object.assign(this, builder)
6060
}
6161

6262
/**
63-
* If there's an error with the query, unwrap will reject the promise by
63+
* If there's an error with the query, throwOnError will reject the promise by
6464
* throwing the error instead of returning it as part of a successful response.
6565
*
6666
* {@link https://github.com/supabase/supabase-js/issues/92}
6767
*/
68-
unwrap(): PostgrestBuilder<T> {
69-
this.unwrapError = true
68+
throwOnError(): PostgrestBuilder<T> {
69+
this.shouldThrowOnError = true
7070
return this
7171
}
7272

@@ -115,7 +115,7 @@ export abstract class PostgrestBuilder<T> implements PromiseLike<PostgrestRespon
115115
} else {
116116
error = await res.json()
117117

118-
if (error && this.unwrapError) {
118+
if (error && this.shouldThrowOnError) {
119119
throw error
120120
}
121121
}

test/__snapshots__/index.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ Object {
626626
}
627627
`;
628628

629-
exports[`connection error when unwrapping too 1`] = `[FetchError: request to http://this.url.does.not.exist/user?select=* failed, reason: getaddrinfo ENOTFOUND this.url.does.not.exist]`;
629+
exports[`connection errors should work the same with throwOnError 1`] = `[FetchError: request to http://this.url.does.not.exist/user?select=* failed, reason: getaddrinfo ENOTFOUND this.url.does.not.exist]`;
630630

631631
exports[`don't mutate PostgrestClient.headers 1`] = `null`;
632632

@@ -2254,7 +2254,7 @@ Object {
22542254
}
22552255
`;
22562256
2257-
exports[`unwrap throws errors instead of returning them 1`] = `
2257+
exports[`throwOnError throws errors instead of returning them 1`] = `
22582258
Object {
22592259
"code": "42P01",
22602260
"details": null,

test/basic.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ test('missing table', async () => {
9595
expect(res).toMatchSnapshot()
9696
})
9797

98-
test('unwrap throws errors instead of returning them', async () => {
98+
test('throwOnError throws errors instead of returning them', async () => {
9999
let isErrorCaught = false
100100

101101
try {
102-
await postgrest.from('missing_table').select().unwrap()
102+
await postgrest.from('missing_table').select().throwOnError()
103103
} catch (error) {
104104
expect(error).toMatchSnapshot()
105105
isErrorCaught = true
@@ -120,14 +120,14 @@ test('connection error', async () => {
120120
expect(isErrorCaught).toBe(true)
121121
})
122122

123-
test('connection error when unwrapping too', async () => {
123+
test('connection errors should work the same with throwOnError', async () => {
124124
const postgrest = new PostgrestClient('http://this.url.does.not.exist')
125125
let isErrorCaught = false
126126
await postgrest
127127
.from('user')
128128
.select()
129-
.unwrap()
130-
.then(undefined, error => {
129+
.throwOnError()
130+
.then(undefined, (error) => {
131131
expect(error).toMatchSnapshot()
132132
isErrorCaught = true
133133
})

0 commit comments

Comments
 (0)