Skip to content

Fix and document MixinBuilder #673

@jannyHou

Description

@jannyHou

Problem:

The problem of MixinBuilder from my test is: function with() doesn't set its returned type as a class extends the BaseClass:
click to see test example

let AppWithRepoMixin = class extends MixinBuilder.mix(Application).with(RepositoryMixin){};

const myApp = new AppWithRepoMixin();
const boundComponentsBefore = myApp.find('components.*').map(b => b.key);

fails when compiling, because compiler doesn't know myApp is an instance of Application, therefore doesn't know it has a prototype function find() returning an Array. The code calls map with any instead of Array and fails.

This looks like a stop for people wanting to use it...

I am going to defer writing doc for MixinBuilder and create an issue for it. I still love the fluent api and would like to dig more ⭐

Document cut from PR loopbackio/loopback.io#491

Mixin Builder

LoopBack provides a mixin builder function MixinBuilder to easily extend a class
with one or multiple mixins,
you can call it to define the new extended class with fluent syntaxed API like:

import {MixiBuilder} from '@loopback/repository';
import {BaseClass} from '<path_exports_BaseClass>';
import {FooMixin, BarMixin} from '<path_exports_Mixins>';

let newClass = MixinBuilder.mix(baseClass).with(FooMixin, BarMixin);

Acceptance Criteria

  • Move MixinBuilder class from @loopback/repository to @loopback/core

  • Fix MixinBuilder to return the correct type for class as it currently fails to do so

  • Update TSDocs for this Class

  • Tests

  • Bonus: See if this can be just a static method on Application instead of a new Class as proposed here: Fix and document MixinBuilder #673 (comment) DO NOT SPEND MORE THAN 1 HOUR on this. Fixing the class is good enough for this task.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions