diff --git a/README.md b/README.md index 86066546..5e50d2d9 100644 --- a/README.md +++ b/README.md @@ -228,6 +228,14 @@ struct GameScore: ParseObject { ### Creating New Cloud Code Routes Adding routes for `ParseHooks` are as simple as adding [routes in Vapor](https://docs.vapor.codes/basics/routing/). `ParseServerSwift` provides some additional methods to routes to easily create and register [Hook Functions](https://parseplatform.org/Parse-Swift/release/documentation/parseswift/parsehookfunctionable) and [Hook Triggers](https://parseplatform.org/Parse-Swift/release/documentation/parseswift/parsehooktriggerable/). All routes should be added to the `routes.swift` file in your project. Example `ParseServerSwift` routes can be found in [ParseServerSwift/Sources/ParseServerSwift/routes.swift](https://github.com/netreconlab/ParseServerSwift/blob/main/Sources/ParseServerSwift/routes.swift). +#### Router Groups and Collections +Since `ParseServerSwift` is a Vapor server, it can be configured a number of different ways to suite your needs. Be sure to read through the [vapor documentation](https://docs.vapor.codes). Some important features you may want to take advantage of are highlighed below: + +- Route [groups](https://docs.vapor.codes/basics/routing/#route-groups) allows you to create a set of routes with a path prefix or specific middleware +- Route [collections](https://legacy.docs.vapor.codes/2.0/routing/collection/) allow multiple routes and route groups to be organized in different files or modules + +To learn more about creating groups and collections, checkout this [blog](https://alexandrecools.medium.com/vapor-routes-groups-and-collections-5ff920720317). + **Be sure to add `import ParseSwift` and `import ParseServerSwift` to the top of routes.swift** ### Sending Errors From Cloud Code Routes diff --git a/Sources/ParseServerSwift/Models/HookFunction.swift b/Sources/ParseServerSwift/Models/HookFunction.swift index fcad18b7..e2f3bd0f 100644 --- a/Sources/ParseServerSwift/Models/HookFunction.swift +++ b/Sources/ParseServerSwift/Models/HookFunction.swift @@ -290,12 +290,10 @@ public extension RoutesBuilder { Will log an error for each `parseServerURLString` that returns an error. */ @discardableResult - func post( - _ path: PathComponent..., - name: String, - use closure: @escaping (Request) async throws -> Response - ) -> Route - where Response: AsyncResponseEncodable { + func post(_ path: PathComponent..., + name: String, + use closure: @escaping (Request) async throws -> Response) -> Route + where Response: AsyncResponseEncodable { self.on(path, name: name, use: closure) @@ -311,12 +309,10 @@ public extension RoutesBuilder { Will log an error for each `parseServerURLString` that returns an error. */ @discardableResult - func post( - _ path: [PathComponent], - name: String, - use closure: @escaping (Request) async throws -> Response - ) -> Route - where Response: AsyncResponseEncodable { + func post(_ path: [PathComponent], + name: String, + use closure: @escaping (Request) async throws -> Response) -> Route + where Response: AsyncResponseEncodable { self.on(path, name: name, use: closure) @@ -333,13 +329,11 @@ public extension RoutesBuilder { Will log an error for each `parseServerURLString` that returns an error. */ @discardableResult - func on( - _ path: PathComponent..., - body: HTTPBodyStreamStrategy = .collect, - name: String, - use closure: @escaping (Request) async throws -> Response - ) -> Route - where Response: AsyncResponseEncodable { + func on(_ path: PathComponent..., + body: HTTPBodyStreamStrategy = .collect, + name: String, + use closure: @escaping (Request) async throws -> Response) -> Route + where Response: AsyncResponseEncodable { self.on(path, body: body, name: name, @@ -357,22 +351,21 @@ public extension RoutesBuilder { Will log an error for each `parseServerURLString` that returns an error. */ @discardableResult - func on( - _ path: [PathComponent], - body: HTTPBodyStreamStrategy = .collect, - name: String, - use closure: @escaping (Request) async throws -> Response - ) -> Route - where Response: AsyncResponseEncodable { + func on(_ path: [PathComponent], + body: HTTPBodyStreamStrategy = .collect, + name: String, + use closure: @escaping (Request) async throws -> Response) -> Route + where Response: AsyncResponseEncodable { + let route = self.on(.POST, path, body: body, use: closure) Task { do { - await configuration.hooks.updateFunctions(try await HookFunction.create(path, + await configuration.hooks.updateFunctions(try await HookFunction.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)") } } - return self.on(.POST, path, body: body, use: closure) + return route } } diff --git a/Sources/ParseServerSwift/Models/HookTrigger.swift b/Sources/ParseServerSwift/Models/HookTrigger.swift index dd487a75..7727c6eb 100644 --- a/Sources/ParseServerSwift/Models/HookTrigger.swift +++ b/Sources/ParseServerSwift/Models/HookTrigger.swift @@ -1083,12 +1083,12 @@ public extension RoutesBuilder { className: String? = nil, triggerName: ParseHookTriggerType, use closure: @escaping (Request) async throws -> Response) -> Route - where Response: AsyncResponseEncodable { - self.on(path, - body: body, - className: className, - trigger: triggerName, - use: closure) + where Response: AsyncResponseEncodable { + self.on(path, + body: body, + className: className, + trigger: triggerName, + use: closure) } /** @@ -1110,12 +1110,12 @@ public extension RoutesBuilder { className: String? = nil, trigger: ParseHookTriggerType, use closure: @escaping (Request) async throws -> Response) -> Route - where Response: AsyncResponseEncodable { - self.on(path, - body: body, - className: className, - trigger: trigger, - use: closure) + where Response: AsyncResponseEncodable { + self.on(path, + body: body, + className: className, + trigger: trigger, + use: closure) } /** @@ -1137,12 +1137,12 @@ public extension RoutesBuilder { object: V.Type, trigger: ParseHookTriggerType, use closure: @escaping (Request) async throws -> Response) -> Route - where Response: AsyncResponseEncodable, V: ParseObject { - self.on(path, - body: body, - className: object.className, - trigger: trigger, - use: closure) + where Response: AsyncResponseEncodable, V: ParseObject { + self.on(path, + body: body, + className: object.className, + trigger: trigger, + use: closure) } /** @@ -1192,7 +1192,8 @@ public extension RoutesBuilder { className: String? = nil, trigger: ParseHookTriggerType, use closure: @escaping (Request) async throws -> Response) -> Route - where Response: AsyncResponseEncodable { + where Response: AsyncResponseEncodable { + let route = self.on(.POST, path, body: body, use: closure) Task { do { await configuration.hooks.updateTriggers(try await HookTrigger.create(path, @@ -1208,7 +1209,7 @@ public extension RoutesBuilder { } } } - return self.on(.POST, path, body: body, use: closure) + return route } }