GitLab API Async Iterator

1.3.1 · active · verified Tue Apr 21

This package provides an asynchronous iterator for the GitLab API, built on top of the popular `axios` HTTP client. It simplifies paginated API responses by allowing developers to iterate through all results using `for await...of` loops, abstracting away the complexities of managing `page` and `per_page` parameters. The current stable version is 1.3.1. While specific release cadence isn't stated, the package is actively maintained given its latest release. A key differentiator is its built-in retry mechanism for common API errors (429, 5xx series) and flexible configuration for GitLab API base URL and private tokens, which can be sourced from environment variables. It also offers a factory function to set up the underlying `axios` instance for direct API calls.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to set up the GitLab API client and use the `GitLabPagedAPIIterator` to fetch and log projects, including handling authentication.

import axios from 'axios';
import { setupGitLabAPI, GitLabPagedAPIIterator } from 'gitlab-api-async-iterator';

// Ensure you have an environment variable like GITLAB_TOKEN set, or pass it directly.
// For demonstration, we'll use a placeholder.
const privateToken = process.env.GITLAB_TOKEN || 'YOUR_PRIVATE_GITLAB_TOKEN';

const GitLabAPI = setupGitLabAPI(axios, {
  baseURL: 'https://gitlab.com/api/v4/',
  privateToken: privateToken,
  maxRetries: 3
});

async function fetchProjects() {
  console.log('Fetching projects...');
  try {
    // Create an iterator for the /projects endpoint
    const projectIterator = new GitLabPagedAPIIterator(GitLabAPI, '/projects', {
      // Optional: search for projects containing 'test'
      search: 'test',
      // Optional: limit to 2 pages max to avoid fetching too much data in example
      maxPages: 2
    });

    let projectCount = 0;
    for await (const project of projectIterator) {
      console.log(`- Project ID: ${project.id}, Name: ${project.name}`);
      projectCount++;
      if (projectCount >= 5) {
        console.log('Showing only first 5 projects for brevity.');
        break; // Stop after a few projects for the example
      }
    }
    console.log(`Successfully fetched ${projectCount} projects.`);
  } catch (error) {
    console.error('Error fetching GitLab projects:', error.message);
    if (error.response && error.response.status === 401) {
      console.error('Check your GitLab private token.');
    }
  }
}

fetchProjects();

view raw JSON →