-
I would like to achieve various multiple implementations of the same client, let's say ApiClient struct ApiClient {
var request: () -> Data
func request(...) { ... }
} Currently we can add With environment approach I would just create 2 static live implementations of this extension ApiClient {
static var authorised: Self { ... }
static var nonAuthorised: Self { ... }
} but with extension DependencyValues {
var apiClient: ApiClient {
get { self[ApiClient.self] }
set { self[ApiClient.self] = newValue }
}
} Is there any way to solve this? or do I need to change implementation of struct ApiClient {
var request: () -> Data
var authorisedRequest: () -> Data
func request(...) { ... }
func authorisedRequest(...) { ... }
} I know that I can create withDependencies {
$0.apiClient = .authorised
} operation {
Model()
} while creating model to change apiClient, but I would like to have it more static not relying on initialisation, but rather on typing |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 9 replies
-
It sounds like you want multiple versions of private enum AuthorizedAPIClientKey: DependencyKey { /* ... */ }
extension DependencyValues {
var authorizedApiClient: ApiClient {
get { self[AuthorizedAPIClientKey.self] }
set { self[AuthorizedAPIClientKey.self] = newValue }
}
}
private enum NonAuthorizedAPIClientKey: DependencyKey { /* ... */ }
extension DependencyValues {
var nonAuthorizedApiClient: ApiClient {
get { self[NonAuthorizedAPIClientKey.self] }
set { self[NonAuthorizedAPIClientKey.self] = newValue }
}
} |
Beta Was this translation helpful? Give feedback.
It sounds like you want multiple versions of
APIClient
available in your application at once? If so, then you are free to add as many computed properties toDependencyValues
as you want. You just need to create a separate type to act as a key: