@@ -2023,3 +2023,187 @@ test('join on 1-1 relation with nullables', async () => {
2023
2023
}
2024
2024
` )
2025
2025
} )
2026
+
2027
+ test ( 'custom fetch function' , async ( ) => {
2028
+ const customFetch = jest . fn ( ) . mockImplementation ( ( ) =>
2029
+ Promise . resolve ( {
2030
+ ok : true ,
2031
+ status : 200 ,
2032
+ statusText : 'OK' ,
2033
+ text : ( ) => Promise . resolve ( '[]' ) ,
2034
+ } )
2035
+ )
2036
+
2037
+ const postgrestWithCustomFetch = new PostgrestClient < Database > ( REST_URL , {
2038
+ fetch : customFetch ,
2039
+ } )
2040
+
2041
+ await postgrestWithCustomFetch . from ( 'users' ) . select ( )
2042
+
2043
+ expect ( customFetch ) . toHaveBeenCalledWith (
2044
+ expect . stringContaining ( REST_URL ) ,
2045
+ expect . objectContaining ( {
2046
+ method : 'GET' ,
2047
+ headers : expect . any ( Object ) ,
2048
+ } )
2049
+ )
2050
+ } )
2051
+
2052
+ test ( 'handles undefined global fetch' , async ( ) => {
2053
+ // Store original fetch
2054
+ const originalFetch = globalThis . fetch
2055
+ // Delete global fetch to simulate environments where it's undefined
2056
+ delete ( globalThis as any ) . fetch
2057
+
2058
+ try {
2059
+ const postgrestClient = new PostgrestClient < Database > ( REST_URL )
2060
+ const result = await postgrestClient . from ( 'users' ) . select ( )
2061
+ expect ( result ) . toMatchInlineSnapshot ( `
2062
+ Object {
2063
+ "count": null,
2064
+ "data": Array [
2065
+ Object {
2066
+ "age_range": "[1,2)",
2067
+ "catchphrase": "'cat' 'fat'",
2068
+ "data": null,
2069
+ "status": "ONLINE",
2070
+ "username": "supabot",
2071
+ },
2072
+ Object {
2073
+ "age_range": "[25,35)",
2074
+ "catchphrase": "'bat' 'cat'",
2075
+ "data": null,
2076
+ "status": "OFFLINE",
2077
+ "username": "kiwicopple",
2078
+ },
2079
+ Object {
2080
+ "age_range": "[25,35)",
2081
+ "catchphrase": "'bat' 'rat'",
2082
+ "data": null,
2083
+ "status": "ONLINE",
2084
+ "username": "awailas",
2085
+ },
2086
+ Object {
2087
+ "age_range": "[20,30)",
2088
+ "catchphrase": "'json' 'test'",
2089
+ "data": Object {
2090
+ "foo": Object {
2091
+ "bar": Object {
2092
+ "nested": "value",
2093
+ },
2094
+ "baz": "string value",
2095
+ },
2096
+ },
2097
+ "status": "ONLINE",
2098
+ "username": "jsonuser",
2099
+ },
2100
+ Object {
2101
+ "age_range": "[20,30)",
2102
+ "catchphrase": "'fat' 'rat'",
2103
+ "data": null,
2104
+ "status": "ONLINE",
2105
+ "username": "dragarcia",
2106
+ },
2107
+ ],
2108
+ "error": null,
2109
+ "status": 200,
2110
+ "statusText": "OK",
2111
+ }
2112
+ ` )
2113
+ // Test passes if we reach here without errors, as it means nodeFetch was used
2114
+ } finally {
2115
+ // Restore original fetch
2116
+ globalThis . fetch = originalFetch
2117
+ }
2118
+ } )
2119
+
2120
+ test ( 'handles array error with 404 status' , async ( ) => {
2121
+ // Mock the fetch response to return an array error with 404
2122
+ const customFetch = jest . fn ( ) . mockImplementation ( ( ) =>
2123
+ Promise . resolve ( {
2124
+ ok : false ,
2125
+ status : 404 ,
2126
+ statusText : 'Not Found' ,
2127
+ text : ( ) => Promise . resolve ( '[]' ) ,
2128
+ } )
2129
+ )
2130
+
2131
+ const postgrestWithCustomFetch = new PostgrestClient < Database > ( REST_URL , {
2132
+ fetch : customFetch ,
2133
+ } )
2134
+
2135
+ const res = await postgrestWithCustomFetch . from ( 'users' ) . select ( )
2136
+
2137
+ expect ( res ) . toMatchInlineSnapshot ( `
2138
+ Object {
2139
+ "count": null,
2140
+ "data": Array [],
2141
+ "error": null,
2142
+ "status": 200,
2143
+ "statusText": "OK",
2144
+ }
2145
+ ` )
2146
+ } )
2147
+
2148
+ test ( 'handles empty body with 404 status' , async ( ) => {
2149
+ // Mock the fetch response to return an empty body with 404
2150
+ const customFetch = jest . fn ( ) . mockImplementation ( ( ) =>
2151
+ Promise . resolve ( {
2152
+ ok : false ,
2153
+ status : 404 ,
2154
+ statusText : 'Not Found' ,
2155
+ text : ( ) => Promise . resolve ( '' ) ,
2156
+ } )
2157
+ )
2158
+
2159
+ const postgrestWithCustomFetch = new PostgrestClient < Database > ( REST_URL , {
2160
+ fetch : customFetch ,
2161
+ } )
2162
+
2163
+ const res = await postgrestWithCustomFetch . from ( 'users' ) . select ( )
2164
+
2165
+ expect ( res ) . toMatchInlineSnapshot ( `
2166
+ Object {
2167
+ "count": null,
2168
+ "data": null,
2169
+ "error": null,
2170
+ "status": 204,
2171
+ "statusText": "No Content",
2172
+ }
2173
+ ` )
2174
+ } )
2175
+
2176
+ test ( 'maybeSingle handles zero rows error' , async ( ) => {
2177
+ const customFetch = jest . fn ( ) . mockImplementation ( ( ) =>
2178
+ Promise . resolve ( {
2179
+ ok : false ,
2180
+ status : 406 ,
2181
+ statusText : 'Not Acceptable' ,
2182
+ text : ( ) =>
2183
+ Promise . resolve (
2184
+ JSON . stringify ( {
2185
+ code : 'PGRST116' ,
2186
+ details : '0 rows' ,
2187
+ hint : null ,
2188
+ message : 'JSON object requested, multiple (or no) rows returned' ,
2189
+ } )
2190
+ ) ,
2191
+ } )
2192
+ )
2193
+
2194
+ const postgrestWithCustomFetch = new PostgrestClient < Database > ( REST_URL , {
2195
+ fetch : customFetch ,
2196
+ } )
2197
+
2198
+ const res = await postgrestWithCustomFetch . from ( 'users' ) . select ( ) . maybeSingle ( )
2199
+
2200
+ expect ( res ) . toMatchInlineSnapshot ( `
2201
+ Object {
2202
+ "count": null,
2203
+ "data": null,
2204
+ "error": null,
2205
+ "status": 200,
2206
+ "statusText": "OK",
2207
+ }
2208
+ ` )
2209
+ } )
0 commit comments