Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

conjure-client

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

conjure-client - npm Package Compare versions

Comparing version 1.8.1 to 2.0.0

lib/fetchBridge/blobReadableStreamAdapter.d.ts

3

lib/fetchBridge/fetchBridge.d.ts

@@ -24,2 +24,3 @@ /**

export interface IFetchResponse extends IFetchBody {
readonly body: ReadableStream<Uint8Array> | null;
readonly headers: Headers;

@@ -53,2 +54,4 @@ readonly ok: boolean;

callEndpoint<T>(params: IHttpEndpointOptions): Promise<T>;
private makeFetchCall;
private handleBinaryResponseBody;
private appendQueryParameter;

@@ -55,0 +58,0 @@ private buildPath;

144

lib/fetchBridge/fetchBridge.js

@@ -18,9 +18,12 @@ "use strict";

*/
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;
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);
};

@@ -65,2 +68,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {

var httpApiBridge_1 = require("../httpApiBridge");
var blobReadableStreamAdapter_1 = require("./blobReadableStreamAdapter");
function formatUserAgent(userAgent) {

@@ -79,66 +83,31 @@ var productName = userAgent.productName, productVersion = userAgent.productVersion;

return __awaiter(this, void 0, void 0, function () {
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) {
var fetchPromise, response, contentType, bodyPromise, body, error_1, error_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
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;
fetchPromise = this.makeFetchCall(params);
_a.label = 1;
case 1:
_b.trys.push([1, 7, , 8]);
return [4 /*yield*/, fetchFunction(url, fetchRequestInit)];
_a.trys.push([1, 7, , 8]);
return [4 /*yield*/, fetchPromise];
case 2:
response = _b.sent();
response = _a.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");
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)];
}
bodyPromise = void 0;
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();
}
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 {

@@ -148,11 +117,11 @@ bodyPromise = response.text();

body = void 0;
_b.label = 3;
_a.label = 3;
case 3:
_b.trys.push([3, 5, , 6]);
_a.trys.push([3, 5, , 6]);
return [4 /*yield*/, bodyPromise];
case 4:
body = _b.sent();
body = _a.sent();
return [3 /*break*/, 6];
case 5:
error_1 = _b.sent();
error_1 = _a.sent();
throw new errors_1.ConjureError(errors_1.ConjureErrorType.Parse, error_1, response.status);

@@ -165,3 +134,3 @@ case 6:

case 7:
error_2 = _b.sent();
error_2 = _a.sent();
if (error_2 instanceof errors_1.ConjureError) {

@@ -182,2 +151,47 @@ 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) {

@@ -184,0 +198,0 @@ query.push(encodeURIComponent(key) + "=" + encodeURIComponent(value));

@@ -40,2 +40,4 @@ /**

data?: any;
/** return binary response as web stream */
binaryAsStream?: boolean;
}

@@ -42,0 +44,0 @@ export declare enum MediaType {

{
"name": "conjure-client",
"version": "1.8.1",
"version": "2.0.0",
"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": "^21.1.10",
"@types/jest": "^24.0.23",
"chai": "^4.2.0",

@@ -34,3 +34,3 @@ "es6-shim": "^0.35.5",

"fetch-mock": "^5.13.1",
"jest": "^21.2.1",
"jest": "^24.9.0",
"karma": "^2.0.5",

@@ -41,11 +41,11 @@ "karma-chai": "^0.1.0",

"karma-nodeunit": "^0.2.0",
"karma-typescript": "^3.0.13",
"karma-typescript": "^4.1.1",
"mocha": "^5.2.0",
"node-fetch-polyfill": "^2.0.6",
"node-fetch": "^2.6.0",
"npm-run-all": "^4.1.5",
"ts-jest": "^21.2.4",
"ts-jest": "^24.1.0",
"tslint": "^5.15.0",
"tslint-react": "^3.6.0",
"typescript": "~2.9.2",
"whatwg-fetch": "^2.0.4"
"typescript": "~3.2.0",
"whatwg-fetch": "^3.0.0"
},

@@ -57,3 +57,6 @@ "author": "Palantir Technologies, Inc.",

"url": "git@github.com:palantir/conjure-typescript-runtime.git"
},
"dependencies": {
"web-streams-polyfill": "^2.0.6"
}
}

@@ -20,2 +20,3 @@ /**

import { IHttpApiBridge, IHttpEndpointOptions, MediaType } from "../httpApiBridge";
import { blobToReadableStream } from "./blobReadableStreamAdapter";

@@ -29,2 +30,3 @@ export interface IFetchBody {

export interface IFetchResponse extends IFetchBody {
readonly body: ReadableStream<Uint8Array> | null;
readonly headers: Headers;

@@ -76,2 +78,53 @@ 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);

@@ -118,46 +171,10 @@ const url = `${this.getBaseUrl()}/${this.buildPath(params)}${query.length > 0 ? `?${query}` : ""}`;

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!;
}
return fetchFunction(url, fetchRequestInit);
}
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);
}
private handleBinaryResponseBody(response: IFetchResponse): ReadableStream<Uint8Array> {
if (response.body === null) {
return blobToReadableStream(response.blob());
} else {
return response.body;
}

@@ -164,0 +181,0 @@ }

@@ -48,2 +48,5 @@ /**

data?: any;
/** return binary response as web stream */
binaryAsStream?: boolean;
}

@@ -50,0 +53,0 @@

@@ -21,4 +21,5 @@ /**

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc