@modern-js/plugin-koa 1.4.5 → 1.4.6

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # @modern-js/plugin-koa
2
2
 
3
+ ## 1.4.6
4
+
5
+ ### Patch Changes
6
+
7
+ - 04ae5262: chore: bump @modern-js/utils to v1.4.1 in dependencies
8
+ - 60f7d8bf: feat: add tests dir to npmignore
9
+ - befd9e5b: fix: compatible with babel-plugin-resolver's handling of relative paths on windows
10
+ - Updated dependencies [b8599d09]
11
+ - Updated dependencies [6cffe99d]
12
+ - Updated dependencies [04ae5262]
13
+ - Updated dependencies [60f7d8bf]
14
+ - Updated dependencies [befd9e5b]
15
+ - Updated dependencies [3bf4f8b0]
16
+ - @modern-js/utils@1.5.0
17
+ - @modern-js/adapter-helpers@1.2.3
18
+ - @modern-js/bff-runtime@1.2.2
19
+ - @modern-js/bff-utils@1.2.3
20
+
3
21
  ## 1.4.5
4
22
 
5
23
  ### Patch Changes
@@ -1,5 +1,6 @@
1
1
  import * as path from 'path';
2
2
  import { createRuntimeExportsUtils } from '@modern-js/utils';
3
+ import { getRelativeRuntimePath } from '@modern-js/adapter-helpers';
3
4
  export default (() => ({
4
5
  name: '@modern-js/plugin-koa',
5
6
  setup: api => {
@@ -16,14 +17,8 @@ export default (() => ({
16
17
  appDirectory
17
18
  } = appContext;
18
19
  bffExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, 'server');
19
- const serverRuntimePath = bffExportsUtils.getPath(); // Look up one level, because the artifacts after build have dist directories
20
-
21
- let relativeRuntimePath = path.join('../', path.relative(appDirectory, serverRuntimePath));
22
-
23
- if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test') {
24
- relativeRuntimePath = `./${path.relative(appDirectory, serverRuntimePath)}`;
25
- }
26
-
20
+ const serverRuntimePath = bffExportsUtils.getPath();
21
+ const relativeRuntimePath = getRelativeRuntimePath(appDirectory, serverRuntimePath);
27
22
  return {
28
23
  source: {
29
24
  alias: {
@@ -9,6 +9,8 @@ var path = _interopRequireWildcard(require("path"));
9
9
 
10
10
  var _utils = require("@modern-js/utils");
11
11
 
12
+ var _adapterHelpers = require("@modern-js/adapter-helpers");
13
+
12
14
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
13
15
 
14
16
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
@@ -29,14 +31,8 @@ var _default = () => ({
29
31
  appDirectory
30
32
  } = appContext;
31
33
  bffExportsUtils = (0, _utils.createRuntimeExportsUtils)(appContext.internalDirectory, 'server');
32
- const serverRuntimePath = bffExportsUtils.getPath(); // Look up one level, because the artifacts after build have dist directories
33
-
34
- let relativeRuntimePath = path.join('../', path.relative(appDirectory, serverRuntimePath));
35
-
36
- if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test') {
37
- relativeRuntimePath = `./${path.relative(appDirectory, serverRuntimePath)}`;
38
- }
39
-
34
+ const serverRuntimePath = bffExportsUtils.getPath();
35
+ const relativeRuntimePath = (0, _adapterHelpers.getRelativeRuntimePath)(appDirectory, serverRuntimePath);
40
36
  return {
41
37
  source: {
42
38
  alias: {
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.4.5",
14
+ "version": "1.4.6",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -34,10 +34,10 @@
34
34
  },
35
35
  "dependencies": {
36
36
  "@babel/runtime": "^7.15.3",
37
- "@modern-js/adapter-helpers": "^1.2.1",
38
- "@modern-js/bff-runtime": "^1.2.1",
39
- "@modern-js/bff-utils": "^1.2.2",
40
- "@modern-js/utils": "^1.3.7",
37
+ "@modern-js/adapter-helpers": "^1.2.3",
38
+ "@modern-js/bff-runtime": "^1.2.2",
39
+ "@modern-js/bff-utils": "^1.2.3",
40
+ "@modern-js/utils": "^1.5.0",
41
41
  "koa-body": "^4.2.0",
42
42
  "koa-router": "^10.0.0",
43
43
  "type-is": "^1.6.18"
@@ -52,8 +52,8 @@
52
52
  "@types/supertest": "^2.0.11",
53
53
  "@types/type-is": "^1.6.3",
54
54
  "jest": "^27",
55
- "@modern-js/server-core": "1.2.5",
56
- "@modern-js/core": "1.6.1",
55
+ "@modern-js/server-core": "1.3.1",
56
+ "@modern-js/core": "1.8.0",
57
57
  "supertest": "^6.1.6",
58
58
  "koa": "^2.13.3",
59
59
  "typescript": "^4"
@@ -1,6 +0,0 @@
1
- module.exports = {
2
- extends: ['@modern-js'],
3
- parserOptions: {
4
- project: require.resolve('./tsconfig.json'),
5
- },
6
- };
package/tests/common.ts DELETED
@@ -1,9 +0,0 @@
1
- // globby needs setImmediate
2
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
3
- // @ts-expect-error
4
- global.setImmediate = setTimeout;
5
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
6
- // @ts-expect-error
7
- global.clearImmediate = clearTimeout;
8
-
9
- export const INTROSPECTION_ROUTE_PATH = '/__introspection__';
@@ -1,12 +0,0 @@
1
- <!DOCTYPE html>
2
- <html lang="en">
3
- <head>
4
- <meta charset="UTF-8">
5
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
6
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
- <title>Document</title>
8
- </head>
9
- <body>
10
- hello koa plugin
11
- </body>
12
- </html>
File without changes
@@ -1 +0,0 @@
1
- export const get = () => ({ message: 'hello koa' });
@@ -1,33 +0,0 @@
1
- import { match } from '@modern-js/bff-runtime';
2
- import { useContext } from '../../../../../src/context';
3
-
4
- export const get = ({ query }: { query: Record<string, unknown> }) => ({
5
- query,
6
- });
7
-
8
- export const post = ({ data }: { data: Record<string, unknown> }) => {
9
- // eslint-disable-next-line react-hooks/rules-of-hooks
10
- const ctx = useContext();
11
- const { cookies } = ctx;
12
- return {
13
- query: ctx.query,
14
- data,
15
- cookies: {
16
- id: cookies.get('id'),
17
- name: cookies.get('name'),
18
- },
19
- };
20
- };
21
-
22
- export const patch = match(
23
- {
24
- request: {
25
- data: {
26
- id: Number,
27
- name: String,
28
- },
29
- },
30
- response: { id: Number },
31
- },
32
- ({ data: { id } }) => ({ id }),
33
- );
@@ -1,4 +0,0 @@
1
- export const post = ({ formData }) => ({
2
- message: 'success',
3
- formData,
4
- });
@@ -1 +0,0 @@
1
- // used by the test, empty file
@@ -1 +0,0 @@
1
- export const get = () => ({ message: 'hello koa' });
@@ -1 +0,0 @@
1
- export const post = (id: string) => ({ id });
@@ -1,33 +0,0 @@
1
- import { match } from '@modern-js/bff-runtime';
2
- import { useContext } from '../../../../../../src/context';
3
-
4
- export const get = ({ query }: { query: Record<string, unknown> }) => ({
5
- query,
6
- });
7
-
8
- export const post = ({ data }: { data: Record<string, unknown> }) => {
9
- // eslint-disable-next-line react-hooks/rules-of-hooks
10
- const ctx = useContext();
11
- const { cookies } = ctx;
12
- return {
13
- query: ctx.query,
14
- data,
15
- cookies: {
16
- id: cookies.get('id'),
17
- name: cookies.get('name'),
18
- },
19
- };
20
- };
21
-
22
- export const patch = match(
23
- {
24
- request: {
25
- data: {
26
- id: Number,
27
- name: String,
28
- },
29
- },
30
- response: { id: Number },
31
- },
32
- ({ data: { id } }) => ({ id }),
33
- );
@@ -1,4 +0,0 @@
1
- export const post = ({ formData }) => ({
2
- message: 'success',
3
- formData,
4
- });
@@ -1,69 +0,0 @@
1
- import * as path from 'path';
2
- import request from 'supertest';
3
- import { serverManager } from '@modern-js/server-core';
4
- import plugin from '../src/plugin';
5
- import { APIPlugin } from './helpers';
6
- import './common';
7
-
8
- const pwd = path.join(__dirname, './fixtures/function-mode');
9
-
10
- describe('function-mode', () => {
11
- const id = '666';
12
- const name = 'foo';
13
- const foo = { id, name };
14
- let apiHandler: any;
15
-
16
- beforeAll(async () => {
17
- const runner = await serverManager
18
- .clone()
19
- .usePlugin(APIPlugin, plugin)
20
- .init();
21
- apiHandler = await runner.prepareApiServer({
22
- pwd,
23
- mode: 'function',
24
- });
25
- });
26
-
27
- test('should works with body', async () => {
28
- const res = await request(apiHandler).post('/nest/user').send(foo);
29
- expect(res.status).toBe(200);
30
- expect(res.body.data).toEqual(foo);
31
- });
32
-
33
- test('should works with schema', async () => {
34
- const res = await request(apiHandler).patch('/nest/user').send({
35
- id: 777,
36
- name: 'xxx',
37
- });
38
- expect(res.status).toBe(200);
39
- expect(res.body.id).toBe(777);
40
-
41
- const res2 = await request(apiHandler).patch('/nest/user').send({
42
- id: 'aaa',
43
- name: 'xxx',
44
- });
45
- expect(res2.status).toBe(400);
46
-
47
- const res3 = await request(apiHandler).patch('/nest/user').send({
48
- id: '777',
49
- name: 'xxx',
50
- });
51
- expect(res3.status).toBe(500);
52
- });
53
-
54
- test('should support upload file', done => {
55
- request(apiHandler)
56
- .post('/upload')
57
- .field('my_field', 'value')
58
- .attach('file', path.join(__dirname, './fixtures/assets/index.html'))
59
- .end(async (err, res) => {
60
- if (err) {
61
- throw err;
62
- }
63
- expect(res.statusCode).toBe(200);
64
- expect(res.body.message).toBe('success');
65
- expect(res.body.formData).not.toBeUndefined();
66
- done();
67
- });
68
- });
69
- });
package/tests/helpers.ts DELETED
@@ -1,14 +0,0 @@
1
- import path from 'path';
2
- import { createPlugin } from '@modern-js/server-core';
3
- import { injectAPIHandlerInfos, API_DIR } from '@modern-js/bff-utils';
4
-
5
- export const APIPlugin = createPlugin(() => ({
6
- prepareApiServer(props, next) {
7
- const { pwd, prefix } = props;
8
- const apiDir = path.resolve(pwd, API_DIR);
9
-
10
- injectAPIHandlerInfos(apiDir, prefix);
11
-
12
- return next(props);
13
- },
14
- }));
@@ -1,318 +0,0 @@
1
- import * as path from 'path';
2
- import request from 'supertest';
3
- import Koa from 'koa';
4
- import { serverManager } from '@modern-js/server-core';
5
- import Router from 'koa-router';
6
- import koaBody from 'koa-body';
7
- import plugin from '../src/plugin';
8
- import { APIPlugin } from './helpers';
9
- import { INTROSPECTION_ROUTE_PATH } from './common';
10
-
11
- const pwd = path.join(__dirname, './fixtures/lambda-mode');
12
- const API_DIR = './api';
13
- const MockKoa = Koa;
14
- const MockRouter = Router;
15
- const MockKoaBody = koaBody;
16
-
17
- describe('lambda-mode', () => {
18
- const id = '666';
19
- const name = 'foo';
20
- const foo = { id, name };
21
- const prefix = '/api';
22
- let apiHandler: any;
23
-
24
- beforeAll(async () => {
25
- const runner = await serverManager
26
- .clone()
27
- .usePlugin(APIPlugin, plugin)
28
- .init();
29
- apiHandler = await runner.prepareApiServer({
30
- pwd,
31
- mode: 'framework',
32
- prefix,
33
- });
34
- });
35
-
36
- test('should works', async () => {
37
- const res = await request(apiHandler).get(`${prefix}/hello`);
38
- expect(res.status).toBe(200);
39
- });
40
-
41
- test('should works with query', async () => {
42
- const res = await request(apiHandler).get(`${prefix}/nest/user?id=${id}`);
43
- expect(res.status).toBe(200);
44
- expect(res.body.query.id).toBe(id);
45
- });
46
-
47
- test('should works with body', async () => {
48
- const res = await request(apiHandler).post(`${prefix}/nest/user`).send(foo);
49
- expect(res.status).toBe(200);
50
- expect(res.body.data).toEqual(foo);
51
- });
52
-
53
- test('should works with dynamic route ', async () => {
54
- const res = await request(apiHandler).post(`${prefix}/nest/${id}`);
55
- expect(res.status).toBe(200);
56
- expect(res.body).toEqual({ id });
57
- });
58
-
59
- test('should works with context', async () => {
60
- const res = await request(apiHandler)
61
- .post(`${prefix}/nest/user?id=${id}`)
62
- .send(foo);
63
- expect(res.status).toBe(200);
64
- expect(res.body.data).toEqual(foo);
65
- expect(res.body.query.id).toBe(id);
66
- });
67
-
68
- test('should support cookies', async () => {
69
- const res = await request(apiHandler)
70
- .post(`${prefix}/nest/user?id=${id}`)
71
- .set('Cookie', [`id=${id};name=${name}`]);
72
- expect(res.status).toBe(200);
73
- expect(res.body.cookies.id).toBe(id);
74
- expect(res.body.cookies.name).toBe(name);
75
- });
76
-
77
- test('should works with schema', async () => {
78
- const res = await request(apiHandler).patch(`${prefix}/nest/user`).send({
79
- id: 777,
80
- name: 'xxx',
81
- });
82
- expect(res.status).toBe(200);
83
-
84
- const res2 = await request(apiHandler).patch(`${prefix}/nest/user`).send({
85
- id: 'aaa',
86
- name: 'xxx',
87
- });
88
- expect(res2.status).toBe(400);
89
-
90
- const res3 = await request(apiHandler).patch(`${prefix}/nest/user`).send({
91
- id: '777',
92
- name: 'xxx',
93
- });
94
- expect(res3.status).toBe(500);
95
- });
96
-
97
- test('introspection', async () => {
98
- const res = await request(apiHandler).get(
99
- `${prefix}${INTROSPECTION_ROUTE_PATH}`,
100
- );
101
- expect(res.status).toBe(200);
102
- expect(res.body.protocol).toBe('Farrow-API');
103
- });
104
-
105
- test('should support upload file', done => {
106
- request(apiHandler)
107
- .post(`${prefix}/upload`)
108
- .field('my_field', 'value')
109
- .attach('file', path.join(__dirname, './fixtures/assets/index.html'))
110
- .end(async (err, res) => {
111
- if (err) {
112
- throw err;
113
- }
114
- expect(res.statusCode).toBe(200);
115
- expect(res.body.message).toBe('success');
116
- expect(res.body.formData).not.toBeUndefined();
117
- done();
118
- });
119
- });
120
- });
121
-
122
- describe('add middlewares', () => {
123
- let runner: any;
124
-
125
- beforeAll(async () => {
126
- runner = await serverManager.clone().usePlugin(APIPlugin, plugin).init();
127
- });
128
-
129
- test('should works', async () => {
130
- const foo = 'foo';
131
- const fakeMiddleware = jest.fn(async (ctx: any, next: any) => {
132
- await next();
133
- ctx.body = foo;
134
- });
135
- const fakeMiddleware2 = jest.fn(async (ctx: any, next: any) => {
136
- await next();
137
- });
138
-
139
- // eslint-disable-next-line @typescript-eslint/naming-convention
140
- const [mock_fakeMiddleware, mock_fakeMiddleware2] = [
141
- fakeMiddleware,
142
- fakeMiddleware2,
143
- ];
144
- const apiHandler = await runner.prepareApiServer({
145
- pwd,
146
- mode: 'framework',
147
- config: {
148
- middleware: [
149
- mock_fakeMiddleware,
150
- mock_fakeMiddleware2,
151
- mock_fakeMiddleware2,
152
- ],
153
- },
154
- });
155
-
156
- const res = await request(apiHandler).get('/user');
157
- expect(fakeMiddleware.mock.calls.length).toBe(1);
158
- expect(fakeMiddleware2.mock.calls.length).toBe(2);
159
- expect(res.status).toBe(200);
160
- expect(res.text).toEqual(foo);
161
- });
162
- });
163
-
164
- describe('support use koaBody in app.ts', () => {
165
- const id = '666';
166
- const name = 'modernjs';
167
- const foo = { id, name };
168
-
169
- let runner: any;
170
-
171
- beforeAll(async () => {
172
- runner = await serverManager.clone().usePlugin(APIPlugin, plugin).init();
173
- });
174
-
175
- test('support use koaBody', async () => {
176
- const mock_foo = foo;
177
- jest.mock(
178
- path.join(pwd, API_DIR, 'app.ts'),
179
- () => {
180
- const app = new MockKoa();
181
- // test app takes effect
182
- app.use(async (ctx, next) => {
183
- ctx.request.query = mock_foo;
184
- await next();
185
- });
186
-
187
- app.use(MockKoaBody());
188
-
189
- return {
190
- __esModule: true,
191
- default: app,
192
- };
193
- },
194
- { virtual: true },
195
- );
196
-
197
- const apiHandler = await runner.prepareApiServer({
198
- pwd,
199
- mode: 'framework',
200
- });
201
- const res = await request(apiHandler).post('/nest/user').send(foo);
202
- expect(res.status).toBe(200);
203
- expect(res.body.query).toEqual(foo);
204
- expect(res.body.data).toEqual(foo);
205
- });
206
- });
207
-
208
- describe('support app.ts in lambda mode', () => {
209
- const name = 'modernjs';
210
-
211
- let runner: any;
212
-
213
- beforeAll(async () => {
214
- runner = await serverManager.clone().usePlugin(APIPlugin, plugin).init();
215
- });
216
-
217
- beforeEach(() => {
218
- jest.resetModules();
219
- });
220
-
221
- test('support es module', async () => {
222
- jest.mock(
223
- path.join(pwd, API_DIR, 'app.ts'),
224
- () => {
225
- const app = new MockKoa();
226
- return {
227
- __esModule: true,
228
- default: app,
229
- };
230
- },
231
- { virtual: true },
232
- );
233
-
234
- const apiHandler = await runner.prepareApiServer({
235
- pwd,
236
- mode: 'framework',
237
- });
238
-
239
- const res = await request(apiHandler).get(`/nest/user?name=${name}`);
240
- expect(res.status).toBe(200);
241
- expect(res.body.query.name).toBe(name);
242
- });
243
-
244
- test('support commonjs module', async () => {
245
- jest.mock(
246
- path.join(pwd, API_DIR, 'app.ts'),
247
- () => {
248
- const app = new MockKoa();
249
- return app;
250
- },
251
- { virtual: true },
252
- );
253
-
254
- const apiHandler = await runner.prepareApiServer({
255
- pwd,
256
- mode: 'framework',
257
- });
258
-
259
- const res = await request(apiHandler).get(`/nest/user?name=${name}`);
260
- expect(res.status).toBe(200);
261
- expect(res.body.query.name).toBe(name);
262
- });
263
-
264
- test('support use middleware', async () => {
265
- const mock_name = name;
266
- jest.mock(
267
- path.join(pwd, API_DIR, 'app.ts'),
268
- () => {
269
- const app = new MockKoa();
270
- app.use(async (ctx, next) => {
271
- ctx.query.name = mock_name;
272
- await next();
273
- });
274
- return app;
275
- },
276
- { virtual: true },
277
- );
278
-
279
- const apiHandler = await runner.prepareApiServer({
280
- pwd,
281
- mode: 'framework',
282
- });
283
-
284
- const res = await request(apiHandler).get(`/nest/user`);
285
- expect(res.status).toBe(200);
286
- expect(res.body.query.name).toBe(name);
287
- });
288
-
289
- test('support use router', async () => {
290
- jest.mock(
291
- path.join(pwd, API_DIR, 'app.ts'),
292
- () => {
293
- const app = new MockKoa();
294
- const router = new MockRouter();
295
- router.get('/hello', ctx => {
296
- ctx.body = `foo`;
297
- });
298
-
299
- app.use(router.routes());
300
- return app;
301
- },
302
- { virtual: true },
303
- );
304
-
305
- const apiHandler = await runner.prepareApiServer({
306
- pwd,
307
- mode: 'framework',
308
- });
309
-
310
- const res1 = await request(apiHandler).get(`/hello`);
311
- expect(res1.status).toBe(200);
312
- expect(res1.text).toBe('foo');
313
-
314
- const res2 = await request(apiHandler).get(`/nest/user?name=${name}`);
315
- expect(res2.status).toBe(200);
316
- expect(res2.body.query.name).toBe(name);
317
- });
318
- });
@@ -1,12 +0,0 @@
1
- {
2
- "extends": "@modern-js/tsconfig/base",
3
- "compilerOptions": {
4
- "declaration": false,
5
- "jsx": "preserve",
6
- "baseUrl": "./",
7
- "isolatedModules": true,
8
- "sourceMap": true
9
- },
10
- "paths": {},
11
- "exclude": ["src/__test__/fixtures/**"]
12
- }
@@ -1,81 +0,0 @@
1
- import * as path from 'path';
2
- import fs from 'fs';
3
- import request from 'supertest';
4
- import { serverManager } from '@modern-js/server-core';
5
- import plugin from '../src/plugin';
6
- import { APIPlugin } from './helpers';
7
- import './common';
8
-
9
- const pwd = path.join(__dirname, './fixtures/function-mode');
10
-
11
- describe('webServer', () => {
12
- const id = '666';
13
- const name = 'foo';
14
- const foo = { id, name };
15
- let webHandler: any;
16
- let runner: any;
17
-
18
- beforeAll(async () => {
19
- runner = await serverManager.clone().usePlugin(APIPlugin, plugin).init();
20
- });
21
-
22
- test('support json', async () => {
23
- const middleware = (ctx: any) => {
24
- // ctx.req.url = `${ctx.req.url}?id=${id}`;
25
- ctx.body = foo;
26
- };
27
-
28
- webHandler = await runner.prepareWebServer({
29
- pwd,
30
- config: { middleware: [middleware] },
31
- });
32
-
33
- const res = await request(webHandler).get('/');
34
- expect(res.status).toBe(200);
35
- expect(res.get('content-type')).toBe('application/json; charset=utf-8');
36
- expect(res.body).toEqual(foo);
37
- });
38
-
39
- test('support stream', async () => {
40
- const middleware = (ctx: any) => {
41
- ctx.response.set('content-type', 'text/html');
42
- ctx.body = fs.createReadStream(
43
- path.resolve(__dirname, './fixtures/assets/index.html'),
44
- );
45
- };
46
-
47
- webHandler = await runner.prepareWebServer({
48
- pwd,
49
- config: { middleware: [middleware] },
50
- });
51
-
52
- const res = await request(webHandler).get('/');
53
- expect(res.status).toBe(200);
54
- expect(res.get('content-type')).toBe('text/html');
55
- expect(res.text).toContain(`hello koa plugin`);
56
- });
57
-
58
- test('should support Multiple middleware', async () => {
59
- const middleware1 = async (ctx: any, next: any) => {
60
- await next();
61
- };
62
-
63
- const middleware2 = async (ctx: any, next: any) => {
64
- ctx.response.set('content-type', 'text/html');
65
- ctx.body = fs.createReadStream(
66
- path.resolve(__dirname, './fixtures/assets/index.html'),
67
- );
68
- await next();
69
- };
70
-
71
- webHandler = await runner.prepareWebServer({
72
- pwd,
73
- config: { middleware: [middleware1, middleware2] },
74
- });
75
-
76
- const res = await request(webHandler).get('/');
77
- expect(res.status).toBe(200);
78
- expect(res.get('content-type')).toBe('text/html');
79
- expect(res.text).toContain(`hello koa plugin`);
80
- });
81
- });