fastify / fastify/help

How check the status code with `undici` streaming

Open
#623 3 comments 0 reactions 0 assignees View on GitHub
help wanted
Dominant language
No language data
Stars
68
Forks
8
Avg merge
11h 2m
Merged PRs (30d)
2

Description

## 💬 Question here
Hi there, I am trying to implement the streaming approach as it is shown in [undici docs](https://github.com/nodejs/undici/blob/main/docs/api/Dispatcher.md#example-2---stream-to-fastify-response). The main idea of mine is that I am trying to implement a streaming approach to an external service which responds with 200 and 404 status codes.
A simple example works as expected
```js
const { Pool } = require("undici");
const server = require("fastify")({
logger: {
prettyPrint: {
translateTime: "HH:MM:ss Z",
ignore: "pid,hostname",
},
},
});

const pool = new Pool("https://www.thunderclient.com");

server.route({
url: "/",
method: "GET",
handler: (request, response) => {
pool.stream(
{
path: "/welcome",
method: "GET",
opaque: response,
},
({ opaque }) => {
return opaque.raw;
}
);
},
});

const start = async () => {
try {
await server.listen(3000);
} catch (err) {
server.log.error(err);
process.exit(1);
}
};
start();
````

My main question is how I could check the status code of the streaming response and return a proper status code back to the response:
```js
server.route({
url: "/not-found",
method: "GET",
handler: (request, response) => {
pool.stream(
{
path: "/404",
method: "GET",
opaque: response,
},
({ opaque, statusCode }) => {
// Status code is 404, it should be sent back to fastify along with a custom error
// eg: response.status(404).send({error: "Not found"})
console.log(statusCode);
return opaque.raw;
}
);
},
});
```

Also is it possible to parse the JSON stream and transform the output?
Thanks in advance.

## Your Environment

- *node version*: 17
- *fastify version*: >=3.27.1
- *os*: Mac
- *undici version*: 4.5.1

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.