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
import axios from "axios";
import {
setupGitLabAPI,
GitLabPagedAPIIterator,
} from "gitlab-api-async-iterator";
const axios = require("axios");
const {
setupGitLabAPI,
GitLabPagedAPIIterator,
} = require("gitlab-api-async-iterator");
const GitLabAPI = setupGitLabAPI(axios, {
baseURL: "https://gitlab.com/api/v4/",
privateToken: null,
maxRetries: 5,
logger: () => {},
});
Usage of the API
let response;
response = await GitLabAPI.get("/user");
response = await GitLabAPI.get("/version");
response = await GitLabAPI.post("/projects", { name: "foobar" });
Usage of the API Iterator
const groupIterator = new GitLabPagedAPIIterator(GitLabAPI, "/groups");
for await (const group of groupIterator) {
if (group.name.startsWith("A")) {
continue;
}
if (group.name.startsWith("C")) {
break;
}
console.log("Group Details:", group);
}
const aliceIterator = new GitLabPagedAPIIterator(GitLabAPI, "/users", {
page: 2,
maxPages: Number.MAX_SAFE_INTEGER,
per_page: 20,
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;
}
}