Skip to content

Commit

Permalink
fix: backported AdapterOptions type + added options to README
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Jan 25, 2023
1 parent cf69cc8 commit 0e48f86
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 9 deletions.
60 changes: 59 additions & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This is a library to save key-value pairs to one or multiple storages (e.g. Sess

- [Installation](#installation)
- [Usage](#usage)
- [Options](#options)

## Installation

Expand All @@ -27,7 +28,7 @@ which should be enabled. By default, all drivers are disabled, expect of the in-
Besides, it is also possible to specify a custom `namespace`, so different instances of the Adapter don't interfere with each other, when they use the same key.

```typescript
import { Adapter } from "browser-storage-adapter";
import { Adapter } from 'browser-storage-adapter';

const warehouse = new Adapter({
driver: {
Expand All @@ -52,3 +53,60 @@ const token = warehouse.get('token');
console.log(token);
// xxx
```

## Options

The Adapter accepts an `OptionsInput` object as input parameter, to modify the default behaviour.

````typescript
import { CookieSerializeOptions } from 'cookie';

declare type OptionsInput = {
/**
* Specify a key prefix.
* e.g.
* namespace: auth
* key: token
* keyWithNamespace: auth_token
*/
namespace?: string,
/**
* Enable or disable some available drivers.
*/
driver?: {
localStorage?: boolean,
sessionStorage?: boolean,
cookie?: boolean | CookieSerializeOptions
},
/**
* Check if the application is server rendered.
*/
isServer?: () => boolean,

/**
* Set cookie.
*
* @param key
* @param value
*/
setCookie?: (key: string, value: unknown) => void,

/**
* Get cookie.
*
* @param key
*/
getCookie?: (key: string) => unknown,

/**
* Append serialized cookie.
*
* @param value
*/
setServerCookie?: (value: string) => void,
/**
* Ger serialized cookie(s).
*/
getServerCookies?: () => string
}
````
9 changes: 6 additions & 3 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { CookieSerializeOptions, parse as parseCookie, serialize as serializeCookie } from 'cookie';
import { DriverType, Options, OptionsInput } from './type';
import { Options, OptionsInput } from './type';
import {
buildOptions,
decodeValue,
Expand All @@ -19,10 +19,13 @@ import { Driver } from './constants';
export class Adapter {
public readonly options: Options;

protected state : Record<string, any> = {};
protected state : Record<string, any>;

// ------------------------------------

constructor(options: OptionsInput) {
this.options = buildOptions(options);
this.state = {};

this.initState();
}
Expand Down Expand Up @@ -166,7 +169,7 @@ export class Adapter {
// Browser Storage
// ------------------------------------

getBrowserStorageItems(type: DriverType) : Record<string, any> {
getBrowserStorageItems(type: `${Driver}`) : Record<string, any> {
let storage : any;
switch (type) {
case Driver.SESSION_STORAGE:
Expand Down
15 changes: 10 additions & 5 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
*/

import { CookieSerializeOptions } from 'cookie';
import { Driver } from './constants';

export interface Options {
export type Options = {
/**
* Specify a key prefix.
* e.g.
Expand Down Expand Up @@ -55,8 +54,14 @@ export interface Options {
* Ger serialized cookie(s).
*/
getServerCookies?: () => string
}

export type DriverType = `${Driver}`;
};

export type OptionsInput = Partial<Options>;

/**
* This is an alias for the OptionsInput type.
* Will be removed in v2.0.0+
*
* @deprecated
*/
export type AdapterOptions = OptionsInput;

0 comments on commit 0e48f86

Please sign in to comment.