mirror of
https://github.com/Kong/insomnia.git
synced 2026-11-02 03:00:25 -05:00
[INS-3767](https://konghq.atlassian.net/browse/INS-3767) ## Summary - Remove scripting-environment imports from Insomnia application modules. - Move shared faker, URL helpers, and response reason data into appropriate insomnia-data modules. - Move certificate matching behavior and its test into the ClientCertificate model. - Update all callers and remove obsolete module paths. ## Verification - insomnia-data type-check and lint - insomnia-scripting-environment lint and tests - insomnia type-check - URL/certificate tests: 50 passed - circular-reference check: no new circular dependencies; insomnia-scripting-environment has 0 cycles The existing scripting-environment tsconfig is intentionally unchanged here; its source-wide type-check scope is enabled in the stacked sendRequest PR after the remaining Insomnia runtime dependency is removed.
49 lines
1.3 KiB
TypeScript
49 lines
1.3 KiB
TypeScript
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
/**
|
|
* Generate an ID of the format "<MODEL_NAME>_<TIMESTAMP><RANDOM>"
|
|
* @param prefix
|
|
* @returns {string}
|
|
*/
|
|
export function generateId(prefix?: string) {
|
|
const id = uuidv4().replace(/-/g, '');
|
|
|
|
if (prefix) {
|
|
return `${prefix}_${id}`;
|
|
}
|
|
return id;
|
|
}
|
|
|
|
/**
|
|
* Turns arbitrary text into a filesystem-safe slug: lowercased, accents
|
|
* stripped, runs of non-alphanumeric characters collapsed to a single `-`,
|
|
* leading/trailing `-` trimmed, and truncated to `maxLength`.
|
|
*
|
|
* Returns `''` when nothing usable remains (e.g. all-emoji or all-punctuation
|
|
* input) — callers should treat that as "no slug available".
|
|
*/
|
|
export function slugify(input: string, maxLength = 40) {
|
|
const slug = input
|
|
.normalize('NFKD')
|
|
.replace(/[\u0300-\u036F]/g, '') // strip accents (combining diacritical marks)
|
|
.toLowerCase()
|
|
.replace(/[^a-z0-9]+/g, '-')
|
|
.replace(/^-+|-+$/g, '');
|
|
|
|
if (slug.length <= maxLength) {
|
|
return slug;
|
|
}
|
|
return slug.slice(0, maxLength).replace(/-+$/g, '');
|
|
}
|
|
|
|
const ESCAPE_REGEX_MATCH = /[-[\]/{}()*+?.\\^$|]/g;
|
|
|
|
/**
|
|
* Escape a dynamic string for use inside of a regular expression
|
|
* @param str - string to escape
|
|
* @returns {string} escaped string
|
|
*/
|
|
export function escapeRegex(str: string) {
|
|
return str.replace(ESCAPE_REGEX_MATCH, '\\$&');
|
|
}
|