diff --git a/src/PostgrestClient.ts b/src/PostgrestClient.ts index 84f261df..09ac92e4 100644 --- a/src/PostgrestClient.ts +++ b/src/PostgrestClient.ts @@ -39,7 +39,7 @@ 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 }) } /** diff --git a/src/lib/PostgrestTransformBuilder.ts b/src/lib/PostgrestTransformBuilder.ts index b45208b6..d4b78a9f 100644 --- a/src/lib/PostgrestTransformBuilder.ts +++ b/src/lib/PostgrestTransformBuilder.ts @@ -1,4 +1,4 @@ -import { PostgrestBuilder } from './types' +import { PostgrestBuilder, PostgrestSingleResponse } from './types' /** * Post-filters (transforms) @@ -68,8 +68,8 @@ export default class PostgrestTransformBuilder extends PostgrestBuilder { * Retrieves only one row from the result. Result must be one row (e.g. using * `limit`), otherwise this will result in an error. */ - single(): PostgrestTransformBuilder { + single(): PromiseLike> { this.headers['Accept'] = 'application/vnd.pgrst.object+json' - return this + return this as PromiseLike> } } diff --git a/src/lib/types.ts b/src/lib/types.ts index 5e5ceaed..cd5f27a7 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -19,25 +19,40 @@ interface PostgrestError { */ interface PostgrestResponse { error: PostgrestError | null - data: T | T[] | null + data: T[] | null status: number statusText: string // For backward compatibility: body === data - body: T | T[] | null + body: T[] | null } -export abstract class PostgrestBuilder implements PromiseLike { - method!: 'GET' | 'HEAD' | 'POST' | 'PATCH' | 'DELETE' - url!: URL - headers!: { [key: string]: string } - schema?: string - body?: Partial | Partial[] +export interface PostgrestSingleResponse { + error: PostgrestError | null + data: T | null + status: number + statusText: string + // For backward compatibility: body === data + body: T | null +} + +export abstract class PostgrestBuilder implements PromiseLike> { + protected method!: 'GET' | 'HEAD' | 'POST' | 'PATCH' | 'DELETE' + protected url!: URL + protected headers!: { [key: string]: string } + protected schema?: string + protected body?: Partial | Partial[] constructor(builder: PostgrestBuilder) { Object.assign(this, builder) } - then(onfulfilled?: (value: any) => any, onrejected?: (value: any) => any): Promise { + then, TResult2 = never>( + onfulfilled?: + | ((value: PostgrestResponse) => TResult1 | PromiseLike) + | undefined + | null, + onrejected?: ((reason: any) => TResult2 | PromiseLike) | undefined | null + ): PromiseLike { // https://postgrest.org/en/stable/api.html#switching-schemas if (typeof this.schema === 'undefined') { // skip @@ -64,13 +79,14 @@ export abstract class PostgrestBuilder implements PromiseLike { error = await res.json() data = null } - return { + const postgrestResponse: PostgrestResponse = { error, data, status: res.status, statusText: res.statusText, body: data, - } as PostgrestResponse + } + return postgrestResponse }) .then(onfulfilled, onrejected) } diff --git a/test/basic.test.ts b/test/basic.test.ts index 4dbc8b4f..f0b2f31f 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -15,12 +15,12 @@ test('stored procedure', async () => { test('custom headers', async () => { const postgrest = new PostgrestClient(REST_URL, { headers: { apikey: 'foo' } }) - expect(postgrest.from('users').select().headers['apikey']).toEqual('foo') + expect((postgrest.from('users').select() as any).headers['apikey']).toEqual('foo') }) test('auth', async () => { const postgrest = new PostgrestClient(REST_URL).auth('foo') - expect(postgrest.from('users').select().headers['Authorization']).toEqual('Bearer foo') + expect((postgrest.from('users').select() as any).headers['Authorization']).toEqual('Bearer foo') }) test('switch schema', async () => {