Skip to content

Commit 5aaf4dd

Browse files
authored
fix: Always use full path for Hook routes (#41)
* fix: Always use full path for Hook routes * lint
1 parent 5f4bde8 commit 5aaf4dd

File tree

3 files changed

+50
-48
lines changed

3 files changed

+50
-48
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,14 @@ struct GameScore: ParseObject {
228228
### Creating New Cloud Code Routes
229229
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).
230230

231+
#### Router Groups and Collections
232+
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:
233+
234+
- 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
235+
- 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
236+
237+
To learn more about creating groups and collections, checkout this [blog](https://alexandrecools.medium.com/vapor-routes-groups-and-collections-5ff920720317).
238+
231239
**Be sure to add `import ParseSwift` and `import ParseServerSwift` to the top of routes.swift**
232240

233241
### Sending Errors From Cloud Code Routes

Sources/ParseServerSwift/Models/HookFunction.swift

+21-28
Original file line numberDiff line numberDiff line change
@@ -290,12 +290,10 @@ public extension RoutesBuilder {
290290
Will log an error for each `parseServerURLString` that returns an error.
291291
*/
292292
@discardableResult
293-
func post<Response>(
294-
_ path: PathComponent...,
295-
name: String,
296-
use closure: @escaping (Request) async throws -> Response
297-
) -> Route
298-
where Response: AsyncResponseEncodable {
293+
func post<Response>(_ path: PathComponent...,
294+
name: String,
295+
use closure: @escaping (Request) async throws -> Response) -> Route
296+
where Response: AsyncResponseEncodable {
299297
self.on(path,
300298
name: name,
301299
use: closure)
@@ -311,12 +309,10 @@ public extension RoutesBuilder {
311309
Will log an error for each `parseServerURLString` that returns an error.
312310
*/
313311
@discardableResult
314-
func post<Response>(
315-
_ path: [PathComponent],
316-
name: String,
317-
use closure: @escaping (Request) async throws -> Response
318-
) -> Route
319-
where Response: AsyncResponseEncodable {
312+
func post<Response>(_ path: [PathComponent],
313+
name: String,
314+
use closure: @escaping (Request) async throws -> Response) -> Route
315+
where Response: AsyncResponseEncodable {
320316
self.on(path,
321317
name: name,
322318
use: closure)
@@ -333,13 +329,11 @@ public extension RoutesBuilder {
333329
Will log an error for each `parseServerURLString` that returns an error.
334330
*/
335331
@discardableResult
336-
func on<Response>(
337-
_ path: PathComponent...,
338-
body: HTTPBodyStreamStrategy = .collect,
339-
name: String,
340-
use closure: @escaping (Request) async throws -> Response
341-
) -> Route
342-
where Response: AsyncResponseEncodable {
332+
func on<Response>(_ path: PathComponent...,
333+
body: HTTPBodyStreamStrategy = .collect,
334+
name: String,
335+
use closure: @escaping (Request) async throws -> Response) -> Route
336+
where Response: AsyncResponseEncodable {
343337
self.on(path,
344338
body: body,
345339
name: name,
@@ -357,22 +351,21 @@ public extension RoutesBuilder {
357351
Will log an error for each `parseServerURLString` that returns an error.
358352
*/
359353
@discardableResult
360-
func on<Response>(
361-
_ path: [PathComponent],
362-
body: HTTPBodyStreamStrategy = .collect,
363-
name: String,
364-
use closure: @escaping (Request) async throws -> Response
365-
) -> Route
366-
where Response: AsyncResponseEncodable {
354+
func on<Response>(_ path: [PathComponent],
355+
body: HTTPBodyStreamStrategy = .collect,
356+
name: String,
357+
use closure: @escaping (Request) async throws -> Response) -> Route
358+
where Response: AsyncResponseEncodable {
359+
let route = self.on(.POST, path, body: body, use: closure)
367360
Task {
368361
do {
369-
await configuration.hooks.updateFunctions(try await HookFunction.create(path,
362+
await configuration.hooks.updateFunctions(try await HookFunction.create(route.path,
370363
name: name))
371364
} catch {
372365
// swiftlint:disable:next line_length
373366
configuration.logger.error("Could not create HookFunction route for path: \(path); name: \(name) on servers: \(configuration.parseServerURLStrings) because of error: \(error)")
374367
}
375368
}
376-
return self.on(.POST, path, body: body, use: closure)
369+
return route
377370
}
378371
}

Sources/ParseServerSwift/Models/HookTrigger.swift

+21-20
Original file line numberDiff line numberDiff line change
@@ -1083,12 +1083,12 @@ public extension RoutesBuilder {
10831083
className: String? = nil,
10841084
triggerName: ParseHookTriggerType,
10851085
use closure: @escaping (Request) async throws -> Response) -> Route
1086-
where Response: AsyncResponseEncodable {
1087-
self.on(path,
1088-
body: body,
1089-
className: className,
1090-
trigger: triggerName,
1091-
use: closure)
1086+
where Response: AsyncResponseEncodable {
1087+
self.on(path,
1088+
body: body,
1089+
className: className,
1090+
trigger: triggerName,
1091+
use: closure)
10921092
}
10931093

10941094
/**
@@ -1110,12 +1110,12 @@ public extension RoutesBuilder {
11101110
className: String? = nil,
11111111
trigger: ParseHookTriggerType,
11121112
use closure: @escaping (Request) async throws -> Response) -> Route
1113-
where Response: AsyncResponseEncodable {
1114-
self.on(path,
1115-
body: body,
1116-
className: className,
1117-
trigger: trigger,
1118-
use: closure)
1113+
where Response: AsyncResponseEncodable {
1114+
self.on(path,
1115+
body: body,
1116+
className: className,
1117+
trigger: trigger,
1118+
use: closure)
11191119
}
11201120

11211121
/**
@@ -1137,12 +1137,12 @@ public extension RoutesBuilder {
11371137
object: V.Type,
11381138
trigger: ParseHookTriggerType,
11391139
use closure: @escaping (Request) async throws -> Response) -> Route
1140-
where Response: AsyncResponseEncodable, V: ParseObject {
1141-
self.on(path,
1142-
body: body,
1143-
className: object.className,
1144-
trigger: trigger,
1145-
use: closure)
1140+
where Response: AsyncResponseEncodable, V: ParseObject {
1141+
self.on(path,
1142+
body: body,
1143+
className: object.className,
1144+
trigger: trigger,
1145+
use: closure)
11461146
}
11471147

11481148
/**
@@ -1192,7 +1192,8 @@ public extension RoutesBuilder {
11921192
className: String? = nil,
11931193
trigger: ParseHookTriggerType,
11941194
use closure: @escaping (Request) async throws -> Response) -> Route
1195-
where Response: AsyncResponseEncodable {
1195+
where Response: AsyncResponseEncodable {
1196+
let route = self.on(.POST, path, body: body, use: closure)
11961197
Task {
11971198
do {
11981199
await configuration.hooks.updateTriggers(try await HookTrigger.create(path,
@@ -1208,7 +1209,7 @@ public extension RoutesBuilder {
12081209
}
12091210
}
12101211
}
1211-
return self.on(.POST, path, body: body, use: closure)
1212+
return route
12121213
}
12131214

12141215
}

0 commit comments

Comments
 (0)