小编Bus*_*men的帖子

Typescript:对象中仅允许大写键

有没有办法构造一个类型,允许对象使用任何类型的键,只要它们是大写的?

我希望这能够通过:

const example: OnlyUppercaseKeys = {
  TEST: 'any string',
  TEST_ME: 'any string'
};
Run Code Online (Sandbox Code Playgroud)

我希望这失败:

const example: OnlyUppercaseKeys = {
  Test: 'any string',
  'test_me': 'any string'
};
Run Code Online (Sandbox Code Playgroud)

我知道有字符串操作类型。我什至可以使用它们来构造一个类型,将任何键转换为大写,但我不知道如何使用将其签入的测试来编写类型Record<???, string>

typescript

5
推荐指数
1
解决办法
1201
查看次数

如何向 NestJS v8 + GraphQL 中的所有响应添加标头?

我想向 NestJS 框架(v8)中的所有响应添加具有值的自定义标头。我认为正确的方法是使用全局拦截器,但我不知道该怎么做。

我正在添加我的拦截器:

app.useGlobalInterceptors(new HeadersInterceptor());
Run Code Online (Sandbox Code Playgroud)

我发现了多种方法,但没有一个有效。最常见的看起来像:

import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';
import { Observable, tap } from 'rxjs';

@Injectable()
export class HeadersInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    return next.handle().pipe(
      tap(() => {
        const res = context.switchToHttp().getResponse();

        res.setHeader('my-global-header', 'important-value');
      }),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

但我收到错误:

res.setHeader is not a function
Run Code Online (Sandbox Code Playgroud)

编辑:从正确的答案来看,我应该提到我也在使用 GraphQL。

javascript response-headers typescript graphql nestjs

0
推荐指数
1
解决办法
2956
查看次数