|
| 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 | +// Should be able to get a PostgrestVersion 13 client from __InternalSupabase |
| 121 | +{ |
| 122 | + type DatabaseWithInternals = { |
| 123 | + __InternalSupabase: { |
| 124 | + PostgrestVersion: '13' |
| 125 | + } |
| 126 | + public: { |
| 127 | + Tables: { |
| 128 | + shops: { |
| 129 | + Row: { |
| 130 | + address: string | null |
| 131 | + id: number |
| 132 | + shop_geom: unknown | null |
| 133 | + } |
| 134 | + Insert: { |
| 135 | + address?: string | null |
| 136 | + id: number |
| 137 | + shop_geom?: unknown | null |
| 138 | + } |
| 139 | + Update: { |
| 140 | + address?: string | null |
| 141 | + id?: number |
| 142 | + shop_geom?: unknown | null |
| 143 | + } |
| 144 | + Relationships: [] |
| 145 | + } |
| 146 | + } |
| 147 | + Views: { |
| 148 | + [_ in never]: never |
| 149 | + } |
| 150 | + Functions: { |
| 151 | + [_ in never]: never |
| 152 | + } |
| 153 | + Enums: { |
| 154 | + [_ in never]: never |
| 155 | + } |
| 156 | + CompositeTypes: { |
| 157 | + [_ in never]: never |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + const pg13BrowserClient = createBrowserClient<DatabaseWithInternals>('HTTP://localhost:3000', '') |
| 162 | + const pg13ServerClient = createServerClient<DatabaseWithInternals>('HTTP://localhost:3000', '', { |
| 163 | + cookies: { getAll: () => [], setAll: () => {} }, |
| 164 | + }) |
| 165 | + const res13 = await pg13BrowserClient.from('shops').update({ id: 21 }).maxAffected(1) |
| 166 | + expectType<typeof res13.data>(null) |
| 167 | + const res13Server = await pg13ServerClient.from('shops').update({ id: 21 }).maxAffected(1) |
| 168 | + expectType<typeof res13Server.data>(null) |
| 169 | +} |
| 170 | +{ |
| 171 | + // Should default to PostgrestVersion 12 |
| 172 | + const pg12BrowserClient = createBrowserClient<Database>('HTTP://localhost:3000', '') |
| 173 | + const pg12ServerClient = createServerClient<Database>('HTTP://localhost:3000', '', { |
| 174 | + cookies: { getAll: () => [], setAll: () => {} }, |
| 175 | + }) |
| 176 | + const res12 = await pg12BrowserClient.from('shops').update({ id: 21 }).maxAffected(1) |
| 177 | + expectType<typeof res12.Error>('maxAffected method only available on postgrest 13+') |
| 178 | + const res12Server = await pg12ServerClient.from('shops').update({ id: 21 }).maxAffected(1) |
| 179 | + expectType<typeof res12Server.Error>('maxAffected method only available on postgrest 13+') |
| 180 | +} |
0 commit comments