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