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

targets-and-clients.mddocs/

Targets and Clients

HTTPSnippet supports code generation for 20+ programming languages (targets), each with multiple library implementations (clients). The target system is extensible, allowing you to add custom languages and clients.

Capabilities

Available Targets

Get information about all supported language targets and their available clients.

/**
 * Get all available language targets with their clients
 * @returns Array of target information including available clients
 */
function availableTargets(): AvailableTarget[];

interface AvailableTarget {
  key: TargetId;
  title: string;
  extname: string;
  default: string;
  clients: ClientInfo[];
}

interface ClientInfo {
  key: string;
  title: string;
  link: string;
  description: string;
}

Usage Example:

import { availableTargets } from "httpsnippet";

const targets = availableTargets();

// List all targets
targets.forEach(target => {
  console.log(`${target.title} (${target.key}):`);
  target.clients.forEach(client => {
    console.log(`  - ${client.title}: ${client.description}`);
  });
});

// Find specific target
const pythonTarget = targets.find(t => t.key === 'python');
console.log(`Python default client: ${pythonTarget.default}`);

File Extensions

Get the appropriate file extension for generated code snippets.

/**
 * Get file extension for a target
 * @param targetId - Target language identifier
 * @returns File extension including dot (e.g., '.py', '.js') or empty string
 */
function extname(targetId: TargetId): string;

Usage Example:

import { extname } from "httpsnippet";

const extension = extname('python'); // '.py'
const jsExtension = extname('javascript'); // '.js'
const curlExtension = extname('shell'); // '.sh'

Targets Registry

Access the complete registry of all built-in language targets.

/**
 * Registry of all available language targets
 * Maps target IDs to their complete Target definitions
 */
const targets: Record<TargetId, Target>;

Usage Example:

import { targets } from "httpsnippet";

// Access specific target
const pythonTarget = targets.python;
console.log(pythonTarget.info.title); // 'Python'
console.log(pythonTarget.info.default); // 'requests'

// List all Python clients
Object.keys(pythonTarget.clientsById).forEach(clientId => {
  const client = pythonTarget.clientsById[clientId];
  console.log(`${client.info.title}: ${client.info.description}`);
});

// Check if target exists
if ('rust' in targets) {
  console.log('Rust target is available');
}

Adding Custom Targets

Extend HTTPSnippet with custom language targets for unsupported languages or frameworks.

/**
 * Add a new language target
 * @param target - Target definition with info and clients
 */
function addTarget(target: Target): void;

interface Target {
  info: TargetInfo;
  clientsById: Record<ClientId, Client>;
}

interface TargetInfo {
  key: TargetId;
  title: string;
  extname: Extension;
  default: string;
}

interface Client<T extends Record<string, any> = Record<string, any>> {
  info: ClientInfo;
  convert: Converter<T>;
}

type Converter<T extends Record<string, any>> = (
  request: Request,
  options?: CodeBuilderOptions & T
) => string;

type Extension = `.${string}` | null;

Usage Example:

import { addTarget, CodeBuilder } from "httpsnippet";

// Define a custom Dart target
const dartTarget = {
  info: {
    key: 'dart',
    title: 'Dart',
    extname: '.dart',
    default: 'http'
  },
  clientsById: {
    http: {
      info: {
        key: 'http',
        title: 'HTTP Package',
        link: 'https://pub.dev/packages/http',
        description: 'Official HTTP client for Dart'
      },
      convert: (request, options = {}) => {
        const builder = new CodeBuilder(options);
        builder.push('import "package:http/http.dart" as http;');
        builder.push('');
        builder.push(`var response = await http.${request.method.toLowerCase()}(`);
        builder.push(`  Uri.parse("${request.url}"),`, 1);
        // Add headers, body, etc...
        builder.push(');');
        return builder.join();
      }
    }
  }
};

addTarget(dartTarget);

Adding Clients to Existing Targets

Add new client libraries to existing language targets.

/**
 * Add a client to an existing target
 * @param targetId - Existing target identifier
 * @param client - Client definition
 */
function addTargetClient(targetId: TargetId, client: Client): void;

Usage Example:

import { addTargetClient } from "httpsnippet";

// Add a custom JavaScript client using a different library
const customClient = {
  info: {
    key: 'superagent',
    title: 'SuperAgent',
    link: 'https://github.com/visionmedia/superagent',
    description: 'Ajax for Node.js and browsers'
  },
  convert: (request, options = {}) => {
    const builder = new CodeBuilder(options);
    builder.push('const request = require("superagent");');
    // Implementation...
    return builder.join();
  }
};

addTargetClient('javascript', customClient);

Target Validation

Validate target and client structures before adding them to the system.

/**
 * Validate that a target has the correct structure
 * @param target - Target to validate
 * @returns True if target is valid (throws error if invalid)
 */
function isTarget(target: Target): target is Target;

/**
 * Validate that a client has the correct structure
 * @param client - Client to validate
 * @returns True if client is valid (throws error if invalid)
 */
function isClient(client: Client): client is Client;

Supported Targets and Clients

HTTPSnippet includes the following built-in language targets:

Shell

  • curl: cURL command-line tool
  • httpie: HTTPie command-line tool
  • wget: GNU Wget

JavaScript

  • xhr: XMLHttpRequest
  • fetch: Fetch API
  • axios: Axios library
  • jquery: jQuery AJAX

Node.js

  • native: Node.js built-in http/https modules
  • request: Request library (deprecated but still supported)
  • axios: Axios library
  • fetch: node-fetch library
  • unirest: Unirest library

Python

  • requests: Requests library
  • python3: Built-in urllib

Java

  • okhttp: OkHttp library
  • unirest: Unirest library
  • asynchttp: AsyncHttpClient
  • nethttp: Java 11+ HttpClient

C#

  • httpclient: HttpClient class
  • restsharp: RestSharp library

Go

  • native: Built-in net/http package

PHP

  • curl: cURL extension
  • guzzle: Guzzle HTTP library
  • http1: pecl_http v1
  • http2: pecl_http v2

Ruby

  • native: Built-in Net::HTTP
  • faraday: Faraday library

Swift

  • nsurlsession: URLSession

Objective-C

  • nsurlsession: NSURLSession

C

  • libcurl: libcurl library

Crystal

  • native: Built-in HTTP::Client

Rust

  • reqwest: Reqwest library

R

  • httr: httr package

OCaml

  • cohttp: Cohttp library

Clojure

  • clj_http: clj-http library

Kotlin

  • okhttp: OkHttp library

PowerShell

  • webrequest: Invoke-WebRequest cmdlet
  • restmethod: Invoke-RestMethod cmdlet

HTTP

  • http1.1: Raw HTTP/1.1 format

Types

type TargetId = 'c' | 'clojure' | 'crystal' | 'csharp' | 'go' | 'http' | 'java' | 
  'javascript' | 'kotlin' | 'node' | 'objc' | 'ocaml' | 'php' | 'powershell' |
  'python' | 'r' | 'ruby' | 'rust' | 'shell' | 'swift';

type ClientId = string;

interface Target {
  info: TargetInfo;
  clientsById: Record<ClientId, Client>;
}

interface TargetInfo {
  key: TargetId;
  title: string;
  extname: Extension;
  default: string;
}

interface Client<T extends Record<string, any> = Record<string, any>> {
  info: ClientInfo;
  convert: Converter<T>;
}

interface ClientInfo {
  key: ClientId;
  title: string;
  link: string;
  description: string;
}

type Converter<T extends Record<string, any>> = (
  request: Request,
  options?: CodeBuilderOptions & T
) => string;