Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Satoshi committed Feb 27, 2024
1 parent 95180b3 commit 4208e2e
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type PocketBaseOption = {
interface PocketBaseOption {
fields?: string
expand?: string
sort?: string
Expand All @@ -18,12 +18,14 @@ type FieldsArrayToUnion<T> = T extends Array<string> ? RemoveModifier<T[number]>
type Option<TSchema extends BaseSchema, TRelation extends BaseRelation<TSchema>> = {
[Key in keyof TSchema]: {
key: Key
fields?: (keyof {
[K in keyof TSchema[Key] as TSchema[Key][K] extends string
? `${K & string}${'' | Modifier}`
: K]: unknown
})[]
expand?: Expand<TSchema, TRelation, Related<TSchema, TRelation, TSchema[Key]>>[]
fields?: Array<
keyof {
[K in keyof TSchema[Key] as TSchema[Key][K] extends string
? `${K & string}${'' | Modifier}`
: K]: unknown
}
>
expand?: Array<Expand<TSchema, TRelation, Related<TSchema, TRelation, TSchema[Key]>>>

sort?: '@random' | `${'' | '+' | '-'}${keyof TSchema[Key] & string}`
filter?: string
Expand All @@ -39,24 +41,23 @@ type Expand<
[Key in TKey]: {
key: Key
fields?: NonNullable<TRelation[Key]> extends Array<infer U> | infer U
? (keyof {
[K in keyof U as U[K] extends string
? `${K & string}${'' | Modifier}`
: K]: unknown
})[]
? Array<
{
[K in keyof U]: U[K] extends string ? `${K & string}${'' | Modifier}` : K
}[keyof U]
>
: never
expand?: NonNullable<TRelation[Key]> extends Array<infer U extends TSchema[keyof TSchema]>
? Expand<TSchema, TRelation, Related<TSchema, TRelation, U>>[]
? Array<Expand<TSchema, TRelation, Related<TSchema, TRelation, U>>>
: NonNullable<TRelation[Key]> extends infer U extends TSchema[keyof TSchema]
? Expand<TSchema, TRelation, Related<TSchema, TRelation, U>>[]
? Array<Expand<TSchema, TRelation, Related<TSchema, TRelation, U>>>
: never

// the following is correct syntax and passes tests, but fails at build step
// infer within union doesn't seem to work for whatever reason
// Build fails with this simpler syntax due to a bug in esbuild (fixed in v0.19.12 but dependency is not updated yet in tsup and vitest)
// expand?: NonNullable<TRelation[Key]> extends
// | Array<infer U extends TSchema[keyof TSchema]>
// | infer U extends TSchema[keyof TSchema]
// ? Expand<TSchema, TRelation, Related<TSchema, TRelation, U>>[]
// ? Array<Expand<TSchema, TRelation, Related<TSchema, TRelation, U>>>
// : never
}
}[TKey]
Expand Down Expand Up @@ -87,7 +88,7 @@ type ResponseType<
ProcessExpandArray<TSchema, TRelation, TOption['expand']>

// check if all items in "expand" array are optional
type AllOptional<TRelation, T extends { key: keyof TRelation; [key: PropertyKey]: unknown }[]> = {
type AllOptional<TRelation, T extends Array<{ key: keyof TRelation }>> = {
[Obj in T[number] as Obj['key']]: undefined extends TRelation[Obj['key']] ? true : false
} extends Record<PropertyKey, true>
? true
Expand Down

0 comments on commit 4208e2e

Please sign in to comment.