How to use the midway.inject function in midway

To help you get started, we’ve selected a few midway 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 ykfe / egg-react-ssr / example / ssr-with-ts / src / app / controller / page.ts View on Github external
import { controller, get, provide, inject, Context } from 'midway'
import { Config } from 'ykfe-utils'
import renderToStream from 'ykfe-utils/lib/renderToStream'
import { IApiService } from '../../interface'

const ssrConfig: Config = require('../../../config/config.ssr')

@provide()
@controller('/')
export class Page {

  @inject()
  ctx: Context

  @inject('ApiService')
  service: IApiService

  async index () {
    try {
      // Page为webpack打包的chunkName,项目默认的entry为Page
      this.ctx.type = 'text/html'
      this.ctx.status = 200
      this.ctx.apiService = this.service.index // 将service挂载到上下文对象
      const config = Object.assign(this.ctx.app.config, ssrConfig)
      const stream = await renderToStream(this.ctx, config)
      this.ctx.res.write('')
      this.ctx.body = stream
    } catch (error) {
      this.ctx.logger.error(`Page Controller renderToStream Error`, error)
    }
  }
github ykfe / egg-react-ssr / example / ssr-with-ts / src / app / controller / api.ts View on Github external
import { controller, provide, inject, Context, get } from 'midway'
import { IApiService } from '../../interface'
@provide()
@controller('/api')
export class Api {
  @inject()
    ctx: Context
  @inject('ApiService')
    service: IApiService
  @get('/getIndexData')
    async index () {
    try {
            // Page为webpack打包的chunkName,项目默认的entry为Page
      this.ctx.type = 'text/json'
      this.ctx.status = 200
      this.ctx.body = await this.service.index()
    } catch (error) {
      this.ctx.logger.error(`Page Controller renderToStream Error`, error)
    }
  }
}
github midwayjs / midway-examples / demo-extended-koa / src / app / controller / user.ts View on Github external
import { controller, get, inject } from 'midway';
import { IUserAbstract, IUserResult } from '../../lib/interfaces/user.abstract';

type nextDefinition = () => void;

@controller('/user/')
export class UserController {
  @inject('userService')
  service: IUserAbstract;

  @get('/:id')
  async getUser(ctx: any, next: nextDefinition): Promise {
    const id: number = ctx.request.params('id');
    const user: IUserResult = await this.service.getUser({id});
    ctx.body = {success: true, message: 'OK', data: user};
    // do not response again
    await next();
  }
}
github midwayjs / midway / benchmark / fixtures / midway-request-scope / src / app / controller / user.ts View on Github external
import { controller, get, inject, provide } from 'midway';
import { IUserResult, IUserService } from '../../interface';

@provide()
@controller('/user')
export class UserController {
  @inject('userService')
  service: IUserService;

  @get('/:id')
  async getUser(ctx): Promise {
    const id: number = ctx.params.id;
    const user: IUserResult = await this.service.getUser({id});
    ctx.body = {success: true, message: 'OK', data: user};
  }
}
github midwayjs / midway-examples / demo-sequelize / src / lib / service / post.ts View on Github external
import { inject, provide } from 'midway';
import { IPostModel } from '../model/post';
import { IListQueryOpt, IListQueryOptions, IPostService } from './post.i';

@provide('postService')
export class PostService implements IPostService {
  @inject('PostModel')
  public model: IPostModel;

  public async list({ offset = 0, limit = 10, title }: IListQueryOptions) {
    const options: IListQueryOpt = {
      offset,
      limit,
      attributes: ['id', 'title', 'content', 'created_at', 'updated_at'],
      order: [['updated_at', 'desc'], ['id', 'desc']],
    };
    if (title) {
      options.where = {
        title,
      };
    }
    return this.model.findAndCountAll(options);
  }
github alibaba / ice / packages / ice-midway-template / templates / server / src / app / controller / user.ts View on Github external
import { controller, get, post, inject, provide } from 'midway';
import { IUserService } from '../../interface';

@provide()
@controller('/user')
export class UserController {
  @inject('userService')
  service: IUserService;

  /**
   * GET /user/profile
   */
  @get('/profile')
  async profile(ctx) {
    const res = await this.service.profile();
    ctx.body = res.data;
  }

  /**
   * POST /user/login
   */
  @post('/login')
  async login(ctx) {
github alibaba / ice / packages / ice-midway-template / templates / server / src / lib / service / user.ts View on Github external
import { provide, inject } from 'midway';
import { IUserService, IUserProfile } from '../../interface';

@provide('userService')
export class UserService implements IUserService {
  @inject('userModel')
  model;

  async profile(): Promise {
    const data = await this.model.findUserProfile();
    return { data };
  }
}
github midwayjs / midway-examples / demo-plugin-egg-graphql / src / app / controller / user.ts View on Github external
import { controller, get, inject, provide } from 'midway';
import { IUserService, IUserResult } from '../../interface';

@provide()
@controller('/user')
export class UserController {
  @inject('userService')
  service: IUserService;

  @get('/:id')
  async getUser(ctx): Promise {
    const id: number = ctx.params.id;
    const user: IUserResult = await this.service.getUser({id});
    ctx.body = {success: true, message: 'OK', data: user};
  }
}
github midwayjs / midway / packages / midway-init / boilerplate / midway-ts-boilerplate / boilerplate / src / app / controller / user.ts View on Github external
import { Context, controller, get, inject, provide } from 'midway';
import { IUserService, IUserResult } from '../../interface';

@provide()
@controller('/user')
export class UserController {

  @inject()
  ctx: Context;

  @inject('userService')
  service: IUserService;

  @get('/:id')
  async getUser(): Promise {
    const id: number = this.ctx.params.id;
    const user: IUserResult = await this.service.getUser({id});
    this.ctx.body = {success: true, message: 'OK', data: user};
  }
}
github midwayjs / midway-examples / demo-sequelize / src / app / controller / post.ts View on Github external
/**
 * midway sequelize 使用范例
 * 参考
 * - https://github.com/midwayjs/midway-examples/tree/master/demo-sequelize
 * - http://docs.sequelizejs.com/manual/typescript.html
 */
import { controller, del, get, inject, patch, post, provide } from 'midway';
import { IPostService } from '../../interface';

@provide()
@controller('/post/')
export class PostController {
  @inject('postService')
  public service: IPostService;

  /**
   * GET /post
   */
  @get('/')
  public async index(ctx) {
    const query = {
      limit: parseInt(ctx.query.limit, 10) || 10,
      offset: parseInt(ctx.query.offset, 10) || 0,
    };
    ctx.body = await this.service.list(query);
  }

  /**
   * GET /post/:id

midway

A Node.js Serverless Framework for front-end/full-stack developers. Build the application for next decade.

MIT
Latest version published 3 years ago

Package Health Score

60 / 100
Full package analysis