Skip to content

Commit 3d2639d

Browse files
authored
Merge pull request #87 from Canner/fix/fix-format-issues
Fix: Fix format error when url is not found
2 parents ce877dd + af0cc72 commit 3d2639d

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

packages/serve/src/lib/middleware/response-format/middleware.ts

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ export class ResponseFormatMiddleware extends BuiltInMiddleware<ResponseFormatOp
5858
public async handle(context: KoaContext, next: Next) {
5959
// return to skip the middleware, if disabled
6060
if (!this.enabled) return next();
61+
// TODO: replace the hardcoded api with configurable prefix
62+
// Only handle the path for Vulcan API
63+
if (!context.request.path.startsWith('/api')) return next();
6164

6265
// get supported and request format to use.
6366
const format = checkUsableFormat({

packages/serve/test/middlewares/built-in-middlewares/response-format/formatResponseMiddleware.spec.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as sinon from 'ts-sinon';
22
import { ResponseFormatMiddleware } from '@vulcan-sql/serve/middleware';
3-
import { BaseResponseFormatter } from '@vulcan-sql/serve';
3+
import { BaseResponseFormatter, KoaContext } from '@vulcan-sql/serve';
44

55
describe('Test format response middleware', () => {
66
afterEach(() => {
@@ -218,4 +218,17 @@ describe('Test format response middleware', () => {
218218
});
219219

220220
// TODO: test handle to get context response
221+
222+
it('Test to do nothing with paths which are not start in /api', async () => {
223+
// Arrange
224+
const middleware = new ResponseFormatMiddleware({}, '', []);
225+
const mockContext = sinon.stubInterface<KoaContext>();
226+
mockContext.request.path = '/favicon.ico';
227+
mockContext.response.body = '123';
228+
// Act
229+
await middleware.handle(mockContext, async () => null);
230+
231+
// Assert
232+
expect(mockContext.response.body).toBe('123');
233+
});
221234
});

0 commit comments

Comments
 (0)