Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | 28x 28x 141x 62x 62x 41x 41x 62x 62x 141x 93x 48x 48x 2x 46x 42x 4x 4x 16x 16x 4x 141x 141x 141x 141x 141x 141x 141x 71x 70x 70x 136x 33x 33x 33x 27x 27x 26x 26x 26x 4x 2x 2x 1x 1x 4x 2x 4x 33x 103x 6x 97x 97x 28x 71x 36x 19x 15x 28x 28x 50x 50x 41x 37x 50x 28x | /**
* GitLab REST API Client
*
* Unified client for GitLab API calls that handles:
* - URL building with base URL from config
* - Query parameters serialization
* - Request body encoding (JSON or form-urlencoded)
* - Response error handling
* - GID cleanup from responses
* - Authentication via enhancedFetch
*/
import { enhancedFetch } from "./fetch";
import { cleanGidsFromObject } from "./idConversion";
type HttpMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
interface RequestOptions {
/** Query parameters - undefined values are filtered out */
query?: Record<string, string | number | boolean | undefined | null>;
/** Request body for POST/PUT/PATCH */
body?: Record<string, unknown> | URLSearchParams | FormData;
/** Content type: 'json' or 'form' for x-www-form-urlencoded (default: 'form') */
contentType?: "json" | "form";
/** Skip GID cleanup from response */
rawResponse?: boolean;
}
/**
* Build query string from params object, filtering out undefined/null values
*/
function buildQueryString(
params?: Record<string, string | number | boolean | undefined | null>
): string {
if (!params) return "";
const searchParams = new URLSearchParams();
for (const [key, value] of Object.entries(params)) {
Eif (value !== undefined && value !== null) {
searchParams.set(key, String(value));
}
}
const str = searchParams.toString();
return str ? `?${str}` : "";
}
/**
* Encode request body based on content type
*/
function encodeBody(
body: Record<string, unknown> | URLSearchParams | FormData | undefined,
contentType: "json" | "form"
): { body?: string | FormData; headers: Record<string, string> } {
if (!body) {
return { headers: {} };
}
// Already encoded
Iif (body instanceof URLSearchParams) {
return {
body: body.toString(),
headers: { "Content-Type": "application/x-www-form-urlencoded" },
};
}
if (body instanceof FormData) {
return {
body,
headers: {}, // Let fetch set Content-Type with boundary
};
}
// Encode as JSON or form
if (contentType === "json") {
return {
body: JSON.stringify(body),
headers: { "Content-Type": "application/json" },
};
}
// Form-urlencoded
const params = new URLSearchParams();
for (const [key, value] of Object.entries(body)) {
Eif (value !== undefined && value !== null) {
params.set(key, String(value));
}
}
return {
body: params.toString(),
headers: { "Content-Type": "application/x-www-form-urlencoded" },
};
}
/**
* Make a GitLab API request
*/
async function request<T>(
method: HttpMethod,
path: string,
options: RequestOptions = {}
): Promise<T> {
const baseUrl = process.env.GITLAB_API_URL ?? "https://gitlab.com";
const queryString = buildQueryString(options.query);
const url = `${baseUrl}/api/v4/${path}${queryString}`;
const { body, headers } = encodeBody(options.body, options.contentType ?? "form");
// For GET requests with no body/headers, don't pass options (matches existing behavior)
const hasBody = !!body;
const hasHeaders = Object.keys(headers).length > 0;
let response: Response;
if (method === "GET" && !hasBody && !hasHeaders) {
response = await enhancedFetch(url);
} else {
const fetchOptions: RequestInit = {
method,
...(hasBody && { body }),
...(hasHeaders && { headers }),
};
response = await enhancedFetch(url, fetchOptions);
}
if (!response.ok) {
let errorDetails = "";
try {
if (typeof response.text === "function") {
const text = await response.text();
if (text.trim()) {
// Try to parse as JSON and extract meaningful error info
const errorResponse = JSON.parse(text) as {
message?: string | { value?: string[] } | Record<string, unknown>;
error?: string;
};
const parts: string[] = [];
Eif (errorResponse.message) {
if (typeof errorResponse.message === "string") {
parts.push(errorResponse.message);
} else if (
typeof errorResponse.message === "object" &&
"value" in errorResponse.message &&
Array.isArray(errorResponse.message.value)
) {
parts.push(errorResponse.message.value.join(", "));
} else {
parts.push(JSON.stringify(errorResponse.message));
}
}
if (errorResponse.error) {
parts.push(errorResponse.error);
}
errorDetails = parts.join(" - ");
}
}
} catch {
// If error response can't be parsed, leave errorDetails empty
}
throw new Error(
`GitLab API error: ${response.status} ${response.statusText}${errorDetails ? ` - ${errorDetails}` : ""}`
);
}
// Handle 204 No Content responses (common for DELETE, some PUT/POST operations)
// Callers expecting void/undefined should use appropriate generic type: gitlab.delete<void>()
// Type assertion is intentional to allow typed handlers to work with void responses
if (response.status === 204) {
return undefined as T;
}
const data = (await response.json()) as T;
return options.rawResponse ? data : cleanGidsFromObject(data);
}
/**
* GitLab API client with typed methods
*/
export const gitlab = {
/**
* GET request
* @example gitlab.get('projects/123/labels', { query: { per_page: 20 } })
*/
get: <T = unknown>(path: string, options?: Omit<RequestOptions, "body" | "contentType">) =>
request<T>("GET", path, options),
/**
* POST request
* @example gitlab.post('projects/123/labels', { body: { name: 'bug', color: '#ff0000' } })
*/
post: <T = unknown>(path: string, options?: RequestOptions) => request<T>("POST", path, options),
/**
* PUT request
* @example gitlab.put('projects/123/labels/1', { body: { color: '#00ff00' } })
*/
put: <T = unknown>(path: string, options?: RequestOptions) => request<T>("PUT", path, options),
/**
* DELETE request
* @example gitlab.delete('projects/123/labels/1')
*/
delete: <T = unknown>(path: string, options?: Omit<RequestOptions, "body" | "contentType">) =>
request<T>("DELETE", path, options),
/**
* PATCH request
* @example gitlab.patch('projects/123', { body: { description: 'New desc' } })
*/
patch: <T = unknown>(path: string, options?: RequestOptions) =>
request<T>("PATCH", path, options),
};
/**
* Helper to build entity paths
*/
export const paths = {
/** Encode path for URL */
encode: (path: string) => encodeURIComponent(path),
/** Projects path */
project: (id: string | number) =>
`projects/${typeof id === "number" ? id : encodeURIComponent(id)}`,
/** Groups path */
group: (id: string | number) => `groups/${typeof id === "number" ? id : encodeURIComponent(id)}`,
/** Namespace (project or group) path based on detection */
namespace: (path: string, entityType: "projects" | "groups") =>
`${entityType}/${encodeURIComponent(path)}`,
};
/**
* Helper to filter options for query params, excluding specified keys
*/
export function toQuery<T extends Record<string, unknown>>(
options: T,
exclude: (keyof T)[] = []
): Record<string, string | number | boolean | undefined> {
const result: Record<string, string | number | boolean | undefined> = {};
for (const [key, value] of Object.entries(options)) {
if (!exclude.includes(key as keyof T) && value !== undefined) {
result[key] = value as string | number | boolean | undefined;
}
}
return result;
}
export default gitlab;
|