1

我在 next.js commerce 的构建命令期间收到此错误。

Type error: Argument of type '{ method: string; headers: { 'Content-Type': string; } | { 'Content-Type': string; } | { 'Content-Type': string; length: number; toString(): string; toLocaleString(): string; pop(): string[] | undefined; ... 29 more ...; [Symbol.unscopables](): { ...; }; } | { ...; }; ... 7 more ...; timeout?: number | undefined; }' is not assignable to parameter of type 'FetchOptions'.
  Type '{ method: string; headers: { 'Content-Type': string; } | { 'Content-Type': string; } | { 'Content-Type': string; length: number; toString(): string; toLocaleString(): string; pop(): string[] | undefined; ... 29 more ...; [Symbol.unscopables](): { ...; }; } | { ...; }; ... 7 more ...; timeout?: number | undefined; }' is not assignable to type '{ agent?: Agent | undefined; retry?: RetryOptions | undefined; }'.
    Types of property 'agent' are incompatible.
      Type 'Agent | ((parsedUrl: URL) => Agent) | undefined' is not assignable to type 'Agent | undefined'.

   8 |   async (query: string, { variables, preview } = {}, fetchOptions) => {
   9 |     const config = getConfig()
> 10 |     const res = await fetch(config.commerceUrl, {
     |                                                 ^
  11 |       ...fetchOptions,
  12 |       method: 'POST',
  13 |       headers: {

它抱怨的文件非常简单:

import { FetcherError } from '@commerce/utils/errors'
import type { GraphQLFetcher } from '@commerce/api'
import type { LocalConfig } from '../index'
import fetch from './fetch'

const fetchGraphqlApi: (getConfig: () => LocalConfig) => GraphQLFetcher =
  (getConfig) =>
  async (query: string, { variables, preview } = {}, fetchOptions) => {
    const config = getConfig()
    const res = await fetch(config.commerceUrl, {
      ...fetchOptions,
      method: 'POST',
      headers: {
        ...fetchOptions?.headers,
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        query,
        variables,
      }),
    })

    const json = await res.json()
    if (json.errors) {
      throw new FetcherError({
        errors: json.errors ?? [{ message: 'Failed to fetch for API' }],
        status: res.status,
      })
    }

    return { data: json.data, res }
  }

export default fetchGraphqlApi

这个文件中的类型定义很少,但很明显,它推断出一堆,我真的不知道如何去调试它。我已经更新到最新版本的 node npm 和 typescript。

我是否需要返回并找到所有调用它的位置以查看输入错误的位置?

似乎这可能与"https-proxy-agent模块的一些问题有关,因为我发现了一些相关的错误。 https://github.com/TooTallNate/node-https-proxy-agent/issues/27 https://github.com/TooTallNate/node-https-proxy-agent/issues/114

但我看不出这些解决方案中的任何一个如何修复此代码。

编辑:./fetch.js

import zeitFetch from '@vercel/fetch'
export default zeitFetch()
4

0 回答 0