Azure ms-rest Client Runtime

2.5.6 · deprecated · verified Tue Apr 21

ms-rest is a foundational client runtime for Node.js client libraries generated using AutoRest, specifically designed for interacting with Azure services. It provides core infrastructure for serialization, deserialization, error handling, tracing, and configuring HTTP client pipelines. The current stable version is 2.5.6, with its last publish in May 2022. While it was a cornerstone of the older Azure SDK for Node.js, this package and its containing repository (`azure-sdk-for-node`) are now officially deprecated. Newer Azure SDKs (`@azure/*`) leverage more modular core packages like `@azure/core-http` and `@azure/core-rest-pipeline` for improved performance and maintainability. ms-rest ships with built-in TypeScript type definitions and adheres to Microsoft REST API Guidelines for consistent API design and error handling, differentiating it by its specific use within the Azure ecosystem for AutoRest-generated clients.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic object serialization and deserialization using `ms-rest` mappers, including handling required properties during validation.

import { serialize, deserialize } from 'ms-rest';

interface Product {
  id: number;
  name: string;
  price?: number;
}

const productMapper = {
  type: {
    name: 'Composite',
    className: 'Product',
    modelProperties: {
      id: {
        required: true,
        serializedName: 'id',
        type: { name: 'Number' }
      },
      name: {
        required: true,
        serializedName: 'productName',
        type: { name: 'String' }
      },
      price: {
        required: false,
        serializedName: 'productPrice',
        type: { name: 'Number' }
      }
    }
  }
};

const originalProduct: Product = { id: 1, name: 'Example Widget', price: 99.99 };

// Serialize an object based on the defined mapper
const serializedProduct = serialize(productMapper, originalProduct, 'productObject');

console.log('Original Product:', originalProduct);
console.log('Serialized Product:', JSON.stringify(serializedProduct, null, 2));

// Assuming the serialized form might have different property names on the wire
const receivedSerializedProduct = { id: 1, productName: 'Example Widget', productPrice: 99.99 };

// Deserialize the received object back into the Product interface
const deserializedProduct = deserialize(productMapper, receivedSerializedProduct, 'productObject') as Product;

console.log('Deserialized Product:', deserializedProduct);

// Example with missing required property (will throw an error during serialization/deserialization)
try {
  const invalidProduct = { name: 'Missing ID' };
  serialize(productMapper, invalidProduct, 'invalidProduct');
} catch (error: any) {
  console.error('\nError during serialization (expected):', error.message);
}

view raw JSON →