Core pipeline creation, configuration, and policy management for building HTTP request processing pipelines with middleware-style policies.
Create pipelines with default policies or start with empty pipelines for custom configurations.
/**
* Create a new pipeline with a default set of customizable policies
* @param options - Options to configure a custom pipeline
* @returns Pipeline instance with default policies applied
*/
function createPipelineFromOptions(options: InternalPipelineOptions): Pipeline;
/**
* Creates a totally empty pipeline
* Useful for testing or creating a custom one
* @returns Empty pipeline instance
*/
function createEmptyPipeline(): Pipeline;
interface InternalPipelineOptions extends PipelineOptions {
loggingOptions?: LogPolicyOptions;
}
interface PipelineOptions {
retryOptions?: PipelineRetryOptions;
proxyOptions?: ProxySettings;
agent?: Agent;
tlsOptions?: TlsSettings;
redirectOptions?: RedirectPolicyOptions;
userAgentOptions?: UserAgentPolicyOptions;
telemetryOptions?: TelemetryOptions;
}
interface TelemetryOptions {
clientRequestIdHeaderName?: string;
}Usage Examples:
import { createPipelineFromOptions, createEmptyPipeline } from "@azure/core-rest-pipeline";
// Create pipeline with default policies
const pipeline = createPipelineFromOptions({
retryOptions: { maxRetries: 5, retryDelayInMs: 1000 },
userAgentOptions: { userAgentPrefix: "MySDK/2.0.0" },
loggingOptions: { logger: console }
});
// Create empty pipeline for custom configuration
const customPipeline = createEmptyPipeline();Main pipeline interface for managing policies and executing HTTP requests.
/**
* Represents a pipeline for making HTTP requests to a URL.
* Pipelines can have multiple policies to manage manipulating each request
* before and after it is made to the server.
*/
interface Pipeline {
/**
* Add a new policy to the pipeline
* @param policy - A policy that manipulates a request
* @param options - A set of options for when the policy should run
*/
addPolicy(policy: PipelinePolicy, options?: AddPolicyOptions): void;
/**
* Remove a policy from the pipeline
* @param options - Options that let you specify which policies to remove
*/
removePolicy(options: { name?: string; phase?: PipelinePhase }): PipelinePolicy[];
/**
* Uses the pipeline to make a HTTP request
* @param httpClient - The HttpClient that actually performs the request
* @param request - The request to be made
*/
sendRequest(httpClient: HttpClient, request: PipelineRequest): Promise<PipelineResponse>;
/**
* Returns the current set of policies in the pipeline in the order in which
* they will be applied to the request
*/
getOrderedPolicies(): PipelinePolicy[];
/**
* Duplicates this pipeline to allow for modifying an existing one without mutating it
*/
clone(): Pipeline;
}Usage Examples:
import {
createEmptyPipeline,
userAgentPolicy,
logPolicy,
type PipelinePolicy
} from "@azure/core-rest-pipeline";
const pipeline = createEmptyPipeline();
// Add policies with ordering
pipeline.addPolicy(userAgentPolicy({ userAgentPrefix: "MyApp/1.0" }));
pipeline.addPolicy(logPolicy({ logger: console }), {
afterPolicies: ["userAgentPolicy"]
});
// Remove policies
const removedPolicies = pipeline.removePolicy({ name: "logPolicy" });
// Clone pipeline for modification
const clonedPipeline = pipeline.clone();
// Get ordered policy list
const policies = pipeline.getOrderedPolicies();
console.log(policies.map(p => p.name));Interface for creating custom pipeline policies with lifecycle management.
/**
* 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.
*/
interface PipelinePolicy {
/**
* The policy name. Must be a unique string in the pipeline.
*/
name: string;
/**
* The main method to implement that manipulates a request/response
* @param request - The request being performed
* @param next - The next policy in the pipeline. Must be called to continue the pipeline
*/
sendRequest(request: PipelineRequest, next: SendRequest): Promise<PipelineResponse>;
}
/**
* Options when adding a policy to the pipeline.
* Used to express dependencies on other policies.
*/
interface AddPolicyOptions {
/**
* Policies that this policy must come before
*/
beforePolicies?: string[];
/**
* Policies that this policy must come after
*/
afterPolicies?: string[];
/**
* The phase that this policy must come after
*/
afterPhase?: PipelinePhase;
/**
* The phase this policy belongs to
*/
phase?: 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
*/
type PipelinePhase = "Deserialize" | "Serialize" | "Retry" | "Sign";Usage Examples:
import { type PipelinePolicy, type PipelineRequest, type SendRequest } from "@azure/core-rest-pipeline";
// Create custom policy
const customHeaderPolicy: PipelinePolicy = {
name: "customHeaderPolicy",
async sendRequest(request: PipelineRequest, next: SendRequest) {
// Modify request before sending
request.headers.set("X-Custom-Header", "MyValue");
// Call next policy in pipeline
const response = await next(request);
// Modify response after receiving
console.log(`Response status: ${response.status}`);
return response;
}
};
// Add to pipeline with specific ordering
pipeline.addPolicy(customHeaderPolicy, {
phase: "Serialize",
beforePolicies: ["userAgentPolicy"]
});Policies are executed in the following order:
Within each phase, policies are ordered based on beforePolicies and afterPolicies dependencies.