kemal-json-api.cr

A Kemal implementation of JSON-API spec

kemal-json-api Build Status

A Crystal library to create JSON API with Kemal.

See examples folder for mongo samples.

NOTE: this is a beta version, a lot of features and security improvements need to be implemented actually

Installation

Add this to your application's shard.yml:

dependencies:
  kemal-json-api:
    github: spoved/kemal-json-api.cr

Usage

require "mongo"
require "kemal"
require "kemal-json-api"

mongodb = KemalJsonApi::Adapter::Mongo.new("localhost", 27017, "test")

class MyModel < KemalJsonApi::Resource::Mongo
end

module WebApp
  KemalJsonApi::Router.add MyModel.new(mongodb, actions: KemalJsonApi::ALL_ACTIONS, prefix: "api", singular: "item")
  KemalJsonApi::Router.generate_routes!
  add_handler KemalJsonApi::Handler.new
  Kemal.run
end

Generated routes:

GET    /api/items
GET    /api/items/:id
POST   /api/items
PATCH  /api/items/:id
DELETE /api/items/:id

Macro

kemal-json-api also has a macro that can be used to shortcut creating routes

require "mongo"
require "kemal"
require "../src/kemal-json-api/macros/router"

mongodb = KemalJsonApi::Adapter::Mongo.new("localhost", 27017, "test")

module WebApp
  json_api_resource "trait", mongodb

  KemalJsonApi::Router.generate_routes!
  add_handler KemalJsonApi::Handler.new
  Kemal.run
end

KemalJsonApi::Resource options

  • plural (String): plural name of the model, used for routes, default = singular pluralized
  • prefix (String): prefix for all API routes, default = ""
  • singular (String): singular name of the model, default = class model name lowercase

More examples

See examples folder.

Notes

Currently Mongodb is the only adapter available

Contributors

Repository

kemal-json-api.cr

Owner
Statistic
  • 10
  • 1
  • 0
  • 0
  • 3
  • over 3 years ago
  • February 4, 2018
License

MIT License

Links
Synced at

Thu, 07 Nov 2024 01:09:17 GMT

Languages