11import Foundation
22@_spi ( Internal) import _Helpers
33
4- let version = _Helpers. version
5-
64/// PostgREST client.
75public actor PostgrestClient {
86 public typealias FetchHandler = @Sendable ( _ request: URLRequest ) async throws -> (
@@ -31,15 +29,15 @@ public actor PostgrestClient {
3129 schema: String ? = nil ,
3230 headers: [ String : String ] = [ : ] ,
3331 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
3634 ) {
3735 self . url = url
3836 self . schema = schema
3937 self . headers = headers
4038 self . fetch = fetch
41- self . encoder = encoder ?? . postgrest
42- self . decoder = decoder ?? . postgrest
39+ self . encoder = encoder
40+ self . decoder = decoder
4341 }
4442 }
4543
@@ -49,9 +47,7 @@ public actor PostgrestClient {
4947 /// - Parameter configuration: The configuration for the client.
5048 public init ( configuration: Configuration ) {
5149 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 }
5551 self . configuration = configuration
5652 }
5753
@@ -68,8 +64,8 @@ public actor PostgrestClient {
6864 schema: String ? = nil ,
6965 headers: [ String : String ] = [ : ] ,
7066 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
7369 ) {
7470 self . init (
7571 configuration: Configuration (
@@ -139,47 +135,3 @@ public actor PostgrestClient {
139135 try rpc ( fn, params: NoParams ( ) , count: count)
140136 }
141137}
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