Skip to content
Draft
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
47 changes: 47 additions & 0 deletions src/JSONInterfaceAdaptor/JSONInterfaceAdaptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-FileCopyrightText: 2025 Gnuxie <[email protected]>
//
// SPDX-License-Identifier: Apache-2.0
//
// SPDX-FileAttributionText: <text>
// This modified file incorporates work from @the-draupnir-project/interface-manager
// https://github.com/the-draupnir-project/interface-manager
// </text>

import { Result } from "@gnuxie/typescript-result";
import { CommandDescription, CommandMeta } from "../Command";
import { DescribeJSONRenderer } from "./JSONRendererDescription";

export type JSONErrorResponse<
JSONBody extends Record<string, unknown> = Record<string, unknown>,
> = {
readonly statusCode: number;
readonly body: JSONBody;
};

export type AsyncCommandResultCB = (
result: Result<Record<string, unknown>, JSONErrorResponse>
) => void;

export interface JSONInterfaceAdaptor<AdaptorContext, InvocationContext> {
parseAndInvoke(
jsonBody: Record<string, unknown>,
context: AdaptorContext,
invocationInfo: InvocationContext
): Promise<Result<Record<string, unknown>, JSONErrorResponse>>;
asyncParseThenInvoke(
jsonBody: Record<string, unknown>,
context: AdaptorContext,
invocationInfo: InvocationContext,
cb: AsyncCommandResultCB
): Promise<Result<void, JSONErrorResponse>>;
describeRenderer<TCommandMeta extends CommandMeta>(
commandDescription: CommandDescription<TCommandMeta>,
rendererDescription: DescribeJSONRenderer<TCommandMeta["CommandResult"]>
): JSONInterfaceAdaptor<AdaptorContext, InvocationContext>;
isDescribingRendererForCommand<
TCommandDescription extends CommandDescription,
>(
commandDescription: TCommandDescription
): boolean;
renderedCommands(): CommandDescription[];
}
23 changes: 23 additions & 0 deletions src/JSONInterfaceAdaptor/JSONRendererDescription.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-FileCopyrightText: 2025 Gnuxie <[email protected]>
//
// SPDX-License-Identifier: Apache-2.0
//
// SPDX-FileAttributionText: <text>
// This modified file incorporates work from @the-draupnir-project/interface-manager
// https://github.com/the-draupnir-project/interface-manager
// </text>

import { Result } from "@gnuxie/typescript-result";
import { JSONErrorResponse } from "./JSONInterfaceAdaptor";

export type JSONRendererDescription<CommandResult = unknown> = {
JSONRenderer?(
commandResult: Result<CommandResult>
): Result<Record<string, unknown>, JSONErrorResponse>;
confirmationJSONRenderer?(
commandResult: Result<CommandResult>
): Result<Record<string, unknown>, JSONErrorResponse>;
};

export type DescribeJSONRenderer<CommandResult = unknown> =
JSONRendererDescription<CommandResult>;
40 changes: 40 additions & 0 deletions src/JSONInterfaceAdaptor/StandardJSONInterfaceAdaptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-FileCopyrightText: 2025 Gnuxie <[email protected]>
//
// SPDX-License-Identifier: Apache-2.0
//
// SPDX-FileAttributionText: <text>
// This modified file incorporates work from @the-draupnir-project/interface-manager
// https://github.com/the-draupnir-project/interface-manager
// </text>

import { CommandDescription } from "../Command";
import {
AsyncCommandResultCB,
JSONInterfaceAdaptor,
} from "./JSONInterfaceAdaptor";
import { JSONRendererDescription } from "./JSONRendererDescription";

export class StandardJSONInterfaceAdaptor<AdaptorContext, InvocationContext>
implements JSONInterfaceAdaptor<AdaptorContext, InvocationContext>
{
private readonly renderers = new Map<

Check failure on line 20 in src/JSONInterfaceAdaptor/StandardJSONInterfaceAdaptor.ts

View workflow job for this annotation

GitHub Actions / Unit Tests

'renderers' is declared but its value is never read.

Check failure on line 20 in src/JSONInterfaceAdaptor/StandardJSONInterfaceAdaptor.ts

View workflow job for this annotation

GitHub Actions / Lint

'renderers' is declared but its value is never read.
CommandDescription,
JSONRendererDescription
>();

constructor(private readonly defaultRenderer: JSONRendererDescription) {

Check failure on line 25 in src/JSONInterfaceAdaptor/StandardJSONInterfaceAdaptor.ts

View workflow job for this annotation

GitHub Actions / Unit Tests

Property 'defaultRenderer' is declared but its value is never read.

Check failure on line 25 in src/JSONInterfaceAdaptor/StandardJSONInterfaceAdaptor.ts

View workflow job for this annotation

GitHub Actions / Lint

Property 'defaultRenderer' is declared but its value is never read.
// nothing to do.
}

public asyncParseThenInvoke(

Check failure on line 29 in src/JSONInterfaceAdaptor/StandardJSONInterfaceAdaptor.ts

View workflow job for this annotation

GitHub Actions / Unit Tests

Property 'asyncParseThenInvoke' in type 'StandardJSONInterfaceAdaptor<AdaptorContext, InvocationContext>' is not assignable to the same property in base type 'JSONInterfaceAdaptor<AdaptorContext, InvocationContext>'.

Check failure on line 29 in src/JSONInterfaceAdaptor/StandardJSONInterfaceAdaptor.ts

View workflow job for this annotation

GitHub Actions / Lint

Property 'asyncParseThenInvoke' in type 'StandardJSONInterfaceAdaptor<AdaptorContext, InvocationContext>' is not assignable to the same property in base type 'JSONInterfaceAdaptor<AdaptorContext, InvocationContext>'.
jsonBody: Record<string, unknown>,
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.
}
}
Loading