This package is intended to speed up backend development using server side swift framework Vapor
Add this package to your Package.swift as dependency and to your target.
dependencies: [
.package(url: "https://github.com/KazaiMazai/vapor-rest-kit", from: "2.0.0")
],
targets: [
.target(name: "App", dependencies: [
.product(name: "VaporRestKit", package: "vapor-rest-kit")
])
]
Import in your code
import VaporRestKit
ResourceUpdateModel, ResourcePatchModel, ResourceOutputModel
protocols:
protocol ResourceUpdateModel: Content, Validatable {
associatedtype Model: Fields
func update(_: Model) -> Model
}
protocol ResourcePatchModel: Content, Validatable {
associatedtype Model: Fields
func patch(_: Model) -> Model
}
protocol ResourceOutputModel: Content {
associatedtype Model: Fields
init(_: Model)
}
Define EagerLoadQueryKeys
, SortQueryKeys
, FilterQueryKeys
if needed
Implement controller with the help of RestKit ResourceController:
struct TodoController {
func create(req: Request) throws -> EventLoopFuture<Todo.Output> {
try ResourceController<Todo.Output>().create(req: req, using: Todo.Input.self)
}
func read(req: Request) throws -> EventLoopFuture<Todo.Output> {
try ResourceController<Todo.Output>().read(req: req)
}
func update(req: Request) throws -> EventLoopFuture<Todo.Output> {
try ResourceController<Todo.Output>().update(req: req, using: Todo.Input.self)
}
func patch(req: Request) throws -> EventLoopFuture<Todo.Output> {
try ResourceController<Todo.Output>().patch(req: req, using: Todo.PatchInput.self)
}
func delete(req: Request) throws -> EventLoopFuture<Todo.Output> {
try ResourceController<Todo.Output>().delete(req: req)
}
func index(req: Request) throws -> EventLoopFuture<CursorPage<Todo.Output>> {
try ResourceController<Todo.Output>().getCursorPage(req: req)
}
}
app.group("todos") {
let controller = TodoController()
$0.on(.POST, use: controller.create)
$0.on(.GET, Todo.idPath, use: controller.read)
$0.on(.PUT, Todo.idPath, use: controller.update)
$0.on(.DELETE, Todo.idPath, use: controller.delete)
$0.on(.PATCH, Todo.idPath, use: controller.patch)
$0.on(.GET, use: controller.index)
}
This will add the following methods to your API endpoint:
HTTP Method | Route | Result |
---|---|---|
POST | /todos | Create new |
GET | /todos/:todoId | Show existing |
PUT | /todos/:todoId | Update existing (Replace) |
PATCH | /todos/:todoId | Patch exsiting (Partial update) |
DELETE | /todos/:todoId | Delete |
GET | /todos | Show paginated list |
Vapor RestKit is licensed under MIT license.
link |
Stars: 61 |
Last commit: 50 weeks ago |
This release brings support for bi-directional pagination.
Bi-directional pagination is disabled by default. It can be turned on in pagination config:
//defalut parameters are limitMax: 25, defaultLimit: 10
let cursorPaginationConfig = CursorPaginationConfig(limitMax: 25, defaultLimit: 10, allowBackwardPagination: true)
When enabled, cursor page API response metadata would contain both next and previous cursors.
{
"items": [
//your data collection here
],
"metadata": {
"next_cursor": "W3siZmllbGRLZXkiOiJhc3NldHNfW3RpY2tlcl0iLCJ2",
"prev_cursor": "dW3siZmllbGRLZXkiOiJhc3NldHNf2312RpY2tlcl0i3"
}
}
They can be used with after
and before
query parameters to request next and previous portions of collection.
Next portion:
https://api.yourservice.com/v1/stars?limit=10&after=W3siZmllbGRLZXkiOiJhc3NldHNfW3RpY2tlcl0iLCJ2YWx1Z...
Previous portion:
https://api.yourservice.com/v1/stars?limit=10&before=W3siZmllbGRLZXkiOiJhc3NldHNfW3RpY2tlcl0iLCJ2YWx1Z...
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics