-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathClientHelpers.swift
More file actions
96 lines (90 loc) · 2.94 KB
/
ClientHelpers.swift
File metadata and controls
96 lines (90 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#if JavaScriptFetch
@_spi(BridgeJS) public import JavaScriptKit
public func makeHeaders(contentType: String? = nil) throws(ClientError) -> Headers {
do {
let h = try Headers()
if let contentType { try h.set("Content-Type", contentType) }
return h
} catch {
throw .transportFailure(error)
}
}
public func checkOk(_ response: Response, operationID: String) throws(ClientError) {
let ok: Bool
let status: Double
do {
ok = try response.ok
status = try response.status
} catch {
throw .transportFailure(error)
}
if !ok {
throw .unexpectedStatus(statusCode: Int(status), operationID: operationID)
}
}
public func deserialize<T: _JSBridgedClass>(_ response: Response) async throws(ClientError) -> T {
let jsValue: JSValue
do {
jsValue = try await response.json()
} catch {
throw .transportFailure(error)
}
guard let object = jsValue.object else {
throw .deserializationFailure("Expected JS object for deserialization")
}
return T(unsafelyWrapping: object)
}
public func deserializeArray<T: _JSBridgedClass>(_ response: Response) async throws(ClientError) -> [T] {
let jsValue: JSValue
do {
jsValue = try await response.json()
} catch {
throw .transportFailure(error)
}
guard let object = jsValue.object else {
throw .deserializationFailure("Expected JS object for array deserialization")
}
guard let jsArray = JSArray(object) else {
throw .deserializationFailure("Expected JS Array, got non-array object")
}
var result: [T] = []
for element in jsArray {
guard let object = element.object else {
throw .deserializationFailure("Expected JS object in array element")
}
result.append(T(unsafelyWrapping: object))
}
return result
}
public func deserializeOneOf(
_ response: Response,
discriminatorProperty: String
) async throws(ClientError) -> (String, JSObject) {
let jsValue: JSValue
do {
jsValue = try await response.json()
} catch {
throw .transportFailure(error)
}
guard let object = jsValue.object else {
throw .deserializationFailure("Expected JS object for oneOf response")
}
let discriminatorJSValue = object[discriminatorProperty]
guard let stringValue = discriminatorJSValue.string else {
throw .deserializationFailure("Discriminator property is not a string")
}
return (stringValue, object)
}
public func appendQueryParam(_ url: String, name: String, value: String) throws(ClientError) -> String {
let encodedName: String
let encodedValue: String
do {
encodedName = try encodeURIComponent(name)
encodedValue = try encodeURIComponent(value)
} catch {
throw .transportFailure(error)
}
let separator = url.contains("?") ? "&" : "?"
return url + separator + encodedName + "=" + encodedValue
}
#endif