This commit is contained in:
2026-02-15 19:19:19 +08:00
commit c726cd8804
11 changed files with 392 additions and 0 deletions

40
readme.md Normal file
View File

@@ -0,0 +1,40 @@
# 环境变量 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 };
```