ash_json_api_wrapper/lib/data_layer/info.ex

92 lines
2.8 KiB
Elixir
Raw Permalink Normal View History

2022-09-16 05:06:29 +12:00
defmodule AshJsonApiWrapper.DataLayer.Info do
@moduledoc "Introspection helpers for AshJsonApiWrapper.DataLayer"
alias Spark.Dsl.Extension
@spec endpoint_base(map | Ash.Resource.t()) :: String.t() | nil
2022-09-16 05:06:29 +12:00
def endpoint_base(resource) do
Extension.get_opt(resource, [:json_api_wrapper, :endpoints], :base, nil, false)
end
@spec tesla(map | Ash.Resource.t()) :: module | nil
def tesla(resource) do
Extension.get_opt(
resource,
[:json_api_wrapper],
:tesla,
AshJsonApiWrapper.DefaultTesla,
false
)
2022-09-16 05:06:29 +12:00
end
@spec base_entity_path(map | Ash.Resource.t()) :: String.t() | nil
def base_entity_path(resource) do
Extension.get_opt(resource, [:json_api_wrapper], :base_entity_path, nil, false)
end
@spec base_paginator(map | Ash.Resource.t()) :: AshJsonApiWrapper.Paginator.ref()
def base_paginator(resource) do
Extension.get_opt(resource, [:json_api_wrapper], :base_paginator, nil, false)
end
@spec field(map | Ash.Resource.t(), atom) :: AshJsonApiWrapper.Field.t() | nil
2022-09-16 05:06:29 +12:00
def field(resource, name) do
resource
|> fields()
|> Enum.find(&(&1.name == name))
end
@spec fields(map | Ash.Resource.t()) :: list(AshJsonApiWrapper.Field.t())
2022-09-16 05:06:29 +12:00
def fields(resource) do
Extension.get_entities(resource, [:json_api_wrapper, :fields])
end
@spec endpoint(map | Ash.Resource.t(), atom) :: AshJsonApiWrapper.Endpoint.t() | nil
2022-09-16 05:06:29 +12:00
def endpoint(resource, action) do
default_endpoint = AshJsonApiWrapper.Endpoint.default(resource)
2022-09-16 05:06:29 +12:00
resource
|> Extension.get_entities([:json_api_wrapper, :endpoints])
|> Enum.reject(& &1.get_for)
|> Enum.find(&Enum.member?(&1.action, action))
2022-09-16 05:06:29 +12:00
|> case do
nil ->
default_endpoint
endpoint ->
if default_endpoint.path && endpoint.path do
%{endpoint | path: default_endpoint.path <> endpoint.path}
else
%{endpoint | path: endpoint.path || default_endpoint.path}
end
end
end
@spec get_endpoint(map | Ash.Resource.t(), atom, atom) :: AshJsonApiWrapper.Endpoint.t() | nil
2022-09-16 05:06:29 +12:00
def get_endpoint(resource, action, get_for) do
default_endpoint = AshJsonApiWrapper.Endpoint.default(resource)
2022-09-16 05:06:29 +12:00
resource
|> Extension.get_entities([:json_api_wrapper, :endpoints])
|> Enum.find(fn endpoint ->
Enum.member?(endpoint.action, action) && endpoint.get_for == get_for
2022-09-16 05:06:29 +12:00
end)
|> case do
nil ->
nil
endpoint ->
if default_endpoint.path && endpoint.path do
%{endpoint | path: default_endpoint.path <> endpoint.path}
else
%{endpoint | path: endpoint.path || default_endpoint.path}
end
end
end
@spec endpoints(map | Ash.Resource.t()) :: list(AshJsonApiWrapper.Endpoint.t())
2022-09-16 05:06:29 +12:00
def endpoints(resource) do
Extension.get_entities(resource, [:json_api_wrapper, :endpoints])
end
end