How do I import the Context type for local development with Netlify Edge Functions?

I know that Deno doesn’t support package imports but when I add the following into my edge functions code (using Typescript), it doesn’t include the Context type with autocomplete/intellisense.

import type { Context } from "https://edge.netlify.com" // Context doesn't include intellisense

I get the following error in VSCode

Cannot find module 'https://edge.netlify.com' or its corresponding type declarations.

I can locally add in the type using the following, but I imagine I might be doing something wrong for local dev. I read through this post, but wasn’t able to figure out how url imports work with types in local development.

type json = string | number | boolean | null | json[] | { [key: string]: json };

type Context = {
  cookies: {
    get: (name: string) => string | null;
    set: (options: {
      name: string;
      value: string;
      expires?: number;
      domain?: string;
      path?: string;
      sameSite?: "strict" | "lax" | "none";
    }) => Promise<undefined>;
    delete: (options: {
      name: string;
      url?: string;
      path?: string;
    }) => Promise<undefined>;
  };
  geo: {
    city: string;
    country: {
      code: string;
      name: string;
    };
    subdivision: {
      code: string;
      name: string;
    };
  };
  json: () => json;
  log: (values: any[]) => void;
  next: () => Promise<Response>;
  rewrite: (url: URL | string) => URL;
};

For context, I’m using Astro

Hi @mikec, there is a similar thread on this forum discussing the problem you are experiencing with imports regarding edge functions.
Kindly check the link below to visit the post thread.

Kindly try out the suggestions in post and let me know the outcome.

Hope this helps thanks.

Ahh I see what happened, I had to install the Deno VS Code extension in VS Code and now it works!

I think that when I ran ntl dev, it initially tried to set it up in a .vscode folder for me but didn’t seem to configure it. For reference, it created the following three files in a .vscode folder:

extensions.json

{
  "recommendations": ["astro-build.astro-vscode"],
  "unwantedRecommendations": []
}

launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "command": "./node_modules/.bin/astro dev",
      "name": "Development server",
      "request": "launch",
      "type": "node-terminal"
    }
  ]
}

settings.json

{
  "deno.enable": true,
  "deno.enablePaths": [
    "netlify/edge-functions"
  ],
  "deno.unstable": true,
  "deno.importMap": ".netlify/edge-functions-import-map.json",
  "deno.path": "/Users/myname/Library/Preferences/netlify/deno-cli/deno"
}
1 Like

Hello @mikec, glad to know you were able to fix the problem.

1 Like