api-query


目录

title: Sending API Query
functions:
  importFromFigma: '../import/figma/index.md'
  downloadGitHubRepo: './github.md'
  downloadFile: './download-file.md'

Sending API query

This tutorial is part of package functions documentation in Iconify Tools.

Function [func]sendAPIQuery() is a wrapper for [func]fetch with caching.

It is used by functions that retrieve text data from remote sources, such as [func]importFromFigma() and [func]downloadGitHubRepo().

To retrieve binary files, see [func]downloadFile().

Usage

Function has the following parameters:

Function returns:

Function is asynchronous. That means you need to handle it as [class]Promise instance, usually by adding [js]await before function call.

APIQueryParams

[type]APIQueryParams object has the following properties:

Example:

const params = {
	uri: 'https://api.whatever.com/some-file/',
	params: new URLSearchParams({
		version: '123',
	}),
	headers: {
		Accept: 'application/json',
	},
};

APICacheOptions

[type]APICacheOptions object has the following properties:

Cache management

If [prop]cache property is not set, data is not cached.

If [prop]cache property is set, function will generate unique hash for query, will check if file with that hash already exists, if it has not expired. On success it will return cached data instead of fetching new data.

You can purge cache at any time using [func]clearAPICache() function:

import { clearAPICache } from '@iconify/tools/lib/download/api/cache';

const cacheDir = 'cache';

// Asynchronous function, so wrapping it in anonymous async function.
// Not needed for Node 17+ that supports top level await.
(async () => {
	await clearAPICache(cacheDir);
})();

Example

src: libraries/tools/package/api.ts
title: 'example.ts'

版本号 #2
由 壳壳虫 创建于 2026-10-31 14:26:16 UTC
由 壳壳虫 更新于 2026-10-31 14:26:17 UTC