@arrirpc/codegen-swift 0.60.0
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE +9 -0
- package/README.md +154 -0
- package/dist/index.cjs +1536 -0
- package/dist/index.d.cts +42 -0
- package/dist/index.d.mts +42 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.mjs +1528 -0
- package/package.json +28 -0
package/LICENSE
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright 2024 Joshua Sosso
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
6
|
+
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
8
|
+
|
9
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
@@ -0,0 +1,154 @@
|
|
1
|
+
# Arri Swift Codegen
|
2
|
+
|
3
|
+
## Setup
|
4
|
+
|
5
|
+
### 1) Add the Swift client generator to your arri config
|
6
|
+
|
7
|
+
```ts
|
8
|
+
// arri.config.ts
|
9
|
+
import { defineConfig, generators } from "arri";
|
10
|
+
|
11
|
+
export default defineConfig({
|
12
|
+
generators: [
|
13
|
+
generators.swiftClient({
|
14
|
+
clientName: "MyClient",
|
15
|
+
outputFile: "./client/Sources/MyClient.g.swift",
|
16
|
+
}),
|
17
|
+
],
|
18
|
+
});
|
19
|
+
```
|
20
|
+
|
21
|
+
**Options:**
|
22
|
+
|
23
|
+
| Name | Description |
|
24
|
+
| --------------------- | ------------------------------------------------------ |
|
25
|
+
| clientName (required) | The name of the generated client |
|
26
|
+
| outputFile (required) | Path to the file that will be created by the generator |
|
27
|
+
| typePrefix | Add a prefix to all of the generated types |
|
28
|
+
|
29
|
+
### 2) Install the Swift client library
|
30
|
+
|
31
|
+
The generated code relies on the [Arri Swift Client](/languages/swift/swift-client) library, so be sure to add it to your swift project. The version number should match your Arri CLI version.
|
32
|
+
|
33
|
+
#### Swift Package Manager
|
34
|
+
|
35
|
+
```swift
|
36
|
+
.package(url: "https://github.com/modiimedia/arri-client-swift.git", from: "<your-arri-cli-version>")
|
37
|
+
```
|
38
|
+
|
39
|
+
## Using the Generated Code
|
40
|
+
|
41
|
+
### Initialize the client
|
42
|
+
|
43
|
+
```swift
|
44
|
+
|
45
|
+
let client = MyClient(
|
46
|
+
baseURL: "https://example.com",
|
47
|
+
delegate: DefaultRequestDelegate(),
|
48
|
+
headers {
|
49
|
+
var headers: Dictionary<String, String> = Dictionary()
|
50
|
+
return headers
|
51
|
+
}
|
52
|
+
)
|
53
|
+
|
54
|
+
await client.myProcedure()
|
55
|
+
```
|
56
|
+
|
57
|
+
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.
|
58
|
+
|
59
|
+
For example if we have some procedures grouped under `"users"` we can initialize just the users service like so.
|
60
|
+
|
61
|
+
```swift
|
62
|
+
let usersService = MyClientUsersService(
|
63
|
+
baseURL: "https://example.com",
|
64
|
+
delegate: DefaultRequestDelegate(),
|
65
|
+
headers: {
|
66
|
+
var headers: Dictionary<String, String> = Dictionary()
|
67
|
+
return headers
|
68
|
+
}
|
69
|
+
)
|
70
|
+
|
71
|
+
usersService.someProcedure()
|
72
|
+
```
|
73
|
+
|
74
|
+
### Using the Generated Types
|
75
|
+
|
76
|
+
All the generated structs, classes, and tagged unions implement the `ArriClientModel` protocol, which looks like this:
|
77
|
+
|
78
|
+
```swift
|
79
|
+
public protocol ArriClientModel: Equatable {
|
80
|
+
init()
|
81
|
+
init(json: JSON)
|
82
|
+
init(JSONString: String)
|
83
|
+
func toJSONString() -> String
|
84
|
+
func toURLQueryParts() -> [URLQueryItem]
|
85
|
+
func clone() -> Self
|
86
|
+
}
|
87
|
+
```
|
88
|
+
|
89
|
+
All generated standard enums implement the `ArriClientEnum` protocol, which looks like this:
|
90
|
+
|
91
|
+
```swift
|
92
|
+
public protocol ArriClientEnum: Equatable {
|
93
|
+
init()
|
94
|
+
init(serialValue: String)
|
95
|
+
func serialValue() -> String
|
96
|
+
}
|
97
|
+
```
|
98
|
+
|
99
|
+
### Calling Event Stream Procedures
|
100
|
+
|
101
|
+
Event Stream Procedures spawn a [Task](https://developer.apple.com/documentation/swift/task)
|
102
|
+
|
103
|
+
```swift
|
104
|
+
var msgCount = 0
|
105
|
+
var openCount = 0
|
106
|
+
let params = WatchUserParams()
|
107
|
+
|
108
|
+
// event stream procedures return a task that you can cancel whenever
|
109
|
+
let task: Task<(), Never> = client.users.watchUser(
|
110
|
+
params,
|
111
|
+
options: EventSourceOptions(
|
112
|
+
onMessage: { msg, eventSource in
|
113
|
+
msgCount += 1
|
114
|
+
print("New message: \(msg)")
|
115
|
+
},
|
116
|
+
onRequest: nil,
|
117
|
+
onRequestError: nil,
|
118
|
+
onResponse: { _, eventSource in
|
119
|
+
openCount += 1
|
120
|
+
print("Established connection!")
|
121
|
+
},
|
122
|
+
onResponseError: { err, eventSource in
|
123
|
+
print("The server returned an error: \(err)")
|
124
|
+
// you can also cancel the task from inside one of these hooks
|
125
|
+
// by calling `cancel()` on the EventSource.
|
126
|
+
// this will cause the parent Task to be completed
|
127
|
+
eventSource.cancel()
|
128
|
+
},
|
129
|
+
onClose: nil,
|
130
|
+
maxRetryCount: nil,
|
131
|
+
maxRetryInterval: nil,
|
132
|
+
)
|
133
|
+
)
|
134
|
+
|
135
|
+
// if you want to wait for the task to finished
|
136
|
+
await task.result
|
137
|
+
// this will continue indefinitely unless the server sends a "done" event
|
138
|
+
// or you call `cancel()` on the EventSource
|
139
|
+
```
|
140
|
+
|
141
|
+
#### Available Event Source Options
|
142
|
+
|
143
|
+
- `onMessage` - Closure that fires whenever a "message" event is received from the server. This is the only required option.
|
144
|
+
- `onRequest` - Closure that fires when a request has been created but has not been executed yet.
|
145
|
+
- `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)
|
146
|
+
- `onResponse` - Closure that fires when we receive a response from the server
|
147
|
+
- `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`
|
148
|
+
- `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.)
|
149
|
+
- `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`)
|
150
|
+
- `maxRetryInterval` - Set the max delay time between retries in milliseconds. Default is `30000`.
|
151
|
+
|
152
|
+
## Additional Notes
|
153
|
+
|
154
|
+
Currently the `DefaultRequestDelegate()` relies on [AsyncHTTPClient](https://github.com/swift-server/async-http-client). 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.
|