Skip to content

Commit

Permalink
Merge pull request #553 from zacharyhansen/fix/support-self-hosted-po…
Browse files Browse the repository at this point in the history
…stgrest

fix: merge url splitting to unified method that handles self hosted s…
  • Loading branch information
psteinroe authored Jan 31, 2025
2 parents 6d84a63 + 2466274 commit a7a8a19
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/slimy-mails-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@supabase-cache-helpers/postgrest-core": minor
---

Merge url parsing for rpc and table name and provide consistant parsing for self hosted servers
20 changes: 20 additions & 0 deletions packages/postgrest-core/src/lib/get-table-from-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Parses a url and returns the table name the url is interacting with.
*
* For mutations, the .split('?') goes unused.
*
* @param url The url we are pulling the table name from
* @returns Table name
*/
export const getTableFromUrl = (url: string): string => {
// Split the url
const split = url.toString().split('/');
// Pop the last part of the path off and remove any params if they exist
const table = split.pop()?.split('?').shift() as string;
// Pop an additional position to check for rpc
const maybeRpc = split.pop() as string;
// Rejoin the result to include rpc otherwise just table name
return [maybeRpc === 'rpc' ? maybeRpc : null, table]
.filter(Boolean)
.join('/');
};
3 changes: 2 additions & 1 deletion packages/postgrest-core/src/lib/get-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import {
GenericSchema,
GenericTable,
} from '@supabase/postgrest-js/dist/cjs/types';
import { getTableFromUrl } from './get-table-from-url';

export const getTable = (
query:
| PostgrestBuilder<any>
| PostgrestQueryBuilder<GenericSchema, GenericTable>,
): string => (query as { url: URL })['url'].pathname.split('/').pop() as string;
): string => getTableFromUrl((query as { url: URL })['url'].pathname);
5 changes: 2 additions & 3 deletions packages/postgrest-core/src/postgrest-parser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { PostgrestBuilder } from '@supabase/postgrest-js';

import { encodeObject } from './lib/encode-object';
import { getTableFromUrl } from './lib/get-table-from-url';
import { isObject } from './lib/is-object';
import type { OrderDefinition } from './lib/query-types';
import { sortSearchParams } from './lib/sort-search-param';
Expand Down Expand Up @@ -39,9 +40,7 @@ export class PostgrestParser<Result> extends PostgrestQueryParser {

this.queryKey = sortSearchParams(this._url.searchParams).toString();

this.table = (this._url.toString().split('/rest/v1/').pop() as string)
.split('?')
.shift() as string;
this.table = getTableFromUrl(this._url.toString());

if (this._body) {
this.bodyKey = encodeObject(this._body as Record<string, unknown>);
Expand Down

0 comments on commit a7a8a19

Please sign in to comment.