@supabase/postgrest-js 0.33.3 → 0.35.1

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 (59) hide show
  1. package/README.md +21 -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 +11 -4
  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/types.d.ts +4 -1
  22. package/dist/main/lib/types.d.ts.map +1 -1
  23. package/dist/main/lib/types.js +31 -6
  24. package/dist/main/lib/types.js.map +1 -1
  25. package/dist/main/lib/version.d.ts +1 -1
  26. package/dist/main/lib/version.js +1 -1
  27. package/dist/module/PostgrestClient.d.ts +7 -2
  28. package/dist/module/PostgrestClient.d.ts.map +1 -1
  29. package/dist/module/PostgrestClient.js +11 -4
  30. package/dist/module/PostgrestClient.js.map +1 -1
  31. package/dist/module/lib/PostgrestFilterBuilder.d.ts +1 -1
  32. package/dist/module/lib/PostgrestFilterBuilder.d.ts.map +1 -1
  33. package/dist/module/lib/PostgrestFilterBuilder.js.map +1 -1
  34. package/dist/module/lib/PostgrestQueryBuilder.d.ts +3 -2
  35. package/dist/module/lib/PostgrestQueryBuilder.d.ts.map +1 -1
  36. package/dist/module/lib/PostgrestQueryBuilder.js +2 -2
  37. package/dist/module/lib/PostgrestQueryBuilder.js.map +1 -1
  38. package/dist/module/lib/PostgrestRpcBuilder.d.ts +5 -3
  39. package/dist/module/lib/PostgrestRpcBuilder.d.ts.map +1 -1
  40. package/dist/module/lib/PostgrestRpcBuilder.js +15 -5
  41. package/dist/module/lib/PostgrestRpcBuilder.js.map +1 -1
  42. package/dist/module/lib/PostgrestTransformBuilder.d.ts +4 -0
  43. package/dist/module/lib/PostgrestTransformBuilder.d.ts.map +1 -1
  44. package/dist/module/lib/PostgrestTransformBuilder.js +7 -0
  45. package/dist/module/lib/PostgrestTransformBuilder.js.map +1 -1
  46. package/dist/module/lib/types.d.ts +4 -1
  47. package/dist/module/lib/types.d.ts.map +1 -1
  48. package/dist/module/lib/types.js +32 -7
  49. package/dist/module/lib/types.js.map +1 -1
  50. package/dist/module/lib/version.d.ts +1 -1
  51. package/dist/module/lib/version.js +1 -1
  52. package/package.json +2 -1
  53. package/src/PostgrestClient.ts +18 -3
  54. package/src/lib/PostgrestFilterBuilder.ts +22 -0
  55. package/src/lib/PostgrestQueryBuilder.ts +7 -3
  56. package/src/lib/PostgrestRpcBuilder.ts +21 -5
  57. package/src/lib/PostgrestTransformBuilder.ts +8 -0
  58. package/src/lib/types.ts +68 -39
  59. package/src/lib/version.ts +1 -1
@@ -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.
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,77 @@ 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
+ }
120
+
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])
125
+ }
126
+ } else {
127
+ const body = await res.text()
123
128
 
124
- if (error && this.shouldThrowOnError) {
125
- throw error
129
+ try {
130
+ error = JSON.parse(body)
131
+ } catch {
132
+ error = {
133
+ message: body,
126
134
  }
127
135
  }
128
136
 
129
- const postgrestResponse: PostgrestResponse<T> = {
130
- error,
131
- data,
132
- count,
133
- status: res.status,
134
- statusText: res.statusText,
135
- body: data,
137
+ if (error && this.shouldThrowOnError) {
138
+ throw error
136
139
  }
140
+ }
141
+
142
+ const postgrestResponse = {
143
+ error,
144
+ data,
145
+ count,
146
+ status: res.status,
147
+ statusText: res.statusText,
148
+ body: data,
149
+ }
150
+
151
+ return postgrestResponse
152
+ })
153
+ if (!this.shouldThrowOnError) {
154
+ res = res.catch((fetchError) => ({
155
+ error: {
156
+ message: `FetchError: ${fetchError.message}`,
157
+ details: '',
158
+ hint: '',
159
+ code: fetchError.code || '',
160
+ },
161
+ data: null,
162
+ body: null,
163
+ count: null,
164
+ status: 400,
165
+ statusText: 'Bad Request',
166
+ }))
167
+ }
137
168
 
138
- return postgrestResponse
139
- })
140
- .then(onfulfilled, onrejected)
169
+ return res.then(onfulfilled, onrejected)
141
170
  }
142
171
  }
@@ -1,2 +1,2 @@
1
1
  // generated by genversion
2
- export const version = '0.33.3'
2
+ export const version = '0.35.1'