Class: Supabase::Functions::Client
- Inherits:
-
Object
- Object
- Supabase::Functions::Client
- Defined in:
- lib/supabase/functions/client.rb
Overview
Sync Edge Functions client. Constructed once per project; reused across invocations.
functions = Supabase::Functions::Client.new(
base_url: "https://project.supabase.co/functions/v1",
headers: { "Authorization" => "Bearer #{key}" }
)
raw = functions.invoke("hello-world", body: { name: "Ada" })
# => Ruby returns String; encoding depends on response_type
# (:text → UTF-8, :binary → ASCII-8BIT, :json → parsed object).
data = functions.invoke("hello-world", body: { name: "Ada" }, response_type: :json)
# => parsed JSON Hash / Array / scalar.
JSON parsing is opt-in via ‘response_type: :json` — Content-Type is not consulted (deliberately different from supabase-js).
For the legacy ‘Types::Response` wrapper (data + status + headers), pass `return_response: true` — note that `Types::Response` is deprecated and will be removed in a future release.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
Instance Method Summary collapse
-
#initialize(base_url:, headers: {}, http_client: nil, verify: true, proxy: nil, timeout: nil) ⇒ Client
constructor
A new instance of Client.
-
#invoke(function_name, body: nil, headers: {}, region: nil, response_type: :text, return_response: false) ⇒ Object, Types::Response
Invoke an Edge Function by name.
-
#set_auth(token) ⇒ Object
Replace the Authorization header (e.g. when a user signs in / out).
Constructor Details
#initialize(base_url:, headers: {}, http_client: nil, verify: true, proxy: nil, timeout: nil) ⇒ Client
Returns a new instance of Client.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/supabase/functions/client.rb', line 41 def initialize(base_url:, headers: {}, http_client: nil, verify: true, proxy: nil, timeout: nil) raise ArgumentError, "base_url must be an http(s) URL" unless http_url?(base_url) @base_url = base_url.chomp("/") @verify = verify @proxy = proxy @timeout = timeout || 60 @headers = { "X-Client-Info" => "supabase-rb/functions-rb v#{VERSION}" }.merge(headers) @session = http_client || build_session end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
33 34 35 |
# File 'lib/supabase/functions/client.rb', line 33 def base_url @base_url end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
33 34 35 |
# File 'lib/supabase/functions/client.rb', line 33 def headers @headers end |
Instance Method Details
#invoke(function_name, body: nil, headers: {}, region: nil, response_type: :text, return_response: false) ⇒ Object, Types::Response
Invoke an Edge Function by name.
Always POSTs. The ‘method:` and `query:` kwargs were dropped in US-030 — they had no analogue in supabase-py and only existed to mirror the supabase-js surface (see Open Question §9.4). Region routing still appends `forceFunctionRegion` to the URL via the region branch below.
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 |
# File 'lib/supabase/functions/client.rb', line 88 def invoke(function_name, body: nil, headers: {}, region: nil, response_type: :text, return_response: false) validate_function_name!(function_name) validate_region!(region) merged_headers = @headers.merge(headers) merged_query = {} if region && region != Types::FunctionRegion::ANY merged_headers["x-region"] = region merged_query["forceFunctionRegion"] = region end encoded_body = case body when nil nil when String merged_headers["Content-Type"] ||= "text/plain" body when Hash, Array merged_headers["Content-Type"] ||= "application/json" JSON.generate(body) else raise ArgumentError, "body must be a String, Hash, Array, or nil (got #{body.class})" end response = @session.run_request( :post, "#{@base_url}/#{function_name}", encoded_body, merged_headers ) do |req| req.params.update(merged_query) unless merged_query.empty? end raise_for_status!(response) raise_for_relay!(response) data = parse_body(response, response_type) return data unless return_response Types::Response.new(data: data, status: response.status, headers: response.headers) end |
#set_auth(token) ⇒ Object
Replace the Authorization header (e.g. when a user signs in / out).
57 58 59 |
# File 'lib/supabase/functions/client.rb', line 57 def set_auth(token) @headers["Authorization"] = "Bearer #{token}" end |