Azure ms-rest Client Runtime
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
-
TypeError: msRest.loginWithServicePrincipalSecret is not a function
cause `ms-rest` itself does not contain authentication methods. These are provided by related packages like `ms-rest-azure` or `@azure/ms-rest-nodeauth`.fixImport authentication methods from `ms-rest-azure` or `@azure/ms-rest-nodeauth`. For modern applications, migrate to `@azure/identity`. Example: `const msRestAzure = require('ms-rest-azure'); msRestAzure.loginWithServicePrincipalSecret(...)` or `import { DefaultAzureCredential } from '@azure/identity';` -
ERR_REQUIRE_ESM: Must use import to load ES Module
cause This error occurs when a CommonJS module (like `ms-rest`) attempts to `require()` a package that has transitioned to ES Module format, or when a CommonJS project is trying to run in an environment configured for ESM without proper interop.fixIf consuming `ms-rest` from an ESM context, you may need to use dynamic `import('ms-rest')` or ensure your build system handles CommonJS interoperability. If `ms-rest` is requiring another package that is now ESM-only, you might need to find a CommonJS-compatible version of that dependency or upgrade `ms-rest` if a newer, ESM-compatible version exists (though `ms-rest` itself is deprecated). -
A required property was not provided for type 'MyType'. Property name: 'requiredField'.
cause `ms-rest` performs validation during serialization and deserialization based on the provided mappers. This error indicates a required property, as defined in the mapper, was missing from the object being processed.fixEnsure that all properties marked `required: true` in your mapper definitions are present in the JavaScript/TypeScript object you are serializing or deserializing. Check the input object and your mapper definition for consistency.
Warnings
- breaking The entire `azure-sdk-for-node` repository, which includes `ms-rest`, has been deprecated. Users are strongly encouraged to migrate to the new, modular `@azure/*` packages within the `Azure SDK for JavaScript` repository.
- breaking The related `ms-rest-js` (isomorphic runtime) experienced breaking changes in its v2.0.0 release, notably changing the default HTTP client from `axios` to `node-fetch` and altering `AbortController` behavior. While `ms-rest` is Node.js-specific, this indicates a pattern of significant API shifts across major versions within the broader `ms-rest` family.
- deprecated Authentication mechanisms previously handled by `ms-rest-azure` (which depends on `ms-rest`) and `@azure/ms-rest-nodeauth` are deprecated. Modern Azure SDKs use `@azure/identity` for a unified authentication experience.
- gotcha Attempting to mix CommonJS `require()` (used by `ms-rest` itself) with ES Modules `import` in a single project can lead to interoperability issues, especially in older Node.js environments or complex module graphs.
Install
-
npm install ms-rest -
yarn add ms-rest -
pnpm add ms-rest
Imports
- serialize
const msRest = require('ms-rest'); const serialized = msRest.serialize(...);import { serialize } from 'ms-rest'; - deserialize
const msRest = require('ms-rest'); const deserialized = msRest.deserialize(...);import { deserialize } from 'ms-rest'; - ServiceClient
const ServiceClient = require('ms-rest').ServiceClient;import { ServiceClient } from 'ms-rest';
Quickstart
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);
}