Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kazaimazai/vapor-rest-kit
Fast pace REST API library for Vapor
https://github.com/kazaimazai/vapor-rest-kit
crud pagination rest-api server-side-swift
Last synced: about 9 hours ago
JSON representation
Fast pace REST API library for Vapor
- Host: GitHub
- URL: https://github.com/kazaimazai/vapor-rest-kit
- Owner: KazaiMazai
- License: mit
- Created: 2020-05-13T17:05:34.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-06-23T18:10:38.000Z (7 months ago)
- Last Synced: 2024-06-24T18:55:57.134Z (7 months ago)
- Topics: crud, pagination, rest-api, server-side-swift
- Language: Swift
- Homepage:
- Size: 566 KB
- Stars: 61
- Watchers: 7
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
This package is intended to speed up backend development using server side swift framework [Vapor](https://github.com/vapor/vapor)
## Features
- CRUDs with Resource and Nested Resource Controllers
- Parent-Child and Siblings relations for Nested Resource Controllers
- Nested Resource Controllers for Authenticatable Resource
- Filter query
- Sorting query
- Eager loading query
- Fluent Model convenience extensions
- Cursor Pagination
____________## Installation
Add this package to your Package.swift as dependency and to your target.
```swift
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
```swift
import VaporRestKit
```____________
1. Define Input, Output structs for your Model, conforming to ```ResourceUpdateModel, ResourcePatchModel, ResourceOutputModel``` protocols:
```swift
protocol ResourceUpdateModel: Content, Validatable {
associatedtype Model: Fieldsfunc update(_: Model) -> Model
}protocol ResourcePatchModel: Content, Validatable {
associatedtype Model: Fieldsfunc patch(_: Model) -> Model
}protocol ResourceOutputModel: Content {
associatedtype Model: Fieldsinit(_: Model)
}```
2. Define `EagerLoadQueryKeys`, `SortQueryKeys`, `FilterQueryKeys` if needed
3. Implement controller with the help of RestKit ResourceController:
```swift
struct TodoController {
func create(req: Request) async throws -> Todo.Output {
try ResourceController().create(req: req, using: Todo.Input.self)
}func read(req: Request) async throws -> Todo.Output {
try ResourceController().read(req: req)
}func update(req: Request) async throws -> Todo.Output {
try ResourceController().update(req: req, using: Todo.Input.self)
}func patch(req: Request) async throws -> Todo.Output {
try ResourceController().patch(req: req, using: Todo.PatchInput.self)
}func delete(req: Request) async throws -> Todo.Output {
try ResourceController().delete(req: req)
}func index(req: Request) async throws -> CursorPage {
try ResourceController().getCursorPage(req: req)
}
}```
4. Setup routes:
```swift
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___________
## Check out the Docs for more details:- [Basics](Docs/Basics.md)
- [Fluent Model Extensions](Docs/Fluent-Model-Convenience-Extensions.md)
- [CRUD for Resource Models](Docs/CRUD-for-Resource-Models.md)
- [CRUD for Related Resource Models](Docs/CRUD-Related-Resource-Models.md)
- [CRUD for Relations](Docs/CRUD-for-Relations.md)
- [Controller Middlewares](Docs/Controller-Middlewares.md)
- [Filtering](Docs/Filtering.md)
- [Sorting](Docs/Sorting.md)
- [Eager Loading](Docs/EaagerLoading.md)
- [QueryModifier](Docs/QueryModifier.md)
- [Pagination](Docs/Pagination.md)## Migration Guides
- [Migration from v1.x to v2.0](Docs/Vapor-RestKit-Migration-guide-from-v1.0-to-v2.0.md)
# LicensingVapor RestKit is licensed under MIT license.