This project builds a binding around the Strapi API to ease the communication between your Swift project and the Strapi backend; attempting to eliminate the need to write any URLSession
code yourself.
Swift 5.8, XCTest, Swift DocC, Mocker (For the test suite only)
The Swift DocC generated documentation is hosted on GitHub Pages. I have tried to provide fairly detailed documentation comments throughout and examples below, this should hopefully be enough to get up and running.
Add the StrapiSwiftCross
library to your Package.swift
file as you would any other library. There is no support for CocoaPods or Carthage, if this is something you need feel free to open a pull request!
// swift-tools-version:5.8
import PackageDescription
let package = Package(
name: "Example",
products: [
.library(name: "Example", targets: ["Example"]),
],
dependencies: [
.package(url: "https://github.com/lorenalexm/StrapiSwiftCross.git", from: "0.1.0")
],
targets: [
.target(
name: "Example",
dependencies: ["StrapiSwiftCross"]),
.testTarget(
name: "ExampleTests",
dependencies: ["Example"]),
]
)
Basic example of querying a posts
resource from Strapi.
import StrapiSwiftCross
let strapi = Strapi(host: "http://localhost:1337/api")
let request = QueryRequest("posts")
let result = try? await strapi.execute(request)
print(result)
Example of querying and filtering a posts
example resource from Strapi.
import StrapiSwiftCross
let strapi = Strapi(host: "http://localhost:1337/api")
let request = QueryRequest("posts")
request.addFilter(type: .greaterThan, onField: "viewCount", forValue: 500)
let result = try? await strapi.execute(request)
print(result)
Example of querying, filtering, and sorting a posts
example resource from Strapi.
import StrapiSwiftCross
let strapi = Strapi(host: "http://localhost:1337/api")
let request = QueryRequest("posts")
request.addFilter(type: .greaterThan, onField: "viewCount", forValue: 500)
request.sort(field: "slug", byDirection: .descending)
request.limit(to: 3)
let result = try? await strapi.execute(request)
print(result)
All of the above examples have shown how to retrieve a JSON string as a result from the Strapi host. There is also a utility method to have the JSONDecoder
automatically run on the response, returning you an object of T
.
The example assumes you have a model struct PostsQueryResponse
defined somewhere. Given the customization capabilities of Strapi, a model definition has been omitted from this sample.
import StrapiSwiftCross
let strapi = Strapi(host: "http://localhost:1337/api")
let request = QueryRequest("posts")
let result: PostsQueryResponse = try? await strapi.executeAndDecode(request)
print(result)
To run the test suite, run the following command
swift test
- Ricardo Rauber and his work on StrapiSwift which was leaned upon heavily during development of StrapiSwiftCross.