如何在 serverless.ts 文件中设置 provider.apiGateway.shouldStartNameWithService?

How to set provider.apiGateway.shouldStartNameWithService in a serverless.ts file?

我正在使用新的无服务器 TypeScript monorepo 启动一个新项目!使用 aws-nodejs-typescript 模板,它给出了 serverless.ts 配置文件。几周后,我现在在命令行上从 Serverless 收到以下警告:

Serverless: Deprecation warning: Starting with next major version, API Gateway naming will be changed from “{stage}-{service}” to “{service}-{stage}”.
Set “provider.apiGateway.shouldStartNameWithService” to “true” to adapt to the new behavior now.
More Info: https://www.serverless.com/framework/docs/deprecations/#AWS_API_GATEWAY_NAME_STARTING_WITH_SERVICE

好的!看起来不错,我喜欢新的命名。因为这是一个新项目,最好在我们发布任何东西之前应用新的命名。但是,看起来 TypeScript 定义相当严格,似乎还不允许使用新变量:

Loading failed with: TSError: ⨯ Unable to compile TypeScript:
serverless.ts(44,7): error TS2322: Type ‘{ minimumCompressionSize: number; shouldStartNameWithService: true; }’ is not assignable to type ‘ApiGateway’.
Object literal may only specify known properties, and ‘shouldStartNameWithService’ does not exist in type ‘ApiGateway’.

awsProvider.d.ts(51, 9): The expected type comes from property ‘apiGateway’ which is declared here on type ‘Provider’

有没有办法设置新的 属性 而无需将所有内容都还原为 YAML,这在这一点上会有些痛苦?

更新 1

非常感谢@NexGen 指点!这是显示解决方案的最小 serverless.ts(强调 TS!)。

import type { Serverless, ApiGateway } from 'serverless/aws';

const serverlessConfiguration: Serverless = {
  service: {
    name: 'foo',
  },
  frameworkVersion: '2',
  custom: {
    webpack: {
      webpackConfig: './webpack.config.js',
      packager: 'yarn',
      includeModules: true,
    },
    alerts: {
      stages: ['stage', 'prod'],
      definitions: {
        functionErrors: { treatMissingData: 'notBreaching' },
      },
      alarms: ['functionErrors'],
    },
  },
  package: {
    individually: true,
  },
  plugins: [
    'serverless-webpack',
    'serverless-jest-plugin',
    'serverless-plugin-aws-alerts',
  ],
  provider: {
    name: 'aws',
    runtime: 'nodejs12.x',
    region: 'us-west-2',
    stage: "${opt:stage, 'dev'}",
    apiGateway: {
      minimumCompressionSize: 1024,
      shouldStartNameWithService: true,
    } as ApiGateway,
    environment: {
      AWS_NODEJS_CONNECTION_REUSE_ENABLED: '1',
    },
  },
};

module.exports = serverlessConfiguration;

应用此更改非常简单,您需要做的就是将其添加到您的 serverless.yml 文件中。

provider:
  apiGateway:
    shouldStartNameWithService: true
provider: {
  name: 'aws',
  runtime: 'nodejs12.x',
  apiGateway: {
    shouldStartNameWithService: true
  } as ApiGateway,
  stage: 'dev'
}