Add support for import overrides
Closed this issue · 0 comments
The repository service controls all application imports. Currently, it returns the actual module or a remote implementation based on the segmentation. The next step is to utilize the repository for providing alternative implementations.
Let's look at a simple example.
export default async function sayHello(): Promise<string>
{
return 'Hello World';
}
// src/greet.ts
import getMessage from './sayHello';
export default async function greet(): Promise<void>
{
const message = await getMessage();
console.log(message); // Hello World
}
All this module does is importing and calling a function from another module. Now suppose we want to replace the sayHello
module with the sayBye
module, without modifying the greet
module. The sayBye
module has the same exports.
export default async function sayBye(): Promise<string>
{
return 'Bye World';
}
For this, we need some way to tell the repository to provide an alternative module when the sayHello
module gets requested. The proposed solution is to add an additional property to the repository to configure this.
{
"url": "http://localhost:3000",
"repository":
{
"overrides":
{
"./sayHello": "./sayBye"
}
}
}
The overrides
property configures the repository to provide the sayBye
module when the sayHello
module is requested. Thus overriding the actual request. This should work for module imports (like the greet
does), and the RPC API.
GET http://localhost:3000/rpc/sayHello HTTP/1.1
This request should result into the 'Bye World' message.