Giter Club home page Giter Club logo

apispec's Introduction

APISpec

APISpec enables Swift Vapor developers to write API docs in Swift, then export those docs to OpenAPI 3.0 JSON.

To build a spec, you must make your types conform to these three protocols:

  • APISpecProviding - This type represents your spec root. Typically your Droplet will conform to this type.
  • APITagProviding - This type represents the various sections of your spec, known in OpenAPI as "Tags," and is typically comprised of Controllers. Conformers are responsible for vending APIOperations, which represent endpoints your API exposes. If your controller conforms to ResourceRepresentable, and your Resource Model conforms to APISchemaProviding, you can generate APIOperations from your resource. Otherwise, you must generate APIOperations manually.
  • APISchemaProviding - A main component of your API contract is your schema. API request/response data must conform to a predictable schema in order for client/server communications to be meaningful. Typically, your Models will conform to this type.

Examples

APISpecProviding

extension Droplet: APISpecProviding {
    public var title: String {
        return "Petworld API"
    }
    public var description: String {
        return "The is the API server for Petworld."
    }
    public var host: String {
        return "api.petworld.com"
    }
    public var schemes: APISchemes {
        return .https
    }
    public var contactEmail: String? {
        return "[email protected]"
    }
}

APITagProviding -

extension PetController: APITagProviding {
    var name: String {
        return "pets"
    }
    var description: String {
        return "Pets have various characteristics such as `name` and `owner`."
    }
    var operations: [APIOperation] {
        // You can generate APIOperations from a Resource where Model conforms to `APISchemaProviding`
        return makeResource().makeAPIOperations(withPath: "/pets")
    }
}

APISchemaProviding

extension Pet: APISchemaProviding {
    static var schemaName: String {
        return "Pet"
    }
    static var properties: [APIProperty] {
        return [
            APIProperty(name: "name", type: .string(example: "Sparky")),
            APIProperty(name: "age", type: .number(example: 3)),
            APIProperty(name: "owner", type: .object(type: Owner.self), isRequired: false)
        ]
    }
}

Generating the JSON

Once you have defined your spec using the various protocols, you can generate OpenAPI JSON which can be used by various tools, such as Swagger UI. Assuming your Droplet conforms to APISpecProviding, your Routes file might include something like this:

import APISpec

extension Droplet {
    func setupRoutes() throws {
        // ... setup other routes

        let tagProviders: [APITagProviding] = [
            petController
        ]
        
        self.get("/docs.json") { _ in
            return try self.generateAPISpecResponse(with: tagProviders)
        }
    }
}

apispec's People

Contributors

daltonclaybrook avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

apispec's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.