PhoenixKit.API.RequestContext (phoenix_kit v2.63.0)

Copy Markdown View Source

What the API knows about the request besides who sent it: the language and currency to answer in, the hosts this install serves, and the base URL for absolute links.

Core's defaults are deliberately plain:

  • :language is the install's primary language (PhoenixKit.Utils.Multilang.primary_language/0);
  • :currency is nil, because core does not depend on Billing. The e-commerce module fills it from its base currency;
  • :hosts is the request host;
  • :base_url is scheme://host[:port] of the request.

A host app overrides any of :language, :currency and :hosts per request (a multi-domain install maps the domain to a language and a currency):

config :phoenix_kit, :api_request_context, {MyApp.ApiContext, :resolve}

The function takes the Plug.Conn and returns a map with any of those three keys. A key of the wrong type is ignored. A function that raises is logged and the defaults are used, so a bug in the hook cannot take the API down. The log names the hook, the exception module and the shape of a bad result, never their values: the hook gets the whole connection, headers included.

Summary

Functions

Resolves the context of conn.

Types

t()

@type t() :: %{
  language: String.t(),
  currency: String.t() | nil,
  hosts: [String.t()],
  base_url: String.t()
}

Functions

resolve(conn)

@spec resolve(Plug.Conn.t()) :: t()

Resolves the context of conn.