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

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

  • 1.0.0
  • unpublished
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

File Structure

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

Getting Start

npm install @master/business

tsconfig.json

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

extends BusinessModel

命名規則:Ving + N


@Business

裝飾模型


@Input(options?)

裝飾需驗證格式的欄位

optionstypedescription
disabledboolean用於禁用 extends 對象的同樣 @Input() 屬性
requiredboolean必輸入
arrayTypeany類型為 SomeType[] 必須於此額外定義目標類型
enumRecord<string, any>類型為 enum 須於此額外定義目標類型

@Output(options?)

裝飾需輸出的欄位

optionstypedescription
disabledboolean用於禁用 extends 對象的同樣 @Input() 屬性

範例

前端藉由 sign-up API 將註冊資料輸入至 Server,Server 再將註冊結果輸出回前端


定義 業務流程之模型

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

	// 其他業務過程的產物欄位
	a = 1;
	b = 2;
}

@Business()
class SigningUpAddress extends BusinessModel {
	
	@Output()
	@Input()
	city: string;

	@Input()
	district: string;
	
	@Input()
	street: string;
}

建立業務過程 ( 以 nestjs 為例 )

// member.controller.ts
import { Business, BusinessModel, Input, validate } from '@master/business';
import { Controller, Post, Res, Body } from '@nestjs/common';

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();
		// 驗證
		if(errors.length) {
			// 欄位錯誤
			res.status(400).send(errors);
		} else {
			// 欄位正確
			// 註冊相關業務邏輯 ...
            this.memberService.signUp(signingUp);
            res.status(200).send(signingUp);
        }
    }
}

前端發出的資料 ( Input )

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

處理中的模型狀態 ( Model )

{
    name: "joy",
    address: {
        city: "taipei",
        district: "zhongshan",
        street: "my home"
    },
    a: 1,
    b: 2
}

前端接收到的結果 ( Output )

{
    name: "joy",
    address: {
        city: "taipei"
    }
}

@Input 定義範例

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

    @Input()
    num: number;

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

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

專有名詞

  • DAO ( Data Access Object ):資料存取物件
  • BM ( Business Model ):業務模型

獨創名詞

  • BD ( Business Decorator ):業務裝飾器
  • DID ( Data Input Decorator ):資料輸入裝飾器
  • DOD ( Data Output Decorator ):資料輸出裝飾器
  • BLA ( Business Logic Artifact ):業務邏輯產物

解決問題

  • 提供開發者豐富的存取介面
  • 遵循 DRY 原則 ( Don't repeat yourself )
  • 省去 Request DTO 與 Response DTO 資料結構的定義
  • 資料結構專注於一個介面
  • 減少程式碼撰寫
  • 無需額外定義變數來承接結果
  • 無需撰寫特定

FAQs

Package last updated on 09 Feb 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