Skip to content
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

fix: type-asserations #525

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
28 changes: 19 additions & 9 deletions src/PostgrestBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @ts-ignore
import nodeFetch from '@supabase/node-fetch'

import type { Fetch, PostgrestSingleResponse } from './types'
import type { Fetch, PostgrestSingleResponse, PostgrestResponseSuccess } from './types'
import PostgrestError from './PostgrestError'

export default abstract class PostgrestBuilder<Result>
Expand Down Expand Up @@ -49,7 +49,7 @@ export default abstract class PostgrestBuilder<Result>

then<TResult1 = PostgrestSingleResponse<Result>, TResult2 = never>(
onfulfilled?:
| ((value: PostgrestSingleResponse<Result>) => TResult1 | PromiseLike<TResult1>)
| ((value: PostgrestResponseSuccess<Result>) => TResult1 | PromiseLike<TResult1>)
| undefined
| null,
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null
Expand All @@ -75,8 +75,8 @@ export default abstract class PostgrestBuilder<Result>
body: JSON.stringify(this.body),
signal: this.signal,
}).then(async (res) => {
let error = null
let data = null
let error: PostgrestError | null = null
let data: string | any[] | null = null
let count: number | null = null
let status = res.status
let statusText = res.statusText
Expand Down Expand Up @@ -110,9 +110,10 @@ export default abstract class PostgrestBuilder<Result>
if (data.length > 1) {
error = {
// https://github.com/PostgREST/postgrest/blob/a867d79c42419af16c18c3fb019eba8df992626f/src/PostgREST/Error.hs#L553
name: 'PostgrestSingleRowError',
code: 'PGRST116',
details: `Results contain ${data.length} rows, application/vnd.pgrst.object+json requires 1 row`,
hint: null,
hint: 'Use application/vnd.pgrst.row+json or application/json instead',
message: 'JSON object requested, multiple (or no) rows returned',
}
data = null
Expand Down Expand Up @@ -146,6 +147,10 @@ export default abstract class PostgrestBuilder<Result>
} else {
error = {
message: body,
details: '',
hint: '',
code: 'UNKNOWN',
name: 'UnknownError',
}
}
}
Expand All @@ -161,9 +166,13 @@ export default abstract class PostgrestBuilder<Result>
}
}

const postgrestResponse = {
if (error !== null) {
throw new PostgrestError(error)
}

const postgrestResponse: PostgrestResponseSuccess<Result> = {
error,
data,
data: data as Result,
count,
status,
statusText,
Expand All @@ -174,16 +183,17 @@ export default abstract class PostgrestBuilder<Result>
if (!this.shouldThrowOnError) {
res = res.catch((fetchError) => ({
error: {
name: fetchError?.name ?? 'FetchError',
message: `${fetchError?.name ?? 'FetchError'}: ${fetchError?.message}`,
details: `${fetchError?.stack ?? ''}`,
hint: '',
code: `${fetchError?.code ?? ''}`,
},
data: null,
data: null as Result | null,
count: null,
status: 0,
statusText: '',
}))
})) as Promise<PostgrestResponseSuccess<Result>>
}

return res.then(onfulfilled, onrejected)
Expand Down
6 changes: 3 additions & 3 deletions src/PostgrestError.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { PostgrestError as IPostgrestError } from './types'

export default class PostgrestError extends Error implements IPostgrestError {
details: string
hint: string
code: string
details?: string
hint?: string
code?: string

constructor(context: IPostgrestError) {
super(context.message)
Expand Down
6 changes: 3 additions & 3 deletions src/PostgrestQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export default class PostgrestQueryBuilder<
): PostgrestFilterBuilder<Schema, Relation['Row'], null, RelationName, Relationships> {
const method = 'POST'

const prefersHeaders = []
const prefersHeaders: string[] = []
if (this.headers['Prefer']) {
prefersHeaders.push(this.headers['Prefer'])
}
Expand Down Expand Up @@ -315,7 +315,7 @@ export default class PostgrestQueryBuilder<
} = {}
): PostgrestFilterBuilder<Schema, Relation['Row'], null, RelationName, Relationships> {
const method = 'PATCH'
const prefersHeaders = []
const prefersHeaders: string[] = []
if (this.headers['Prefer']) {
prefersHeaders.push(this.headers['Prefer'])
}
Expand Down Expand Up @@ -360,7 +360,7 @@ export default class PostgrestQueryBuilder<
count?: 'exact' | 'planned' | 'estimated'
} = {}): PostgrestFilterBuilder<Schema, Relation['Row'], null, RelationName, Relationships> {
const method = 'DELETE'
const prefersHeaders = []
const prefersHeaders: string[] = []
if (count) {
prefersHeaders.push(`count=${count}`)
}
Expand Down
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ export type Fetch = typeof fetch
*/
export type PostgrestError = {
message: string
details: string
hint: string
code: string
details?: string
hint?: string
code?: string
}

/**
Expand Down