Skip to content

Commit de46d28

Browse files
Merge branch 'master' into docs
2 parents febef56 + c179942 commit de46d28

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

Sources/PostgREST/PostgrestBuilder.swift

+9-6
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class PostgrestBuilder {
3535
}
3636

3737
let session = URLSession.shared
38-
let dataTask = session.dataTask(with: request, completionHandler: { [unowned self] (data, response, error) -> Void in
38+
let dataTask = session.dataTask(with: request, completionHandler: { (data, response, error) -> Void in
3939
if let error = error {
4040
completion(.failure(error))
4141
return
@@ -52,8 +52,8 @@ public class PostgrestBuilder {
5252
}
5353

5454
do {
55-
try validate(data: data, response: response)
56-
let response = try parse(data: data, response: response)
55+
try Self.validate(data: data, response: response)
56+
let response = try Self.parse(data: data, response: response, request: request)
5757
completion(.success(response))
5858
} catch {
5959
completion(.failure(error))
@@ -68,7 +68,7 @@ public class PostgrestBuilder {
6868
/// - data: `Data` received from the server.
6969
/// - response: `HTTPURLResponse` received from the server.
7070
/// - Throws: Throws `PostgrestError` if invalid JSON object.
71-
private func validate(data: Data, response: HTTPURLResponse) throws {
71+
private static func validate(data: Data, response: HTTPURLResponse) throws {
7272
if 200 ..< 300 ~= response.statusCode {
7373
return
7474
}
@@ -86,11 +86,11 @@ public class PostgrestBuilder {
8686
/// - response: Response received from the server
8787
/// - Throws: Throws an `Error` if invalid JSON.
8888
/// - Returns: Returns a `PostgrestResponse`
89-
private func parse(data: Data, response: HTTPURLResponse) throws -> PostgrestResponse {
89+
private static func parse(data: Data, response: HTTPURLResponse, request: URLRequest) throws -> PostgrestResponse {
9090
var body: Any = data
9191
var count: Int?
9292

93-
if method == "HEAD" {
93+
if request.httpMethod == "HEAD" {
9494
if let accept = response.allHeaderFields["Accept"] as? String, accept == "text/csv" {
9595
body = data
9696
} else {
@@ -160,6 +160,9 @@ public class PostgrestBuilder {
160160
var request = URLRequest(url: url)
161161
request.httpMethod = method
162162
request.allHTTPHeaderFields = headers
163+
if let body = body {
164+
request.httpBody = try JSONSerialization.data(withJSONObject: body, options: [])
165+
}
163166
return request
164167
}
165168

Sources/PostgREST/PostgrestError.swift

+5-8
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@ public struct PostgrestError: Error {
77
public var message: String
88

99
init?(from dictionary: [String: Any]) {
10-
guard let details = dictionary["details"] as? String,
11-
let hint = dictionary["hint"] as? String,
12-
let code = dictionary["code"] as? String,
13-
let message = dictionary["message"] as? String
14-
else {
10+
guard let message = dictionary["message"] as? String else {
1511
return nil
1612
}
17-
self.details = details
18-
self.hint = hint
19-
self.code = code
13+
14+
self.details = dictionary["details"] as? String
15+
self.hint = dictionary["hint"] as? String
16+
self.code = dictionary["code"] as? String
2017
self.message = message
2118
}
2219

Tests/PostgRESTTests/BuildURLRequestTests.swift

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ final class BuildURLRequestTests: XCTestCase {
2222
.select()
2323
.like(column: "email", value: "%@supabase.co")
2424
.buildURLRequest(head: false, count: nil)
25+
},
26+
TestCase(name: "insert new user") { client in
27+
try client.form("users")
28+
.insert(values: ["email": "[email protected]"])
29+
.buildURLRequest(head: false, count: nil)
2530
}
2631
]
2732

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
curl \
2+
--request POST \
3+
--header "Prefer: return=representation" \
4+
--data "{\"email\":\"[email protected]\"}" \
5+
"https://example.supabase.co/users"

0 commit comments

Comments
 (0)