diff --git a/packages/result/lib/ResultFactory.ts b/packages/result/lib/ResultFactory.ts index 326f92b..dd66650 100644 --- a/packages/result/lib/ResultFactory.ts +++ b/packages/result/lib/ResultFactory.ts @@ -31,9 +31,6 @@ export class ResultFactoryImpl implements ResultFactory { constructor(options?: Partial) { this.options = optionsMerge(ResultFactoryImpl.defaultOptions, options); } - final(result: ReuseResult): FinalResult { - throw new Error('Method not implemented.'); - } createResult(result: OriginResult): ReuseResult { const factory = this; @@ -45,16 +42,16 @@ export class ResultFactoryImpl implements ResultFactory { getString(language?: string) { return factory.getResultString(this, language); }, - final() { - return { + final(): FinalResult { + return Object.freeze({ code: this.getCode(), message: this.getString(), - }; + }); }, }; return reuseResult; } - getResultString(result: ReuseResult, language?: string) { + private getResultString(result: ReuseResult, language?: string) { const messageTemplate = get(this.getCurrentMessageMap(language), this.getMessageToken(result)); // 没有对应的message if (!messageTemplate) { @@ -68,7 +65,7 @@ export class ResultFactoryImpl implements ResultFactory { // 搞个简单的插值 return params(messageTemplate, { ...result.payload, error: result.error?.message }); } - getResultCode(result: ReuseResult): string | number { + private getResultCode(result: ReuseResult): string | number { return this.getMessageToken(result); } diff --git a/packages/result/src/index.ts b/packages/result/src/index.ts index 6edf25c..c0b3633 100644 --- a/packages/result/src/index.ts +++ b/packages/result/src/index.ts @@ -28,9 +28,9 @@ const result = resultFactory.createResult({ console.log(`successResult`, result); console.log(`successResult.getString() `, result.getString()); console.log(`successResult.getCode()`, result.getCode()); - // 多语言测试 console.log(`successResult.getString('en')`, result.getString('en')); +console.log(`result.final()`, result.final()); // 测试切换status result.status = false; @@ -39,3 +39,4 @@ console.log(`errorResult`, result); console.log(`errorResult status`, result.status); console.log(`errorResult code`, result.getCode()); console.log(`errorResult`, result.getString()); +console.log(`result.final()`, result.final());