Sharing the Gitbeaker API client across different functions

I seem to have misunderstood something and now I am confused. I could really use some guidance, please.

Currently, I am utilizing the Gitbeaker API in my Node.js code as I write tests. However, I am encountering failures with the following errors:

ReferenceError: requesterFn must be passed

In my code, I have initialized the API client as follows:

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

I believe I should be using the @gitbeaker/rest, but when I attempt to do so, the TypeScript compiler does not approve of the above code:

'Gitlab' refers to a value, but is being used as a type here. Did you mean 'typeof Gitlab'?

Therefore, I opted to utilize the core package.

The function createGitLabApi looks like this:

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 wish to pass this created API client to functions so that only one client is generated but multiple calls can be initiated.

In my test scenario, I have the following setup:

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

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

(I am still figuring out how to create a mock object of Gitlab to pass it to my getGitLabProjects function and specify different mock responses for this mock, but that's a separate issue)
However, when I run the test, I encounter the error message:

ReferenceError: requesterFn must be passed

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

I assume I should be using @gitbeaker/rest, but then TypeScript complains, and I am unable to set 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, currently, I am somewhat uncertain about how to initialize the Gitbeaker API client. Any hints, please?

Answer №1

Instead of relying on Gitbeaker, I decided to go with a more direct approach and use axios to interact with the GitLab REST API. While Gitbeaker offers numerous features, for me it was simpler and more efficient to just make the calls directly through axios.

Answer №2

The solution I discovered was located within an archived gitbeaker issue. Here is the snippet of code:

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to implement an instance method within a Typescript class for a Node.js application

I am encountering an issue with a callback function in my Typescript project. The problem arises when I try to implement the same functionality in a Node project using Typescript. It seems that when referencing 'this' in Node, it no longer points ...

Variety of type 'class' in Typescript

My goal is to achieve the following: createClass(c:class):SomeInstance { return new class() as SomeInstance; } But I encounter an error that says 'type expected' when I specify the :class part. ...

What is the process of organizing information with http in Angular 2?

Currently, I am working through a heroes tutorial and have made progress with the code so far. You can view the code <a href="https://plnkr.co/edit/YHzyzm6ZXt4ESr76mNuB?preview" rel="nofollow noreferrer">here</a>. My next goal is to implement ...

Setting the value of the allDay tab in the dayView using full calendar and angular2 can be achieved by following these

https://i.sstatic.net/KwzNA.png Displayed above is the day view of my fullcalendar. The issue I am facing is that I am unable to assign a value to be displayed in the all-day tab. Therefore, I am seeking help on how to manually set the value of the all-d ...

Troubleshoot Typescript scripts within a Docker container using VSCode

I've been struggling with this problem for a few hours now and can't find a solution anywhere - My Node API is running in a Docker container, coded in Typescript, and I'm attempting to debug it using VSCode. I am able to establish a connect ...

Establish a new subpage that can have multiple main pages

I am currently working on implementing a navigation feature in an Angular application. I want to be able to access the same page from different "parent" pages while maintaining the navigation bar's awareness of the origin page. For navigation, I am u ...

Initiating a reactive form within a service and then injecting it into a component

How can I set up a reactive form in a service and then inject it into each component? I have two components and a service. Both components share the same form as it is simply for typing in one component and displaying in the other. // ---- Service S ...

Mapping Form Fields (with Formik)

Currently, the Formik/Yup validation setup in my form is working perfectly: export default function AddUserPage() { const [firstName, setFirstName] = useState(""); const [email, setEmail] = useState(""); return ( <div> <Formik ...

Mouse event listener includes timeout function that updates a global variable

I'm currently working on a function that gets activated by a mouse click, but there's a 10-second cooldown period using setTimeout() before it can be triggered again. However, after the timeout, my variable doesn't get set to the correct boo ...

Tips for enhancing code quality and minimizing redundancies

Below is the code snippet for different classes describing models and emitters: export class Findbyobjectidlatest { onChanged = new EventEmitter<Ifindbyobjectidlatest>(); model = <Ifindbyobjectidlatest>{ pagesize: 10 }; emit() { this ...

The error occurred when trying to assign a value to a property 'id' that is not defined

Currently in my Angular 7 project, I am working on posting a request to the server. The data is being collected from an Angular reactive form and needs to follow a specific structure. { "title" : "Test Title", "user": ...

Is the state variable not being properly set by using React's setState within the useCallback() hook?

Within a React FunctionComponent, I have code that follows this pattern: const MyComponent: React.FunctionComponent<ISomeInterface> = ({ someArray, someFunction }) => { const [someStateObjVar, setSomeStateObjVar] = React.useState({}); const [ ...

Attempting to retrieve data from the API, but unfortunately, the information is not appearing on the display

I am facing an issue with my Context API implementation. I have set up the API call to fetch data and use it globally within the state, but for some reason, it is not working as expected. There are no errors showing up in the console. Interestingly, when ...

When working with React and Typescript, I encountered an issue with refs causing a typescript error: "The property 'current' does not exist on the type '(instance: HTMLInputElement | null) => void'"

I am working on a component that requires a ref prop: import React, {forwardRef, ReactElement, ReactNode, HTMLProps, useRef} from 'react' import styles from './index.module.css' import classNames from 'classnames' import { Ico ...

The function Sync in the cp method of fs.default is not a valid function

When attempting to install TurboRepo, I encountered an issue after selecting npm. >>> TURBOREPO >>> Welcome to Turborepo! Let's get you set up with a new codebase. ? Where would you like to create your turborepo? ./my-turborepo ...

Confirm that the setter method was invoked in ts-mockito

Location defines a structure, including a property called "search", as shown below: interface Location { ... search: string; ... } An example service is presented here: export class MyService { constructor(private readonly location: Locati ...

What is the process of deploying Angular Server Side Rendering (SSR) build files to Azure Web App service using FileZilla FTP?

I'm currently working on Angular SSR and utilizing the Angular official sample project for testing purposes. Steps for building the Angular SSR project: Execute npm run build:ssr This will automatically generate the dist folder, which contains both ...

The attribute 'value' is not recognized on this type 'IntrinsicAttributes & ChildrenProps'

Working on a project in React with Typescript is proving to be quite challenging for me. I'm attempting to implement a context to share a useState, but encountering an error when using value={{ isOpen, setIsOpen }}: Type '{ children: ReactNode; v ...

An issue has been identified with React's HTML input maxLength feature where it does not display an error

Within my form, I have an input field that currently does not include validation for a maximum length. <input type="text" className="form-control" id="company" onBlur= ...

What is the reason my Barchart begins on day 2 rather than day 1?

I have a concern regarding the data retrieval process using tanstackquery. Currently, I am facing an issue where the day starts at day 2 instead of day when I attempt to display the data. https://i.sstatic.net/LRTQztFd.png This is how I retrieve my data ...