@supabase/postgrest-js 0.33.2 → 0.35.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/README.md +19 -15
  2. package/dist/main/PostgrestClient.d.ts +7 -2
  3. package/dist/main/PostgrestClient.d.ts.map +1 -1
  4. package/dist/main/PostgrestClient.js +13 -5
  5. package/dist/main/PostgrestClient.js.map +1 -1
  6. package/dist/main/lib/PostgrestFilterBuilder.d.ts +1 -1
  7. package/dist/main/lib/PostgrestFilterBuilder.d.ts.map +1 -1
  8. package/dist/main/lib/PostgrestFilterBuilder.js.map +1 -1
  9. package/dist/main/lib/PostgrestQueryBuilder.d.ts +3 -2
  10. package/dist/main/lib/PostgrestQueryBuilder.d.ts.map +1 -1
  11. package/dist/main/lib/PostgrestQueryBuilder.js +2 -2
  12. package/dist/main/lib/PostgrestQueryBuilder.js.map +1 -1
  13. package/dist/main/lib/PostgrestRpcBuilder.d.ts +5 -3
  14. package/dist/main/lib/PostgrestRpcBuilder.d.ts.map +1 -1
  15. package/dist/main/lib/PostgrestRpcBuilder.js +15 -5
  16. package/dist/main/lib/PostgrestRpcBuilder.js.map +1 -1
  17. package/dist/main/lib/PostgrestTransformBuilder.d.ts +4 -0
  18. package/dist/main/lib/PostgrestTransformBuilder.d.ts.map +1 -1
  19. package/dist/main/lib/PostgrestTransformBuilder.js +7 -0
  20. package/dist/main/lib/PostgrestTransformBuilder.js.map +1 -1
  21. package/dist/main/lib/constants.d.ts +4 -0
  22. package/dist/main/lib/constants.d.ts.map +1 -0
  23. package/dist/main/lib/constants.js +6 -0
  24. package/dist/main/lib/constants.js.map +1 -0
  25. package/dist/main/lib/types.d.ts +4 -1
  26. package/dist/main/lib/types.d.ts.map +1 -1
  27. package/dist/main/lib/types.js +22 -5
  28. package/dist/main/lib/types.js.map +1 -1
  29. package/dist/main/lib/version.d.ts +2 -0
  30. package/dist/main/lib/version.d.ts.map +1 -0
  31. package/dist/main/lib/version.js +6 -0
  32. package/dist/main/lib/version.js.map +1 -0
  33. package/dist/module/PostgrestClient.d.ts +7 -2
  34. package/dist/module/PostgrestClient.d.ts.map +1 -1
  35. package/dist/module/PostgrestClient.js +13 -5
  36. package/dist/module/PostgrestClient.js.map +1 -1
  37. package/dist/module/lib/PostgrestFilterBuilder.d.ts +1 -1
  38. package/dist/module/lib/PostgrestFilterBuilder.d.ts.map +1 -1
  39. package/dist/module/lib/PostgrestFilterBuilder.js.map +1 -1
  40. package/dist/module/lib/PostgrestQueryBuilder.d.ts +3 -2
  41. package/dist/module/lib/PostgrestQueryBuilder.d.ts.map +1 -1
  42. package/dist/module/lib/PostgrestQueryBuilder.js +2 -2
  43. package/dist/module/lib/PostgrestQueryBuilder.js.map +1 -1
  44. package/dist/module/lib/PostgrestRpcBuilder.d.ts +5 -3
  45. package/dist/module/lib/PostgrestRpcBuilder.d.ts.map +1 -1
  46. package/dist/module/lib/PostgrestRpcBuilder.js +15 -5
  47. package/dist/module/lib/PostgrestRpcBuilder.js.map +1 -1
  48. package/dist/module/lib/PostgrestTransformBuilder.d.ts +4 -0
  49. package/dist/module/lib/PostgrestTransformBuilder.d.ts.map +1 -1
  50. package/dist/module/lib/PostgrestTransformBuilder.js +7 -0
  51. package/dist/module/lib/PostgrestTransformBuilder.js.map +1 -1
  52. package/dist/module/lib/constants.d.ts +4 -0
  53. package/dist/module/lib/constants.d.ts.map +1 -0
  54. package/dist/module/lib/constants.js +3 -0
  55. package/dist/module/lib/constants.js.map +1 -0
  56. package/dist/module/lib/types.d.ts +4 -1
  57. package/dist/module/lib/types.d.ts.map +1 -1
  58. package/dist/module/lib/types.js +23 -6
  59. package/dist/module/lib/types.js.map +1 -1
  60. package/dist/module/lib/version.d.ts +2 -0
  61. package/dist/module/lib/version.d.ts.map +1 -0
  62. package/dist/module/lib/version.js +3 -0
  63. package/dist/module/lib/version.js.map +1 -0
  64. package/package.json +5 -2
  65. package/src/PostgrestClient.ts +20 -4
  66. package/src/lib/PostgrestFilterBuilder.ts +22 -0
  67. package/src/lib/PostgrestQueryBuilder.ts +7 -3
  68. package/src/lib/PostgrestRpcBuilder.ts +21 -5
  69. package/src/lib/PostgrestTransformBuilder.ts +8 -0
  70. package/src/lib/constants.ts +2 -0
  71. package/src/lib/types.ts +61 -40
  72. package/src/lib/version.ts +2 -0
@@ -1,12 +1,16 @@
1
- import { PostgrestBuilder } from './types'
1
+ import { Fetch, PostgrestBuilder } from './types'
2
2
  import PostgrestFilterBuilder from './PostgrestFilterBuilder'
3
3
 
4
4
  export default class PostgrestRpcBuilder<T> extends PostgrestBuilder<T> {
5
5
  constructor(
6
6
  url: string,
7
- { headers = {}, schema }: { headers?: { [key: string]: string }; schema?: string } = {}
7
+ {
8
+ headers = {},
9
+ schema,
10
+ fetch,
11
+ }: { headers?: { [key: string]: string }; schema?: string; fetch?: Fetch } = {}
8
12
  ) {
9
- super({} as PostgrestBuilder<T>)
13
+ super(({ fetch } as unknown) as PostgrestBuilder<T>)
10
14
  this.url = new URL(url)
11
15
  this.headers = { ...headers }
12
16
  this.schema = schema
@@ -18,13 +22,25 @@ export default class PostgrestRpcBuilder<T> extends PostgrestBuilder<T> {
18
22
  rpc(
19
23
  params?: object,
20
24
  {
25
+ head = false,
21
26
  count = null,
22
27
  }: {
28
+ head?: boolean
23
29
  count?: null | 'exact' | 'planned' | 'estimated'
24
30
  } = {}
25
31
  ): PostgrestFilterBuilder<T> {
26
- this.method = 'POST'
27
- this.body = params
32
+ if (head) {
33
+ this.method = 'HEAD'
34
+
35
+ if (params) {
36
+ Object.entries(params).forEach(([name, value]) => {
37
+ this.url.searchParams.append(name, value)
38
+ })
39
+ }
40
+ } else {
41
+ this.method = 'POST'
42
+ this.body = params
43
+ }
28
44
 
29
45
  if (count) {
30
46
  if (this.headers['Prefer'] !== undefined) this.headers['Prefer'] += `,count=${count}`
@@ -85,6 +85,14 @@ export default class PostgrestTransformBuilder<T> extends PostgrestBuilder<T> {
85
85
  return this
86
86
  }
87
87
 
88
+ /**
89
+ * Sets the AbortSignal for the fetch request.
90
+ */
91
+ abortSignal(signal: AbortSignal): this {
92
+ this.signal = signal
93
+ return this
94
+ }
95
+
88
96
  /**
89
97
  * Retrieves only one row from the result. Result must be one row (e.g. using
90
98
  * `limit`), otherwise this will result in an error.
@@ -0,0 +1,2 @@
1
+ import { version } from './version'
2
+ export const DEFAULT_HEADERS = { 'X-Client-Info': `postgrest-js/${version}` }
package/src/lib/types.ts CHANGED
@@ -1,4 +1,6 @@
1
- import fetch from 'cross-fetch'
1
+ import crossFetch from 'cross-fetch'
2
+
3
+ export type Fetch = typeof fetch
2
4
 
3
5
  /**
4
6
  * Error format
@@ -54,10 +56,13 @@ export abstract class PostgrestBuilder<T> implements PromiseLike<PostgrestRespon
54
56
  protected headers!: { [key: string]: string }
55
57
  protected schema?: string
56
58
  protected body?: Partial<T> | Partial<T>[]
57
- protected shouldThrowOnError?: boolean
59
+ protected shouldThrowOnError = false
60
+ protected signal?: AbortSignal
61
+ protected fetch: Fetch
58
62
 
59
63
  constructor(builder: PostgrestBuilder<T>) {
60
64
  Object.assign(this, builder)
65
+ this.fetch = builder.fetch || crossFetch
61
66
  }
62
67
 
63
68
  /**
@@ -90,53 +95,69 @@ export abstract class PostgrestBuilder<T> implements PromiseLike<PostgrestRespon
90
95
  this.headers['Content-Type'] = 'application/json'
91
96
  }
92
97
 
93
- return fetch(this.url.toString(), {
98
+ let res = this.fetch(this.url.toString(), {
94
99
  method: this.method,
95
100
  headers: this.headers,
96
101
  body: JSON.stringify(this.body),
97
- })
98
- .then(async (res) => {
99
- let error = null
100
- let data = null
101
- let count = null
102
-
103
- if (res.ok) {
104
- const isReturnMinimal = this.headers['Prefer']?.split(',').includes('return=minimal')
105
- if (this.method !== 'HEAD' && !isReturnMinimal) {
106
- const text = await res.text()
107
- if (!text) {
108
- // discard `text`
109
- } else if (this.headers['Accept'] === 'text/csv') {
110
- data = text
111
- } else {
112
- data = JSON.parse(text)
113
- }
114
- }
102
+ signal: this.signal,
103
+ }).then(async (res) => {
104
+ let error = null
105
+ let data = null
106
+ let count = null
115
107
 
116
- const countHeader = this.headers['Prefer']?.match(/count=(exact|planned|estimated)/)
117
- const contentRange = res.headers.get('content-range')?.split('/')
118
- if (countHeader && contentRange && contentRange.length > 1) {
119
- count = parseInt(contentRange[1])
108
+ if (res.ok) {
109
+ const isReturnMinimal = this.headers['Prefer']?.split(',').includes('return=minimal')
110
+ if (this.method !== 'HEAD' && !isReturnMinimal) {
111
+ const text = await res.text()
112
+ if (!text) {
113
+ // discard `text`
114
+ } else if (this.headers['Accept'] === 'text/csv') {
115
+ data = text
116
+ } else {
117
+ data = JSON.parse(text)
120
118
  }
121
- } else {
122
- error = await res.json()
119
+ }
123
120
 
124
- if (error && this.shouldThrowOnError) {
125
- throw error
126
- }
121
+ const countHeader = this.headers['Prefer']?.match(/count=(exact|planned|estimated)/)
122
+ const contentRange = res.headers.get('content-range')?.split('/')
123
+ if (countHeader && contentRange && contentRange.length > 1) {
124
+ count = parseInt(contentRange[1])
127
125
  }
126
+ } else {
127
+ error = await res.json()
128
128
 
129
- const postgrestResponse: PostgrestResponse<T> = {
130
- error,
131
- data,
132
- count,
133
- status: res.status,
134
- statusText: res.statusText,
135
- body: data,
129
+ if (error && this.shouldThrowOnError) {
130
+ throw error
136
131
  }
132
+ }
133
+
134
+ const postgrestResponse = {
135
+ error,
136
+ data,
137
+ count,
138
+ status: res.status,
139
+ statusText: res.statusText,
140
+ body: data,
141
+ }
142
+
143
+ return postgrestResponse
144
+ })
145
+ if (!this.shouldThrowOnError) {
146
+ res = res.catch((fetchError) => ({
147
+ error: {
148
+ message: `FetchError: ${fetchError.message}`,
149
+ details: '',
150
+ hint: '',
151
+ code: fetchError.code || '',
152
+ },
153
+ data: null,
154
+ body: null,
155
+ count: null,
156
+ status: 400,
157
+ statusText: 'Bad Request',
158
+ }))
159
+ }
137
160
 
138
- return postgrestResponse
139
- })
140
- .then(onfulfilled, onrejected)
161
+ return res.then(onfulfilled, onrejected)
141
162
  }
142
163
  }
@@ -0,0 +1,2 @@
1
+ // generated by genversion
2
+ export const version = '0.35.0'