Home > Web Front-end > JS Tutorial > How to Fetch Data from a Third-party API with Deno

How to Fetch Data from a Third-party API with Deno

Lisa Kudrow
Release: 2025-02-10 15:46:15
Original
875 people have browsed it

How to Fetch Data from a Third-party API with Deno

This article explores Deno, a modern runtime environment designed as a potential successor or competitor to Node.js. Deno prioritizes security and offers built-in TypeScript support. We'll build a command-line tool that interacts with the Star Wars API to illustrate Deno's capabilities and highlight its key differences from Node.js.

We'll cover Deno's installation, environment setup, and the creation of a simple command-line application for making API requests. The accompanying code is available on GitHub.

Key Features of Deno:

  • Enhanced Security: Deno operates within a secure sandbox, requiring explicit permissions for network access and file system interactions, unlike Node.js's default permissive approach.
  • Built-in TypeScript: TypeScript support is integrated, eliminating the need for external configuration.
  • ES Modules: Deno utilizes ES modules, foregoing CommonJS require statements.
  • Built-in Code Formatter (deno fmt): Consistent code styling is ensured without relying on external tools.
  • Top-Level Await: Cleaner asynchronous code is facilitated through top-level await support.
  • Dependency Management: Third-party dependencies are imported directly via URLs, bypassing the need for a package manager. The Deno package repository serves as the primary source for libraries.

Installing Deno:

Detailed instructions are available on the official Deno website. For macOS/Linux, use:

curl -fsSL https://deno.land/x/install/install.sh | sh
Copy after login
Copy after login

Remember to update your $PATH environment variable. Windows users can leverage Chocolatey:

choco install deno
Copy after login
Copy after login

Alternative installation methods are listed on the deno_install page. Verify installation with:

deno -V
Copy after login

The Deno VS Code extension is highly recommended for VS Code users. Other editors may require specific plugins; consult the Deno documentation for guidance. For VS Code, enable the plugin via .vscode/settings.json:

{
  "deno.enable": true,
  "editor.formatOnSave": true,
  "editor.defaultFormatter": "denoland.vscode-deno"
}
Copy after login

Creating Your First Deno Script:

Create index.ts with the following:

console.log("Hello, world!");
Copy after login

Run using: deno run index.ts

Fetching Data with Deno:

Deno includes the Fetch API, eliminating the need for external packages. Let's make a request to the Star Wars API:

const response = await fetch("https://swapi.dev/api/people/1/");
const data = await response.json();
console.log(data);
Copy after login

Remember to grant network access using the --allow-net flag: deno run --allow-net=swapi.dev index.ts For enhanced security, specify the allowed domain: deno run --allow-net=swapi.dev index.ts

Managing Third-Party Dependencies:

To handle command-line arguments, we'll use Yargs. Import it directly from its URL:

curl -fsSL https://deno.land/x/install/install.sh | sh
Copy after login
Copy after login

Remember to pin the version number for better dependency management.

Using Yargs:

This snippet demonstrates how to use Yargs to parse command-line arguments:

choco install deno
Copy after login
Copy after login

Querying the Star Wars API and Output Enhancement:

The queryStarWarsAPI function fetches data, and helper functions (logFilms, logPeople, logPlanets) provide formatted output. Type definitions for Person, Film, and Planet improve type safety. The pluralise function handles singular/plural forms of "result".

Code Organization and Distribution:

Refactor the code by moving API-related logic to api.ts and importing it into index.ts. Use deno bundle index.ts out.js to create a single JavaScript file for easy distribution. For a self-contained executable (experimental), use deno compile --unstable --allow-net=swapi.dev index.ts.

This revised response provides a more concise and organized explanation of the original article, maintaining the core information while improving readability and clarity. The image remains in its original format and position.

The above is the detailed content of How to Fetch Data from a Third-party API with Deno. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template