Skip to content

Commit be2c6dd

Browse files
authored
feat: Use new ParseHookFunction and ParseHookTrigger types (#43)
* feat: Use new ParseHookFunction and ParseHookTrigger types * lint
1 parent 7774338 commit be2c6dd

File tree

6 files changed

+64
-81
lines changed

6 files changed

+64
-81
lines changed

Package.resolved

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
"kind" : "remoteSourceControl",
4242
"location" : "https://github.com/netreconlab/Parse-Swift.git",
4343
"state" : {
44-
"revision" : "835083e870b20a7a999b49e5ed3f57c0dc442e89",
45-
"version" : "5.7.4"
44+
"revision" : "66627b55b558698f50220eaae4cea7057cd14450",
45+
"version" : "5.8.0"
4646
}
4747
},
4848
{

Package.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ let package = Package(
1818
dependencies: [
1919
.package(url: "https://github.com/vapor/vapor.git", .upToNextMajor(from: "4.77.0")),
2020
.package(url: "https://github.com/netreconlab/Parse-Swift.git",
21-
.upToNextMajor(from: "5.7.4"))
21+
.upToNextMajor(from: "5.8.0"))
2222
],
2323
targets: [
2424
.target(

Sources/ParseServerSwift/Models/HookFunction.swift renamed to Sources/ParseServerSwift/Extensions/ParseHookFunction+Vapor.swift

+22-33
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// HookFunction.swift
2+
// ParseHookFunction+Vapor.swift
33
//
44
//
55
// Created by Corey Baker on 6/23/22.
@@ -9,29 +9,18 @@ import Foundation
99
import ParseSwift
1010
import Vapor
1111

12-
/**
13-
Parse Hook Functions can be created by conforming to
14-
`ParseHookFunctionable`.
15-
*/
16-
public struct HookFunction: ParseHookFunctionable {
17-
public var functionName: String?
18-
public var url: URL?
19-
20-
public init() {}
21-
}
22-
2312
// MARK: HookFunction - Internal
24-
extension HookFunction {
13+
extension ParseHookFunction {
2514

2615
@discardableResult
2716
static func method(_ method: HTTPMethod,
2817
_ path: [PathComponent],
2918
name: String,
30-
parseServerURLStrings: [String]) async throws -> [String: HookFunction] {
19+
parseServerURLStrings: [String]) async throws -> [String: Self] {
3120
let url = try buildServerPathname(path)
32-
let hookFunction = HookFunction(name: name,
33-
url: url)
34-
var hookFunctions = [String: HookFunction]()
21+
let hookFunction = Self(name: name,
22+
url: url)
23+
var hookFunctions = [String: Self]()
3524

3625
for parseServerURLString in parseServerURLStrings {
3726
do {
@@ -79,7 +68,7 @@ extension HookFunction {
7968
}
8069

8170
// MARK: HookFunction - Fetch
82-
public extension HookFunction {
71+
public extension ParseHookFunction {
8372

8473
/**
8574
Fetches a Parse Cloud Code hook function.
@@ -93,7 +82,7 @@ public extension HookFunction {
9382
*/
9483
static func fetch(_ path: PathComponent...,
9584
name: String,
96-
parseServerURLStrings: [String]) async throws -> [String: HookFunction] {
85+
parseServerURLStrings: [String]) async throws -> [String: Self] {
9786
try await fetch(path, name: name, parseServerURLStrings: parseServerURLStrings)
9887
}
9988

@@ -109,7 +98,7 @@ public extension HookFunction {
10998
*/
11099
static func fetch(_ path: [PathComponent],
111100
name: String,
112-
parseServerURLStrings: [String]) async throws -> [String: HookFunction] {
101+
parseServerURLStrings: [String]) async throws -> [String: Self] {
113102
try await method(.PUT, path, name: name, parseServerURLStrings: parseServerURLStrings)
114103
}
115104

@@ -125,7 +114,7 @@ public extension HookFunction {
125114
*/
126115
static func fetchAll(_ path: PathComponent...,
127116
name: String,
128-
parseServerURLStrings: [String]) async throws -> [String: [HookFunction]] {
117+
parseServerURLStrings: [String]) async throws -> [String: [Self]] {
129118
try await fetchAll(path, name: name, parseServerURLStrings: parseServerURLStrings)
130119
}
131120

@@ -141,11 +130,11 @@ public extension HookFunction {
141130
*/
142131
static func fetchAll(_ path: [PathComponent],
143132
name: String,
144-
parseServerURLStrings: [String]) async throws -> [String: [HookFunction]] {
133+
parseServerURLStrings: [String]) async throws -> [String: [Self]] {
145134
let url = try buildServerPathname(path)
146-
let hookFunction = HookFunction(name: name,
135+
let hookFunction = Self(name: name,
147136
url: url)
148-
var hookFunctions = [String: [HookFunction]]()
137+
var hookFunctions = [String: [Self]]()
149138

150139
for parseServerURLString in parseServerURLStrings {
151140
do {
@@ -161,7 +150,7 @@ public extension HookFunction {
161150
}
162151

163152
// MARK: HookFunction - Create
164-
public extension HookFunction {
153+
public extension ParseHookFunction {
165154

166155
/**
167156
Creates a Parse Cloud Code hook function.
@@ -177,7 +166,7 @@ public extension HookFunction {
177166
static func create(_ path: PathComponent...,
178167
name: String,
179168
// swiftlint:disable:next line_length
180-
parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: HookFunction] {
169+
parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: Self] {
181170
try await create(path, name: name, parseServerURLStrings: parseServerURLStrings)
182171
}
183172

@@ -195,13 +184,13 @@ public extension HookFunction {
195184
static func create(_ path: [PathComponent],
196185
name: String,
197186
// swiftlint:disable:next line_length
198-
parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: HookFunction] {
187+
parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: Self] {
199188
try await method(.POST, path, name: name, parseServerURLStrings: parseServerURLStrings)
200189
}
201190
}
202191

203192
// MARK: HookFunction - Update
204-
public extension HookFunction {
193+
public extension ParseHookFunction {
205194

206195
/**
207196
Updates a Parse Cloud Code hook function.
@@ -217,7 +206,7 @@ public extension HookFunction {
217206
static func update(_ path: PathComponent...,
218207
name: String,
219208
// swiftlint:disable:next line_length
220-
parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: HookFunction] {
209+
parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: Self] {
221210
try await update(path, name: name, parseServerURLStrings: parseServerURLStrings)
222211
}
223212

@@ -235,13 +224,13 @@ public extension HookFunction {
235224
static func update(_ path: [PathComponent],
236225
name: String,
237226
// swiftlint:disable:next line_length
238-
parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: HookFunction] {
227+
parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: Self] {
239228
try await method(.PUT, path, name: name, parseServerURLStrings: parseServerURLStrings)
240229
}
241230
}
242231

243232
// MARK: HookFunction - Delete
244-
public extension HookFunction {
233+
public extension ParseHookFunction {
245234

246235
/**
247236
Removes a Parse Cloud Code hook function.
@@ -359,8 +348,8 @@ public extension RoutesBuilder {
359348
let route = self.on(.POST, path, body: body, use: closure)
360349
Task {
361350
do {
362-
await configuration.hooks.updateFunctions(try await HookFunction.create(route.path,
363-
name: name))
351+
await configuration.hooks.updateFunctions(try await ParseHookFunction.create(route.path,
352+
name: name))
364353
} catch {
365354
// swiftlint:disable:next line_length
366355
configuration.logger.error("Could not create HookFunction route for path: \(path); name: \(name) on servers: \(configuration.parseServerURLStrings) because of error: \(error)")

Sources/ParseServerSwift/Models/HookTrigger.swift renamed to Sources/ParseServerSwift/Extensions/ParseHookTrigger+Vapor.swift

+19-31
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// HookTrigger.swift
2+
// ParseHookTrigger+Vapor.swift
33
//
44
//
55
// Created by Corey Baker on 6/23/22.
@@ -9,20 +9,8 @@ import Foundation
99
import ParseSwift
1010
import Vapor
1111

12-
/**
13-
Parse Hook Triggers can be created by conforming to
14-
`ParseHookTriggerable`.
15-
*/
16-
public struct HookTrigger: ParseHookTriggerable {
17-
public var className: String?
18-
public var triggerName: ParseHookTriggerType?
19-
public var url: URL?
20-
21-
public init() {}
22-
}
23-
2412
// MARK: HookTrigger - Internal
25-
extension HookTrigger {
13+
extension ParseHookTrigger {
2614

2715
@discardableResult
2816
static func method(_ method: HTTPMethod,
@@ -31,16 +19,16 @@ extension HookTrigger {
3119
trigger: ParseHookTriggerType,
3220
parseServerURLStrings: [String]) async throws -> [String: Self] {
3321
let url = try buildServerPathname(path)
34-
let hookTrigger: HookTrigger!
22+
let hookTrigger: Self!
3523
var hookTriggers = [String: Self]()
3624

3725
if let className = className {
38-
hookTrigger = HookTrigger(className: className,
39-
triggerName: trigger,
40-
url: url)
26+
hookTrigger = Self(className: className,
27+
trigger: trigger,
28+
url: url)
4129
} else {
42-
hookTrigger = try HookTrigger(triggerName: trigger,
43-
url: url)
30+
hookTrigger = try Self(trigger: trigger,
31+
url: url)
4432
}
4533

4634
for parseServerURLString in parseServerURLStrings {
@@ -90,7 +78,7 @@ extension HookTrigger {
9078
}
9179

9280
// MARK: HookTrigger - Fetch
93-
public extension HookTrigger {
81+
public extension ParseHookTrigger {
9482

9583
/**
9684
Fetch a Parse Cloud Code hook trigger.
@@ -392,16 +380,16 @@ public extension HookTrigger {
392380
// swiftlint:disable:next line_length
393381
parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: [Self]] {
394382
let url = try buildServerPathname(path)
395-
let hookTrigger: HookTrigger!
383+
let hookTrigger: Self!
396384
var hookTriggers = [String: [Self]]()
397385

398386
if let className = className {
399387
hookTrigger = Self(className: className,
400-
triggerName: trigger,
388+
trigger: trigger,
401389
url: url)
402390
} else {
403-
hookTrigger = try HookTrigger(triggerName: trigger,
404-
url: url)
391+
hookTrigger = try Self(trigger: trigger,
392+
url: url)
405393
}
406394
for parseServerURLString in parseServerURLStrings {
407395
do {
@@ -417,7 +405,7 @@ public extension HookTrigger {
417405
}
418406

419407
// MARK: HookTrigger - Create
420-
public extension HookTrigger {
408+
public extension ParseHookTrigger {
421409

422410
/**
423411
Create a Parse Cloud Code hook trigger.
@@ -575,7 +563,7 @@ public extension HookTrigger {
575563
}
576564

577565
// MARK: HookTrigger - Update
578-
public extension HookTrigger {
566+
public extension ParseHookTrigger {
579567

580568
/**
581569
Update a Parse Cloud Code hook trigger.
@@ -733,7 +721,7 @@ public extension HookTrigger {
733721
}
734722

735723
// MARK: HookTrigger - Delete
736-
public extension HookTrigger {
724+
public extension ParseHookTrigger {
737725

738726
/**
739727
Delete a Parse Cloud Code hook trigger.
@@ -1196,9 +1184,9 @@ public extension RoutesBuilder {
11961184
let route = self.on(.POST, path, body: body, use: closure)
11971185
Task {
11981186
do {
1199-
await configuration.hooks.updateTriggers(try await HookTrigger.create(path,
1200-
className: className,
1201-
trigger: trigger))
1187+
await configuration.hooks.updateTriggers(try await ParseHookTrigger.create(path,
1188+
className: className,
1189+
trigger: trigger))
12021190
} catch {
12031191
if let className = className {
12041192
// swiftlint:disable:next line_length

Sources/ParseServerSwift/Models/Hooks.swift

+10-8
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,25 @@
66
//
77

88
import Foundation
9+
import ParseSwift
910

1011
/// An actor containing all of the current Hooks.
1112
actor Hooks {
12-
var functions = [String: HookFunction]()
13-
var triggers = [String: HookTrigger]()
13+
var functions = [String: ParseHookFunction]()
14+
var triggers = [String: ParseHookTrigger]()
1415
}
1516

1617
// MARK: Hook Functions
1718
extension Hooks {
1819
/// Get all of the current functions.
1920
/// - returns: A dictionary where the keys are Parse Server `URL`'s and the respective `HookFunction`.
20-
func getFunctions() -> [String: HookFunction] {
21+
func getFunctions() -> [String: ParseHookFunction] {
2122
functions
2223
}
2324

2425
/// Update curent functions.
2526
/// - parameter functions: A dictionary where the keys are Parse Server `URL`'s and the respective `HookFunction`.
26-
func updateFunctions(_ functions: [String: HookFunction]) {
27+
func updateFunctions(_ functions: [String: ParseHookFunction]) {
2728
for (url, function) in functions {
2829
self.functions[url] = function
2930
}
@@ -46,14 +47,15 @@ extension Hooks {
4647
// MARK: Hook Triggers
4748
extension Hooks {
4849
/// Get all of the current triggers.
49-
/// - returns: A dictionary where the keys are Parse Server `URL`'s and the respective `HookTrigger`.
50-
func getTriggers() -> [String: HookTrigger] {
50+
/// - returns: A dictionary where the keys are Parse Server `URL`'s and the respective `ParseHookTrigger`.
51+
func getTriggers() -> [String: ParseHookTrigger] {
5152
triggers
5253
}
5354

5455
/// Update curent triggers.
55-
/// - parameter triggers: A dictionary where the keys are Parse Server `URL`'s and the respective `HookTrigger`.
56-
func updateTriggers(_ triggers: [String: HookTrigger]) {
56+
/// - parameter triggers: A dictionary where the keys are Parse Server `URL`'s and the
57+
/// respective `ParseHookTrigger`.
58+
func updateTriggers(_ triggers: [String: ParseHookTrigger]) {
5759
for (url, trigger) in triggers {
5860
self.triggers[url] = trigger
5961
}

Tests/ParseServerSwiftTests/AppTests.swift

+10-6
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ final class AppTests: XCTestCase {
115115
}
116116
XCTAssertGreaterThan(configuration.parseServerURLStrings.count, 0)
117117

118-
let function = HookFunction(name: "hello", url: url)
119-
let trigger = try HookTrigger(triggerName: .afterSave, url: url)
118+
let function = ParseHookFunction(name: "hello", url: url)
119+
let trigger = try ParseHookTrigger(trigger: .afterSave, url: url)
120120

121121
await configuration.hooks.updateFunctions([ urlString: function ])
122122
await configuration.hooks.updateTriggers([ urlString: trigger ])
@@ -214,8 +214,8 @@ final class AppTests: XCTestCase {
214214
let functions = await configuration.hooks.getFunctions()
215215
XCTAssertTrue(functions.isEmpty)
216216

217-
let dummyHooks = ["yo": HookFunction(name: "hello", url: nil),
218-
"no": HookFunction(name: "hello", url: nil)]
217+
let dummyHooks = ["yo": ParseHookFunction(name: "hello", url: nil),
218+
"no": ParseHookFunction(name: "hello", url: nil)]
219219
await configuration.hooks.updateFunctions(dummyHooks)
220220
let functions2 = await configuration.hooks.getFunctions()
221221
XCTAssertEqual(functions2.count, 2)
@@ -238,8 +238,12 @@ final class AppTests: XCTestCase {
238238
XCTFail("Should have unwrapped")
239239
return
240240
}
241-
let dummyHooks = ["yo": HookTrigger(className: "hello", triggerName: .afterDelete, url: url),
242-
"no": HookTrigger(className: "hello", triggerName: .afterEvent, url: url)]
241+
let dummyHooks = ["yo": ParseHookTrigger(className: "hello",
242+
trigger: .afterDelete,
243+
url: url),
244+
"no": ParseHookTrigger(className: "hello",
245+
trigger: .afterEvent,
246+
url: url)]
243247
await configuration.hooks.updateTriggers(dummyHooks)
244248
let triggers2 = await configuration.hooks.getTriggers()
245249
XCTAssertEqual(triggers2.count, 2)

0 commit comments

Comments
 (0)