Skip to content

Commit

Permalink
fix: Always use full path for Hook routes (#41)
Browse files Browse the repository at this point in the history
* fix: Always use full path for Hook routes

* lint
  • Loading branch information
cbaker6 committed Jun 23, 2023
1 parent 5f4bde8 commit 5aaf4dd
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 48 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 21 additions & 28 deletions Sources/ParseServerSwift/Models/HookFunction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,10 @@ public extension RoutesBuilder {
Will log an error for each `parseServerURLString` that returns an error.
*/
@discardableResult
func post<Response>(
_ path: PathComponent...,
name: String,
use closure: @escaping (Request) async throws -> Response
) -> Route
where Response: AsyncResponseEncodable {
func post<Response>(_ path: PathComponent...,
name: String,
use closure: @escaping (Request) async throws -> Response) -> Route
where Response: AsyncResponseEncodable {
self.on(path,
name: name,
use: closure)
Expand All @@ -311,12 +309,10 @@ public extension RoutesBuilder {
Will log an error for each `parseServerURLString` that returns an error.
*/
@discardableResult
func post<Response>(
_ path: [PathComponent],
name: String,
use closure: @escaping (Request) async throws -> Response
) -> Route
where Response: AsyncResponseEncodable {
func post<Response>(_ path: [PathComponent],
name: String,
use closure: @escaping (Request) async throws -> Response) -> Route
where Response: AsyncResponseEncodable {
self.on(path,
name: name,
use: closure)
Expand All @@ -333,13 +329,11 @@ public extension RoutesBuilder {
Will log an error for each `parseServerURLString` that returns an error.
*/
@discardableResult
func on<Response>(
_ path: PathComponent...,
body: HTTPBodyStreamStrategy = .collect,
name: String,
use closure: @escaping (Request) async throws -> Response
) -> Route
where Response: AsyncResponseEncodable {
func on<Response>(_ 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,
Expand All @@ -357,22 +351,21 @@ public extension RoutesBuilder {
Will log an error for each `parseServerURLString` that returns an error.
*/
@discardableResult
func on<Response>(
_ path: [PathComponent],
body: HTTPBodyStreamStrategy = .collect,
name: String,
use closure: @escaping (Request) async throws -> Response
) -> Route
where Response: AsyncResponseEncodable {
func on<Response>(_ 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
}
}
41 changes: 21 additions & 20 deletions Sources/ParseServerSwift/Models/HookTrigger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/**
Expand All @@ -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)
}

/**
Expand All @@ -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)
}

/**
Expand Down Expand Up @@ -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,
Expand All @@ -1208,7 +1209,7 @@ public extension RoutesBuilder {
}
}
}
return self.on(.POST, path, body: body, use: closure)
return route
}

}

0 comments on commit 5aaf4dd

Please sign in to comment.