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.
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}`);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'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');
}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);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);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;HTTPSnippet includes the following built-in language targets:
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;