Then add the VaporRequestStorage to the dependencies array of any target you want access to it in.
Complete the installation by running vapor update or swift package update from the command-line.
Usage
In the configure.swift file, import VaporRequestStorage. Then register the provider with the services object passed into the configure(_:_:_:) function:
try service.register(StorageProvider())
You can now use the Storage service and a privateContainer to store data in a request.
To set a value, there is a request.set(_:to:) method:
try request.set("key", to: value)
To get the value later, use the request.get(_:as:) method:
try request.get("key", as: Value.self)
The second parameter of the get method has a default value of Stored.self, which means if you specify the return type somewhere else, you can skip passing that value in:
Vapor Request Storage
This package works as a replacement for
request.storagewhich was available in Vapor 1 & 2.Installation
Add the package declaration to your project’s manifest
dependenciesarray:Then add the
VaporRequestStorageto thedependenciesarray of any target you want access to it in.Complete the installation by running
vapor updateorswift package updatefrom the command-line.Usage
In the
configure.swiftfile, importVaporRequestStorage. Then register the provider with theservicesobject passed into theconfigure(_:_:_:)function:You can now use the
Storageservice and aprivateContainerto store data in a request.To set a value, there is a
request.set(_:to:)method:To get the value later, use the
request.get(_:as:)method:The second parameter of the
getmethod has a default value ofStored.self, which means if you specify the return type somewhere else, you can skip passing that value in: