A middleware for Vapor 4 routing to trim a slash in url path
It’s a common thing for a website or a backend to allow URLs like mySite.com/webpage and mySite.Com/webpage/ for the same page. These pages are different URLs for a search engine. If you want to avoid duplicates, you can add a simple middleware to trim the ending slash and redirect a user.
Here’s an example code for such a middleware class for Vapor 4:
final class TrimSlashInPathMiddleware: Middleware {
func respond(to request: Request, chainingTo next: Responder) -> EventLoopFuture<Response> {
if request.url.path.count > 1, request.url.path.hasSuffix("/") {
let newPath = String(request.url.path.trimmingSuffix(while: { $0 == "/" }))
let response = request.redirect(to: newPath, redirectType: .permanent)
return request.eventLoop.makeSucceededFuture(response)
}
return next.respond(to: request)
}
}
Just add it to configure.swift file:
import Vapor
public func configure(_ app: Application) throws {
app.middleware.use(TrimSlashInPathMiddleware())
app.http.server.configuration.port = 8081
try routes(app)
}
Works with Vapor 4.92.5.
How to get selected text inside of TextEditor in SwiftUI on macOS
TextEditor in SwiftUI still doesn’t have any API to get user selection. But since it’s using NSTextView internally we can subscribe to its notifications.
Here’s an example of getting a substring from user selection:
Swift CouchDB client 1.3.2
Recently, there have been a couple of new releases for my Swift CouchDB library. Here are the recent changes:
- dateDecodingStrategy and dateEncodingStrategy can now be passed as parameters for get, update and insert methods.
- Added a check to handle expired authentication cookies.
- Comparing the set-cookie header in the response as lowercased.
- Introduced new methods to utilize the _find API, allowing you to find documents using a declarative JSON querying syntax
- Fixed an issue where the update method didn’t use dateEncodingStrategy parameter.
CouchDB Client on GitHub | Documentation with examples and tutorials.
Swift CouchDB client 1.2.1

Just a small update for Swift CouchDB client lib with couple new methods that I needed by myself:
- Added a new method to create a database [docs].
- Added a new method to delete a database [docs].
- Added a new method to check if a database exists [docs].
- Every request handles unauthorised errors now.
- CouchDBClientError model has a description text now.
CouchDB Client on GitHub | Documentation with examples and tutorials.
Swift CouchDB client 1.2.0

Couple months ago I started learning Apple's DocC tool that generates documentation from your source code. I've decided to use as many features as possible so I took my small lib CouchDB Client and added docs to every method including usage examples that Xcode will show in the autocomplete popup. As it often happens, during adding docs and examples I found that many things in the library can be done in a much better way. So I've updated existing methods and added some more that can take a doc as a param and use generic types.
Next step was tutorials. Apple allows devs to create exactly the same tutorials as they have for SwiftUI on their own website. So I've added a couple. They're also part of the repo on GitHub.
Pretty sure that I've spent more time on the documentation than on the lib itself but I hope it's worth it.
CouchDB Client on GitHub | Documentation with examples and tutorials.
Password generation using Security Framework on iOS and macOS
Couple lines of code to generate a 15 characters length password (just like in Safari):
import Security
let pass = SecCreateSharedWebCredentialPassword() as String?
print(pass as Any)
Swift CouchDB client 1.0.0
Finished new version of Swift CouchDB client. Now it's using only async-http-client as a dependency to make http/https requests. Can be used with Vapor 4.
Available on Github: https://github.com/makoni/couchdb-swift