Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@master/business

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@master/business

A business data model for quick verification, access and output of specific data formats.

  • 3.0.7
  • unpublished
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source
License: MIT

Master Business

A business data model for quick verification, access and output of specific data formats.

 

npm install @master/business

tsconfig.json

{
    "compilerOptions": {
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true
    }
}

Usage

import { Business, BusinessModel, Input, Output } from '@master/business';

@Business()
export class MyBusiness extends BusinessModel {
    @Input()
    prop1: string;

    @Output()
    prop2: number;

    @Input()
    @Output()
    prop3: OtherBusinessModel;

    ...
}

@Input(options?)

Decorate the property that need to be validated

optionstypedescription
disabledbooleanUsed to disable the @Input() decoration behavior of extended objects
requiredbooleanIs the property required
arrayTypeanyAssuming the type is YourType[], the target type must be additionally defined here
enumRecord<string, any>Assuming the type is enum, the target type must be additionally defined here

@Output(options?)

Decorate the property that need to be outputed

optionstypedescription
disabledbooleanUsed to disable the @Input() decoration behavior of extended objects

Example

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.

File Structure

├── businesses
│   └── member
│       ├── member.controller.ts
│       ├── member.service.ts
│       ├── member.ts // DAO
│       └── signing-up.ts

Define the business model

// 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;
}

Process business logic ( nestjs for example )

// 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);
        }
    }
}

Input:request data

{
    name: "joy",
    address: {
        city: "taipei",
        district: "zhongshan",
        street: "my home"
    }
}

Processing:business data

{
    name: "joy",
    address: {
        city: "taipei",
        district: "zhongshan",
        street: "my home"
    },
    type: 'general',
    a: 1,
    b: 2,
    c: 3,
    d: 4
}

Output:response data

{
    name: "joy",
    address: {
        city: "taipei"
    },
    type: 'general'
}

@Input definitions

@Business()
class MyBusiness extends BusinessModel {
    @Input()
    str: string;

    @Input()
    num: number;

    @Input({ enum: MyEnum })
    enum: MyEnum;

    @Input({ arrayType: MyArrayType })
    arrayType: MyArrayType[];
}

Solutions

  • Provide a rich access interface for developers

  • Follow the DRY principle (Don't repeat yourself)

  • Omit the definition of Request DTO and Response DTO data structure

  • Data structure focuses on one interface

  • Reduce code writing

  • No need to define variables individually to manipulate data

Code Contributors

BenSeagearon-twmiles0930BenSeage
BenSeageAronMilesLola
creatordesignermaintainermaintainer

Keywords

FAQs

Package last updated on 30 May 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc