index.d.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import * as EggApplication from 'egg';
  2. import * as mongoose from 'mongoose';
  3. declare module 'egg' {
  4. type MongooseModels = {
  5. [key: string]: mongoose.Model<any>
  6. };
  7. type MongooseSingleton = {
  8. clients: Map<string, mongoose.Connection>,
  9. get (id: string) : mongoose.Connection
  10. };
  11. type MongooseConfig = {
  12. url: string,
  13. options?: mongoose.ConnectionOptions
  14. };
  15. // extend app
  16. interface Application {
  17. mongooseDB: mongoose.Connection | MongooseSingleton;
  18. mongoose: typeof mongoose;
  19. model: MongooseModels;
  20. tenantModel(tenant: string): MongooseModels;
  21. }
  22. // extend context
  23. interface Context {
  24. model: MongooseModels;
  25. /**
  26. * compose from ctx.query and ctx.request.body
  27. */
  28. requestparam: any;
  29. /**
  30. * 租户ID,用于多租户系统
  31. */
  32. tenant: string;
  33. /**
  34. * 当前用户ID
  35. */
  36. userid: string;
  37. /**
  38. * 当前用户角色
  39. */
  40. role: string;
  41. /**
  42. * 当前用户标签
  43. */
  44. tags: string;
  45. // 返回JSON结果
  46. json(errcode: number, errmsg: string, data: object);
  47. success(message: string, data: object);
  48. fail(errcode: number, errmsg: string, details: any);
  49. /**
  50. * same to success(message: string, data: object);
  51. */
  52. ok(message: string, data: object);
  53. }
  54. // extend your config
  55. interface EggAppConfig {
  56. mongoose: {
  57. url?: string,
  58. options?: mongoose.ConnectionOptions,
  59. client?: MongooseConfig,
  60. clients?: {
  61. [key: string]: MongooseConfig
  62. }
  63. };
  64. }
  65. }
  66. import { NafService } from './naf-service';
  67. export { NafService } from './naf-service';
  68. /**
  69. * CrudService is extending from {@link NafService} ,
  70. */
  71. export class CrudService extends NafService { }
  72. export class AxiosService extends EggApplication.Service {
  73. constructor(ctx: EggApplication.Context, meta: object, options: object = {});
  74. async httpGet(uri: string, query: object, options);
  75. async httpPost(uri: string, query: object = {}, data: object, options);
  76. async request(uri: string, query: object, data: object, options);
  77. }