microsoft-cognitiveservices-speech-sdk
Version:
Microsoft Cognitive Services Speech SDK for JavaScript
1 lines • 6.08 kB
Source Map (JSON)
{"version":3,"sources":["src/common.browser/RestMessageAdapter.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAE/C,oBAAY,eAAe;IACvB,GAAG,QAAQ;IACX,IAAI,SAAS;IACb,MAAM,WAAW;IACjB,IAAI,SAAS;CAChB;AAED,MAAM,WAAW,aAAa;IAC1B,EAAE,EAAE,OAAO,CAAC;IACZ,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,GAAG,CAAC;IACV,OAAO,EAAE,MAAM,CAAC;CACnB;AASD,qBAAa,kBAAkB;IAE3B,OAAO,CAAC,eAAe,CAAU;IACjC,OAAO,CAAC,WAAW,CAA4B;gBAG3C,YAAY,EAAE,eAAe;WAWnB,kBAAkB,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM;IAqB5E,IAAW,OAAO,CAAC,YAAY,EAAE,eAAe,EAG/C;IAEM,UAAU,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI;IAI5C,OAAO,CACV,MAAM,EAAE,eAAe,EACvB,GAAG,EAAE,MAAM,EACX,WAAW,GAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;KAAO,EACxC,IAAI,GAAE,GAAU,GACb,OAAO,CAAC,aAAa,CAAC;IAiD7B,OAAO,CAAC,WAAW;CAKtB","file":"RestMessageAdapter.d.ts","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved.\r\n// Licensed under the MIT license.\r\n\r\nimport bent, { BentResponse, RequestBody } from \"bent\";\r\nimport {\r\n ArgumentNullError,\r\n Deferred\r\n} from \"../common/Exports.js\";\r\nimport { IRequestOptions } from \"./Exports.js\";\r\n\r\nexport enum RestRequestType {\r\n Get = \"GET\",\r\n Post = \"POST\",\r\n Delete = \"DELETE\",\r\n File = \"file\",\r\n}\r\n\r\nexport interface IRestResponse {\r\n ok: boolean;\r\n status: number;\r\n statusText: string;\r\n data: string;\r\n json: any;\r\n headers: string;\r\n}\r\n\r\ninterface JsonError {\r\n error?: {\r\n message: string;\r\n };\r\n}\r\n\r\n// accept rest operations via request method and return abstracted objects from server response\r\nexport class RestMessageAdapter {\r\n\r\n private privIgnoreCache: boolean;\r\n private privHeaders: { [key: string]: string };\r\n\r\n public constructor(\r\n configParams: IRequestOptions\r\n ) {\r\n\r\n if (!configParams) {\r\n throw new ArgumentNullError(\"configParams\");\r\n }\r\n\r\n this.privHeaders = configParams.headers;\r\n this.privIgnoreCache = configParams.ignoreCache;\r\n }\r\n\r\n public static extractHeaderValue(headerKey: string, headers: string): string {\r\n let headerValue: string = \"\";\r\n\r\n try {\r\n const arr = headers.trim().split(/[\\r\\n]+/);\r\n const headerMap: { [key: string]: string } = {};\r\n arr.forEach((line: string): void => {\r\n const parts = line.split(\": \");\r\n const header = parts.shift().toLowerCase();\r\n const value = parts.join(\": \");\r\n headerMap[header] = value;\r\n });\r\n\r\n headerValue = headerMap[headerKey.toLowerCase()];\r\n } catch (e) {\r\n // ignore the error\r\n }\r\n\r\n return headerValue;\r\n }\r\n\r\n public set options(configParams: IRequestOptions) {\r\n this.privHeaders = configParams.headers;\r\n this.privIgnoreCache = configParams.ignoreCache;\r\n }\r\n\r\n public setHeaders(key: string, value: string): void {\r\n this.privHeaders[key] = value;\r\n }\r\n\r\n public request(\r\n method: RestRequestType,\r\n uri: string,\r\n queryParams: { [key: string]: any } = {},\r\n body: any = null,\r\n ): Promise<IRestResponse> {\r\n\r\n const responseReceivedDeferral = new Deferred<IRestResponse>();\r\n\r\n const requestCommand = method === RestRequestType.File ? \"POST\" : method;\r\n const handleRestResponse = (data: BentResponse, j: JsonError = {}): IRestResponse => {\r\n const d: { statusText?: string; statusMessage?: string } = data;\r\n return {\r\n data: JSON.stringify(j),\r\n headers: JSON.stringify(data.headers),\r\n json: j,\r\n ok: data.statusCode >= 200 && data.statusCode < 300,\r\n status: data.statusCode,\r\n statusText: j.error ? j.error.message : d.statusText ? d.statusText : d.statusMessage\r\n };\r\n };\r\n\r\n const send = (postData: RequestBody): void => {\r\n const sendRequest = bent(uri, requestCommand, this.privHeaders, 200, 201, 202, 204, 400, 401, 402, 403, 404);\r\n const params = this.queryParams(queryParams) === \"\" ? \"\" : `?${this.queryParams(queryParams)}`;\r\n sendRequest(params, postData).then( async (data: BentResponse): Promise<void> => {\r\n if (method === RestRequestType.Delete || data.statusCode === 204) {\r\n // No JSON from Delete and reset (204) operations\r\n responseReceivedDeferral.resolve(handleRestResponse(data));\r\n } else {\r\n try {\r\n const j: JsonError = await data.json() as JsonError;\r\n responseReceivedDeferral.resolve(handleRestResponse(data, j));\r\n } catch {\r\n responseReceivedDeferral.resolve(handleRestResponse(data));\r\n }\r\n }\r\n }).catch((error: string): void => {\r\n responseReceivedDeferral.reject(error);\r\n });\r\n };\r\n\r\n if (this.privIgnoreCache) {\r\n this.privHeaders[\"Cache-Control\"] = \"no-cache\";\r\n }\r\n\r\n if (method === RestRequestType.Post && body) {\r\n this.privHeaders[\"content-type\"] = \"application/json\";\r\n this.privHeaders[\"Content-Type\"] = \"application/json\";\r\n }\r\n send(body as RequestBody);\r\n return responseReceivedDeferral.promise;\r\n }\r\n\r\n private queryParams(params: { [key: string]: string } = {}): string {\r\n return Object.keys(params)\r\n .map((k: string): string => encodeURIComponent(k) + \"=\" + encodeURIComponent(params[k]))\r\n .join(\"&\");\r\n }\r\n}\r\n"]}