UNPKG

counterfact

Version:

Generate a TypeScript-based mock server from an OpenAPI spec in seconds — with stateful routes, hot reload, and REPL support.

53 lines (52 loc) 1.83 kB
import { generate } from "json-schema-faker"; /** * A collection of utility helpers made available to route handlers via * `$.tools`. * * Provides random selection, content-type acceptance checking, and * schema-based random data generation. */ export class Tools { headers; constructor({ headers = {}, } = {}) { this.headers = headers; } /** * Returns a randomly selected element from `array`. * * @param array - The array to pick from. * @returns A random element. */ oneOf(array) { return array[Math.floor(Math.random() * array.length)]; } /** * Returns `true` when the request's `Accept` header is compatible with * `contentType`. * * A missing or empty `Accept` header is treated as `*/*` (accepts anything). * * @param contentType - The MIME type to check (e.g. `"application/json"`). */ accepts(contentType) { const acceptHeader = Object.entries(this.headers).find(([key]) => key.toLowerCase() === "accept")?.[1]; if (acceptHeader === "" || acceptHeader === undefined) { return true; } const acceptTypes = String(acceptHeader).split(","); return acceptTypes.some((acceptType) => { const [type, subtype] = acceptType.trim().split("/"); return ((type === "*" || type === contentType.split("/")[0]) && (subtype === "*" || subtype === contentType.split("/")[1])); }); } /** * Generates a random value that satisfies `schema` using json-schema-faker. * * @param schema - A JSON Schema object. * @returns A promise that resolves to a generated value. */ randomFromSchema(schema) { return generate(schema, { useExamplesValue: true, fillProperties: false }); } }