Ideally Implemented simple web client-cookie source code, and using Typescript.
See also: cookies are rarely controlled directly by the client.
The example doesn't work in the local environment.
The following describes how to get a instance of cookie object.
// Not Null-Safety
const version = Cookie.getObjectByKey<string>("version");
// Null-Safety
const version = Cookie.getObjectByKeyWithNullSafe<string>("version", "1.0.0");
const cookies: CookieObject<any>[] = Cookie.objects;
The following describes how to define a value of cookie object.
// Useing setter
version.value = "1.0.0";
// Useing function.
version.setValue("1.0.0");
The following describes how to register a callback function that is called whenever the value of a cookie object updates.
Cookie.addListener<string>({
key: "version",
listener(value: string) {
console.log(`Update to ${value}`);
},
})
const version = Cookie.getObjectByKeyWithNullSafe("version", "1.0.0");
version.addListener(value => {
console.log(`Update to ${value}`);
});