Swiftpack.co - KazaiMazai/vapor-rest-kit as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by KazaiMazai.
KazaiMazai/vapor-rest-kit 2.1.0
Fast pace REST API framework for Vapor
⭐️ 61
🕓 1 year ago
macOS
.package(url: "https://github.com/KazaiMazai/vapor-rest-kit.git", from: "2.1.0")

Sublime's custom image

This package is intended to speed up backend development using server side swift framework 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.

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

  1. Define Input, Output structs for your Model, conforming to 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)
}

  1. Define EagerLoadQueryKeys, SortQueryKeys, FilterQueryKeys if needed

  2. 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)
    }
}

  1. Setup routes:

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:

Migration Guides

Licensing

Vapor RestKit is licensed under MIT license.

GitHub

link
Stars: 61
Last commit: 50 weeks ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

v2.1.0
1 year 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 beforequery 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