-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core-api): plugin import types: LOCAL & REMOTE
List of files with the interesting changes ================================= The rest of the diff is mostly just compiler worship. ./packages/cactus-core-api/src/main/typescript/openapi-spec.ts ./packages/cactus-core-api/src/main/typescript/plugin-factory-factory.ts ./packages/cactus-core-api/src/main/typescript/i-plugin-factory-options.ts ./packages/cactus-cmd-api-server/src/main/typescript/api-server.ts Primary change ============= The original plugin import type that the API server received via configuration (via ENV, CLI or FILE). The `type` property of the plugin import can be used by the different `createPluginFactory()` implementations to determine the kind of factory they need to return. For example: 1. For `LOCAL` type of plugin imports, the factory returned will construct the actual plugin implementation class (e.g. directly instantiate it) 2. For `REMOTE` type of plugin imports, the factory returned will create an API client object, configured to point to an arbitrary implementation of the plugin over the network (which is how we enable/unlock the possibility to have language independent plugin implementations since by specifying `REMOTE` when importing a plugin, you can provide a network host where a plugin is deployed that was implemented in your preferred programming langugage rather than Typescript/Javascript for example.) Important note: When specifying `REMOTE` as the plugin import type, you still need to also specify the `packageName` property pointing to an npm package that has the API client class definition and the corresponding factory in it. The local implementation however does not have to be present in that npm package, which is the whole point. To provide a specific example of the above case: Writing a ledger connector plugin in a non-NodeJS language (Rust, Go, Java, etc.) can be done in the following way: 1. Define the API client for your plugin in Typescript (only needed if not already defined for the ledger of your choice) We recommend auto-generating this after having written a platform/language netural OpenAPI spec file first. This API client doesn't provide any actual implementation of your plugin, it just maps method names of your plugin to HTTP requests that your actual plugin implementation (in your language of choice) will have to be able to service. 2. Write the actual code of your plugin in your language of choice. 3. Profit. Also note: If you are re-implementing a connector plugin in a different language that already has an implementation in NodeJS/Typescript, then you can skip the step above that has you publish the npm package with the API client class and really only have to work in your language of choice. Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
- Loading branch information
Showing
22 changed files
with
226 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
packages/cactus-core-api/src/main/typescript/i-plugin-factory-options.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { PluginImportType } from "./generated/openapi/typescript-axios/index"; | ||
|
||
/** | ||
* The base interface for describing the object that is to be passed in to all | ||
* the `createPluginFactory()` functions that all plugin packages must expose. | ||
* | ||
*/ | ||
export interface IPluginFactoryOptions { | ||
/** | ||
* The original plugin import type that the API server received via configuration | ||
* (via ENV, CLI or FILE). | ||
* The `type` property of the plugin import can be used by the different | ||
* `createPluginFactory()` implementations to determine the kind of factory | ||
* they need to return. For example: | ||
* | ||
* 1. For `LOCAL` type of plugin imports, the factory returned will construct | ||
* the actual plugin implementation class (e.g. directly instantiate it) | ||
* | ||
* 2. For `REMOTE` type of plugin imports, the factory returned will create an | ||
* API client object, configured to point to an arbitrary implementation of | ||
* the plugin over the network (which is how we enable/unlock the possibility | ||
* to have language independent plugin implementations since by specifying | ||
* `REMOTE` when importing a plugin, you can provide a network host where a | ||
* plugin is deployed that was implemented in your preferred programming | ||
* langugage rather than Typescript/Javascript for example.) | ||
* | ||
* Important note: | ||
* When specifying `REMOTE` as the plugin import type, you still need to also | ||
* specify the `packageName` property pointing to an npm package that has the | ||
* API client class definition and the corresponding factory in it. The local | ||
* implementation however does not have to be present in that npm package, | ||
* which is the whole point. | ||
* | ||
* To provide a specific example of the above case: Writing a ledger connector | ||
* plugin in a non-NodeJS language (Rust, Go, Java, etc.) can be done in the | ||
* following way: | ||
* 1. Define the API client for your plugin in Typescript (only needed if | ||
* not already defined for the ledger of your choice) | ||
* We recommend auto-generating this after having written a platform/language | ||
* netural OpenAPI spec file first. This API client doesn't provide any | ||
* actual implementation of your plugin, it just maps method names of your | ||
* plugin to HTTP requests that your actual plugin implementation (in your | ||
* language of choice) will have to be able to service. | ||
* 2. Write the actual code of your plugin in your language of choice. | ||
* 3. Profit. | ||
* | ||
* Also note: If you are re-implementing a connector plugin in a different | ||
* language that already has an implementation in NodeJS/Typescript, then | ||
* you can skip the step above that has you publish the npm package with the | ||
* API client class and really only have to work in your language of choice. | ||
*/ | ||
pluginImportType: PluginImportType; | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/cactus-core-api/src/main/typescript/plugin-factory-factory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { IPluginFactoryOptions } from "./i-plugin-factory-options"; | ||
import { ICactusPlugin } from "./plugin/i-cactus-plugin"; | ||
import { PluginFactory } from "./plugin/plugin-factory"; | ||
|
||
/** | ||
* This is the function that each plugin npm package must export under the name | ||
* `"createPluginFactory"` so that the API server can perform the automatic | ||
* wiring of the plugin once a plugin import has been placed in the configuration | ||
* of it (API server) via either ENV, CLI or a config file. | ||
*/ | ||
export type PluginFactoryFactory = ( | ||
pluginFactoryOptions: IPluginFactoryOptions | ||
) => Promise<PluginFactory<ICactusPlugin, any, IPluginFactoryOptions>>; |
7 changes: 5 additions & 2 deletions
7
packages/cactus-core-api/src/main/typescript/plugin/plugin-factory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
export abstract class PluginFactory<T, K> { | ||
abstract async create(options: K): Promise<T>; | ||
import { IPluginFactoryOptions } from "../i-plugin-factory-options"; | ||
export abstract class PluginFactory<T, K, C extends IPluginFactoryOptions> { | ||
constructor(public readonly options: C) {} | ||
|
||
abstract create(options: K): Promise<T>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 8 additions & 4 deletions
12
...s/cactus-plugin-consortium-manual/src/main/typescript/plugin-factory-consortium-manual.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,20 @@ | ||
import { PluginFactory } from "@hyperledger/cactus-core-api"; | ||
import { | ||
IPluginFactoryOptions, | ||
PluginFactory, | ||
} from "@hyperledger/cactus-core-api"; | ||
import { | ||
IPluginConsortiumManualOptions, | ||
PluginConsortiumManual, | ||
} from "./plugin-consortium-manual"; | ||
|
||
export class PluginFactoryWebService extends PluginFactory< | ||
PluginConsortiumManual, | ||
IPluginConsortiumManualOptions | ||
IPluginConsortiumManualOptions, | ||
IPluginFactoryOptions | ||
> { | ||
async create( | ||
options: IPluginConsortiumManualOptions | ||
pluginOptions: IPluginConsortiumManualOptions | ||
): Promise<PluginConsortiumManual> { | ||
return new PluginConsortiumManual(options); | ||
return new PluginConsortiumManual(pluginOptions); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 6 additions & 3 deletions
9
packages/cactus-plugin-keychain-memory/src/main/typescript/plugin-factory-keychain.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,26 @@ | ||
import { v4 as uuidv4 } from "uuid"; | ||
|
||
import { IPluginFactoryOptions } from "@hyperledger/cactus-core-api"; | ||
import { PluginFactory } from "@hyperledger/cactus-core-api"; | ||
|
||
import { | ||
IPluginKeychainMemoryOptions, | ||
PluginKeychainMemory, | ||
} from "./plugin-keychain-memory"; | ||
|
||
export class PluginFactoryKeychain extends PluginFactory< | ||
PluginKeychainMemory, | ||
IPluginKeychainMemoryOptions | ||
IPluginKeychainMemoryOptions, | ||
IPluginFactoryOptions | ||
> { | ||
async create( | ||
options: IPluginKeychainMemoryOptions = { | ||
pluginOptions: IPluginKeychainMemoryOptions = { | ||
backend: new Map(), | ||
instanceId: uuidv4(), | ||
keychainId: uuidv4(), | ||
logLevel: "TRACE", | ||
} | ||
): Promise<PluginKeychainMemory> { | ||
return new PluginKeychainMemory(options); | ||
return new PluginKeychainMemory(pluginOptions); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.