在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:beam-community/jsonapi开源软件地址:https://github.com/beam-community/jsonapi开源编程语言:Elixir 100.0%开源软件介绍:JSONAPI ElixirA project that will render your data models into JSONAPI Documents and parse/verify JSONAPI query strings. JSONAPI SupportThis library implements version 1.1 of the JSON:API spec.
DocumentationBadgesHow to use with PhoenixInstallationAdd the following line to your defp deps do [
...
{:jsonapi, "~> x.x.x"}
...
]
UsageSimply add defmodule MyApp.PostView do
use JSONAPI.View, type: "posts"
def fields do
[:text, :body, :excerpt]
end
def excerpt(post, _conn) do
String.slice(post.body, 0..5)
end
def meta(data, _conn) do
# this will add meta to each record
# To add meta as a top level property, pass as argument to render function (shown below)
%{meta_text: "meta_#{data[:text]}"}
end
def relationships do
# The post's author will be included by default
[author: {MyApp.UserView, :include},
comments: MyApp.CommentView]
end
end You can now call If you'd like to use this without Phoenix simply use the Parsing and validating a JSONAPI RequestIn your controller you may add plug JSONAPI.QueryParser,
filter: ~w(name),
sort: ~w(name title inserted_at),
view: PostView This will add a The config holds the values parsed into things that are easy to pass into an Ecto
query, for example This sort of behavior is consistent for includes. The Camelized or Dasherized FieldsJSONAPI has recommended in the past the use of dashes ( Transforming fields requires two steps:
Spec EnforcementWe include a set of Plugs to make enforcing the JSONAPI spec for requests easy. To add spec enforcement to your application, add plug JSONAPI.EnsureSpec Under-the-hood
Configurationconfig :jsonapi,
host: "www.someotherhost.com",
scheme: "https",
namespace: "/api",
field_transformation: :underscore,
remove_links: false,
json_library: Jason,
paginator: nil
PaginationPagination links can be generated by overriding the ...
def pagination_links(data, conn, page, options) do
%{first: nil, last: nil, prev: nil, next: nil}
end
... Alternatively you can define generic pagination strategies by implementing a module
conforming to the defmodule PageBasedPaginator do
@moduledoc """
Page based pagination strategy
"""
@behaviour JSONAPI.Paginator
@impl true
def paginate(data, view, conn, page, options) do
number =
page
|> Map.get("page", "0")
|> String.to_integer()
size =
page
|> Map.get("size", "0")
|> String.to_integer()
total_pages = Keyword.get(options, :total_pages, 0)
%{
first: view.url_for_pagination(data, conn, Map.put(page, "page", "1")),
last: view.url_for_pagination(data, conn, Map.put(page, "page", total_pages)),
next: next_link(data, view, conn, number, size, total_pages),
prev: previous_link(data, view, conn, number, size)
}
end
defp next_link(data, view, conn, page, size, total_pages)
when page < total_pages,
do: view.url_for_pagination(data, conn, %{size: size, page: page + 1})
defp next_link(_data, _view, _conn, _page, _size, _total_pages),
do: nil
defp previous_link(data, view, conn, page, size)
when page > 1,
do: view.url_for_pagination(data, conn, %{size: size, page: page - 1})
defp previous_link(_data, _view, _conn, _page, _size),
do: nil
end and configuring it as the global pagination logic in your config :jsonapi, :paginator, PageBasedPaginator or as the view pagination logic when using use JSONAPI.View, paginator: PageBasedPaginator Links can be generated using the Actual pagination is expected to be handled in your application logic and is outside the scope of this library. Other
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论