CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-azure--core-client

Core library for interfacing with AutoRest generated code

Pending
Overview
Eval results
Files

pipeline.mddocs/

Pipeline Management

HTTP pipeline creation and configuration for request/response processing. The pipeline system provides a customizable chain of policies that handle authentication, serialization, deserialization, retry logic, and other cross-cutting concerns.

Capabilities

Pipeline Creation

Creates a customized HTTP pipeline with built-in policies for Azure service communication, including serialization, deserialization, and optional bearer token authentication.

/**
 * Creates a new HTTP pipeline for use with ServiceClient
 * Automatically adds serialization and deserialization policies
 * Optionally adds bearer token authentication policy
 * @param options - Configuration options for the pipeline
 * @returns Configured Pipeline instance
 */
function createClientPipeline(options?: InternalClientPipelineOptions): Pipeline;

Internal Pipeline Options

Configuration interface for creating client pipelines with authentication, serialization, and deserialization options.

/**
 * Options for creating a Pipeline to use with ServiceClient
 * Extends base pipeline options with client-specific configuration
 */
interface InternalClientPipelineOptions extends InternalPipelineOptions {
  /**
   * Options to customize bearerTokenAuthenticationPolicy
   */
  credentialOptions?: {
    /** OAuth scopes for token requests */
    credentialScopes: string | string[];
    /** Authentication credential from @azure/core-auth */
    credential: TokenCredential;
  };

  /**
   * Options to customize deserializationPolicy
   */
  deserializationOptions?: DeserializationPolicyOptions;

  /**
   * Options to customize serializationPolicy
   */
  serializationOptions?: SerializationPolicyOptions;
}

Usage Examples

Basic Pipeline Creation

import { createClientPipeline, ServiceClient } from "@azure/core-client";

// Create a basic pipeline with default serialization/deserialization
const pipeline = createClientPipeline();

const client = new ServiceClient({
  endpoint: "https://myservice.azure.com",
  pipeline
});

Pipeline with Authentication

import { createClientPipeline } from "@azure/core-client";
import { DefaultAzureCredential } from "@azure/identity";

// Create pipeline with OAuth bearer token authentication
const credential = new DefaultAzureCredential();
const pipeline = createClientPipeline({
  credentialOptions: {
    credential,
    credentialScopes: ["https://management.azure.com/.default"]
  }
});

Pipeline with Custom Serialization

import { createClientPipeline } from "@azure/core-client";

// Create pipeline with custom XML handling
const pipeline = createClientPipeline({
  serializationOptions: {
    stringifyXML: (obj) => customXMLStringifier(obj),
    serializerOptions: {
      xml: {
        rootName: "customRoot",
        includeRoot: true
      }
    }
  },
  deserializationOptions: {
    parseXML: async (str) => await customXMLParser(str),
    expectedContentTypes: {
      xml: [/application\/xml/, /text\/xml/]
    }
  }
});

Pipeline with Additional Policies

import { 
  createClientPipeline, 
  ServiceClient 
} from "@azure/core-client";
import { 
  logPolicy, 
  retryPolicy,
  userAgentPolicy 
} from "@azure/core-rest-pipeline";

// Create base pipeline
const pipeline = createClientPipeline({
  credentialOptions: {
    credential: new DefaultAzureCredential(),
    credentialScopes: ["https://vault.azure.net/.default"]
  }
});

// Add custom policies
pipeline.addPolicy(userAgentPolicy({ 
  userAgentPrefix: "MyApp/1.0.0" 
}));

pipeline.addPolicy(logPolicy({ 
  allowedHeaderNames: ["x-ms-request-id", "x-ms-client-request-id"],
  allowedQueryParameters: ["api-version"]
}));

pipeline.addPolicy(retryPolicy({
  maxRetries: 5,
  retryDelayInMs: 1000
}));

const client = new ServiceClient({
  endpoint: "https://mykeyvault.vault.azure.net",
  pipeline
});

Pipeline Policy Ordering

The pipeline executes policies in a specific order to ensure proper request/response processing:

// Pipeline policy execution order:
// 1. Request policies (user agent, custom headers, etc.)
// 2. Authentication policy (if configured)
// 3. Serialization policy (converts objects to HTTP payload)
// 4. HTTP transport policy (sends the request)
// 5. Deserialization policy (converts HTTP response to objects)
// 6. Response policies (logging, custom processing, etc.)

const pipeline = createClientPipeline({
  credentialOptions: {
    credential,
    credentialScopes: ["https://management.azure.com/.default"]
  }
});

// Policies are automatically added in the correct order:
// - bearerTokenAuthenticationPolicy (from credentialOptions)
// - serializationPolicy (automatic)
// - deserializationPolicy (automatic)

Policy Configuration Options

Serialization Policy Options

interface SerializationPolicyOptions {
  /**
   * Custom XML stringifier function
   */
  stringifyXML?: (obj: unknown, opts?: any) => string;

  /**
   * Serializer configuration options
   */
  serializerOptions?: SerializerOptions;
}

Deserialization Policy Options

interface DeserializationPolicyOptions {
  /**
   * Expected content types for automatic deserialization
   */
  expectedContentTypes?: DeserializationContentTypes;

  /**
   * Custom XML parser function
   */
  parseXML?: (str: string, opts?: any) => Promise<any>;

  /**
   * Serializer configuration options
   */
  serializerOptions?: SerializerOptions;
}

interface DeserializationContentTypes {
  /** RegExp patterns for JSON content types */
  json?: RegExp[];
  /** RegExp patterns for XML content types */
  xml?: RegExp[];
}

Integration with ServiceClient

The pipeline integrates seamlessly with ServiceClient for complete request/response handling:

import { ServiceClient, createClientPipeline } from "@azure/core-client";

class MyServiceClient extends ServiceClient {
  constructor(endpoint: string, credential: TokenCredential) {
    // Create pipeline with authentication and custom options
    const pipeline = createClientPipeline({
      credentialOptions: {
        credential,
        credentialScopes: [`${endpoint}/.default`]
      },
      serializationOptions: {
        serializerOptions: {
          xml: { rootName: "request" }
        }
      }
    });

    super({ endpoint, pipeline });
  }
}

Error Handling

Pipeline policies can intercept and handle errors at different stages:

import { 
  createClientPipeline, 
  PipelinePolicy 
} from "@azure/core-client";

// Custom error handling policy
const errorHandlingPolicy: PipelinePolicy = {
  name: "errorHandlingPolicy",
  sendRequest: async (request, next) => {
    try {
      const response = await next(request);
      return response;
    } catch (error) {
      // Handle specific error conditions
      if (error.statusCode === 429) {
        // Handle rate limiting
        const retryAfter = error.response?.headers?.get("retry-after");
        console.log(`Rate limited. Retry after: ${retryAfter}`);
      }
      throw error;
    }
  }
};

const pipeline = createClientPipeline();
pipeline.addPolicy(errorHandlingPolicy);

Install with Tessl CLI

npx tessl i tessl/npm-azure--core-client

docs

authentication.md

index.md

operations.md

pipeline.md

policies.md

serialization.md

service-client.md

tile.json