This repository has been archived by the owner on Feb 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
supabase.ex
178 lines (138 loc) · 4.22 KB
/
supabase.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
defmodule Supabase do
@moduledoc """
Elixir library for `Supabase`.
Combines Supabase Auth, provided by [gotrue-elixir](https://github.com/joshnuss/gotrue-elixir),
Supabase Database/Rest, via [postgrestex](https://github.com/J0/postgrest-ex), and Supabase
Storage, implemented in this library.
Once you configured your application
config :supabase,
base_url: <supabase-url>
api_key: <supabase-anon-key>
you can initiate the usage of the different services with
## Auth
Supabase.auth()
|> GoTrue.get_user(access_token)
## Database
Supabase.rest(access_token)
|> Postgrestex.from("profiles")
|> Postgrestex.eq("user_id", "1")
|> Postgrestex.call()
Instead of `Postgrestex.call()`, `supabase-elixir` provides a `Supabase.json/2` function
to directly parse the response to a map or struct.
Supabase.rest(access_token)
|> Postgrestex.from("profiles")
|> Postgrestex.eq("user_id", "1")
|> Supabase.json(keys: :atoms)
## Storage
Supabase.storage(access_token)
|> Supabase.Storage.from("avatars")
|> Supabase.Storage.list()
"""
@doc """
Returns a client that can be used for functions of the GoTrue library.
Example
iex> Supabase.auth() |> GoTrue.settings()
%{
"autoconfirm" => false,
"disable_signup" => false,
"external" => %{
"azure" => false,
"bitbucket" => false,
"email" => true,
"facebook" => false,
"github" => true,
"gitlab" => false,
"google" => false,
"saml" => false
},
"external_labels" => %{}
}
"""
def auth() do
{url, api_key} = connection_details()
auth(url, api_key)
end
def auth(base_url, api_key) do
base_url
|> URI.merge("/auth/v1")
|> URI.to_string()
|> GoTrue.client(api_key)
end
@doc "Entry point for the Storage API"
def storage() do
Supabase.Connection.new()
end
@doc """
Entry point for the Storage API for usage in a user context
## Example
Supabase.storage(access_token)
|> Supabase.Storage.from("avatars")
|> Supabase.Storage.download("avatar1.png")
"""
def storage(access_token) do
Supabase.Connection.new(
Application.fetch_env!(:supabase, :base_url),
Application.fetch_env!(:supabase, :api_key),
access_token
)
end
@doc """
Entrypoint for the Postgrest library
## Example
Supabase.rest(access_token)
|> Postgrestex.from("profiles")
|> Postgrestex.call()
"""
def rest() do
init()
end
def rest(access_token) when is_binary(access_token) do
init(access_token: access_token)
end
def rest(options), do: init(options)
def rest(access_token, options) do
init(Keyword.put(options, :access_token, access_token))
end
def init(options \\ []) do
{url, api_key} = connection_details()
init(url, api_key, options)
end
def init(base_url, api_key, options \\ []) do
schema = Keyword.get(options, :schema, "public")
jwt = Keyword.get(options, :access_token, api_key)
req =
Postgrestex.init(schema, URI.to_string(URI.merge(base_url, "/rest/v1")))
|> Postgrestex.auth(jwt)
update_in(req.headers, &Map.merge(&1, %{apikey: api_key}))
end
@spec json({:ok, HTTPoison.Response.t()} | HTTPoison.Response.t()) :: %{
body: map() | list(),
status: integer()
}
def json(_response, options \\ [])
def json({:ok, %HTTPoison.Response{} = response}, options) do
json(response, options)
end
def json(%HTTPoison.Response{body: body, status_code: status}, options) do
%{body: decode_body(body, options), status: status}
end
@spec json(map(), keyword()) :: %{
body: map() | list(),
status: integer()
}
def json(request, options) do
request
|> Postgrestex.call()
|> json(options)
end
defp decode_body(body, options) do
{_, body} = Jason.decode(body, options)
body
end
defp connection_details() do
{Application.fetch_env!(:supabase, :base_url), Application.fetch_env!(:supabase, :api_key)}
end
def storage_url() do
URI.merge(Application.fetch_env!(:supabase, :base_url), "/storage/v1") |> URI.to_string()
end
end