npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gitlab-api-async-iterator

v1.3.1

Published

Async iterator for GitLab API based on axios

Downloads

9,150

Readme

gitlab-api-async-iterator

Async iterator for GitLab API based on axios

Usage

This module exposes a factory for creating async iterators for GitLab APIs.

This allows to use APIs pretty efficiently (code-wise).

It has a built-in retry feature in case you hit one of the following API errors: [429, 500, 502, 503, 504]

Instantiation

// es6 / webpack
import axios from "axios";
import {
  setupGitLabAPI,
  GitLabPagedAPIIterator,
} from "gitlab-api-async-iterator";
// CJS
const axios = require("axios");
const {
  setupGitLabAPI,
  GitLabPagedAPIIterator,
} = require("gitlab-api-async-iterator");

const GitLabAPI = setupGitLabAPI(axios, {
  //default values
  baseURL: "https://gitlab.com/api/v4/",
  // If no token is defined, it tries to read process.env.GITLAB_TOKEN || process.env.DANGER_GITLAB_API_TOKEN
  privateToken: null,
  // How often certain failing requests are retried
  maxRetries: 5,
  // Logging some warning messages, e.g. if we hit a retry limit. Default: Noop
  logger: () => {},
});

Usage of the API

let response;
// The GitLabAPI wraps an axios instance, so you could do:
response = await GitLabAPI.get("/user"); // Retrieve current user
response = await GitLabAPI.get("/version"); // Retrieve current GitLab version
response = await GitLabAPI.post("/projects", { name: "foobar" }); // Create a project

Usage of the API Iterator

// The real powerhouse is the GitLabPagedAPIIterator:
const groupIterator = new GitLabPagedAPIIterator(GitLabAPI, "/groups");

// This will paginate through _all_ groups, and you have normal loop controls
for await (const group of groupIterator) {
  //Skip groups starting with 'A'
  if (group.name.startsWith("A")) {
    continue;
  }
  // Stop looping as soon as we hit a group starting with C
  if (group.name.startsWith("C")) {
    break;
  }
  console.log("Group Details:", group);
}

// You can provide options to the group iterator:
const aliceIterator = new GitLabPagedAPIIterator(GitLabAPI, "/users", {
  // General parameters:
  page: 2, // Start with page two
  maxPages: Number.MAX_SAFE_INTEGER, // how many pages to receive at a maximum
  per_page: 20, //Pagination size (GitLab default is 20)
  // Parameters specific to the endpoint
  search: "Alice",
});

for await (const alice of aliceIterator) {
  console.log("Found an Alice", alice);
}

Subclassing the API Iterator:

class PipelineIterator extends GitLabPagedAPIIterator {
  constructor(projectId, options) {
    super(GitLabAPI, `/projects/${projectId}/pipelines`, options);
  }
}

class PipelineJobIterator extends GitLabPagedAPIIterator {
  constructor(projectId, pipelineId, options) {
    super(
      GitLabAPI,
      `/projects/${projectId}/pipelines/${pipelineId}/jobs`,
      options
    );
  }
}

const projectId = "foobar";

const pipelines = new PipelineIterator(projectId);
for await (const pipeline of pipelines) {
  console.log(pipeline);

  const jobs = new PipelineJobIterator(projectId, pipeline.id);

  let found = false;

  for await (const job of jobs) {
    if (job.name.includes("jest")) {
      found = true;
      break;
    }
  }

  if (found) {
    console.log(`${pipeline.web_url} is the first Pipeline with a jest job`);
    break;
  }
}