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
Add this to your application's shard.yml
:
dependencies:
kemal-json-api:
github: spoved/kemal-json-api.cr
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
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
- 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
See examples folder.
Currently Mongodb is the only adapter available
- Holden Omans - creator, maintainer, Crystal fan