Skip to content

Commit

Permalink
add new fetcher functions
Browse files Browse the repository at this point in the history
  • Loading branch information
zoedsoupe committed Apr 15, 2024
1 parent 480bf22 commit 2aab59b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
30 changes: 29 additions & 1 deletion lib/supabase/fetcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ defmodule Supabase.Fetcher do
when url: String.t() | URI.t(),
body: binary | nil | {:stream, Enumerable.t()},
headers: list(tuple)
defp new_connection(method, url, body, headers) do
def new_connection(method, url, body, headers) do
headers = merge_headers(default_headers(), headers)
Finch.build(method, url, headers, body)
end
Expand Down Expand Up @@ -170,6 +170,34 @@ defmodule Supabase.Fetcher do
|> format_response()
end

@doc """
Simple HEAD request that format the response to a map or
retrieve the error as `String.t()`
"""
@impl true
def head(url, body, headers \\ []) do
headers = merge_headers(headers, [{"content-type", "application/json"}])

:head
|> new_connection(url, Jason.encode_to_iodata!(body), headers)
|> Finch.request(Supabase.Finch)
|> format_response()
end

@doc """
Simple PATCH request that format the response to a map or
retrieve the error as `String.t()`
"""
@impl true
def patch(url, body, headers \\ []) do
headers = merge_headers(headers, [{"content-type", "application/json"}])

:put
|> new_connection(url, Jason.encode_to_iodata!(body), headers)
|> Finch.request(Supabase.Finch)
|> format_response()
end

@doc """
Simple DELETE request that format the response to a map or retrieve
the error reason as `String.t()`.
Expand Down
2 changes: 2 additions & 0 deletions lib/supabase/fetcher_behaviour.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ defmodule Supabase.FetcherBehaviour do
when opts: [resolve_json: boolean]
@callback post(url, body, headers) :: result
@callback put(url, body, headers) :: result
@callback head(url, body, headers) :: result
@callback patch(url, body, headers) :: result
@callback delete(url, body, headers) :: result
@callback upload(method, url, Path.t(), headers) :: result
@callback stream(url, headers, keyword) :: {:ok, stream} | {:error, reason}
Expand Down

0 comments on commit 2aab59b

Please sign in to comment.