Since the Aurelia 2 beta is out, I started to check if I can manage to port my Aurelia 1 applications to the beta. I am struggling to say the least. For now I am already stuck on registering transients for DI.
In Aurelia 1, I registered my clients like this.
const clients = [
Client1,
Client2,
// etc
];
for (const t of clients) {
aurelia.container.registerTransient(t, () => {
const type = new t(
{
accessToken: () => 'accessToken'
// rest of config
},
'client-endpoint'
);
return type;
});
}
For Aurelia 2, I am trying to register it like this:
const container = DI.createContainer();
const clients = [
Client1,
Client2,
// etc
];
for (const t of clients) {
container.register(
Registration.transient(t, () =>
new t(
{
accessToken: () => 'accessToken'
// rest of config
},
'client-endpoint'
);
})
);
}
Which gives me the error:
TS2345: Argument of type '() => Client1 | Client2 | etc.' is not assignable to parameter of type 'Constructable<{}>'.
Type '() => Client1, Client2 | etc. provides no match for the signature 'new (...args: any[]): {}'.
I tried using the Registration.callback
instead, which gives me no compile errors. But when calling an instance from DI doesn’t seem to provide an instance with the provided config in the constructor when registering it.
@inject(Client1)
export class MyApp {
constructor(
private readonly clientApi: Client1 //
) {}
public async attached(): Promise<void> {
const client = await this.clientApi.getAuthenticated();
console.log(client);
}
}
When I try to use Client1
it should have to config configured (which the clientApi.getAuthenticated()
uses) but it gives the error:
Cannot read properties of undefined (reading 'accessToken')
which means the config is not provided when constructing the Client
one.