Protocol HTTP utilities for Smithy TypeScript clients and servers, providing HTTP request/response handling, field management, hostname validation, and extension configuration for HTTP-based protocol implementations
—
HTTP field handling for headers and trailers with case-insensitive operations and value management. Fields represent name-value pairs transmitted in HTTP messages with support for multiple values and different placement positions.
Represents a single HTTP field (header or trailer) with name-value pairs. All field names are case insensitive and support multiple values.
/**
* A name-value pair representing a single field transmitted in an HTTP Request or Response.
* The kind dictates metadata placement within an HTTP message.
* All field names are case insensitive.
*/
class Field {
readonly name: string;
readonly kind: FieldPosition;
values: string[];
constructor(options: FieldOptions);
add(value: string): void;
set(values: string[]): void;
remove(value: string): void;
toString(): string;
get(): string[];
}
interface FieldOptions {
name: string;
kind?: FieldPosition;
values?: string[];
}Usage Examples:
import { Field } from "@smithy/protocol-http";
import { FieldPosition } from "@smithy/types";
// Create a header field
const contentType = new Field({
name: "Content-Type",
kind: FieldPosition.HEADER,
values: ["application/json"]
});
// Add additional values
contentType.add("charset=utf-8");
console.log(contentType.toString()); // "application/json, charset=utf-8"
// Replace all values
contentType.set(["text/html"]);
console.log(contentType.get()); // ["text/html"]
// Remove specific values
contentType.add("application/json");
contentType.remove("text/html");
console.log(contentType.get()); // ["application/json"]Appends a value to the field's value list.
/**
* Appends a value to the field.
* @param value - The value to append
*/
add(value: string): void;Overwrites existing field values with new values.
/**
* Overwrite existing field values.
* @param values - The new field values
*/
set(values: string[]): void;Removes all matching entries from the value list.
/**
* Remove all matching entries from list.
* @param value - Value to remove
*/
remove(value: string): void;Gets comma-delimited string representation of field values. Values with spaces or commas are automatically double-quoted.
/**
* Get comma-delimited string.
* @returns String representation of Field
*/
toString(): string;Returns the field values as an array of strings.
/**
* Get string values as a list
* @returns Values in Field as a list
*/
get(): string[];Collection of Field entries mapped by name with case-insensitive access and filtering capabilities.
/**
* Collection of Field entries mapped by name.
*/
class Fields {
constructor(options: FieldsOptions);
setField(field: Field): void;
getField(name: string): Field | undefined;
removeField(name: string): void;
getByType(kind: FieldPosition): Field[];
}
interface FieldsOptions {
fields?: Field[];
encoding?: string;
}Usage Examples:
import { Field, Fields } from "@smithy/protocol-http";
import { FieldPosition } from "@smithy/types";
// Create fields collection
const headerField = new Field({
name: "Accept",
kind: FieldPosition.HEADER,
values: ["application/json"]
});
const trailerField = new Field({
name: "Status",
kind: FieldPosition.TRAILER,
values: ["complete"]
});
const fields = new Fields({
fields: [headerField, trailerField],
encoding: "utf-8"
});
// Access fields (case insensitive)
const accept = fields.getField("accept");
const acceptCaps = fields.getField("ACCEPT"); // Same field
// Get fields by type
const headers = fields.getByType(FieldPosition.HEADER);
const trailers = fields.getByType(FieldPosition.TRAILER);
// Modify collection
fields.removeField("status");Sets or updates a field entry in the collection using the field's name as the key.
/**
* Set entry for a Field name. The name attribute will be used to key the collection.
* @param field - The Field to set
*/
setField(field: Field): void;Retrieves a field entry by name with case-insensitive matching.
/**
* Retrieve Field entry by name.
* @param name - The name of the Field entry to retrieve
* @returns The Field if it exists
*/
getField(name: string): Field | undefined;Deletes a field entry from the collection by name.
/**
* Delete entry from collection.
* @param name - Name of the entry to delete
*/
removeField(name: string): void;Helper function for retrieving fields of a specific type (headers or trailers).
/**
* Helper function for retrieving specific types of fields.
* Used to grab all headers or all trailers.
* @param kind - FieldPosition of entries to retrieve
* @returns The Field entries with the specified FieldPosition
*/
getByType(kind: FieldPosition): Field[];interface FieldOptions {
name: string;
kind?: FieldPosition;
values?: string[];
}
interface FieldsOptions {
fields?: Field[];
encoding?: string;
}Install with Tessl CLI
npx tessl i tessl/npm-smithy--protocol-http