|
| 1 | +import { createServerClient, createBrowserClient } from '@supabase/ssr' |
| 2 | +import { expectType } from 'tsd' |
| 3 | + |
| 4 | +// Copied from ts-expect |
| 5 | +// https://github.com/TypeStrong/ts-expect/blob/master/src/index.ts#L23-L27 |
| 6 | +export type TypeEqual<Target, Value> = (<T>() => T extends Target ? 1 : 2) extends < |
| 7 | + T |
| 8 | +>() => T extends Value ? 1 : 2 |
| 9 | + ? true |
| 10 | + : false |
| 11 | + |
| 12 | +type Database = { |
| 13 | + public: { |
| 14 | + Tables: { |
| 15 | + shops: { |
| 16 | + Row: { |
| 17 | + address: string | null |
| 18 | + id: number |
| 19 | + shop_geom: unknown | null |
| 20 | + } |
| 21 | + Insert: { |
| 22 | + address?: string | null |
| 23 | + id: number |
| 24 | + shop_geom?: unknown | null |
| 25 | + } |
| 26 | + Update: { |
| 27 | + address?: string | null |
| 28 | + id?: number |
| 29 | + shop_geom?: unknown | null |
| 30 | + } |
| 31 | + Relationships: [] |
| 32 | + } |
| 33 | + } |
| 34 | + Views: { |
| 35 | + [_ in never]: never |
| 36 | + } |
| 37 | + Functions: { |
| 38 | + [_ in never]: never |
| 39 | + } |
| 40 | + Enums: { |
| 41 | + [_ in never]: never |
| 42 | + } |
| 43 | + CompositeTypes: { |
| 44 | + [_ in never]: never |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +{ |
| 50 | + // createBrowserClient should return a typed client |
| 51 | + const pg12Client = createBrowserClient<Database>('HTTP://localhost:3000', '') |
| 52 | + const res12 = await pg12Client.from('shops').select('*') |
| 53 | + expectType< |
| 54 | + TypeEqual< |
| 55 | + | { |
| 56 | + address: string | null |
| 57 | + id: number |
| 58 | + shop_geom: unknown | null |
| 59 | + }[] |
| 60 | + | null, |
| 61 | + typeof res12.data |
| 62 | + > |
| 63 | + >(true) |
| 64 | +} |
| 65 | + |
| 66 | +{ |
| 67 | + // createBrowserClient should infer everything to any without types provided |
| 68 | + const pg12Client = createBrowserClient('HTTP://localhost:3000', '') |
| 69 | + const res12 = await pg12Client.from('shops').select('address, id, relation(field)') |
| 70 | + expectType< |
| 71 | + TypeEqual< |
| 72 | + | { |
| 73 | + address: any |
| 74 | + id: any |
| 75 | + relation: { |
| 76 | + field: any |
| 77 | + }[] |
| 78 | + }[] |
| 79 | + | null, |
| 80 | + typeof res12.data |
| 81 | + > |
| 82 | + >(true) |
| 83 | +} |
| 84 | + |
| 85 | +{ |
| 86 | + // createServerClient should return a typed client |
| 87 | + const pg12Server = createServerClient<Database>('HTTP://localhost:3000', '') |
| 88 | + const res12 = await pg12Server.from('shops').select('*') |
| 89 | + expectType< |
| 90 | + TypeEqual< |
| 91 | + | { |
| 92 | + address: string | null |
| 93 | + id: number |
| 94 | + shop_geom: unknown | null |
| 95 | + }[] |
| 96 | + | null, |
| 97 | + typeof res12.data |
| 98 | + > |
| 99 | + >(true) |
| 100 | +} |
| 101 | + |
| 102 | +{ |
| 103 | + // createServerClient should infer everything to any without types provided |
| 104 | + const pg12Server = createServerClient('HTTP://localhost:3000', '') |
| 105 | + const res12 = await pg12Server.from('shops').select('address, id, relation(field)') |
| 106 | + expectType< |
| 107 | + TypeEqual< |
| 108 | + | { |
| 109 | + address: any |
| 110 | + id: any |
| 111 | + relation: { |
| 112 | + field: any |
| 113 | + }[] |
| 114 | + }[] |
| 115 | + | null, |
| 116 | + typeof res12.data |
| 117 | + > |
| 118 | + >(true) |
| 119 | +} |
| 120 | + |
| 121 | +// should default to postgrest 12 for untyped client |
| 122 | +{ |
| 123 | + const pg12ServerClient = createServerClient('HTTP://localhost:3000', '') |
| 124 | + const res12Server = await pg12ServerClient.from('shops').update({ id: 21 }).maxAffected(1) |
| 125 | + const pg12BrowserClient = createBrowserClient('HTTP://localhost:3000', '') |
| 126 | + const res12Browser = await pg12BrowserClient.from('shops').update({ id: 21 }).maxAffected(1) |
| 127 | + expectType<typeof res12Server.Error>('maxAffected method only available on postgrest 13+') |
| 128 | + expectType<typeof res12Browser.Error>('maxAffected method only available on postgrest 13+') |
| 129 | +} |
0 commit comments