Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mttankkeo/web_cookie

Ideally Implemented simple web client-cookie source code, and using Typescript.
https://github.com/mttankkeo/web_cookie

client-side cookie typescript-library web

Last synced: 4 days ago
JSON representation

Ideally Implemented simple web client-cookie source code, and using Typescript.

Awesome Lists containing this project

README

        

# Web Cookie
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.

## Usage

### Get instance of cookie object
The following describes how to get a instance of cookie object.

```ts
// Not Null-Safety
const version = Cookie.getObjectByKey("version");
```

```ts
// Null-Safety
const version = Cookie.getObjectByKeyWithNullSafe("version", "1.0.0");
```

```ts
const cookies: CookieObject[] = Cookie.objects;
```

### Set value of cookie object
The following describes how to define a value of cookie object.

```ts
// Useing setter
version.value = "1.0.0";

// Useing function.
version.setValue("1.0.0");
```

## Listener
The following describes how to register a callback function that is called whenever the value of a cookie object updates.

### globally register listener
```ts
Cookie.addListener({
key: "version",
listener(value: string) {
console.log(`Update to ${value}`);
},
})
```

### Register listener by instance
```ts
const version = Cookie.getObjectByKeyWithNullSafe("version", "1.0.0");

version.addListener(value => {
console.log(`Update to ${value}`);
});
```