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:
require
statements.deno fmt
): Consistent code styling is ensured without relying on external tools.await
support.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
Remember to update your $PATH
environment variable. Windows users can leverage Chocolatey:
choco install deno
Alternative installation methods are listed on the deno_install
page. Verify installation with:
deno -V
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" }
Creating Your First Deno Script:
Create index.ts
with the following:
console.log("Hello, world!");
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);
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
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
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!