Skip to content

KazaiMazai/vapor-rest-kit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

317 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sublime's custom image

This package is intended to speed up backend development using server side swift framework Vapor

Continuous Integration

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) async throws -> Todo.Output {
        try ResourceController<Todo.Output>().create(req: req, using: Todo.Input.self)
    }

    func read(req: Request) async throws -> Todo.Output {
        try ResourceController<Todo.Output>().read(req: req)
    }

    func update(req: Request) async throws -> Todo.Output {
        try ResourceController<Todo.Output>().update(req: req, using: Todo.Input.self)
    }

    func patch(req: Request) async throws -> Todo.Output {
        try ResourceController<Todo.Output>().patch(req: req, using: Todo.PatchInput.self)
    }

    func delete(req: Request) async throws -> Todo.Output {
        try ResourceController<Todo.Output>().delete(req: req)
    }

    func index(req: Request) async throws -> 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.

About

Fast pace REST API library for Vapor

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages