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
32 changes: 30 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
# Change Log

## 10.2.0

* Update sdk to use swift-native doc comments instead of jsdoc styled comments as per [Swift Documentation Comments](https://github.com/swiftlang/swift/blob/main/docs/DocumentationComments.md)
* Add `incrementDocumentAttribute` and `decrementDocumentAttribute` support to `Databases` service
* Add `gif` support to `ImageFormat` enum
* Remove `Content-Type`, `Content-Length` headers and body from websocket requests

## 10.1.1

* Adds warnings to bulk operation methods
* Fix select Queries by updating internal attributes like id, createdAt, updatedAt etc. to be optional in Document model.
* Fix querying datetime values by properly encoding URLs

## 10.1.0

* Add `devKeys` support to `Client` service
* Add `upsertDocument` support to `Databases` service

## 10.0.0

* Add `<REGION>` to doc examples due to the new multi region endpoints
* Add `<REGION>` to doc examples due to the new multi region endpoints
* Add `token` param to `getFilePreview` and `getFileView` for File tokens usage
* Remove `search` param from `listExecutions` method
* Remove `Gif` from ImageFormat enum
* Remove `Gif` from ImageFormat enum

## 9.0.1

* Fix requests failing by removing `Content-Type` header from `GET` and `HEAD` requests

## 9.0.0

* Remove redundant titles from method descriptions.
* Add `codable` models
* Ensure response attribute in `AppwriteException` is always string
59 changes: 58 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Add the package to your `Package.swift` dependencies:

```swift
dependencies: [
.package(url: "[email protected]:appwrite/sdk-for-apple.git", from: "10.1.1"),
.package(url: "[email protected]:appwrite/sdk-for-apple.git", from: "10.2.0"),
],
```

Expand Down Expand Up @@ -163,6 +163,63 @@ func main() {
}
```

### Type Safety with Models

The Appwrite Apple SDK provides type safety when working with database documents through generic methods. Methods like `listDocuments`, `getDocument`, and others accept a `nestedType` parameter that allows you to specify your custom model type for full type safety.

```swift
struct Book: Codable {
let name: String
let author: String
let releaseYear: String?
let category: String?
let genre: [String]?
let isCheckedOut: Bool
}

let databases = Databases(client)

do {
let documents = try await databases.listDocuments(
databaseId: "your-database-id",
collectionId: "your-collection-id",
nestedType: Book.self // Pass in your custom model type
)

for book in documents.documents {
print("Book: \(book.name) by \(book.author)") // Now you have full type safety
}
} catch {
print(error.localizedDescription)
}
```

**Tip**: You can use the `appwrite types` command to automatically generate model definitions based on your Appwrite database schema. Learn more about [type generation](https://appwrite.io/docs/products/databases/type-generation).

### Working with Model Methods

All Appwrite models come with built-in methods for data conversion and manipulation:

**`toMap()`** - Converts a model instance to a dictionary format, useful for debugging or manual data manipulation:
```swift
let user = try await account.get()
let userMap = user.toMap()
print(userMap) // Prints all user properties as a dictionary
```

**`from(map:)`** - Creates a model instance from a dictionary, useful when working with raw data:
```swift
let userData: [String: Any] = ["$id": "123", "name": "John", "email": "[email protected]"]
let user = User.from(map: userData)
```

**`encode(to:)`** - Encodes the model to JSON format (part of Swift's Codable protocol), useful for serialization:
```swift
let user = try await account.get()
let jsonData = try JSONEncoder().encode(user)
let jsonString = String(data: jsonData, encoding: .utf8)
```

### Error Handling

When an error occurs, the Appwrite Apple SDK throws an `AppwriteError` object with `message` and `code` properties. You can handle any errors in a catch block and present the `message` or `localizedDescription` to the user or handle it yourself based on the provided error information. Below is an example.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Appwrite/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ open class Client {
"x-sdk-name": "Apple",
"x-sdk-platform": "client",
"x-sdk-language": "apple",
"x-sdk-version": "10.1.1",
"x-sdk-version": "10.2.0",
"x-appwrite-response-format": "1.7.0"
]

Expand Down
4 changes: 2 additions & 2 deletions Sources/Appwrite/OAuth/WebAuthComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class WebAuthComponent {
url: url,
callbackURLScheme: callbackScheme
) { callbackURL, error in
if let error = error {
if error != nil {
cleanUp()
} else if let callbackURL = callbackURL {
// handle cookies here itself!
Expand Down Expand Up @@ -161,7 +161,7 @@ class PresentationContextProvider: NSObject, ASWebAuthenticationPresentationCont
static let shared = PresentationContextProvider()

func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
if let mainWindow = OSApplication.shared.windows.first { $0.isKeyWindow } {
if let mainWindow = OSApplication.shared.windows.first(where: { $0.isKeyWindow }) {
return mainWindow
}

Expand Down
Loading