typescriptgitlab

Passing Gitbeaker API client between functions


Probably I misunderstand something so my confusion but I'd need guidance please.

I am using Gitbeaker API in my Node.js code. Currently I am writing tests but it fails with the following errors:

ReferenceError: requesterFn must be passed

In the code I have the following initialization of the API client:

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

I think I should use the @gitbeaker/rest but when I use that then TS compiler is not happy with the code above: 'Gitlab' refers to a value, but is being used as a type here. Did you mean 'typeof Gitlab'?
So I thought I'll use the core package.

The createGitLabApi is:

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

I want to pass this created API client to functions so only one client is created but multiple calls can be started.

In test I have the following:

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

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

(well, I am still struggling to create a mock object of Gitlab so it can be passed to my getGitLabProjects function and different mock response can be specified of this mock but it is a separate topic)
When I run the test is says

ReferenceError: requesterFn must be passed

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

I presume I should use @gitbeaker/rest but then Typescript is not happy, I cannot specify the type of the API client as Gitlab -> 'Gitlab' refers to a value, but is being used as a type here. Did you mean 'typeof Gitlab'?

So I am slightly (?) at a loss how to initialize the Gitbeaker API client. Any hint please?


Solution

  • My 'solution' was to give up using Gitbeaker and just call the Gitlab REST API directly with axios. I am sure Gitbeaker can give lots of features but to me it was easier just to use axios and call GitLab directly.