1.17.0 • Published 15 days ago

@azure/core-rest-pipeline v1.17.0

Weekly downloads
-
License
MIT
Repository
-
Last release
15 days ago

Azure Core HTTP client library for JavaScript (Experimental)

This is the core HTTP pipeline for Azure SDK JavaScript libraries which work in the browser and Node.js. This library is primarily intended to be used in code generated by AutoRest and autorest.typescript.

Getting started

Requirements

Installation

This package is primarily used in generated code and not meant to be consumed directly by end users.

Key concepts

PipelineRequest

A PipelineRequest describes all the information necessary to make a request to an HTTP REST endpoint.

PipelineResponse

A PipelineResponse describes the HTTP response (body, headers, and status code) from a REST endpoint that was returned after making an HTTP request.

SendRequest

A SendRequest method is a method that given a PipelineRequest can asynchronously return a PipelineResponse.

export type SendRequest = (request: PipelineRequest) => Promise<PipelineResponse>;

HttpClient

An HttpClient is any object that satisfies the following interface to implement a SendRequest method:

export interface HttpClient {
  /**
   * The method that makes the request and returns a response.
   */
  sendRequest: SendRequest;
}

HttpClients are expected to actually make the HTTP request to a server endpoint, using some platform-specific mechanism for doing so.

Pipeline Policies

A PipelinePolicy is a simple object that implements the following interface:

export 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>;
}

It is similar in shape to HttpClient, but includes a policy name as well as a slightly modified SendRequest signature that allows it to conditionally call the next policy in the pipeline.

One can view the role of policies as that of middleware, a concept that is familiar to NodeJS developers who have worked with frameworks such as Express.

The sendRequest implementation can both transform the outgoing request as well as the incoming response:

const customPolicy = {
  name: "My wonderful policy",
  async sendRequest(request: PipelineRequest, next: SendRequest): Promise<PipelineResponse> {
    // Change the outgoing request by adding a new header
    request.headers.set("X-Cool-Header", 42);
    const result = await next(request);
    if (response.status === 403) {
      // Do something special if this policy sees Forbidden
    }
    return result;
  }
};

Most policies only concern themselves with either the request or the response, but there are some exceptions such as the LogPolicy which logs information from each.

Pipelines

A Pipeline is an object that manages a set of PipelinePolicy objects. Its main function is to ensure that policies are executed in a consistent and predictable order.

You can think of policies being applied like a stack (first-in/last-out.) The first PipelinePolicy is able to modify the PipelineRequest before any other policies, and it is also the last to modify the PipelineResponse, making it the closest to the caller. The final policy is the last able to modify the outgoing request, and the first to handle the response, making it the closest to the network.

A Pipeline satisfies the following interface:

export interface Pipeline {
  addPolicy(policy: PipelinePolicy, options?: AddPolicyOptions): void;
  removePolicy(options: { name?: string; phase?: PipelinePhase }): PipelinePolicy[];
  sendRequest(httpClient: HttpClient, request: PipelineRequest): Promise<PipelineResponse>;
  getOrderedPolicies(): PipelinePolicy[];
  clone(): Pipeline;
}

As you can see it allows for policies to be added or removed and it is loosely coupled with HttpClient to perform the real request to the server endpoint.

One important concept for Pipelines is that they group policies into ordered phases:

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

Phases occur in the above order, with serialization policies being applied first and retry policies being applied last. Most custom policies fall into the second bucket and are not given a phase name.

When adding a policy to the pipeline you can specify not only what phase a policy is in, but also if it has any dependencies:

export interface AddPolicyOptions {
  beforePolicies?: string[];
  afterPolicies?: string[];
  afterPhase?: PipelinePhase;
  phase?: PipelinePhase;
}

beforePolicies are policies that the new policy must execute before and afterPolicies are policies that the new policy must happen after. Similarly, afterPhase means the policy must only execute after the specified phase has occurred.

This syntax allows custom policy authors to express any necessary relationships between their own policies and the built-in policies provided by @azure/core-rest-pipeline when creating a pipeline using createPipelineFromOptions.

Implementers are also able to remove policies by name or phase, in the case that they wish to modify an existing Pipeline without having to create a new one using createEmptyPipeline. The clone method is particularly useful when recreating a Pipeline without modifying the original.

After all other constraints have been satisfied, policies are applied in the order which they were added.

Examples

Examples can be found in the samples folder.

Next steps

You can build and run the tests locally by executing rushx test. Explore the test folder to see advanced usage and behavior of the public classes.

Troubleshooting

If you run into issues while using this library, please feel free to file an issue.

Contributing

If you'd like to contribute to this library, please read the contributing guide to learn more about how to build and test the code.

Impressions

@everything-registry/sub-chunk-107teamcloudspec-box-tms-synclims-server-openapiirontask-devtestims-clienthelen-common-infrasstchurazure-storage-blobskyeye-svc-common-utils@aipmorg/chat@autorest/typescript@affinda/affinda@alexok/arm-servicefabric-managedcluster@azure-tools/communication-alpha-ids@azure-tools/communication-domain-verification@azure-tools/communication-recipient-verification@azure-tools/communication-short-codes@azure-tools/communication-tiering@azure-tools/communication-toll-free-verification@azure-tools/test-recorder@azure-tools/test-utils@azure-tools/typespec-client-generator-cli@azure-tools/test-perf@azure/cosmos@azure/data-tables@azure/digital-twins-core@azure-rest/arm-servicefabric@azure-rest/batch@azure-rest/communication-job-router@azure-rest/communication-messages@azure-rest/confidential-ledger@azure-rest/core-client@azure-rest/maps-route@azure-rest/maps-search@azure-rest/openai@azure-rest/purview-account@azure-rest/purview-administration@azure-rest/purview-catalog@azure-rest/purview-datamap@azure-rest/purview-scanning@azure-rest/purview-sharing@azure-rest/purview-workflow@azure-rest/core-client-paging@azure-rest/defender-easm@azure-rest/developer-devcenter@azure-rest/health-deidentification@azure-rest/health-insights-cancerprofiling@azure-rest/health-insights-clinicalmatching@azure-rest/health-insights-radiologyinsights@azure-rest/arm-appservice@azure-rest/arm-compute@azure-rest/arm-containerservice@azure-rest/arm-network@azure-rest/iot-device-update@azure-rest/load-testing@azure-rest/maps-geolocation@azure-rest/maps-render@azure-rest/synapse-access-control@azure-rest/ai-translation-document@azure-rest/ai-translation-text@azure-rest/ai-vision-face@azure-rest/ai-vision-image-analysis@azure/arm-imagebuilder@azure/arm-informaticadatamanagement@azure/arm-iotcentral@azure/arm-iotfirmwaredefense@azure/arm-elastic@azure/arm-elasticsan@azure/arm-eventgrid@azure/arm-eventhub@azure/arm-healthbot@azure/arm-healthcareapis@azure/arm-healthdataaiservices@azure/arm-hybridcompute@azure/arm-hybridconnectivity@azure/arm-eventhub-profile-2020-09-01-hybrid@azure/arm-extendedlocation@azure/arm-fabric@azure/arm-features@azure/arm-fluidrelay@azure/arm-frontdoor@azure/arm-graphservices@azure/arm-guestconfiguration@azure/arm-hanaonazure@azure/arm-hybridcontainerservice@azure/arm-hybridkubernetes@azure/arm-hybridnetwork@azure/arm-hardwaresecuritymodules@azure/arm-hdinsight@azure/arm-hdinsightcontainers@azure/arm-dynatrace@azure/arm-edgezones@azure/arm-education@azure/arm-domainservices@azure/container-registry@azure/core-http-compat@azure/core-client@azure/template@azure/template-dpg@azure/video-analyzer-edge
1.17.0

2 months ago

1.16.3

4 months ago

1.16.2

4 months ago

1.16.1

5 months ago

1.16.0

7 months ago

1.15.2

7 months ago

1.15.1

8 months ago

1.15.0

8 months ago

1.14.0

10 months ago

1.13.0

11 months ago

1.12.2

1 year ago

1.12.1

1 year ago

1.12.0

1 year ago

1.11.0

1 year ago

1.10.3

2 years ago

1.10.2

2 years ago

1.10.1

2 years ago

1.10.0

2 years ago

1.9.2

2 years ago

1.9.1

2 years ago

1.9.0

2 years ago

1.8.1

3 years ago

1.8.0

3 years ago

1.7.0

3 years ago

1.6.0

3 years ago

1.4.0

3 years ago

1.5.0

3 years ago

1.3.2

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.0

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.4

3 years ago

1.1.0-beta.3

3 years ago

1.1.0-beta.2

3 years ago

1.1.0-beta.1

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

1.0.0-beta.2

4 years ago