quickappcn / issues

快应用开发问题反馈和需求征集
27 stars 2 forks source link

missing http response headers #171

Closed yunyu950908 closed 1 year ago

yunyu950908 commented 1 year ago

快应用包装后的 fetch 没有把该有的 response header 正确返回给开发人员。

server

const fastify = require("fastify")({
  logger: true,
  cors: true,
});

const cors = require("@fastify/cors");

fastify.register(cors, {
  origin: "*",
  methods: ["GET", "POST", "OPTIONS"],
});

fastify.get("/testing", async (req, reply) => {
  reply.header("Content-Type", "application/json");
  reply.header("x-Custom-Type", "application/yaml");
  reply.header("Access-Control-Expose-Headers", "x-Custom-Type");
  return "";
});

// Run the server!
fastify.listen({ port: 3300 }, function (err, address) {
  if (err) {
    fastify.log.error(err);
    process.exit(1);
  }
  // Server is now listening on ${address}
});

client 使用官方提供的包 import fetch from '@system.fetch'

    const res = await fetch.fetch({
      url: 'http://localhost:3300/testing',
    })
    console.log(res.data.headers)

log 结果如下

Connection: "keep-alive"
Content-Length: "0"
Content-Type: "application/json; charset=utf-8"
Date: "Wed, 21 Jun 2023 05:48:56 GMT"
Etag: null
Server: null

期望应当有如下响应头

'x-custom-type': 'application/yaml'

在 devtool 中可以看到得到的 response header 是正确的

HTTP/1.1 200 OK
Connection: keep-alive
Date: Wed, 21 Jun 2023 05:50:21 GMT
Keep-Alive: timeout=72
access-control-allow-origin: *
access-control-expose-headers: x-Custom-Type
content-length: 0
content-type: application/json; charset=utf-8
vary: Origin
x-custom-type: application/yaml
yunyu950908 commented 1 year ago

以上 'x-custom-type': 'application/yaml' 只是示例。实际业务中会有更多的自定义 headers