Skip to content

Commit

Permalink
fix(rest): refine imports to avoid circular dependencies
Browse files Browse the repository at this point in the history
Circular dependencies cause undefined exported classes
  • Loading branch information
raymondfeng committed May 31, 2019
1 parent 7e9bd28 commit 787c7d5
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 29 deletions.
28 changes: 10 additions & 18 deletions packages/rest/src/__tests__/integration/http-handler.integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@ import {
BodyParser,
createBodyParserBinding,
DefaultSequence,
FindRouteAction,
HttpHandler,
InvokeMethodAction,
JsonBodyParser,
ParseParamsAction,
RawBodyParser,
RejectAction,
Request,
RequestBodyParser,
RestBindings,
SendAction,
StreamBodyParser,
TextBodyParser,
UrlEncodedBodyParser,
Expand Down Expand Up @@ -630,32 +635,19 @@ describe('HttpHandler', () => {
.bind(SequenceActions.LOG_ERROR)
.to(createUnexpectedHttpErrorLogger());

/**
* Use dynamic import to avoid circular Node.js require
*/
const actions = await import('../../actions');

bindAction(
rootContext,
SequenceActions.FIND_ROUTE_ACTION,
actions.FindRouteAction,
);
bindAction(rootContext, SequenceActions.FIND_ROUTE_ACTION, FindRouteAction);
bindAction(
rootContext,
SequenceActions.PARSE_PARAMS_ACTION,
actions.ParseParamsAction,
ParseParamsAction,
);
bindAction(
rootContext,
SequenceActions.INVOKE_METHOD_ACTION,
actions.InvokeMethodAction,
);
bindAction(rootContext, SequenceActions.SEND_ACTION, actions.SendAction);
bindAction(
rootContext,
SequenceActions.REJECT_ACTION,
actions.RejectAction,
InvokeMethodAction,
);
bindAction(rootContext, SequenceActions.SEND_ACTION, SendAction);
bindAction(rootContext, SequenceActions.REJECT_ACTION, RejectAction);

rootContext.bind(RestBindings.SEQUENCE).toClass(DefaultSequence);

Expand Down
2 changes: 1 addition & 1 deletion packages/rest/src/coercion/coerce-parameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

import {isReferenceObject, ParameterObject} from '@loopback/openapi-v3-types';
import * as debugModule from 'debug';
import {RestHttpErrors} from '../';
import {parseJson} from '../parse-json';
import {RestHttpErrors} from '../rest-http-error';
import {
DateCoercionOptions,
getOAIPrimitiveType,
Expand Down
2 changes: 1 addition & 1 deletion packages/rest/src/coercion/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// License text available at https://opensource.org/licenses/MIT

import {ParameterObject, SchemaObject} from '@loopback/openapi-v3-types';
import {RestHttpErrors} from '../';
import {RestHttpErrors} from '../rest-http-error';

/**
* A set of options to pass into the validator functions
Expand Down
4 changes: 0 additions & 4 deletions packages/rest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,3 @@ export * from './router';
export * from './sequence';
export * from './types';
export * from './writer';
export {HttpErrors};

// export all errors from external http-errors package
import * as HttpErrors from 'http-errors';
7 changes: 5 additions & 2 deletions packages/rest/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ import {
OptionsUrlencoded,
} from 'body-parser';
import {Request, Response} from 'express';
// export all errors from external http-errors package
import * as HttpErrors from 'http-errors';
import {RestTags} from './keys';
import {ResolvedRoute, RouteEntry} from './router';

export {Request, Response};
export {HttpErrors};

/**
* An object holding HTTP request, response and other data
Expand Down Expand Up @@ -140,8 +143,8 @@ export function asRestAction(phase?: string) {

/**
* `@restAction` decorator to mark a provider class as RestAction
* @param phase Phase
* @param specs
* @param phase - Phase name
* @param specs - Binding specs
*/
export function restAction(phase?: string, ...specs: BindingSpec[]) {
return bind(asRestAction(phase), ...specs);
Expand Down
8 changes: 5 additions & 3 deletions packages/rest/src/validation/request-body.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
// License text available at https://opensource.org/licenses/MIT

import {
ReferenceObject,
RequestBodyObject,
SchemaObject,
ReferenceObject,
SchemasObject,
} from '@loopback/openapi-v3-types';
import * as AJV from 'ajv';
import * as debugModule from 'debug';
import * as util from 'util';
import {HttpErrors, RestHttpErrors, RequestBody} from '..';
import * as _ from 'lodash';
import * as util from 'util';
import {RequestBody} from '../body-parsers';
import {RestHttpErrors} from '../rest-http-error';
import {HttpErrors} from '../types';

const toJsonSchema = require('openapi-schema-to-json-schema');
const debug = debugModule('loopback:rest:validation');
Expand Down

0 comments on commit 787c7d5

Please # to comment.