Skip to content

v1.4.0

Latest
Compare
Choose a tag to compare
@carson-katri carson-katri released this 18 May 14:52
· 5 commits to main since this release
6266d88

What's New

  • Improved Combine support (#34)

Request is now a Publisher. This lets you use Combine operators on your Request:

Request {
  Url("https://jsonplaceholder.typicode.com/todos")
}
.map(\.data)
.decode([Todo].self, decoder: JSONDecoder())
.sink(receiveCompletion: { ... }, receiveValue: { ... })

There are also properties for objectPublisher, stringPublisher, and jsonPublisher. Using AnyRequest, that previous example could be rewritten as:

AnyRequest<[Todo]> {
  Url("https://jsonplaceholder.typicode.com/todos")
}
.objectPublisher
.sink(receiveCompletion: { ... }, receiveValue: { ... })
  • buildEither support in RequestBuilder (#39)
  • Concatenate Urls with + (#39) - Url("https://baseurl.com") + Url("/path")
  • Improved internals of Request (#42)

If you would like to add your own parameters to be used in a Request, you can simply implement the RequestParam protocol:

struct MyCustomParam: RequestParam {
  func buildParam(_ request: inout URLRequest) {}
}

You can also conform to SessionParam to modify the URLSessionConfiguration:

struct MyCustomSessionParam: SessionParam {
  func buildConfiguration(_ configuration: URLSessionConfiguration) {}
}
  • multipart/form-data support (#43, #51)

Use the Form parameter in your Request body to send form data. You can send Form.Data for file content, and key-value pairs with Form.Value.

Request {
  Url("http://example.com/send")
  Method(.post)
  Form {
    Form.Data(data, named: "image.txt", withType: .text)
    Form.Value(key: "email", "[email protected]")
  }
}
  • @Requested property wrapper and reimplementation of RequestView (#46, iOS 14+, macOS 11+)

This allows a Request to be specified in a property wrapper, and it will automatically update your View's body when the Request finishes. The projectedValue contains the status of the Request as a RequestStatus enum:

struct TodoList: View {
  @Requested private var allTodos = AnyRequest<[Todo]> {
    Url("https://jsonplaceholder.typicode.com/todos")
  }

  var body: some View {
    switch $allTodos {
    case .loading: ProgressView()
    case let .failure(error): Text(error.localizedDescription)
    case let .success(todos):
      List(todos) { todo in
        Label(todo.title, systemImage: "checkmark.circle\(todo.completed ? ".fill" : "")")
      }
    }
  }
}

RequestView was reimplemented for iOS 14+ and macOS 11+ to provide more reliability. It also provides a new initializer that uses a RequestStatus enum in the body:

RequestView(AnyRequest<[Todo]> {
  Url("https://jsonplaceholder.typicode.com/todos")
}) { result in
  switch result {
  case .loading: ProgressView()
  case let .failure(error): Text(error.localizedDescription)
  case let .success(todos):
    List(todos) { todo in
      Label(todo.title, systemImage: "checkmark.circle\(todo.completed ? ".fill" : "")")
    }
  }
}

Merged PRs

  • Check if filename contains "." before splitting it bug (#53) by @brennobemoura
  • Added Form.Value to send key and value content in a form data (#51) by @brennobemoura
  • Update MediaType.swift (#47) by @rain2540
  • @Requested property wrapper and reimplementation of RequestView (#46) by @carson-katri
  • Support MultipartFormData (#43) by @brennobemoura
  • Simplify RequestParam internal API (#42) by @brennobemoura
  • Solves #36 #37 (#39) by @brennobemoura
  • Improve Combine Support (#34) by @carson-katri