Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions src/adapters/automaton.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import type { RoutingOptions } from '../utils/router';

import { Router } from '../utils';

export type Automata = 'ADD_USER_TO_ALL_GROUPS' | 'REMOVE_USER_FROM_ALL_GROUPS';

export type AutomatonStatus = 'EXECUTING' | 'COMPLETED' | 'FAILED';

export type AutomatonParameterType = 'string' | 'date' | 'long' | 'double';

export interface AutomatonParameter {
objectType: AutomatonParameterType;
value: string | number | null;
}

export interface AutomatonParameters {
[key: string]: AutomatonParameter;
}

export interface AutomatedReadOutView {
jobId: number | null;
error: string | null;
status: AutomatonStatus | null;
}


/**
* Starts an automaton job for a given project.
* Base URL: POST `https://forio.com/api/v3/{ACCOUNT}/{PROJECT}/automaton/{AUTOMATA}`
*
* @example
* import { automatonAdapter } from 'epicenter-libs';
* const job = await automatonAdapter.start('ADD_USER_TO_ALL_GROUPS', {
* userId: { objectType: 'string', value: 'user123' },
* });
*
* @param automata The automaton action to execute
* @param parameters Parameters for the automaton job, keyed by parameter name
* @param [optionals] Optional arguments; pass network call options overrides here. Special arguments specific to this method are listed below if they exist.
* @param [optionals.addNonce] If true, adds a nonce to the request
* @returns promise that resolves to the automaton job status
*/
export async function start(
automata: Automata,
parameters: AutomatonParameters,
optionals: {
addNonce?: boolean;
} & RoutingOptions = {},
): Promise<AutomatedReadOutView> {
const { addNonce, ...routingOptions } = optionals;
return await new Router()
.withSearchParams({ addNonce })
.post(`/automaton/${automata}`, {
body: { parameters },
...routingOptions,
}).then(({ body }) => body);
Comment on lines +51 to +56

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using await with .then() is redundant. An async function will automatically wrap the returned value in a Promise, so you can directly return the promise chain. This will make the code cleaner and slightly more performant.

This comment applies to getStatus in this file, and all similar async functions in the other new adapter files (context.ts, dashboard.ts, etc.).

Suggested change
return await new Router()
.withSearchParams({ addNonce })
.post(`/automaton/${automata}`, {
body: { parameters },
...routingOptions,
}).then(({ body }) => body);
return new Router()
.withSearchParams({ addNonce })
.post(`/automaton/${automata}`, {
body: { parameters },
...routingOptions,
}).then(({ body }) => body);

}


/**
* Gets the status of an automaton job.
* Base URL: GET `https://forio.com/api/v3/{ACCOUNT}/{PROJECT}/automaton/{JOB_ID}`
*
* @example
* import { automatonAdapter } from 'epicenter-libs';
* const status = await automatonAdapter.getStatus(12345);
*
* @param jobId The job ID returned from starting an automaton
* @param [optionals] Optional arguments; pass network call options overrides here.
* @returns promise that resolves to the automaton job status
*/
export async function getStatus(
jobId: number,
optionals: RoutingOptions = {},
): Promise<AutomatedReadOutView> {
return await new Router()
.get(`/automaton/${jobId}`, optionals)
.then(({ body }) => body);
}
117 changes: 117 additions & 0 deletions src/adapters/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import type { RoutingOptions } from '../utils/router';
import type { ModelLanguage, Morphology } from '../utils/constants';

import { Router } from '../utils';

export interface StellaModelTool {
objectType: 'stella';
gameMode?: boolean | null;
}

export interface VensimModelTool {
objectType: 'vensim';
sensitivityMode?: boolean | null;
cinFiles?: unknown[] | null;
}

export type ModelTool = StellaModelTool | VensimModelTool;

export interface V1ExecutionContext {
version: unknown;
presets?: Record<string, object> | null;
mappedFiles?: Record<string, string> | null;
tool?: ModelTool;
}

export interface ModelContext {
[key: string]: unknown;
}


/**
* Gets the model context for a given model file.
* Base URL: GET `https://forio.com/api/v3/{ACCOUNT}/{PROJECT}/context/{MODEL_FILE}`
*
* @example
* import { contextAdapter } from 'epicenter-libs';
* const context = await contextAdapter.get('model.vmf');
*
* @param modelFile The model file name
* @param [optionals] Optional arguments; pass network call options overrides here. Special arguments specific to this method are listed below if they exist.
* @param [optionals.morphology] The morphology type for the model context
* @returns promise that resolves to the model context
*/
export async function get(
modelFile: string,
optionals: {
morphology?: Morphology;
} & RoutingOptions = {},
): Promise<ModelContext> {
const { morphology, ...routingOptions } = optionals;
return await new Router()
.withSearchParams({ morphology })
.get(`/context/${modelFile}`, routingOptions)
.then(({ body }) => body);
}


/**
* Upgrades the model context for a given model file.
* Base URL: POST `https://forio.com/api/v3/{ACCOUNT}/{PROJECT}/context/upgrade/{MODEL_FILE}`
*
* @example
* import { contextAdapter } from 'epicenter-libs';
* await contextAdapter.upgrade('model.vmf', { morphology: 'SINGULAR' });
*
* @param modelFile The model file name
* @param [optionals] Optional arguments; pass network call options overrides here. Special arguments specific to this method are listed below if they exist.
* @param [optionals.morphology] The morphology type for the upgrade
* @returns promise that resolves to void on success
*/
export async function upgrade(
modelFile: string,
optionals: {
morphology?: Morphology;
} & RoutingOptions = {},
): Promise<void> {
const { morphology, ...routingOptions } = optionals;
return await new Router()
.withSearchParams({ morphology })
.post(`/context/upgrade/${modelFile}`, routingOptions)
.then(({ body }) => body);
}


/**
* Verifies a model context configuration.
* Base URL: POST `https://forio.com/api/v3/{ACCOUNT}/{PROJECT}/context/verify`
*
* @example
* import { contextAdapter } from 'epicenter-libs';
* await contextAdapter.verify('model.vmf', {
* modelLanguage: 'VENSIM',
* morphology: 'SINGULAR',
* });
*
* @param modelFile The model file name
* @param [optionals] Optional arguments; pass network call options overrides here. Special arguments specific to this method are listed below if they exist.
* @param [optionals.morphology] The morphology type
* @param [optionals.modelLanguage] The model language
* @param [optionals.executionContext] The execution context configuration
* @returns promise that resolves to void on success
*/
export async function verify(
modelFile: string,
optionals: {
morphology?: Morphology;
modelLanguage?: ModelLanguage;
executionContext?: V1ExecutionContext;
} & RoutingOptions = {},
): Promise<void> {
const { morphology, modelLanguage, executionContext, ...routingOptions } = optionals;
return await new Router()
.post('/context/verify', {
body: { modelFile, morphology, modelLanguage, executionContext },
...routingOptions,
}).then(({ body }) => body);
}
Loading
Loading