-
-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor the library with additional exports
- Loading branch information
1 parent
c96ccfb
commit 38d604d
Showing
7 changed files
with
309 additions
and
301 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import PostgrestQueryBuilder from './lib/PostgrestQueryBuilder' | ||
import { PostgrestBuilder } from './lib/types' | ||
|
||
export default class PostgrestClient { | ||
url: string | ||
headers: { [key: string]: string } | ||
schema?: string | ||
|
||
/** | ||
* Creates a PostgREST client. | ||
* | ||
* @param url URL of the PostgREST endpoint. | ||
* @param headers Custom headers. | ||
* @param schema Postgres schema to switch to. | ||
*/ | ||
constructor( | ||
url: string, | ||
{ headers = {}, schema }: { headers?: { [key: string]: string }; schema?: string } = {} | ||
) { | ||
this.url = url | ||
this.headers = headers | ||
this.schema = schema | ||
} | ||
|
||
/** | ||
* Authenticates the request with JWT. | ||
* | ||
* @param token The JWT token to use. | ||
*/ | ||
auth(token: string): this { | ||
this.headers['Authorization'] = `Bearer ${token}` | ||
return this | ||
} | ||
|
||
/** | ||
* Perform a table operation. | ||
* | ||
* @param table The table name to operate on. | ||
*/ | ||
from<T = any>(table: string): PostgrestQueryBuilder<T> { | ||
const url = `${this.url}/${table}` | ||
return new PostgrestQueryBuilder(url, { headers: this.headers, schema: this.schema }) | ||
} | ||
|
||
/** | ||
* Perform a stored procedure call. | ||
* | ||
* @param fn The function name to call. | ||
* @param params The parameters to pass to the function call. | ||
*/ | ||
rpc<T = any>(fn: string, params?: object): PostgrestBuilder<T> { | ||
const url = `${this.url}/rpc/${fn}` | ||
return new PostgrestQueryBuilder<T>(url, { headers: this.headers, schema: this.schema }).rpc( | ||
params | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,6 @@ | ||
import { PostgrestBuilder, PostgrestQueryBuilder } from './builder' | ||
import PostgrestClient from './PostgrestClient' | ||
import PostgrestFilterBuilder from './lib/PostgrestFilterBuilder' | ||
import PostgrestQueryBuilder from './lib/PostgrestQueryBuilder' | ||
import { PostgrestBuilder } from './lib/types' | ||
|
||
export class PostgrestClient { | ||
url: string | ||
headers: { [key: string]: string } | ||
schema?: string | ||
|
||
/** | ||
* Creates a PostgREST client. | ||
* | ||
* @param url URL of the PostgREST endpoint. | ||
* @param headers Custom headers. | ||
* @param schema Postgres schema to switch to. | ||
*/ | ||
constructor( | ||
url: string, | ||
{ headers = {}, schema }: { headers?: { [key: string]: string }; schema?: string } = {} | ||
) { | ||
this.url = url | ||
this.headers = headers | ||
this.schema = schema | ||
} | ||
|
||
/** | ||
* Authenticates the request with JWT. | ||
* | ||
* @param token The JWT token to use. | ||
*/ | ||
auth(token: string): this { | ||
this.headers['Authorization'] = `Bearer ${token}` | ||
return this | ||
} | ||
|
||
/** | ||
* Perform a table operation. | ||
* | ||
* @param table The table name to operate on. | ||
*/ | ||
from<T = any>(table: string): PostgrestQueryBuilder<T> { | ||
const url = `${this.url}/${table}` | ||
return new PostgrestQueryBuilder(url, { headers: this.headers, schema: this.schema }) | ||
} | ||
|
||
/** | ||
* Perform a stored procedure call. | ||
* | ||
* @param fn The function name to call. | ||
* @param params The parameters to pass to the function call. | ||
*/ | ||
rpc<T = any>(fn: string, params?: object): PostgrestBuilder<T> { | ||
const url = `${this.url}/rpc/${fn}` | ||
return new PostgrestQueryBuilder<T>(url, { headers: this.headers, schema: this.schema }).rpc( | ||
params | ||
) | ||
} | ||
} | ||
export { PostgrestClient, PostgrestFilterBuilder, PostgrestQueryBuilder, PostgrestBuilder } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.