Platform and environment agnostic cookie module; Manage cookies on the browser and the backend.
// ES6 Module
import pastry, { SetWebCookieOptions, WebCookieHelpers, SetServerCookieOptions } from "pastryjs";
const cookie = pastry();
// Commonjs Module
const pastry, { SetWebCookieOptions, WebCookieHelpers, SetServerCookieOptions } = require("pastryjs");
You can also add the CDN via jsDelivr
:
<script src="https://cdn.jsdelivr.net/npm/pastryjs@0.1.0/lib/index.js"></script>
On the web
import pastry from "pastryjs";
// for the web; has internal method
const webCookie = pastry().web()
// create a new cookie
webCookie.set(...)
On the server
import pastry from "pastryjs";
import server from "...";
const app = server();
app.get("/", (request, response) => {
response.header.set(
"Set-Cookie",
pastry().server("foo", "bar", {
domain: "https://www.foo.bar",
maxAge: 3600,
})
);
return response.json({ message: "Cookie set!" });
});
app.listen(8080);
Initialise it as follows:
const cookie = pastry();
// This has five internal methods
const webCookie = cookie.web();
// server(...) is it's own method
const serverCookie = cookie.server(...);
Used for setting cookies on the web. It returns the following methods.
Set a cookie client side using javascript.
Get the specific value of a cookie string.
Delete a cookie that was already set.
Update a cookie client side using javascript; It calls cookie().set(...)
under the hood but with the extra step of validating whether or not the cookie exists.
View all available cookie keys.
View all available cookie values.
It returns a string that you can use to set a Set-Cookie
header.
Typings for the internal methods of the cookie().web()
method.
{
set: (key: string, value: string, options?: SetCookieOptions) => boolean;
get: (key: string) => string | null;
delete: (key: string) => boolean;
update: (key: string, value: string, options?: SetCookieOptions) => boolean;
keys: () => string[] | null;
values: () => string[] | null;
}
Options for setting the cookie.
{
domain?: string;
expiry?: Date;
maxAge?: number;
partitioned?: boolean;
path?: string;
sameSite?: "Lax" | "Strict" | "None";
secure?: boolean;
httpOnly?: boolean;
}
Options for setting the cookie on the server.
{
domain?: string;
expiry?: Date;
maxAge?: number;
partitioned?: boolean;
path?: string;
sameSite?: "Lax" | "Strict" | "None";
secure?: boolean;
httpOnly?: boolean;
}
v0.1.0
- Initial release