refactor: split api into api, api-cli & api-plugin

This commit is contained in:
2023-06-02 16:41:25 +02:00
parent 495a63977c
commit b21833de40
205 changed files with 1981 additions and 1492 deletions

View File

@@ -12,7 +12,6 @@
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {asyncPool} from '@krlwlfrt/async-pool/lib/async-pool.js';
import {
isThing,
SCAssociatedThingWithoutReferences,
@@ -25,8 +24,6 @@ import {
SCThingUpdateResponse,
SCThingUpdateRoute,
} from '@openstapps/core';
import moment from 'moment';
import clone from 'rfdc';
import {v5} from 'uuid';
import {Bulk} from './bulk.js';
import {Client} from './client.js';
@@ -61,12 +58,11 @@ export class ConnectorClient extends Client {
*
* Note: valid namespace IDs are license plates of StApps universities.
* See documentation of `NAMESPACES` for valid namespace IDs.*
*
* @param uid UID to make UUID from
* @param namespaceId Namespace ID to use to make UUID
*/
static makeUUID(uid: string, namespaceId: SCLicensePlate): string {
if (typeof SCNamespaces[namespaceId] === 'undefined') {
if (SCNamespaces[namespaceId] === undefined) {
throw new NamespaceNotDefinedError(namespaceId);
}
@@ -77,12 +73,10 @@ export class ConnectorClient extends Client {
* Remove fields from a thing that are references
*
* This effectively turns a thing into a thing without references, e.g. SCDish into SCDishWithoutReferences.
*
* @param thing Thing to remove references from
*/
static removeReferences<THING extends SCThings>(thing: THING): SCAssociatedThingWithoutReferences<THING> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const thingWithoutReferences = clone()<any>(thing);
const thingWithoutReferences = JSON.parse(JSON.stringify(thing));
delete thingWithoutReferences.origin;
@@ -145,7 +139,6 @@ export class ConnectorClient extends Client {
/**
* Recursively deletes all undefined properties from an object instance
*
* @param object Object to delete undefined properties from
*/
static removeUndefinedProperties(object: object): void {
@@ -163,7 +156,7 @@ export class ConnectorClient extends Client {
const indexedObject = object as {[k: string]: unknown};
if (typeof indexedObject[key] === 'undefined') {
if (indexedObject[key] === undefined) {
// delete undefined keyss
delete indexedObject[key];
} else {
@@ -180,17 +173,17 @@ export class ConnectorClient extends Client {
*
* This uses the Bulk API supplied by the backend and returns an object that can be used
* just like the client itself, while handling the information necessary in bulk transfers.
*
* @param type StAppsCore thing type
* @param source Source identifier (should be unique per actual data source)
* @param timeout Timeout in seconds when the bulk should expire
*/
async bulk<T extends SCThings>(type: SCThingType, source: string, timeout?: number): Promise<Bulk<T>> {
// set default value for timeout to one hour
const bulkTimeout = typeof timeout === 'number' ? timeout : ConnectorClient.BULK_TIMEOUT;
// set the default value for timeout to one hour
const expiration = new Date();
expiration.setSeconds(expiration.getSeconds() + (timeout ?? ConnectorClient.BULK_TIMEOUT));
const bulkData = await this.invokeRoute<SCBulkResponse>(this.bulkRoute, undefined, {
expiration: moment().add(bulkTimeout, 'seconds').format(),
expiration: expiration.toISOString(),
source: source,
type: type,
});
@@ -203,7 +196,6 @@ export class ConnectorClient extends Client {
*
* Note that source is optional but is set to `'stapps-api'` in that case.
* This will override any previous bulk that you indexed with that source.
*
* @param things List of things to index
* @param source Source of the things
* @param timeout Timeout of the bulk in seconds
@@ -216,13 +208,19 @@ export class ConnectorClient extends Client {
}
// set the default source if none is given
const thingSource = typeof source === 'undefined' ? 'stapps-api' : source;
const thingSource = source === undefined ? 'stapps-api' : source;
// request a new bulk
const bulk = await this.bulk(things[0].type, thingSource, timeout);
// add items to the bulk - 5 concurrently
await asyncPool(ConnectorClient.ITEM_CONCURRENT_LIMIT, things, thing => bulk.add(thing));
await Promise.all(
Array.from({length: ConnectorClient.ITEM_CONCURRENT_LIMIT}).map(async () => {
for (let thing = things.pop(); thing !== undefined; thing = things.pop()) {
await bulk.add(thing);
}
}),
);
// close bulk
await bulk.done();
@@ -230,7 +228,6 @@ export class ConnectorClient extends Client {
/**
* Update an existing StAppsCore thing
*
* @param thing StAppsCore thing to update
*/
async update(thing: SCThings): Promise<SCThingUpdateResponse> {