You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
session-ios/SessionSnodeKit/Utilities/Publisher+Utilities.swift

34 lines
1.0 KiB
Swift

// Copyright © 2024 Rangeproof Pty Ltd. All rights reserved.
import Foundation
import Combine
import SessionUtilitiesKit
// MARK: - Data Decoding
public extension Publisher where Output == Data, Failure == Error {
func decoded<R: Decodable>(
as type: R.Type,
using dependencies: Dependencies
) -> AnyPublisher<R, Failure> {
self
.tryMap { data -> R in try data.decoded(as: type, using: dependencies) }
.eraseToAnyPublisher()
}
}
public extension Publisher where Output == (ResponseInfoType, Data?), Failure == Error {
func decoded<R: Decodable>(
as type: R.Type,
using dependencies: Dependencies
) -> AnyPublisher<(ResponseInfoType, R), Error> {
self
.tryMap { responseInfo, maybeData -> (ResponseInfoType, R) in
guard let data: Data = maybeData else { throw NetworkError.parsingFailed }
return (responseInfo, try data.decoded(as: type, using: dependencies))
}
.eraseToAnyPublisher()
}
}