Skip to content

Commit f8edc51

Browse files
authored
Merge pull request #394 from supabase/fix/rpc-return-type
fix: rpc return type
2 parents 8f49667 + 9c1ddd0 commit f8edc51

10 files changed

+1393
-1492
lines changed

src/PostgrestBuilder.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import crossFetch from 'cross-fetch'
22

3-
import type { Fetch, PostgrestResponse } from './types'
3+
import type { Fetch, PostgrestSingleResponse } from './types'
44

55
export default abstract class PostgrestBuilder<Result>
6-
implements PromiseLike<PostgrestResponse<Result>>
6+
implements PromiseLike<PostgrestSingleResponse<Result>>
77
{
88
protected method: 'GET' | 'HEAD' | 'POST' | 'PATCH' | 'DELETE'
99
protected url: URL
@@ -45,9 +45,9 @@ export default abstract class PostgrestBuilder<Result>
4545
return this
4646
}
4747

48-
then<TResult1 = PostgrestResponse<Result>, TResult2 = never>(
48+
then<TResult1 = PostgrestSingleResponse<Result>, TResult2 = never>(
4949
onfulfilled?:
50-
| ((value: PostgrestResponse<Result>) => TResult1 | PromiseLike<TResult1>)
50+
| ((value: PostgrestSingleResponse<Result>) => TResult1 | PromiseLike<TResult1>)
5151
| undefined
5252
| null,
5353
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null

src/PostgrestQueryBuilder.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export default class PostgrestQueryBuilder<
5252
* `"estimated"`: Uses exact count for low numbers and planned count for high
5353
* numbers.
5454
*/
55-
select<Query extends string = '*', Result = GetResult<Schema, Relation['Row'], Query>>(
55+
select<Query extends string = '*', ResultOne = GetResult<Schema, Relation['Row'], Query>>(
5656
columns?: Query,
5757
{
5858
head = false,
@@ -61,7 +61,7 @@ export default class PostgrestQueryBuilder<
6161
head?: boolean
6262
count?: 'exact' | 'planned' | 'estimated'
6363
} = {}
64-
): PostgrestFilterBuilder<Schema, Relation['Row'], Result> {
64+
): PostgrestFilterBuilder<Schema, Relation['Row'], ResultOne[]> {
6565
const method = head ? 'HEAD' : 'GET'
6666
// Remove whitespaces except when quoted
6767
let quoted = false
@@ -89,7 +89,7 @@ export default class PostgrestQueryBuilder<
8989
schema: this.schema,
9090
fetch: this.fetch,
9191
allowEmpty: false,
92-
} as unknown as PostgrestBuilder<Result>)
92+
} as unknown as PostgrestBuilder<ResultOne[]>)
9393
}
9494

9595
/**

src/PostgrestTransformBuilder.ts

+19-22
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import PostgrestBuilder from './PostgrestBuilder'
22
import { GetResult } from './select-query-parser'
3-
import {
4-
GenericSchema,
5-
PostgrestMaybeSingleResponse,
6-
PostgrestResponse,
7-
PostgrestSingleResponse,
8-
} from './types'
3+
import { GenericSchema } from './types'
94

105
export default class PostgrestTransformBuilder<
116
Schema extends GenericSchema,
@@ -21,9 +16,9 @@ export default class PostgrestTransformBuilder<
2116
*
2217
* @param columns - The columns to retrieve, separated by commas
2318
*/
24-
select<Query extends string = '*', NewResult = GetResult<Schema, Row, Query>>(
19+
select<Query extends string = '*', NewResultOne = GetResult<Schema, Row, Query>>(
2520
columns?: Query
26-
): PostgrestTransformBuilder<Schema, Row, NewResult> {
21+
): PostgrestTransformBuilder<Schema, Row, NewResultOne[]> {
2722
// Remove whitespaces except when quoted
2823
let quoted = false
2924
const cleanedColumns = (columns ?? '*')
@@ -43,7 +38,7 @@ export default class PostgrestTransformBuilder<
4338
this.headers['Prefer'] += ','
4439
}
4540
this.headers['Prefer'] += 'return=representation'
46-
return this as unknown as PostgrestTransformBuilder<Schema, Row, NewResult>
41+
return this as unknown as PostgrestTransformBuilder<Schema, Row, NewResultOne[]>
4742
}
4843

4944
order<ColumnName extends string & keyof Row>(
@@ -138,9 +133,11 @@ export default class PostgrestTransformBuilder<
138133
* Query result must be one row (e.g. using `.limit(1)`), otherwise this
139134
* returns an error.
140135
*/
141-
single(): PromiseLike<PostgrestSingleResponse<Result>> {
136+
single<
137+
ResultOne = Result extends (infer ResultOne)[] ? ResultOne : never
138+
>(): PostgrestBuilder<ResultOne> {
142139
this.headers['Accept'] = 'application/vnd.pgrst.object+json'
143-
return this as PromiseLike<PostgrestSingleResponse<Result>>
140+
return this as PostgrestBuilder<ResultOne>
144141
}
145142

146143
/**
@@ -149,26 +146,28 @@ export default class PostgrestTransformBuilder<
149146
* Query result must be zero or one row (e.g. using `.limit(1)`), otherwise
150147
* this returns an error.
151148
*/
152-
maybeSingle(): PromiseLike<PostgrestMaybeSingleResponse<Result>> {
149+
maybeSingle<
150+
ResultOne = Result extends (infer ResultOne)[] ? ResultOne : never
151+
>(): PostgrestBuilder<ResultOne | null> {
153152
this.headers['Accept'] = 'application/vnd.pgrst.object+json'
154153
this.allowEmpty = true
155-
return this as PromiseLike<PostgrestMaybeSingleResponse<Result>>
154+
return this as PostgrestBuilder<ResultOne | null>
156155
}
157156

158157
/**
159158
* Return `data` as a string in CSV format.
160159
*/
161-
csv(): PromiseLike<PostgrestSingleResponse<string>> {
160+
csv(): PostgrestBuilder<string> {
162161
this.headers['Accept'] = 'text/csv'
163-
return this as PromiseLike<PostgrestSingleResponse<string>>
162+
return this as PostgrestBuilder<string>
164163
}
165164

166165
/**
167166
* Return `data` as an object in [GeoJSON](https://geojson.org) format.
168167
*/
169-
geojson(): PromiseLike<PostgrestSingleResponse<Record<string, unknown>>> {
168+
geojson(): PostgrestBuilder<Record<string, unknown>> {
170169
this.headers['Accept'] = 'application/geo+json'
171-
return this as PromiseLike<PostgrestSingleResponse<Record<string, unknown>>>
170+
return this as PostgrestBuilder<Record<string, unknown>>
172171
}
173172

174173
/**
@@ -206,9 +205,7 @@ export default class PostgrestTransformBuilder<
206205
buffers?: boolean
207206
wal?: boolean
208207
format?: 'json' | 'text'
209-
} = {}):
210-
| PromiseLike<PostgrestResponse<Record<string, unknown>>>
211-
| PromiseLike<PostgrestSingleResponse<string>> {
208+
} = {}): PostgrestBuilder<Record<string, unknown>[]> | PostgrestBuilder<string> {
212209
const options = [
213210
analyze ? 'analyze' : null,
214211
verbose ? 'verbose' : null,
@@ -223,8 +220,8 @@ export default class PostgrestTransformBuilder<
223220
this.headers[
224221
'Accept'
225222
] = `application/vnd.pgrst.plan+${format}; for="${forMediatype}"; options=${options};`
226-
if (format === 'json') return this as PromiseLike<PostgrestResponse<Record<string, unknown>>>
227-
else return this as PromiseLike<PostgrestSingleResponse<string>>
223+
if (format === 'json') return this as PostgrestBuilder<Record<string, unknown>[]>
224+
else return this as PostgrestBuilder<string>
228225
}
229226

230227
/**

src/types.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,25 @@ interface PostgrestResponseBase {
2121
status: number
2222
statusText: string
2323
}
24-
25-
interface PostgrestResponseSuccess<T> extends PostgrestResponseBase {
24+
interface PostgrestSingleResponseSuccess<T> extends PostgrestResponseBase {
2625
error: null
27-
data: T[]
26+
data: T
2827
count: number | null
2928
}
3029
interface PostgrestResponseFailure extends PostgrestResponseBase {
3130
error: PostgrestError
3231
data: null
3332
count: null
3433
}
35-
export type PostgrestResponse<T> = PostgrestResponseSuccess<T> | PostgrestResponseFailure
3634

37-
interface PostgrestSingleResponseSuccess<T> extends PostgrestResponseBase {
38-
error: null
39-
data: T
40-
count: number | null
41-
}
35+
// TODO: in v3:
36+
// - remove PostgrestResponse and PostgrestMaybeSingleResponse
37+
// - rename PostgrestSingleResponse to PostgrestResponse
4238
export type PostgrestSingleResponse<T> =
4339
| PostgrestSingleResponseSuccess<T>
4440
| PostgrestResponseFailure
4541
export type PostgrestMaybeSingleResponse<T> = PostgrestSingleResponse<T | null>
42+
export type PostgrestResponse<T> = PostgrestSingleResponse<T[]>
4643

4744
export type GenericTable = {
4845
Row: Record<string, unknown>

0 commit comments

Comments
 (0)