refactor: provide common functions and abstraction

This commit is contained in:
Michel Jonathan Schmitz
2019-05-16 08:22:43 +02:00
parent 8ec4401d21
commit 7dfbb9a650
15 changed files with 2379 additions and 1759 deletions

View File

@@ -2,9 +2,9 @@
## Prerequisites:
* `node` (version 10+) and `npm` installed
* `node` (version 10 LTS) and `npm` installed
* a backend, which is running locally (on http://localhost:3000) --- for testing purposes, it is advisable to use `minimal-deployment` project
* a backend, which is running locally (on http://localhost:3000) for testing purposes, it is advisable to use `minimal-deployment` project
## How to get started
@@ -20,7 +20,9 @@ npm run build
To execute the code in "CLI" script (basically to execute the connector):
```sh
node lib/cli.js
node lib/cli.js run <backendURL> <origin> <licensePlate>
e.g.:
node lib/cli.js run http://localhost:3000 minimal-connector f-u
```
To run some sample tests, use:
@@ -30,14 +32,38 @@ npm test
The `npm` scripts are defined in `package.json` file.
## Creating your own connector
1. Update the `executeConnector`-function in `cli.ts` according to the comments and needs of your connector
2. Implement the `fetchItems()` function in `MinimalConnector.ts`
3. Test your connector in your test environment by passing the according arguments to the CLI (See execution-example above)
## Next steps
You may want to:
* modify the convenience methods in the `minimal-connector/api` to your needs.
* add additional options to your cli.
Explore open source connectors from other schools to get some ideas.
## Go into production
1. Deploy your connector.
2. Adjust the CLI-args to fit your production mode
## Code structure
File [src/main.ts](src/index.ts) contains:
* `MinimalConnector` as class with sample `getItems()` method, which simulates fetching of items from a resource and validator property which is useful for validating items against **StAppsCore**
* sample indexing methods: `parse`, `bulk`, `index` and `finish`
File [src/cli.ts](src/cli.ts) contains:
* instantiation of a client
* calls to asynchronous indexing methods using `auto` method of `async` library
Folder [src](src/) contains:
* Reference implementations for CLI and a connector, using the api-classes.
* [/cli.ts](src/cli.ts)
* minimal CLI to start your connector, that uses CLI-args, so that there are no hard coded values for configuration.
* will execute the specified connectors and push the data to the backend
* [/common.ts](src/api/Connector.ts)
* `createUUID`, that will generate a unique id for a given identifying object
* `executeConnector`, that will execute the connector, which will fetch the items and push them to the backend
* [/Connector.ts](src/api/Connector.ts) abstracts the process of executing your specific connector and creating unique ids for the imported items
* [/MinimalConnector.ts](src/MinimalConnector.ts) example connector with mock-up data
* shows how to instantiate things
* shows how to use the convenience functions
File [test/MinimalConnector.spec.ts](test/MinimalConnector.spec.ts) contains sample test suite using `mocha` and `chai`.