From d15f254a56affd5c6e36ba9d81c29b09f83afed8 Mon Sep 17 00:00:00 2001 From: Ryan Cooney Date: Tue, 21 Jun 2022 15:54:37 -0700 Subject: [PATCH] feat: support multipart/form-data requests in Use FME Service activity (#30) --- src/activities/UseFmeService.ts | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/activities/UseFmeService.ts b/src/activities/UseFmeService.ts index 4b6206a..973b354 100644 --- a/src/activities/UseFmeService.ts +++ b/src/activities/UseFmeService.ts @@ -25,6 +25,7 @@ export interface UseFmeServiceInputs { | "notifications/topics" | "repositories" | "resources/connections" + | "resources/types" | "schedules" | "schedules/categories" | string; @@ -35,7 +36,10 @@ export interface UseFmeServiceInputs { /** * @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"; + contentType?: + | "application/x-www-form-urlencoded" + | "application/json" + | "multipart/form-data"; /** * @description The body parameters to pass to the service operation when the method is POST or PUT. */ @@ -68,15 +72,21 @@ export class UseFmeService implements IActivityHandler { throw new Error("path is required"); } - let body: string | undefined; + let params: string | FormData | undefined; if (typeof parameters === "object") { if (contentType === "application/json") { - body = JSON.stringify(parameters); + params = JSON.stringify(parameters); } else if (contentType === "application/x-www-form-urlencoded") { - body = objectToQueryString(parameters); + params = objectToQueryString(parameters); + } else if (contentType === "multipart/form-data") { + const formData = new FormData(); + for (const name in parameters) { + formData.append(name, parameters[name]); + } + params = formData; } } else { - body = parameters; + params = parameters; } /* eslint-disable @typescript-eslint/no-non-null-assertion */ @@ -89,7 +99,7 @@ export class UseFmeService implements IActivityHandler { result, }); }, - body!, + params as any, // FormData is accepted and used internally by the API. contentType! ); });