updated the runtime
This commit is contained in:
Родитель
163b617f3d
Коммит
c0cd61d802
|
@ -71,8 +71,8 @@
|
|||
"request": "2.52.0",
|
||||
"node-uuid": "~1.4",
|
||||
"azure-storage": "0.3.3",
|
||||
"ms-rest-azure": "^1.10.0",
|
||||
"ms-rest": "^1.10.0"
|
||||
"ms-rest-azure": "^1.11.0",
|
||||
"ms-rest": "^1.11.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"adal-node": "0.1.17",
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
{
|
||||
"bitwise": false,
|
||||
"camelcase": true,
|
||||
"curly": false,
|
||||
"eqeqeq": false,
|
||||
"forin": false,
|
||||
"immed": true,
|
||||
"indent": 2,
|
||||
"latedef": false,
|
||||
"maxparams": false,
|
||||
"maxdepth": false,
|
||||
"maxstatements": false,
|
||||
"maxcomplexity": false,
|
||||
"newcap": true,
|
||||
"noarg": true,
|
||||
"node": true,
|
||||
"noempty": true,
|
||||
"nonew": true,
|
||||
"plusplus": false,
|
||||
"quotmark": "single",
|
||||
"regexp": true,
|
||||
"sub": true,
|
||||
"strict": false,
|
||||
"trailing": true,
|
||||
"undef": false,
|
||||
"unused": true,
|
||||
"loopfunc": true
|
||||
}
|
|
@ -11,6 +11,21 @@ Infrastructure for error handling, tracing, and http client pipeline configurati
|
|||
npm install ms-rest-azure
|
||||
```
|
||||
|
||||
## Usage
|
||||
```javascript
|
||||
var msrestAzure = require('ms-rest-azure');
|
||||
```
|
||||
## Authentication
|
||||
|
||||
```javascript
|
||||
//user authentication
|
||||
var credentials = new msRestAzure.UserTokenCredentials('your-client-id', 'your-domain', 'your-username', 'your-password', 'your-redirect-uri');
|
||||
//service principal authentication
|
||||
var credentials = new msRestAzure.ApplicationTokenCredentials('your-client-id', 'your-domain', 'your-secret');
|
||||
```
|
||||
### Non-Interactive Authentication
|
||||
If you need to create an automation account for non interactive or scripting scenarios then please take a look at the documentation over [here](https://github.com/Azure/azure-sdk-for-node/blob/autorest/Documentation/Authentication.md).
|
||||
|
||||
## Related Projects
|
||||
|
||||
- [AutoRest](https://github.com/Azure/AutoRest)
|
|
@ -23,7 +23,7 @@ function AzureEnvironment(authenticationEndpoint, tokenAudience, validateAuthori
|
|||
/**
|
||||
* Provides the settings for authentication with Azure
|
||||
*/
|
||||
var Azure = new AzureEnvironment('https://login.windows.net/',
|
||||
var Azure = new AzureEnvironment('https://login.microsoftonline.com/',
|
||||
'https://management.core.windows.net/',
|
||||
true);
|
||||
|
||||
|
@ -34,8 +34,16 @@ var AzureChina = new AzureEnvironment('https://login.chinacloudapi.cn/',
|
|||
'https://management.core.chinacloudapi.cn/',
|
||||
true);
|
||||
|
||||
/**
|
||||
* Provides the settings for authentication with Azure US Government
|
||||
*/
|
||||
var AzureUSGovernment = new AzureEnvironment('https://login.microsoftonline.com/',
|
||||
'https://management.core.usgovcloudapi.net/',
|
||||
true);
|
||||
|
||||
_.extend(module.exports, {
|
||||
Azure: Azure,
|
||||
AzureChina: AzureChina,
|
||||
AzureEnvironment: AzureEnvironment
|
||||
AzureEnvironment: AzureEnvironment,
|
||||
AzureUSGovernment: AzureUSGovernment
|
||||
});
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
exports.AzureServiceClient = require('./azureServiceClient');
|
||||
exports.UserTokenCredentials = require('./credentials/userTokenCredentials');
|
||||
exports.ApplicationTokenCredentials = require('./credentials/applicationTokenCredentials');
|
||||
exports.AzureEnvironment = require('./azureEnvironment');
|
||||
exports.BaseResource = require('./baseResource');
|
||||
exports.CloudError = require('./cloudError');
|
||||
exports.TokenCredentials = require('ms-rest').TokenCredentials;
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
"email": "azsdkteam@microsoft.com",
|
||||
"url": "https://github.com/Azure/AutoRest"
|
||||
},
|
||||
"version": "1.8.0",
|
||||
"version": "1.11.0",
|
||||
"description": "Client Runtime for Node.js Azure client libraries generated using AutoRest",
|
||||
"tags": [ "node", "microsoft", "autorest", "azure", "clientruntime" ],
|
||||
"keywords": [ "node", "microsoft", "autorest", "azure", "clientruntime" ],
|
||||
|
@ -22,8 +22,8 @@
|
|||
"dependencies": {
|
||||
"async": "0.2.7",
|
||||
"uuid": "2.0.1",
|
||||
"adal-node": "0.1.16",
|
||||
"ms-rest": "^1.8.0",
|
||||
"adal-node": "0.1.17",
|
||||
"ms-rest": "^1.11.0",
|
||||
"underscore": "^1.4.0",
|
||||
"moment": "^2.6.0"
|
||||
},
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
# MS-Rest
|
||||
|
||||
Infrastructure for error handling, tracing, and http client pipeline configuration. Required by nodeJS client libraries generated using AutoRest.
|
||||
Infrastructure for serialization/deserialization, error handling, tracing, and http client pipeline configuration. Required by nodeJS client libraries generated using AutoRest.
|
||||
|
||||
- **Node.js version: 0.10.0 or higher**
|
||||
|
||||
|
@ -11,6 +11,47 @@ Infrastructure for error handling, tracing, and http client pipeline configurati
|
|||
npm install ms-rest
|
||||
```
|
||||
|
||||
## Usage
|
||||
```javascript
|
||||
var msrest = require('ms-rest');
|
||||
```
|
||||
## Serialization/Deserialization
|
||||
Features
|
||||
- Type checking
|
||||
- (String, Number, Boolean, ByteArray, Date, DateTime, Enum, TimeSpan, DateTimeRfc1123, Object, Stream, Sequence, Dictionary, Composite, Uuid(as a string))
|
||||
- Validation of specified constraints
|
||||
- ExclusiveMaximum, ExclusiveMinimum, InclusiveMaximum, InclusiveMinimum, MaxItems, MaxLength, MinItems, MinLength, MultipleOf, Pattern, UniqueItems
|
||||
- Flattening/Unflattening properties
|
||||
- Default Values
|
||||
- Model Properties marked as constant are set during serialization, irrespective of they being provided or not
|
||||
- Required check (If a model or property is marked required and is not provided in the object then an error is thrown)
|
||||
- Readonly check (If a model or property is marked readonly then it is not sent on the wire during, serialization)
|
||||
|
||||
- serialize an array of dictionary of primitive values
|
||||
```javascript
|
||||
var mapper = {
|
||||
type : {
|
||||
name: 'Sequence',
|
||||
element: {
|
||||
type : {
|
||||
name: 'Dictionary',
|
||||
value: {
|
||||
type: {
|
||||
name: 'Boolean'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
var array = [{ 1: true }, { 2: false }, { 1: true, 2: false, 3: true }];
|
||||
var serializedArray = msRest.serialize(mapper, array, 'arrayObj');
|
||||
assert.deepEqual(array, serializedArray);
|
||||
var serializedProduct = msrest.serialize(mapper, productObj, 'productObject');
|
||||
var deserializedArray = msRest.deserialize(mapper, serializedArray, 'serializedArrayObj');
|
||||
```
|
||||
For more examples on serialization/deserialization with complex types please take a look over [here](https://github.com/Azure/autorest/blob/master/ClientRuntimes/NodeJS/ms-rest/test/serializationTests.js#L116).
|
||||
|
||||
## Related Projects
|
||||
|
||||
- [AutoRest](https://github.com/Azure/AutoRest)
|
|
@ -3,7 +3,6 @@
|
|||
|
||||
var util = require('util');
|
||||
var Constants = require('../constants');
|
||||
var validate = require('../validate');
|
||||
|
||||
var HeaderConstants = Constants.HeaderConstants;
|
||||
var DEFAULT_AUTHORIZATION_SCHEME = 'Basic';
|
||||
|
@ -17,10 +16,13 @@ var DEFAULT_AUTHORIZATION_SCHEME = 'Basic';
|
|||
* @param {string} [authorizationScheme] The authorization scheme.
|
||||
*/
|
||||
function BasicAuthenticationCredentials(userName, password, authorizationScheme) {
|
||||
validate.validateArgs('BasicAuthenticationCredentials', function (v) {
|
||||
v.string(userName, 'userName');
|
||||
v.string(password, 'password');
|
||||
});
|
||||
if (userName === null || userName === undefined || typeof userName.valueOf() !== 'string') {
|
||||
throw new Error('userName cannot be null or undefined and must be of type string.');
|
||||
}
|
||||
|
||||
if (password === null || password === undefined || typeof password.valueOf() !== 'string') {
|
||||
throw new Error('password cannot be null or undefined and must be of type string.');
|
||||
}
|
||||
|
||||
this.userName = userName;
|
||||
this.password = password;
|
||||
|
|
|
@ -155,8 +155,6 @@ function ExponentialRetryPolicyFilter(retryCount, retryInterval, minRetryInterva
|
|||
});
|
||||
|
||||
return newFilter;
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -2,38 +2,33 @@
|
|||
// Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
|
||||
'use strict';
|
||||
|
||||
var HttpOperationResponse = ( function() {
|
||||
/**
|
||||
* Wrapper object for http request and response. Deserialized object is stored in
|
||||
* the `body` property.
|
||||
* @class
|
||||
* Initializes a new instance of the HttpOperationResponse class.
|
||||
* @constructor
|
||||
*/
|
||||
function HttpOperationResponse(request, response) {
|
||||
/**
|
||||
* Wrapper object for http request and response. Deserialized object is stored in
|
||||
* the `body` property.
|
||||
* @class
|
||||
* Initializes a new instance of the HttpOperationResponse class.
|
||||
* @constructor
|
||||
* Reference to the original request object.
|
||||
* [WebResource] object.
|
||||
* @type {object}
|
||||
*/
|
||||
function HttpOperationResponse(request, response) {
|
||||
/**
|
||||
* Reference to the original request object.
|
||||
* [WebResource] object.
|
||||
* @type {object}
|
||||
*/
|
||||
this.request = request;
|
||||
|
||||
/**
|
||||
* Reference to the original response object.
|
||||
* [ServerResponse] object.
|
||||
* @type {object}
|
||||
*/
|
||||
this.response = response;
|
||||
|
||||
/**
|
||||
* The response object.
|
||||
* @type {object}
|
||||
*/
|
||||
this.body = null;
|
||||
}
|
||||
this.request = request;
|
||||
|
||||
return HttpOperationResponse;
|
||||
})();
|
||||
/**
|
||||
* Reference to the original response object.
|
||||
* [ServerResponse] object.
|
||||
* @type {object}
|
||||
*/
|
||||
this.response = response;
|
||||
|
||||
exports = module.exports = HttpOperationResponse;
|
||||
/**
|
||||
* The response object.
|
||||
* @type {object}
|
||||
*/
|
||||
this.body = null;
|
||||
}
|
||||
|
||||
module.exports = HttpOperationResponse;
|
||||
|
|
|
@ -1,83 +0,0 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
|
||||
function Logger(level, loggerFunction) {
|
||||
this.level = level;
|
||||
this.loggerFunction = loggerFunction;
|
||||
|
||||
if (!this.loggerFunction) {
|
||||
this.loggerFunction = this.defaultLoggerFunction;
|
||||
}
|
||||
}
|
||||
|
||||
Logger.LogLevels = {
|
||||
/**
|
||||
* Fatal condition.
|
||||
*/
|
||||
FATAL : 'fatal',
|
||||
|
||||
/**
|
||||
* Error condition.
|
||||
*/
|
||||
ERROR : 'error',
|
||||
|
||||
/**
|
||||
* Warning condition.
|
||||
*/
|
||||
WARNING : 'warning',
|
||||
|
||||
/**
|
||||
* Purely informational message.
|
||||
*/
|
||||
INFO : 'info',
|
||||
|
||||
/**
|
||||
* Application debug messages.
|
||||
*/
|
||||
DEBUG : 'debug'
|
||||
};
|
||||
|
||||
Logger.logPriority = [
|
||||
Logger.LogLevels.FATAL,
|
||||
Logger.LogLevels.ERROR,
|
||||
Logger.LogLevels.WARNING,
|
||||
Logger.LogLevels.NOTICE,
|
||||
Logger.LogLevels.INFO,
|
||||
Logger.LogLevels.DEBUG
|
||||
];
|
||||
|
||||
Logger.prototype.log = function (level, msg) {
|
||||
this.loggerFunction(level, msg);
|
||||
};
|
||||
|
||||
Logger.prototype.fatal = function(msg) {
|
||||
this.log(Logger.LogLevels.FATAL, msg);
|
||||
};
|
||||
|
||||
Logger.prototype.error = function(msg) {
|
||||
this.log(Logger.LogLevels.ERROR, msg);
|
||||
};
|
||||
|
||||
Logger.prototype.warn = function(msg) {
|
||||
this.log(Logger.LogLevels.WARNING, msg);
|
||||
};
|
||||
|
||||
Logger.prototype.info = function(msg) {
|
||||
this.log(Logger.LogLevels.INFO, msg);
|
||||
};
|
||||
|
||||
Logger.prototype.debug = function(msg) {
|
||||
this.log(Logger.LogLevels.DEBUG, msg);
|
||||
};
|
||||
|
||||
Logger.prototype.defaultLoggerFunction = function(logLevel , msg) {
|
||||
var currentLevelIndex = Logger.logPriority.indexOf(this.level);
|
||||
var logLevelIndex = Logger.logPriority.indexOf(logLevel);
|
||||
var time = new Date();
|
||||
var timeStamp = time.toISOString();
|
||||
if (logLevelIndex <= currentLevelIndex) {
|
||||
console.log('[' + timeStamp + ']' + this.level + ' : ' + msg);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = Logger;
|
|
@ -2,16 +2,7 @@
|
|||
// Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
|
||||
var utils = require('./utils');
|
||||
var nodeVersion = utils.getNodeVersion();
|
||||
if (nodeVersion.major === 0 &&
|
||||
nodeVersion.minor > 8 &&
|
||||
!(nodeVersion.minor > 10 || (nodeVersion.minor === 10 && nodeVersion.patch >= 3))) {
|
||||
throw new Error('The Microsoft client runtime does not work with node versions > 0.8.22 and ' +
|
||||
'< 0.10.3, due to security issues. Please upgrade to node >= 0.10.3');
|
||||
}
|
||||
|
||||
exports.Constants = require('./constants');
|
||||
exports.Logger = require('./logger');
|
||||
|
||||
exports.WebResource = require('./webResource');
|
||||
exports.ServiceClient = require('./serviceClient');
|
||||
|
@ -27,10 +18,10 @@ exports.LogFilter = require('./filters/logFilter');
|
|||
exports.SigningFilter = require('./filters/signingFilter');
|
||||
exports.ExponentialRetryPolicyFilter = require('./filters/exponentialRetryPolicyFilter');
|
||||
|
||||
exports.validate = require('./validate');
|
||||
exports.requestPipeline = require('./requestPipeline');
|
||||
exports.stripResponse = utils.stripResponse;
|
||||
exports.stripRequest = utils.stripRequest;
|
||||
exports.isValidUuid = utils.isValidUuid;
|
||||
|
||||
//serialization
|
||||
exports.serializeObject = require('./serialization').serializeObject;
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
var util = require('util');
|
||||
var moment = require('moment');
|
||||
var stream = require('stream');
|
||||
var utils = require('./utils');
|
||||
|
||||
/**
|
||||
* Serializes the JSON Object. It serializes Buffer object to a
|
||||
|
@ -55,17 +56,21 @@ exports.serializeObject = function (toSerialize) {
|
|||
exports.serialize = function (mapper, object, objectName) {
|
||||
var payload = {};
|
||||
var mapperType = mapper.type.name;
|
||||
if (!objectName) objectName = objectNameFromSerializedName(mapper.serializedName);
|
||||
if (!objectName) objectName = mapper.serializedName;
|
||||
if (mapperType.match(/^Sequence$/ig) !== null) payload = [];
|
||||
//Throw if required and object is null or undefined
|
||||
if (mapper.required && (object === null || object === undefined)) {
|
||||
if (mapper.required && (object === null || object === undefined) && !mapper.isConstant) {
|
||||
throw new Error(util.format('\'%s\' cannot be null or undefined.'), objectName);
|
||||
}
|
||||
//Set Defaults
|
||||
if (mapper.defaultValue && (object === null || object === undefined)) object = mapper.defaultValue;
|
||||
if ((mapper.defaultValue !== null && mapper.defaultValue !== undefined) &&
|
||||
(object === null || object === undefined)) {
|
||||
object = mapper.defaultValue;
|
||||
}
|
||||
if (mapper.isConstant) object = mapper.defaultValue;
|
||||
//Validate Constraints if any
|
||||
validateConstraints.call(this, mapper, object, objectName);
|
||||
if (mapperType.match(/^(Number|String|Boolean|Object|Stream)$/ig) !== null) {
|
||||
if (mapperType.match(/^(Number|String|Boolean|Object|Stream|Uuid)$/ig) !== null) {
|
||||
payload = serializeBasicTypes.call(this, mapperType, objectName, object);
|
||||
} else if (mapperType.match(/^Enum$/ig) !== null) {
|
||||
payload = serializeEnumType.call(this, objectName, mapper.type.allowedValues, object);
|
||||
|
@ -185,22 +190,29 @@ function serializeDictionaryType(mapper, object, objectName) {
|
|||
function serializeCompositeType(mapper, object, objectName) {
|
||||
/*jshint validthis: true */
|
||||
//check for polymorphic discriminator
|
||||
if (mapper.type.polymorphicDiscriminator) {
|
||||
if (mapper.type.polymorphicDiscriminator !== null && mapper.type.polymorphicDiscriminator !== undefined) {
|
||||
if (object === null || object === undefined) {
|
||||
throw new Error(util.format('\'%s\' cannot be null or undefined. \'%s\' is the ' +
|
||||
'polmorphicDiscriminator and is a required property.', objectName,
|
||||
mapper.type.polymorphicDiscriminator));
|
||||
}
|
||||
if (!object[mapper.type.polymorphicDiscriminator]) {
|
||||
if (object[mapper.type.polymorphicDiscriminator] === null || object[mapper.type.polymorphicDiscriminator] === undefined) {
|
||||
throw new Error(util.format('No discriminator field \'%s\' was found in \'%s\'.',
|
||||
mapper.type.polymorphicDiscriminator, objectName));
|
||||
}
|
||||
if (!this.models.discriminators[object[mapper.type.polymorphicDiscriminator]]) {
|
||||
throw new Error(util.format('\'%s\': \'%s\' in \'%s\' is not a valid ' +
|
||||
'discriminator as a corresponding model class for that value was not found.',
|
||||
mapper.type.polymorphicDiscriminator, object[mapper.type.polymorphicDiscriminator], objectName));
|
||||
var indexDiscriminator = null;
|
||||
if (object[mapper.type.polymorphicDiscriminator] === mapper.type.uberParent) {
|
||||
indexDiscriminator = object[mapper.type.polymorphicDiscriminator];
|
||||
} else {
|
||||
indexDiscriminator = mapper.type.uberParent + '.' + object[mapper.type.polymorphicDiscriminator];
|
||||
}
|
||||
mapper = new this.models.discriminators[object[mapper.type.polymorphicDiscriminator]]().mapper();
|
||||
if (!this.models.discriminators[indexDiscriminator]) {
|
||||
throw new Error(util.format('\'%s\': \'%s\' in \'%s\' is not a valid ' +
|
||||
'discriminator as a corresponding model class for the disciminator \'%s\' ' +
|
||||
'was not found in this.models.discriminators object.',
|
||||
mapper.type.polymorphicDiscriminator, object[mapper.type.polymorphicDiscriminator], objectName, indexDiscriminator));
|
||||
}
|
||||
mapper = new this.models.discriminators[indexDiscriminator]().mapper();
|
||||
}
|
||||
|
||||
var payload = {};
|
||||
|
@ -229,21 +241,37 @@ function serializeCompositeType(mapper, object, objectName) {
|
|||
}
|
||||
}
|
||||
|
||||
if (requiresFlattening(modelProps, object) && !payload.properties) payload.properties = {};
|
||||
for (var key in modelProps) {
|
||||
if (modelProps.hasOwnProperty(key)) {
|
||||
var paths = splitSerializeName(modelProps[key].serializedName);
|
||||
var propName = paths.pop();
|
||||
|
||||
var parentObject = payload;
|
||||
paths.forEach(function(pathName) {
|
||||
var childObject = parentObject[pathName];
|
||||
if ((childObject === null || childObject === undefined) && (object[key] !== null && object[key] !== undefined)) {
|
||||
parentObject[pathName] = {};
|
||||
}
|
||||
parentObject = parentObject[pathName];
|
||||
});
|
||||
|
||||
//make sure required properties of the CompositeType are present
|
||||
if (modelProps[key].required) {
|
||||
if (modelProps[key].required && !modelProps[key].isConstant) {
|
||||
if (object[key] === null || object[key] === undefined) {
|
||||
throw new Error(util.format('\'%s\' cannot be null or undefined in \'%s\'.', key, objectName));
|
||||
}
|
||||
}
|
||||
//make sure that readOnly properties are not sent on the wire
|
||||
if (modelProps[key].readOnly) {
|
||||
continue;
|
||||
}
|
||||
//serialize the property if it is present in the provided object instance
|
||||
if (object[key] !== null && object[key] !== undefined) {
|
||||
var propertyObjectName = objectName + '.' + objectNameFromSerializedName(modelProps[key].serializedName);
|
||||
if ((modelProps[key].defaultValue !== null && modelProps[key].defaultValue !== undefined) ||
|
||||
(object[key] !== null && object[key] !== undefined)) {
|
||||
var propertyObjectName = objectName + '.' + modelProps[key].serializedName;
|
||||
var propertyMapper = modelProps[key];
|
||||
var serializedValue = exports.serialize.call(this, propertyMapper, object[key], propertyObjectName);
|
||||
assignProperty(modelProps[key].serializedName, payload, serializedValue);
|
||||
parentObject[propName] = serializedValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -256,15 +284,19 @@ function serializeBasicTypes(typeName, objectName, value) {
|
|||
if (value !== null && value !== undefined) {
|
||||
if (typeName.match(/^Number$/ig) !== null) {
|
||||
if (typeof value !== 'number') {
|
||||
throw new Error(util.format('%s must be of type number.', objectName));
|
||||
throw new Error(util.format('%s with value %s must be of type number.', objectName, value));
|
||||
}
|
||||
} else if (typeName.match(/^String$/ig) !== null) {
|
||||
if (typeof value.valueOf() !== 'string') {
|
||||
throw new Error(util.format('%s must be of type string.', objectName));
|
||||
throw new Error(util.format('%s with value \'%s\' must be of type string.', objectName, value));
|
||||
}
|
||||
} else if (typeName.match(/^Uuid$/ig) !== null) {
|
||||
if (!(typeof value.valueOf() === 'string' && utils.isValidUuid(value))) {
|
||||
throw new Error(util.format('%s with value \'%s\' must be of type string and a valid uuid.', objectName, value));
|
||||
}
|
||||
} else if (typeName.match(/^Boolean$/ig) !== null) {
|
||||
if (typeof value !== 'boolean') {
|
||||
throw new Error(util.format('%s must be of type boolean.', objectName));
|
||||
throw new Error(util.format('%s with value %s must be of type boolean.', objectName, value));
|
||||
}
|
||||
} else if (typeName.match(/^Object$/ig) !== null) {
|
||||
if (typeof value !== 'object') {
|
||||
|
@ -283,7 +315,13 @@ function serializeEnumType(objectName, allowedValues, value) {
|
|||
if (!allowedValues) {
|
||||
throw new Error(util.format('Please provide a set of allowedValues to validate %s as an Enum Type.', objectName));
|
||||
}
|
||||
if (!allowedValues.some(function (item) { return item === value; })) {
|
||||
var isPresent = allowedValues.some(function (item) {
|
||||
if (typeof item.valueOf() === 'string') {
|
||||
return item.toLowerCase() === value.toLowerCase();
|
||||
}
|
||||
return item === value;
|
||||
});
|
||||
if (!isPresent) {
|
||||
throw new Error(util.format('%s is not a valid value for %s. The valid values are: %s',
|
||||
value, objectName, JSON.stringify(allowedValues)));
|
||||
}
|
||||
|
@ -345,10 +383,10 @@ exports.deserialize = function (mapper, responseBody, objectName) {
|
|||
if (responseBody === null || responseBody === undefined) return responseBody;
|
||||
var payload = {};
|
||||
var mapperType = mapper.type.name;
|
||||
if (!objectName) objectName = objectNameFromSerializedName(mapper.serializedName);
|
||||
if (!objectName) objectName = mapper.serializedName;
|
||||
if (mapperType.match(/^Sequence$/ig) !== null) payload = [];
|
||||
|
||||
if (mapperType.match(/^(Number|String|Boolean|Enum|Object|Stream)$/ig) !== null) {
|
||||
if (mapperType.match(/^(Number|String|Boolean|Enum|Object|Stream|Uuid)$/ig) !== null) {
|
||||
payload = responseBody;
|
||||
} else if (mapperType.match(/^(Date|DateTime|DateTimeRfc1123)$/ig) !== null) {
|
||||
payload = new Date(responseBody);
|
||||
|
@ -363,6 +401,9 @@ exports.deserialize = function (mapper, responseBody, objectName) {
|
|||
} else if (mapperType.match(/^Composite$/ig) !== null) {
|
||||
payload = deserializeCompositeType.call(this, mapper, responseBody, objectName);
|
||||
}
|
||||
|
||||
if (mapper.isConstant) payload = mapper.defaultValue;
|
||||
|
||||
return payload;
|
||||
};
|
||||
|
||||
|
@ -403,22 +444,29 @@ function deserializeDictionaryType(mapper, responseBody, objectName) {
|
|||
function deserializeCompositeType(mapper, responseBody, objectName) {
|
||||
/*jshint validthis: true */
|
||||
//check for polymorphic discriminator
|
||||
if (mapper.type.polymorphicDiscriminator) {
|
||||
if (mapper.type.polymorphicDiscriminator !== null && mapper.type.polymorphicDiscriminator !== undefined) {
|
||||
if (responseBody === null || responseBody === undefined) {
|
||||
throw new Error(util.format('\'%s\' cannot be null or undefined. \'%s\' is the ' +
|
||||
'polmorphicDiscriminator and is a required property.', objectName,
|
||||
mapper.type.polymorphicDiscriminator));
|
||||
}
|
||||
if (!responseBody[mapper.type.polymorphicDiscriminator]) {
|
||||
if (responseBody[mapper.type.polymorphicDiscriminator] === null || responseBody[mapper.type.polymorphicDiscriminator] === undefined) {
|
||||
throw new Error(util.format('No discriminator field \'%s\' was found in \'%s\'.',
|
||||
mapper.type.polymorphicDiscriminator, objectName));
|
||||
}
|
||||
if (!this.models.discriminators[responseBody[mapper.type.polymorphicDiscriminator]]) {
|
||||
throw new Error(util.format('\'%s\': \'%s\' in \'%s\' is not a valid ' +
|
||||
'discriminator as a corresponding model class for that value was not found.',
|
||||
mapper.type.polymorphicDiscriminator, responseBody[mapper.type.polymorphicDiscriminator], objectName));
|
||||
var indexDiscriminator = null;
|
||||
if (responseBody[mapper.type.polymorphicDiscriminator] === mapper.type.uberParent) {
|
||||
indexDiscriminator = responseBody[mapper.type.polymorphicDiscriminator];
|
||||
} else {
|
||||
indexDiscriminator = mapper.type.uberParent + '.' + responseBody[mapper.type.polymorphicDiscriminator];
|
||||
}
|
||||
mapper = new this.models.discriminators[responseBody[mapper.type.polymorphicDiscriminator]]().mapper();
|
||||
if (!this.models.discriminators[indexDiscriminator]) {
|
||||
throw new Error(util.format('\'%s\': \'%s\' in \'%s\' is not a valid ' +
|
||||
'discriminator as a corresponding model class for the disciminator \'%s\' ' +
|
||||
'was not found in this.models.discriminators object.',
|
||||
mapper.type.polymorphicDiscriminator, responseBody[mapper.type.polymorphicDiscriminator], objectName, indexDiscriminator));
|
||||
}
|
||||
mapper = new this.models.discriminators[indexDiscriminator]().mapper();
|
||||
}
|
||||
|
||||
var instance = {};
|
||||
|
@ -449,19 +497,25 @@ function deserializeCompositeType(mapper, responseBody, objectName) {
|
|||
|
||||
for (var key in modelProps) {
|
||||
if (modelProps.hasOwnProperty(key)) {
|
||||
|
||||
var jpath = ['responseBody'];
|
||||
var paths = splitSerializeName(modelProps[key].serializedName);
|
||||
paths.forEach(function(item){
|
||||
jpath.push(util.format('[\'%s\']', item));
|
||||
});
|
||||
//deserialize the property if it is present in the provided responseBody instance
|
||||
var propertyInstance = responseBody[modelProps[key].serializedName];
|
||||
if (stringContainsProperties(modelProps[key].serializedName)) {
|
||||
if (responseBody.properties) {
|
||||
var serializedKey = objectNameFromSerializedName(modelProps[key].serializedName);
|
||||
propertyInstance = responseBody.properties[serializedKey];
|
||||
}
|
||||
var propertyInstance;
|
||||
try {
|
||||
/*jslint evil: true */
|
||||
propertyInstance = eval(jpath.join(''));
|
||||
} catch (err) {
|
||||
continue;
|
||||
}
|
||||
var propertyObjectName = objectName + '.' + modelProps[key].serializedName;
|
||||
var propertyMapper = modelProps[key];
|
||||
var serializedValue;
|
||||
//paging
|
||||
if (key === 'value' && util.isArray(responseBody[key]) && modelProps[key].serializedName === '') {
|
||||
if (util.isArray(responseBody[key]) && modelProps[key].serializedName === '') {
|
||||
propertyInstance = responseBody[key];
|
||||
instance = exports.deserialize.call(this, propertyMapper, propertyInstance, propertyObjectName);
|
||||
} else if (propertyInstance !== null && propertyInstance !== undefined) {
|
||||
|
@ -475,31 +529,22 @@ function deserializeCompositeType(mapper, responseBody, objectName) {
|
|||
return responseBody;
|
||||
}
|
||||
|
||||
function assignProperty(serializedName, payload, serializedValue) {
|
||||
var key = objectNameFromSerializedName(serializedName);
|
||||
if (stringContainsProperties(serializedName)) {
|
||||
payload.properties[key] = serializedValue;
|
||||
} else {
|
||||
payload[key] = serializedValue;
|
||||
}
|
||||
}
|
||||
function splitSerializeName(prop) {
|
||||
var classes = [];
|
||||
var partialclass = '';
|
||||
var subwords = prop.split('.');
|
||||
|
||||
function requiresFlattening(mapper, object) {
|
||||
return Object.keys(mapper).some(function (key) {
|
||||
return ((mapper[key].serializedName.match(/^properties\./ig) !== null) &&
|
||||
(object[key] !== null && object[key] !== undefined));
|
||||
subwords.forEach(function(item) {
|
||||
if (item.charAt(item.length - 1) === '\\') {
|
||||
partialclass += item.substr(0, item.length - 1) + '.';
|
||||
} else {
|
||||
partialclass += item;
|
||||
classes.push(partialclass);
|
||||
partialclass = '';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function objectNameFromSerializedName(name) {
|
||||
if (stringContainsProperties(name)) {
|
||||
return name.match(/^properties\.(\w+)$/i)[1];
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
function stringContainsProperties(prop) {
|
||||
return (prop.match(/^properties\.(\w+)$/i) !== null);
|
||||
return classes;
|
||||
}
|
||||
|
||||
exports = module.exports;
|
|
@ -14,20 +14,6 @@ exports.urlIsHTTPS = function (urlToCheck) {
|
|||
return urlToCheck.protocol.toLowerCase() === Constants.HTTPS;
|
||||
};
|
||||
|
||||
/**
|
||||
* Provides the version of nodejs on the system.
|
||||
*
|
||||
* @return {object} An object specifying the major, minor and patch version of nodejs on the system.
|
||||
*/
|
||||
exports.getNodeVersion = function () {
|
||||
var parsedVersion = process.version.split('.');
|
||||
return {
|
||||
major: parseInt(parsedVersion[0].substr(1), 10),
|
||||
minor: parseInt(parsedVersion[1], 10),
|
||||
patch: parseInt(parsedVersion[2], 10)
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks if a value is null or undefined.
|
||||
*
|
||||
|
@ -83,6 +69,8 @@ exports.stripRequest = function (request) {
|
|||
strippedRequest = JSON.parse(JSON.stringify(request));
|
||||
if (strippedRequest.headers && strippedRequest.headers.Authorization) {
|
||||
delete strippedRequest.headers.Authorization;
|
||||
} else if (strippedRequest.headers && strippedRequest.headers.authorization) {
|
||||
delete strippedRequest.headers.authorization;
|
||||
}
|
||||
} catch (err) {
|
||||
var errMsg = err.message;
|
||||
|
@ -93,4 +81,16 @@ exports.stripRequest = function (request) {
|
|||
return strippedRequest;
|
||||
};
|
||||
|
||||
/**
|
||||
* Validates the given uuid as a string
|
||||
*
|
||||
* @param {string} uuid - The uuid as a string that needs to be validated
|
||||
*
|
||||
* @return {boolean} result - True if the uuid is valid; false otherwise.
|
||||
*/
|
||||
exports.isValidUuid = function(uuid) {
|
||||
var validUuidRegex = new RegExp('^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$', 'ig');
|
||||
return validUuidRegex.test(uuid);
|
||||
};
|
||||
|
||||
exports = module.exports;
|
||||
|
|
|
@ -1,130 +0,0 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
|
||||
var _ = require('underscore');
|
||||
var check = require('validator');
|
||||
|
||||
exports = module.exports;
|
||||
|
||||
function initCallback(callbackParam, resultsCb) {
|
||||
var fail;
|
||||
if (callbackParam) {
|
||||
fail = function (err) {
|
||||
callbackParam(new Error(err));
|
||||
return false;
|
||||
};
|
||||
} else {
|
||||
fail = function (err) {
|
||||
throw new Error(err);
|
||||
};
|
||||
callbackParam = function () {};
|
||||
}
|
||||
|
||||
resultsCb(fail, callbackParam);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a anonymous function that check if the given uri is valid or not.
|
||||
*
|
||||
* @param {string} uri The uri to validate.
|
||||
* @return {function}
|
||||
*/
|
||||
exports.isValidUri = function (uri) {
|
||||
if (!check.isURL(uri)){
|
||||
throw new Error('The provided URI "' + uri + '" is invalid.');
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
exports.isValidUuid = function(uuid, callback) {
|
||||
var validUuidRegex = /^[a-zA-Z0-9]{8}\-[a-zA-Z0-9]{4}\-[a-zA-Z0-9]{4}\-[a-zA-Z0-9]{4}\-[a-zA-Z0-9]{12}$/;
|
||||
|
||||
var fail;
|
||||
|
||||
initCallback(callback, function (f, cb) {
|
||||
fail = f;
|
||||
callback = cb;
|
||||
});
|
||||
|
||||
if (!validUuidRegex.test(uuid)) {
|
||||
return fail('The value is not a valid UUID format.');
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Validates a function.
|
||||
*
|
||||
* @param {object} function The function to validate.
|
||||
* @return {function}
|
||||
*/
|
||||
exports.isValidFunction = function (functionObject, functionName) {
|
||||
if (!functionObject || !_.isFunction(functionObject)) {
|
||||
throw new Error(functionName + ' must be specified.');
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// common functions for validating arguments
|
||||
|
||||
function throwMissingArgument(name, func) {
|
||||
throw new Error('Required argument ' + name + ' for function ' + func + ' is not defined');
|
||||
}
|
||||
|
||||
function ArgumentValidator(functionName) {
|
||||
this.func = functionName;
|
||||
}
|
||||
|
||||
_.extend(ArgumentValidator.prototype, {
|
||||
string: function (val, name) {
|
||||
if (typeof val != 'string' || val.length === 0) {
|
||||
throwMissingArgument(name, this.func);
|
||||
}
|
||||
},
|
||||
|
||||
object: function (val, name) {
|
||||
if (!val) {
|
||||
throwMissingArgument(name, this.func);
|
||||
}
|
||||
},
|
||||
|
||||
exists: function (val, name) {
|
||||
this.object(val, name);
|
||||
},
|
||||
|
||||
function: function (val, name) {
|
||||
if (typeof val !== 'function') {
|
||||
throw new Error('Parameter ' + name + ' for function ' + this.func + ' should be a function but is not');
|
||||
}
|
||||
},
|
||||
|
||||
value: function (val, name) {
|
||||
if (!val) {
|
||||
throwMissingArgument(name, this.func);
|
||||
}
|
||||
},
|
||||
|
||||
nonEmptyArray: function (val, name) {
|
||||
if (!val || val.length === 0) {
|
||||
throw new Error('Required array argument ' + name + ' for function ' + this.func + ' is either not defined or empty');
|
||||
}
|
||||
},
|
||||
|
||||
callback: function (val) {
|
||||
this.object(val, 'callback');
|
||||
this.function(val, 'callback');
|
||||
},
|
||||
|
||||
test: function (predicate, message) {
|
||||
if (!predicate()) {
|
||||
throw new Error(message + ' in function ' + this.func);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
function validateArgs(functionName, validationRules) {
|
||||
var validator = new ArgumentValidator(functionName);
|
||||
validationRules(validator);
|
||||
}
|
||||
|
||||
exports.ArgumentValidator = ArgumentValidator;
|
||||
exports.validateArgs = validateArgs;
|
|
@ -5,7 +5,7 @@
|
|||
"email": "azsdkteam@microsoft.com",
|
||||
"url": "https://github.com/Azure/AutoRest"
|
||||
},
|
||||
"version": "1.8.0",
|
||||
"version": "1.11.0",
|
||||
"description": "Client Runtime for Node.js client libraries generated using AutoRest",
|
||||
"tags": ["node", "microsoft", "autorest", "clientruntime"],
|
||||
"keywords": ["node", "microsoft", "autorest", "clientruntime"],
|
||||
|
@ -23,18 +23,17 @@
|
|||
"dependencies": {
|
||||
"underscore": "^1.4.0",
|
||||
"tunnel": "~0.0.2",
|
||||
"request": "2.52.0",
|
||||
"validator": "~3.1.0",
|
||||
"request": "2.69.0",
|
||||
"duplexer": "~0.1.1",
|
||||
"through": "~2.3.4",
|
||||
"tough-cookie": "*",
|
||||
"moment": "^2.6.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"jshint": "2.6.3",
|
||||
"xunit-file": "0.0.5",
|
||||
"mocha": "2.2.5",
|
||||
"should": "5.2.0"
|
||||
"should": "5.2.0",
|
||||
"node-uuid": "*"
|
||||
},
|
||||
"homepage": "https://github.com/Azure/AutoRest",
|
||||
"repository": {
|
||||
|
|
|
@ -23,11 +23,9 @@
|
|||
"dependencies": {
|
||||
"underscore": "1.4.x",
|
||||
"tunnel": "~0.0.2",
|
||||
"request": "2.52.0",
|
||||
"validator": "~3.1.0",
|
||||
"request": "2.69.0",
|
||||
"duplexer": "~0.1.1",
|
||||
"through": "~2.3.4",
|
||||
"tough-cookie": "*"
|
||||
"through": "~2.3.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"jshint": "2.6.3",
|
||||
|
|
|
@ -9,6 +9,7 @@ var msRest = require('../lib/msRest');
|
|||
var testClient = require('./data/TestClient/lib/testClient');
|
||||
|
||||
var tokenCredentials = new msRest.TokenCredentials('dummy');
|
||||
var valid_uuid = 'ceaafd1e-f936-429f-bbfc-82ee75dddc33';
|
||||
|
||||
describe('msrest', function () {
|
||||
describe('serializeObject', function () {
|
||||
|
@ -115,12 +116,28 @@ describe('msrest', function () {
|
|||
|
||||
describe('serialize', function () {
|
||||
var mapper = {};
|
||||
var invalid_uuid = 'abcd-efgd90-90890jkh';
|
||||
it('should correctly serialize a string', function (done) {
|
||||
mapper = { type : { name: 'String' } };
|
||||
var serializedObject = msRest.serialize(mapper, 'foo', 'stringBody');
|
||||
serializedObject.should.equal('foo');
|
||||
done();
|
||||
});
|
||||
it('should correctly serialize a uuid', function (done) {
|
||||
mapper = { type : { name: 'Uuid' } };
|
||||
var serializedObject = msRest.serialize(mapper, valid_uuid, 'uuidBody');
|
||||
serializedObject.should.equal(valid_uuid);
|
||||
done();
|
||||
});
|
||||
it('should throw an error if the value is not a valid Uuid', function (done) {
|
||||
mapper = { type : { name: 'Uuid' } };
|
||||
try {
|
||||
var serializedObject = msRest.serialize(mapper, invalid_uuid, 'uuidBody');
|
||||
} catch (error) {
|
||||
error.message.should.match(/.*with value.*must be of type string and a valid uuid/ig);
|
||||
done();
|
||||
}
|
||||
});
|
||||
it('should correctly serialize a number', function (done) {
|
||||
mapper = { type : { name: 'Number' } };
|
||||
var serializedObject = msRest.serialize(mapper, 1.506, 'stringBody');
|
||||
|
@ -372,6 +389,12 @@ describe('msrest', function () {
|
|||
});
|
||||
|
||||
describe('deserialize', function () {
|
||||
it('should correctly deserialize a uuid', function (done) {
|
||||
mapper = { type : { name: 'Uuid' } };
|
||||
var serializedObject = msRest.deserialize(mapper, valid_uuid, 'uuidBody');
|
||||
serializedObject.should.equal(valid_uuid);
|
||||
done();
|
||||
});
|
||||
it('should correctly deserialize a composite type', function (done) {
|
||||
var client = new testClient('http://localhost:9090');
|
||||
var product = new client.models['Product']();
|
||||
|
|
Загрузка…
Ссылка в новой задаче