question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unclear how to set `ctx.res` in TypeScript implementation

See original GitHub issue

https://github.com/malijs/mali/pull/53 added TypeScript type definitions, but I’m having trouble figuring out how to use the simple examples from the docs. Following the docs (https://mali.js.org/guide/context.html#context), in my handler I set ctx.res = { foo: 'bar' } to set the response value, which leads to the TypeScript error Type '{ status: ServingStatus; }' is not assignable to type 'GrpcResponse'.

It seems to me the union type of GrpcResponse should include any, because you’re supposed to be able to set the res property to the actual response message / object. https://mali.js.org/guide/context.html#ctx-res

In all other cases set the res property to the actual response message / object in case of UNIRY and REQUEST_STREAM calls, and to the output stream in case of RESPONSE_STREAM calls

Am I understanding this right? What is the proper way to set Context.res in TypeScript?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
yosiatcommented, Mar 19, 2019

Hi @bojand settings the types to any solves the issue currently but no the root issue.

@sykorax wrote here the best solution - https://github.com/malijs/mali/issues/43#issuecomment-472823653

Ideally, I would like to write the next code:

async function sayHello(ctx: Contect<GreetRequest, GreetResponse>) {
  console.log(ctx);
  ctx.res = {message: 'Hello '.concat(ctx.req.name)};
}

if we can make this happen I will try to write typescript generator for protobuf that supports mali convention of ctx

If you want I can submit a PR for this

1reaction
joemckiecommented, Dec 28, 2019

This is still an issue in 0.19.0. GrpcRequest and GrpcResponse are now set to be any, resulting in having to do something like the following:

const request: HelloRequest = ctx.req;

It seems like Context should be generic and can fall back to any if no params are given. This shouldn’t be too hard, if there’s no issues with it I’m happy to submit a PR?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Request Context with TypeScript and Express
A simple implementation involves a piece of middleware along with a Context class that is strongly typed. To begin with, let's implement a ......
Read more >
How to add Typescript definitions to Express req & res
First, make sure you have installed the type definitions for express ( npm install -D @types/express ). Example: // middleware/authCheck.ts ...
Read more >
Documentation - Module Resolution - TypeScript
There are two possible module resolution strategies: Node and Classic. You can use the moduleResolution option to specify the module resolution strategy. If...
Read more >
Announcing TypeScript 4.7 - Microsoft Developer Blogs
To get started using TypeScript, you can get it through NuGet, or use npm with the following command: npm install -D typescript.
Read more >
Develop Simple OpenID Authorization Server with Node.js ...
1 Series: How to Implement OpenID with Node.js & Typescript & MongoDB 2 ... interactionDetails( ctx.req, ctx.res )) as any; if (prompt.name ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found