Skip to content

updating node-fetch to v3 #3051

Open
Open
@jimmywarting

Description

@jimmywarting

const fetch = require("node-fetch");
function convertToGitHubApiUrl(githubPath) {
const urlPieces = githubPath.split('/');
const [user, repo] = urlPieces.slice(0, 2);
// TODO(davideast): Don't hardcode main branch
const githubApiUrl = [user, repo, 'master', ...urlPieces.slice(2, urlPieces.length)].join('/');
return `https://raw.githubusercontent.com/${githubApiUrl}`;
}
async function fetchCode(githubPath) {
const githubApiUrl = convertToGitHubApiUrl(githubPath);
const response = await fetch(githubApiUrl);
return response.text();
}

to use latest node-fetch version you would need to use import instead of require

 async function fetchCode(githubPath) { 
   const fetch = globalThis.fetch || import('node-fetch').then(mod => mod.default);
   const githubApiUrl = convertToGitHubApiUrl(githubPath); 
   const response = await fetch(githubApiUrl); 
   return response.text(); 
 } 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions