From 068d708f2de64b4f06722158fcabb61099548e22 Mon Sep 17 00:00:00 2001 From: Omar Rida Date: Wed, 20 Mar 2024 05:54:30 +0300 Subject: [PATCH] refactor openapi notification to match postman implementation --- .../src/GeneratorNotificationService.ts | 23 ++++++++++++++ .../openapi/src/generatorLoggingWrapper.ts | 31 ------------------- generators/openapi/src/writeOpenApi.ts | 4 +-- 3 files changed, 25 insertions(+), 33 deletions(-) create mode 100644 generators/openapi/src/GeneratorNotificationService.ts delete mode 100644 generators/openapi/src/generatorLoggingWrapper.ts diff --git a/generators/openapi/src/GeneratorNotificationService.ts b/generators/openapi/src/GeneratorNotificationService.ts new file mode 100644 index 00000000000..7af13fbe832 --- /dev/null +++ b/generators/openapi/src/GeneratorNotificationService.ts @@ -0,0 +1,23 @@ +import { FernGeneratorExec, FernGeneratorExecClient } from "@fern-fern/generator-exec-sdk"; + +export class GeneratorNotificationService { + // implementation defined in constructor + public sendUpdate: (update: FernGeneratorExec.GeneratorUpdate) => Promise; + + constructor(generatorConfig: FernGeneratorExec.GeneratorConfig) { + // eslint-disable-next-line no-console + console.log(`Generator config environment is ${generatorConfig.environment.type}`); + if (generatorConfig.environment.type === "remote") { + const generatorExecClient = new FernGeneratorExecClient({ + environment: generatorConfig.environment.coordinatorUrlV2 + }); + const taskId = generatorConfig.environment.id; + this.sendUpdate = async (update) => { + await generatorExecClient.logging.sendUpdate(taskId, [update]); + }; + } else { + // no-op + this.sendUpdate = () => Promise.resolve(); + } + } +} diff --git a/generators/openapi/src/generatorLoggingWrapper.ts b/generators/openapi/src/generatorLoggingWrapper.ts deleted file mode 100644 index f9fd70f7bdb..00000000000 --- a/generators/openapi/src/generatorLoggingWrapper.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { FernGeneratorExec, FernGeneratorExecClient } from "@fern-fern/generator-exec-sdk"; - -export class GeneratorLoggingWrapper { - private maybeSendUpdates = async (_updates: FernGeneratorExec.GeneratorUpdate[]) => { - /* default to no-op */ - }; - - constructor(generatorConfig: FernGeneratorExec.GeneratorConfig) { - // eslint-disable-next-line no-console - console.log("generatorConfig", JSON.stringify(generatorConfig)); - if (generatorConfig.environment.type === "remote") { - // eslint-disable-next-line no-console - console.log("generatorConfig.environment.coordinatorUrlV2", generatorConfig.environment.coordinatorUrlV2); - const generatorLoggingClient = new FernGeneratorExecClient({ - environment: generatorConfig.environment.coordinatorUrlV2 - }); - const taskId = generatorConfig.environment.id; - this.maybeSendUpdates = async (updates) => { - await generatorLoggingClient.logging.sendUpdate(taskId, updates); - }; - } - } - - public async sendUpdate(update: FernGeneratorExec.GeneratorUpdate): Promise { - await this.sendUpdates([update]); - } - - public async sendUpdates(updates: FernGeneratorExec.GeneratorUpdate[]): Promise { - await this.maybeSendUpdates(updates); - } -} diff --git a/generators/openapi/src/writeOpenApi.ts b/generators/openapi/src/writeOpenApi.ts index 4d0481bd533..c1ee448d1a8 100644 --- a/generators/openapi/src/writeOpenApi.ts +++ b/generators/openapi/src/writeOpenApi.ts @@ -7,7 +7,7 @@ import merge from "lodash-es/merge"; import path from "path"; import { convertToOpenApi } from "./convertToOpenApi"; import { getCustomConfig } from "./customConfig"; -import { GeneratorLoggingWrapper } from "./generatorLoggingWrapper"; +import { GeneratorNotificationService } from "./GeneratorNotificationService"; const OPENAPI_JSON_FILENAME = "openapi.json"; const OPENAPI_YML_FILENAME = "openapi.yml"; @@ -24,7 +24,7 @@ export async function writeOpenApi(mode: Mode, pathToConfig: string): Promise