Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: accept URI structs as endpoint/base_url options #2015

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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