conjure-client
Advanced tools
Comparing version 1.8.0 to 1.8.1
@@ -24,3 +24,2 @@ /** | ||
export interface IFetchResponse extends IFetchBody { | ||
readonly body: ReadableStream<Uint8Array> | null; | ||
readonly headers: Headers; | ||
@@ -54,4 +53,2 @@ readonly ok: boolean; | ||
callEndpoint<T>(params: IHttpEndpointOptions): Promise<T>; | ||
private makeFetchCall; | ||
private handleBinaryResponseBody; | ||
private appendQueryParameter; | ||
@@ -58,0 +55,0 @@ private buildPath; |
@@ -18,12 +18,9 @@ "use strict"; | ||
*/ | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
var __assign = (this && this.__assign) || Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
@@ -68,3 +65,2 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
var httpApiBridge_1 = require("../httpApiBridge"); | ||
var blobReadableStreamAdapter_1 = require("./blobReadableStreamAdapter"); | ||
function formatUserAgent(userAgent) { | ||
@@ -83,31 +79,66 @@ var productName = userAgent.productName, productVersion = userAgent.productVersion; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var fetchPromise, response, contentType, bodyPromise, body, error_1, error_2; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
var query, url, data, _a, headers, method, requestMediaType, responseMediaType, stringifiedHeaders, fetchRequestInit, token, fetchFunction, response, contentType, bodyPromise, body, error_1, error_2; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
fetchPromise = this.makeFetchCall(params); | ||
_a.label = 1; | ||
query = this.buildQueryString(params.queryArguments); | ||
url = this.getBaseUrl() + "/" + this.buildPath(params) + (query.length > 0 ? "?" + query : ""); | ||
data = params.data, _a = params.headers, headers = _a === void 0 ? {} : _a, method = params.method, requestMediaType = params.requestMediaType, responseMediaType = params.responseMediaType; | ||
headers["Fetch-User-Agent"] = formatUserAgent(this.userAgent); | ||
stringifiedHeaders = {}; | ||
// Only send present headers as strings | ||
Object.keys(headers).forEach(function (key) { | ||
var headerValue = headers[key]; | ||
if (headerValue != null) { | ||
stringifiedHeaders[key] = headerValue.toString(); | ||
} | ||
}); | ||
fetchRequestInit = { | ||
credentials: "same-origin", | ||
headers: stringifiedHeaders, | ||
method: method, | ||
}; | ||
token = this.getToken(); | ||
if (token !== undefined) { | ||
fetchRequestInit.headers = __assign({}, fetchRequestInit.headers, { Authorization: "Bearer " + token }); | ||
} | ||
if (requestMediaType != null && requestMediaType !== httpApiBridge_1.MediaType.MULTIPART_FORM_DATA) { | ||
// don't include for form data because we need the browser to fill in the form boundary | ||
fetchRequestInit.headers["Content-Type"] = requestMediaType; | ||
} | ||
if (responseMediaType != null) { | ||
// If an endpoint can return multiple content types, make sure it returns the type that we're expecting | ||
// instead of the default `*/* | ||
fetchRequestInit.headers[FetchBridge.ACCEPT_HEADER] = responseMediaType; | ||
} | ||
if (data != null) { | ||
fetchRequestInit.body = this.handleBody(params); | ||
} | ||
fetchFunction = this.fetch || fetch; | ||
_b.label = 1; | ||
case 1: | ||
_a.trys.push([1, 7, , 8]); | ||
return [4 /*yield*/, fetchPromise]; | ||
_b.trys.push([1, 7, , 8]); | ||
return [4 /*yield*/, fetchFunction(url, fetchRequestInit)]; | ||
case 2: | ||
response = _a.sent(); | ||
response = _b.sent(); | ||
if (response.status === 204) { | ||
// Users of this HTTP bridge are responsible for declaring whether their endpoint might return a 204 | ||
// by including `| undefined` in callEndpoint's generic type param. We choose this over declaring the | ||
// return type of this method as `Promise<T | undefined>` so that API calls which are guaranteed to | ||
// return type of this method as `Promise<T | undefined` so that API calls which are guaranteed to | ||
// resolve to a non-null value (when successful) don't have to deal with an unreachable code path. | ||
return [2 /*return*/, undefined]; | ||
} | ||
contentType = response.headers.get("Content-Type") != null ? response.headers.get("Content-Type") : ""; | ||
if (contentType.includes(httpApiBridge_1.MediaType.APPLICATION_OCTET_STREAM) && params.binaryAsStream) { | ||
return [2 /*return*/, this.handleBinaryResponseBody(response)]; | ||
} | ||
contentType = response.headers.get("Content-Type"); | ||
bodyPromise = void 0; | ||
if (contentType.includes(httpApiBridge_1.MediaType.APPLICATION_JSON)) { | ||
bodyPromise = response.json(); | ||
if (contentType != null) { | ||
if (contentType.includes(httpApiBridge_1.MediaType.APPLICATION_JSON)) { | ||
bodyPromise = response.json(); | ||
} | ||
else if (contentType.includes(httpApiBridge_1.MediaType.APPLICATION_OCTET_STREAM)) { | ||
bodyPromise = response.blob(); | ||
} | ||
else { | ||
bodyPromise = response.text(); | ||
} | ||
} | ||
else if (contentType.includes(httpApiBridge_1.MediaType.APPLICATION_OCTET_STREAM)) { | ||
bodyPromise = response.blob(); | ||
} | ||
else { | ||
@@ -117,11 +148,11 @@ bodyPromise = response.text(); | ||
body = void 0; | ||
_a.label = 3; | ||
_b.label = 3; | ||
case 3: | ||
_a.trys.push([3, 5, , 6]); | ||
_b.trys.push([3, 5, , 6]); | ||
return [4 /*yield*/, bodyPromise]; | ||
case 4: | ||
body = _a.sent(); | ||
body = _b.sent(); | ||
return [3 /*break*/, 6]; | ||
case 5: | ||
error_1 = _a.sent(); | ||
error_1 = _b.sent(); | ||
throw new errors_1.ConjureError(errors_1.ConjureErrorType.Parse, error_1, response.status); | ||
@@ -134,3 +165,3 @@ case 6: | ||
case 7: | ||
error_2 = _a.sent(); | ||
error_2 = _b.sent(); | ||
if (error_2 instanceof errors_1.ConjureError) { | ||
@@ -151,47 +182,2 @@ throw error_2; | ||
}; | ||
FetchBridge.prototype.makeFetchCall = function (params) { | ||
var query = this.buildQueryString(params.queryArguments); | ||
var url = this.getBaseUrl() + "/" + this.buildPath(params) + (query.length > 0 ? "?" + query : ""); | ||
var data = params.data, _a = params.headers, headers = _a === void 0 ? {} : _a, method = params.method, requestMediaType = params.requestMediaType, responseMediaType = params.responseMediaType; | ||
headers["Fetch-User-Agent"] = formatUserAgent(this.userAgent); | ||
var stringifiedHeaders = {}; | ||
// Only send present headers as strings | ||
Object.keys(headers).forEach(function (key) { | ||
var headerValue = headers[key]; | ||
if (headerValue != null) { | ||
stringifiedHeaders[key] = headerValue.toString(); | ||
} | ||
}); | ||
var fetchRequestInit = { | ||
credentials: "same-origin", | ||
headers: stringifiedHeaders, | ||
method: method, | ||
}; | ||
var token = this.getToken(); | ||
if (token !== undefined) { | ||
fetchRequestInit.headers = __assign({}, fetchRequestInit.headers, { Authorization: "Bearer " + token }); | ||
} | ||
if (requestMediaType != null && requestMediaType !== httpApiBridge_1.MediaType.MULTIPART_FORM_DATA) { | ||
// don't include for form data because we need the browser to fill in the form boundary | ||
fetchRequestInit.headers["Content-Type"] = requestMediaType; | ||
} | ||
if (responseMediaType != null) { | ||
// If an endpoint can return multiple content types, make sure it returns the type that we're expecting | ||
// instead of the default `*/* | ||
fetchRequestInit.headers[FetchBridge.ACCEPT_HEADER] = responseMediaType; | ||
} | ||
if (data != null) { | ||
fetchRequestInit.body = this.handleBody(params); | ||
} | ||
var fetchFunction = this.fetch || fetch; | ||
return fetchFunction(url, fetchRequestInit); | ||
}; | ||
FetchBridge.prototype.handleBinaryResponseBody = function (response) { | ||
if (response.body === null) { | ||
return blobReadableStreamAdapter_1.blobToReadableStream(response.blob()); | ||
} | ||
else { | ||
return response.body; | ||
} | ||
}; | ||
FetchBridge.prototype.appendQueryParameter = function (query, key, value) { | ||
@@ -198,0 +184,0 @@ query.push(encodeURIComponent(key) + "=" + encodeURIComponent(value)); |
@@ -40,4 +40,2 @@ /** | ||
data?: any; | ||
/** return binary response as web stream */ | ||
binaryAsStream?: boolean; | ||
} | ||
@@ -44,0 +42,0 @@ export declare enum MediaType { |
{ | ||
"name": "conjure-client", | ||
"version": "1.8.0", | ||
"version": "1.8.1", | ||
"description": "An HTTP bridge library for use in front end applications and generated conjure code", | ||
@@ -28,3 +28,3 @@ "sideEffects": false, | ||
"@types/fetch-mock": "^5.12.2", | ||
"@types/jest": "^24.0.23", | ||
"@types/jest": "^21.1.10", | ||
"chai": "^4.2.0", | ||
@@ -34,3 +34,3 @@ "es6-shim": "^0.35.5", | ||
"fetch-mock": "^5.13.1", | ||
"jest": "^24.9.0", | ||
"jest": "^21.2.1", | ||
"karma": "^2.0.5", | ||
@@ -41,11 +41,11 @@ "karma-chai": "^0.1.0", | ||
"karma-nodeunit": "^0.2.0", | ||
"karma-typescript": "^4.1.1", | ||
"karma-typescript": "^3.0.13", | ||
"mocha": "^5.2.0", | ||
"node-fetch": "^2.6.0", | ||
"node-fetch-polyfill": "^2.0.6", | ||
"npm-run-all": "^4.1.5", | ||
"ts-jest": "^24.1.0", | ||
"ts-jest": "^21.2.4", | ||
"tslint": "^5.15.0", | ||
"tslint-react": "^3.6.0", | ||
"typescript": "~3.2.0", | ||
"whatwg-fetch": "^3.0.0" | ||
"typescript": "~2.9.2", | ||
"whatwg-fetch": "^2.0.4" | ||
}, | ||
@@ -57,6 +57,3 @@ "author": "Palantir Technologies, Inc.", | ||
"url": "git@github.com:palantir/conjure-typescript-runtime.git" | ||
}, | ||
"dependencies": { | ||
"web-streams-polyfill": "^2.0.6" | ||
} | ||
} |
@@ -20,3 +20,2 @@ /** | ||
import { IHttpApiBridge, IHttpEndpointOptions, MediaType } from "../httpApiBridge"; | ||
import { blobToReadableStream } from "./blobReadableStreamAdapter"; | ||
@@ -30,3 +29,2 @@ export interface IFetchBody { | ||
export interface IFetchResponse extends IFetchBody { | ||
readonly body: ReadableStream<Uint8Array> | null; | ||
readonly headers: Headers; | ||
@@ -78,53 +76,2 @@ readonly ok: boolean; | ||
public async callEndpoint<T>(params: IHttpEndpointOptions): Promise<T> { | ||
const fetchPromise = this.makeFetchCall(params); | ||
try { | ||
const response = await fetchPromise; | ||
if (response.status === 204) { | ||
// Users of this HTTP bridge are responsible for declaring whether their endpoint might return a 204 | ||
// by including `| undefined` in callEndpoint's generic type param. We choose this over declaring the | ||
// return type of this method as `Promise<T | undefined>` so that API calls which are guaranteed to | ||
// resolve to a non-null value (when successful) don't have to deal with an unreachable code path. | ||
return undefined!; | ||
} | ||
const contentType = | ||
response.headers.get("Content-Type") != null ? (response.headers.get("Content-Type") as string) : ""; | ||
if (contentType.includes(MediaType.APPLICATION_OCTET_STREAM) && params.binaryAsStream) { | ||
return this.handleBinaryResponseBody(response) as any; | ||
} | ||
let bodyPromise; | ||
if (contentType.includes(MediaType.APPLICATION_JSON)) { | ||
bodyPromise = response.json(); | ||
} else if (contentType.includes(MediaType.APPLICATION_OCTET_STREAM)) { | ||
bodyPromise = response.blob(); | ||
} else { | ||
bodyPromise = response.text(); | ||
} | ||
let body; | ||
try { | ||
body = await bodyPromise; | ||
} catch (error) { | ||
throw new ConjureError(ConjureErrorType.Parse, error, response.status); | ||
} | ||
if (!response.ok) { | ||
throw new ConjureError(ConjureErrorType.Status, undefined, response.status, body); | ||
} | ||
return body; | ||
} catch (error) { | ||
if (error instanceof ConjureError) { | ||
throw error; | ||
} else if (error instanceof TypeError) { | ||
throw new ConjureError(ConjureErrorType.Network, error); | ||
} else { | ||
throw new ConjureError(ConjureErrorType.Other, error); | ||
} | ||
} | ||
} | ||
private makeFetchCall(params: IHttpEndpointOptions): Promise<IFetchResponse> { | ||
const query = this.buildQueryString(params.queryArguments); | ||
@@ -171,10 +118,46 @@ const url = `${this.getBaseUrl()}/${this.buildPath(params)}${query.length > 0 ? `?${query}` : ""}`; | ||
return fetchFunction(url, fetchRequestInit); | ||
} | ||
try { | ||
const response = await fetchFunction(url, fetchRequestInit); | ||
if (response.status === 204) { | ||
// Users of this HTTP bridge are responsible for declaring whether their endpoint might return a 204 | ||
// by including `| undefined` in callEndpoint's generic type param. We choose this over declaring the | ||
// return type of this method as `Promise<T | undefined` so that API calls which are guaranteed to | ||
// resolve to a non-null value (when successful) don't have to deal with an unreachable code path. | ||
return undefined!; | ||
} | ||
private handleBinaryResponseBody(response: IFetchResponse): ReadableStream<Uint8Array> { | ||
if (response.body === null) { | ||
return blobToReadableStream(response.blob()); | ||
} else { | ||
return response.body; | ||
const contentType = response.headers.get("Content-Type"); | ||
let bodyPromise; | ||
if (contentType != null) { | ||
if (contentType.includes(MediaType.APPLICATION_JSON)) { | ||
bodyPromise = response.json(); | ||
} else if (contentType.includes(MediaType.APPLICATION_OCTET_STREAM)) { | ||
bodyPromise = response.blob(); | ||
} else { | ||
bodyPromise = response.text(); | ||
} | ||
} else { | ||
bodyPromise = response.text(); | ||
} | ||
let body; | ||
try { | ||
body = await bodyPromise; | ||
} catch (error) { | ||
throw new ConjureError(ConjureErrorType.Parse, error, response.status); | ||
} | ||
if (!response.ok) { | ||
throw new ConjureError(ConjureErrorType.Status, undefined, response.status, body); | ||
} | ||
return body; | ||
} catch (error) { | ||
if (error instanceof ConjureError) { | ||
throw error; | ||
} else if (error instanceof TypeError) { | ||
throw new ConjureError(ConjureErrorType.Network, error); | ||
} else { | ||
throw new ConjureError(ConjureErrorType.Other, error); | ||
} | ||
} | ||
@@ -181,0 +164,0 @@ } |
@@ -48,5 +48,2 @@ /** | ||
data?: any; | ||
/** return binary response as web stream */ | ||
binaryAsStream?: boolean; | ||
} | ||
@@ -53,0 +50,0 @@ |
@@ -21,5 +21,4 @@ /** | ||
require("whatwg-fetch"); | ||
require("web-streams-polyfill"); | ||
// tslint:enable no-var-requires | ||
window.fetch = jest.fn(); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
0
66799
32
1283
- Removedweb-streams-polyfill@^2.0.6
- Removedweb-streams-polyfill@2.1.1(transitive)