ConnectableKit is a Swift package for the Vapor framework that simplifies the response DTOs and JSON structures for API projects.
Features
Generic JSON structure: The Connectable protocol allows you to define a wrapped Vapor Content structs.
Custom HTTPStatus for every responses.
ErrorMiddleware configurations for handling Vapor’s error as ConnectableKit JSON output.
CORSMiddleware configurations for handling Vapor’s CORSMiddleware with ease.
Structure
Type
Description
Type
status
Five possible cases: information, success, redirection, failure, and error.
ResponseStatus: String
message
Optional custom message from server.
String? = nil
data
Generic associated type that represents the data that is being sent as a response. It can be any type that conforms to Vapor's Content protocol, which includes types such as String, Int, and custom structs or classes.
public static func configureCORS(_ app: Vapor.Application, configuration: Vapor.CORSMiddleware.Configuration? = nil)
Inspiration
The JSend specification, developed by Omniti Labs, outlines a consistent JSON response format for API endpoints. I found the specification to be very helpful in ensuring that API consumers can easily understand and parse the responses returned by the API.
As a result, I have heavily borrowed from the JSend specification for the response format used in this project. Specifically, I have adopted the status field to indicate the overall success or failure of the request, as well as the use of a message field to provide additional context for the response.
While I have made some modifications to the response format to suit the specific needs of this project, I am grateful for the clear and thoughtful guidance provided by the JSend specification.
License
ConnectableKit is available under the MIT license. See the LICENSE file for more info.
ConnectableKit
ConnectableKit is a Swift package for the Vapor framework that simplifies the response DTOs and JSON structures for API projects.
Features
Connectable
protocol allows you to define a wrapped VaporContent
structs.Structure
Installation
ConnectableKit can be installed using Swift Package Manager. Simply add the following line to your Package.swift file:
Usage
To use the ConnectableKit Framework,
- For Connectable
In Struct, simply conform that
Profile
isConnectable
In Response call
.DTO
for responding wrapped generic response.return
.toDTO(_ httpStatus: Vapor.HTTPStatus = .ok, status: ResponserStatus = .success, message: String? = nil) -> Responser<Self>
- For Empty Response
Connectable protocol
- For ErrorMiddleware
Error Response Example
Database Error:
AbortError:
Response
If you use
ConnectableKitErrorMiddleware
, don’t forget to use before all middleware configureations.See in Vapor’s Documentation Error Middleware
- For CORSMiddleware
func configureCORS
Inspiration
The JSend specification, developed by Omniti Labs, outlines a consistent JSON response format for API endpoints. I found the specification to be very helpful in ensuring that API consumers can easily understand and parse the responses returned by the API.
As a result, I have heavily borrowed from the JSend specification for the response format used in this project. Specifically, I have adopted the status field to indicate the overall success or failure of the request, as well as the use of a message field to provide additional context for the response.
While I have made some modifications to the response format to suit the specific needs of this project, I am grateful for the clear and thoughtful guidance provided by the JSend specification.
License
ConnectableKit is available under the MIT license. See the LICENSE file for more info.