在函数之间传递 Gitbeaker API 客户端

问题描述 投票:0回答:1

可能我误解了一些东西,所以我很困惑,但我需要指导。

我在 Node.js 代码中使用

Gitbeaker
API。目前我正在编写测试,但失败并出现以下错误:

ReferenceError: requesterFn must be passed

在代码中,我对 API 客户端进行了以下初始化:

import { Gitlab } from '@gitbeaker/core'
...
const gitLabApi: Gitlab<false> = await createGitLabApi(repoId)

我认为我应该使用

@gitbeaker/rest
,但是当我使用它时,TS编译器对上面的代码不满意:
'Gitlab' refers to a value, but is being used as a type here. Did you mean 'typeof Gitlab'?

所以我想我会使用 core 包。

createGitLabApi
是:

import { Gitlab } from '@gitbeaker/core'
import RepositoryModel from '../models/repository/repositoryModel'

const createGitLabApi = async (repoId: number): Promise<Gitlab<false>> => {

    const repository = await RepositoryModel.findByPk(repoId)
    if (!repository)
        throw new Error(`The repository ID [${repoId}] doesn't exist in database!`)

    return new Gitlab({
        token: repository.token ?? '',
        host: repository.host
    })
}

export default createGitLabApi

我想将这个创建的 API 客户端传递给函数,这样只创建一个客户端,但可以启动多个调用。

在测试中我有以下内容:

const mockedGitLabApi = jest.mocked(new Gitlab({host: '', token: ''}), {shallow: true})

const projects: GitLabProjectType[] = await getGitLabProjects(mockedGitLabApi, path);

(好吧,我仍在努力创建

Gitlab
的模拟对象,以便将其传递给我的
getGitLabProjects
函数,并且可以为此模拟指定不同的模拟响应,但这是一个单独的主题)
当我运行测试时说

ReferenceError: requesterFn must be passed

  83 |
> 84 |     const mockedGitLabApi = jest.mocked(new Gitlab({host: '', token: ''}), {shallow: true})

我认为我应该使用

@gitbeaker/rest
但 Typescript 不高兴,我无法将 API 客户端的类型指定为
Gitlab
->
'Gitlab' refers to a value, but is being used as a type here. Did you mean 'typeof Gitlab'?

所以我有点(?)不知道如何初始化 Gitbeaker API 客户端。 请问有什么提示吗?

typescript gitlab
1个回答
0
投票

我的“解决方案”是放弃使用 Gitbeaker,直接使用 axios 调用 Gitlab REST API。我确信 Gitbeaker 可以提供很多功能,但对我来说,使用 axios 并直接调用 GitLab 更容易。

© www.soinside.com 2019 - 2024. All rights reserved.