Azion `Purge` library

The Purge library provides methods to interact with the Edge Cache Purge API, allowing you to purge URLs, cache keys, and wildcard expressions from the cache. This client is configurable and supports both debug mode and environment variable-based configuration.

Go to Azion Libraries Overview

You can interact with the API using a client or calling the methods directly from the library. When making direct calls, you can use the environment variables to configure the client without passing the token and debug parameters directly.

This is an example of how a .env file with your environment variables may look like:

Terminal window
AZION_TOKEN=<your-api-token>
AZION_DEBUG=true
VariableDescription
AZION_TOKENYour Azion API token.
AZION_DEBUGEnable debug mode (true/false).

If you want to create a specific client for interacting with Purge, do it by calling the createClient method from the library:

import { createClient } from 'azion/purge';
import type { AzionPurgeClient, AzionPurgeResponse, AzionPurge } from 'azion/purge';
const client: AzionPurgeClient = createClient({ token: 'your-api-token', options: { debug: true } });
const { data: purgeURLResponse, error }: AzionPurgeResponse<AzionPurge> = await client.purgeURL([
'http://www.domain.com/path/image.jpg',
]);
if (purgeURLResponse) {
console.log('Purge successful:', purgeURLResponse);
} else {
console.error('Purge failed', error);
}

The createClient method has the following parameters and return value:

Parameters:

ParameterTypeDescription
configPartial<{ token: string; options?: OptionsParams }>Configuration options for the Purge client.

Returns:

Return TypeDescription
AzionPurgeClientAn object with methods to interact with Purge.

Usage

purgeURL

Purges a URL from the Edge Cache.

Example:

import { purgeURL } from 'azion/purge';
import type { AzionPurgeResponse, AzionPurge } from 'azion/purge';
const url: string[] = ['http://www.domain.com/path/image.jpg'];
const { data: response, error }: AzionPurgeResponse<AzionPurge> = await purgeURL(url, { debug: true });
if (response) {
console.log('Purge successful:', response);
} else {
console.error('Purge failed', error);
}

Parameters:

ParameterTypeDescription
urlstring[]URL(s) to purge.
optionsAzionClientOptions (optional)Client options including debug mode.

Returns:

Return TypeDescription
Promise<AzionPurgeResponse<AzionPurge>>The purge response or error in case of failure.

purgeCacheKey

Purges a cache key from the edge cache.

Example:

import { purgeCacheKey } from 'azion/purge';
import type { AzionPurgeResponse, AzionPurge } from 'azion/purge';
const cacheKey: string[] = ['http://www.domain.com/path/image.jpg'];
const { data: response, error }: AzionPurgeResponse<AzionPurge> = await purgeCacheKey(cacheKey, { debug: true });
if (response) {
console.log('Purge successful:', response);
} else {
console.error('Purge failed', error);
}

Parameters:

ParameterTypeDescription
cacheKeystring[]Cache key(s) to purge.
optionsAzionClientOptions (optional)Client options including debug mode.

Returns:

Return TypeDescription
Promise<AzionPurgeResponse<AzionPurge>>The purge response or error in case of failure.

purgeWildCard

Purges using a wildcard expression from the edge cache.

Example:

import { purgeWildCard } from 'azion/purge';
import type { AzionPurgeResponse, AzionPurge } from 'azion/purge';
const wildcard: string[] = ['http://www.domain.com/path/image.jpg*'];
const { data: response, error }: AzionPurgeResponse<AzionPurge> = await purgeWildCard(wildcard, { debug: true });
if (response) {
console.log('Purge successful:', response);
} else {
console.error('Purge failed', error);
}

Parameters:

ParameterTypeDescription
wildcardstring[]Wildcard expression(s) to purge.
optionsAzionClientOptions (optional)Client options including debug mode.

Returns:

Return TypeDescription
Promise<AzionPurgeResponse<AzionPurge>>The purge response or error in case of failure.

Types

This section defines the various types and interfaces used in the Purge library.

ClientConfig

Configuration options for the Purge client.

ParameterTypeDescription
tokenstring (optional)Your Azion API token.
optionsAzionClientOptions (optional)Additional options for the client.

PurgeClient

An object with methods to interact with Purge.

MethodParametersReturn Type
purgeURLurls: string[], options?: AzionClientOptionsPromise<AzionPurgeResponse<AzionPurge>>
purgeCacheKeycacheKeys: string[], options?: AzionClientOptionsPromise<AzionPurgeResponse<AzionPurge>>
purgeWildCardwildcards: string[], options?: AzionClientOptionsPromise<AzionPurgeResponse<AzionPurge>>

Purge

The response object from a purge request.

PropertyTypeDescription
state`‘executed''pending’`
itemsstring[]The items that were purged.

Contributors