
Security News
Google’s OSV Fix Just Added 500+ New Advisories — All Thanks to One Small Policy Change
A data handling bug in OSV.dev caused disputed CVEs to disappear from vulnerability feeds until a recent fix restored over 500 advisories.
@gitbeaker/rest
Advanced tools
A Typed GitLab SDK for Browsers, Node.js, and Deno.
Browsers |
Load @gitbeaker/rest directly from esm.sh
|
---|---|
Deno |
Load @gitbeaker/rest directly from esm.sh
|
Node 18+ |
Install with
|
Instantiate the library using a basic token created in your GitLab Profile
const api = new Gitlab({
token: 'personaltoken',
});
Available instantiating options:
Name | Optional | Default | Description |
---|---|---|---|
host | Yes | https://gitlab.com | Gitlab Instance Host URL |
token | Yes* | N/A | Personal Token. Recommended (one of the three tokens are recommended) |
oauthToken | Yes* | N/A | OAuth Token. Recommended (one of the three tokens are recommended) |
jobToken | Yes* | N/A | CI Job Token. Recommended (one of the three tokens are recommended) |
rejectUnauthorized | Yes | true | Http Certificate setting, Only applies to non-browser releases and HTTPS hosts urls |
sudo | Yes | false | Sudo query parameter |
camelize | Yes | false | Camelizes all response body keys |
requesterFn | No | @gitbeaker/rest & @gitbeaker/cli : fetch-based, The @gitbeaker/core package does not have a default and thus must be set explicitly | Request Library Wrapper |
queryTimeout | Yes | 300000 | Query Timeout in ms |
profileToken | Yes | N/A | Requests Profiles Token |
profileMode | Yes | execution | Requests Profiles Token |
rateLimits | No | DEFAULT_RATE_LIMITS | Global and endpoint specific adjustable rate limits |
rateLimitDuration | No | 60 | Timeout duration when rate limit is reached |
*One of these options should be supplied, as most API requests require authentication.
For simplicity, only the response body is returned from the API methods. However, seeing additional response fields, such as the status, headers, etc., may be helpful. For this purpose, an additional optional parameter, showExpanded
can be passed for most API methods.
For methods that return non-paginated results, the payload has this structure:
type ResponseBodyTypes =
| Record<string, unknown>
| Record<string, unknown>[]
| ReadableStream
| Blob
| string
| string[]
| number
| void
| null;
interface FormattedResponse<T extends ResponseBodyTypes = ResponseBodyTypes> {
body: T;
headers: Record<string, string>;
status: number;
}
For methods that return paginated results, the payload also includes paginated information outlined in the Pagination documentation
Available pagination options:
Name | Keyset | Offset | Type | Default | Description |
---|---|---|---|---|---|
pagination | X | X | 'offset' or 'keyset' | 'offset' | Defines which pagination type should be used |
perPage | X | X | Number | 20 | Amount of results per request |
orderBy | X | String | What field the results should be ordered by | ||
sort | X | 'asc' or 'desc' | 'asc' | The direction of sort for the results | |
maxPages | X | Number | N/A | Maximum amount of requests that should be made | |
page | X | Number | N/A | Specific page to be retrieved | |
showExpanded | X | Boolean | false | Returns with the pagination information in addition to the data |
For any .all() function on a resource, it will return all the items from GitLab. This can be troublesome if there are many items, as the request itself can take a while to be fulfilled. As such, a maxPages option can be passed to limit the scope of the all function.
import { Gitlab } from '@gitbeaker/rest';
const api = new Gitlab({
host: 'http://example.com',
token: 'personaltoken',
});
let projects = await api.Projects.all({ maxPages: 2 });
You can also use this in conjunction with the perPage argument which would override the default of 30 per page set by Gitlab:
import { Gitlab } from '@gitbeaker/rest';
const api = new Gitlab({
host: 'http://example.com',
token: 'personaltoken',
});
let projects = await api.Projects.all({ maxPages: 2, perPage: 40 });
Additionally, if you would like to get back the pagination information, to know how many total pages there are for example, pass the option showExpanded
. If there are multiple results the pagination property will be included as shown below:
...
const { data, paginationInfo } = await api.Projects.all({
perPage:40,
maxPages:2,
showExpanded: true
});
...
This will result in a response in this format:
data: [
...
],
paginationInfo: {
next: 4,
current: 2,
previous: 1,
perPage: 3,
}
Note: Supplying any pagination restrictions is call intensive. Some resources will require many requests which can put a significant load on the GitLab Server. The general best practice would be setting the page request option to only return the first page if all results are not required.
Similarly, support for Keyset pagination can be toggled on by passing a pagination parameter as a query option
const { data } = await api.Projects.all({
pagination: 'keyset',
sort: 'asc',
orderBy: 'created_at',
});
Rate limits are completely customizable, and are used to limit the request rate between consecutive API requests within the library. By default, all non-specified endpoints use a 3000 rps rate limit, while some endpoints have much smaller rates as dictated by the GitLab Docs. See below for the default values:
const DEFAULT_RATE_LIMITS = Object.freeze({
// Default rate limit
'**': 3000,
// Import/Export
'projects/import': 6,
'projects/*/export': 6,
'projects/*/download': 1,
'groups/import': 6,
'groups/*/export': 6,
'groups/*/download': 1,
// Note creation
'projects/*/issues/*/notes': {
method: 'post',
limit: 300,
},
'projects/*/snippets/*/notes': {
method: 'post',
limit: 300,
},
'projects/*/merge_requests/*/notes': {
method: 'post',
limit: 300,
},
'groups/*/epics/*/notes': {
method: 'post',
limit: 300,
},
// Repositories - get file archive
'projects/*/repository/archive*': 5,
// Project Jobs
'projects/*/jobs': 600,
// Member deletion
'projects/*/members': 60,
'groups/*/members': 60,
});
Rate limits can be overridden when instantiating a API wrapper. For ease of use, these limits are configured using glob patterns, and can be formatted in two ways.
You can also override the rateLimitDuration
default value of 60 seconds. This is how long a specific endpoint will be timed out for when its rate limit is reached.
const api = new Gitlab({
token: 'token',
rateLimits: {
'**': 30,
'projects/import/*': 40,
'projects/*/issues/*/notes': {
method: 'post',
limit: 300,
},
},
rateLimitDuration: 30,
});
Request errors are returned back within a plain Error instance, using the cause to hold the original response and a text description of the error pulled from the response's error or message fields if JSON, or its plain text value:
class GitbeakerError extends Error {
constructor(
message: string,
options?: {
cause: {
description: string;
request: Request;
response: Response;
};
},
) {
super(message, options);
this.name = 'GitbeakerError';
}
}
Note, the message is assigned to the Response's statusText
, and the Request and Response types are from the NodeJS API.
Once you have your library instantiated, you can utilize many of the API's functionality:
Using the await/async method
import { Gitlab } from '@gitbeaker/rest';
const api = new Gitlab({
host: 'http://example.com',
token: 'personaltoken',
});
// Listing users
let users = await api.Users.all();
// Or using Promise-Then notation
api.Projects.all().then((projects) => {
console.log(projects);
});
A general rule about all the function parameters:
ie.
import { Projects } from '@gitbeaker/rest';
const projectsAPI = new Projects({
host: 'http://example.com',
token: 'personaltoken',
});
projectsAPI.create({
//options defined in the GitLab API documentation
});
This started as a fork from node-gitlab-legacy but I ended up rewriting much of the code. Here are the original work's contributors.
node-gitlab is another npm package for interacting with the GitLab API. It provides similar functionalities to @gitbeaker/rest, such as project and issue management. However, @gitbeaker/rest is more comprehensive and actively maintained.
gitlab is a simple wrapper around the GitLab API. It offers basic functionalities for interacting with GitLab, but it lacks the extensive feature set and ease of use provided by @gitbeaker/rest.
FAQs
Cross Platform implementation of the GitLab API
The npm package @gitbeaker/rest receives a total of 567,378 weekly downloads. As such, @gitbeaker/rest popularity was classified as popular.
We found that @gitbeaker/rest demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A data handling bug in OSV.dev caused disputed CVEs to disappear from vulnerability feeds until a recent fix restored over 500 advisories.
Research
/Security News
175 malicious npm packages (26k+ downloads) used unpkg CDN to host redirect scripts for a credential-phishing campaign targeting 135+ organizations worldwide.
Security News
Python 3.14 adds template strings, deferred annotations, and subinterpreters, plus free-threaded mode, an experimental JIT, and Sigstore verification.