JSPM

  • Created
  • Published
  • Downloads 7
  • Score
    100M100P100Q46540F
  • License MIT

front end SPA frameworks

Package Exports

  • simple-boot-core
  • simple-boot-core/SimOption
  • simple-boot-core/SimOption.js
  • simple-boot-core/SimpleApplication
  • simple-boot-core/SimpleApplication.js
  • simple-boot-core/decorators/SimDecorator
  • simple-boot-core/decorators/SimDecorator.js
  • simple-boot-core/decorators/aop/AOPDecorator
  • simple-boot-core/decorators/aop/AOPDecorator.js
  • simple-boot-core/decorators/event/EventListener
  • simple-boot-core/decorators/event/EventListener.js
  • simple-boot-core/decorators/exception/ExceptionDecorator
  • simple-boot-core/decorators/exception/ExceptionDecorator.js
  • simple-boot-core/decorators/inject/Inject
  • simple-boot-core/decorators/inject/Inject.js
  • simple-boot-core/decorators/route/Router
  • simple-boot-core/decorators/route/Router.js
  • simple-boot-core/decorators/validate/Validation
  • simple-boot-core/decorators/validate/Validation.js
  • simple-boot-core/errors/ValidException
  • simple-boot-core/errors/ValidException.js
  • simple-boot-core/intent/Intent
  • simple-boot-core/intent/Intent.js
  • simple-boot-core/intent/IntentManager
  • simple-boot-core/intent/IntentManager.js
  • simple-boot-core/queues/AsyncBlockingQueue
  • simple-boot-core/queues/AsyncBlockingQueue.js
  • simple-boot-core/route/RouterManager
  • simple-boot-core/route/RouterManager.js
  • simple-boot-core/route/RouterModule
  • simple-boot-core/route/RouterModule.js
  • simple-boot-core/simstance/SimAtomic
  • simple-boot-core/simstance/SimAtomic.js
  • simple-boot-core/simstance/SimstanceManager
  • simple-boot-core/simstance/SimstanceManager.js
  • simple-boot-core/throwable/SimError
  • simple-boot-core/throwable/SimError.js
  • simple-boot-core/utils/function/FunctionUtils
  • simple-boot-core/utils/function/FunctionUtils.js
  • simple-boot-core/utils/object/ObjectUtils
  • simple-boot-core/utils/object/ObjectUtils.js
  • simple-boot-core/utils/random/RandomUtils
  • simple-boot-core/utils/random/RandomUtils.js
  • simple-boot-core/utils/reflect/ReflectUtils
  • simple-boot-core/utils/reflect/ReflectUtils.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (simple-boot-core) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

SIMPLE-BOOT-CORE

It provides convenience in service development.

typescript typescript license

  • Object management.
    • Dependency Injection (DI)
    • Life cycle provided.
  • Aspect Oriented Programming (AOP)
  • ExceptionHandler (Global or Local)
  • Router System
  • Intent Event System

🚀 Quick start

npm init -y
npm install simple-boot-core
tsc --init --experimentalDecorators --emitDecoratorMetadata
// index.ts
import {SimpleApplication} from 'simple-boot-core';
import {Router} from 'simple-boot-core/decorators/route/Router';
import {Sim} from 'simple-boot-core/decorators/SimDecorator';
@Sim
class User {
  say() {
    console.log('say~ hello');
  }
}

@Sim @Router({ path: '', route: {'/user': User}})
class AppRouter {}

const app = new SimpleApplication(AppRouter);

// type 1
app.run();
app.sim(User).say();

// type 2
// app.run().getOrNewSim(User).say();

// type 3
// app.run();
// const atomic = app.simAtomic(User);
// atomic.value.say();

// type 4 routing
// app.run();
// app.routing('/user').then(it => {
//     it.getModuleInstance<User>()?.say();
// })
npx ts-node index.ts
# say~ hello

😃 examples

Object management.

Dependency Injection (DI) 🔻(click)

@Sim

Decorators must be declared to be managed.

@Sim
class ProjectService {
  sum(x: number, y: number) {
    return x + y;
  }
}

@Sim
class User {

  constructor(private projectService: ProjectService) {
  }

  say() {
    console.log(`say~ hello: ${this.projectService.sum(5, 25)}`);
  }
}
// 💥 call say()
// say~ hello: 30

SimConfig

export enum Lifecycle {
  /**
   * The default registration scope, Each resolve will return the same instance (including resolves from child containers)
   */
  Singleton = 'Singleton',
  /**
   * a new instance will be created with each resolve
   */
  Transient = 'Transient'
}

export interface SimConfig {
  symbol?: Symbol | (Symbol[]);
  scheme?: string | (string[]);
  scope?: Lifecycle;
  autoStart?: boolean;  // auto start = auto new
  proxy?: ((ProxyHandler<any> | ConstructorType<any> | Function)) | (ProxyHandler<any> | ConstructorType<any> | Function)[];
  type?: (ConstructorType<any> | Function) | (ConstructorType<any> | Function)[];
  using?: (ConstructorType<any> | Function) | (ConstructorType<any> | Function)[];
}
@Sim({...config})
class test {}
Life cycle provided 🔻(click)

OnSimCreate interface

Sim Object created just one call

@Sim
class User implements OnSimCreate {
  onSimCreate(): void {
    console.log('on Create')
  }
}
// output 💥
// on Create

Aspect Oriented Programming (AOP)

Method call AOP 🔻(click)

@Before @After

@Sim
class User {

  @Before({property: 'say'})
  sayBefore() {
    console.log('sayBefore')
  }

  @After({property: 'say'})
  sayAfter() {
    console.log('sayAfter')
  }

  say() {
    console.log(`say~ hello`);
  }
}
// 💥 call say()
// sayBefore
// say~ hello
// sayAfter

ExceptionHandler (Global or Local)

Local Exception Advice 🔻(click)

@ExceptionHandler

@Sim
class User {

  @ExceptionHandler()
  otherException(@Inject({situationType: ExceptionHandlerSituationType.ERROR_OBJECT}) e: any) {
    console.log(`otherException : ${e.message}`)
  }

  @ExceptionHandler({type: Error})
  errorTypeException(e: Error) {
    console.log(`errorTypeException : ${e.message}`)
  }

  say1() {
    console.log(`say~ hello`);
    throw {message: 'otherException'}
  }

  say2() {
    console.log(`say~ hello`);
    throw new Error('error');
  }

}

// 💥 call say1()
// say~ hello
// { message: 'otherException' }
// otherException : otherException

// 💥 call say2()
// say~ hello
// Error: error at ...
// errorTypeException : error
Global Exception Advice 🔻(click)
@Sim
class GlobalAdvice {
  @ExceptionHandler()
  otherException(@Inject({situationType: ExceptionHandlerSituationType.ERROR_OBJECT}) e: any) {
    console.log(`otherException : ${e.message}`)
  }

  @ExceptionHandler({type: Error})
  errorTypeException(e: Error) {
    console.log(`errorTypeException : ${e.message}`)
  }
}

@Sim
class User {
  say1() {
    console.log(`say~ hello`);
    throw {message: 'otherException'}
  }

  say2() {
    console.log(`say~ hello`);
    throw new Error('error');
  }

}
const option = new SimOption([GlobalAdvice])
new SimpleApplication(AppRouter, option).run().routing('/user').then(it => {
  it.getModuleInstance<User>()?.say1();
})
// 💥 call say1()
// say~ hello
// { message: 'otherException' }
// otherException : otherException

// 💥 call say2()
// say~ hello
// Error: error at ...
// errorTypeException : error

Route System

@Router🔻(click)
@Sim
@Router({
  path: '',
  route: {
    '/user': User
  }
})
class AppRouter {
}
RouterAction Interface🔻(click)

route change call canActivate meehod

@Sim
@Router({
  path: '',
  route: {
    '/user': User
  }
})
class AppRouter implements RouterAction {

  async canActivate(url: Intent, module: any) {
    console.log('--', url, module)
  }

}

const option = new SimOption([GlobalAdvice])
new SimpleApplication(AppRouter, option).run().routing('/user').then(it => {
  it.getModuleInstance<User>()?.say();
})
// output 💥
// -- Intent { uri: '/user', data: undefined, event: undefined } User { say: [Function (anonymous)], say2: [Function (anonymous)] }
// say~ hello
@Route🔻(click)
@Sim
@Router({
  path: ''
})
class AppRouter {
    
  @Route({path:'/user'})
  user1() {
    console.log('user say1~')
  }
  
  @Route({path:'/user'})
  user2() {
    console.log('user say2~')
  }
  
  @Route({path:'/user-props'})
  user2(props: string) {
    console.log('user propss', props)
  }
}

const option = new SimOption([GlobalAdvice])
new SimpleApplication(AppRouter, option).run().routing('/user').then(it => {
  it.propertyKeys?.forEach(key => {
    it.executeModuleProperty(key);
  });
})
new SimpleApplication(AppRouter, option).run().routing('/user-props').then(it => {
  // direct call
  let propertyKey = it.propertyKeys?.[0];
  let moduleInstance = routerModule.getModuleInstance<(props: string) => void>(propertyKey);
  moduleInstance('propData');
})
// output 💥
// user say1~
// user say2~

Intent Event System

Intent Message🔻(click)
  • transmit data between objects and generate events
  • send data and generate events to @Sim scheme
    • Support Object transmission
    • Support query parameters
    • Allocate directly to variables
    • Calling the method
@Sim({scheme: 'AppRouter'}) @Router({path: '',route: {'/user': User}})
class AppRouter {
  say(intent: Intent) {
    console.log('say1-->', intent.data);
  }
}
const app = new SimpleApplication(AppRouter).run();
app.publishIntent(new Intent('AppRouter://say1', {name: 'visualkhh', age: 99}));
// output 💥
// say1--> { name: 'visualkhh', age: 99 }
const intent = new Intent('AppRouter://say2', ['visualkhh', 99]);
intent.publishType = PublishType.INLINE_DATA_PARAMETERS;
app.publishIntent(intent);
// output 💥
// say2--> visualkhh 99
const global = new Intent('://say2'); // <-- global intent message event
const queryParam = new Intent('scheme://say2?age=5&name=visualkhh'); // <-- query parameter
queryParam.queryParams.name;
queryParam.queryParams.age;

License