or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

cli-usage.mdcore-api.mdhelper-utilities.mdindex.mdtargets-and-clients.md
tile.json

helper-utilities.mddocs/

Helper Utilities

HTTPSnippet provides several utility modules for code generation, string manipulation, and header processing. These utilities are useful both within HTTPSnippet itself and for building custom targets and clients.

Capabilities

Code Builder

Utility class for constructing formatted code snippets with proper indentation and line joining.

/**
 * Helper class to format and aggregate lines of code
 * @param options - Configuration for indentation and line joining
 */
class CodeBuilder {
  constructor(options?: CodeBuilderOptions);
  
  /** Add line at the end with optional indentation */
  push(line: string, indentationLevel?: number): void;
  
  /** Add line at the beginning with optional indentation */
  unshift(line: string, indentationLevel?: number): void;
  
  /** Append text to the last line */
  pushToLast(line: string): void;
  
  /** Add empty line */
  blank(): void;
  
  /** Apply indentation to a line */
  indentLine(line: string, indentationLevel?: number): string;
  
  /** Join all lines and apply post-processors */
  join(): string;
  
  /** Add post-processing function */
  addPostProcessor(postProcessor: PostProcessor): void;
}

interface CodeBuilderOptions {
  /** Indentation character (default: '') */
  indent?: string;
  /** Line join character (default: '\n') */
  join?: string;
}

type PostProcessor = (unreplacedCode: string) => string;

Usage Examples:

import { CodeBuilder } from "httpsnippet";

// Basic usage
const builder = new CodeBuilder({ indent: '  ', join: '\n' });
builder.push('function makeRequest() {');
builder.push('const url = "https://api.example.com/users";', 1);
builder.push('return fetch(url);', 1);
builder.push('}');

const code = builder.join();
console.log(code);
// Output:
// function makeRequest() {
//   const url = "https://api.example.com/users";
//   return fetch(url);
// }

// With post-processor
builder.addPostProcessor(code => 
  code.replace(/fetch/g, 'window.fetch')
);

String Escaping

Functions for safely escaping strings in different contexts for code generation.

/**
 * Escape characters within a value for safe insertion into code snippets
 * @param rawValue - Value to escape (converted to string)
 * @param options - Escape configuration
 * @returns Escaped string
 */
function escapeString(rawValue: any, options?: EscapeOptions): string;

interface EscapeOptions {
  /** Delimiter to escape (default: '"') */
  delimiter?: string;
  /** Escape character (default: '\') */
  escapeChar?: string;
  /** Whether to escape newlines (default: true) */
  escapeNewlines?: boolean;
}

Usage Examples:

import { escapeString } from "httpsnippet";

// Basic escaping
const userInput = 'Hello "world" \n with newlines';
const escaped = escapeString(userInput);
console.log(`"${escaped}"`); // "Hello \"world\" \n with newlines"

// Custom options
const customEscaped = escapeString('Line 1\nLine 2', {
  delimiter: '`',
  escapeChar: '\\',
  escapeNewlines: false
});

Header Utilities

Case-insensitive header processing utilities for HTTP request manipulation.

/**
 * Get header value by case-insensitive name
 * @param headers - Headers object
 * @param name - Header name (case-insensitive)
 * @returns Header value or undefined
 */
function getHeader<T>(headers: Record<string, T>, name: string): T | undefined;

/**
 * Get actual header name by case-insensitive lookup
 * @param headers - Headers object
 * @param name - Header name to find (case-insensitive)
 * @returns Actual header name or undefined
 */
function getHeaderName<T>(headers: Record<string, T>, name: string): string | undefined;

Usage Examples:

import { getHeader, getHeaderName } from "httpsnippet";

const headers = {
  'Content-Type': 'application/json',
  'Authorization': 'Bearer token123',
  'User-Agent': 'MyApp/1.0'
};

// Case-insensitive header access
const contentType = getHeader(headers, 'content-type'); // 'application/json'
const authHeader = getHeader(headers, 'AUTHORIZATION'); // 'Bearer token123'

// Get actual header name
const actualName = getHeaderName(headers, 'user-agent'); // 'User-Agent'

Form Data Utilities

Internal utilities for handling multipart form data in different environments.

/**
 * Iterator for FormData objects to handle browser/Node.js differences
 * @param form - FormData instance
 * @param boundary - Multipart boundary string
 * @returns Generator yielding form data chunks
 */
function formDataIterator(form: FormData, boundary: string): Generator<string>;

/**
 * Check if value is a Blob object
 * @param value - Value to check
 * @returns True if value is Blob
 */
function isBlob(value: any): boolean;

Object Reduction Utilities

Utilities for converting arrays to objects, commonly used for processing HAR data.

/**
 * Reducer function for converting name/value arrays to objects
 * @param accumulator - Accumulating object
 * @param param - Name/value pair
 * @returns Updated accumulator
 */
function reducer(
  accumulator: Record<string, any>, 
  param: { name: string; value: string }
): Record<string, any>;

type ReducedHelperObject = Record<string, string[] | string>;

Usage Example:

import { reducer } from "httpsnippet";

const queryParams = [
  { name: 'page', value: '1' },
  { name: 'limit', value: '10' },
  { name: 'sort', value: 'name' }
];

const queryObj = queryParams.reduce(reducer, {});
console.log(queryObj); // { page: '1', limit: '10', sort: 'name' }

Shell Utilities

Utilities for shell command generation and escaping.

/**
 * Quote and escape strings for shell command safety
 * @param value - Value to quote
 * @returns Shell-safe quoted string
 */
function quote(value: string): string;

Usage Example:

import { quote } from "httpsnippet";

const unsafeUrl = "https://api.example.com/search?q=hello world&type=user";
const safeUrl = quote(unsafeUrl);
console.log(`curl ${safeUrl}`); // curl 'https://api.example.com/search?q=hello world&type=user'

Additional String Escaping Functions

Convenience functions for common escaping scenarios.

/**
 * Make a string safe for single-quoted contexts
 * @param value - Value to escape
 * @returns Escaped string safe for single quotes
 */
function escapeForSingleQuotes(value: any): string;

/**
 * Make a string safe for double-quoted contexts  
 * @param value - Value to escape
 * @returns Escaped string safe for double quotes
 */
function escapeForDoubleQuotes(value: any): string;

/**
 * Escape newlines and carriage returns for shell contexts
 * @param value - String value to escape
 * @returns String with escaped newlines
 */
function escape(value: string): string;

Usage Examples:

import { escapeForSingleQuotes, escapeForDoubleQuotes, escape } from "httpsnippet";

const userInput = 'Text with "quotes" and newlines\n';

// Single quote escaping
const singleQuoted = escapeForSingleQuotes(userInput);
console.log(`'${singleQuoted}'`);

// Double quote escaping  
const doubleQuoted = escapeForDoubleQuotes(userInput);
console.log(`"${doubleQuoted}"`);

// Shell newline escaping
const shellEscaped = escape('line1\nline2\r\nline3');
console.log(shellEscaped); // 'line1\\nline2\\r\\nline3'

Additional Header Utilities

Extended header processing functions for advanced header manipulation.

/**
 * Check if a header exists by case-insensitive name
 * @param headers - Headers object
 * @param name - Header name (case-insensitive)
 * @returns True if header exists
 */
function hasHeader<T>(headers: Record<string, T>, name: string): boolean;

/**
 * Check if a MIME type represents JSON content
 * @param mimeType - MIME type string to check
 * @returns True if MIME type is JSON or JSON variant
 */
function isMimeTypeJSON(mimeType: string): boolean;

Usage Examples:

import { hasHeader, isMimeTypeJSON } from "httpsnippet";

const headers = {
  'Content-Type': 'application/json',
  'Authorization': 'Bearer token123'
};

// Check header existence
if (hasHeader(headers, 'content-type')) {
  console.log('Content-Type header is present');
}

// Check JSON MIME types
console.log(isMimeTypeJSON('application/json')); // true
console.log(isMimeTypeJSON('application/vnd.api+json')); // true  
console.log(isMimeTypeJSON('text/json')); // true
console.log(isMimeTypeJSON('text/plain')); // false

Types

interface CodeBuilderOptions {
  indent?: string;
  join?: string;
}

type PostProcessor = (unreplacedCode: string) => string;

interface EscapeOptions {
  delimiter?: string;
  escapeChar?: string;
  escapeNewlines?: boolean;
}

type ReducedHelperObject = Record<string, string[] | string>;