40 lines
1.9 KiB
Markdown
40 lines
1.9 KiB
Markdown
# 环境变量 context
|
||
|
||
上下文 的node或则浏览器当中,如果global中不存在context,则赋值到global,如果存在,则获取对应的。
|
||
|
||
```ts
|
||
import { useContextKey } from '@kevisual/context';
|
||
|
||
const v = useContextKey('a', () => '123'); // 如果是第一次初始化,返回123,否则返回之前初始化的值
|
||
```
|
||
|
||
|
||
|
||
### types
|
||
```ts
|
||
type GlobalContext<T = SimpleObject> = {
|
||
name?: string;
|
||
[key: string]: any;
|
||
} & T;
|
||
declare const useEnv: <T = SimpleObject>(initEnv?: GlobalContext<T>, initKey?: string, isOverwrite?: boolean) => Required<GlobalContext<T>>;
|
||
type InitResult<T> = T | Promise<T> | null;
|
||
type InitFn<T> = () => T | Promise<T>;
|
||
type Init<T> = T | InitFn<T> | null;
|
||
type AsyncResult<T, ASYNC extends boolean = false> = ASYNC extends true ? Promise<T> : T;
|
||
type SimpleObject = Record<string, any>;
|
||
declare const useEnvKey: <T = any>(key: string, init?: Init<T>, initKey?: string) => InitResult<T>;
|
||
declare const usePageEnv: (init?: Init<any>, initKey?: string) => any;
|
||
declare const useEnvKeyNew: (key: string, initKey?: string, opts?: {
|
||
getNew?: boolean;
|
||
init?: Init<any>;
|
||
}) => any;
|
||
declare const useContext: <T = SimpleObject>(initContext?: GlobalContext<T>, isOverwrite?: boolean) => Required<GlobalContext<T>>;
|
||
declare const useContextKey: <T = any, ASYNC extends boolean = false>(key: string, init?: Init<T>, isNew?: boolean) => AsyncResult<T, ASYNC>;
|
||
declare const usePageContext: (init?: Init<any>) => any;
|
||
declare const useConfig: <T = SimpleObject>(initConfig?: Partial<GlobalContext<T>>, isOverwrite?: boolean) => Required<GlobalContext<Partial<GlobalContext<T>>>>;
|
||
declare const useConfigKey: <T = any, ASYNC extends boolean = false>(key: string, init?: Init<T>, isNew?: boolean) => AsyncResult<T, ASYNC>;
|
||
declare const usePageConfig: (init?: Init<any>) => any;
|
||
|
||
export { useConfig, useConfigKey, useContext, useContextKey, useEnv, useEnvKey, useEnvKeyNew, usePageConfig, usePageContext, usePageEnv };
|
||
|
||
``` |