From a2c01488c48a24214eb4bbdd618613dc98f80b04 Mon Sep 17 00:00:00 2001 From: Petr Fiala Date: Thu, 7 Feb 2019 17:32:12 +0100 Subject: [PATCH] Added possibility to add custom URL parameters --- Spine/Query.swift | 12 ++++++++++++ Spine/Routing.swift | 7 +++++++ 2 files changed, 19 insertions(+) diff --git a/Spine/Query.swift b/Spine/Query.swift index 7b8fe89b..00d0e11c 100644 --- a/Spine/Query.swift +++ b/Spine/Query.swift @@ -41,6 +41,7 @@ public struct Query { public internal(set) var pagination: Pagination? + public internal(set) var customURLparams: [String: String] = [:] //MARK: Init @@ -274,6 +275,17 @@ public struct Query { public mutating func paginate(_ pagination: Pagination?) { self.pagination = pagination } + + + // MARK: Custom URL params + + /// Add custom URL parameter. Pass value=nil to remove parameter. + /// + /// - parameter key: key of added parameter + /// - parameter value: value of added parameter (URL...&key=value&...) + public mutating func addCustomURLparameter(_ key: String, value: String?) { + self.customURLparams[key] = value + } } diff --git a/Spine/Routing.swift b/Spine/Routing.swift index eb4b8851..3892b21e 100644 --- a/Spine/Routing.swift +++ b/Spine/Routing.swift @@ -176,6 +176,13 @@ open class JSONAPIRouter: Router { } } + // Custom URL params + for itemKey in query.customURLparams.keys { + let value = query.customURLparams[itemKey] + let item = URLQueryItem(name: itemKey, value: value) + appendQueryItem(item, to: &queryItems) + } + // Compose URL if !queryItems.isEmpty { urlComponents.queryItems = queryItems