Releases: bpisano/NetworkKit
1.3.0
@Path macro
The @Path property wrapper is now a macro. This lets you write your path properties as let instead of var. You can also specify a custom name for the path so your property name doesn't need to exactly match the one you provided in the method macro.
@Get("/users/:id")
struct GetUser {
- @Path
- var id: String
+ @Path("id")
+ let userId: String
}Response improvements
The Response type changed the data property to decodedData to improve error handling. You can now receive a 400 error from your server without NetworkKit throwing a decoding error instantly.
In the following example, the print function would never be executed because the Client would first try to decode the received data in order to populate the response.data property. However, servers tend to return a different response type when the request returns an error.
let response = try await client.perform(request)
print(response.statusCode) // never executed in case of a 400 status codeWith NetworkKit 1.3.0, the decoding of the response is now performed in the Response instead of the Client.
let response = try await client.perform(request)
print(response.statusCode) // At this point, the server has returned a valid response
let data: User = try response.decodedData // Will throw a decoding error in case of a type mismatch.You can also choose in which format you want to decode the received data.
let response = try await client.perform(request)
let errorData = try response.decodedData(as: HttpNotFoundError.self)1.2.0
- Added a new
@Responsethat makes response type definition more flexible and intuitive. - NetworkKit now includes DocC documentation! Read it here.
Response macro
You can use the @Response macro like this:
@Get("/users/:id")
@Response(User.self) // if you want to reuse an external type
struct GetUserRequest {
@Path
var id: String
}or
@Get("/users/:id")
struct GetUserRequest {
@Response // if you want to declare the type as part of the request
struct UserDto {
let id: String
let name: String
let email: String
}
@Path
var id: String
}When used internally, the @Response macro automatically adds Decodable conformance to your response types. This replaces the previous @Get(/users, of: [Users].self) syntax.