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}" }
)

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

Async::Client

Constant Summary collapse

VALID_METHODS =
%w[GET OPTIONS HEAD POST PUT PATCH DELETE].freeze

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)


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

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

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)

  • method (String, Symbol) (defaults to: "POST")

    HTTP method, defaults to “POST”

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

    one of Types::FunctionRegion::ALL

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

    :json to parse JSON, anything else returns raw bytes

  • query (Hash, nil) (defaults to: nil)

    extra query-string params

Returns:



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