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}" }
)
response = functions.invoke("hello-world", body: { name: "Ada" })
response.data # => parsed JSON or raw bytes
response.status # => 200
response.headers # => { "content-type" => "application/json", ... }
Direct Known Subclasses
Constant Summary collapse
- VALID_METHODS =
%w[GET OPTIONS HEAD POST PUT PATCH DELETE].freeze
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: {}, method: "POST", region: nil, response_type: :text, query: nil) ⇒ 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.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/supabase/functions/client.rb', line 35 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.
27 28 29 |
# File 'lib/supabase/functions/client.rb', line 27 def base_url @base_url end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
27 28 29 |
# File 'lib/supabase/functions/client.rb', line 27 def headers @headers end |
Instance Method Details
#invoke(function_name, body: nil, headers: {}, method: "POST", region: nil, response_type: :text, query: nil) ⇒ Types::Response
Invoke an Edge Function by name.
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 |
# File 'lib/supabase/functions/client.rb', line 65 def invoke(function_name, body: nil, headers: {}, method: "POST", region: nil, response_type: :text, query: nil) validate_function_name!(function_name) http_method = method.to_s.upcase unless VALID_METHODS.include?(http_method) raise ArgumentError, "method must be one of #{VALID_METHODS.join(', ')}" end merged_headers = @headers.merge(headers) merged_query = (query || {}).transform_keys(&:to_s) 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( http_method.downcase.to_sym, "#{@base_url}/#{function_name}", encoded_body, merged_headers ) do |req| req.params.update(merged_query) unless merged_query.empty? end raise_for_relay!(response) raise_for_status!(response) Types::Response.new( data: parse_body(response, response_type), status: response.status, headers: response.headers ) end |
#set_auth(token) ⇒ Object
Replace the Authorization header (e.g. when a user signs in / out).
51 52 53 |
# File 'lib/supabase/functions/client.rb', line 51 def set_auth(token) @headers["Authorization"] = "Bearer #{token}" end |