Swift: Invokes a Supabase Edge Function.

Invoke a Supabase Edge Function.

Examples

Invocation with `Decodable` response

struct Response: Decodable \{
  // Expected response definition
\}

let response: Response = try await supabase.functions
  .invoke(
    "hello", 
    options: FunctionInvokeOptions(
      body: ["foo": "bar"]
    )
  )

Invocation with custom response

let response = try await supabase.functions
  .invoke(
    "hello", 
    options: FunctionInvokeOptions(
      body: ["foo": "bar"]
    ),
    decode: \{ data, response in 
      String(data: data, encoding: .utf8)
    \}
  )

print(type(of: response)) // String?

Error handling


do \{
  let response = try await supabase.functions
    .invoke(
      "hello", 
      options: FunctionInvokeOptions(
        body: ["foo": "bar"]
      )
    )
\} catch FunctionsError.httpError(let code, let data) \{
  print("Function returned code \(code) with response \(String(data: data, encoding: .utf8) ?? "")")
\} catch FunctionsError.relayError \{
  print("Relay error")
\} catch \{
  print("Other error: \(error.localizedDescription)")
\}

Passing custom headers

let response = try await supabase.functions
  .invoke(
    "hello", 
    options: FunctionInvokeOptions(
      headers: [
        "my-custom-header": "my-custom-header-value"
      ]
    )
  )

Calling with DELETE HTTP verb

let response = try await supabase.functions
  .invoke(
    "hello", 
    options: FunctionInvokeOptions(
      method: .delete,
      headers: [
        "my-custom-header": "my-custom-header-value"
      ],
      body: ["foo": "bar"]
    )
  )

Calling with GET HTTP verb

let response = try await supabase.functions
  .invoke(
    "hello", 
    options: FunctionInvokeOptions(
      method: .get,
      headers: [
        "my-custom-header": "my-custom-header-value"
      ]
    )
  )