@midwayjs/core 3.17.0 → 3.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -30,6 +30,7 @@ class DataSourceManager {
30
30
  this.dataSource = new Map();
31
31
  this.options = {};
32
32
  this.modelMapping = new WeakMap();
33
+ this.dataSourcePriority = {};
33
34
  }
34
35
  async initDataSource(dataSourceConfig, baseDirOrOptions) {
35
36
  var _a;
@@ -1004,12 +1004,12 @@ export interface ServerSendEventMessage {
1004
1004
  id?: string;
1005
1005
  retry?: number;
1006
1006
  }
1007
- export interface ServerStreamOptions {
1008
- tpl?: (data: unknown) => unknown;
1007
+ export interface ServerStreamOptions<CTX extends IMidwayContext> {
1008
+ tpl?: (data: unknown, ctx: CTX) => unknown;
1009
1009
  }
1010
- export interface ServerSendEventStreamOptions {
1010
+ export interface ServerSendEventStreamOptions<CTX extends IMidwayContext> {
1011
1011
  closeEvent?: string;
1012
- tpl?: (data: ServerSendEventMessage) => ServerSendEventMessage;
1012
+ tpl?: (data: ServerSendEventMessage, ctx: CTX) => ServerSendEventMessage;
1013
1013
  }
1014
1014
  export {};
1015
1015
  //# sourceMappingURL=interface.d.ts.map
@@ -4,9 +4,9 @@ export declare class ServerResponse<CTX extends IMidwayContext = IMidwayContext>
4
4
  protected readonly ctx: any;
5
5
  protected isSuccess: boolean;
6
6
  constructor(ctx: CTX);
7
- static TEXT_TPL: (data: string, isSuccess: boolean) => unknown;
8
- static JSON_TPL: (data: Record<any, any>, isSuccess: boolean) => unknown;
9
- static BLOB_TPL: (data: Buffer, isSuccess: boolean) => unknown;
7
+ static TEXT_TPL: <CTX_1 extends import("../interface").Context>(data: string, isSuccess: boolean, ctx: CTX_1) => unknown;
8
+ static JSON_TPL: <CTX_1 extends import("../interface").Context>(data: Record<any, any>, isSuccess: boolean, ctx: CTX_1) => unknown;
9
+ static BLOB_TPL: <CTX_1 extends import("../interface").Context>(data: Buffer, isSuccess: boolean, ctx: CTX_1) => unknown;
10
10
  json(data: Record<any, any>): any;
11
11
  text(data: string): any;
12
12
  blob(data: Buffer): any;
@@ -7,13 +7,13 @@ class ServerResponse {
7
7
  this.ctx = ctx;
8
8
  }
9
9
  json(data) {
10
- return Object.getPrototypeOf(this).constructor.JSON_TPL(data, this.isSuccess);
10
+ return Object.getPrototypeOf(this).constructor.JSON_TPL(data, this.isSuccess, this.ctx);
11
11
  }
12
12
  text(data) {
13
- return Object.getPrototypeOf(this).constructor.TEXT_TPL(data, this.isSuccess);
13
+ return Object.getPrototypeOf(this).constructor.TEXT_TPL(data, this.isSuccess, this.ctx);
14
14
  }
15
15
  blob(data) {
16
- return Object.getPrototypeOf(this).constructor.BLOB_TPL(data, this.isSuccess);
16
+ return Object.getPrototypeOf(this).constructor.BLOB_TPL(data, this.isSuccess, this.ctx);
17
17
  }
18
18
  success() {
19
19
  this.isSuccess = true;
@@ -25,10 +25,10 @@ class ServerResponse {
25
25
  }
26
26
  }
27
27
  exports.ServerResponse = ServerResponse;
28
- ServerResponse.TEXT_TPL = (data, isSuccess) => {
28
+ ServerResponse.TEXT_TPL = (data, isSuccess, ctx) => {
29
29
  return data;
30
30
  };
31
- ServerResponse.JSON_TPL = (data, isSuccess) => {
31
+ ServerResponse.JSON_TPL = (data, isSuccess, ctx) => {
32
32
  if (isSuccess) {
33
33
  return {
34
34
  success: 'true',
@@ -42,7 +42,7 @@ ServerResponse.JSON_TPL = (data, isSuccess) => {
42
42
  };
43
43
  }
44
44
  };
45
- ServerResponse.BLOB_TPL = (data, isSuccess) => {
45
+ ServerResponse.BLOB_TPL = (data, isSuccess, ctx) => {
46
46
  return data;
47
47
  };
48
48
  //# sourceMappingURL=base.js.map
@@ -7,9 +7,10 @@ import { Readable } from 'stream';
7
7
  import { HttpStreamResponse } from './stream';
8
8
  export declare class HttpServerResponse<CTX extends IMidwayContext> extends ServerResponse<CTX> {
9
9
  constructor(ctx: CTX);
10
- static FILE_TPL: (data: Readable, isSuccess: boolean) => Readable;
11
- static SSE_TPL: (data: ServerSendEventMessage) => ServerSendEventMessage;
12
- static STREAM_TPL: (data: unknown) => unknown;
10
+ static FILE_TPL: <CTX_1 extends import("../interface").Context>(data: Readable, isSuccess: boolean, ctx: CTX_1) => Readable;
11
+ static SSE_TPL: <CTX_1 extends import("../interface").Context>(data: ServerSendEventMessage, ctx: CTX_1) => ServerSendEventMessage;
12
+ static STREAM_TPL: <CTX_1 extends import("../interface").Context>(data: unknown, ctx: CTX_1) => unknown;
13
+ static HTML_TPL: <CTX_1 extends import("../interface").Context>(data: string, isSuccess: boolean, ctx: CTX_1) => unknown;
13
14
  status(code: number): this;
14
15
  header(key: string, value: string): this;
15
16
  headers(headers: Record<string, string>): this;
@@ -17,7 +18,9 @@ export declare class HttpServerResponse<CTX extends IMidwayContext> extends Serv
17
18
  text(data: string): any;
18
19
  file(filePath: string, mimeType?: string): any;
19
20
  blob(data: Buffer, mimeType?: string): any;
20
- sse(options?: ServerSendEventStreamOptions): ServerSendEventStream;
21
- stream(options?: ServerStreamOptions): HttpStreamResponse;
21
+ html(data: string): any;
22
+ redirect(url: string, status?: number): any;
23
+ sse(options?: ServerSendEventStreamOptions<CTX>): ServerSendEventStream<import("../interface").Context>;
24
+ stream(options?: ServerStreamOptions<CTX>): HttpStreamResponse<import("../interface").Context>;
22
25
  }
23
26
  //# sourceMappingURL=http.d.ts.map
@@ -31,20 +31,36 @@ class HttpServerResponse extends base_1.ServerResponse {
31
31
  }
32
32
  json(data) {
33
33
  this.header('Content-Type', 'application/json');
34
- return Object.getPrototypeOf(this).constructor.JSON_TPL(data, this.isSuccess);
34
+ return Object.getPrototypeOf(this).constructor.JSON_TPL(data, this.isSuccess, this.ctx);
35
35
  }
36
36
  text(data) {
37
37
  this.header('Content-Type', 'text/plain');
38
- return Object.getPrototypeOf(this).constructor.TEXT_TPL(data, this.isSuccess);
38
+ return Object.getPrototypeOf(this).constructor.TEXT_TPL(data, this.isSuccess, this.ctx);
39
39
  }
40
40
  file(filePath, mimeType) {
41
41
  this.header('Content-Type', mimeType || 'application/octet-stream');
42
42
  this.header('Content-Disposition', `attachment; filename=${(0, path_1.basename)(filePath)}`);
43
- return Object.getPrototypeOf(this).constructor.FILE_TPL(typeof filePath === 'string' ? (0, fs_1.createReadStream)(filePath) : filePath, this.isSuccess);
43
+ return Object.getPrototypeOf(this).constructor.FILE_TPL(typeof filePath === 'string' ? (0, fs_1.createReadStream)(filePath) : filePath, this.isSuccess, this.ctx);
44
44
  }
45
45
  blob(data, mimeType) {
46
46
  this.header('Content-Type', mimeType || 'application/octet-stream');
47
- return Object.getPrototypeOf(this).constructor.BLOB_TPL(data, this.isSuccess);
47
+ return Object.getPrototypeOf(this).constructor.BLOB_TPL(data, this.isSuccess, this.ctx);
48
+ }
49
+ html(data) {
50
+ this.header('Content-Type', 'text/html');
51
+ return Object.getPrototypeOf(this).constructor.HTML_TPL(data, this.isSuccess, this.ctx);
52
+ }
53
+ redirect(url, status = 302) {
54
+ this.status(status);
55
+ if (this.ctx.redirect) {
56
+ return this.ctx.redirect(url);
57
+ }
58
+ else if (this.ctx.res.redirect) {
59
+ return this.ctx.res.redirect(url);
60
+ }
61
+ else {
62
+ this.header('Location', url);
63
+ }
48
64
  }
49
65
  sse(options = {}) {
50
66
  return new sse_1.ServerSendEventStream(this.ctx, {
@@ -60,13 +76,16 @@ class HttpServerResponse extends base_1.ServerResponse {
60
76
  }
61
77
  }
62
78
  exports.HttpServerResponse = HttpServerResponse;
63
- HttpServerResponse.FILE_TPL = (data, isSuccess) => {
79
+ HttpServerResponse.FILE_TPL = (data, isSuccess, ctx) => {
80
+ return data;
81
+ };
82
+ HttpServerResponse.SSE_TPL = (data, ctx) => {
64
83
  return data;
65
84
  };
66
- HttpServerResponse.SSE_TPL = (data) => {
85
+ HttpServerResponse.STREAM_TPL = (data, ctx) => {
67
86
  return data;
68
87
  };
69
- HttpServerResponse.STREAM_TPL = (data) => {
88
+ HttpServerResponse.HTML_TPL = (data, isSuccess, ctx) => {
70
89
  return data;
71
90
  };
72
91
  //# sourceMappingURL=http.js.map
@@ -1,18 +1,18 @@
1
1
  /// <reference types="node" />
2
2
  import { Transform } from 'stream';
3
- import { ServerSendEventStreamOptions } from '../interface';
3
+ import { IMidwayContext, ServerSendEventStreamOptions } from '../interface';
4
4
  interface MessageEvent {
5
5
  data?: string | object;
6
6
  event?: string;
7
7
  id?: string;
8
8
  retry?: number;
9
9
  }
10
- export declare class ServerSendEventStream extends Transform {
10
+ export declare class ServerSendEventStream<CTX extends IMidwayContext> extends Transform {
11
11
  private readonly ctx;
12
12
  private isActive;
13
13
  private readonly closeEvent;
14
14
  private options;
15
- constructor(ctx: any, options?: ServerSendEventStreamOptions);
15
+ constructor(ctx: any, options?: ServerSendEventStreamOptions<CTX>);
16
16
  _transform(chunk: any, encoding: any, callback: any): void;
17
17
  sendError(error: Error): void;
18
18
  sendEnd(message?: MessageEvent): void;
@@ -95,7 +95,7 @@ class ServerSendEventStream extends stream_1.Transform {
95
95
  this.send(message);
96
96
  }
97
97
  send(message) {
98
- super.write(this.options.tpl(message));
98
+ super.write(this.options.tpl(message, this.ctx));
99
99
  }
100
100
  handleClose() {
101
101
  this.end();
@@ -1,11 +1,11 @@
1
1
  /// <reference types="node" />
2
2
  import { Transform } from 'stream';
3
- import { ServerStreamOptions } from '../interface';
4
- export declare class HttpStreamResponse extends Transform {
3
+ import { IMidwayContext, ServerStreamOptions } from '../interface';
4
+ export declare class HttpStreamResponse<CTX extends IMidwayContext> extends Transform {
5
5
  private ctx;
6
6
  private isActive;
7
7
  private options;
8
- constructor(ctx: any, options?: ServerStreamOptions);
8
+ constructor(ctx: any, options?: ServerStreamOptions<CTX>);
9
9
  _transform(chunk: any, encoding: any, callback: any): void;
10
10
  send(data: unknown): void;
11
11
  sendError(error: any): void;
@@ -41,7 +41,7 @@ class HttpStreamResponse extends stream_1.Transform {
41
41
  if (!this.writable) {
42
42
  return;
43
43
  }
44
- this.write(this.options.tpl(data));
44
+ this.write(this.options.tpl(data, this.ctx));
45
45
  }
46
46
  sendError(error) {
47
47
  this.ctx.logger.error(error);
@@ -104,7 +104,7 @@ class HttpClient {
104
104
  this.defaultOptions = defaultOptions;
105
105
  }
106
106
  async request(url, options) {
107
- return makeHttpRequest(url, Object.assign(this.defaultOptions, options));
107
+ return makeHttpRequest(url, Object.assign({}, this.defaultOptions, options));
108
108
  }
109
109
  }
110
110
  exports.HttpClient = HttpClient;
@@ -26,7 +26,12 @@ const extractKoaLikeValue = (key, data, paramType) => {
26
26
  return ctx.getFileStream(data);
27
27
  }
28
28
  else if (ctx.files) {
29
- return ctx.files[0];
29
+ if (Array.isArray(ctx.files)) {
30
+ return ctx.files[0];
31
+ }
32
+ else {
33
+ return ctx.files;
34
+ }
30
35
  }
31
36
  else {
32
37
  return undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/core",
3
- "version": "3.17.0",
3
+ "version": "3.18.0",
4
4
  "description": "midway core",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -43,5 +43,5 @@
43
43
  "engines": {
44
44
  "node": ">=12"
45
45
  },
46
- "gitHead": "21faf25ae7277f28d61f8416d1acd5a786a2c353"
46
+ "gitHead": "5e6180ba0fabe1acc6df112c68855129a534e941"
47
47
  }