Skip to content

Commit

Permalink
feat: accept URI structs as endpoint/base_url options
Browse files Browse the repository at this point in the history
  • Loading branch information
icehaunter committed Nov 21, 2024
1 parent 090fab5 commit b3c4443
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-rabbits-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@core/elixir-client": patch
---

feat: accept URI structs as endpoint/base_url options
6 changes: 3 additions & 3 deletions packages/elixir-client/lib/electric/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ defmodule Electric.Client do
)
for %{headers: %{operation: operation}, value: value} <- stream do
# The message `value` will now be a `%Foo{}` struct
# The message `value` will now be a `%Foo{}` struct
IO.inspect([{operation, value}], pretty: true, syntax_colors: IO.ANSI.syntax_colors())
end
Expand Down Expand Up @@ -159,12 +159,12 @@ defmodule Electric.Client do
@api_endpoint_path "/v1/shape"
@client_schema NimbleOptions.new!(
base_url: [
type: :string,
type: {:or, [:string, {:struct, URI}]},
doc:
"The URL of the electric server, not including the path. E.g. for local development this would be `http://localhost:3000`."
],
endpoint: [
type: :string,
type: {:or, [:string, {:struct, URI}]},
doc:
"The full URL of the shape API endpoint. E.g. for local development this would be `http://localhost:3000/v1/shape`. Use this if you need a non-standard API path."
],
Expand Down
31 changes: 31 additions & 0 deletions packages/elixir-client/test/electric/client_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ defmodule Electric.ClientTest do
Client.new(base_url: "http://localhost:3000/some/random/path")
end

test ":base_url is used as the base of the endpoint and accepts a URI" do
endpoint = URI.new!("http://localhost:3000/v1/shape")

{:ok, %Client{endpoint: ^endpoint}} =
Client.new(base_url: URI.new!("http://localhost:3000"))

endpoint = URI.new!("http://localhost:3000/proxy/v1/shape")

{:ok, %Client{endpoint: ^endpoint}} =
Client.new(base_url: URI.new!("http://localhost:3000/proxy"))

endpoint = URI.new!("http://localhost:3000/some/random/path/v1/shape")

{:ok, %Client{endpoint: ^endpoint}} =
Client.new(base_url: URI.new!("http://localhost:3000/some/random/path"))
end

test ":endpoint is used as-is" do
endpoint = URI.new!("http://localhost:3000")

Expand All @@ -70,6 +87,20 @@ defmodule Electric.ClientTest do
Client.new(endpoint: "http://localhost:3000/some/random/path")
end

test ":endpoint is used as-is and accepts a URI" do
endpoint = URI.new!("http://localhost:3000")

{:ok, %Client{endpoint: ^endpoint}} = Client.new(endpoint: endpoint)

endpoint = URI.new!("http://localhost:3000/v1/shape")

{:ok, %Client{endpoint: ^endpoint}} = Client.new(endpoint: endpoint)

endpoint = URI.new!("http://localhost:3000/some/random/path")

{:ok, %Client{endpoint: ^endpoint}} = Client.new(endpoint: endpoint)
end

test "returns an error if neither :base_url or :endpoint is given" do
assert {:error, _} = Client.new([])
end
Expand Down

0 comments on commit b3c4443

Please sign in to comment.