If you set log to true you will receive helpful logs in case anything goes wrong.
Adding the Service
Instantiate and register NStackProvider with config created in the previous step.
If you plan on using the leaf tag (see below), make sure to use a synchronous cache, such as MemoryKeyedCache (and not RedisCache); otherwise it might break your leaf templates, see https://github.com/vapor/leaf/issues/134
NStack comes with a built-in Leaf tag. The tag yields a translated string or the given key if translation fails
// Get translation for camelCasedSection.camelCasedKey
#nstack:translate("camelCasedSection", "camelCasedKey")
// Get translation for camelCasedSection.camelCasedKey and replace searchString1 with replaceString1 etc
#nstack:translate("camelCasedSection", "camelCasedKey", "searchString1", "replaceString1", "searchString2", "replaceString2", ...)
IMPORTANT: Due to a bug in leaf you have to make sure that the translations are already loaded and available synchronously when rendering the view. This can be achieved by using the NStackPreloadMiddleware on the routes for your views:
let nstackPreloadMiddleware = try container.make(NStackPreloadMiddleware.self)
let unprotectedBackend = router.grouped(nstackPreloadMiddleware)
Please note that the leaf tag always uses the current application with the default translate config that you have provided.
Version Updates
NStack has the ability to retrieve the latest version for a certain platform. You can achieve that with the getLatestVersion(for platform: Platforms) -> Future<UpdateVersion?> method, e.g.:
let nstack = try NStack.makeService(for: req)
nstack.application.version.getLatestVersion(for: .android)
Responses
NStack can be used to store JSON responses. To make use of this feature you can use the ResponseController.
let nstack = try NStack.makeService(for: req)
nstack.application.response[42].do { (response: Response) in
print(response.content)
}
This gets the unmodified NStack Response with your JSON data in an object keyed by data, eg.:
{"data":{"myJSONData":"Starts here"}}
Alternatively you can decode your JSON object like so:
nstack.application.response[42].do { (response: [String: String]) in
print(response)
}
This would yield your Decodable object, in this case our dictionary: ["myJSONData": "Starts here"].
Caching
NStack uses the KeyedCache registered with Vapor. If you don’t register any Cache, this should be the KeyedMemory Cache. If you configure Vapor to prefer another Cache, NStack will use this one instead. Example for Redis:
NStack 🛠
This package is a wrapper around the NStack.io API.
Supports the following NStack modules:
📦 Installation
Package.swift
Add
NStack
to the Package dependencies:as well as to your target (e.g. “App”):
Getting started 🚀
Import NStack where needed:
Config
Create
NStack.Config
to configureNStack
, yourApplications
as well as the defaultTranslate.Config
.If you set
log
totrue
you will receive helpful logs in case anything goes wrong.Adding the Service
Instantiate and register
NStackProvider
with config created in the previous step. If you plan on using the leaf tag (see below), make sure to use a synchronous cache, such asMemoryKeyedCache
(and notRedisCache
); otherwise it might break your leaf templates, see https://github.com/vapor/leaf/issues/134In
configure.swift
:Usage
Features
Translate
You can also provide
searchReplacePairs
:If you are using multiple NStack applications within your project you can switch them with
getApplication()
:Note: you can specify the
get()
call further in case you don’t want to go with the values provided indefaultTranslateConfig
:Leaf Tag
In order to render the NStack Leaf tags, you will need to add them first:
NStack comes with a built-in Leaf tag. The tag yields a translated string or the given key if translation fails
IMPORTANT: Due to a bug in leaf you have to make sure that the translations are already loaded and available synchronously when rendering the view. This can be achieved by using the
NStackPreloadMiddleware
on the routes for your views:Please note that the leaf tag always uses the current application with the default translate config that you have provided.
Version Updates
NStack has the ability to retrieve the latest version for a certain platform. You can achieve that with the
getLatestVersion(for platform: Platforms) -> Future<UpdateVersion?>
method, e.g.:Responses
NStack can be used to store JSON responses. To make use of this feature you can use the
ResponseController
.This gets the unmodified NStack
Response
with your JSON data in an object keyed bydata
, eg.:Alternatively you can decode your JSON object like so:
This would yield your
Decodable
object, in this case our dictionary:["myJSONData": "Starts here"]
.Caching
NStack uses the
KeyedCache
registered with Vapor. If you don’t register any Cache, this should be theKeyedMemory
Cache. If you configure Vapor to prefer another Cache, NStack will use this one instead. Example for Redis:In
configure.swift
:🏆 Credits
This package is developed and maintained by the Vapor team at Nodes.
📄 License
This package is open-sourced software licensed under the MIT license