Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import { Injectable } from 'injection-js'
import { Stateful } from '../../services/stateful'
@Injectable()
export class Logger {
log(...args: any[]) {
console.log(...args)
}
}
type State = Readonly<{
count: number
}>
@Injectable()
export class CounterService extends Stateful {
readonly state: State = { count: 0 }
constructor(private logger: Logger) {
super()
import {Injectable} from 'injection-js'
@Injectable()
export class CarService {
}
describe(`@Optional/optional()`, () => {
@Injectable()
class Engine {
type?: string
}
@Injectable()
class Car {
engine: Engine | null
constructor(@Optional() engine: Engine) {
this.engine = engine ? engine : null
}
}
@Injectable()
class CarWillCrashWithoutEngine {
constructor(public engine: Engine) {}
}
import {Injectable} from 'injection-js';
import {Observe} from '@rxstack/async-event-dispatcher';
import {
ApplicationEvents, BootstrapEvent, HttpMetadata, httpMetadataStorage, WebSocketMetadata,
webSocketMetadataStorage, ResponseEvent, KernelEvents
} from '@rxstack/core';
import {OperationMetadata} from '../metadata/operation.metadata';
import {PLATFORM_OPERATION_KEY} from '../interfaces';
import {AbstractOperation} from '../operations/abstract-operation';
import {Exception} from '@rxstack/exceptions';
@Injectable()
export class ResourceListener {
@Observe(ApplicationEvents.BOOTSTRAP)
async onBootstrap(event: BootstrapEvent): Promise {
const injector = event.injector;
event.resolvedProviders.forEach((provider) => {
const service = injector.get(provider.key);
if (service instanceof AbstractOperation) {
this.register(service);
}
});
}
@Observe(KernelEvents.KERNEL_RESPONSE, 100)
async onResponse(event: ResponseEvent): Promise {
if (event.getRequest().attributes.has('pagination')) {
import {Injectable} from 'injection-js';
import {PasswordEncoderInterface} from '../interfaces';
const bcrypt = require('bcryptjs');
@Injectable()
export class BcryptPasswordEncoder implements PasswordEncoderInterface {
static readonly ENCODER_NAME = 'bcrypt';
async encodePassword(raw: string): Promise {
return bcrypt.hashSync(raw, 10);
}
async isPasswordValid(encoded: string, raw: string): Promise {
return bcrypt.compareSync(raw, encoded);
}
getName(): string {
return BcryptPasswordEncoder.ENCODER_NAME;
}
}
import * as _ from 'lodash';
import {Injectable} from 'injection-js';
import {UserFactoryFunc, UserProviderInterface} from '../interfaces';
import {UserNotFoundException} from '../exceptions/index';
import {UserInterface} from '@rxstack/core';
@Injectable()
export class InMemoryUserProvider implements UserProviderInterface {
static readonly PROVIDER_NAME = 'in-memory';
private users: T[] = [];
constructor(data: any, factory: UserFactoryFunc) {
data.forEach((item: any) => this.users.push(factory(item)));
}
async loadUserByUsername(username: string): Promise {
const user = _.find(this.users, ['username', username]);
if (!user) throw new UserNotFoundException(username);
return user as T;
}
import { Stateful } from '@martin_hotell/rea-di'
import { Injectable } from 'injection-js'
import { getClassName } from './helpers'
import { Logger } from './logger.service'
type State = Readonly
const initialState = {
count: 0,
}
@Injectable()
export class CounterService extends Stateful {
readonly state = initialState
constructor(public logger: Logger) {
super()
}
get value() {
return this.state.count
}
onIncrement() {
this.setState((prevState) => ({ count: prevState.count + 1 }))
this.logger.log(
`${getClassName(this)}: increment called and count set to: ${
this.state.count
import {UserNotFoundException, UserProviderInterface} from '@rxstack/security';
import {UserInterface} from '@rxstack/core';
import {Injectable} from 'injection-js';
import {ServiceInterface} from '../interfaces';
@Injectable()
export class UserProvider implements UserProviderInterface {
static readonly PROVIDE_NAME = 'platform-service';
constructor(private service: ServiceInterface) { }
async loadUserByUsername(username: string): Promise {
const user = await this.service.findOne({'username': {'$eq': username}});
if (user) return user; else throw new UserNotFoundException(username);
}
getName(): string {
return UserProvider.PROVIDE_NAME;
}
}
export function RestController(target: Function){
const dependencies = Reflect.getMetadata("design:paramtypes", target);
let controllerName = target["name"];
let controllerHandler: ControllerHandler = Container.controllerHandlers[controllerName];
if (!controllerHandler) {
controllerHandler = new ControllerHandler();
}
controllerHandler.isRest = true;
Injectable().call(null, target);
Container.components[controllerName] = target;
}
import { ALL_METHODS } from '../interfaces';
import { Handler } from '../handler';
import { Injectable } from 'injection-js';
import { Message } from '../interfaces';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
/**
* Catch all "not found" handler
*/
@Injectable() export class NotFound extends Handler {
handle(message$: Observable): Observable {
return message$.pipe(
tap(({ request, response }) => {
if (request.method === 'OPTIONS') {
response.headers['Allow'] = ALL_METHODS.join(',');
response.statusCode = 200;
}
else response.statusCode = response.statusCode || 404;
})
);
}
}