@arrirpc/codegen-swift

中文文档
0.85.0发布于 2026/10/03MITTypeScriptESM + CJS近 30 天下载 4567

_Please note: The generated code does not yet support swift-tools-version 6 or greater. Please track the status of this issue here. In the meantime please set your swift-tools-version to 5.10 or lower

原始使用文档
文档目录8 节
  1. Setup
  2. 1) Add the Swift client generator to your arri config
  3. 2) Install the Swift client library
  4. Using the Generated Code
  5. Initialize the client
  6. Using the Generated Types
  7. Calling Event Stream Procedures
  8. Additional Notes

Arri Swift Codegen

Please note: The generated code does not yet support swift-tools-version 6 or greater. Please track the status of this issue here. In the meantime please set your swift-tools-version to 5.10 or lower.

Setup

1) Add the Swift client generator to your arri config

// arri.config.ts
import { defineConfig, generators } from 'arri';

export default defineConfig({
    generators: [
        generators.swiftClient({
            clientName: 'MyClient',
            outputFile: './client/Sources/MyClient.g.swift',
        }),
    ],
});

Options:

NameDescription
outputFile (required)Path to the file that will be created by the generator
clientNameThe name of the generated client
typePrefixAdd a prefix to all of the generated types
rootServiceThe root service of the generated client

2) Install the Swift client library

The generated code relies on the Arri Swift Client library, so be sure to add it to your swift project. The version number should match your Arri CLI version.

Swift Package Manager

Add the arri-client-swift git repo to your package dependencies

.package(
    url: "https://github.com/modiimedia/arri-client-swift.git",
    from: "<your-arri-cli-version>"
)

then add ArriClient as a dependency to your target

.target(
    name: "MyApp",
    dependencies: [
        .product(name: "ArriClient", package: "arri-client-swift")
    ]
)

Using the Generated Code

Initialize the client


let client = MyClient(
    baseURL: "https://example.com",
    delegate: DefaultRequestDelegate(),
    headers: {
        var headers: Dictionary<String, String> = Dictionary()
        return headers
    },
    // optional
    onError: { err in
        // do something
    }
)

await client.myProcedure()

The root client will be a struct containing all of the sub-services and procedures. If you only need a particular service you can initialize just that service.

For example if we have some procedures grouped under "users" we can initialize just the users service like so.

let usersService = MyClientUsersService(
    baseURL: "https://example.com",
    delegate: DefaultRequestDelegate(),
    headers: {
        var headers: Dictionary<String, String> = Dictionary()
        return headers
    }
)

usersService.someProcedure()

Using the Generated Types

All the generated structs, classes, and tagged unions implement the ArriClientModel protocol, which looks like this:

public protocol ArriClientModel: Equatable {
    init()
    init(json: JSON)
    init(JSONString: String)
    func toJSONString() -> String
    func toURLQueryParts() -> [URLQueryItem]
    func clone() -> Self
}

All generated standard enums implement the ArriClientEnum protocol, which looks like this:

public protocol ArriClientEnum: Equatable {
    init()
    init(serialValue: String)
    func serialValue() -> String
}

Calling Event Stream Procedures

Event Stream Procedures spawn a Task

var msgCount = 0
var openCount = 0
let params = WatchUserParams()

// event stream procedures return a task that you can cancel whenever
let task: Task<(), Never> = client.users.watchUser(
    params,
    options: EventSourceOptions(
        onMessage: { msg, eventSource in
            msgCount += 1
            print("New message: \(msg)")
        },
        onRequest: nil,
        onRequestError: nil,
        onResponse: { _, eventSource in
            openCount += 1
            print("Established connection!")
        },
        onResponseError: { err, eventSource in
            print("The server returned an error: \(err)")
            // you can also cancel the task from inside one of these hooks
            // by calling `cancel()` on the EventSource.
            // this will cause the parent Task to be completed
            eventSource.cancel()
        },
        onClose: nil,
        maxRetryCount: nil,
        maxRetryInterval: nil,
    )
)

// if you want to wait for the task to finished
await task.result
// this will continue indefinitely unless the server sends a "done" event
// or you call `cancel()` on the EventSource

Available Event Source Options

  • onMessage - Closure that fires whenever a "message" event is received from the server. This is the only required option.
  • onRequest - Closure that fires when a request has been created but has not been executed yet.
  • onRequestError - Closure that fires when there was an error in creating the request (i.e. a malformed URL), or if we were unable to connect to the server. (i.e a connectionRefused error)
  • onResponse - Closure that fires when we receive a response from the server
  • onResponseError - Closure that fires when the server has not responded with status code from 200 - 299 or the Content-Type header does not contain text/event-stream
  • onClose - Closure that fires when the EventSource is closed. (This will only fire if the EventSource was already able successfully receive a response from the server.)
  • maxRetryCount - Limit the number of times that the EventSource tries to reconnect to the server. When set to nil it will retry indefinitely. (Default is nil)
  • maxRetryInterval - Set the max delay time between retries in milliseconds. Default is 30000.

Additional Notes

Currently the DefaultRequestDelegate() relies on AsyncHTTPClient. I would like to eventually remove this dependency, so if anyone knows how to get Server Sent Events working with Foundation Swift libraries please open an issue. Please note that these clients needs to run on Linux, so the proposed solution needs to work without making use of URLSession.asyncBytes or any of the other APIs that only work on Apple platforms.

版本与发布记录

@arrirpc/codegen-swift 共发布 56 个版本,首次发布于 2024/08/17;过去 12 个月发布了 11 个版本。

版本发布日期距今
0.85.0latest2026/10/031 个月前
0.84.12026/08/043 个月前
0.84.02026/08/043 个月前
0.83.12026/07/143 个月前
0.83.02026/07/143 个月前
0.82.02026/05/225 个月前

其他发布通道:beta → 0.76.3-beta.0。

关于 @arrirpc/codegen-swift 的常见问题

@arrirpc/codegen-swift 是什么?

_Please note: The generated code does not yet support swift-tools-version 6 or greater. Please track the status of this issue here. In the meantime please set your swift-tools-version to 5.10 or lower

如何安装 @arrirpc/codegen-swift?

在项目目录运行 npm install @arrirpc/codegen-swift,或使用 pnpm add / yarn add / bun add。

@arrirpc/codegen-swift 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@arrirpc/codegen-swift 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@arrirpc/codegen-swift 可以商用吗?

@arrirpc/codegen-swift 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@arrirpc/codegen-swift 的下载量有多少?

根据 npm 官方下载统计,@arrirpc/codegen-swift 在 2026/10/07 – 2026/11/05 这 30 天内被下载 4,567 次,日均约 152 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@arrirpc/codegen-swift 还在维护吗?

最新版本 0.85.0 发布于 2026/10/03(1 个月前);至今共发布 56 个版本,其中过去 12 个月发布了 11 个;首次发布于 2024/08/17。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。