From be2c6dd2eb90b4ee9c976ab7a27f6e8f8473f994 Mon Sep 17 00:00:00 2001 From: Corey Date: Sat, 24 Jun 2023 17:57:42 -0400 Subject: [PATCH] feat: Use new ParseHookFunction and ParseHookTrigger types (#43) * feat: Use new ParseHookFunction and ParseHookTrigger types * lint --- Package.resolved | 4 +- Package.swift | 2 +- .../ParseHookFunction+Vapor.swift} | 55 ++++++++----------- .../ParseHookTrigger+Vapor.swift} | 50 +++++++---------- Sources/ParseServerSwift/Models/Hooks.swift | 18 +++--- Tests/ParseServerSwiftTests/AppTests.swift | 16 ++++-- 6 files changed, 64 insertions(+), 81 deletions(-) rename Sources/ParseServerSwift/{Models/HookFunction.swift => Extensions/ParseHookFunction+Vapor.swift} (94%) rename Sources/ParseServerSwift/{Models/HookTrigger.swift => Extensions/ParseHookTrigger+Vapor.swift} (98%) diff --git a/Package.resolved b/Package.resolved index 58827c11..165205f6 100644 --- a/Package.resolved +++ b/Package.resolved @@ -41,8 +41,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/netreconlab/Parse-Swift.git", "state" : { - "revision" : "835083e870b20a7a999b49e5ed3f57c0dc442e89", - "version" : "5.7.4" + "revision" : "66627b55b558698f50220eaae4cea7057cd14450", + "version" : "5.8.0" } }, { diff --git a/Package.swift b/Package.swift index a244e6a9..8b5e450e 100644 --- a/Package.swift +++ b/Package.swift @@ -18,7 +18,7 @@ let package = Package( dependencies: [ .package(url: "https://github.com/vapor/vapor.git", .upToNextMajor(from: "4.77.0")), .package(url: "https://github.com/netreconlab/Parse-Swift.git", - .upToNextMajor(from: "5.7.4")) + .upToNextMajor(from: "5.8.0")) ], targets: [ .target( diff --git a/Sources/ParseServerSwift/Models/HookFunction.swift b/Sources/ParseServerSwift/Extensions/ParseHookFunction+Vapor.swift similarity index 94% rename from Sources/ParseServerSwift/Models/HookFunction.swift rename to Sources/ParseServerSwift/Extensions/ParseHookFunction+Vapor.swift index e2f3bd0f..9d9ce684 100644 --- a/Sources/ParseServerSwift/Models/HookFunction.swift +++ b/Sources/ParseServerSwift/Extensions/ParseHookFunction+Vapor.swift @@ -1,5 +1,5 @@ // -// HookFunction.swift +// ParseHookFunction+Vapor.swift // // // Created by Corey Baker on 6/23/22. @@ -9,29 +9,18 @@ import Foundation import ParseSwift import Vapor -/** - Parse Hook Functions can be created by conforming to - `ParseHookFunctionable`. - */ -public struct HookFunction: ParseHookFunctionable { - public var functionName: String? - public var url: URL? - - public init() {} -} - // MARK: HookFunction - Internal -extension HookFunction { +extension ParseHookFunction { @discardableResult static func method(_ method: HTTPMethod, _ path: [PathComponent], name: String, - parseServerURLStrings: [String]) async throws -> [String: HookFunction] { + parseServerURLStrings: [String]) async throws -> [String: Self] { let url = try buildServerPathname(path) - let hookFunction = HookFunction(name: name, - url: url) - var hookFunctions = [String: HookFunction]() + let hookFunction = Self(name: name, + url: url) + var hookFunctions = [String: Self]() for parseServerURLString in parseServerURLStrings { do { @@ -79,7 +68,7 @@ extension HookFunction { } // MARK: HookFunction - Fetch -public extension HookFunction { +public extension ParseHookFunction { /** Fetches a Parse Cloud Code hook function. @@ -93,7 +82,7 @@ public extension HookFunction { */ static func fetch(_ path: PathComponent..., name: String, - parseServerURLStrings: [String]) async throws -> [String: HookFunction] { + parseServerURLStrings: [String]) async throws -> [String: Self] { try await fetch(path, name: name, parseServerURLStrings: parseServerURLStrings) } @@ -109,7 +98,7 @@ public extension HookFunction { */ static func fetch(_ path: [PathComponent], name: String, - parseServerURLStrings: [String]) async throws -> [String: HookFunction] { + parseServerURLStrings: [String]) async throws -> [String: Self] { try await method(.PUT, path, name: name, parseServerURLStrings: parseServerURLStrings) } @@ -125,7 +114,7 @@ public extension HookFunction { */ static func fetchAll(_ path: PathComponent..., name: String, - parseServerURLStrings: [String]) async throws -> [String: [HookFunction]] { + parseServerURLStrings: [String]) async throws -> [String: [Self]] { try await fetchAll(path, name: name, parseServerURLStrings: parseServerURLStrings) } @@ -141,11 +130,11 @@ public extension HookFunction { */ static func fetchAll(_ path: [PathComponent], name: String, - parseServerURLStrings: [String]) async throws -> [String: [HookFunction]] { + parseServerURLStrings: [String]) async throws -> [String: [Self]] { let url = try buildServerPathname(path) - let hookFunction = HookFunction(name: name, + let hookFunction = Self(name: name, url: url) - var hookFunctions = [String: [HookFunction]]() + var hookFunctions = [String: [Self]]() for parseServerURLString in parseServerURLStrings { do { @@ -161,7 +150,7 @@ public extension HookFunction { } // MARK: HookFunction - Create -public extension HookFunction { +public extension ParseHookFunction { /** Creates a Parse Cloud Code hook function. @@ -177,7 +166,7 @@ public extension HookFunction { static func create(_ path: PathComponent..., name: String, // swiftlint:disable:next line_length - parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: HookFunction] { + parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: Self] { try await create(path, name: name, parseServerURLStrings: parseServerURLStrings) } @@ -195,13 +184,13 @@ public extension HookFunction { static func create(_ path: [PathComponent], name: String, // swiftlint:disable:next line_length - parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: HookFunction] { + parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: Self] { try await method(.POST, path, name: name, parseServerURLStrings: parseServerURLStrings) } } // MARK: HookFunction - Update -public extension HookFunction { +public extension ParseHookFunction { /** Updates a Parse Cloud Code hook function. @@ -217,7 +206,7 @@ public extension HookFunction { static func update(_ path: PathComponent..., name: String, // swiftlint:disable:next line_length - parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: HookFunction] { + parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: Self] { try await update(path, name: name, parseServerURLStrings: parseServerURLStrings) } @@ -235,13 +224,13 @@ public extension HookFunction { static func update(_ path: [PathComponent], name: String, // swiftlint:disable:next line_length - parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: HookFunction] { + parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: Self] { try await method(.PUT, path, name: name, parseServerURLStrings: parseServerURLStrings) } } // MARK: HookFunction - Delete -public extension HookFunction { +public extension ParseHookFunction { /** Removes a Parse Cloud Code hook function. @@ -359,8 +348,8 @@ public extension RoutesBuilder { let route = self.on(.POST, path, body: body, use: closure) Task { do { - await configuration.hooks.updateFunctions(try await HookFunction.create(route.path, - name: name)) + await configuration.hooks.updateFunctions(try await ParseHookFunction.create(route.path, + name: name)) } catch { // swiftlint:disable:next line_length configuration.logger.error("Could not create HookFunction route for path: \(path); name: \(name) on servers: \(configuration.parseServerURLStrings) because of error: \(error)") diff --git a/Sources/ParseServerSwift/Models/HookTrigger.swift b/Sources/ParseServerSwift/Extensions/ParseHookTrigger+Vapor.swift similarity index 98% rename from Sources/ParseServerSwift/Models/HookTrigger.swift rename to Sources/ParseServerSwift/Extensions/ParseHookTrigger+Vapor.swift index 78451393..49598450 100644 --- a/Sources/ParseServerSwift/Models/HookTrigger.swift +++ b/Sources/ParseServerSwift/Extensions/ParseHookTrigger+Vapor.swift @@ -1,5 +1,5 @@ // -// HookTrigger.swift +// ParseHookTrigger+Vapor.swift // // // Created by Corey Baker on 6/23/22. @@ -9,20 +9,8 @@ import Foundation import ParseSwift import Vapor -/** - Parse Hook Triggers can be created by conforming to - `ParseHookTriggerable`. - */ -public struct HookTrigger: ParseHookTriggerable { - public var className: String? - public var triggerName: ParseHookTriggerType? - public var url: URL? - - public init() {} -} - // MARK: HookTrigger - Internal -extension HookTrigger { +extension ParseHookTrigger { @discardableResult static func method(_ method: HTTPMethod, @@ -31,16 +19,16 @@ extension HookTrigger { trigger: ParseHookTriggerType, parseServerURLStrings: [String]) async throws -> [String: Self] { let url = try buildServerPathname(path) - let hookTrigger: HookTrigger! + let hookTrigger: Self! var hookTriggers = [String: Self]() if let className = className { - hookTrigger = HookTrigger(className: className, - triggerName: trigger, - url: url) + hookTrigger = Self(className: className, + trigger: trigger, + url: url) } else { - hookTrigger = try HookTrigger(triggerName: trigger, - url: url) + hookTrigger = try Self(trigger: trigger, + url: url) } for parseServerURLString in parseServerURLStrings { @@ -90,7 +78,7 @@ extension HookTrigger { } // MARK: HookTrigger - Fetch -public extension HookTrigger { +public extension ParseHookTrigger { /** Fetch a Parse Cloud Code hook trigger. @@ -392,16 +380,16 @@ public extension HookTrigger { // swiftlint:disable:next line_length parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: [Self]] { let url = try buildServerPathname(path) - let hookTrigger: HookTrigger! + let hookTrigger: Self! var hookTriggers = [String: [Self]]() if let className = className { hookTrigger = Self(className: className, - triggerName: trigger, + trigger: trigger, url: url) } else { - hookTrigger = try HookTrigger(triggerName: trigger, - url: url) + hookTrigger = try Self(trigger: trigger, + url: url) } for parseServerURLString in parseServerURLStrings { do { @@ -417,7 +405,7 @@ public extension HookTrigger { } // MARK: HookTrigger - Create -public extension HookTrigger { +public extension ParseHookTrigger { /** Create a Parse Cloud Code hook trigger. @@ -575,7 +563,7 @@ public extension HookTrigger { } // MARK: HookTrigger - Update -public extension HookTrigger { +public extension ParseHookTrigger { /** Update a Parse Cloud Code hook trigger. @@ -733,7 +721,7 @@ public extension HookTrigger { } // MARK: HookTrigger - Delete -public extension HookTrigger { +public extension ParseHookTrigger { /** Delete a Parse Cloud Code hook trigger. @@ -1196,9 +1184,9 @@ public extension RoutesBuilder { let route = self.on(.POST, path, body: body, use: closure) Task { do { - await configuration.hooks.updateTriggers(try await HookTrigger.create(path, - className: className, - trigger: trigger)) + await configuration.hooks.updateTriggers(try await ParseHookTrigger.create(path, + className: className, + trigger: trigger)) } catch { if let className = className { // swiftlint:disable:next line_length diff --git a/Sources/ParseServerSwift/Models/Hooks.swift b/Sources/ParseServerSwift/Models/Hooks.swift index 3ccf7209..ce115f77 100644 --- a/Sources/ParseServerSwift/Models/Hooks.swift +++ b/Sources/ParseServerSwift/Models/Hooks.swift @@ -6,24 +6,25 @@ // import Foundation +import ParseSwift /// An actor containing all of the current Hooks. actor Hooks { - var functions = [String: HookFunction]() - var triggers = [String: HookTrigger]() + var functions = [String: ParseHookFunction]() + var triggers = [String: ParseHookTrigger]() } // MARK: Hook Functions extension Hooks { /// Get all of the current functions. /// - returns: A dictionary where the keys are Parse Server `URL`'s and the respective `HookFunction`. - func getFunctions() -> [String: HookFunction] { + func getFunctions() -> [String: ParseHookFunction] { functions } /// Update curent functions. /// - parameter functions: A dictionary where the keys are Parse Server `URL`'s and the respective `HookFunction`. - func updateFunctions(_ functions: [String: HookFunction]) { + func updateFunctions(_ functions: [String: ParseHookFunction]) { for (url, function) in functions { self.functions[url] = function } @@ -46,14 +47,15 @@ extension Hooks { // MARK: Hook Triggers extension Hooks { /// Get all of the current triggers. - /// - returns: A dictionary where the keys are Parse Server `URL`'s and the respective `HookTrigger`. - func getTriggers() -> [String: HookTrigger] { + /// - returns: A dictionary where the keys are Parse Server `URL`'s and the respective `ParseHookTrigger`. + func getTriggers() -> [String: ParseHookTrigger] { triggers } /// Update curent triggers. - /// - parameter triggers: A dictionary where the keys are Parse Server `URL`'s and the respective `HookTrigger`. - func updateTriggers(_ triggers: [String: HookTrigger]) { + /// - parameter triggers: A dictionary where the keys are Parse Server `URL`'s and the + /// respective `ParseHookTrigger`. + func updateTriggers(_ triggers: [String: ParseHookTrigger]) { for (url, trigger) in triggers { self.triggers[url] = trigger } diff --git a/Tests/ParseServerSwiftTests/AppTests.swift b/Tests/ParseServerSwiftTests/AppTests.swift index 688a4e53..0da52400 100644 --- a/Tests/ParseServerSwiftTests/AppTests.swift +++ b/Tests/ParseServerSwiftTests/AppTests.swift @@ -115,8 +115,8 @@ final class AppTests: XCTestCase { } XCTAssertGreaterThan(configuration.parseServerURLStrings.count, 0) - let function = HookFunction(name: "hello", url: url) - let trigger = try HookTrigger(triggerName: .afterSave, url: url) + let function = ParseHookFunction(name: "hello", url: url) + let trigger = try ParseHookTrigger(trigger: .afterSave, url: url) await configuration.hooks.updateFunctions([ urlString: function ]) await configuration.hooks.updateTriggers([ urlString: trigger ]) @@ -214,8 +214,8 @@ final class AppTests: XCTestCase { let functions = await configuration.hooks.getFunctions() XCTAssertTrue(functions.isEmpty) - let dummyHooks = ["yo": HookFunction(name: "hello", url: nil), - "no": HookFunction(name: "hello", url: nil)] + let dummyHooks = ["yo": ParseHookFunction(name: "hello", url: nil), + "no": ParseHookFunction(name: "hello", url: nil)] await configuration.hooks.updateFunctions(dummyHooks) let functions2 = await configuration.hooks.getFunctions() XCTAssertEqual(functions2.count, 2) @@ -238,8 +238,12 @@ final class AppTests: XCTestCase { XCTFail("Should have unwrapped") return } - let dummyHooks = ["yo": HookTrigger(className: "hello", triggerName: .afterDelete, url: url), - "no": HookTrigger(className: "hello", triggerName: .afterEvent, url: url)] + let dummyHooks = ["yo": ParseHookTrigger(className: "hello", + trigger: .afterDelete, + url: url), + "no": ParseHookTrigger(className: "hello", + trigger: .afterEvent, + url: url)] await configuration.hooks.updateTriggers(dummyHooks) let triggers2 = await configuration.hooks.getTriggers() XCTAssertEqual(triggers2.count, 2)