Skip to content

Commit

Permalink
Merge pull request #193 from supabase/fix/csv
Browse files Browse the repository at this point in the history
fix: csv
  • Loading branch information
kiwicopple committed Jun 20, 2021
2 parents 915df5d + 494252a commit 6533232
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/lib/PostgrestTransformBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ export default class PostgrestTransformBuilder<T> extends PostgrestBuilder<T> {
/**
* Set the response type to CSV.
*/
csv(): this {
csv(): PromiseLike<PostgrestSingleResponse<string>> {
this.headers['Accept'] = 'text/csv'
return this
return this as PromiseLike<PostgrestSingleResponse<string>>
}
}
7 changes: 6 additions & 1 deletion src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,13 @@ export abstract class PostgrestBuilder<T> implements PromiseLike<PostgrestRespon
const isReturnMinimal = this.headers['Prefer']?.split(',').includes('return=minimal')
if (this.method !== 'HEAD' && !isReturnMinimal) {
const text = await res.text()
if (text && text !== '' && this.headers['Accept'] !== 'text/csv')
if (!text) {
// discard `text`
} else if (this.headers['Accept'] === 'text/csv') {
data = text
} else {
data = JSON.parse(text)
}
}

const countHeader = this.headers['Prefer']?.match(/count=(exact|planned|estimated)/)
Expand Down
2 changes: 0 additions & 2 deletions test/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -626,8 +626,6 @@ Object {
}
`;

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]`;

exports[`don't mutate PostgrestClient.headers 1`] = `null`;

exports[`embedded filters embedded eq 1`] = `
Expand Down
7 changes: 3 additions & 4 deletions test/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ test('throwOnError throws errors instead of returning them', async () => {
})

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

test('connection errors should work the same with throwOnError', async () => {
const postgrest = new PostgrestClient('http://this.url.does.not.exist')
const postgrest = new PostgrestClient('http://foo.invalid')
let isErrorCaught = false
await postgrest
.from('user')
.select()
.throwOnError()
.then(undefined, (error) => {
expect(error).toMatchSnapshot()
.then(undefined, () => {
isErrorCaught = true
})
expect(isErrorCaught).toBe(true)
Expand Down
22 changes: 22 additions & 0 deletions test/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,25 @@ test('select on stored procedure', async () => {
.select('status')
expect(res).toMatchSnapshot()
})

test('csv', async () => {
const res = await postgrest.from('users').select().csv()
expect(res).toMatchInlineSnapshot(`
Object {
"body": "username,data,age_range,status,catchphrase
supabot,,\\"[1,2)\\",ONLINE,\\"'cat' 'fat'\\"
kiwicopple,,\\"[25,35)\\",OFFLINE,\\"'bat' 'cat'\\"
awailas,,\\"[25,35)\\",ONLINE,\\"'bat' 'rat'\\"
dragarcia,,\\"[20,30)\\",ONLINE,\\"'fat' 'rat'\\"",
"count": null,
"data": "username,data,age_range,status,catchphrase
supabot,,\\"[1,2)\\",ONLINE,\\"'cat' 'fat'\\"
kiwicopple,,\\"[25,35)\\",OFFLINE,\\"'bat' 'cat'\\"
awailas,,\\"[25,35)\\",ONLINE,\\"'bat' 'rat'\\"
dragarcia,,\\"[20,30)\\",ONLINE,\\"'fat' 'rat'\\"",
"error": null,
"status": 200,
"statusText": "OK",
}
`)
})

0 comments on commit 6533232

Please sign in to comment.