azion
Version:
Azion Packages for Edge Computing.
142 lines (137 loc) • 4.83 kB
TypeScript
interface Metadata {
geoip_asn: string;
geoip_city: string;
geoip_city_continent_code: string;
geoip_city_country_code: string;
geoip_city_country_name: string;
geoip_continent_code: string;
geoip_country_code: string;
geoip_country_name: string;
geoip_region: string;
geoip_region_name: string;
remote_addr: string;
remote_port: string;
remote_user: string;
server_protocol: string;
ssl_cipher: string;
ssl_protocol: string;
[key: string]: string;
}
/**
* Represents the FetchEvent interface.
*/
interface FetchEvent extends Event {
request: Request & {
metadata: Metadata;
};
waitUntil(promise: Promise<any>): void;
respondWith(response: Response | Promise<Response>): void;
}
/**
* Represents the request URL for the SSG or SPA.
*/
type RequestURL = string;
/**
* Represents the asset path in the file system.
*/
type AssetPath = URL;
/**
* Function that mounts the SSG for a specific request.
* @param requestURL - The original URL from the event request.
* @returns A promise that resolves to the response from the SSG.
*/
type mountMPAFunction = (requestURL: RequestURL) => Promise<Response>;
/**
* Function that mounts the SPA for a specific request.
* @param requestURL - The original URL from the event request.
* @returns A promise that resolves to the response from the SPA.
*/
type MountSPAFunction = (requestURL: RequestURL) => Promise<Response>;
/**
* Function that parses and logs the details of an incoming request.
* @param event - The incoming FetchEvent object.
* @returns A promise that resolves to the ParsedRequest object.
*/
type ParseRequestFunction = (event: FetchEvent) => Promise<ParsedRequest>;
/**
* Function that parses and logs the details of an incoming request.
* @param event - The incoming FetchEvent object.
* @returns A promise that resolves to the request details object.
*/
type ParsedRequest = {
timestamp: string;
method: string;
url: {
full: string;
protocol: string;
hostname: string;
path: string;
query: Record<string, string>;
};
headers: Record<string, string>;
cookies: Record<string, string>;
body: string | null;
client: {
ip: string;
userAgent: string;
};
referer: string;
origin: string;
cacheControl: string;
pragma: string;
contentType: string;
contentLength: string;
acceptLanguage: string;
acceptEncoding: string;
priority: string;
host: string;
authorization: string;
metadata: Metadata;
};
/**
* @function
* @description The `mountMPA` function handles requests for Static Site Generation (SSG)
* at the edge, directly within a serverless worker.
* It processes the incoming request URL, constructs the appropriate asset path,
* and fetches the corresponding response from the SSG.
* @param {RequestURL} requestURL - The original URL from the event request.
* @returns {Promise<Response>} A promise that resolves to the response from the SSG.
* @example
* // Handle a request for a homepage
* // Input: mountMPA('https://example.com/');
* // Output: fetch('file:///index.html');
* @example
* // Handle a request for an asset (CSS file)
* // Input: mountMPA('https://example.com/styles/main.css');
* // Output: fetch('file:///styles/main.css');
* @example
* // Handle a request for a specific route
* // Input: mountMPA('https://example.com/about');
* // Output: fetch('file:///about/index.html');
*/
declare const mountMPA: mountMPAFunction;
/**
* @function
* @description The `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).
*
* This function determines if the incoming request is for a static
* asset or a route within the application,
* and mounts the appropriate request URL for fetching the required resource from the origin server.
* @param {RequestURL} requestURL - The original URL from the incoming request.
* @returns {Promise<Response>} A promise that resolves to the response from the fetched resource.
* @example
* // Handle a request for a homepage
* // Input: mountSPA('https://example.com/');
* // Output: fetch('file:///index.html');
* @example
* // Handle a request for an asset (CSS file)
* // Input: mountSPA('https://example.com/styles/main.css');
* // Output: fetch('file:///styles/main.css');
* @example
* // Handle a request for a specific route
* // Input: mountSPA('https://example.com/about');
* // Output: fetch('file:///index.html');
*/
declare const mountSPA: MountSPAFunction;
export { type AssetPath, type MountSPAFunction, type ParseRequestFunction, type ParsedRequest, type RequestURL, mountMPA, type mountMPAFunction, mountSPA };