Skip to content

chore: Types improvements. #122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/PostgrestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default class PostgrestClient {
*/
from<T = any>(table: string): PostgrestQueryBuilder<T> {
const url = `${this.url}/${table}`
return new PostgrestQueryBuilder(url, { headers: this.headers, schema: this.schema })
return new PostgrestQueryBuilder<T>(url, { headers: this.headers, schema: this.schema })
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/lib/PostgrestTransformBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PostgrestBuilder } from './types'
import { PostgrestBuilder, PostgrestSingleResponse } from './types'

/**
* Post-filters (transforms)
Expand Down Expand Up @@ -68,8 +68,8 @@ export default class PostgrestTransformBuilder<T> extends PostgrestBuilder<T> {
* 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<T> {
single(): PromiseLike<PostgrestSingleResponse<T>> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol wtf

this.headers['Accept'] = 'application/vnd.pgrst.object+json'
return this
return this as PromiseLike<PostgrestSingleResponse<T>>
}
}
38 changes: 27 additions & 11 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,40 @@ interface PostgrestError {
*/
interface PostgrestResponse<T> {
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<T> implements PromiseLike<any> {
method!: 'GET' | 'HEAD' | 'POST' | 'PATCH' | 'DELETE'
url!: URL
headers!: { [key: string]: string }
schema?: string
body?: Partial<T> | Partial<T>[]
export interface PostgrestSingleResponse<T> {
error: PostgrestError | null
data: T | null
status: number
statusText: string
// For backward compatibility: body === data
body: T | null
}

export abstract class PostgrestBuilder<T> implements PromiseLike<PostgrestResponse<T>> {
protected method!: 'GET' | 'HEAD' | 'POST' | 'PATCH' | 'DELETE'
protected url!: URL
protected headers!: { [key: string]: string }
protected schema?: string
protected body?: Partial<T> | Partial<T>[]

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

then(onfulfilled?: (value: any) => any, onrejected?: (value: any) => any): Promise<any> {
then<TResult1 = PostgrestResponse<T>, TResult2 = never>(
onfulfilled?:
| ((value: PostgrestResponse<T>) => TResult1 | PromiseLike<TResult1>)
| undefined
| null,
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null
): PromiseLike<TResult1 | TResult2> {
// https://postgrest.org/en/stable/api.html#switching-schemas
if (typeof this.schema === 'undefined') {
// skip
Expand All @@ -64,13 +79,14 @@ export abstract class PostgrestBuilder<T> implements PromiseLike<any> {
error = await res.json()
data = null
}
return {
const postgrestResponse: PostgrestResponse<T> = {
error,
data,
status: res.status,
statusText: res.statusText,
body: data,
} as PostgrestResponse<T>
}
return postgrestResponse
})
.then(onfulfilled, onrejected)
}
Expand Down
4 changes: 2 additions & 2 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down