Add redux-persist and basic transformers (#13389)

* Add definitions for redux-persist

* Add missin generic types

* Add definitions for filter transformer

* Add definitions for encrypt transformer

* Fix header

* Add definitions for compress transformer

* Delete unnecessary linter configs

* Change way of importing, fix tests
This commit is contained in:
Karol Janyst 2016-12-19 09:36:29 +09:00 committed by Andy
parent 59ce85c4aa
commit de2e7e96d7
18 changed files with 297 additions and 0 deletions

View File

@ -0,0 +1,9 @@
// Type definitions for redux-persist-transform-compress 4.1
// Project: https://github.com/rt2zz/redux-persist-transform-compress
// Definitions by: Karol Janyst <https://github.com/LKay>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
import { PersistConfig, PersistTransformer } from "redux-persist";
export = createCompressor;
declare function createCompressor (config?: PersistConfig): PersistTransformer;

View File

@ -0,0 +1,5 @@
{
"dependencies": {
"redux": "^3.6.0"
}
}

View File

@ -0,0 +1,11 @@
import { createStore, Reducer, Store } from "redux"
import { createPersistor, Persistor, PersistTransformer } from "redux-persist"
import createCompressor = require("redux-persist-transform-compress")
const reducer: Reducer<any> = (state: any, action: any) => ({})
const compressor: PersistTransformer = createCompressor({ whitelist : ["foo"] })
const store: Store<any> = createStore(reducer)
const persistor: Persistor = createPersistor(store, { transforms : [compressor] })

View File

@ -0,0 +1,19 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"noImplicitAny": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"redux-persist-transform-compress-tests.ts"
]
}

View File

@ -0,0 +1,18 @@
// Type definitions for redux-persist-transform-encrypt 0.1
// Project: https://github.com/maxdeviant/redux-persist-transform-encrypt#readme
// Definitions by: Karol Janyst <https://github.com/LKay>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
import { PersistTransformer } from "redux-persist";
export as namespace ReduxPersistEncryptor;
export = createEncryptor;
declare function createEncryptor (config: createEncryptor.EncryptorConfig): PersistTransformer;
declare namespace createEncryptor {
export interface EncryptorConfig {
secretKey: string;
}
}

View File

@ -0,0 +1,5 @@
{
"dependencies": {
"redux": "^3.6.0"
}
}

View File

@ -0,0 +1,13 @@
import { createStore, Reducer, Store } from "redux"
import { createPersistor, Persistor, PersistTransformer } from "redux-persist"
import { EncryptorConfig } from "redux-persist-transform-encrypt"
import * as createEncryptor from "redux-persist-transform-encrypt"
const reducer: Reducer<any> = (state: any, action: any) => ({})
const config: EncryptorConfig = { secretKey : "foo" }
const encryptor: PersistTransformer = createEncryptor(config)
const store: Store<any> = createStore(reducer)
const persistor: Persistor = createPersistor(store, { transforms : [encryptor] })

View File

@ -0,0 +1,19 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"noImplicitAny": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"redux-persist-transform-encrypt-tests.ts"
]
}

View File

@ -0,0 +1,8 @@
// Type definitions for redux-persist-transform-filter 0.0
// Project: https://github.com/edy/redux-persist-transform-filter
// Definitions by: Karol Janyst <https://github.com/LKay>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
import { PersistTransformer } from "redux-persist";
export default function createFilter (reducerName: string, inboundPaths?: string[], outboundPaths?: string[]): PersistTransformer;

View File

@ -0,0 +1,5 @@
{
"dependencies": {
"redux": "^3.6.0"
}
}

View File

@ -0,0 +1,15 @@
import { createStore, Reducer, Store } from "redux"
import { createPersistor, Persistor, PersistTransformer } from "redux-persist"
import createFilter from "redux-persist-transform-filter"
const reducer: Reducer<any> = (state: any, action: any) => ({})
const filter: PersistTransformer = createFilter(
"foo",
["foo.bar"],
["fizz.buzz"]
)
const store: Store<any> = createStore(reducer)
const persistor: Persistor = createPersistor(store, { transforms : [filter] })

View File

@ -0,0 +1,19 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"noImplicitAny": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"redux-persist-transform-filter-tests.ts"
]
}

2
redux-persist/constants.d.ts vendored Normal file
View File

@ -0,0 +1,2 @@
export const KEY_PREFIX: string;
export const REHYDRATE: string;

66
redux-persist/index.d.ts vendored Normal file
View File

@ -0,0 +1,66 @@
// Type definitions for redux-persist 4.0
// Project: https://github.com/rt2zz/redux-persist
// Definitions by: Karol Janyst <https://github.com/LKay>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
import { Action, GenericStoreEnhancer, Store } from "redux";
export interface PersistAction<S> extends Action {
payload?: S;
error?: any;
}
export interface PersistorRehydrateOptions {
serial?: boolean;
}
export interface Persistor {
purge(keys?: string[]): void;
rehydrate<A>(incoming: A, options?: PersistorRehydrateOptions): A;
pause(): void;
resume(): void;
}
export type PersistCallback<A> = (err: any, response?: A) => void;
export interface PersistTransformer {
in(state: any, key: string): any;
out(state: any, key: string): any;
}
export interface PersistStorage {
setItem(key: string, value: any, callback?: PersistCallback<void>): Promise<void>;
getItem<A>(key: string, callback?: PersistCallback<A>): Promise<A>;
removeItem(key: string, callback?: PersistCallback<void>): Promise<void>;
getAllKeys<A>(callback?: PersistCallback<A>): Promise<A>;
[key: string]: any;
}
export type PersistStateReconciler<A, B, C> = (state: A, inboundState: B, reducedState: C, log?: boolean) => C;
export interface PersistAutoRehydrateConfig<A, B, C> {
stateReconcile?: PersistStateReconciler<A, B, C>;
}
export interface PersistConfig {
whitelist?: string[];
blacklist?: string[];
transforms?: PersistTransformer[];
storage?: PersistStorage;
debounce?: number;
keyPrefix?: string;
serialize?: (data: any) => string;
deserialize?: (data: string) => any;
}
export function autoRehydrate<A, B ,C> (config?: PersistAutoRehydrateConfig<A, B, C>): GenericStoreEnhancer;
export function createPersistor<A> (store: Store<A>, config?: PersistConfig): Persistor;
export function createTransform (inbound: any, outbound: any, config?: PersistConfig): PersistTransformer;
export function getStoredState(config?: PersistConfig, callback?: PersistCallback<any>): Promise<any>;
export function persistStore<A> (store: Store<A>, config?: PersistConfig, callback?: PersistCallback<any>): Persistor;
export function purgeStoredState (config?: PersistConfig, keys?: string[]): Promise<void>;

View File

@ -0,0 +1,5 @@
{
"dependencies": {
"redux": "^3.6.0"
}
}

View File

@ -0,0 +1,53 @@
import { createStore, Store, Reducer } from "redux"
import {
autoRehydrate,
createPersistor,
createTransform,
getStoredState,
persistStore,
purgeStoredState,
PersistConfig,
PersistAutoRehydrateConfig,
PersistCallback,
Persistor,
PersistorRehydrateOptions,
PersistTransformer
} from "redux-persist"
import { KEY_PREFIX, REHYDRATE } from "redux-persist/constants"
import { asyncLocalStorage, asyncSessionStorage } from "redux-persist/storages"
const reducer: Reducer<any> = (state: any, action: any) => ({})
const persistCallback: PersistCallback<any> = (err: any, response: any) => {}
const transform: PersistTransformer = createTransform({}, {}, { whitelist : ["foo"] })
const persistConfig: PersistConfig = {
blacklist : ["foo"],
whitelist : ["bar"],
storage : asyncLocalStorage,
transforms : [transform],
debounce : 1000,
keyPrefix : KEY_PREFIX
}
const rehydrateOptions: PersistorRehydrateOptions = { serial : true }
const autoRehydrateConfig: PersistAutoRehydrateConfig<any, any, any> = {
stateReconcile: (state: any, inboundState: any, reducedState: any, log: boolean) => ({})
}
const store: Store<any> = createStore(reducer, autoRehydrate(autoRehydrateConfig))
const persistor: Persistor = persistStore(store, persistConfig, persistCallback)
purgeStoredState({ whitelist : ["foo"] }, ["bar"])
getStoredState(persistConfig, (err: any, restoredState: any) => {
const store: Store<any> = createStore(reducer, restoredState)
const persistor: Persistor = createPersistor(store, persistConfig)
const secondaryPersistor: Persistor = createPersistor(store, { storage : asyncSessionStorage })
persistor.pause()
persistor.resume()
persistor.purge(["foo", "bar"])
persistor.rehydrate(restoredState, rehydrateOptions)
})

4
redux-persist/storages.d.ts vendored Normal file
View File

@ -0,0 +1,4 @@
import { PersistStorage } from "redux-persist";
export const asyncLocalStorage: PersistStorage;
export const asyncSessionStorage: PersistStorage;

View File

@ -0,0 +1,21 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"noImplicitAny": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"constants.d.ts",
"storages.d.ts",
"redux-persist-tests.ts"
]
}