@@ -79,11 +79,14 @@ final class FunctionAPIClient {
7979 _ endpoint: FunctionAPIEndpoint < Response > ,
8080 requiresAuthentication: Bool = true
8181 ) async throws -> Response {
82- try await send (
83- endpoint,
84- payload: EmptyPayload ( ) ,
85- requiresAuthentication: requiresAuthentication
86- )
82+ var request = try client ( )
83+ . request ( endpoint)
84+
85+ if requiresAuthentication {
86+ request = request. authorized ( )
87+ }
88+
89+ return try await request. send ( )
8790 }
8891
8992 private func client( ) throws -> NXAPIClient {
@@ -96,16 +99,36 @@ struct FunctionAPIEndpoint<Response: Decodable>: NXEndpoint {
9699 let path : String
97100}
98101
99- struct FunctionAPIResponse : Decodable {
102+ struct GithubAuthenticationResponse : Decodable {
100103 let accessToken : String ?
101104 let customToken : String ?
102- let refreshToken : String ?
103- let token : String ?
104105}
105106
106- struct EmptyAPIResponse : Decodable { }
107+ struct AppleChallengeResponse : Decodable {
108+ let challengeId : String
109+ let hashedNonce : String
110+ }
111+
112+ struct AppleCustomTokenResponse : Decodable {
113+ let customToken : String
114+ }
115+
116+ struct AppleOperationResponse : Decodable {
117+ let success : Bool
118+ }
119+
120+ struct AppleCustomTokenRequest : Encodable {
121+ let challengeId : String
122+ let authorizationCode : String
123+ }
107124
108- private struct EmptyPayload : Encodable { }
125+ struct AppleAccountLinkRequest : Encodable {
126+ let challengeId : String
127+ let authorizationCode : String
128+ let credentialEmail : String ?
129+ }
130+
131+ struct EmptyAPIResponse : Decodable { }
109132
110133private struct FunctionAPIErrorBody : Decodable {
111134 let code : String
@@ -116,9 +139,16 @@ private enum FunctionAPIErrorCode: String {
116139 case emailNotFound = " email-not-found "
117140 case emailMismatch = " email-mismatch "
118141 case githubEmailConflict = " github-email-changed-account-conflict "
142+ case appleProviderLinkConflict = " apple-provider-link-conflict "
143+ case lastProvider = " last-provider "
119144}
120145
121- private struct FunctionAPIServerErrorDecoder : NXServerErrorDecoder {
146+ enum AppleAuthenticationAPIError : Error , Equatable {
147+ case providerLinkConflict
148+ case lastProvider
149+ }
150+
151+ struct FunctionAPIServerErrorDecoder : NXServerErrorDecoder {
122152 func decodeServerError(
123153 data: Data ,
124154 response: HTTPURLResponse ,
@@ -136,6 +166,10 @@ private struct FunctionAPIServerErrorDecoder: NXServerErrorDecoder {
136166 return EmailError . mismatch
137167 case . githubEmailConflict:
138168 return EmailError . githubEmailConflict
169+ case . appleProviderLinkConflict:
170+ return AppleAuthenticationAPIError . providerLinkConflict
171+ case . lastProvider:
172+ return AppleAuthenticationAPIError . lastProvider
139173 }
140174 }
141175}
@@ -152,13 +186,19 @@ private actor FirebaseAuthTokenProvider: NXAuthTokenProvider {
152186
153187extension Error {
154188 var apiEmailError : EmailError ? {
189+ functionAPIUnderlyingError as? EmailError
190+ }
191+
192+ var apiAppleAuthenticationError : AppleAuthenticationAPIError ? {
193+ functionAPIUnderlyingError as? AppleAuthenticationAPIError
194+ }
195+
196+ private var functionAPIUnderlyingError : ( any Error ) ? {
155197 guard let error = self as? NXError ,
156- case let . server(
157- statusCode: _,
158- data: _,
159- underlying: underlying
160- ) = error else { return nil }
198+ case let . server( statusCode: _, data: _, underlying: underlying) = error else {
199+ return nil
200+ }
161201
162- return underlying as? EmailError
202+ return underlying
163203 }
164204}
0 commit comments