Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/core/postgrest-js/src/PostgrestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,10 +397,10 @@ export default class PostgrestClient<
let method: 'HEAD' | 'GET' | 'POST'
const url = new URL(`${this.url}/rpc/${fn}`)
let body: unknown | undefined
// objects/arrays-of-objects can't be serialized to URL params, use POST + return=minimal instead
// objects/arrays-of-objects can't be serialized to URL params, fall back to POST
const _isObject = (v: unknown): boolean =>
v !== null && typeof v === 'object' && (!Array.isArray(v) || v.some(_isObject))
const _hasObjectArg = head && Object.values(args as object).some(_isObject)
const _hasObjectArg = (head || get) && Object.values(args as object).some(_isObject)
if (_hasObjectArg) {
method = 'POST'
body = args
Expand All @@ -421,7 +421,7 @@ export default class PostgrestClient<
}

const headers = new Headers(this.headers)
if (_hasObjectArg) {
if (head && _hasObjectArg) {
headers.set('Prefer', count ? `count=${count},return=minimal` : 'return=minimal')
} else if (count) {
headers.set('Prefer', `count=${count}`)
Expand Down
23 changes: 23 additions & 0 deletions packages/core/postgrest-js/test/fetch-errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,29 @@ describe('Fetch error handling', () => {
expect(options.headers['prefer']).toContain('return=minimal')
})

test('rpc with get: true and object args uses POST and returns the body', async () => {
const mockFetch = jest.fn().mockResolvedValue({
ok: true,
status: 200,
statusText: 'OK',
headers: new Headers(),
json: async () => null,
text: async () => '',
})

const postgrest = new PostgrestClient<Database>('https://example.com', {
fetch: mockFetch as any,
})

await postgrest.rpc('my_func' as any, { obj_arg: { nested: 'value' } }, { get: true })

const [url, options] = mockFetch.mock.calls[0]
expect(options.method).toBe('POST')
expect(JSON.parse(options.body)).toEqual({ obj_arg: { nested: 'value' } })
expect(url).not.toContain('object')
expect(options.headers['prefer'] ?? '').not.toContain('return=minimal')
})

test('PostgrestError serializes message with JSON.stringify', () => {
const err = new PostgrestError({
message: 'RLS denied',
Expand Down
Loading