Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Plugins with Typescript #303

Open
damianon opened this issue Oct 21, 2019 · 7 comments
Open

Use Plugins with Typescript #303

damianon opened this issue Oct 21, 2019 · 7 comments

Comments

@damianon
Copy link

I'm trying to figure out how to use plugins (expire specifically) while using typescript.
The Store.set method doesn't accept a third parameter (expiration), even if I call Store.addPlugin(Expire)

@Mike-Logit
Copy link

@damianon I want to know this as well, specifically for expire.js. Were you able to find a fix? Maybe create your own ts definition file for the parts you use?

@damianon
Copy link
Author

damianon commented Jul 8, 2020

No, I ended up creating my on wrapper to expire entries

@EgorVasilev
Copy link

@damianon could you please share your solution?

@damianon
Copy link
Author

Don't have the code at hand currently, but basically I wrapped the object and added an expiration timestamp. Upon reading the value I checked the timestamp and if expired returned null

@harry0000
Copy link

For future reader, try this.

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/03a4c5c3eb10ac287b3400d5d0cdca7ab25b39c7/types/store/store-tests.ts#L68-L91

npm i @types/store

src/@types/store.d.ts

interface StoreJsAPI {
  // expirePlugin
  set(key: string, value: any, expiration: number): any;
}
import * as engine from 'store/src/store-engine';
import * as expirePlugin from 'store/plugins/expire';

const myStore = engine.createStore([], [expirePlugin]);
myStore.set('foo', 'bar', new Date().getTime() + 3000);

I hope this helps.

@laozhengfanle
Copy link

laozhengfanle commented Nov 6, 2022

ts中,添加了过期时间插件,set方法由于声明的类型缺少第三个参数,导致无法使用set方法

@SoraDevin
Copy link

SoraDevin commented Sep 20, 2023

posting the solution to my problem for more convenience:

import engine from "store/src/store-engine";
import expirePlugin from "store/plugins/expire";

declare global {
  interface StoreJsAPI {
    // expirePlugin
    set(key: string, value: any, expiration: number): any;
  }
}

const store = engine.createStore([], [expirePlugin]);
const storeKey = "form-data";
const formExpiryDuration = 1000 * 60 * 60; // 1 Hour

export function getFormData() {
  return store.get(storeKey) || {};
}

export function setFormData(data: any) {
  store.set(
    storeKey,
    Object.assign(getFormData(), data),
    new Date().getTime() + formExpiryDuration,
  );
}

export function clearFormData() {
  store.set(storeKey, {});
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants