Step 7: Tips and Tricks

Deno vs. Node

Deno is very similar to Node but there are gotchas to keep in mind. - All imports in Deno use the exact file path with the extension (e.g import xx from yy.ts) - You don’t use NPM with Deno and instead use exact file paths to modules. You can learn more here . - Deno has very strict permissions out of the box unlike node. When running functions the Yext hosting environment will use --allow-net so that you can make external API calls

Additional Files

You DON’T need to put everything in mod.ts. That’s the entry point but you can easily add additional .ts files to the folder and reference those using import.

Function Signatures

Depending on the use case for your function the input and the output might be a different format. Here are some input and output formats for different areas of the platform:

Use Case Input Output
Connector Source Optional next page token string { data: string, nextPageToken: string} (JSON Stringified)
Connector Transform string string

Connector Source

As you can see above, the connector source returns a JSON stringified object. Make sure to do this before returning data. For example:

export function sourceFunction(nextPageToken?: string) {
  //Logic goes here

 const data = [{ name: "Item 1" }, { name: "Item 2" }];
 const nextPageToken = "hello";
 if (nextPageToken) {
   return JSON.stringify({ data });
 } else {
   return JSON.stringify({ data, nextPageToken });
 }
}

Also note that if you return the nextPageToken your function will get called again. Make sure you don’t return the nextPageToken on the final page or else you will have an infinite loop.

If you want to test the connector function you should write a test function that looks something like this. This loops through the function until no page token is returned.

import { myConnectorSource } from "./mod.ts";

Deno.test("Test Connector Source", () => {
 let nextPageToken: string | undefined = undefined;
 let fullData: unknown[] = [];
 let pageNumber = 1;
 do {
   const res: string = myConnectorSource(nextPageToken);
   const resObj = JSON.parse(res);
   const data = resObj.data;
   nextPageToken = resObj.nextPageToken;

   console.log(`Found ${data.length} rows on page ${pageNumber}`);

   fullData = [...fullData, ...data];

   pageNumber += 1;
 } while (nextPageToken);
});

Check out this repo for an example that uses IMDB APIs to get movie details and images for every movie released in a given year.

Handling Global Variables in test.ts

Global variables declared in _resource.json are only accessible at runtime in the platform. Therefore, you need to redeclare globals in test.ts to avoid exceptions.

declare global {
  var API_KEY: string;
}

globalThis.API_KEY = 'abcd123';

If you don’t want the value of the global hardcoded in test.ts you could set the value as undefined.

declare global {
  var API_KEY: string | undefined;
}

globalThis.API_KEY = undefined;

Then, add the API Key to a .env file.

API_KEY=abcd123

And finally use dotenv to grab the value saved in your .env file if you’re running a test.

import { config } from "https://deno.land/x/dotenv/mod.ts";

declare const API_KEY: string;

export function sourceFunction(nextPageToken?: string) {
  const apiKey = API_KEY ?? config().API_KEY; 
  //Logic goes here
}
Feedback