output.ts configuration


Introduction to the output.ts file

This file exports an object with fields that define the structure of the operation's output i.e. Output Schema.

Defining this will improve the UX when using the connector snake or JSONpaths on the Tray Builder UI.

What this file contains will depend on what you return from the operation in the handler.ts file.

Single HTTP operation

You may choose to retun the response as received by the 3rd party API Surfacing without any transformation.

In this case, you should first test the API through an API client and use the response to prepare the file.

Here is an example:

handler.ts (returning same response upstream)output.ts (returning same response upstream)
Copy
Copied
export const getTopRatedMoviesHandler = OperationHandlerSetup.configureHandler<
  TmdbAuth,
  GetTopRatedMoviesInput,
  GetTopRatedMoviesOutput
>((handler) =>
  handler.withGlobalConfiguration(globalConfigHttp).usingHttp((http) =>
    http
      .get("/3/movie/top_rated")
      .handleRequest((_ctx, _input, request) => request.withoutBody())
      .handleResponse((_ctx, _input, response) =>
        response.parseWithBodyAsJson()
      )
  )
);
Copy
Copied
type GetTopRatedMoviesObject = {
  adult: boolean;
  backdrop_path: string;
  genre_ids: number[];
  id: number;
  original_language: string;
  original_title: string;
  overview: string;
  popularity: number;
  poster_path: string;
  release_date: string;
  title: string;
  video: boolean;
  vote_average: number;
  vote_count: number;
};

export type GetTopRatedMoviesOutput = {
  page: number;
  results: GetTopRatedMoviesObject[];
  total_pages: number;
  total_results: number;
};

Tranforming responses

You may transform the responses received by the 3rd party API. In this case, the content of output file will depend on what you are returning from handler.

Here is an example:

handler.ts (returning transformed response upstream)output.ts (returning transformed response upstream)
Copy
Copied
export const getTopRatedMoviesHandler = OperationHandlerSetup.configureHandler<
  TmdbAuth,
  GetTopRatedMoviesInput,
  GetTopRatedMoviesOutput
>((handler) =>
  handler.withGlobalConfiguration(globalConfigHttp).usingHttp((http) =>
    http
      .get("/3/movie/top_rated")
      .handleRequest((_ctx, _input, request) => request.withoutBody())
      .handleResponse((_ctx, _input, response) => {
        // destructuring response
        const { page, results, total_pages, total_results } = response.parseWithBodyAsJson();

        // sending custom response
        return {
            page_number: page;
            results: results.map(({id, title, overvie, vote_average, vote_count}) => ({
                //mapping response and returning just 5 fields
                id, title, overvie, vote_average, vote_count
            }))
            total_pages: number;
            total_results: number;
        }
      })
  )
);
Copy
Copied
type GetTopRatedMoviesObject = {
  id: number;
  overview: string;
  title: string;
  vote_average: number;
  vote_count: number;
};

export type GetTopRatedMoviesOutput = {
  page_number: number;
  results: GetTopRatedMoviesObject[];
  total_pages: number;
  total_results: number;
};

Composite operation

The content of the file will depend on what is being returned through the success method of the OperationHandlerResult in handler file.

Here is an example:

handler.tsoutput.ts
Copy
Copied
import TurndownService from "turndown";

// converts HTML to markdown using turndown
export const htmlToMarkdownHandler = OperationHandlerSetup.configureHandler<
  DataFormatHelperAuth,
  HtmlToMarkdownInput,
  HtmlToMarkdownOutput
>((handler) =>
  handler.usingComposite(async (ctx, { htmlString }, invoke) => {
    const turndownService = new TurndownService();
    const markdownString = turndownService.turndown(htmlString);
    // returns an object: { markdownString: "" }
    return OperationHandlerResult.success({ markdownString });
  })
);
Copy
Copied
export type HtmlToMarkdownOutput = {
  markdownString: string;
};