JavaScript/TypeScript Support

Facility supports tools and libraries for using JavaScript and/or TypeScript with Facility Service Definitions.

Tools

Generate a JavaScript or TypeScript client or server for your Facility Service Definition by doing one of the following:

  • Use the Facility Editor. Enter an API definition in the left pane, choose the “JavaScript” or “TypeScript” generator, review the generated files, and click Download.
  • Run the fsdgenjs tool on the command line or in a build script.
  • Use the Facility.CodeGen.JavaScript .NET library (NuGet Package) in your own build tool.

fsdgenjs

fsdgenjs is a command-line tool that generates JavaScript or TypeScript for a Facility Service Definition.

Install fsdgenjs as documented from its NuGet package.

fsdgenjs generates files for client, Express server, and types and supports the standard command-line options as well as the following additional command-line options:

  • --module <name>: Sets the name of the generated module.
  • --typescript: Generates TypeScript rather than JavaScript.
  • --express: Generates an Express server.
  • --disable-eslint: Disables ESLint via code comment.

Events

Events in Facility enable streaming responses using server-sent events (SSE). The JavaScript/TypeScript code generator produces methods that return async iterables for events.

For an event defined as:

event streamChat
{
  prompt: string;
}:
{
  textDelta: string;
}

The generated TypeScript client includes:

streamChat(
  request: IStreamChatRequest
): Promise<IServiceResult<AsyncIterable<IServiceResult<IStreamChatResponse>>>>;

Using Events in JavaScript/TypeScript

Clients can consume event streams using for await...of:

const request = { prompt: "Hello" };
const result = await client.streamChat(request);

if (!result.ok) {
  // Handle error
  console.error(`Error: ${result.error.message}`);
  return;
}

for await (const chunkResult of result.value) {
  if (!chunkResult.ok) {
    // Handle error in stream
    console.error(`Stream error: ${chunkResult.error.message}`);
    break;
  }
  
  const chunk = chunkResult.value;
  process.stdout.write(chunk.textDelta);
}

The HTTP client implementation uses the Fetch API to consume server-sent events with all HTTP methods supported, automatically parsing each SSE event into a response object.