Skip to content

Commit

Permalink
chore: apply feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Rifa Achrinza <[email protected]>
  • Loading branch information
achrinza committed Aug 29, 2020
1 parent a21deb4 commit 124ca6d
Show file tree
Hide file tree
Showing 16 changed files with 41 additions and 147 deletions.
6 changes: 3 additions & 3 deletions bodyparsers/msgpack/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bodyparsers/msgpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"@types/msgpack-lite": "0.1.7",
"@types/node": "^10.17.28",
"@types/type-is": "^1.6.3",
"typescript": "~3.9.7"
"typescript": "~4.0.2"
},
"copyright.owner": "IBM Corp.",
"publishConfig": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ import {
stubExpressContext,
} from '@loopback/testlab';
import {encode} from 'msgpack-lite';
import {
DefaultMsgPackParserLibrary,
MsgPackBodyParser,
MsgPackBodyParserBindings,
} from '../..';
import {MsgPackBodyParser} from '../..';

describe('MessagePack body parser', () => {
let requestBodyParser: RequestBodyParser;
Expand Down Expand Up @@ -131,9 +127,6 @@ describe('MessagePack body parser', () => {
const options = {};
const parsers = [new MsgPackBodyParser(options)];
const context = new Context();
context
.bind(MsgPackBodyParserBindings.PARSER_LIBRARY)
.toClass(DefaultMsgPackParserLibrary);
requestBodyParser = new RequestBodyParser(parsers, context);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,30 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {RestApplication} from '@loopback/rest';
import {RestApplication, RestBindings} from '@loopback/rest';
import {expect} from '@loopback/testlab';
import {StubParserLibrary} from '../';
import {
DefaultMsgPackParserLibrary,
MsgPackBodyParser,
MsgPackBodyParserBindings,
MsgPackBodyParserComponent,
} from '../..';

describe('MessagePack body parser component', () => {
let restApplication: RestApplication;
beforeEach(givenRestAppWithComponent);
it('binds MessagePack body parser', async () => {
const restApplication = new RestApplication();
restApplication.component(MsgPackBodyParserComponent);

it('binds default parser library', async () => {
expect(
await restApplication.get(MsgPackBodyParserBindings.PARSER_LIBRARY),
).to.be.instanceOf(DefaultMsgPackParserLibrary);
await restApplication.get(MsgPackBodyParserBindings.BODY_PARSER),
).to.be.instanceOf(MsgPackBodyParser);
});

it('uses a custom parser library', async () => {
restApplication
.bind(MsgPackBodyParserBindings.PARSER_LIBRARY)
.toClass(StubParserLibrary);
it('throws error without raw body parser', async () => {
const restApplication = new RestApplication();
restApplication.unbind(RestBindings.REQUEST_BODY_PARSER_RAW);

expect(
await restApplication.get(MsgPackBodyParserBindings.PARSER_LIBRARY),
).to.be.instanceOf(StubParserLibrary);
expect(() =>
restApplication.component(MsgPackBodyParserComponent),
).to.throw();
});

function givenRestAppWithComponent() {
restApplication = new RestApplication();
restApplication.component(MsgPackBodyParserComponent);
}
});
6 changes: 0 additions & 6 deletions bodyparsers/msgpack/src/__tests__/fixtures/index.ts

This file was deleted.

This file was deleted.

6 changes: 0 additions & 6 deletions bodyparsers/msgpack/src/__tests__/index.ts

This file was deleted.

2 changes: 2 additions & 0 deletions bodyparsers/msgpack/src/__tests__/unit/bodyparser.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ describe('MessagePack body parser', () => {
'application/x-msgpack',
'application/subtype+msgpack',
];

let bodyParser: MsgPackBodyParser;

beforeEach(givenBodyParser);

for (const contentType of contentTypes) {
Expand Down
35 changes: 0 additions & 35 deletions bodyparsers/msgpack/src/__tests__/unit/parserlibrary.unit.ts

This file was deleted.

16 changes: 2 additions & 14 deletions bodyparsers/msgpack/src/bodyparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,17 @@ import {
RequestBodyParserOptions,
RestBindings,
} from '@loopback/rest';
import msgpack from 'msgpack-lite';
import {is} from 'type-is';
import {
DefaultMsgPackParserLibrary,
MsgPackBodyParserBindings,
MsgPackParserLibrary,
} from '.';

// FIXME(achrinza): Workaround for https://github.com/microsoft/rushstack/pull/1867
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import * as loopbackBodyParserMsgPack from '.';

export class MsgPackBodyParser extends RawBodyParser {
name = Symbol('msgpack');
private readonly _parserLibrary: MsgPackParserLibrary;

constructor(
@inject(RestBindings.REQUEST_BODY_PARSER_OPTIONS, {optional: true})
options: RequestBodyParserOptions = {},
@inject(MsgPackBodyParserBindings.PARSER_LIBRARY)
parserLibrary: MsgPackParserLibrary = new DefaultMsgPackParserLibrary(),
) {
super(options);
this._parserLibrary = parserLibrary;
}

supports(mediaType: string) {
Expand All @@ -47,7 +35,7 @@ export class MsgPackBodyParser extends RawBodyParser {

async parse(request: Request): Promise<RequestBody> {
const result = await super.parse(request);
const body = this._parserLibrary.decode(result.value);
const body = msgpack.decode(result.value);

return {
value: body,
Expand Down
30 changes: 16 additions & 14 deletions bodyparsers/msgpack/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,28 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {Binding, Component} from '@loopback/core';
import {createBodyParserBinding} from '@loopback/rest';
import {Binding, Component, inject} from '@loopback/core';
import {
DefaultMsgPackParserLibrary,
MsgPackBodyParser,
MsgPackBodyParserBindings,
} from '.';

// FIXME(achrinza): Workaround for https://github.com/microsoft/rushstack/pull/1867
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import * as loopbackCore from '@loopback/core';
createBodyParserBinding,
RawBodyParser,
RestBindings,
} from '@loopback/rest';
import {MsgPackBodyParser, MsgPackBodyParserBindings} from '.';

export class MsgPackBodyParserComponent implements Component {
bindings = [
bindings: Binding[] = [
createBodyParserBinding(
MsgPackBodyParser,
MsgPackBodyParserBindings.BODY_PARSER,
),
Binding.bind(MsgPackBodyParserBindings.PARSER_LIBRARY).toClass(
DefaultMsgPackParserLibrary,
),
];

constructor(
@inject(RestBindings.REQUEST_BODY_PARSER_RAW, {optional: true})
rawBodyParser?: RawBodyParser,
) {
if (rawBodyParser == null) {
throw new Error('MessagePack body parser requires raw body parser.');
}
}
}
2 changes: 0 additions & 2 deletions bodyparsers/msgpack/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@
export * from './bodyparser';
export * from './component';
export * from './keys';
export * from './parserlibrary';
export * from './types';
5 changes: 0 additions & 5 deletions bodyparsers/msgpack/src/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@

import {BindingKey} from '@loopback/core';
import {BodyParser, RestBindings} from '@loopback/rest';
import {MsgPackParserLibrary} from '.';

export namespace MsgPackBodyParserBindings {
export const BODY_PARSER = BindingKey.create<BodyParser>(
`${RestBindings.REQUEST_BODY_PARSER}.msgpack`,
);

export const PARSER_LIBRARY = BindingKey.create<MsgPackParserLibrary>(
`${BODY_PARSER}.parserLibrary`,
);
}
14 changes: 0 additions & 14 deletions bodyparsers/msgpack/src/parserlibrary.ts

This file was deleted.

8 changes: 0 additions & 8 deletions bodyparsers/msgpack/src/types.ts

This file was deleted.

3 changes: 3 additions & 0 deletions packages/express/src/middleware-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import {
MiddlewareBindingOptions,
} from './types';

// FIXME(rfeng): Workaround for https://github.com/microsoft/rushstack/pull/1867
/* eslint-disable @typescript-eslint/no-unused-vars */
import * as loopbackContext from '@loopback/core';
/* eslint-enable @typescript-eslint/no-unused-vars */

/**
Expand Down

0 comments on commit 124ca6d

Please sign in to comment.