Skip to content

Commit 7c11bd3

Browse files
authored
feat: Add ability to pass routes into configuration (#74)
* feat: Add ability to pass routes into configuration * lint
1 parent 336b0d2 commit 7c11bd3

File tree

8 files changed

+163
-91
lines changed

8 files changed

+163
-91
lines changed

Package.resolved

+10-10
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
"kind" : "remoteSourceControl",
1515
"location" : "https://github.com/vapor/async-kit.git",
1616
"state" : {
17-
"revision" : "7ece208cd401687641c88367a00e3ea2b04311f1",
18-
"version" : "1.19.0"
17+
"revision" : "e048c8ee94967e8d8a1c2ec0e1156d6f7fa34d31",
18+
"version" : "1.20.0"
1919
}
2020
},
2121
{
@@ -41,8 +41,8 @@
4141
"kind" : "remoteSourceControl",
4242
"location" : "https://github.com/netreconlab/Parse-Swift.git",
4343
"state" : {
44-
"revision" : "48b38b15363846e0714bc632c1939f721f71f4b2",
45-
"version" : "5.11.1"
44+
"revision" : "b56de0a0770fb3ac267d3d8d1cc3924fbfbf3d16",
45+
"version" : "5.11.2"
4646
}
4747
},
4848
{
@@ -122,8 +122,8 @@
122122
"kind" : "remoteSourceControl",
123123
"location" : "https://github.com/apple/swift-nio.git",
124124
"state" : {
125-
"revision" : "fc79798d5a150d61361a27ce0c51169b889e23de",
126-
"version" : "2.68.0"
125+
"revision" : "e4abde8be0e49dc7d66e6eed651254accdcd9533",
126+
"version" : "2.69.0"
127127
}
128128
},
129129
{
@@ -140,8 +140,8 @@
140140
"kind" : "remoteSourceControl",
141141
"location" : "https://github.com/apple/swift-nio-http2.git",
142142
"state" : {
143-
"revision" : "a0224f3d20438635dd59c9fcc593520d80d131d0",
144-
"version" : "1.33.0"
143+
"revision" : "b5f7062b60e4add1e8c343ba4eb8da2e324b3a94",
144+
"version" : "1.34.0"
145145
}
146146
},
147147
{
@@ -176,8 +176,8 @@
176176
"kind" : "remoteSourceControl",
177177
"location" : "https://github.com/apple/swift-system.git",
178178
"state" : {
179-
"revision" : "6a9e38e7bd22a3b8ba80bddf395623cf68f57807",
180-
"version" : "1.3.1"
179+
"revision" : "d2ba781702a1d8285419c15ee62fd734a9437ff5",
180+
"version" : "1.3.2"
181181
}
182182
},
183183
{

Package.swift

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.6
1+
// swift-tools-version:5.7
22
import PackageDescription
33

44
// swiftlint:disable line_length
@@ -21,11 +21,11 @@ let package = Package(
2121
dependencies: [
2222
.package(
2323
url: "https://github.com/vapor/vapor.git",
24-
.upToNextMajor(from: "4.102.1")
24+
.upToNextMajor(from: "4.102.1")
2525
),
2626
.package(
2727
url: "https://github.com/netreconlab/Parse-Swift.git",
28-
.upToNextMajor(from: "5.11.1")
28+
.upToNextMajor(from: "5.11.2")
2929
)
3030
],
3131
targets: [
@@ -43,10 +43,14 @@ let package = Package(
4343
// the `.unsafeFlags` construct required by SwiftPM, this flag is recommended for Release
4444
// builds. See <https://github.com/swift-server/guides/blob/main/docs/building.md#building-for-production> for details.
4545
.unsafeFlags(["-cross-module-optimization"], .when(configuration: .release))
46-
]),
47-
.testTarget(name: "ParseServerSwiftTests", dependencies: [
48-
.target(name: "ParseServerSwift"),
49-
.product(name: "XCTVapor", package: "vapor")
50-
])
46+
]
47+
),
48+
.testTarget(
49+
name: "ParseServerSwiftTests",
50+
dependencies: [
51+
.target(name: "ParseServerSwift"),
52+
.product(name: "XCTVapor", package: "vapor")
53+
]
54+
)
5155
]
5256
)

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@ enum Entrypoint {
122122
let executorTakeoverSuccess = NIOSingletons.unsafeTryInstallSingletonPosixEventLoopGroupAsConcurrencyGlobalExecutor()
123123
app.logger.debug("Running with \(executorTakeoverSuccess ? "SwiftNIO" : "standard") Swift Concurrency default executor")
124124

125-
try await parseServerSwiftConfigure(app)
125+
try await parseServerSwiftConfigure(
126+
app,
127+
using: exampleRoutes
128+
)
126129
try await app.execute()
127130
try await app.asyncShutdown()
128131
}

Sources/App/entrypoint.swift

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ enum Entrypoint {
2828
"Running with \(executorTakeoverSuccess ? "SwiftNIO" : "standard") Swift Concurrency default executor"
2929
)
3030

31-
try await parseServerSwiftConfigure(app)
31+
try await parseServerSwiftConfigure(
32+
app,
33+
using: exampleRoutes
34+
)
3235
try await app.execute()
3336
try await app.asyncShutdown()
3437
}

Sources/ParseServerSwift/Parse.swift

+27-16
Original file line numberDiff line numberDiff line change
@@ -30,42 +30,53 @@ public var configuration: ParseServerConfiguration {
3030
server URL.
3131
- warning: Be sure to call this method before calling `try routes(app)`.
3232
*/
33-
public func initialize(_ configuration: ParseServerConfiguration,
34-
app: Application) async throws {
33+
public func initialize(
34+
_ configuration: ParseServerConfiguration,
35+
app: Application
36+
) async throws {
3537
try await initializeServer(configuration, app: app)
3638
}
3739

38-
func initialize(_ configuration: ParseServerConfiguration,
39-
app: Application,
40-
testing: Bool) async throws {
40+
func initialize(
41+
_ configuration: ParseServerConfiguration,
42+
app: Application,
43+
testing: Bool
44+
) async throws {
4145
var configuration = configuration
4246
configuration.isTesting = testing
4347
try await initialize(configuration, app: app)
4448
}
4549

46-
func initializeServer(_ configuration: ParseServerConfiguration,
47-
app: Application) async throws {
50+
func initializeServer(
51+
_ configuration: ParseServerConfiguration,
52+
app: Application
53+
) async throws {
4854

4955
// Parse uses tailored encoders/decoders. These can be retrieved from any ParseObject
5056
ContentConfiguration.global.use(encoder: User.getEncoder(), for: .json)
5157
ContentConfiguration.global.use(decoder: User.getDecoder(), for: .json)
5258

5359
guard let parseServerURL = URL(string: configuration.primaryParseServerURLString) else {
54-
throw ParseError(code: .otherCause,
55-
message: "Could not make a URL from the Parse Server string")
60+
let error = ParseError(
61+
code: .otherCause,
62+
message: "Could not make a URL from the Parse Server string"
63+
)
64+
throw error
5665
}
5766

5867
if !configuration.isTesting {
5968
try setConfiguration(configuration)
6069
do {
6170
// Initialize the Parse-Swift SDK. Add any additional parameters you need
62-
try await ParseSwift.initialize(applicationId: configuration.applicationId,
63-
primaryKey: configuration.primaryKey,
64-
serverURL: parseServerURL,
65-
// POST all queries instead of using GET.
66-
usingPostForQuery: true,
67-
// Do not use cache for anything.
68-
requestCachePolicy: .reloadIgnoringLocalCacheData) { _, completionHandler in
71+
try await ParseSwift.initialize(
72+
applicationId: configuration.applicationId,
73+
primaryKey: configuration.primaryKey,
74+
serverURL: parseServerURL,
75+
// POST all queries instead of using GET.
76+
usingPostForQuery: true,
77+
// Do not use cache for anything.
78+
requestCachePolicy: .reloadIgnoringLocalCacheData
79+
) { _, completionHandler in
6980
// Setup to use default certificate pinning. See Parse-Swift docs for more info
7081
completionHandler(.performDefaultHandling, nil)
7182
}

Sources/ParseServerSwift/configure.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import Vapor
99
*/
1010
public func parseServerSwiftConfigure(
1111
_ app: Application,
12-
with configuration: ParseServerConfiguration? = nil
12+
with configuration: ParseServerConfiguration? = nil,
13+
using routes: ((Application) throws -> Void)
1314
) async throws {
1415
// Initialize ParseServerSwift
1516
let configuration = try configuration ?? ParseServerConfiguration(app: app)

Sources/ParseServerSwift/routes.swift

+61-26
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Vapor
22
import ParseSwift
33

44
// swiftlint:disable:next cyclomatic_complexity function_body_length
5-
func routes(_ app: Application) throws {
5+
public func exampleRoutes(_ app: Application) throws {
66

77
// A typical route in Vapor.
88
app.get { req in
@@ -15,8 +15,10 @@ func routes(_ app: Application) throws {
1515
}
1616

1717
// A simple Parse Hook Function route that returns "Hello World".
18-
app.post("hello",
19-
name: "hello") { req async throws -> ParseHookResponse<String> in
18+
app.post(
19+
"hello",
20+
name: "hello"
21+
) { req async throws -> ParseHookResponse<String> in
2022
// Note that `ParseHookResponse<String>` means a "successful"
2123
// response will return a "String" type.
2224
if let error: ParseHookResponse<String> = checkHeaders(req) {
@@ -39,8 +41,10 @@ func routes(_ app: Application) throws {
3941
}
4042

4143
// Another simple Parse Hook Function route that returns the version of the server.
42-
app.post("version",
43-
name: "version") { req async throws -> ParseHookResponse<String> in
44+
app.post(
45+
"version",
46+
name: "version"
47+
) { req async throws -> ParseHookResponse<String> in
4448
// Note that `ParseHookResponse<String>` means a "successful"
4549
// response will return a "String" type.
4650
if let error: ParseHookResponse<String> = checkHeaders(req) {
@@ -89,9 +93,13 @@ func routes(_ app: Application) throws {
8993
}
9094

9195
// A Parse Hook Trigger route.
92-
app.post("score", "save", "before",
93-
object: GameScore.self,
94-
trigger: .beforeSave) { req async throws -> ParseHookResponse<GameScore> in
96+
app.post(
97+
"score",
98+
"save",
99+
"before",
100+
object: GameScore.self,
101+
trigger: .beforeSave
102+
) { req async throws -> ParseHookResponse<GameScore> in
95103
// Note that `ParseHookResponse<GameScore>` means a "successful"
96104
// response will return a "GameScore" type.
97105
if let error: ParseHookResponse<GameScore> = checkHeaders(req) {
@@ -117,9 +125,13 @@ func routes(_ app: Application) throws {
117125
}
118126

119127
// Another Parse Hook Trigger route.
120-
app.post("score", "find", "before",
121-
object: GameScore.self,
122-
trigger: .beforeFind) { req async throws -> ParseHookResponse<[GameScore]> in
128+
app.post(
129+
"score",
130+
"find",
131+
"before",
132+
object: GameScore.self,
133+
trigger: .beforeFind
134+
) { req async throws -> ParseHookResponse<[GameScore]> in
123135
// Note that `ParseHookResponse<[GameScore]>` means a "successful"
124136
// response will return a "[GameScore]" type.
125137
if let error: ParseHookResponse<[GameScore]> = checkHeaders(req) {
@@ -144,9 +156,13 @@ func routes(_ app: Application) throws {
144156
}
145157

146158
// Another Parse Hook Trigger route.
147-
app.post("user", "login", "after",
148-
object: User.self,
149-
trigger: .afterLogin) { req async throws -> ParseHookResponse<Bool> in
159+
app.post(
160+
"user",
161+
"login",
162+
"after",
163+
object: User.self,
164+
trigger: .afterLogin
165+
) { req async throws -> ParseHookResponse<Bool> in
150166
// Note that `ParseHookResponse<Bool>` means a "successful"
151167
// response will return a "Bool" type. Bool is the standard response with
152168
// a "true" response meaning everything is okay or continue.
@@ -161,8 +177,12 @@ func routes(_ app: Application) throws {
161177
}
162178

163179
// A Parse Hook Trigger route for `ParseFile`.
164-
app.on("file", "save", "before",
165-
trigger: .beforeSave) { req async throws -> ParseHookResponse<Bool> in
180+
app.on(
181+
"file",
182+
"save",
183+
"before",
184+
trigger: .beforeSave
185+
) { req async throws -> ParseHookResponse<Bool> in
166186
// Note that `ParseHookResponse<Bool>` means a "successful"
167187
// response will return a "Bool" type. Bool is the standard response with
168188
// a "true" response meaning everything is okay or continue. Sending "false"
@@ -178,8 +198,12 @@ func routes(_ app: Application) throws {
178198
}
179199

180200
// Another Parse Hook Trigger route for `ParseFile`.
181-
app.post("file", "delete", "before",
182-
trigger: .beforeDelete) { req async throws -> ParseHookResponse<Bool> in
201+
app.post(
202+
"file",
203+
"delete",
204+
"before",
205+
trigger: .beforeDelete
206+
) { req async throws -> ParseHookResponse<Bool> in
183207
// Note that `ParseHookResponse<Bool>` means a "successful"
184208
// response will return a "Bool" type. Bool is the standard response with
185209
// a "true" response meaning everything is okay or continue.
@@ -194,8 +218,11 @@ func routes(_ app: Application) throws {
194218
}
195219

196220
// A Parse Hook Trigger route for `ParseLiveQuery`.
197-
app.post("connect", "before",
198-
trigger: .beforeConnect) { req async throws -> ParseHookResponse<Bool> in
221+
app.post(
222+
"connect",
223+
"before",
224+
trigger: .beforeConnect
225+
) { req async throws -> ParseHookResponse<Bool> in
199226
// Note that `ParseHookResponse<Bool>` means a "successful"
200227
// response will return a "Bool" type. Bool is the standard response with
201228
// a "true" response meaning everything is okay or continue.
@@ -210,9 +237,13 @@ func routes(_ app: Application) throws {
210237
}
211238

212239
// Another Parse Hook Trigger route for `ParseLiveQuery`.
213-
app.post("score", "subscribe", "before",
214-
object: GameScore.self,
215-
trigger: .beforeSubscribe) { req async throws -> ParseHookResponse<Bool> in
240+
app.post(
241+
"score",
242+
"subscribe",
243+
"before",
244+
object: GameScore.self,
245+
trigger: .beforeSubscribe
246+
) { req async throws -> ParseHookResponse<Bool> in
216247
// Note that `ParseHookResponse<Bool>` means a "successful"
217248
// response will return a "Bool" type. Bool is the standard response with
218249
// a "true" response meaning everything is okay or continue.
@@ -227,9 +258,13 @@ func routes(_ app: Application) throws {
227258
}
228259

229260
// Another Parse Hook Trigger route for `ParseLiveQuery`.
230-
app.post("score", "event", "after",
231-
object: GameScore.self,
232-
trigger: .afterEvent) { req async throws -> ParseHookResponse<Bool> in
261+
app.post(
262+
"score",
263+
"event",
264+
"after",
265+
object: GameScore.self,
266+
trigger: .afterEvent
267+
) { req async throws -> ParseHookResponse<Bool> in
233268
// Note that `ParseHookResponse<Bool>` means a "successful"
234269
// response will return a "Bool" type. Bool is the standard response with
235270
// a "true" response meaning everything is okay or continue.

0 commit comments

Comments
 (0)