Merge pull request #2 from amarzavery/stringBody

added support for handling string response body
This commit is contained in:
Amar Zavery 2017-09-16 18:09:20 -07:00 коммит произвёл GitHub
Родитель cda898bb3a cf44b04968
Коммит 9e28513965
12 изменённых файлов: 340 добавлений и 1268 удалений

9
Changelog.md Normal file
Просмотреть файл

@ -0,0 +1,9 @@
### 0.1.0 - 2017-09-16
- Initial version of ms-rest-js
- Provides support for basic credentials
- Supports serialization and deserialization of basic and complex types
- Supports sending requests in the node environment and also in the browser
- Builds the request pipeline by adding predefined filters
- Provides mechanism to add custom flters in the pipeline
- Provides a bundled file named [msRestBundle.js](./msRestBundle.js) that can be used in the browser
- Please take a look at the [samples](./samples) directory for node and browser samples

22
dist/lib/util/utils.js поставляемый
Просмотреть файл

@ -316,10 +316,24 @@ function dispatchRequest(options) {
}
}
catch (err) {
const msg = `Error "${err}" occured while executing JSON.parse on the response body - ${operationResponse.bodyAsText}.`;
const errCode = err.code || "JSON_PARSE_ERROR";
const e = new restError_1.RestError(msg, errCode, res.status, options, res, operationResponse.bodyAsText);
return Promise.reject(e);
const textResponse = operationResponse.bodyAsText;
// As per, http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.25 and
// https://spacetelescope.github.io/understanding-json-schema/reference/type.html JSON has
// ("null", "boolean", "object", "array", "number", or "string"), or "integer" as basic types.
// For JSON.parse() to a parse a string, the input should have double quotes in it like "\"Some Text\"".
// Since this is not the case, we want to make sure that the response body was actually a string
// and not a malformed JSON object or JSON array. If the response body was null like "null" or
// a number like "10" or a boolean like "true" or "false", then JSON.parse() handles those
// scenarios correctly. If the input does not start with "{" or "[" then it is most probably a string.
if (!(textResponse.startsWith("{") || textResponse.startsWith("["))) {
operationResponse.bodyAsJson = textResponse;
}
else {
const msg = `Error "${err}" occured while executing JSON.parse on the response body - ${textResponse}.`;
const errCode = err.code || "JSON_PARSE_ERROR";
const e = new restError_1.RestError(msg, errCode, res.status, options, res, textResponse);
return Promise.reject(e);
}
}
}
return Promise.resolve(operationResponse);

2
dist/lib/util/utils.js.map поставляемый

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

2
dist/test/serializationTests.js поставляемый
Просмотреть файл

@ -7,7 +7,7 @@ const moment = require("moment");
const msRest = require("../lib/msRest");
const should = require("should");
const testClient_1 = require("./data/TestClient/lib/testClient");
const mappers_1 = require("./data/Testclient/lib/models/mappers");
const mappers_1 = require("./data/TestClient/lib/models/mappers");
let Serializer = new msRest.Serializer({});
let valid_uuid = "ceaafd1e-f936-429f-bbfc-82ee75dddc33";
describe("msrest", function () {

Просмотреть файл

@ -313,10 +313,23 @@ export async function dispatchRequest(options: WebResource): Promise<HttpOperati
operationResponse.bodyAsJson = JSON.parse(operationResponse.bodyAsText);
}
} catch (err) {
const msg = `Error "${err}" occured while executing JSON.parse on the response body - ${operationResponse.bodyAsText}.`;
const errCode = err.code || "JSON_PARSE_ERROR";
const e = new RestError(msg, errCode, res.status, options, res, operationResponse.bodyAsText);
return Promise.reject(e);
const textResponse = operationResponse.bodyAsText;
// As per, http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.25 and
// https://spacetelescope.github.io/understanding-json-schema/reference/type.html JSON has
// ("null", "boolean", "object", "array", "number", or "string"), or "integer" as basic types.
// For JSON.parse() to a parse a string, the input should have double quotes in it like "\"Some Text\"".
// Since this is not the case, we want to make sure that the response body was actually a string
// and not a malformed JSON object or JSON array. If the response body was null like "null" or
// a number like "10" or a boolean like "true" or "false", then JSON.parse() handles those
// scenarios correctly. If the input does not start with "{" or "[" then it is most probably a string.
if (!(textResponse.startsWith("{") || textResponse.startsWith("["))) {
operationResponse.bodyAsJson = textResponse;
} else {
const msg = `Error "${err}" occured while executing JSON.parse on the response body - ${textResponse}.`;
const errCode = err.code || "JSON_PARSE_ERROR";
const e = new RestError(msg, errCode, res.status, options, res, textResponse);
return Promise.reject(e);
}
}
}
return Promise.resolve(operationResponse);

Просмотреть файл

@ -387,10 +387,24 @@ function dispatchRequest(options) {
}
}
catch (err) {
const msg = `Error "${err}" occured while executing JSON.parse on the response body - ${operationResponse.bodyAsText}.`;
const errCode = err.code || "JSON_PARSE_ERROR";
const e = new restError_1.RestError(msg, errCode, res.status, options, res, operationResponse.bodyAsText);
return Promise.reject(e);
const textResponse = operationResponse.bodyAsText;
// As per, http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.25 and
// https://spacetelescope.github.io/understanding-json-schema/reference/type.html JSON has
// ("null", "boolean", "object", "array", "number", or "string"), or "integer" as basic types.
// For JSON.parse() to a parse a string, the input should have double quotes in it like "\"Some Text\"".
// Since this is not the case, we want to make sure that the response body was actually a string
// and not a malformed JSON object or JSON array. If the response body was null like "null" or
// a number like "10" or a boolean like "true" or "false", then JSON.parse() handles those
// scenarios correctly. If the input does not start with "{" or "[" then it is most probably a string.
if (!(textResponse.startsWith("{") || textResponse.startsWith("["))) {
operationResponse.bodyAsJson = textResponse;
}
else {
const msg = `Error "${err}" occured while executing JSON.parse on the response body - ${textResponse}.`;
const errCode = err.code || "JSON_PARSE_ERROR";
const e = new restError_1.RestError(msg, errCode, res.status, options, res, textResponse);
return Promise.reject(e);
}
}
}
return Promise.resolve(operationResponse);

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

2
msRestBundle.min.js поставляемый

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

1493
package-lock.json сгенерированный

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -3,17 +3,23 @@
"author": {
"name": "Microsoft Corporation",
"email": "azsdkteam@microsoft.com",
"url": "https://github.com/Azure/azure-sdk-for-node"
"url": "https://github.com/Azure/ms-rest-js"
},
"version": "0.1.0",
"description": "Client Runtime for Node.js client libraries generated using AutoRest",
"description": "Isomorphic client Runtime for Typescript/node.js/browser javascript client libraries generated using AutoRest",
"tags": [
"isomorphic",
"browser",
"javascript",
"node",
"microsoft",
"autorest",
"clientruntime"
],
"keywords": [
"isomorphic",
"browser",
"javascript",
"node",
"microsoft",
"autorest",
@ -26,7 +32,7 @@
"@types/form-data": "^2.2.0",
"@types/node": "^8.0.28",
"@types/node-fetch": "^1.6.7",
"@types/uuid": "^3.4.1",
"@types/uuid": "^3.4.2",
"@types/is-stream": "^1.1.0",
"fetch-cookie": "^0.6.0",
"form-data": "^2.3.1",
@ -41,27 +47,28 @@
"devDependencies": {
"@types/mocha": "^2.2.40",
"@types/should": "^8.1.30",
"mocha": "^3.2.0",
"mocha": "^3.5.3",
"should": "5.2.0",
"ts-loader": "^2.3.7",
"tslint": "^5.7.0",
"typescript": "^2.5.2",
"webpack": "^3.5.6",
"webpack": "^3.6.0",
"uglify-es": "^3.1.0"
},
"homepage": "https://github.com/Azure/azure-sdk-for-node/runtime/ms-rest",
"homepage": "https://github.com/Azure/ms-rest-js",
"repository": {
"type": "git",
"url": "git@github.com:Azure/azure-sdk-for-node.git"
"url": "git@github.com:Azure/ms-rest-js.git"
},
"bugs": {
"url": "http://github.com/Azure/azure-sdk-for-node/issues"
"url": "http://github.com/Azure/ms-rest-js/issues"
},
"scripts": {
"tsc": "tsc -p tsconfig.json",
"test": "npm install && npm -s run-script unit",
"unit": "mocha -t 50000 dist/test",
"test": "npm -s run-script unit",
"unit": "mocha -t 50000 ./dist/test",
"uglify": "node node_modules/uglify-es/bin/uglifyjs --source-map -c -m -o msRestBundle.min.js msRestBundle.js",
"build": "npm -s run-script tsc && webpack && npm -s run-script uglify"
"build": "npm -s run-script tsc && webpack && npm -s run-script uglify",
"tslint": "tslint -p . -c tslint.json --exclude test/**/*.ts"
}
}

Просмотреть файл

@ -7,7 +7,7 @@ import * as msRest from "../lib/msRest";
const should = require("should");
import { TestClient } from "./data/TestClient/lib/testClient";
import { Mappers } from "./data/Testclient/lib/models/mappers";
import { Mappers } from "./data/TestClient/lib/models/mappers";
let Serializer = new msRest.Serializer({});
let valid_uuid = "ceaafd1e-f936-429f-bbfc-82ee75dddc33";