Unit.Resource behaviour (Unit v1.0.0)

Copy Markdown View Source

Behaviour and helpers for Unit JSON:API resource structs.

Every resource module implements cast/1 to convert raw JSON:API data maps into typed structs, and uses from_response/2 / from_response_list/2 to wrap API results.

Summary

Callbacks

Cast a raw JSON:API data map into a struct.

Functions

Parse an ISO 8601 date string into a Date.

Parse an ISO 8601 datetime string into a DateTime.

Wrap an API response containing a single data object.

Wrap an API response containing a list of data objects.

Extract a relationship ID from a JSON:API relationships map.

Extract a relationship type from a JSON:API relationships map.

Callbacks

cast(map)

@callback cast(map()) :: struct()

Cast a raw JSON:API data map into a struct.

Functions

cast_date(str)

@spec cast_date(String.t() | nil) :: Date.t() | nil

Parse an ISO 8601 date string into a Date.

cast_datetime(str)

@spec cast_datetime(String.t() | nil) :: DateTime.t() | nil

Parse an ISO 8601 datetime string into a DateTime.

from_response(mod, arg2)

@spec from_response(module(), map() | nil) :: {:ok, struct() | nil}

Wrap an API response containing a single data object.

from_response_list(mod, arg2)

@spec from_response_list(module(), map() | nil) :: {:ok, [struct()], map()}

Wrap an API response containing a list of data objects.

rel_id(relationships, key)

@spec rel_id(map(), String.t()) :: String.t() | nil

Extract a relationship ID from a JSON:API relationships map.

rel_type(relationships, key)

@spec rel_type(map(), String.t()) :: String.t() | nil

Extract a relationship type from a JSON:API relationships map.