Helper functions and utilities for Tink.
Provides common utility functions used throughout the library:
- URL building and encoding
- Query parameter handling
- Response parsing
- Date/time formatting
- Money/decimal handling
Examples
# Build URL with query params
url = Tink.Helpers.build_url("/api/v1/accounts", page_size: 10, page_token: "abc")
#=> "/api/v1/accounts?page_size=10&page_token=abc"
# Parse money amount
amount = Tink.Helpers.parse_money(%{"value" => "123.45", "currencyCode" => "USD"})
#=> %{amount: #Decimal<123.45>, currency: "USD"}
Summary
Functions
Builds a query string from parameters.
Builds a URL with query parameters.
Safely decodes JSON.
Safely encodes body as JSON.
Formats a date for the Tink API (ISO 8601).
Safely gets a nested value from a map.
Merges pagination params into options.
Parses a date from ISO 8601 string.
Parses a decimal value safely.
Parses a money amount from Tink API response.
Redacts sensitive information from logs.
Converts snake_case to camelCase.
Validates required parameters are present.
Functions
Builds a query string from parameters.
Handles nil values, lists, and nested maps appropriately.
Examples
iex> Tink.Helpers.build_query_string(foo: "bar", baz: 123)
"foo=bar&baz=123"
iex> Tink.Helpers.build_query_string(foo: nil, bar: "baz")
"bar=baz"
Builds a URL with query parameters.
Examples
iex> Tink.Helpers.build_url("/api/v1/accounts", page_size: 10)
"/api/v1/accounts?page_size=10"
iex> Tink.Helpers.build_url("/api/v1/accounts", [])
"/api/v1/accounts"
@spec decode_json(String.t()) :: {:ok, term()} | {:error, Jason.DecodeError.t()}
Safely decodes JSON.
Examples
iex> Tink.Helpers.decode_json(~s({"foo":"bar"}))
{:ok, %{"foo" => "bar"}}
iex> Tink.Helpers.decode_json("invalid")
{:error, %Jason.DecodeError{}}
Safely encodes body as JSON.
Examples
iex> Tink.Helpers.encode_json(%{foo: "bar"})
{:ok, ~s({"foo":"bar"})}
iex> Tink.Helpers.encode_json(nil)
{:ok, nil}
Formats a date for the Tink API (ISO 8601).
Examples
iex> date = ~D[2024-01-15]
iex> Tink.Helpers.format_date(date)
"2024-01-15"
iex> Tink.Helpers.format_date("2024-01-15")
"2024-01-15"
Safely gets a nested value from a map.
Examples
iex> map = %{"user" => %{"name" => "John"}}
iex> Tink.Helpers.get_in_safe(map, ["user", "name"])
"John"
iex> Tink.Helpers.get_in_safe(map, ["user", "age"])
nil
Merges pagination params into options.
Examples
iex> Tink.Helpers.merge_pagination_params([page_size: 10], "token123")
[page_size: 10, page_token: "token123"]
Parses a date from ISO 8601 string.
Examples
iex> Tink.Helpers.parse_date("2024-01-15")
{:ok, ~D[2024-01-15]}
iex> Tink.Helpers.parse_date("invalid")
{:error, :invalid_date}
Parses a decimal value safely.
Examples
iex> Tink.Helpers.parse_decimal("123.45")
#Decimal<123.45>
iex> Tink.Helpers.parse_decimal(123.45)
#Decimal<123.45>
iex> Tink.Helpers.parse_decimal(nil)
#Decimal<0>
Parses a money amount from Tink API response.
Examples
iex> Tink.Helpers.parse_money(%{"value" => "123.45", "currencyCode" => "USD"})
%{amount: Decimal.new("123.45"), currency: "USD"}
iex> Tink.Helpers.parse_money(%{"value" => 123.45, "currencyCode" => "EUR"})
%{amount: Decimal.new("123.45"), currency: "EUR"}
Redacts sensitive information from logs.
Examples
iex> Tink.Helpers.redact_sensitive(%{access_token: "secret", data: "public"})
%{access_token: "[REDACTED]", data: "public"}
Converts snake_case to camelCase.
Examples
iex> Tink.Helpers.to_camel_case("page_size")
"pageSize"
iex> Tink.Helpers.to_camel_case("page_token")
"pageToken"
Validates required parameters are present.
Examples
iex> params = %{user_id: "123", scope: "accounts:read"}
iex> Tink.Helpers.validate_required(params, [:user_id, :scope])
:ok
iex> params = %{user_id: "123"}
iex> Tink.Helpers.validate_required(params, [:user_id, :scope])
{:error, "Missing required parameter: scope"}