2021-10-30 15:40:27 +13:00
|
|
|
defmodule AshJsonApiWrapper.Endpoint do
|
2022-02-12 10:29:14 +13:00
|
|
|
defstruct [
|
|
|
|
:action,
|
|
|
|
:path,
|
|
|
|
:entity_path,
|
|
|
|
:fields,
|
|
|
|
:fields_in,
|
|
|
|
:write_entity_path,
|
|
|
|
:get_for,
|
2022-09-16 05:06:29 +12:00
|
|
|
:runtime_sort?,
|
2023-01-15 19:07:41 +13:00
|
|
|
:limit_with,
|
|
|
|
:paginator
|
2022-02-12 10:29:14 +13:00
|
|
|
]
|
2021-10-30 15:40:27 +13:00
|
|
|
|
|
|
|
@type t :: %__MODULE__{}
|
|
|
|
|
|
|
|
def schema do
|
|
|
|
[
|
|
|
|
action: [
|
2023-09-26 03:16:47 +13:00
|
|
|
type: {:wrap_list, :atom},
|
2021-10-30 15:40:27 +13:00
|
|
|
required: true,
|
|
|
|
doc: "The action this path is for"
|
|
|
|
],
|
|
|
|
path: [
|
|
|
|
type: :string,
|
|
|
|
default: "/",
|
|
|
|
doc: "The path of the endpoint relative to the base, or an absolute path"
|
|
|
|
],
|
2022-09-16 05:06:29 +12:00
|
|
|
limit_with: [
|
|
|
|
type: :any,
|
|
|
|
doc: "To provide query limits as endpoint query params, use `{:param, \"param_name\"}`"
|
|
|
|
],
|
2021-10-30 15:40:27 +13:00
|
|
|
fields_in: [
|
|
|
|
type: {:in, [:body, :params]},
|
|
|
|
default: :body,
|
|
|
|
doc: "Where to place the fields when writing them."
|
|
|
|
],
|
|
|
|
write_entity_path: [
|
|
|
|
type: {:list, :string},
|
|
|
|
doc:
|
|
|
|
"The list path at which the entity should be placed in the body when creating/updating."
|
|
|
|
],
|
|
|
|
entity_path: [
|
|
|
|
type: :string,
|
|
|
|
doc: "A json path at which the entities can be read back from the response"
|
2022-02-12 10:29:14 +13:00
|
|
|
],
|
|
|
|
runtime_sort?: [
|
|
|
|
type: :boolean,
|
|
|
|
default: false,
|
|
|
|
doc:
|
|
|
|
"Whether or not this endpoint should support sorting at runtime after the data has been received."
|
2023-01-15 19:07:41 +13:00
|
|
|
],
|
|
|
|
paginator: [
|
|
|
|
type:
|
|
|
|
{:spark_behaviour, AshJsonApiWrapper.Paginator, AshJsonApiWrapper.Paginator.Builtins},
|
|
|
|
doc:
|
|
|
|
"A module implementing the `AshJSonApiWrapper.Paginator` behaviour, to allow scanning pages when reading."
|
2021-10-30 15:40:27 +13:00
|
|
|
]
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2021-11-04 11:53:51 +13:00
|
|
|
def get_schema do
|
|
|
|
Keyword.merge(
|
|
|
|
schema(),
|
|
|
|
get_for: [
|
|
|
|
type: :atom,
|
|
|
|
doc: """
|
|
|
|
Signifies that this endpoint is a get endpoint for a given field.
|
|
|
|
|
|
|
|
See the docs of `get_endpoint` for more.
|
|
|
|
"""
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2023-01-15 07:28:21 +13:00
|
|
|
def default(resource) do
|
2021-10-30 15:40:27 +13:00
|
|
|
%__MODULE__{
|
2023-01-15 07:28:21 +13:00
|
|
|
path: AshJsonApiWrapper.DataLayer.Info.endpoint_base(resource),
|
|
|
|
entity_path: AshJsonApiWrapper.DataLayer.Info.base_entity_path(resource),
|
2023-01-15 19:07:41 +13:00
|
|
|
paginator: AshJsonApiWrapper.DataLayer.Info.base_paginator(resource),
|
|
|
|
fields: AshJsonApiWrapper.DataLayer.Info.fields(resource),
|
2021-10-30 15:40:27 +13:00
|
|
|
fields_in: :body
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|