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?

Invocation with streamed response

var response = Data()
for try await data try await supabase.functions._invokeWithStreamedResponse("hello") {
  response.append(data)
}

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

Invoking a Function in the UsEast1 region

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

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

Pass additional query params

let response = try await supabase.functions
  .invoke(
    "hello",
    options: FunctionInvokeOptions(
      query: [URLQueryItem(name: "key", value: "value")]
    )
  )