diff --git a/Sources/PostgREST/PostgrestBuilder.swift b/Sources/PostgREST/PostgrestBuilder.swift index 1e6b733..649bc6f 100644 --- a/Sources/PostgREST/PostgrestBuilder.swift +++ b/Sources/PostgREST/PostgrestBuilder.swift @@ -30,7 +30,7 @@ public class PostgrestBuilder { } let session = URLSession.shared - let dataTask = session.dataTask(with: request, completionHandler: { [unowned self] (data, response, error) -> Void in + let dataTask = session.dataTask(with: request, completionHandler: { (data, response, error) -> Void in if let error = error { completion(.failure(error)) return @@ -47,8 +47,8 @@ public class PostgrestBuilder { } do { - try validate(data: data, response: response) - let response = try parse(data: data, response: response) + try Self.validate(data: data, response: response) + let response = try Self.parse(data: data, response: response, request: request) completion(.success(response)) } catch { completion(.failure(error)) @@ -58,7 +58,7 @@ public class PostgrestBuilder { dataTask.resume() } - private func validate(data: Data, response: HTTPURLResponse) throws { + private static func validate(data: Data, response: HTTPURLResponse) throws { if 200 ..< 300 ~= response.statusCode { return } @@ -70,11 +70,11 @@ public class PostgrestBuilder { throw PostgrestError(from: json) ?? PostgrestError(message: "failed to get error") } - private func parse(data: Data, response: HTTPURLResponse) throws -> PostgrestResponse { + private static func parse(data: Data, response: HTTPURLResponse, request: URLRequest) throws -> PostgrestResponse { var body: Any = data var count: Int? - if method == "HEAD" { + if request.httpMethod == "HEAD" { if let accept = response.allHeaderFields["Accept"] as? String, accept == "text/csv" { body = data } else { @@ -138,6 +138,9 @@ public class PostgrestBuilder { var request = URLRequest(url: url) request.httpMethod = method request.allHTTPHeaderFields = headers + if let body = body { + request.httpBody = try JSONSerialization.data(withJSONObject: body, options: []) + } return request } diff --git a/Sources/PostgREST/PostgrestError.swift b/Sources/PostgREST/PostgrestError.swift index a0505a2..7656b84 100644 --- a/Sources/PostgREST/PostgrestError.swift +++ b/Sources/PostgREST/PostgrestError.swift @@ -7,16 +7,13 @@ public struct PostgrestError: Error { public var message: String init?(from dictionary: [String: Any]) { - guard let details = dictionary["details"] as? String, - let hint = dictionary["hint"] as? String, - let code = dictionary["code"] as? String, - let message = dictionary["message"] as? String - else { + guard let message = dictionary["message"] as? String else { return nil } - self.details = details - self.hint = hint - self.code = code + + self.details = dictionary["details"] as? String + self.hint = dictionary["hint"] as? String + self.code = dictionary["code"] as? String self.message = message } diff --git a/Tests/PostgRESTTests/BuildURLRequestTests.swift b/Tests/PostgRESTTests/BuildURLRequestTests.swift index 8c3d801..be77337 100644 --- a/Tests/PostgRESTTests/BuildURLRequestTests.swift +++ b/Tests/PostgRESTTests/BuildURLRequestTests.swift @@ -22,6 +22,11 @@ final class BuildURLRequestTests: XCTestCase { .select() .like(column: "email", value: "%@supabase.co") .buildURLRequest(head: false, count: nil) + }, + TestCase(name: "insert new user") { client in + try client.form("users") + .insert(values: ["email": "johndoe@supabase.io"]) + .buildURLRequest(head: false, count: nil) } ] diff --git a/Tests/PostgRESTTests/__Snapshots__/BuildURLRequestTests/testBuildURLRequest.insert-new-user.txt b/Tests/PostgRESTTests/__Snapshots__/BuildURLRequestTests/testBuildURLRequest.insert-new-user.txt new file mode 100644 index 0000000..ed68a51 --- /dev/null +++ b/Tests/PostgRESTTests/__Snapshots__/BuildURLRequestTests/testBuildURLRequest.insert-new-user.txt @@ -0,0 +1,5 @@ +curl \ + --request POST \ + --header "Prefer: return=representation" \ + --data "{\"email\":\"johndoe@supabase.io\"}" \ + "https://example.supabase.co/users" \ No newline at end of file