Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## 14.0.0

* Add array-based enum parameters (e.g., `permissions: [BrowserPermission]`).
* Breaking change: `Output` enum has been removed; use `ImageFormat` instead.
* Add `Channel` helpers for Realtime.

## 13.5.0

* Add `getScreenshot` method to `Avatars` service
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2025 Appwrite (https://appwrite.io) and individual contributors.
Copyright (c) 2026 Appwrite (https://appwrite.io) and individual contributors.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

![Swift Package Manager](https://img.shields.io/github/v/release/appwrite/sdk-for-apple.svg?color=green&style=flat-square)
![License](https://img.shields.io/github/license/appwrite/sdk-for-apple.svg?style=flat-square)
![Version](https://img.shields.io/badge/api%20version-1.8.0-blue.svg?style=flat-square)
![Version](https://img.shields.io/badge/api%20version-1.8.1-blue.svg?style=flat-square)
[![Build Status](https://img.shields.io/travis/com/appwrite/sdk-generator?style=flat-square)](https://travis-ci.com/appwrite/sdk-generator)
[![Twitter Account](https://img.shields.io/twitter/follow/appwrite?color=00acee&label=twitter&style=flat-square)](https://twitter.com/appwrite)
[![Discord](https://img.shields.io/discord/564160730845151244?label=discord&style=flat-square)](https://appwrite.io/discord)

**This SDK is compatible with Appwrite server version 1.8.x. For older versions, please check [previous releases](https://github.com/appwrite/sdk-for-apple/releases).**

Appwrite is an open-source backend as a service server that abstract and simplify complex and repetitive development tasks behind a very simple to use REST API. Appwrite aims to help you develop your apps faster and in a more secure way. Use the Apple SDK to integrate your app with the Appwrite server to easily start interacting with all of Appwrite backend APIs and tools. For full API documentation and tutorials go to [https://appwrite.io/docs](https://appwrite.io/docs)
Appwrite is an open-source backend as a service server that abstracts and simplifies complex and repetitive development tasks behind a very simple to use REST API. Appwrite aims to help you develop your apps faster and in a more secure way. Use the Apple SDK to integrate your app with the Appwrite server to easily start interacting with all of Appwrite backend APIs and tools. For full API documentation and tutorials go to [https://appwrite.io/docs](https://appwrite.io/docs)

![Appwrite](https://github.com/appwrite/appwrite/raw/main/public/images/github.png)

Expand All @@ -31,7 +31,7 @@ Add the package to your `Package.swift` dependencies:

```swift
dependencies: [
.package(url: "git@github.com:appwrite/sdk-for-apple.git", from: "13.5.0"),
.package(url: "git@github.com:appwrite/sdk-for-apple.git", from: "14.0.0"),
],
```

Expand Down
250 changes: 250 additions & 0 deletions Sources/Appwrite/Channel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
import Foundation

// Marker structs for type safety (structs work with == constraints in extensions)
public struct _Root {}
public struct _Database {}
public struct _Collection {}
public struct _Document {}
public struct _TablesDB {}
public struct _Table {}
public struct _Row {}
public struct _Bucket {}
public struct _File {}
public struct _Func {}
public struct _Execution {}
public struct _Team {}
public struct _Membership {}
public struct _Resolved {}

// Helper for normalizing IDs
private func normalize(_ id: String) -> String {
let trimmed = id.trimmingCharacters(in: .whitespacesAndNewlines)
return trimmed.isEmpty ? "*" : trimmed
}

/// Channel class with generic type parameter for type-safe method chaining
public class RealtimeChannel<T> {
private let segments: [String]

internal init(_ segments: [String]) {
self.segments = segments
}

/// Internal helper to transition to next state with segment and ID
internal func next<N>(_ segment: String, _ id: String = "*") -> RealtimeChannel<N> {
return RealtimeChannel<N>(segments + [segment, normalize(id)])
}

/// Internal helper for terminal actions (no ID segment)
internal func resolve(_ action: String) -> RealtimeChannel<_Resolved> {
return RealtimeChannel<_Resolved>(segments + [action])
}

/// Convert channel to string representation
public func toString() -> String {
return segments.joined(separator: ".")
}
}

/// Non-generic Channel enum for static factory methods
public enum Channel {

// MARK: - Root Factories

public static func database(_ id: String = "*") -> RealtimeChannel<_Database> {
return RealtimeChannel<_Database>(["databases", normalize(id)])
}

public static func tablesdb(_ id: String = "*") -> RealtimeChannel<_TablesDB> {
return RealtimeChannel<_TablesDB>(["tablesdb", normalize(id)])
}

public static func bucket(_ id: String = "*") -> RealtimeChannel<_Bucket> {
return RealtimeChannel<_Bucket>(["buckets", normalize(id)])
}

public static func function(_ id: String = "*") -> RealtimeChannel<_Func> {
return RealtimeChannel<_Func>(["functions", normalize(id)])
}

public static func team(_ id: String = "*") -> RealtimeChannel<_Team> {
return RealtimeChannel<_Team>(["teams", normalize(id)])
}

public static func membership(_ id: String = "*") -> RealtimeChannel<_Membership> {
return RealtimeChannel<_Membership>(["memberships", normalize(id)])
}

public static func account(_ userId: String = "") -> String {
let id = normalize(userId)
return id == "*" ? "account" : "account.\(id)"
}

// Global events
public static func documents() -> String { "documents" }
public static func rows() -> String { "rows" }
public static func files() -> String { "files" }
public static func executions() -> String { "executions" }
public static func teams() -> String { "teams" }
}

// MARK: - DATABASE ROUTE
// Protocol extensions restricted by receiver type

/// Only available on RealtimeChannel<_Database>
extension RealtimeChannel where T == _Database {
public func collection(_ id: String = "*") -> RealtimeChannel<_Collection> {
return self.next("collections", id)
}
}

/// Only available on RealtimeChannel<_Collection>
extension RealtimeChannel where T == _Collection {
public func document(_ id: String = "*") -> RealtimeChannel<_Document> {
return self.next("documents", id)
}
}

// MARK: - TABLESDB ROUTE

/// Only available on RealtimeChannel<_TablesDB>
extension RealtimeChannel where T == _TablesDB {
public func table(_ id: String = "*") -> RealtimeChannel<_Table> {
return self.next("tables", id)
}
}

/// Only available on RealtimeChannel<_Table>
extension RealtimeChannel where T == _Table {
public func row(_ id: String = "*") -> RealtimeChannel<_Row> {
return self.next("rows", id)
}
}

// MARK: - BUCKET ROUTE

/// Only available on RealtimeChannel<_Bucket>
extension RealtimeChannel where T == _Bucket {
public func file(_ id: String = "*") -> RealtimeChannel<_File> {
return self.next("files", id)
}
}

// MARK: - FUNCTION ROUTE

/// Only available on RealtimeChannel<_Func>
extension RealtimeChannel where T == _Func {
public func execution(_ id: String = "*") -> RealtimeChannel<_Execution> {
return self.next("executions", id)
}
}

// MARK: - TERMINAL ACTIONS
// Restricted to actionable types (_Document, _Row, _File, _Execution, _Team, _Membership)

/// Only available on RealtimeChannel<_Document>
extension RealtimeChannel where T == _Document {
public func create() -> RealtimeChannel<_Resolved> {
return self.resolve("create")
}

public func update() -> RealtimeChannel<_Resolved> {
return self.resolve("update")
}

public func delete() -> RealtimeChannel<_Resolved> {
return self.resolve("delete")
}
}

/// Only available on RealtimeChannel<_Row>
extension RealtimeChannel where T == _Row {
public func create() -> RealtimeChannel<_Resolved> {
return self.resolve("create")
}

public func update() -> RealtimeChannel<_Resolved> {
return self.resolve("update")
}

public func delete() -> RealtimeChannel<_Resolved> {
return self.resolve("delete")
}
}

/// Only available on RealtimeChannel<_File>
extension RealtimeChannel where T == _File {
public func create() -> RealtimeChannel<_Resolved> {
return self.resolve("create")
}

public func update() -> RealtimeChannel<_Resolved> {
return self.resolve("update")
}

public func delete() -> RealtimeChannel<_Resolved> {
return self.resolve("delete")
}
}

/// Only available on RealtimeChannel<_Execution>
extension RealtimeChannel where T == _Execution {
public func create() -> RealtimeChannel<_Resolved> {
return self.resolve("create")
}

public func update() -> RealtimeChannel<_Resolved> {
return self.resolve("update")
}

public func delete() -> RealtimeChannel<_Resolved> {
return self.resolve("delete")
}
}

/// Only available on RealtimeChannel<_Team>
extension RealtimeChannel where T == _Team {
public func create() -> RealtimeChannel<_Resolved> {
return self.resolve("create")
}

public func update() -> RealtimeChannel<_Resolved> {
return self.resolve("update")
}

public func delete() -> RealtimeChannel<_Resolved> {
return self.resolve("delete")
}
}

/// Only available on RealtimeChannel<_Membership>
extension RealtimeChannel where T == _Membership {
public func create() -> RealtimeChannel<_Resolved> {
return self.resolve("create")
}

public func update() -> RealtimeChannel<_Resolved> {
return self.resolve("update")
}

public func delete() -> RealtimeChannel<_Resolved> {
return self.resolve("delete")
}
}

// MARK: - Protocol for backward compatibility

/// Protocol for channel values that can be converted to strings
public protocol ChannelValue {
func toString() -> String
}

// Make String conform to ChannelValue
extension String: ChannelValue {
public func toString() -> String {
return self
}
}

// Make RealtimeChannel conform to ChannelValue
extension RealtimeChannel: ChannelValue {}
4 changes: 1 addition & 3 deletions Sources/Appwrite/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ open class Client {
"x-sdk-name": "Apple",
"x-sdk-platform": "client",
"x-sdk-language": "apple",
"x-sdk-version": "13.5.0",
"x-sdk-version": "14.0.0",
"x-appwrite-response-format": "1.8.0"
]

Expand All @@ -34,7 +34,6 @@ open class Client {

internal var http: HTTPClient


private static let boundaryChars = "abcdefghijklmnopqrstuvwxyz1234567890"

private static let boundary = randomBoundary()
Expand Down Expand Up @@ -316,7 +315,6 @@ open class Client {
var request = HTTPClientRequest(url: endPoint + path + queryParameters)
request.method = .RAW(value: method)


for (key, value) in self.headers.merging(headers, uniquingKeysWith: { $1 }) {
request.headers.add(name: key, value: value)
}
Expand Down
58 changes: 58 additions & 0 deletions Sources/Appwrite/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,20 @@ public struct Query : Codable, CustomStringConvertible {
).description
}

/// Filter resources where attribute matches a regular expression pattern.
///
/// - Parameters:
/// - attribute: The attribute to filter on.
/// - pattern: The regular expression pattern to match.
/// - Returns: The query string.
public static func regex(_ attribute: String, pattern: String) -> String {
return Query(
method: "regex",
attribute: attribute,
values: [pattern]
).description
}

public static func lessThan(_ attribute: String, value: Any) -> String {
return Query(
method: "lessThan",
Expand Down Expand Up @@ -211,6 +225,28 @@ public struct Query : Codable, CustomStringConvertible {
).description
}

/// Filter resources where the specified attributes exist.
///
/// - Parameter attributes: The list of attributes that must exist.
/// - Returns: The query string.
public static func exists(_ attributes: [String]) -> String {
return Query(
method: "exists",
values: attributes
).description
}

/// Filter resources where the specified attributes do not exist.
///
/// - Parameter attributes: The list of attributes that must not exist.
/// - Returns: The query string.
public static func notExists(_ attributes: [String]) -> String {
return Query(
method: "notExists",
values: attributes
).description
}

public static func between(_ attribute: String, start: Int, end: Int) -> String {
return Query(
method: "between",
Expand Down Expand Up @@ -432,6 +468,28 @@ public struct Query : Codable, CustomStringConvertible {
).description
}

/// Filter array elements where at least one element matches all the specified queries.
///
/// - Parameters:
/// - attribute: The attribute containing the array to filter on.
/// - queries: The list of query strings to match against array elements.
/// - Returns: The query string.
public static func elemMatch(_ attribute: String, queries: [String]) -> String {
let decoder = JSONDecoder()
let decodedQueries = queries.compactMap { queryStr -> Query? in
guard let data = queryStr.data(using: .utf8) else {
return nil
}
return try? decoder.decode(Query.self, from: data)
}

return Query(
method: "elemMatch",
attribute: attribute,
values: decodedQueries
).description
}

public static func distanceEqual(_ attribute: String, values: [Any], distance: Double, meters: Bool = true) -> String {
return Query(
method: "distanceEqual",
Expand Down
Loading