Skip to content

Commit 48aa9af

Browse files
authored
feat: Add configuration to framework (#16)
* feat: Add configuration to framework * Update README.md * Update README.md * Update README.md * Update README.md * nits
1 parent 1ca2997 commit 48aa9af

File tree

13 files changed

+501
-287
lines changed

13 files changed

+501
-287
lines changed

README.md

+96-43
Large diffs are not rendered by default.

Sources/ParseServerSwift/Documentation.docc/ParseServerSwift.md

+9-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ Write Parse Cloud Code in Swift!
55
## Overview
66

77
What is Cloud Code? For complex apps, sometimes you just need logic that isn’t running on a mobile device. Cloud Code makes this possible.
8-
Cloud Code in ParseServerSwift is easy to use because it’s built using [Parse-Swift](https://github.com/parse-community/Parse-Swift)
9-
and [Vapor](https://github.com/vapor/vapor). The only difference is that this code runs in your ParseServerSwift rather than running on the user’s mobile device. When you update your Cloud Code,
10-
it becomes available to all mobile environments instantly. You don’t have to wait for a new release of your application.
11-
This lets you change app behavior on the fly and add new features faster.
8+
Cloud Code in `ParseServerSwift` is easy to use because it’s built using [Parse-Swift<sup>OG</sup>](https://github.com/netreconlab/Parse-Swift)
9+
and [Vapor](https://github.com/vapor/vapor). `ParseServerSwift` provides many additional benefits over the traditional [JS based Cloud Code](https://docs.parseplatform.org/cloudcode/guide/) that runs on the [Node.js parse-server](https://github.com/parse-community/parse-server):
10+
11+
* Write code with the [Parse-Swift SDK](https://github.com/netreconlab/Parse-Swift) vs the [Parse JS SDK](https://github.com/parse-community/Parse-SDK-JS) allowing you to take advantage of a modern SDK which is strongly typed
12+
* Runs on a dedicated server/container, allowing the [Node.js parse-server](https://github.com/parse-community/parse-server) to focus on request reducing the burden by offloading intensive tasks and providing a true [microservice](https://microservices.io)
13+
* All Cloud Code is in one place, but automatically connects supports the [Node.js parse-server](https://github.com/parse-community/parse-server) at scale. This circumvents the issues faced when using [JS based Cloud Code](https://docs.parseplatform.org/cloudcode/guide/) with [PM2](https://pm2.keymetrics.io)
14+
* Leverage the capabilities of [server-side-swift](https://www.swift.org/server/) with [Vapor](https://github.com/vapor/vapor)
15+
16+
Technically, complete apps can be written with `ParseServerSwift`, the only difference is that this code runs in your `ParseServerSwift` rather than running on the user’s mobile device. When you update your Cloud Code, it becomes available to all mobile environments instantly. You don’t have to wait for a new release of your application. This lets you change app behavior on the fly and add new features faster.

Sources/ParseServerSwift/Extensions/Parse+Vapor.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public extension ParseHookRequestable {
2323
In a single Parse Server environment, use options().
2424
*/
2525
func options(_ request: Request,
26-
parseServerURLStrings: [String]) throws -> API.Options {
26+
parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) throws -> API.Options {
2727
var options = self.options()
2828
options.insert(.serverURL(try serverURLString(request.url,
2929
parseServerURLStrings: parseServerURLStrings)))
@@ -35,14 +35,15 @@ public extension ParseHookRequestable {
3535
- parameter options: A set of header options sent to the server. Defaults to an empty set.
3636
- parameter request: The HTTP request of the application.
3737
- parameter parseServerURLStrings: A set of Parse Server `URL`'s.
38+
Defaults to the set of servers added during configuration.
3839
- returns: Returns the `ParseHookRequestable` with the hydrated `ParseCloudUser`.
3940
- throws: An error of type `ParseError`.
4041
- note: The default cache policy for this method is `.reloadIgnoringLocalCacheData`. If a developer
4142
desires a different policy, it should be inserted in `options`.
4243
*/
4344
func hydrateUser(options: API.Options = [],
4445
request: Request,
45-
parseServerURLStrings: [String]) async throws -> Self {
46+
parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> Self {
4647
var updatedOptions = try self.options(request, parseServerURLStrings: parseServerURLStrings)
4748
updatedOptions = updatedOptions.union(options)
4849
return try await withCheckedThrowingContinuation { continuation in

Sources/ParseServerSwift/Models/HookFunction.swift

+19-28
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ extension HookFunction {
5252
throw ParseError(code: .otherCause,
5353
message: "Method \(method) is not supported for Hook Function: \"\(String(describing: hookFunction))\"")
5454
}
55-
logger.notice("Successful \(method); Hook Function: \"\(String(describing: hookFunction))\" on server: \(parseServerURLString)")
55+
configuration.logger.notice("Successful \(method); Hook Function: \"\(String(describing: hookFunction))\" on server: \(parseServerURLString)")
5656
} catch {
5757
if error.containedIn([.webhookError]) && method == .POST {
58-
logger.warning("Hook Function: \"\(String(describing: hookFunction))\"; warning: \(error); on server: \(parseServerURLString)")
58+
configuration.logger.warning("Hook Function: \"\(String(describing: hookFunction))\"; warning: \(error); on server: \(parseServerURLString)")
5959
} else {
60-
logger.error("Could not \(method) Hook Function: \"\(String(describing: hookFunction))\"; error: \(error); on server: \(parseServerURLString)")
60+
configuration.logger.error("Could not \(method) Hook Function: \"\(String(describing: hookFunction))\"; error: \(error); on server: \(parseServerURLString)")
6161
}
6262
}
6363
}
@@ -140,7 +140,7 @@ public extension HookFunction {
140140
hookFunctions[parseServerURLString] = try await hookFunction
141141
.fetchAll(options: [.serverURL(parseServerURLString)])
142142
} catch {
143-
logger.error("Could not fetchAll function: \"\(String(describing: hookFunction))\"; error: \(error); on server: \(parseServerURLString)")
143+
configuration.logger.error("Could not fetchAll function: \"\(String(describing: hookFunction))\"; error: \(error); on server: \(parseServerURLString)")
144144
}
145145
}
146146
return hookFunctions
@@ -155,14 +155,15 @@ public extension HookFunction {
155155
- parameter path: A variadic list of paths.
156156
- parameter name: The name of the function.
157157
- parameter parseServerURLStrings: A set of Parse Server `URL`'s to create hook functions for.
158+
Defaults to the set of servers added during configuration.
158159
- returns: A dictionary where the keys are Parse Server `URL`'s and the respective `HookFunction`.
159160
- throws: An error of `ParseError` type.
160161
- note: WIll attempt to create functions on all `parseServerURLStrings`.
161162
Will log an error for each `parseServerURLString` that returns an error.
162163
*/
163164
static func create(_ path: PathComponent...,
164165
name: String,
165-
parseServerURLStrings: [String]) async throws -> [String: HookFunction] {
166+
parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: HookFunction] {
166167
try await create(path, name: name, parseServerURLStrings: parseServerURLStrings)
167168
}
168169

@@ -171,14 +172,15 @@ public extension HookFunction {
171172
- parameter path: An array of paths.
172173
- parameter name: The name of the function.
173174
- parameter parseServerURLStrings: A set of Parse Server `URL`'s to create hook functions for.
175+
Defaults to the set of servers added during configuration.
174176
- returns: A dictionary where the keys are Parse Server `URL`'s and the respective `HookFunction`.
175177
- throws: An error of `ParseError` type.
176178
- note: WIll attempt to create functions on all `parseServerURLStrings`.
177179
Will log an error for each `parseServerURLString` that returns an error.
178180
*/
179181
static func create(_ path: [PathComponent],
180182
name: String,
181-
parseServerURLStrings: [String]) async throws -> [String: HookFunction] {
183+
parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: HookFunction] {
182184
try await method(.POST, path, name: name, parseServerURLStrings: parseServerURLStrings)
183185
}
184186
}
@@ -191,14 +193,15 @@ public extension HookFunction {
191193
- parameter path: A variadic list of paths.
192194
- parameter name: The name of the function.
193195
- parameter parseServerURLStrings: A set of Parse Server `URL`'s to create hook functions for.
196+
Defaults to the set of servers added during configuration.
194197
- returns: A dictionary where the keys are Parse Server `URL`'s and the respective `HookFunction`.
195198
- throws: An error of `ParseError` type.
196199
- note: WIll attempt to create functions on all `parseServerURLStrings`.
197200
Will log an error for each `parseServerURLString` that returns an error.
198201
*/
199202
static func update(_ path: PathComponent...,
200203
name: String,
201-
parseServerURLStrings: [String]) async throws -> [String: HookFunction] {
204+
parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: HookFunction] {
202205
try await update(path, name: name, parseServerURLStrings: parseServerURLStrings)
203206
}
204207

@@ -207,14 +210,15 @@ public extension HookFunction {
207210
- parameter path: An array of paths.
208211
- parameter name: The name of the function.
209212
- parameter parseServerURLStrings: A set of Parse Server `URL`'s to create hook functions for.
213+
Defaults to the set of servers added during configuration.
210214
- returns: A dictionary where the keys are Parse Server `URL`'s and the respective `HookFunction`.
211215
- throws: An error of `ParseError` type.
212216
- note: WIll attempt to create functions on all `parseServerURLStrings`.
213217
Will log an error for each `parseServerURLString` that returns an error.
214218
*/
215219
static func update(_ path: [PathComponent],
216220
name: String,
217-
parseServerURLStrings: [String]) async throws -> [String: HookFunction] {
221+
parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: HookFunction] {
218222
try await method(.PUT, path, name: name, parseServerURLStrings: parseServerURLStrings)
219223
}
220224
}
@@ -227,13 +231,14 @@ public extension HookFunction {
227231
- parameter path: A variadic list of paths.
228232
- parameter name: The name of the function.
229233
- parameter parseServerURLStrings: A set of Parse Server `URL`'s to create hook functions for.
234+
Defaults to the set of servers added during configuration.
230235
- throws: An error of `ParseError` type.
231236
- note: WIll attempt to create functions on all `parseServerURLStrings`.
232237
Will log an error for each `parseServerURLString` that returns an error.
233238
*/
234239
static func delete(_ path: PathComponent...,
235240
name: String,
236-
parseServerURLStrings: [String]) async throws {
241+
parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws {
237242
try await delete(path, name: name, parseServerURLStrings: parseServerURLStrings)
238243
}
239244

@@ -243,12 +248,13 @@ public extension HookFunction {
243248
- parameter name: The name of the function.
244249
- parameter parseServerURLStrings: A set of Parse Server `URL`'s to create hook functions for.
245250
- throws: An error of `ParseError` type.
251+
Defaults to the set of servers added during configuration.
246252
- note: WIll attempt to create functions on all `parseServerURLStrings`.
247253
Will log an error for each `parseServerURLString` that returns an error.
248254
*/
249255
static func delete(_ path: [PathComponent],
250256
name: String,
251-
parseServerURLStrings: [String]) async throws {
257+
parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws {
252258
try await method(.DELETE, path, name: name, parseServerURLStrings: parseServerURLStrings)
253259
}
254260
}
@@ -268,16 +274,12 @@ public extension RoutesBuilder {
268274
func post<Response>(
269275
_ path: PathComponent...,
270276
name: String,
271-
parseServerURLStrings: [String],
272-
hooks: Hooks,
273277
use closure: @escaping (Request) async throws -> Response
274278
) -> Route
275279
where Response: AsyncResponseEncodable
276280
{
277281
self.on(path,
278282
name: name,
279-
parseServerURLStrings: parseServerURLStrings,
280-
hooks: hooks,
281283
use: closure)
282284
}
283285

@@ -294,16 +296,12 @@ public extension RoutesBuilder {
294296
func post<Response>(
295297
_ path: [PathComponent],
296298
name: String,
297-
parseServerURLStrings: [String],
298-
hooks: Hooks,
299299
use closure: @escaping (Request) async throws -> Response
300300
) -> Route
301301
where Response: AsyncResponseEncodable
302302
{
303303
self.on(path,
304304
name: name,
305-
parseServerURLStrings: parseServerURLStrings,
306-
hooks: hooks,
307305
use: closure)
308306
}
309307

@@ -322,17 +320,13 @@ public extension RoutesBuilder {
322320
_ path: PathComponent...,
323321
body: HTTPBodyStreamStrategy = .collect,
324322
name: String,
325-
parseServerURLStrings: [String],
326-
hooks: Hooks,
327323
use closure: @escaping (Request) async throws -> Response
328324
) -> Route
329325
where Response: AsyncResponseEncodable
330326
{
331327
self.on(path,
332328
body: body,
333329
name: name,
334-
parseServerURLStrings: parseServerURLStrings,
335-
hooks: hooks,
336330
use: closure)
337331
}
338332

@@ -351,19 +345,16 @@ public extension RoutesBuilder {
351345
_ path: [PathComponent],
352346
body: HTTPBodyStreamStrategy = .collect,
353347
name: String,
354-
parseServerURLStrings: [String],
355-
hooks: Hooks,
356348
use closure: @escaping (Request) async throws -> Response
357349
) -> Route
358350
where Response: AsyncResponseEncodable
359351
{
360352
Task {
361353
do {
362-
await hooks.updateFunctions(try await HookFunction.create(path,
363-
name: name,
364-
parseServerURLStrings: parseServerURLStrings))
354+
await configuration.hooks.updateFunctions(try await HookFunction.create(path,
355+
name: name))
365356
} catch {
366-
logger.error("Could not create HookFunction route for path: \(path); name: \(name) on servers: \(parseServerURLStrings) because of error: \(error)")
357+
configuration.logger.error("Could not create HookFunction route for path: \(path); name: \(name) on servers: \(configuration.parseServerURLStrings) because of error: \(error)")
367358
}
368359
}
369360
return self.on(.POST, path, body: body, use: closure)

0 commit comments

Comments
 (0)