How to use injection-js - 10 common examples

To help you get started, we’ve selected a few injection-js examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ng-packagr / ng-packagr / src / lib / steps / entry-point-transforms.ts View on Github external
log.info('Writing package metadata');
    const relativeDestPath: string = path.relative(entryPoint.destinationPath, pkg.primary.destinationPath);
    await writePackage(entryPoint, {
      main: ensureUnixPath(path.join(relativeDestPath, 'bundles', entryPoint.flatModuleFile + '.umd.js')),
      module: ensureUnixPath(path.join(relativeDestPath, 'esm5', entryPoint.flatModuleFile + '.js')),
      es2015: ensureUnixPath(path.join(relativeDestPath, 'esm2015', entryPoint.flatModuleFile + '.js')),
      typings: ensureUnixPath(`${entryPoint.flatModuleFile}.d.ts`),
      // XX 'metadata' property in 'package.json' is non-standard. Keep it anyway?
      metadata: ensureUnixPath(`${entryPoint.flatModuleFile}.metadata.json`)
    });

    log.success(`Built ${entryPoint.moduleId}`);
  };
}

export const ENTRY_POINT_TRANSFORMS_TOKEN = new InjectionToken('ng.v5.entryPointTransforms');

export const ENTRY_POINT_TRANSFORMS_PROVIDER: FactoryProvider = {
  provide: ENTRY_POINT_TRANSFORMS_TOKEN,
  useFactory: transformSourcesFactory,
  deps: [INIT_TS_CONFIG_TOKEN]
};
github Hotell / blogposts / 2018-06 / ts-react-di / src / app / inject.tsx View on Github external
import React, { Component, ReactNode, createContext, PureComponent, ComponentType } from 'react'
import {
  Type,
  ReflectiveInjector,
  Provider as ProviderConfig,
  Injector,
  InjectionToken,
} from 'injection-js'
import { isType, isProvider, isObject } from './guards'

const rootInjector = ReflectiveInjector.resolveAndCreate([])
// let lastInjector: ReflectiveInjector

const Context = createContext(rootInjector)

type ProviderProps = { provide: ProviderConfig[] }
export class Provider extends Component {
  private static debugMode = {
    on: false,
  }
  static enableDebugMode() {
    this.debugMode.on = true
  }
  private static Debug = (props: {
    parentInjector: ReflectiveInjector
    children: ReactNode
    registeredProviders: ProviderConfig[]
github ng-packagr / ng-packagr / src / lib / ng-v5 / packagr.ts View on Github external
public buildAsObservable(): Observable {
    const injector = ReflectiveInjector.resolveAndCreate(this.providers);
    const buildTransformOperator = injector.get(this.buildTransform);

    return observableOf(new BuildGraph()).pipe(
      buildTransformOperator,
      catchError(err => {
        // Report error and re-throw to subscribers
        log.error(err);
        return throwError(err);
      }),
      map(() => undefined),
    );
  }
}
github mflorence99 / serverx-ts / src / router.ts View on Github external
// find matching route, fabricating if necessary
    let route = this.matchImpl(paths, rpaths, method, parent, routes, params);
    // we always need a handler
    if (!route.handler) {
      if (route.children)
        route.handler = NotFound;
      else route.handler = route.redirectTo? RedirectTo : StatusCode200;
    }
    // create an injector
    if (!route.injector) {
      const providers = (route.services || []).concat(route.middlewares || []);
      providers.push(route.handler);
      const resolved = ReflectiveInjector.resolve(providers);
      if (parent)
        route.injector = parent.injector.createChildFromResolved(resolved);
      else route.injector = ReflectiveInjector.fromResolvedProviders(resolved);
    }
    // look to the children
    if (route.children && (paths.length > rpaths.length))
      route = this.match(paths.slice(rpaths.length), method, route, route.children, params);
    return route;
  }
github Hotell / blogposts / 2018-06 / ts-react-di / src / app / core / logger.service.ts View on Github external
constructor(
    @Optional()
    @Inject(LoggerConfig)
    protected config: Config
  ) {
    if (!config) {
      this.config = defaultConfig
    }
  }
  protected logs: string[] = [] // capture logs for testing
github mflorence99 / serverx-ts / src / router.ts View on Github external
routes: Route[],
                params: Map): Route | undefined {
    const rpaths = [];
    // find matching route, fabricating if necessary
    let route = this.matchImpl(paths, rpaths, method, parent, routes, params);
    // we always need a handler
    if (!route.handler) {
      if (route.children)
        route.handler = NotFound;
      else route.handler = route.redirectTo? RedirectTo : StatusCode200;
    }
    // create an injector
    if (!route.injector) {
      const providers = (route.services || []).concat(route.middlewares || []);
      providers.push(route.handler);
      const resolved = ReflectiveInjector.resolve(providers);
      if (parent)
        route.injector = parent.injector.createChildFromResolved(resolved);
      else route.injector = ReflectiveInjector.fromResolvedProviders(resolved);
    }
    // look to the children
    if (route.children && (paths.length > rpaths.length))
      route = this.match(paths.slice(rpaths.length), method, route, route.children, params);
    return route;
  }
github Hotell / blogposts / 2018-06 / ts-react-di / src / app / core / logger.service.ts View on Github external
constructor(
    @Optional()
    @Inject(LoggerConfig)
    protected config: Config
  ) {
    if (!config) {
      this.config = defaultConfig
    }
  }
  protected logs: string[] = [] // capture logs for testing
github Hotell / rea-di / src / __tests__ / setup / services.ts View on Github external
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()
github Hotell / rea-di / src / __tests__ / components / provide-inject.spec.tsx View on Github external
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) {}
    }