Skip to content

Commit

Permalink
chore: ResultFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
radiorz committed Jan 6, 2025
1 parent d5ef0f5 commit db14fbd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
13 changes: 5 additions & 8 deletions packages/result/lib/ResultFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ export class ResultFactoryImpl implements ResultFactory {
constructor(options?: Partial<ResultFactoryOptions>) {
this.options = optionsMerge(ResultFactoryImpl.defaultOptions, options);
}
final(result: ReuseResult): FinalResult {
throw new Error('Method not implemented.');
}

createResult(result: OriginResult): ReuseResult {
const factory = this;
Expand All @@ -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) {
Expand All @@ -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);
}

Expand Down
3 changes: 2 additions & 1 deletion packages/result/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());

0 comments on commit db14fbd

Please sign in to comment.