Class: Supabase::Auth::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/supabase/auth/api.rb

Direct Known Subclasses

AdminApi

Constant Summary collapse

CONTENT_TYPE =
"application/json;charset=UTF-8"
UUID_REGEX =
/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/i

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Api.

Parameters:

  • url (String)

    The GoTrue API base URL

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

    Default headers to include on every request (e.g., apikey)

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

    Optional custom Faraday client

  • verify (Boolean) (defaults to: true)

    Verify TLS certificates (default true)

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

    HTTP proxy URL

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

    Per-request timeout in seconds



20
21
22
23
24
25
26
27
# File 'lib/supabase/auth/api.rb', line 20

def initialize(url:, headers: {}, http_client: nil, verify: true, proxy: nil, timeout: nil)
  @url = url
  @headers = headers
  @http_client = http_client
  @verify = verify
  @proxy = proxy
  @timeout = timeout
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



12
13
14
# File 'lib/supabase/auth/api.rb', line 12

def headers
  @headers
end

#urlObject (readonly)

Returns the value of attribute url.



12
13
14
# File 'lib/supabase/auth/api.rb', line 12

def url
  @url
end

Instance Method Details

#_request(method, path, jwt: nil, body: nil, params: {}, headers: {}, redirect_to: nil, xform: nil, no_resolve_json: false) ⇒ Hash, Object

Central HTTP dispatch method. Builds URL, merges headers (including API version and Authorization), handles redirect_to as query param, parses JSON, applies optional transform, and maps errors via Helpers.handle_exception.

Parameters:

  • method (String, Symbol)

    HTTP method (GET, POST, PUT, DELETE)

  • path (String)

    Request path (relative to base URL)

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

    Bearer token for Authorization header

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

    Request body (serialized to JSON)

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

    Query parameters

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

    Additional headers for this request

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

    If present, added as redirect_to query param

  • xform (Proc, nil) (defaults to: nil)

    Optional transform function applied to parsed response

  • no_resolve_json (Boolean) (defaults to: false)

    If true, return raw Faraday::Response

Returns:

  • (Hash, Object)

    Parsed JSON response, transformed result, or raw response



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/supabase/auth/api.rb', line 43

def _request(method, path, jwt: nil, body: nil, params: {}, headers: {}, redirect_to: nil, xform: nil, no_resolve_json: false)
  merged_headers = @headers.merge(headers)
  merged_headers["Content-Type"] ||= CONTENT_TYPE
  merged_headers[Constants::API_VERSION_HEADER_NAME] ||= Constants::API_VERSIONS.keys.last
  merged_headers["Authorization"] = "Bearer #{jwt}" if jwt

  query = params.dup
  query["redirect_to"] = redirect_to if redirect_to

  full_path = build_path(path)
  json_body = body ? JSON.generate(body) : nil

  response = connection.run_request(method.to_s.downcase.to_sym, full_path, json_body, merged_headers) do |req|
    req.params.update(query) unless query.empty?
  end

  result = no_resolve_json ? response : parse_response(response)

  xform ? xform.call(result) : result
rescue Faraday::Error => e
  raise Helpers.handle_exception(e)
rescue StandardError => e
  raise Helpers.handle_exception(e)
end

#_validate_uuid(id) ⇒ Object

Parameters:

  • id (String)

    UUID to validate

Raises:

  • (ArgumentError)

    if not a valid UUID format



70
71
72
73
74
# File 'lib/supabase/auth/api.rb', line 70

def _validate_uuid(id)
  unless id.is_a?(String) && id.match?(UUID_REGEX)
    raise ArgumentError, "Invalid id, '#{id}' is not a valid uuid"
  end
end

#delete(path, headers: {}, params: {}) ⇒ Object



90
91
92
# File 'lib/supabase/auth/api.rb', line 90

def delete(path, headers: {}, params: {})
  _request(:delete, path, headers: headers, params: params)
end

#get(path, headers: {}, params: {}) ⇒ Object

Convenience methods that delegate to _request



78
79
80
# File 'lib/supabase/auth/api.rb', line 78

def get(path, headers: {}, params: {})
  _request(:get, path, headers: headers, params: params)
end

#post(path, body: {}, headers: {}, params: {}) ⇒ Object



82
83
84
# File 'lib/supabase/auth/api.rb', line 82

def post(path, body: {}, headers: {}, params: {})
  _request(:post, path, body: body, headers: headers, params: params)
end

#put(path, body: {}, headers: {}, params: {}) ⇒ Object



86
87
88
# File 'lib/supabase/auth/api.rb', line 86

def put(path, body: {}, headers: {}, params: {})
  _request(:put, path, body: body, headers: headers, params: params)
end