如何在红石拦截器中添加 CORS headers?

How do you add CORS headers in Redstone interceptor?

我正在尝试将 CORS headers 添加到传入请求中,但我注意到 app.response.headers 是一个不可变的映射并且 app.request.response 不存在,即使它出现在文档示例。因此,对于 OPTIONS 请求,我使用新的 Shelf 响应进行回复,但我找不到向实际请求的响应添加任何新的 headers 的方法。有什么想法吗?

@app.Interceptor(r"/api/.*", chainIdx: 1)
corsInterceptor() {

    if (app.request.method == "OPTIONS") {
        var response = new shelf.Response.ok("", headers: HEADERS);
        app.chain.interrupt(statusCode: HttpStatus.OK, responseValue: response);
    } else {
    // app.request.response is not available
        app.request.response.headers.add('Access-Control-Allow-Origin', '*');
        app.chain.next();
    }
}

我在拦截器文档中的第一段代码中找到了修复...:)

@app.Interceptor(r"/api/.*", chainIdx: 1)
corsInterceptor() {
    if (app.request.method == "OPTIONS") {
        var response = new shelf.Response.ok("", headers: HEADERS);
        app.chain.interrupt(statusCode: HttpStatus.OK, responseValue: response);
    } else {
        app.chain.next(() => app.response.change(headers: HEADERS));
    }
}

app.chain.next() 可以将回调作为参数,预期为 return Response object。在这种情况下 app.response.change() returns 具有正确 headers 的响应。