From 65dc4185873472e69a5722e03de52eb58408e155 Mon Sep 17 00:00:00 2001 From: Mykola Buhaiov Date: Thu, 13 Jun 2024 10:26:07 +0300 Subject: [PATCH] refactor: add public into fields --- .../Sources/Utilities/HostsError.swift | 5 +++-- .../Utilities/AuthenticationError.swift | 11 ++++++----- .../Extensions/Application+Extensions.swift | 18 ++++++++++-------- .../Sources/MongoComponents.swift | 2 +- .../Sources/MongoComponentsProtocol.swift | 2 +- .../Mock/MongoComponentsMock.swift | 2 +- 6 files changed, 22 insertions(+), 18 deletions(-) diff --git a/Services/AllowedHostsMiddleware/Sources/Utilities/HostsError.swift b/Services/AllowedHostsMiddleware/Sources/Utilities/HostsError.swift index 464e02a..0d72bab 100644 --- a/Services/AllowedHostsMiddleware/Sources/Utilities/HostsError.swift +++ b/Services/AllowedHostsMiddleware/Sources/Utilities/HostsError.swift @@ -23,8 +23,9 @@ // import Vapor +import ErrorMiddleware -public enum HostError: AbortError, DebuggableError { +public enum HostError: AppError { case notAcceptable case unauthorizedAccessAttempt(ipAddress: String) @@ -55,7 +56,7 @@ public enum HostError: AbortError, DebuggableError { } } - var number: String { + public var number: String { switch self { case .notAcceptable: return "0001" diff --git a/Services/JWTComponents/Sources/Utilities/AuthenticationError.swift b/Services/JWTComponents/Sources/Utilities/AuthenticationError.swift index a7abea8..cdbf539 100644 --- a/Services/JWTComponents/Sources/Utilities/AuthenticationError.swift +++ b/Services/JWTComponents/Sources/Utilities/AuthenticationError.swift @@ -23,16 +23,17 @@ // import Vapor +import ErrorMiddleware /// A generic `AuthenticationError` data. -enum AuthenticationError: AbortError, DebuggableError { +public enum AuthenticationError: AppError { /// The `AuthorizationError` not found bearer token case missingAuthorizationHeader case claimVerificationFailure /// Http response status of error - var status: HTTPResponseStatus { + public var status: HTTPResponseStatus { switch self { case .missingAuthorizationHeader: return .unauthorized @@ -43,7 +44,7 @@ enum AuthenticationError: AbortError, DebuggableError { } /// Reason of error - var reason: String { + public var reason: String { switch self { case .missingAuthorizationHeader: return "Missing authorization header" @@ -54,7 +55,7 @@ enum AuthenticationError: AbortError, DebuggableError { } /// Identifier of error - var identifier: String { + public var identifier: String { switch self { case .missingAuthorizationHeader: return "missing_authorization_header" @@ -64,7 +65,7 @@ enum AuthenticationError: AbortError, DebuggableError { } } - var number: String { + public var number: String { switch self { case .missingAuthorizationHeader: return "0001" diff --git a/Services/MongoComponents/Sources/Extensions/Application+Extensions.swift b/Services/MongoComponents/Sources/Extensions/Application+Extensions.swift index 527e1fe..9de9a08 100644 --- a/Services/MongoComponents/Sources/Extensions/Application+Extensions.swift +++ b/Services/MongoComponents/Sources/Extensions/Application+Extensions.swift @@ -52,10 +52,11 @@ extension Application { set { storage[MongoClusterKey.self] = newValue } } - /* - /// For example setup mongo cluster need use method like this: - app.mongoCluster = try await MongoCluster(connectingTo: ConnectionSettings(connectionString)) - */ + /// Initialize MongoCluster + /// - Parameter connectionString: URI as `String`. Example: "mongodb://localhost/myapp + public func initializeMongoCluster(connectionString: String) throws { + self.mongoCluster = try MongoCluster(lazyConnectingTo: ConnectionSettings(connectionString)) + } } extension Application { @@ -71,8 +72,9 @@ extension Application { set { storage[MongoDBKey.self] = newValue } } - /* - /// For example setup mongo database need use method like this: - app.mongoDB = try await try MongoDatabase.lazyConnect(to: connectionString) - */ + /// Initialize MongoDB + /// - Parameter connectionString: URI as `String`. Example: "mongodb://localhost/myapp + public func initializeMongoDB(connectionString: String) throws { + self.mongoDB = try MongoDatabase.lazyConnect(to: connectionString) + } } diff --git a/Services/MongoComponents/Sources/MongoComponents.swift b/Services/MongoComponents/Sources/MongoComponents.swift index e602d5d..73d0368 100644 --- a/Services/MongoComponents/Sources/MongoComponents.swift +++ b/Services/MongoComponents/Sources/MongoComponents.swift @@ -33,7 +33,7 @@ public struct MongoComponents: MongoComponentsProtocol { self.app = app } - public func getConnection(by url: String) async -> MongoConnectionState { + public func checkConnection(by url: String) async -> MongoConnectionState { let result = app.mongoCluster?.connectionState app.logger.debug("Connect to mongo have result: \(String(describing: result))") guard let result else { diff --git a/Services/MongoComponents/Sources/MongoComponentsProtocol.swift b/Services/MongoComponents/Sources/MongoComponentsProtocol.swift index a6b550b..7c46e50 100644 --- a/Services/MongoComponents/Sources/MongoComponentsProtocol.swift +++ b/Services/MongoComponents/Sources/MongoComponentsProtocol.swift @@ -27,5 +27,5 @@ import MongoClient /// Groups func for get mongo components public protocol MongoComponentsProtocol { - func getConnection(by url: String) async -> MongoConnectionState + func checkConnection(by url: String) async -> MongoConnectionState } diff --git a/Tests/fs-componentsTests/Mock/MongoComponentsMock.swift b/Tests/fs-componentsTests/Mock/MongoComponentsMock.swift index 965de63..2e583a2 100644 --- a/Tests/fs-componentsTests/Mock/MongoComponentsMock.swift +++ b/Tests/fs-componentsTests/Mock/MongoComponentsMock.swift @@ -27,7 +27,7 @@ import MongoClient @testable import MongoComponents public struct MongoComponentsMock: MongoComponentsProtocol { - public func getConnection(by url: String) async -> MongoConnectionState { + public func checkConnection(by url: String) async -> MongoConnectionState { MongoConnectionState.connecting } }