Share via


@typespec/ts-http-runtime package

Classes

AbortError

This error is thrown when an asynchronous operation has been aborted. Check for this error by testing the name that the name property of the error matches "AbortError".

Example

import { AbortError } from "@typespec/ts-http-runtime";

async function doAsyncWork(options: { abortSignal: AbortSignal }): Promise<void> {
  if (options.abortSignal.aborted) {
    throw new AbortError();
  }

  // do async work
}

const controller = new AbortController();
controller.abort();

try {
  doAsyncWork({ abortSignal: controller.signal });
} catch (e) {
  if (e instanceof Error && e.name === "AbortError") {
    // handle abort error here.
  }
}
RestError

A custom error type for failed pipeline requests.

Interfaces

AddPolicyOptions

Options when adding a policy to the pipeline. Used to express dependencies on other policies.

AdditionalPolicyConfig

Used to configure additional policies added to the pipeline at construction.

Agent

An interface compatible with NodeJS's http.Agent. We want to avoid publicly re-exporting the actual interface, since it might vary across runtime versions.

ApiKeyAuthScheme

Represents API Key authentication scheme. API Key authentication requires a key to be provided with each request. The key can be provided in different locations: query parameter, header, or cookie.

ApiKeyCredential

Credential for API Key authentication. Provides an API key that will be used in the request headers.

AuthorizationCodeFlow

Represents OAuth2 Authorization Code flow configuration.

BasicAuthScheme

Represents HTTP Basic authentication scheme. Basic authentication scheme requires a username and password to be provided with each request. The credentials are encoded using Base64 and included in the Authorization header.

BasicCredential

Credential for HTTP Basic authentication. Provides username and password for basic authentication headers.

BearerAuthScheme

Represents HTTP Bearer authentication scheme. Bearer authentication scheme requires a bearer token to be provided with each request. The token is included in the Authorization header with the "Bearer" prefix.

BearerTokenCredential

Credential for Bearer token authentication.

BodyPart

A part of the request body in a multipart request.

Client

Shape of a Rest Level Client

ClientCredentialsFlow

Represents OAuth2 Client Credentials flow configuration.

Debugger
FullOperationResponse

Wrapper object for http request and response. Deserialized object is stored in the parsedBody property when the response body is received in JSON.

GetBearerTokenOptions

Options used when creating and sending get bearer token requests for this operation.

GetOAuth2TokenOptions

Options used when creating and sending get OAuth 2 requests for this operation.

HttpClient

The required interface for a client that makes HTTP requests on behalf of a pipeline.

HttpHeaders

Represents a set of HTTP headers on a request/response. Header names are treated as case insensitive.

ImplicitFlow

Represents OAuth2 Implicit flow configuration.

KeyObject

An interface compatible with NodeJS's tls.KeyObject. We want to avoid publicly re-exporting the actual interface, since it might vary across runtime versions.

LogPolicyOptions

Options to configure the logPolicy.

MultipartRequestBody

A request body consisting of multiple parts.

NoAuthAuthScheme

Represents an endpoint or operation that requires no authentication.

OAuth2AuthScheme

Represents OAuth2 authentication scheme with specified flows

OAuth2TokenCredential

Credential for OAuth2 authentication flows.

OperationOptions

The base options type for all operations.

OperationRequestOptions

Options used when creating and sending HTTP requests for this operation.

PasswordFlow

Represents OAuth2 Password flow configuration.

PathParameterWithOptions

An object that can be passed as a path parameter, allowing for additional options to be set relating to how the parameter is encoded.

Pipeline

Represents a pipeline for making a HTTP request to a URL. Pipelines can have multiple policies to manage manipulating each request before and after it is made to the server.

PipelineOptions

Defines options that are used to configure the HTTP pipeline for an SDK client.

PipelinePolicy

A pipeline policy manipulates a request as it travels through the pipeline. It is conceptually a middleware that is allowed to modify the request before it is made as well as the response when it is received.

PipelineRequest

Metadata about a request being made by the pipeline.

PipelineRequestOptions

Settings to initialize a request. Almost equivalent to Partial, but url is mandatory.

PipelineResponse

Metadata about a response received by the pipeline.

PipelineRetryOptions

Options that control how to retry failed requests.

ProxySettings

Options to configure a proxy for outgoing requests (Node.js only).

PxfObject

An interface compatible with NodeJS's tls.PxfObject. We want to avoid publicly re-exporting the actual interface, since it might vary across runtime versions.

RedirectPolicyOptions

Options for how redirect responses are handled.

ResourceMethods

Defines the methods that can be called on a resource

RestErrorOptions

The options supported by RestError.

TelemetryOptions

Defines options that are used to configure common telemetry and tracing info

TlsSettings

Represents a certificate for TLS authentication.

UserAgentPolicyOptions

Options for adding user agent details to outgoing requests.

Type Aliases

AuthScheme

Union type of all supported authentication schemes

ClientCredential

Union type of all supported authentication credentials.

ClientOptions

General options that a Rest Level Client can take

EncodingType

The supported character encoding type

FormDataMap

A simple object that provides form data, as if from a browser form.

FormDataValue

Each form data entry can be a string, Blob, or a File. If you wish to pass a file with a name but do not have access to the File class, you can use the createFile helper to create one.

HttpBrowserStreamResponse

Http Response which body is a NodeJS stream object

HttpMethods

Supported HTTP methods to use when making requests.

HttpNodeStreamResponse

Http Response which body is a NodeJS stream object

HttpResponse

Represents the shape of an HttpResponse

OAuth2Flow

Union type of all supported OAuth2 flows

PathParameters

Helper type used to detect parameters in a path template text surrounded by {} will be considered a path parameter

PathUnchecked

Defines the signature for pathUnchecked.

PathUncheckedResponse

Type to use with pathUnchecked, overrides the body type to any to allow flexibility

PipelinePhase

Policies are executed in phases. The execution order is:

  1. Serialize Phase
  2. Policies not in a phase
  3. Deserialize Phase
  4. Retry Phase
  5. Sign Phase
RawHttpHeaders

A HttpHeaders collection represented as a simple JSON object.

RawHttpHeadersInput

A HttpHeaders collection for input, represented as a simple JSON object.

RawResponseCallback

A function to be called each time a response is received from the server while performing the requested operation. May be called multiple times.

RequestBodyType

Types of bodies supported on the request. NodeJS.ReadableStream and () => NodeJS.ReadableStream is Node only. Blob, ReadableStream, and () => ReadableStream are browser only.

RequestParameters

Shape of the default request parameters, this may be overridden by the specific request types to provide strong types

SendRequest

A simple interface for making a pipeline request and receiving a response.

StreamableMethod

Defines the type for a method that supports getting the response body as a raw stream

TransferProgressEvent

Fired in response to upload or download progress.

TypeSpecRuntimeClientLogger

A TypeSpecRuntimeClientLogger is a function that can log to an appropriate severity level.

TypeSpecRuntimeLogLevel

The log levels supported by the logger. The log levels in order of most verbose to least verbose are:

  • verbose
  • info
  • warning
  • error

Functions

TypeSpecRuntimeLogger(any[])

Immediately enables logging at the specified log level. If no level is specified, logging is disabled.

createDefaultHttpClient()

Create the correct HttpClient for the current environment.

createEmptyPipeline()

Creates a totally empty pipeline. Useful for testing or creating a custom one.

createHttpHeaders(RawHttpHeadersInput)

Creates an object that satisfies the HttpHeaders interface.

createPipelineRequest(PipelineRequestOptions)

Creates a new pipeline request with the given options. This method is to allow for the easy setting of default values and not required.

createRestError(PathUncheckedResponse)

Creates a rest error from a PathUnchecked response

createRestError(string, PathUncheckedResponse)

Creates a rest error from an error message and a PathUnchecked response

getClient(string, ClientOptions)

Creates a client with a default pipeline

getLogLevel()

Retrieves the currently specified log level.

isRestError(unknown)

Typeguard for RestError

operationOptionsToRequestParameters(OperationOptions)

Helper function to convert OperationOptions to RequestParameters

setLogLevel(TypeSpecRuntimeLogLevel)

Retrieves the currently specified log level.

stringToUint8Array(string, EncodingType)

The helper that transforms string to specific character encoded bytes array.

uint8ArrayToString(Uint8Array, EncodingType)

The helper that transforms bytes with specific character encoding into string

Function Details

TypeSpecRuntimeLogger(any[])

Immediately enables logging at the specified log level. If no level is specified, logging is disabled.

function TypeSpecRuntimeLogger(args: any[])

Parameters

args

any[]

createDefaultHttpClient()

Create the correct HttpClient for the current environment.

function createDefaultHttpClient(): HttpClient

Returns

createEmptyPipeline()

Creates a totally empty pipeline. Useful for testing or creating a custom one.

function createEmptyPipeline(): Pipeline

Returns

createHttpHeaders(RawHttpHeadersInput)

Creates an object that satisfies the HttpHeaders interface.

function createHttpHeaders(rawHeaders?: RawHttpHeadersInput): HttpHeaders

Parameters

rawHeaders
RawHttpHeadersInput

A simple object representing initial headers

Returns

createPipelineRequest(PipelineRequestOptions)

Creates a new pipeline request with the given options. This method is to allow for the easy setting of default values and not required.

function createPipelineRequest(options: PipelineRequestOptions): PipelineRequest

Parameters

options
PipelineRequestOptions

The options to create the request with.

Returns

createRestError(PathUncheckedResponse)

Creates a rest error from a PathUnchecked response

function createRestError(response: PathUncheckedResponse): RestError

Parameters

Returns

createRestError(string, PathUncheckedResponse)

Creates a rest error from an error message and a PathUnchecked response

function createRestError(message: string, response: PathUncheckedResponse): RestError

Parameters

message

string

Returns

getClient(string, ClientOptions)

Creates a client with a default pipeline

function getClient(endpoint: string, clientOptions?: ClientOptions): Client

Parameters

endpoint

string

Base endpoint for the client

clientOptions
ClientOptions

Returns

getLogLevel()

Retrieves the currently specified log level.

function getLogLevel(): TypeSpecRuntimeLogLevel | undefined

Returns

isRestError(unknown)

Typeguard for RestError

function isRestError(e: unknown): e

Parameters

e

unknown

Something caught by a catch clause.

Returns

e

operationOptionsToRequestParameters(OperationOptions)

Helper function to convert OperationOptions to RequestParameters

function operationOptionsToRequestParameters(options: OperationOptions): RequestParameters

Parameters

options
OperationOptions

the options that are used by Modular layer to send the request

Returns

the result of the conversion in RequestParameters of RLC layer

setLogLevel(TypeSpecRuntimeLogLevel)

Retrieves the currently specified log level.

function setLogLevel(logLevel?: TypeSpecRuntimeLogLevel)

Parameters

stringToUint8Array(string, EncodingType)

The helper that transforms string to specific character encoded bytes array.

function stringToUint8Array(value: string, format: EncodingType): Uint8Array

Parameters

value

string

the string to be converted

format
EncodingType

the format we use to decode the value

Returns

Uint8Array

a uint8array

uint8ArrayToString(Uint8Array, EncodingType)

The helper that transforms bytes with specific character encoding into string

function uint8ArrayToString(bytes: Uint8Array, format: EncodingType): string

Parameters

bytes

Uint8Array

the uint8array bytes

format
EncodingType

the format we use to encode the byte

Returns

string

a string of the encoded string