UNPKG

mentoss

Version:

A utility to mock fetch requests and responses.

140 lines (139 loc) 5.48 kB
/** * Creates a CORS error object. * @param {string} requestUrl The URL of the request. * @param {string} origin The origin of the client making the request. * @param {string} message The error message. * @returns {TypeError} A TypeError with CORS error message. */ export function createCorsError(requestUrl: string, origin: string, message: string): TypeError; /** * Creates a CORS preflight error object. * @param {string} requestUrl The URL of the request. * @param {string} origin The origin of the client making the request. * @param {string} message The error message. * @returns {TypeError} A TypeError with CORS preflight error message. */ export function createCorsPreflightError(requestUrl: string, origin: string, message: string): TypeError; /** * Asserts that the response has the correct CORS headers. * @param {Response} response The response to check. * @param {string} origin The origin to check against. * @param {boolean} isPreflight `true` if this is a preflight request, `false` otherwise. * @returns {void} * @throws {Error} When the response doesn't have the correct CORS headers. */ export function assertCorsResponse(response: Response, origin: string, isPreflight?: boolean): void; /** * Asserts that the response has the correct CORS headers for credentials. * @param {Response} response The response to check. * @param {string} origin The origin to check against. * @returns {void} * @throws {CorsError} When the response doesn't have the correct CORS headers for credentials. * @see https://fetch.spec.whatwg.org/#http-headers */ export function assertCorsCredentials(response: Response, origin: string): void; /** * Asserts that a request is valid for "no-cors" mode. * @param {RequestInit} requestInit The request to check. * @returns {void} * @throws {TypeError} When the request is not valid for "no-cors" mode. */ export function assertValidNoCorsRequestInit(requestInit?: RequestInit): void; /** * Processes a CORS response to ensure it's valid and doesn't contain * any forbidden headers. * @param {Response} response The response to process. * @param {string} origin The origin of the request. * @param {boolean} useCorsCredentials `true` if credentials are used, `false` otherwise. * @returns {Response} The processed response. */ export function processCorsResponse(response: Response, origin: string, useCorsCredentials: boolean): Response; /** * Determines if a request is a simple CORS request. * @param {Request} request The request to check. * @returns {boolean} `true` if the request is a simple CORS request, `false` otherwise. */ export function isCorsSimpleRequest(request: Request): boolean; /** * Validates a CORS request. * @param {Request} request The request to validate. * @param {string} origin The origin of the request. * @returns {void} * @throws {CorsError} When the request is not allowed. */ export function validateCorsRequest(request: Request, origin: string): void; /** * Gets an array of headers that are not allowed in a CORS simple request. * @param {Request} request The request to check. * @returns {string[]} Array of header names that are not simple headers. */ export function getUnsafeHeaders(request: Request): string[]; /** * @fileoverview CORS utilities for Fetch API requests. * @author Nicholas C. Zakas */ export const safeMethods: Set<string>; export const alwaysSafeRequestHeaders: Set<string>; export const safeRequestHeaders: Set<string>; export const forbiddenRequestHeaders: Set<string>; export const safeResponseHeaders: Set<string>; export const forbiddenResponseHeaders: Set<string>; export const forbiddenMethods: Set<string>; export const CORS_ALLOW_ORIGIN: "Access-Control-Allow-Origin"; export const CORS_ALLOW_CREDENTIALS: "Access-Control-Allow-Credentials"; export const CORS_EXPOSE_HEADERS: "Access-Control-Expose-Headers"; export const CORS_ALLOW_METHODS: "Access-Control-Allow-Methods"; export const CORS_ALLOW_HEADERS: "Access-Control-Allow-Headers"; export const CORS_MAX_AGE: "Access-Control-Max-Age"; export const CORS_REQUEST_METHOD: "Access-Control-Request-Method"; export const CORS_REQUEST_HEADERS: "Access-Control-Request-Headers"; export const CORS_ORIGIN: "Origin"; /** * A class for storing CORS preflight data. */ export class CorsPreflightData { /** * Creates a new instance. * @param {Headers} headers The headers to use. */ constructor(headers: Headers); /** * The allowed methods for this URL. * @type {Set<string>} */ allowedMethods: Set<string>; /** * Whether all methods are allowed for this URL. * @type {boolean} */ allowAllMethods: boolean; /** * The allowed headers for this URL. * @type {Set<string>} **/ allowedHeaders: Set<string>; /** * Whether all headers are allowed for this URL. * @type {boolean} */ allowAllHeaders: boolean; /** * Whether credentials are allowed for this URL. * @type {boolean} */ allowCredentials: boolean; /** * The maximum age for this URL. * @type {number} */ maxAge: number; /** * Validates a request against the preflight data. * @param {Request} request The request to validate. * @param {string} origin The origin of the request. * @returns {void} * @throws {Error} When the request is not allowed. */ validate(request: Request, origin: string): void; #private; }