diff --git a/src/activities/RunFmeDataDownload.ts b/src/activities/RunFmeDataDownload.ts index 7bb2a6b..b7d4ac4 100644 --- a/src/activities/RunFmeDataDownload.ts +++ b/src/activities/RunFmeDataDownload.ts @@ -1,5 +1,6 @@ import type { IActivityHandler } from "@geocortex/workflow/runtime/IActivityHandler"; import { FmeService } from "../FmeService"; +import { objectToQueryString } from "../utils"; /** An interface that defines the inputs of the activity. */ export interface RunFmeDataDownloadInputs { @@ -71,7 +72,7 @@ export class RunFmeDataDownload implements IActivityHandler { service.server.runDataDownload( repository, workspace, - this.objectToQueryString(parameters), + objectToQueryString(parameters), (result) => { return resolve({ result, @@ -80,20 +81,4 @@ export class RunFmeDataDownload implements IActivityHandler { ); }); } - - objectToQueryString(data?: {}): string { - if (!data) { - return ""; - } - return Object.keys(data) - .map((k) => { - const value = data[k]; - const valueToEncode = - value === undefined || value === null ? "" : value; - return `${encodeURIComponent(k)}=${encodeURIComponent( - valueToEncode - )}`; - }) - .join("&"); - } } diff --git a/src/activities/UseFmeService.ts b/src/activities/UseFmeService.ts new file mode 100644 index 0000000..561a39c --- /dev/null +++ b/src/activities/UseFmeService.ts @@ -0,0 +1,94 @@ +import type { IActivityHandler } from "@geocortex/workflow/runtime/IActivityHandler"; +import { FmeService } from "../FmeService"; +import { objectToQueryString } from "../utils"; + +/** An interface that defines the inputs of the activity. */ +export interface UseFmeServiceInputs { + /** + * @displayName FME Service + * @description The FME service. + * @required + */ + service: FmeService; + + /** + * @description The FME service operation. + * @required + */ + path: + | "healthcheck" + | "info" + | "notifications/publications" + | "notifications/publishers" + | "notifications/subscribers" + | "notifications/subscriptions" + | "notifications/topics" + | "repositories" + | "resources/connections" + | "schedules" + | "schedules/categories" + | string; + /** + * @description The HTTP method to use to make the request. The default is GET. + */ + method?: "GET" | "POST" | "PUSH" | "DELETE"; + /** + * @description The content type of the request. This is required when the method is POST or PUT. + */ + contentType?: "application/x-www-form-urlencoded" | "application/json"; + /** + * @description The body parameters to pass to the service operation when the method is POST or PUT. + */ + parameters?: string | object; +} + +/** An interface that defines the outputs of the activity. */ +export interface UseFmeServiceOutputs { + /** + * @description The result of the service operation. + */ + result: any; +} + +/** + * @displayName Use FME Service + * @category FME + * @description Utility activity to generically access any FME Server REST API operation. + * @helpUrl https://docs.safe.com/fme/html/FME_REST/apidoc/v3/#! + */ +export class UseFmeService implements IActivityHandler { + async execute(inputs: UseFmeServiceInputs): Promise { + const { contentType, method, parameters, path, service } = inputs; + if (!service) { + throw new Error("service is required"); + } + if (!path) { + throw new Error("path is required"); + } + + let body: string | undefined; + if (typeof parameters === "object") { + if (contentType === "application/json") { + body = JSON.stringify(parameters); + } else if (contentType === "application/x-www-form-urlencoded") { + body = objectToQueryString(parameters); + } + } else { + body = parameters; + } + + return new Promise((resolve) => { + service.server.customRequest( + `${service.url}/fmerest/v3/${path}`, + method || "GET", + (result) => { + return resolve({ + result, + }); + }, + body!, + contentType! + ); + }); + } +} diff --git a/src/index.ts b/src/index.ts index c5f16b6..1eec4c6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,3 +5,4 @@ export * from "./activities/GetFmeWorkspaceParameters"; export * from "./activities/RunFmeDataDownload"; export * from "./activities/RunFmeJob"; export * from "./activities/RunFmeJobAsync"; +export * from "./activities/UseFmeService"; diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 0000000..8bb606e --- /dev/null +++ b/src/utils.ts @@ -0,0 +1,15 @@ +export function objectToQueryString(data?: {}): string { + if (!data) { + return ""; + } + return Object.keys(data) + .map((k) => { + const value = data[k]; + const valueToEncode = + value === undefined || value === null ? "" : value; + return `${encodeURIComponent(k)}=${encodeURIComponent( + valueToEncode + )}`; + }) + .join("&"); +}