npm install @master/business
{
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true
}
}
import { Business, BusinessModel, Input, Output } from '@master/business';
@Business()
export class MyBusiness extends BusinessModel {
@Input()
prop1: string;
@Output()
prop2: number;
@Input()
@Output()
prop3: OtherBusinessModel;
...
}
Decorate the property that need to be validated
options | type | description |
---|---|---|
disabled |
boolean | Used to disable the @Input() decoration behavior of extended objects |
required |
boolean | Is the property required |
arrayType |
any | Assuming the type is YourType[], the target type must be additionally defined here |
enum |
Record<string, any> | Assuming the type is enum, the target type must be additionally defined here |
Decorate the property that need to be outputed
options | type | description |
---|---|---|
disabled |
boolean | Used to disable the @Input() decoration behavior of extended objects |
The front-end inputs the registration data to the server through the sign-up API, and then outputs the registration result back to the front-end.
├── businesses
│ └── member
│ ├── member.controller.ts
│ ├── member.service.ts
│ ├── member.ts // DAO
│ └── signing-up.ts
// signing-up.ts
import { Business, BusinessModel, Input } from '@master/business';
@Business()
export class SigningUp extends BusinessModel {
@Output()
@Input({ required: true })
name: string;
@Output()
@Input()
address: SigningUpAddress;
@Output()
type = 'general';
// other fields for quick access
a = 1;
b = 2;
c = 3;
d = 4;
}
@Business()
class SigningUpAddress extends BusinessModel {
@Output()
@Input()
city: string;
@Input()
district: string;
@Input()
street: string;
}
// member.controller.ts
import { Business, BusinessModel, Input, validate } from '@master/business';
import { MemberService } from './member.service.ts';
import { SigningUp } from './signing-up.ts';
@Controller('member')
export class MemberController {
constructor(
private memberService: MemberService
) {}
@Post()
async SignUp(
@Body() data: any,
@Res() res: Response
): Promise<void> {
const signingUp = new SigningUp(data);
const errors = signingUp.validate();
// validate
if(errors.length) {
// property error
res.status(400).send(errors);
} else {
// correct
// business logic process here ...
this.memberService.signUp(signingUp);
res.status(200).send(signingUp);
}
}
}
{
name: "joy",
address: {
city: "taipei",
district: "zhongshan",
street: "my home"
}
}
{
name: "joy",
address: {
city: "taipei",
district: "zhongshan",
street: "my home"
},
type: 'general',
a: 1,
b: 2,
c: 3,
d: 4
}
{
name: "joy",
address: {
city: "taipei"
},
type: 'general'
}
@Business()
class MyBusiness extends BusinessModel {
@Input()
str: string;
@Input()
num: number;
@Input({ enum: MyEnum })
enum: MyEnum;
@Input({ arrayType: MyArrayType })
arrayType: MyArrayType[];
}
BenSeage | Aron | Miles | Lola |
creator | designer | maintainer | maintainer |