|
| 1 | +import { expectType } from 'tsd' |
| 2 | +import { PostgrestBuilder, PostgrestClient } from '../src/index' |
| 3 | +import { Database } from './types' |
| 4 | +import { TypeEqual } from 'ts-expect' |
| 5 | + |
| 6 | +const REST_URL = 'http://localhost:3000' |
| 7 | +const postgrest = new PostgrestClient<Database>(REST_URL) |
| 8 | + |
| 9 | +// Test returns() with different end methods |
| 10 | +{ |
| 11 | + // Test with single() |
| 12 | + const singleResult = await postgrest |
| 13 | + .from('users') |
| 14 | + .select() |
| 15 | + .single() |
| 16 | + .returns<{ username: string }>() |
| 17 | + if (singleResult.error) { |
| 18 | + throw new Error(singleResult.error.message) |
| 19 | + } |
| 20 | + let result: typeof singleResult.data |
| 21 | + let expected: { username: string } |
| 22 | + expectType<TypeEqual<typeof result, typeof expected>>(true) |
| 23 | + |
| 24 | + // Test with maybeSingle() |
| 25 | + const maybeSingleResult = await postgrest |
| 26 | + .from('users') |
| 27 | + .select() |
| 28 | + .maybeSingle() |
| 29 | + .returns<{ username: string }>() |
| 30 | + if (maybeSingleResult.error) { |
| 31 | + throw new Error(maybeSingleResult.error.message) |
| 32 | + } |
| 33 | + let maybeSingleResultType: typeof maybeSingleResult.data |
| 34 | + let maybeSingleExpected: { username: string } |
| 35 | + expectType<TypeEqual<typeof maybeSingleResultType, typeof maybeSingleExpected>>(true) |
| 36 | + |
| 37 | + // Test array to non-array type casting error |
| 38 | + const invalidCastArray = (await postgrest.from('users').select().returns<{ username: string }>()) |
| 39 | + .data |
| 40 | + expectType<typeof invalidCastArray>({ |
| 41 | + Error: |
| 42 | + 'Type mismatch: Cannot cast array result to a single object. Use .returns<Array<YourType>> for array results or .single() to convert the result to a single object', |
| 43 | + }) |
| 44 | + |
| 45 | + // Test non-array to array type casting error |
| 46 | + const invalidCastSingle = postgrest |
| 47 | + .from('users') |
| 48 | + .select() |
| 49 | + .single() |
| 50 | + .returns<{ username: string }[]>() |
| 51 | + expectType< |
| 52 | + PostgrestBuilder< |
| 53 | + { |
| 54 | + Error: 'Type mismatch: Cannot cast single object to array type. Remove Array wrapper from return type or make sure you are not using .single() up in the calling chain' |
| 55 | + }, |
| 56 | + false |
| 57 | + > |
| 58 | + >(invalidCastSingle) |
| 59 | + |
| 60 | + // Test with csv() |
| 61 | + const csvResult = await postgrest.from('users').select().csv().returns<string>() |
| 62 | + if (csvResult.error) { |
| 63 | + throw new Error(csvResult.error.message) |
| 64 | + } |
| 65 | + let csvResultType: typeof csvResult.data |
| 66 | + let csvExpected: string |
| 67 | + expectType<TypeEqual<typeof csvResultType, typeof csvExpected>>(true) |
| 68 | + |
| 69 | + // Test with throwOnError() |
| 70 | + const throwResult = await postgrest |
| 71 | + .from('users') |
| 72 | + .select() |
| 73 | + .returns<{ username: string }[]>() |
| 74 | + .throwOnError() |
| 75 | + let throwResultType: typeof throwResult.data |
| 76 | + let throwExpected: { username: string }[] |
| 77 | + expectType<TypeEqual<typeof throwResultType, typeof throwExpected>>(true) |
| 78 | + let throwErrorType: typeof throwResult.error |
| 79 | + let throwErrorExpected: null |
| 80 | + expectType<TypeEqual<typeof throwErrorType, typeof throwErrorExpected>>(true) |
| 81 | +} |
| 82 | + |
| 83 | +// Test returns() with nested selects and relationships |
| 84 | +{ |
| 85 | + const result = await postgrest |
| 86 | + .from('users') |
| 87 | + .select('username, messages(id, content)') |
| 88 | + .single() |
| 89 | + .returns<{ |
| 90 | + username: string |
| 91 | + messages: { id: number; content: string }[] |
| 92 | + }>() |
| 93 | + if (result.error) { |
| 94 | + throw new Error(result.error.message) |
| 95 | + } |
| 96 | + let resultType: typeof result.data |
| 97 | + let expected: { |
| 98 | + username: string |
| 99 | + messages: { id: number; content: string }[] |
| 100 | + } |
| 101 | + expectType<TypeEqual<typeof resultType, typeof expected>>(true) |
| 102 | +} |
| 103 | + |
| 104 | +// Test returns() with JSON operations |
| 105 | +{ |
| 106 | + const result = await postgrest |
| 107 | + .from('users') |
| 108 | + .select('data->settings') |
| 109 | + .single() |
| 110 | + .returns<{ settings: { theme: 'light' | 'dark' } }>() |
| 111 | + if (result.error) { |
| 112 | + throw new Error(result.error.message) |
| 113 | + } |
| 114 | + let resultType: typeof result.data |
| 115 | + let expected: { settings: { theme: 'light' | 'dark' } } |
| 116 | + expectType<TypeEqual<typeof resultType, typeof expected>>(true) |
| 117 | +} |
0 commit comments