StrapiSwift

1.0.1

Swift Package that provides a simple and easy way to interact with Strapi APIs in Swift | Mees Akveld
meesakveld/strapi-swift

What's New

v1.0.1

2025-04-24T16:15:51Z

1.0.1 (2025-04-24)

Downgraded deployment target to iOS 15 and updated Swift tools version to 5.5.

StrapiSwift

StrapiSwift is a Swift package that provides a simple and easy way to interact with Strapi APIs. It is built on top of the URLRequest and Codable protocols to provide a seamless experience for developers. StrapiSwift is designed to work with Strapi APIs that are built with the default REST API plugin.

Table of Contents


Installation

You can add StrapiSwift to your Swift project using Swift Package Manager.

  1. In Xcode, go to File -> Swift Packages -> Add Package Dependency.

  2. Enter the repository URL: https://github.com/meesakveld/strapi-swift.git

  3. Choose the version or branch you want to use.


Configuration

To configure StrapiSwift, you need to provide the base URL of your Strapi API and the authentication token (if needed).

Setup

import SwiftUI
import StrapiSwift

@main
struct SomeNameApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
        // —— Configure StrapiSwift ——
        Strapi.configure(
            baseURL: "https://your-strapi-url.com",
            token: "some-token"
        )
        
        return true
    }

}

The configuration can always be overwritten, to for example use a jwt token after logging in.

Use a replacing token once

After making an request with the token, the one time use token will be replaced with the original configured token.

Strapi.useTokenOnce(token: "some-token")

Content Manager

Filter, sort, populate, fields, pagination, locale and status parameters are supported.

  • GET requests

    // Get all documents
    try await Strapi.contentManager.collection("gifts").getDocuments(as: [Gift].self)
    
    // Get a single document
    try await Strapi.contentManager.collection("gifts").withDocumentId("a4fsdnjsdf42dsfd").getDocument(as: Gift.self)
  • POST requests

    let data = StrapiRequestBody([
        "invitedUserEmail": .string(email),
        "event": .string(eventDocumentId)
    ])
    
    try await Strapi.contentManager.collection("event-invites").postData(data, as: EventInvite.self)
  • PUT requests

    let data = StrapiRequestBody([
        "status": .string("accepted")
    ])
    
    try await Strapi.contentManager.collection("event-invites").withDocumentId("a4fsdnjsdf42dsfd").putData(data, as: EventInvite.self)
  • DELETE requests

    try await Strapi.contentManager.collection("event-invites").withDocumentId("a4fsdnjsdf42dsfd").delete()

Authentication Local Provider

  • Register: a new user

    try await Strapi.authentication.local.register(username: "michaelscott", email: "michaelscott@dundermifflin.com", password: "password", as: User.self)
  • Login: an existing user

    try await Strapi.authentication.local.login(identifier: "michaelscott", password: "password", as: User.self)
  • Update Profile: for a user

    let data: StrapiRequestBody = StrapiRequestBody([
        "firstname": .string("Michael"),
        "lastname": .string("Scott"),
    ])
    
    try await Strapi.authentication.local.updateProfile(data, userId: 1, as: User.self)
  • Me: to get the current user (with option add an extended url)

    // Get the current user
    try await Strapi.authentication.local.me(as: User.self)
    
    // Me with an extended url for custom endpoints
    let data: StrapiRequestBody = StrapiRequestBody([
        "deviceToken": .string("dfsdg53gdfg532GFD6fgds")
    ])
    
    try await Strapi.authentication.local.me(extendUrl: "/device-token", requestType: .PUT, data: data, as: User.self)
  • Change Password: for a user

    try await Strapi.authentication.local.changePassword(currentPassword: "currentPassword", newPassword: "newPassword", as: User.self)

Media Library

  • Get All Files: in the media library

    try await Strapi.mediaLibrary.files.getFiles(as: [StrapiImage].self)
  • Get File: by ID

    try await Strapi.mediaLibrary.files.withId(1).getFile(as: StrapiImage.self)
  • Upload Image: to the media library (via UIImage or URL)

    // Upload image from UIImage
    try await Strapi.mediaLibrary.files.uploadImage(image: image)
    
    // Upload image from URL
    try await Strapi.mediaLibrary.files.uploadImage(from: "https://picsum.photos/200/300")
  • Delete File: by ID

    try await Strapi.mediaLibrary.files.withId(1).delete(as: StrapiImage.self)

How to Contribute

If you'd like to contribute, feel free to fork the repository, make your changes, and open a pull request.

License

StrapiSwift is licensed under the Apache License 2.0. See the LICENSE file for more details.

Description

  • Swift Tools 5.5.0
View More Packages from this Author

Dependencies

  • None
Last updated: Fri May 16 2025 14:23:26 GMT-0900 (Hawaii-Aleutian Daylight Time)