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 factory from "gitlab-api-async-iterator";
const factory = require("gitlab-api-async-iterator");
const { GitLabAPI, GitLabAPIIterator } = factory({
baseURL: "https://gitlab.com/api/v4/",
privateToken: null,
maxRetries: 5,
});
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 GitLabAPIIterator("/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 GitLabAPIIterator("/users", {
page: 2,
maxPages: Number.MAX_SAFE_INTEGER,
per_page: 20,
search: "Alice",
});
for await (const alice of aliceIterator) {
console.log("Found a Alice", alice);
}
Subclassing the API Iterator:
class PipelineIterator extends GitLabAPIIterator {
constructor(projectId, options) {
super(`/projects/${projectId}/pipelines`, options);
}
}
class PipelineJobIterator extends GitLabAPIIterator {
constructor(projectId, pipelineId, options) {
super(`/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 PipelineIterator(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;
}
}