Comments (2)
The first problem is that you shouldn't reassign the def[Whatever]
variables since we're already returning references to their initial values (which is why you're seeing it only ever returning the first response). The second problem is that you're not throwing an error if the second attempt fails for the same isAuthError(response)
logic. Here's an attempt to fix your interceptor:
export default (): RpcInterceptor => ({
interceptUnary(
next: NextUnaryFn,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
method: MethodInfo<any, any>,
input: object,
options: RpcOptions,
): UnaryCall {
let defHeader = new Deferred<RpcMetadata>();
let defMessage = new Deferred<object>();
let defStatus = new Deferred<RpcStatus>();
let defTrailer = new Deferred<RpcMetadata>();
void (async () => {
try {
const first_try = await next(method, input, options);
// If the response doesn't have an auth error then resolve and exit.
if (!isAuthError(first_try.response)) {
defHeader.resolve(first_try.headers);
defMessage.resolve(first_try.response);
defStatus.resolve(first_try.status);
defTrailer.resolve(first_try.trailers);
return;
}
// Otherwise try again with a reset auth status
console.warn(`[${method.name}] auth error, unauthenticating & retrying...`);
const second_try = await next(
method,
{
...input,
authStatus: {},
},
options,
);
// If the second attempt's response also has an auth error then throw a custom RpcError.
if (isAuthError(second_try.response)) {
const error = new RpcError(
'The request does not have valid authentication credentials for the operation',
'UNAUTHENTICATED',
{...second_try.headers, ...second_try.trailers}
);
error.methodName = method.name;
error.serviceName = method.service.typeName;
throw error;
}
defHeader.resolve(second_try.headers);
defMessage.resolve(second_try.response);
defStatus.resolve(second_try.status);
defTrailer.resolve(second_try.trailers);
} catch (err) {
defHeader.rejectPending(err);
defMessage.rejectPending(err);
defStatus.rejectPending(err);
defTrailer.rejectPending(err);
}
})();
return new UnaryCall(
method,
options.meta ?? {},
input,
defHeader.promise,
defMessage.promise,
defStatus.promise,
defTrailer.promise,
);
},
});
from protobuf-ts.
Worked like a charm. Thank you very much for the pointers. I couldn't wrap my head around the def[Whatever]
variables.
from protobuf-ts.
Related Issues (20)
- 2.9.2 not working with 'exactOptionalPropertyTypes: true' HOT 1
- Release 2.9.3 breaks serialization for me HOT 6
- Generated message classes cannot be subclassed because of missing 'export' HOT 1
- Error decoding from binary: "cant skip wire type 4" HOT 1
- How to debug INVALID_ARGUMENT? HOT 1
- Missing headers on RPCError HOT 8
- rpcError cannot support Chinese
- [Request] Flag to handle google wrapper types HOT 1
- Vite cannot found module HOT 2
- Invalid typescript in descriptor.ts HOT 2
- Typesafe API to retrieve enums from strings HOT 4
- `output_javascript` no longer emits `.d.ts` files HOT 6
- Request: Support for moduleResolution "nodenext" and "node16" HOT 2
- Fails to download v27.0-rc1 (which was just released) HOT 7
- invalid url for latest v27.0-rc1 release HOT 1
- Object generated by protobuf-ts seems not support TWO-WAY binding in vue. HOT 5
- Support for WebTransport/HTTP3 HOT 1
- RPC Output Stream drops data if `AsyncIterator` isn't invoked before response is received
- How can I get the lower snake case when using protoc to generate TypeScript from .proto definition files? HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from protobuf-ts.