Frank
Frank copied to clipboard
Query parameters
Am I understanding it correctly that Frank doesn't include the notion of query parameters for routes? They're not part of Nest are they?
I think this would be a part of https://github.com/nestproject/Inquiline, but it appears that it isn't in fact accounted for. I opened this: https://github.com/nestproject/Inquiline/issues/13
Here, throw this is somewhere if you're looking for a quick fix for this.
import Nest
extension RequestType {
/// Returns the query
public var query:Dictionary<String, String> {
// Split the string at the ?
// Then digest the key-value pairs
let pathArr = self.path.characters.split{$0 == "?"}.map(String.init)
let queryArr = pathArr[1].characters.split{$0 == "&"}.map(String.init)
var queries:Dictionary<String, String> = [:]
for (_, element) in queryArr.enumerate() {
let keyValue = element.characters.split{$0 == "="}.map(String.init)
queries[keyValue[0]] = keyValue[1]
}
return queries
}
}
You can now use request.query
.
My solution to adding query parameters include:
- Adding the following in Nest.swift to
RequestType
var query: [String: String] { get }
- Adding the following import, property, function, and function call to Request.swift
// Top of file
import Foundation
// Property with path, body, content... etc
public var query = [String: String]()
// Inside the initializer
self.getQueries()
// Request function definition
mutating func getQueries() {
let splitPath = self.path.components(separatedBy: "?")
self.path = splitPath[0]
guard splitPath.count > 1 else {
return
}
let queries = splitPath[1].components(separatedBy: "&")
for splitQuery in queries {
let query = splitQuery.components(separatedBy: "=")
guard query.count == 2 else {
continue
}
self.query[query[0]] = query[1]
}
}
This solution removes the query from the path, and is accessible via: request.query
as a [String: String]