1
1
import Foundation
2
2
@_spi ( Internal) import _Helpers
3
3
4
- let version = _Helpers. version
5
-
6
4
/// PostgREST client.
7
5
public actor PostgrestClient {
8
6
public typealias FetchHandler = @Sendable ( _ request: URLRequest ) async throws -> (
@@ -31,15 +29,15 @@ public actor PostgrestClient {
31
29
schema: String ? = nil ,
32
30
headers: [ String : String ] = [ : ] ,
33
31
fetch: @escaping FetchHandler = { try await URLSession . shared. data ( for: $0) } ,
34
- encoder: JSONEncoder ? = nil ,
35
- decoder: JSONDecoder ? = nil
32
+ encoder: JSONEncoder = PostgrestClient . Configuration . jsonEncoder ,
33
+ decoder: JSONDecoder = PostgrestClient . Configuration . jsonDecoder
36
34
) {
37
35
self . url = url
38
36
self . schema = schema
39
37
self . headers = headers
40
38
self . fetch = fetch
41
- self . encoder = encoder ?? . postgrest
42
- self . decoder = decoder ?? . postgrest
39
+ self . encoder = encoder
40
+ self . decoder = decoder
43
41
}
44
42
}
45
43
@@ -49,9 +47,7 @@ public actor PostgrestClient {
49
47
/// - Parameter configuration: The configuration for the client.
50
48
public init ( configuration: Configuration ) {
51
49
var configuration = configuration
52
- if configuration. headers [ " X-Client-Info " ] == nil {
53
- configuration. headers [ " X-Client-Info " ] = " postgrest-swift/ \( version) "
54
- }
50
+ configuration. headers. merge ( Configuration . defaultHeaders) { l, _ in l }
55
51
self . configuration = configuration
56
52
}
57
53
@@ -68,8 +64,8 @@ public actor PostgrestClient {
68
64
schema: String ? = nil ,
69
65
headers: [ String : String ] = [ : ] ,
70
66
fetch: @escaping FetchHandler = { try await URLSession . shared. data ( for: $0) } ,
71
- encoder: JSONEncoder ? = nil ,
72
- decoder: JSONDecoder ? = nil
67
+ encoder: JSONEncoder = PostgrestClient . Configuration . jsonEncoder ,
68
+ decoder: JSONDecoder = PostgrestClient . Configuration . jsonDecoder
73
69
) {
74
70
self . init (
75
71
configuration: Configuration (
@@ -139,47 +135,3 @@ public actor PostgrestClient {
139
135
try rpc ( fn, params: NoParams ( ) , count: count)
140
136
}
141
137
}
142
-
143
- private let supportedDateFormatters : [ ISO8601DateFormatter ] = [
144
- { ( ) -> ISO8601DateFormatter in
145
- let formatter = ISO8601DateFormatter ( )
146
- formatter. formatOptions = [ . withInternetDateTime, . withFractionalSeconds]
147
- return formatter
148
- } ( ) ,
149
- { ( ) -> ISO8601DateFormatter in
150
- let formatter = ISO8601DateFormatter ( )
151
- formatter. formatOptions = [ . withInternetDateTime]
152
- return formatter
153
- } ( ) ,
154
- ]
155
-
156
- extension JSONDecoder {
157
- /// The JSONDecoder instance for PostgREST responses.
158
- public static let postgrest = { ( ) -> JSONDecoder in
159
- let decoder = JSONDecoder ( )
160
- decoder. dateDecodingStrategy = . custom { decoder in
161
- let container = try decoder. singleValueContainer ( )
162
- let string = try container. decode ( String . self)
163
-
164
- for formatter in supportedDateFormatters {
165
- if let date = formatter. date ( from: string) {
166
- return date
167
- }
168
- }
169
-
170
- throw DecodingError . dataCorruptedError (
171
- in: container, debugDescription: " Invalid date format: \( string) "
172
- )
173
- }
174
- return decoder
175
- } ( )
176
- }
177
-
178
- extension JSONEncoder {
179
- /// The JSONEncoder instance for PostgREST requests.
180
- public static let postgrest = { ( ) -> JSONEncoder in
181
- let encoder = JSONEncoder ( )
182
- encoder. dateEncodingStrategy = . iso8601
183
- return encoder
184
- } ( )
185
- }
0 commit comments