Create virtual adapter (interface)

The point of a virtual adapter is to run one dapplet on many adapters. In this exercise you should implement a virtual adapter for two search adapters.

Here is the initial code for this example: ex10-new-virtual-adapter-exercise.

The starting point of the exercise is the result of the Create site specific adapter. There is an adapter for Google Search and a dapplet for it.

  1. Add a search adapter for Yahoo. See the code on GitHub: example-yahoo-adapter.

  2. Add /example-virtual-adapter/ folder that should have a structure similar to the Google Adapter Example.

1example-virtual-adapter
2├── .gitignore
3├── dapplet.json
4├── package-lock.json
5├── package.json
6└── tsconfig.json
  1. In the /example-virtual-adapter/dapplet.json set "type": "INTERFACE" and remove "contextIds" and "dependencies".
1{
2 "name": {
3 "$ref": "package.json#/name"
4 },
5 "branch": "default",
6 "version": {
7 "$ref": "package.json#/version"
8 },
9 "type": "INTERFACE",
10 "title": "Search Virtual Adapter",
11 "description": {
12 "$ref": "package.json#/description"
13 }
14}
  1. Add virtual adapter to "interfaces: []" in dapplet.json of Google and Yahoo adapters and to "contextIds" and "dependencies" of the dapplet-feature.
1// example-google-adapter/dapplet.json example-yahoo-adapter/dapplet.json
2{
3 "interfaces": {
4 "example-virtual-adapter.dapplet-base.eth": "0.5.0"
5 }
6}
1// dapplet-feature/dapplet.json
2{
3 "contextIds": ["example-virtual-adapter.dapplet-base.eth"],
4 "dependencies": {
5 "example-virtual-adapter.dapplet-base.eth": "0.5.0"
6 }
7}
  1. Change injecting of Google adapter to Virtual adapter in /dapplet-feature/src/index.ts.
@Inject('example-virtual-adapter.dapplet-base.eth')
public adapter: any;
  1. Add the scripts to /package.json to install dependencies and run Google, Yahoo and virtual adapters, and the dapplet using concurrently.
"postinstall": "concurrently -c \"yellow,magenta,green,blue\" -n \"example-google-adapter,example-yahoo-adapter,dapplet,example-virtual-adapter\" \"cd example-google-adapter && npm i\" \"cd example-yahoo-adapter && npm i\" \"cd dapplet-feature && npm i\" \"cd example-virtual-adapter && npm i\"",
"start": "concurrently -c \"yellow,magenta,green,blue\" -n \"example-google-adapter,example-yahoo-adapter,dapplet,example-virtual-adapter\" \"cd example-google-adapter && npm start\" \"cd example-yahoo-adapter && npm start\" \"cd dapplet-feature && npm start\" \"cd example-virtual-adapter && npm start\"",

Here is the result: ex10-new-virtual-adapter-solution.

Run the dapplet:

npm i
npm start

In this example we run four servers concurrently. This means you have to add four registry addresses to the Dapplet extension in the Development tab. Click here for instructions.

video