Skip to main content

Installation

Add InsForge to your Swift Package Manager dependencies:
dependencies: [
    .package(url: "https://github.com/insforge/insforge-swift.git", from: "0.0.9")
]
import InsForge

let insforge = InsForgeClient(
    baseURL: URL(string: "https://your-app.insforge.app")!,
    anonKey: "your-anon-key"
)

Enable Logging (Optional)

For debugging, you can configure the SDK log level and destination:
let options = InsForgeClientOptions(
    global: .init(
        logLevel: .debug,
        logDestination: .osLog,
        logSubsystem: "com.example.MyApp"
    )
)

let insforge = InsForgeClient(
    baseURL: URL(string: "https://your-app.insforge.app")!,
    anonKey: "your-anon-key",
    options: options
)
Log Levels:
LevelDescription
.traceMost verbose, includes all internal details
.debugDetailed information for debugging
.infoGeneral operational information (default)
.warningWarnings that don’t prevent operation
.errorErrors that affect functionality
.criticalCritical failures
Log Destinations:
DestinationDescription
.consoleStandard output (print)
.osLogApple’s unified logging system (recommended for iOS/macOS)
.noneDisable logging
.customProvide your own LogHandler factory
Use .info or .error in production to avoid exposing sensitive data in logs.
Currently, InsForge only supports JavaScript/TypeScript functions running in a Deno environment.

invoke()

Invoke a serverless function by slug.

Parameters

  • slug (String) - Function slug/name
  • body ([String: Any], optional) - Request body as dictionary

Overloads

The invoke method has three overloads:
  1. With dictionary body, returning typed response
    func invoke<T: Decodable>(_ slug: String, body: [String: Any]?) async throws -> T
    
  2. With Encodable body, returning typed response
    func invoke<I: Encodable, O: Decodable>(_ slug: String, body: I) async throws -> O
    
  3. Without expecting response body
    func invoke(_ slug: String, body: [String: Any]?) async throws
    
SDK automatically includes authentication token from logged-in user.

Examples

Example: Basic Invocation with Typed Response

// Define response model
struct HelloResponse: Codable {
    let message: String
    let timestamp: String
}

// Invoke function with dictionary body
let response: HelloResponse = try await insforge.functions.invoke(
    "hello-world",
    body: ["name": "World", "greeting": "Hello"]
)

print(response.message)  // "Hello, World!"

Example: With Encodable Request Body

// Define request and response models
struct GreetingRequest: Codable {
    let name: String
    let greeting: String
}

struct GreetingResponse: Codable {
    let message: String
    let timestamp: String
}

// Invoke with typed request
let request = GreetingRequest(name: "World", greeting: "Hello")
let response: GreetingResponse = try await insforge.functions.invoke(
    "hello-world",
    body: request
)

print(response.message)

Error Handling

do {
    let response: MyResponse = try await insforge.functions.invoke(
        "my-function",
        body: ["key": "value"]
    )
    print("Success: \(response)")
} catch let error as InsForgeError {
    switch error {
    case .httpError(let statusCode, let message):
        print("HTTP Error \(statusCode): \(message)")
    case .decodingError(let error):
        print("Failed to decode response: \(error)")
    case .networkError(let error):
        print("Network error: \(error)")
    default:
        print("Error: \(error)")
    }
} catch {
    print("Unexpected error: \(error)")
}