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
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
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.
npm install @smithy/protocol-httpimport {
Field,
Fields,
HttpRequest,
HttpResponse,
isValidHostname,
HttpHandler,
HttpHandlerUserInput
} from "@smithy/protocol-http";For CommonJS:
const {
Field,
Fields,
HttpRequest,
HttpResponse,
isValidHostname
} = require("@smithy/protocol-http");import { Field, Fields, HttpRequest, HttpResponse, isValidHostname } from "@smithy/protocol-http";
import { FieldPosition } from "@smithy/types";
// Create HTTP fields (headers/trailers)
const headerField = new Field({
name: "Content-Type",
kind: FieldPosition.HEADER,
values: ["application/json"]
});
// Manage field collections
const fields = new Fields({ fields: [headerField] });
const contentType = fields.getField("content-type"); // Case insensitive
// Create HTTP requests
const request = new HttpRequest({
method: "POST",
hostname: "api.example.com",
path: "/users",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Alice" })
});
// Create HTTP responses
const response = new HttpResponse({
statusCode: 200,
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ id: 123, name: "Alice" })
});
// Validate hostnames
if (isValidHostname("api.example.com")) {
console.log("Valid hostname");
}@smithy/protocol-http is built around several key components:
Field and Fields classes for managing HTTP metadata (headers, trailers) with case-insensitive accessHttpRequest and HttpResponse classes providing concrete implementations with cloning capabilitiesHttpHandler type and configuration for pluggable HTTP client implementationsHTTP field handling for headers and trailers with case-insensitive operations and value management.
class Field {
constructor(options: { name: string; kind?: FieldPosition; values?: string[] });
add(value: string): void;
set(values: string[]): void;
remove(value: string): void;
toString(): string;
get(): string[];
}
class Fields {
constructor(options: { fields?: Field[]; encoding?: string });
setField(field: Field): void;
getField(name: string): Field | undefined;
removeField(name: string): void;
getByType(kind: FieldPosition): Field[];
}HTTP message implementations with cloning support and type validation.
class HttpRequest {
constructor(options: HttpRequestOptions);
static clone(request: IHttpRequest): HttpRequest;
static isInstance(request: unknown): request is HttpRequest;
clone(): HttpRequest; // @deprecated
}
class HttpResponse {
constructor(options: { statusCode: number; reason?: string; headers?: HeaderBag; body?: any });
static isInstance(response: unknown): response is HttpResponse;
}HTTP handler interfaces and user input types for configurable request handling.
/**
* @internal
*/
type HttpHandler<HttpHandlerConfig extends object = {}> = RequestHandler<
HttpRequest,
HttpResponse,
HttpHandlerOptions
> & {
updateHttpClientConfig(key: keyof HttpHandlerConfig, value: HttpHandlerConfig[typeof key]): void;
httpHandlerConfigs(): HttpHandlerConfig;
};
type HttpHandlerUserInput =
| HttpHandler
| NodeHttpHandlerOptions
| FetchHttpHandlerOptions
| Record<string, unknown>;Hostname format validation utility for URL construction and validation.
function isValidHostname(hostname: string): boolean;Internal extension configuration system for HTTP handler customization.
interface HttpHandlerExtensionConfiguration<HandlerConfig extends object = {}> {
setHttpHandler(handler: HttpHandler<HandlerConfig>): void;
httpHandler(): HttpHandler<HandlerConfig>;
updateHttpClientConfig(key: keyof HandlerConfig, value: HandlerConfig[typeof key]): void;
httpHandlerConfigs(): HandlerConfig;
}Type aliases maintaining backward compatibility with existing code. These types are deprecated and users should migrate to @smithy/types.
/** @deprecated Use FieldOptions from @smithy/types instead */
type FieldOptions = __FieldOptions;
/** @deprecated Use FieldPosition from @smithy/types instead */
type FieldPosition = __FieldPosition;
/** @deprecated Use HeaderBag from @smithy/types instead */
type HeaderBag = __HeaderBag;
/** @deprecated Use HttpMessage from @smithy/types instead */
type HttpMessage = __HttpMessage;
/** @deprecated Use HttpHandlerOptions from @smithy/types instead */
type HttpHandlerOptions = __HttpHandlerOptions;