The Azion Utils package provides a set of utility functions designed to simplify common tasks when working with Azion edge functions. These utilities cover tasks such as routing for Single Page Applications (SPA) and Multi-Page Applications (MPA), as well as parsing and debugging incoming requests.
Install the package using npm or yarn:
npm install azionor
yarn add azionThe mountSPA function is designed to process requests to a Single Page Application (SPA) that's being computed at the edge of a Content Delivery Network (CDN). It determines whether the incoming request is for a static asset or an application route and then mounts the appropriate request URL for fetching the required resource.
JavaScript Example:
import { mountSPA } from 'azion/utils';
const myApp = await mountSPA('https://example.com/');
console.log(myApp);
// Fetches: file:///index.html
// Response object representing the content of index.htmlTypeScript Example:
import { mountSPA } from 'azion/utils';
const myApp: Response = await mountSPA('https://example.com/');
console.log(myApp);
// Fetches: file:///index.html
// Response object representing the content of index.htmlThe mountMPA function handles requests for Multi-Page Applications (MPA) at the edge. It processes the incoming request URL, constructs the appropriate asset path, and fetches the corresponding response from the origin server or edge cache.
JavaScript Example:
import { mountMPA } from 'azion/utils';
const myApp = await mountMPA('https://example.com/about');
console.log(myApp);
// Fetches: file:///about/index.html
// Response object representing the content of about/index.htmlTypeScript Example:
import { mountMPA } from 'azion/utils';
const myApp: Response = await mountMPA('https://example.com/about');
console.log(myApp);
// Fetches: file:///about/index.html
// Response object representing the content of about/index.htmlThe parseRequest function is designed to parse and log the details of an incoming request. It extracts key information such as headers, cookies, body, and client data from the incoming FetchEvent, providing a structured object with all these details for further processing or logging.
JavaScript Example:
import { parseRequest } from 'azion/utils';
const parsedRequest = await parseRequest(event);
console.log(parsedRequest);
// Example ParsedRequest object:
// {
// timestamp: '2024-08-14T12:34:56.789Z',
// metadata: {
// geoip_asn: '12345',
// geoip_city: 'Sao Paulo',
// geoip_city_continent_code: 'SA',
// geoip_city_country_code: 'BR',
// geoip_city_country_name: 'Brazil',
// geoip_continent_code: 'SA',
// geoip_country_code: 'BR',
// geoip_country_name: 'Brazil',
// geoip_region: 'SP',
// geoip_region_name: 'Sao Paulo',
// remote_addr: '192.0.2.1',
// remote_port: '443',
// remote_user: 'user',
// server_protocol: 'HTTP/1.1',
// ssl_cipher: 'ECDHE-RSA-AES128-GCM-SHA256',
// ssl_protocol: 'TLSv1.2',
// },
// method: 'GET',
// url: {
// full: 'https://example.com/',
// protocol: 'https:',
// hostname: 'example.com',
// path: '/',
// query: {},
// },
// headers: {
// 'user-agent': 'Mozilla/5.0 ...',
// 'accept-language': 'en-US,en;q=0.9',
// },
// cookies: {
// 'session_id': 'abc123',
// },
// body: null,
// client: {
// ip: '192.0.2.1',
// userAgent: 'Mozilla/5.0 ...',
// },
// referer: 'Unknown',
// origin: 'Unknown',
// cacheControl: 'no-cache',
// pragma: 'no-cache',
// contentType: 'text/html',
// contentLength: '1234',
// acceptLanguage: 'en-US,en;q=0.9',
// acceptEncoding: 'gzip, deflate, br',
// priority: 'High',
// host: 'example.com',
// authorization: 'Not Present',
// }TypeScript Example:
import { parseRequest } from 'azion/utils';
import type { ParsedRequest } from 'azion/utils';
const parsedRequest: ParsedRequest = await parseRequest(event);
console.log(parsedRequest);
// Example ParsedRequest object:
// {
// timestamp: '2024-08-14T12:34:56.789Z',
// metadata: {
// geoip_asn: '12345',
// geoip_city: 'Sao Paulo',
// geoip_city_continent_code: 'SA',
// geoip_city_country_code: 'BR',
// geoip_city_country_name: 'Brazil',
// geoip_continent_code: 'SA',
// geoip_country_code: 'BR',
// geoip_country_name: 'Brazil',
// geoip_region: 'SP',
// geoip_region_name: 'Sao Paulo',
// remote_addr: '192.0.2.1',
// remote_port: '443',
// remote_user: 'user',
// server_protocol: 'HTTP/1.1',
// ssl_cipher: 'ECDHE-RSA-AES128-GCM-SHA256',
// ssl_protocol: 'TLSv1.2',
// },
// method: 'GET',
// url: {
// full: 'https://example.com/',
// protocol: 'https:',
// hostname: 'example.com',
// path: '/',
// query: {},
// },
// headers: {
// 'user-agent': 'Mozilla/5.0 ...',
// 'accept-language': 'en-US,en;q=0.9',
// },
// cookies: {
// 'session_id': 'abc123',
// },
// body: null,
// client: {
// ip: '192.0.2.1',
// userAgent: 'Mozilla/5.0 ...',
// },
// referer: 'Unknown',
// origin: 'Unknown',
// cacheControl: 'no-cache',
// pragma: 'no-cache',
// contentType: 'text/html',
// contentLength: '1234',
// acceptLanguage: 'en-US,en;q=0.9',
// acceptEncoding: 'gzip, deflate, br',
// priority: 'High',
// host: 'example.com',
// authorization: 'Not Present',
// }Processes requests for Single Page Applications, determining the correct path and fetching the required resource.
Handles routing for Multi-Page Applications, constructing the asset path and fetching the corresponding response.
Parses and logs the details of an incoming request, including headers, cookies, and client information.
Feel free to submit issues or pull requests to improve the functionality or documentation.