Skip to content

Commit

Permalink
fix: unwrap error within arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
avallete authored and soedirgo committed Oct 22, 2024
1 parent b4e587a commit e31b1ff
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
16 changes: 10 additions & 6 deletions src/select-query-parser/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@ import {
UnionToArray,
} from './types'

export type UnwrapErrorMessages<T> = T extends SelectQueryError<infer M>
? M
: T extends Record<string, unknown>
? { [K in keyof T]: UnwrapErrorMessages<T[K]> }
: T

export type SelectQueryError<Message extends string> = { error: true } & Message

type RequireHintingSelectQueryError<
DistantName extends string,
RelationName extends string
> = SelectQueryError<`Could not embed because more than one relationship was found for '${DistantName}' and '${RelationName}' you need to hint the column with ${DistantName}!<columnName> ?`>

export type UnwrapErrorMessages<T> = T extends SelectQueryError<infer M>
? M
: T extends SelectQueryError<infer M>[]
? M[]
: T extends (infer U)[]
? UnwrapErrorMessages<U>[]
: T extends Record<string, unknown>
? { [K in keyof T]: UnwrapErrorMessages<T[K]> }
: T

export type GetFieldNodeResultName<Field extends Ast.FieldNode> = Field['alias'] extends string
? Field['alias']
: Field['aggregateFunction'] extends AggregateFunctions
Expand Down
5 changes: 2 additions & 3 deletions test/select-query-parser/select.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { expectType } from 'tsd'
import { TypeEqual } from 'ts-expect'
import { Json } from '../../src/select-query-parser/types'
import { SelectQueryError } from '../../src/select-query-parser/utils'
import { Prettify } from '../../src/types'
import { Database } from '../types'
import { selectQueries } from '../relationships'
Expand Down Expand Up @@ -271,7 +270,7 @@ type Schema = Database['public']
id: number
second_user: string
third_wheel: string | null
first_user: SelectQueryError<"Could not embed because more than one relationship was found for 'users' and 'best_friends' you need to hint the column with users!<columnName> ?">
first_user: "Could not embed because more than one relationship was found for 'users' and 'best_friends' you need to hint the column with users!<columnName> ?"
}>
second_friend_of: Array<Database['public']['Tables']['best_friends']['Row']>
third_wheel_of: Array<Database['public']['Tables']['best_friends']['Row']>
Expand Down Expand Up @@ -440,7 +439,7 @@ type Schema = Database['public']
let result: Exclude<typeof data, null>
let expected: {
username: string
messages: SelectQueryError<"column 'sum' does not exist on 'messages'.">[]
messages: "column 'sum' does not exist on 'messages'."[]
}
expectType<TypeEqual<typeof result, typeof expected>>(true)
}
Expand Down

0 comments on commit e31b1ff

Please sign in to comment.