Skip to content

Improved typings #279

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
Jun 13, 2022
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
12 changes: 7 additions & 5 deletions src/PostgrestBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ import crossFetch from 'cross-fetch'

import type { Fetch, PostgrestResponse } from './types'

export default abstract class PostgrestBuilder<T> implements PromiseLike<PostgrestResponse<T>> {
export default abstract class PostgrestBuilder<Result>
implements PromiseLike<PostgrestResponse<Result>>
{
protected method: 'GET' | 'HEAD' | 'POST' | 'PATCH' | 'DELETE'
protected url: URL
protected headers: Record<string, string>
protected schema?: string
protected body?: Partial<T> | Partial<T>[]
protected body?: unknown
protected shouldThrowOnError: boolean
protected signal?: AbortSignal
protected fetch: Fetch
protected allowEmpty: boolean

constructor(builder: PostgrestBuilder<T>) {
constructor(builder: PostgrestBuilder<Result>) {
this.method = builder.method
this.url = builder.url
this.headers = builder.headers
Expand Down Expand Up @@ -45,9 +47,9 @@ export default abstract class PostgrestBuilder<T> implements PromiseLike<Postgre
return this
}

then<TResult1 = PostgrestResponse<T>, TResult2 = never>(
then<TResult1 = PostgrestResponse<Result>, TResult2 = never>(
onfulfilled?:
| ((value: PostgrestResponse<T>) => TResult1 | PromiseLike<TResult1>)
| ((value: PostgrestResponse<Result>) => TResult1 | PromiseLike<TResult1>)
| undefined
| null,
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null
Expand Down
51 changes: 36 additions & 15 deletions src/PostgrestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@ import PostgrestQueryBuilder from './PostgrestQueryBuilder'
import PostgrestFilterBuilder from './PostgrestFilterBuilder'
import PostgrestBuilder from './PostgrestBuilder'
import { DEFAULT_HEADERS } from './constants'
import { Fetch } from './types'
import { Fetch, GenericSchema } from './types'

export default class PostgrestClient {
export default class PostgrestClient<
Database = any,
SchemaName extends string & keyof Database = 'public' extends keyof Database
? 'public'
: string & keyof Database,
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
? Database[SchemaName]
: any
> {
url: string
headers: Record<string, string>
schema?: string
schema?: SchemaName
fetch?: Fetch
shouldThrowOnError: boolean

Expand All @@ -27,7 +35,7 @@ export default class PostgrestClient {
throwOnError = false,
}: {
headers?: Record<string, string>
schema?: string
schema?: SchemaName
fetch?: Fetch
throwOnError?: boolean
} = {}
Expand Down Expand Up @@ -56,9 +64,12 @@ export default class PostgrestClient {
*
* @param table The table name to operate on.
*/
from<T = any>(table: string): PostgrestQueryBuilder<T> {
const url = `${this.url}/${table}`
return new PostgrestQueryBuilder<T>(url, {
from<
TableName extends string & keyof Schema['Tables'],
Table extends Schema['Tables'][TableName]
>(table: TableName): PostgrestQueryBuilder<Table> {
const url = new URL(`${this.url}/${table}`)
return new PostgrestQueryBuilder<Table>(url, {
headers: { ...this.headers },
schema: this.schema,
fetch: this.fetch,
Expand All @@ -70,33 +81,43 @@ export default class PostgrestClient {
* Perform a function call.
*
* @param fn The function name to call.
* @param params The parameters to pass to the function call.
* @param args The parameters to pass to the function call.
* @param options Named parameters.
* @param options.head When set to true, no data will be returned.
* @param options.count Count algorithm to use to count rows in a table.
*/
rpc<T = any>(
fn: string,
params: Record<string, unknown> = {},
rpc<
FunctionName extends string & keyof Schema['Functions'],
Function_ extends Schema['Functions'][FunctionName]
>(
fn: FunctionName,
args: Function_['Args'] = {},
{
head = false,
count,
}: {
head?: boolean
count?: 'exact' | 'planned' | 'estimated'
} = {}
): PostgrestFilterBuilder<T> {
): PostgrestFilterBuilder<
Function_['Returns'] extends any[]
? Function_['Returns'][number] extends Record<string, unknown>
? Function_['Returns'][number]
: never
: never,
Function_['Returns']
> {
let method: 'HEAD' | 'POST'
const url = new URL(`${this.url}/rpc/${fn}`)
let body: unknown | undefined
if (head) {
method = 'HEAD'
Object.entries(params).forEach(([name, value]) => {
Object.entries(args).forEach(([name, value]) => {
url.searchParams.append(name, `${value}`)
})
} else {
method = 'POST'
body = params
body = args
}

const headers = { ...this.headers }
Expand All @@ -113,6 +134,6 @@ export default class PostgrestClient {
fetch: this.fetch,
shouldThrowOnError: this.shouldThrowOnError,
allowEmpty: false,
} as unknown as PostgrestBuilder<T>)
} as unknown as PostgrestBuilder<Function_['Returns']>)
}
}
Loading