fix: fix context error
This commit is contained in:
parent
a57579281e
commit
7403e09ea3
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@kevisual/use-config",
|
"name": "@kevisual/use-config",
|
||||||
"version": "1.0.5",
|
"version": "1.0.7",
|
||||||
"types": "dist/config.d.ts",
|
"types": "dist/config.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "npm run clean && rollup -c",
|
"build": "npm run clean && rollup -c",
|
||||||
|
@ -1,33 +1,46 @@
|
|||||||
type GlobalContext = {
|
type GlobalContext<U = any> = {
|
||||||
redis?: any;
|
redis?: any;
|
||||||
};
|
} & U;
|
||||||
export const useContext = (initContext?: GlobalContext) => {
|
|
||||||
|
export const useContext = <T = GlobalContext>(initContext?: GlobalContext): T => {
|
||||||
const context: GlobalContext = (global as any).context;
|
const context: GlobalContext = (global as any).context;
|
||||||
const _context = context || initContext;
|
const _context = context || initContext;
|
||||||
!context && ((global as any)['context'] = _context);
|
if (!context) {
|
||||||
return _context;
|
if (_context) {
|
||||||
|
(global as any).context = _context;
|
||||||
|
} else {
|
||||||
|
global.context = {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return global.context;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useContextKey = <T>(key: string, init: () => T): T => {
|
export const useContextKey = <T>(key: string, init: () => T): T => {
|
||||||
const _context = useContext({});
|
const _context = useContext({});
|
||||||
|
if (key && typeof _context[key] !== 'undefined') {
|
||||||
|
return _context[key];
|
||||||
|
}
|
||||||
if (key && init) {
|
if (key && init) {
|
||||||
_context[key] = init();
|
_context[key] = init();
|
||||||
return _context[key] as any;
|
return _context[key] as any;
|
||||||
}
|
}
|
||||||
if (key) {
|
|
||||||
return _context[key];
|
|
||||||
}
|
|
||||||
return _context as any;
|
return _context as any;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useContextKeySync = async <T = any>(key: string, init: () => Promise<T>): Promise<T> => {
|
export const useContextKeySync = async <T = any>(key: string, init: () => Promise<T>): Promise<T> => {
|
||||||
const _context = useContext({});
|
const _context = useContext({});
|
||||||
|
if (key && typeof _context[key] !== 'undefined') {
|
||||||
|
return _context[key];
|
||||||
|
}
|
||||||
if (key && init) {
|
if (key && init) {
|
||||||
_context[key] = await init();
|
_context[key] = await init();
|
||||||
return _context[key] as any;
|
return _context[key] as any;
|
||||||
}
|
}
|
||||||
if (key) {
|
|
||||||
return _context[key];
|
|
||||||
}
|
|
||||||
return _context as any;
|
return _context as any;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const setContextKey = <T>(key: string, value: T) => {
|
||||||
|
const _context = useContext<GlobalContext<T>>({});
|
||||||
|
_context[key] = value;
|
||||||
|
return _context;
|
||||||
|
};
|
||||||
|
@ -11,7 +11,7 @@ export const getPkgs = () => {
|
|||||||
const config = JSON.parse(fs.readFileSync(configFile, 'utf-8'));
|
const config = JSON.parse(fs.readFileSync(configFile, 'utf-8'));
|
||||||
return config;
|
return config;
|
||||||
};
|
};
|
||||||
|
export type { App };
|
||||||
export const getApp = (): App => {
|
export const getApp = (): App => {
|
||||||
const config = getPkgs();
|
const config = getPkgs();
|
||||||
return config.app || {};
|
return config.app || {};
|
||||||
|
@ -1,79 +1,97 @@
|
|||||||
import { FromSchema } from 'json-schema-to-ts';
|
import { FromSchema } from 'json-schema-to-ts';
|
||||||
const App = {
|
const App = {
|
||||||
$schema: 'http://json-schema.org/draft-07/schema#',
|
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||||
title: 'App Configuration Schema',
|
"title": "App Configuration Schema",
|
||||||
type: 'object',
|
"type": "object",
|
||||||
properties: {
|
"properties": {
|
||||||
type: {
|
"type": {
|
||||||
type: 'string',
|
"type": "string",
|
||||||
enum: ['inline-app', 'micro-app'],
|
"enum": [
|
||||||
$comment: "Type must be either 'inline-app' or 'micro-app'",
|
"system-app",
|
||||||
|
"micro-app",
|
||||||
|
"gateway-app"
|
||||||
|
],
|
||||||
|
"$comment": "Type must be either 'system-app' or 'micro-app' or 'gateway-app'."
|
||||||
},
|
},
|
||||||
single: {
|
"home": {
|
||||||
type: 'boolean',
|
"type": "string",
|
||||||
$comment: '是否单例模式,独立启动服务。',
|
"pattern": "^/.*",
|
||||||
|
"$comment": "https://kevisual.xiongxiao.me"
|
||||||
},
|
},
|
||||||
port: {
|
"single": {
|
||||||
type: 'integer',
|
"type": "boolean",
|
||||||
minimum: 0,
|
"$comment": "是否单例模式,独立启动服务。"
|
||||||
maximum: 65535,
|
|
||||||
$comment: '开发和单例启动服务的端口',
|
|
||||||
},
|
},
|
||||||
remote: {
|
"port": {
|
||||||
type: 'object',
|
"type": "integer",
|
||||||
properties: {
|
"minimum": 0,
|
||||||
host: {
|
"maximum": 65535,
|
||||||
type: 'string',
|
"$comment": "开发和单例启动服务的端口"
|
||||||
format: 'hostname',
|
},
|
||||||
},
|
"remote": {
|
||||||
path: {
|
"type": "object",
|
||||||
type: 'string',
|
"properties": {
|
||||||
pattern: '^/.*',
|
"host": {
|
||||||
|
"type": "string",
|
||||||
|
"format": "hostname"
|
||||||
},
|
},
|
||||||
|
"path": {
|
||||||
|
"type": "string",
|
||||||
|
"pattern": "^/.*"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
required: ['host', 'path'],
|
"required": [
|
||||||
|
"host",
|
||||||
|
"path"
|
||||||
|
],
|
||||||
|
"$comment": "远程服务的地址和路径"
|
||||||
},
|
},
|
||||||
micro: {
|
"micro": {
|
||||||
type: 'object',
|
"type": "object",
|
||||||
properties: {
|
"properties": {
|
||||||
serve: {
|
"serve": {
|
||||||
type: 'object',
|
"type": "object",
|
||||||
properties: {
|
"properties": {
|
||||||
name: {
|
"name": {
|
||||||
type: 'string',
|
"type": "string",
|
||||||
pattern: '^[a-z0-9-]+$',
|
"pattern": "^[a-z0-9-]+$",
|
||||||
$comment: '服务名称',
|
"$comment": "服务名称"
|
||||||
},
|
|
||||||
port: {
|
|
||||||
type: 'integer',
|
|
||||||
minimum: 0,
|
|
||||||
maximum: 65535,
|
|
||||||
$comment: '启动的服务端口',
|
|
||||||
},
|
},
|
||||||
|
"port": {
|
||||||
|
"type": "integer",
|
||||||
|
"minimum": 0,
|
||||||
|
"maximum": 65535,
|
||||||
|
"$comment": "启动的服务端口"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
required: [],
|
"required": []
|
||||||
},
|
},
|
||||||
remote: {
|
"remote": {
|
||||||
type: 'object',
|
"type": "object",
|
||||||
properties: {
|
"properties": {
|
||||||
host: {
|
"host": {
|
||||||
type: 'string',
|
"type": "string",
|
||||||
format: 'hostname',
|
"format": "hostname"
|
||||||
},
|
|
||||||
port: {
|
|
||||||
type: 'integer',
|
|
||||||
minimum: 0,
|
|
||||||
maximum: 65535,
|
|
||||||
$comment: '链接的远程地址的端口',
|
|
||||||
},
|
},
|
||||||
|
"port": {
|
||||||
|
"type": "integer",
|
||||||
|
"minimum": 0,
|
||||||
|
"maximum": 65535,
|
||||||
|
"$comment": "链接的远程地址的端口"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
required: ['host', 'port'],
|
"required": [
|
||||||
},
|
"host",
|
||||||
|
"port"
|
||||||
|
]
|
||||||
|
}
|
||||||
},
|
},
|
||||||
required: [],
|
"required": []
|
||||||
},
|
}
|
||||||
},
|
},
|
||||||
required: ['type'],
|
"required": [
|
||||||
|
"type"
|
||||||
|
]
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
export type App = FromSchema<typeof App>;
|
export type App = FromSchema<typeof App>;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user