Class: Supabase::Functions::Client

Inherits:
Object
  • Object
show all
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

Async::Client

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, headers: {}, http_client: nil, verify: true, proxy: nil, timeout: nil) ⇒ Client

Returns a new instance of Client.

Parameters:

  • base_url (String)

    full URL to the Edge Functions endpoint

  • headers (Hash) (defaults to: {})

    static headers attached to every invocation

  • http_client (Faraday::Connection, nil) (defaults to: nil)

    inject a pre-built Faraday for tests

  • verify (Boolean) (defaults to: true)

    TLS cert verification

  • proxy (String, nil) (defaults to: nil)
  • timeout (Numeric, nil) (defaults to: nil)

    per-request timeout (seconds), default 60

Raises:

  • (ArgumentError)


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_urlObject (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

#headersObject (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.

Parameters:

  • function_name (String)
  • body (Hash, String, nil) (defaults to: nil)

    JSON-encoded if Hash, sent as-is if String

  • headers (Hash) (defaults to: {})

    per-invocation headers (merged over the client defaults)

  • region (String, nil) (defaults to: nil)

    one of Types::FunctionRegion::ALL

  • response_type (Symbol, String) (defaults to: :text)

    controls how the response body is returned. Ruby always returns a ‘String` (unlike supabase-py, which returns `bytes` for binary). Supported values:

    * `:json`   — parse the body as JSON; returns Hash / Array / scalar.
    * `:text`   — return a `String` with `Encoding::UTF_8` (default).
    * `:binary` — return a `String` with `Encoding::BINARY`
      (`ASCII-8BIT`), byte-for-byte equal to the HTTP response body.
    

    Parsing/encoding is opt-in by the caller, never inferred from the response Content-Type.

  • return_response (Boolean) (defaults to: false)

    when true, return the deprecated Types::Response wrapper (data + status + headers) instead of the bare parsed body. Default ‘false` (US-026). The wrapper is scheduled for removal — prefer reading the data directly.

Returns:

  • (Object, Types::Response)

    parsed body (Hash / String / Array / nil) by default; the deprecated ‘Types::Response` struct when `return_response: true` is passed.



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