Cookie Builder
It would be nice to have a cookie builder.
Where should the code be located? In lib/Format.js
Within the Format.js file there needs to be a new function added called Format.cookie(name, value = '', options = {})
I took the options from this page: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie
This means that the function takes the following parameters.
-
name
{String} name of the cookie to set -
value
{String|Object} String will be set directly and ignore options, Object will be parsed into a URIEncoded name/value pair, eg: 'var1=val1&var2=val2` -
options
{Object} containing additional cookie parameters, as well as cookie building options
Available Options
- Expires=
- Max-Age=
- Domain=
- Path=
- Secure=
- HttpOnly=<boolean}
- SameSite=<Strict|Lax|None>
Where should the functionality be available from? In lib/Router.js
There is a method in lib/Router.js called standardPreparation
this sets up all of the shortcuts that are added to each req, res
object pair as they arrives.
Thus something like the following would be added:
class Router {
...
standardPrepartion () {
...
res.cookie = (name, value, options) => {
const cookie = Format.cookie(name, value, options)
res.setHeader('Set-Cookie', cookie)
}
...
}
}
In the example the ...
means there is existing code already around these areas that I dont want to include into the example.
Finally using the cookie builder will be simple.
class SomeModule extends Module {
someRoute (req, res) {
res.cookie('remember', { days: 30, userId: 1, createdAt: +new Date() }, { path: '/', httpOnly: true })
}
}
So to complete the task you are working on you need to make a new function in Format called cookie() which is going to take those parameters like in the example and then produce a Set-Cookie string like the one we worked out earlier today.
Second, you will need to add some code to lib/Router to make use of the new function in Format.
Third, you will need to write a test against Format.cookie() in test/Format.test.js
Fourth, you will need to write documentation about Format.cookie() using some of the information I provided in this task and add that to doc/Format.md
This task should carry you through tomorrow and will be a great addition to Kado. Tonight you should focus on making the actual Format.cookie() function.