From f5266813be9ffeda8d4883f86d273bfa4a53dc2b Mon Sep 17 00:00:00 2001 From: gnuxie Date: Tue, 13 May 2025 16:38:35 +0100 Subject: [PATCH] Lemme give you an idea mare. --- .../JSONInterfaceAdaptor.ts | 47 +++++++++++++++++++ .../JSONRendererDescription.ts | 23 +++++++++ .../StandardJSONInterfaceAdaptor.ts | 40 ++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 src/JSONInterfaceAdaptor/JSONInterfaceAdaptor.ts create mode 100644 src/JSONInterfaceAdaptor/JSONRendererDescription.ts create mode 100644 src/JSONInterfaceAdaptor/StandardJSONInterfaceAdaptor.ts diff --git a/src/JSONInterfaceAdaptor/JSONInterfaceAdaptor.ts b/src/JSONInterfaceAdaptor/JSONInterfaceAdaptor.ts new file mode 100644 index 0000000..8d5a4eb --- /dev/null +++ b/src/JSONInterfaceAdaptor/JSONInterfaceAdaptor.ts @@ -0,0 +1,47 @@ +// SPDX-FileCopyrightText: 2025 Gnuxie +// +// SPDX-License-Identifier: Apache-2.0 +// +// SPDX-FileAttributionText: +// This modified file incorporates work from @the-draupnir-project/interface-manager +// https://github.com/the-draupnir-project/interface-manager +// + +import { Result } from "@gnuxie/typescript-result"; +import { CommandDescription, CommandMeta } from "../Command"; +import { DescribeJSONRenderer } from "./JSONRendererDescription"; + +export type JSONErrorResponse< + JSONBody extends Record = Record, +> = { + readonly statusCode: number; + readonly body: JSONBody; +}; + +export type AsyncCommandResultCB = ( + result: Result, JSONErrorResponse> +) => void; + +export interface JSONInterfaceAdaptor { + parseAndInvoke( + jsonBody: Record, + context: AdaptorContext, + invocationInfo: InvocationContext + ): Promise, JSONErrorResponse>>; + asyncParseThenInvoke( + jsonBody: Record, + context: AdaptorContext, + invocationInfo: InvocationContext, + cb: AsyncCommandResultCB + ): Promise>; + describeRenderer( + commandDescription: CommandDescription, + rendererDescription: DescribeJSONRenderer + ): JSONInterfaceAdaptor; + isDescribingRendererForCommand< + TCommandDescription extends CommandDescription, + >( + commandDescription: TCommandDescription + ): boolean; + renderedCommands(): CommandDescription[]; +} diff --git a/src/JSONInterfaceAdaptor/JSONRendererDescription.ts b/src/JSONInterfaceAdaptor/JSONRendererDescription.ts new file mode 100644 index 0000000..318bdc8 --- /dev/null +++ b/src/JSONInterfaceAdaptor/JSONRendererDescription.ts @@ -0,0 +1,23 @@ +// SPDX-FileCopyrightText: 2025 Gnuxie +// +// SPDX-License-Identifier: Apache-2.0 +// +// SPDX-FileAttributionText: +// This modified file incorporates work from @the-draupnir-project/interface-manager +// https://github.com/the-draupnir-project/interface-manager +// + +import { Result } from "@gnuxie/typescript-result"; +import { JSONErrorResponse } from "./JSONInterfaceAdaptor"; + +export type JSONRendererDescription = { + JSONRenderer?( + commandResult: Result + ): Result, JSONErrorResponse>; + confirmationJSONRenderer?( + commandResult: Result + ): Result, JSONErrorResponse>; +}; + +export type DescribeJSONRenderer = + JSONRendererDescription; diff --git a/src/JSONInterfaceAdaptor/StandardJSONInterfaceAdaptor.ts b/src/JSONInterfaceAdaptor/StandardJSONInterfaceAdaptor.ts new file mode 100644 index 0000000..f3f6d8c --- /dev/null +++ b/src/JSONInterfaceAdaptor/StandardJSONInterfaceAdaptor.ts @@ -0,0 +1,40 @@ +// SPDX-FileCopyrightText: 2025 Gnuxie +// +// SPDX-License-Identifier: Apache-2.0 +// +// SPDX-FileAttributionText: +// This modified file incorporates work from @the-draupnir-project/interface-manager +// https://github.com/the-draupnir-project/interface-manager +// + +import { CommandDescription } from "../Command"; +import { + AsyncCommandResultCB, + JSONInterfaceAdaptor, +} from "./JSONInterfaceAdaptor"; +import { JSONRendererDescription } from "./JSONRendererDescription"; + +export class StandardJSONInterfaceAdaptor + implements JSONInterfaceAdaptor +{ + private readonly renderers = new Map< + CommandDescription, + JSONRendererDescription + >(); + + constructor(private readonly defaultRenderer: JSONRendererDescription) { + // nothing to do. + } + + public asyncParseThenInvoke( + jsonBody: Record, + context: AdaptorContext, + invocationInfo: InvocationContext, + cb: AsyncCommandResultCB + ) { + // to do this properly we need to call the parser on the JSON body. + // that should return a presentation argument stream from which we + // can create a partial command. + // From there we just do the ususal stuff. + } +}