@bufbuild/connect-web
Advanced tools
Comparing version 0.2.1 to 0.3.0
@@ -33,3 +33,4 @@ "use strict"; | ||
class ConnectError extends Error { | ||
constructor(message, code = code_js_1.Code.Unknown, details, metadata) { | ||
constructor(message, code = code_js_1.Code.Unknown, detailsOrMetadata, metadata) { | ||
var _a; | ||
super(createMessage(message, code)); | ||
@@ -41,3 +42,5 @@ this.name = "ConnectError"; | ||
this.code = code; | ||
this.metadata = new Headers(metadata !== null && metadata !== void 0 ? metadata : {}); | ||
// TODO once we remove the deprecated constructor, this can become `new Headers(metadata ?? {})` | ||
const metadataInit = (_a = metadata !== null && metadata !== void 0 ? metadata : (Array.isArray(detailsOrMetadata) ? undefined : detailsOrMetadata)) !== null && _a !== void 0 ? _a : {}; | ||
this.metadata = new Headers(metadataInit); | ||
this.details = []; | ||
@@ -44,0 +47,0 @@ } |
@@ -53,3 +53,3 @@ "use strict"; | ||
var _a; | ||
const response = await fetch(unaryRequest.url, Object.assign(Object.assign({}, unaryRequest.init), { headers: unaryRequest.header, signal: unaryRequest.signal, body: createConnectRequestBody(unaryRequest.message, method.kind, useBinaryFormat, options.jsonOptions) })); | ||
const response = await fetch(unaryRequest.url, Object.assign(Object.assign({}, unaryRequest.init), { headers: unaryRequest.header, signal: unaryRequest.signal, body: createConnectRequestBody(unaryRequest.message, method.kind, useBinaryFormat, options.jsonOptions, options.binaryOptions) })); | ||
const responseType = (_a = response.headers.get("Content-Type")) !== null && _a !== void 0 ? _a : ""; | ||
@@ -99,3 +99,3 @@ if (response.status != 200) { | ||
var _a; | ||
const response = await fetch(unaryRequest.url, Object.assign(Object.assign({}, unaryRequest.init), { headers: unaryRequest.header, signal: unaryRequest.signal, body: createConnectRequestBody(unaryRequest.message, method.kind, useBinaryFormat, options.jsonOptions) })); | ||
const response = await fetch(unaryRequest.url, Object.assign(Object.assign({}, unaryRequest.init), { headers: unaryRequest.header, signal: unaryRequest.signal, body: createConnectRequestBody(unaryRequest.message, method.kind, useBinaryFormat, options.jsonOptions, options.binaryOptions) })); | ||
const responseType = (_a = response.headers.get("Content-Type")) !== null && _a !== void 0 ? _a : ""; | ||
@@ -121,31 +121,36 @@ if (response.status != 200) { | ||
async read() { | ||
const result = await reader.read(); | ||
if (result.done) { | ||
if (!endStreamReceived) { | ||
throw new connect_error_js_1.ConnectError("missing EndStreamResponse"); | ||
try { | ||
const result = await reader.read(); | ||
if (result.done) { | ||
if (!endStreamReceived) { | ||
throw new connect_error_js_1.ConnectError("missing EndStreamResponse"); | ||
} | ||
return { | ||
done: true, | ||
value: undefined, | ||
}; | ||
} | ||
return { | ||
done: true, | ||
value: undefined, | ||
}; | ||
} | ||
if ((result.value.flags & endStreamResponseFlag) === | ||
endStreamResponseFlag) { | ||
endStreamReceived = true; | ||
const endStream = endStreamFromJson(result.value.data); | ||
endStream.metadata.forEach((value, key) => this.trailer.append(key, value)); | ||
if (endStream.error) { | ||
throw endStream.error; | ||
if ((result.value.flags & endStreamResponseFlag) === | ||
endStreamResponseFlag) { | ||
endStreamReceived = true; | ||
const endStream = endStreamFromJson(result.value.data); | ||
endStream.metadata.forEach((value, key) => this.trailer.append(key, value)); | ||
if (endStream.error) { | ||
throw endStream.error; | ||
} | ||
return { | ||
done: true, | ||
value: undefined, | ||
}; | ||
} | ||
return { | ||
done: true, | ||
value: undefined, | ||
done: false, | ||
value: useBinaryFormat | ||
? method.O.fromBinary(result.value.data, options.binaryOptions) | ||
: method.O.fromJsonString(new TextDecoder().decode(result.value.data), options.jsonOptions), | ||
}; | ||
} | ||
return { | ||
done: false, | ||
value: useBinaryFormat | ||
? method.O.fromBinary(result.value.data, options.binaryOptions) | ||
: method.O.fromJsonString(new TextDecoder().decode(result.value.data), options.jsonOptions), | ||
}; | ||
catch (e) { | ||
throw (0, connect_error_js_1.connectErrorFromReason)(e); | ||
} | ||
}, | ||
@@ -167,5 +172,5 @@ }; | ||
*/ | ||
function createConnectRequestBody(message, methodKind, useBinaryFormat, jsonOptions) { | ||
function createConnectRequestBody(message, methodKind, useBinaryFormat, jsonOptions, binaryOptions) { | ||
const encoded = useBinaryFormat | ||
? message.toBinary() | ||
? message.toBinary(binaryOptions) | ||
: message.toJsonString(jsonOptions); | ||
@@ -172,0 +177,0 @@ if (methodKind == protobuf_1.MethodKind.Unary) { |
@@ -141,26 +141,31 @@ "use strict"; | ||
async read() { | ||
const result = await reader.read(); | ||
if (result.done) { | ||
if (messageReceived && !endStreamReceived) { | ||
throw new connect_error_js_1.ConnectError("missing trailers", code_js_1.Code.Internal); | ||
try { | ||
const result = await reader.read(); | ||
if (result.done) { | ||
if (messageReceived && !endStreamReceived) { | ||
throw new connect_error_js_1.ConnectError("missing trailers", code_js_1.Code.Internal); | ||
} | ||
return { | ||
done: true, | ||
value: undefined, | ||
}; | ||
} | ||
if ((result.value.flags & trailerFlag) === trailerFlag) { | ||
endStreamReceived = true; | ||
const trailer = parseGrpcWebTrailerAndExtractError(result.value.data); | ||
trailer.forEach((value, key) => this.trailer.append(key, value)); | ||
return { | ||
done: true, | ||
value: undefined, | ||
}; | ||
} | ||
messageReceived = true; | ||
return { | ||
done: true, | ||
value: undefined, | ||
done: false, | ||
value: method.O.fromBinary(result.value.data, options.binaryOptions), | ||
}; | ||
} | ||
if ((result.value.flags & trailerFlag) === trailerFlag) { | ||
endStreamReceived = true; | ||
const trailer = parseGrpcWebTrailerAndExtractError(result.value.data); | ||
trailer.forEach((value, key) => this.trailer.append(key, value)); | ||
return { | ||
done: true, | ||
value: undefined, | ||
}; | ||
catch (e) { | ||
throw (0, connect_error_js_1.connectErrorFromReason)(e); | ||
} | ||
messageReceived = true; | ||
return { | ||
done: false, | ||
value: method.O.fromBinary(result.value.data, options.binaryOptions), | ||
}; | ||
}, | ||
@@ -167,0 +172,0 @@ }; |
@@ -30,3 +30,4 @@ // Copyright 2021-2022 Buf Technologies, Inc. | ||
export class ConnectError extends Error { | ||
constructor(message, code = Code.Unknown, details, metadata) { | ||
constructor(message, code = Code.Unknown, detailsOrMetadata, metadata) { | ||
var _a; | ||
super(createMessage(message, code)); | ||
@@ -38,3 +39,5 @@ this.name = "ConnectError"; | ||
this.code = code; | ||
this.metadata = new Headers(metadata !== null && metadata !== void 0 ? metadata : {}); | ||
// TODO once we remove the deprecated constructor, this can become `new Headers(metadata ?? {})` | ||
const metadataInit = (_a = metadata !== null && metadata !== void 0 ? metadata : (Array.isArray(detailsOrMetadata) ? undefined : detailsOrMetadata)) !== null && _a !== void 0 ? _a : {}; | ||
this.metadata = new Headers(metadataInit); | ||
this.details = []; | ||
@@ -41,0 +44,0 @@ } |
@@ -50,3 +50,3 @@ // Copyright 2021-2022 Buf Technologies, Inc. | ||
var _a; | ||
const response = await fetch(unaryRequest.url, Object.assign(Object.assign({}, unaryRequest.init), { headers: unaryRequest.header, signal: unaryRequest.signal, body: createConnectRequestBody(unaryRequest.message, method.kind, useBinaryFormat, options.jsonOptions) })); | ||
const response = await fetch(unaryRequest.url, Object.assign(Object.assign({}, unaryRequest.init), { headers: unaryRequest.header, signal: unaryRequest.signal, body: createConnectRequestBody(unaryRequest.message, method.kind, useBinaryFormat, options.jsonOptions, options.binaryOptions) })); | ||
const responseType = (_a = response.headers.get("Content-Type")) !== null && _a !== void 0 ? _a : ""; | ||
@@ -96,3 +96,3 @@ if (response.status != 200) { | ||
var _a; | ||
const response = await fetch(unaryRequest.url, Object.assign(Object.assign({}, unaryRequest.init), { headers: unaryRequest.header, signal: unaryRequest.signal, body: createConnectRequestBody(unaryRequest.message, method.kind, useBinaryFormat, options.jsonOptions) })); | ||
const response = await fetch(unaryRequest.url, Object.assign(Object.assign({}, unaryRequest.init), { headers: unaryRequest.header, signal: unaryRequest.signal, body: createConnectRequestBody(unaryRequest.message, method.kind, useBinaryFormat, options.jsonOptions, options.binaryOptions) })); | ||
const responseType = (_a = response.headers.get("Content-Type")) !== null && _a !== void 0 ? _a : ""; | ||
@@ -118,31 +118,36 @@ if (response.status != 200) { | ||
async read() { | ||
const result = await reader.read(); | ||
if (result.done) { | ||
if (!endStreamReceived) { | ||
throw new ConnectError("missing EndStreamResponse"); | ||
try { | ||
const result = await reader.read(); | ||
if (result.done) { | ||
if (!endStreamReceived) { | ||
throw new ConnectError("missing EndStreamResponse"); | ||
} | ||
return { | ||
done: true, | ||
value: undefined, | ||
}; | ||
} | ||
return { | ||
done: true, | ||
value: undefined, | ||
}; | ||
} | ||
if ((result.value.flags & endStreamResponseFlag) === | ||
endStreamResponseFlag) { | ||
endStreamReceived = true; | ||
const endStream = endStreamFromJson(result.value.data); | ||
endStream.metadata.forEach((value, key) => this.trailer.append(key, value)); | ||
if (endStream.error) { | ||
throw endStream.error; | ||
if ((result.value.flags & endStreamResponseFlag) === | ||
endStreamResponseFlag) { | ||
endStreamReceived = true; | ||
const endStream = endStreamFromJson(result.value.data); | ||
endStream.metadata.forEach((value, key) => this.trailer.append(key, value)); | ||
if (endStream.error) { | ||
throw endStream.error; | ||
} | ||
return { | ||
done: true, | ||
value: undefined, | ||
}; | ||
} | ||
return { | ||
done: true, | ||
value: undefined, | ||
done: false, | ||
value: useBinaryFormat | ||
? method.O.fromBinary(result.value.data, options.binaryOptions) | ||
: method.O.fromJsonString(new TextDecoder().decode(result.value.data), options.jsonOptions), | ||
}; | ||
} | ||
return { | ||
done: false, | ||
value: useBinaryFormat | ||
? method.O.fromBinary(result.value.data, options.binaryOptions) | ||
: method.O.fromJsonString(new TextDecoder().decode(result.value.data), options.jsonOptions), | ||
}; | ||
catch (e) { | ||
throw connectErrorFromReason(e); | ||
} | ||
}, | ||
@@ -163,5 +168,5 @@ }; | ||
*/ | ||
function createConnectRequestBody(message, methodKind, useBinaryFormat, jsonOptions) { | ||
function createConnectRequestBody(message, methodKind, useBinaryFormat, jsonOptions, binaryOptions) { | ||
const encoded = useBinaryFormat | ||
? message.toBinary() | ||
? message.toBinary(binaryOptions) | ||
: message.toJsonString(jsonOptions); | ||
@@ -168,0 +173,0 @@ if (methodKind == MethodKind.Unary) { |
@@ -138,26 +138,31 @@ // Copyright 2021-2022 Buf Technologies, Inc. | ||
async read() { | ||
const result = await reader.read(); | ||
if (result.done) { | ||
if (messageReceived && !endStreamReceived) { | ||
throw new ConnectError("missing trailers", Code.Internal); | ||
try { | ||
const result = await reader.read(); | ||
if (result.done) { | ||
if (messageReceived && !endStreamReceived) { | ||
throw new ConnectError("missing trailers", Code.Internal); | ||
} | ||
return { | ||
done: true, | ||
value: undefined, | ||
}; | ||
} | ||
if ((result.value.flags & trailerFlag) === trailerFlag) { | ||
endStreamReceived = true; | ||
const trailer = parseGrpcWebTrailerAndExtractError(result.value.data); | ||
trailer.forEach((value, key) => this.trailer.append(key, value)); | ||
return { | ||
done: true, | ||
value: undefined, | ||
}; | ||
} | ||
messageReceived = true; | ||
return { | ||
done: true, | ||
value: undefined, | ||
done: false, | ||
value: method.O.fromBinary(result.value.data, options.binaryOptions), | ||
}; | ||
} | ||
if ((result.value.flags & trailerFlag) === trailerFlag) { | ||
endStreamReceived = true; | ||
const trailer = parseGrpcWebTrailerAndExtractError(result.value.data); | ||
trailer.forEach((value, key) => this.trailer.append(key, value)); | ||
return { | ||
done: true, | ||
value: undefined, | ||
}; | ||
catch (e) { | ||
throw connectErrorFromReason(e); | ||
} | ||
messageReceived = true; | ||
return { | ||
done: false, | ||
value: method.O.fromBinary(result.value.data, options.binaryOptions), | ||
}; | ||
}, | ||
@@ -164,0 +169,0 @@ }; |
@@ -29,3 +29,3 @@ import { Code } from "./code.js"; | ||
*/ | ||
readonly details: Pick<Any, "typeUrl" | "value">[]; | ||
details: Pick<Any, "typeUrl" | "value">[]; | ||
/** | ||
@@ -39,2 +39,12 @@ * The error message, but without a status code in front. | ||
name: string; | ||
/** | ||
* Create a new ConnectError. If no code is provided, code "unknown" is | ||
* used. | ||
*/ | ||
constructor(message: string, code?: Code, metadata?: HeadersInit); | ||
/** | ||
* @deprecated We do not support providing error details in the constructor. | ||
* This signature was left here by accident, and will be removed in the next | ||
* release. | ||
*/ | ||
constructor(message: string, code?: Code, details?: AnyMessage[], metadata?: HeadersInit); | ||
@@ -41,0 +51,0 @@ } |
{ | ||
"name": "@bufbuild/connect-web", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"license": "Apache-2.0", | ||
@@ -17,2 +17,3 @@ "repository": { | ||
}, | ||
"main": "./dist/cjs/index.js", | ||
"type": "module", | ||
@@ -25,5 +26,4 @@ "types": "./dist/types/index.d.ts", | ||
}, | ||
"main": "./dist/cjs/index.js", | ||
"peerDependencies": { | ||
"@bufbuild/protobuf": "0.1.1" | ||
"@bufbuild/protobuf": "0.2.0" | ||
}, | ||
@@ -30,0 +30,0 @@ "files": [ |
@@ -27,3 +27,3 @@ # @bufbuild/connect-web | ||
}); | ||
const answer = res.json(); | ||
const answer = await res.json(); | ||
console.log(answer); | ||
@@ -30,0 +30,0 @@ // {sentence: 'When you feel happy, what do you do?'} |
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
157393
3600