How to use midway-core - 10 common examples

To help you get started, we’ve selected a few midway-core 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 midwayjs / midway / packages / midway-web / src / loader / webLoader.ts View on Github external
protected loadApplicationContext(): void {
    // this.app.options.container 测试用例编写方便点
    const containerConfig = this.config.container || this.app.options.container || {};
    if (!containerConfig.loadDir) {
      // 如果没有配置,默认就把扫描目录改到 /src or /dist
      containerConfig.baseDir = this.baseDir;
    }
    // 在 super constructor 中会调用到getAppInfo,之后会被赋值
    // 如果是typescript会加上 dist 或者 src 目录
    this.containerLoader = new ContainerLoader({
      baseDir: this.appDir,
      isTsMode: this.isTsMode
    });
    this.containerLoader.initialize();
    this.applicationContext.registerObject('appDir', this.appDir);
    // 如果没有关闭autoLoad 则进行load
    this.containerLoader.loadDirectory(containerConfig);

    // register handler for container
    this.containerLoader.registerHook(MidwayHandlerKey.CONFIG, (key: string) => {
      return safelyGet(key, this.config);
    });

    this.containerLoader.registerHook(MidwayHandlerKey.PLUGIN, (key: string) => {
      return this.app[key] || this.pluginContext.get(key);
    });
github midwayjs / midway-faas / packages / faas / src / starter.ts View on Github external
registerDecorator() {
    // register handler for container
    this.loader.registerHook(MidwayHandlerKey.CONFIG, key => {
      return this.globalConfig[key];
    });

    this.loader.registerHook(MidwayHandlerKey.PLUGIN, (key, target) => {
      const ctx = target[REQUEST_OBJ_CTX_KEY] || {};
      return ctx[key];
    });

    this.loader.registerHook(MidwayHandlerKey.LOGGER, (key, target) => {
      const ctx = target[REQUEST_OBJ_CTX_KEY] || {};
      return ctx['logger'];
    });
  }
github midwayjs / midway / packages / midway-web / src / loader / webLoader.ts View on Github external
// 如果没有配置,默认就把扫描目录改到 /src or /dist
      containerConfig.baseDir = this.baseDir;
    }
    // 在 super constructor 中会调用到getAppInfo,之后会被赋值
    // 如果是typescript会加上 dist 或者 src 目录
    this.containerLoader = new ContainerLoader({
      baseDir: this.appDir,
      isTsMode: this.isTsMode
    });
    this.containerLoader.initialize();
    this.applicationContext.registerObject('appDir', this.appDir);
    // 如果没有关闭autoLoad 则进行load
    this.containerLoader.loadDirectory(containerConfig);

    // register handler for container
    this.containerLoader.registerHook(MidwayHandlerKey.CONFIG, (key: string) => {
      return safelyGet(key, this.config);
    });

    this.containerLoader.registerHook(MidwayHandlerKey.PLUGIN, (key: string) => {
      return this.app[key] || this.pluginContext.get(key);
    });

    this.containerLoader.registerHook(MidwayHandlerKey.LOGGER, (key: string) => {
      if (this.app.getLogger) {
        return this.app.getLogger(key);
      }
      return this.options.logger;
    });
  }
github midwayjs / midway / packages / midway-web / src / loader / webLoader.ts View on Github external
});
    this.containerLoader.initialize();
    this.applicationContext.registerObject('appDir', this.appDir);
    // 如果没有关闭autoLoad 则进行load
    this.containerLoader.loadDirectory(containerConfig);

    // register handler for container
    this.containerLoader.registerHook(MidwayHandlerKey.CONFIG, (key: string) => {
      return safelyGet(key, this.config);
    });

    this.containerLoader.registerHook(MidwayHandlerKey.PLUGIN, (key: string) => {
      return this.app[key] || this.pluginContext.get(key);
    });

    this.containerLoader.registerHook(MidwayHandlerKey.LOGGER, (key: string) => {
      if (this.app.getLogger) {
        return this.app.getLogger(key);
      }
      return this.options.logger;
    });
  }
github midwayjs / midway-faas / packages / faas / src / starter.ts View on Github external
registerDecorator() {
    // register handler for container
    this.loader.registerHook(MidwayHandlerKey.CONFIG, key => {
      return this.globalConfig[key];
    });

    this.loader.registerHook(MidwayHandlerKey.PLUGIN, (key, target) => {
      const ctx = target[REQUEST_OBJ_CTX_KEY] || {};
      return ctx[key];
    });

    this.loader.registerHook(MidwayHandlerKey.LOGGER, (key, target) => {
      const ctx = target[REQUEST_OBJ_CTX_KEY] || {};
      return ctx['logger'];
    });
  }
github midwayjs / midway / packages / midway-web / src / loader / webLoader.ts View on Github external
// 如果是typescript会加上 dist 或者 src 目录
    this.containerLoader = new ContainerLoader({
      baseDir: this.appDir,
      isTsMode: this.isTsMode
    });
    this.containerLoader.initialize();
    this.applicationContext.registerObject('appDir', this.appDir);
    // 如果没有关闭autoLoad 则进行load
    this.containerLoader.loadDirectory(containerConfig);

    // register handler for container
    this.containerLoader.registerHook(MidwayHandlerKey.CONFIG, (key: string) => {
      return safelyGet(key, this.config);
    });

    this.containerLoader.registerHook(MidwayHandlerKey.PLUGIN, (key: string) => {
      return this.app[key] || this.pluginContext.get(key);
    });

    this.containerLoader.registerHook(MidwayHandlerKey.LOGGER, (key: string) => {
      if (this.app.getLogger) {
        return this.app.getLogger(key);
      }
      return this.options.logger;
    });
  }
github midwayjs / midway-faas / packages / faas / src / starter.ts View on Github external
registerDecorator() {
    // register handler for container
    this.loader.registerHook(MidwayHandlerKey.CONFIG, key => {
      return this.globalConfig[key];
    });

    this.loader.registerHook(MidwayHandlerKey.PLUGIN, (key, target) => {
      const ctx = target[REQUEST_OBJ_CTX_KEY] || {};
      return ctx[key];
    });

    this.loader.registerHook(MidwayHandlerKey.LOGGER, (key, target) => {
      const ctx = target[REQUEST_OBJ_CTX_KEY] || {};
      return ctx['logger'];
    });
  }
github midwayjs / midway / packages / midway-web / app / extend / context.js View on Github external
get requestContext() {
    if (!this[rc]) {
      this[rc] = new MidwayRequestContainer(this.app.applicationContext, this);
      this[rc].ready();
    }
    return this[rc];
  },
};
github midwayjs / midway-faas / packages / faas / src / starter.ts View on Github external
getContext(context) {
    if (!context.requestContext) {
      context.requestContext = new MidwayRequestContainer(
        this.loader.getApplicationContext(),
        context
      );
    }
    return context;
  }
}
github midwayjs / midway-faas / packages / faas / src / starter.ts View on Github external
constructor(
    options: {
      baseDir?: string;
      config?: object;
      typescript?: boolean;
      preloadModules?: any[];
      logger?: any;
    } = {}
  ) {
    this.appDir = options.baseDir || process.cwd();
    this.globalConfig = options.config || {};
    this.logger = options.logger || console;
    this.isTsMode = this.getTsMode(options.typescript);
    this.baseDir = this.getBaseDir();

    this.loader = new ContainerLoader({
      baseDir: this.baseDir,
      isTsMode: this.isTsMode,
      preloadModules: options.preloadModules || [],
    });
    this.loader.initialize();
  }