Skip to content

Commit 5ddecaf

Browse files
authored
Merge pull request #193 from supabase/fix/csv
fix: csv
2 parents 691ae74 + 8d724e8 commit 5ddecaf

File tree

5 files changed

+33
-9
lines changed

5 files changed

+33
-9
lines changed

packages/core/postgrest-js/src/lib/PostgrestTransformBuilder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ export default class PostgrestTransformBuilder<T> extends PostgrestBuilder<T> {
123123
/**
124124
* Set the response type to CSV.
125125
*/
126-
csv(): this {
126+
csv(): PromiseLike<PostgrestSingleResponse<string>> {
127127
this.headers['Accept'] = 'text/csv'
128-
return this
128+
return this as PromiseLike<PostgrestSingleResponse<string>>
129129
}
130130
}

packages/core/postgrest-js/src/lib/types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,13 @@ export abstract class PostgrestBuilder<T> implements PromiseLike<PostgrestRespon
104104
const isReturnMinimal = this.headers['Prefer']?.split(',').includes('return=minimal')
105105
if (this.method !== 'HEAD' && !isReturnMinimal) {
106106
const text = await res.text()
107-
if (text && text !== '' && this.headers['Accept'] !== 'text/csv')
107+
if (!text) {
108+
// discard `text`
109+
} else if (this.headers['Accept'] === 'text/csv') {
110+
data = text
111+
} else {
108112
data = JSON.parse(text)
113+
}
109114
}
110115

111116
const countHeader = this.headers['Prefer']?.match(/count=(exact|planned|estimated)/)

packages/core/postgrest-js/test/__snapshots__/index.test.ts.snap

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,8 +626,6 @@ Object {
626626
}
627627
`;
628628

629-
exports[`connection errors should work the same with throwOnError 1`] = `[FetchError: request to http://this.url.does.not.exist/user?select=* failed, reason: getaddrinfo ENOTFOUND this.url.does.not.exist]`;
630-
631629
exports[`don't mutate PostgrestClient.headers 1`] = `null`;
632630

633631
exports[`embedded filters embedded eq 1`] = `

packages/core/postgrest-js/test/basic.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ test('throwOnError throws errors instead of returning them', async () => {
109109
})
110110

111111
test('connection error', async () => {
112-
const postgrest = new PostgrestClient('http://this.url.does.not.exist')
112+
const postgrest = new PostgrestClient('http://foo.invalid')
113113
let isErrorCaught = false
114114
await postgrest
115115
.from('user')
@@ -121,14 +121,13 @@ test('connection error', async () => {
121121
})
122122

123123
test('connection errors should work the same with throwOnError', async () => {
124-
const postgrest = new PostgrestClient('http://this.url.does.not.exist')
124+
const postgrest = new PostgrestClient('http://foo.invalid')
125125
let isErrorCaught = false
126126
await postgrest
127127
.from('user')
128128
.select()
129129
.throwOnError()
130-
.then(undefined, (error) => {
131-
expect(error).toMatchSnapshot()
130+
.then(undefined, () => {
132131
isErrorCaught = true
133132
})
134133
expect(isErrorCaught).toBe(true)

packages/core/postgrest-js/test/transforms.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,25 @@ test('select on stored procedure', async () => {
5656
.select('status')
5757
expect(res).toMatchSnapshot()
5858
})
59+
60+
test('csv', async () => {
61+
const res = await postgrest.from('users').select().csv()
62+
expect(res).toMatchInlineSnapshot(`
63+
Object {
64+
"body": "username,data,age_range,status,catchphrase
65+
supabot,,\\"[1,2)\\",ONLINE,\\"'cat' 'fat'\\"
66+
kiwicopple,,\\"[25,35)\\",OFFLINE,\\"'bat' 'cat'\\"
67+
awailas,,\\"[25,35)\\",ONLINE,\\"'bat' 'rat'\\"
68+
dragarcia,,\\"[20,30)\\",ONLINE,\\"'fat' 'rat'\\"",
69+
"count": null,
70+
"data": "username,data,age_range,status,catchphrase
71+
supabot,,\\"[1,2)\\",ONLINE,\\"'cat' 'fat'\\"
72+
kiwicopple,,\\"[25,35)\\",OFFLINE,\\"'bat' 'cat'\\"
73+
awailas,,\\"[25,35)\\",ONLINE,\\"'bat' 'rat'\\"
74+
dragarcia,,\\"[20,30)\\",ONLINE,\\"'fat' 'rat'\\"",
75+
"error": null,
76+
"status": 200,
77+
"statusText": "OK",
78+
}
79+
`)
80+
})

0 commit comments

Comments
 (0)