Skip to main content

LoopBack 4 November 2020 Milestone Update

· 3 min read
Janny Hou

Originally published on strongloop.com

Hope you had a wonderful Thanksgiving for those who celebrate it! In November, LoopBack team focused on improving the context module and documentation, as well as bug fixes. The Toronto squad participated in the CASCONxEVOKE conference. Instead of a physical booth, we held a virtual one online. We welcomed @nflaig as the new maintainer of loopback-next.

Read more to know about the highlighted improvements:

Context

  • A new phase init() was added to the application life cycle events. It is used when a component need to contribute bindings asynchronously. For example:

    export class MyComponent implements Component, LifeCycleObserver {
    // ...
    async init() {
    // Contribute bindings via `init`
    // This cannot be done via constructor since it's synchronous.
    const val = await readFromConfig();
    this.app.bind('abc').to(val);
    this.status = 'initialized';
    this.initialized = true;
    }
    }

    You can check the Component page to learn about its usage.

  • toInjectable() was introduced as a shortcut to decorate a common/provider/dynamic-value-factory class and automatically creating binding for them. For example:

    @injectable({scope: BindingScope.SINGLETON})
    class MyController {
    constructor(@inject('my-options') private options: MyOptions) {
    // ...
    }
    }

    binding.toInjectable(MyController);

    The decorator's usage is well documented on page Binding.

  • PR #6701 updated test cases to reflect how the design types of array/undefined/complex properties are retrieved.

  • Method injection is allowed for the lifecycle methods in PR #6740. For example:

    class MyObserverWithMethodInjection implements LifeCycleObserver {
    status = 'not-initialized';
    init(@inject('prefix') prefix: string) {
    this.status = `${prefix}:initialized`;
    }
    start(@inject('prefix') prefix: string) {
    this.status = `${prefix}:started`;
    }
    stop(@inject('prefix') prefix: string) {
    this.status = `${prefix}:stopped`;
    }
    }
  • In some cases, your Express middleware wants to access LoopBack's RequestContext to resolve certain bindings. This can be done via MIDDLEWARE_CONTEXT property of the Express request object, which is set up by LoopBack when the RequestContext is instantiated. For example:

    import {MIDDLEWARE_CONTEXT, RequestContext} from '@loopback/rest';
    function expressHandler(req, res, next) {
    const reqCtx = (req as any)[MIDDLEWARE_CONTEXT];
    // Now you have access to the LoopBack RequestContext
    }

Documentation

REST

  • Matching a URL or hostname against a regular expression that contains an unescaped dot as part of the hostname might match more hostnames than expected. PR #6813 updated the regular expression of hostnames accordingly.

Repository

  • #6755 threw 400 BadRequestError for invalid inclusion relation name by rejecting the request with statusCode as 400.

Examples

  • PR #6598 updated artifacts of the todo-list example to the latest style generated by the LoopBack CLI.

Enriching LoopBack and its Community - You are Invited!

As mentioned in our recent blog post, your contribution is important to make LoopBack a sustainable open source project.

Here is what you can do:

Let's make LoopBack a better framework together!