diff --git a/package.json b/package.json index 125ef66..d3e217e 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/context.ts b/src/context.ts index 12688ac..4f6dc3e 100644 --- a/src/context.ts +++ b/src/context.ts @@ -1,33 +1,46 @@ -type GlobalContext = { +type GlobalContext = { redis?: any; -}; -export const useContext = (initContext?: GlobalContext) => { +} & U; + +export const useContext = (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 = (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 (key: string, init: () => Promise): Promise => { 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 = (key: string, value: T) => { + const _context = useContext>({}); + _context[key] = value; + return _context; +}; diff --git a/src/pkgs.ts b/src/pkgs.ts index 0995807..7ffa136 100644 --- a/src/pkgs.ts +++ b/src/pkgs.ts @@ -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 || {}; diff --git a/src/read-app-schema.ts b/src/read-app-schema.ts index 786fe30..2bf55dd 100644 --- a/src/read-app-schema.ts +++ b/src/read-app-schema.ts @@ -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;