encoredev/encore

"Record"object not found

mohitxskull opened this issue · 3 comments

interface RouteResponse {
  message: string;
  headers?: Record<string, string>;
}

was using this and when i run encore run i get ❌ Building Encore application graph... Failed: object not found: "Record"

Hey @mohitxskull! I don't have everything figured out here (still learning Encore) but I may have an answer! Seems like the Encore likes to have a bit more detail here than Record<string, string> but is magical when it has it. Though Record<string, string> is a valid type Encore likes to have something like this.

import { api, Header } from "encore.dev/api";

interface ReturnValue {
  id: string; // the URL to shorten
  token?: Header<'x-api-token'>
}

// Get retrieves the original URL for the id.
export const get = api(
  { expose: true, auth: false, method: "GET", path: "/url/:id" },
  async ({ id }: { id: string }): Promise<ReturnValue> => {
    return { id, token: 'testing' };
  }
);

When you send a request to that endpoint Encore automatically sets the headers where you asked it to based on the Header<...>.
Screenshot 2024-05-22 at 9 30 34 PM

When I saw this it made sense to me why the Record<string, string> didn't work. Though a valid type in TypeScript, it's pretty difficult to enforce all the permutations of a structure such as Record when Encore automatically (err automagically) sets those values based on the interface.

I don't know if the headings version should work or not but if you were blocked on this I figured I could see if my findings would help!

Just like @tuckers-tech says, when specifying your request and response schema you'll need to specify the header names that your schema requires.

If you for some reason need to access all the headers in the request you can do so via currentRequest, eg.:

import { currentRequest } from "encore.dev";

//...

const headers = currentRequest().headers;

Also note that we have since added support for the Record type as well :)