Skip to content

Built for people who want to own their automations. Join the waitlist for an invite.

Package listing

@kody/gitlab

src/projects/list.ts

51 lines · 1.3 KB · TypeScript
import { pickAuthInput, request } from '../core.ts'
import { asRecord, pickQuery, slimProject } from '../shape.ts'

const QUERY_KEYS = [
	'membership',
	'owned',
	'starred',
	'search',
	'visibility',
	'simple',
	'archived',
	'order_by',
	'sort',
	'page',
	'per_page',
] as const

/**
 * List GitLab projects visible to the selected identity.
 *
 * Defaults to `membership: true` so the call stays scoped to projects the
 * caller can actually use. Pass `membership: false` to search more broadly.
 *
 * @example
 * import listGitlabProjects from 'kody:@kody/gitlab/projects/list'
 * const result = await listGitlabProjects({ search: 'example', per_page: 5 })
 */
export default async function listGitlabProjects(params: Record<string, unknown> = {}) {
	const auth = pickAuthInput(params)
	const query = pickQuery(params, QUERY_KEYS)
	if (query.membership === undefined) query.membership = true

	const response = await request<unknown[]>({
		...auth,
		path: '/projects',
		query,
		throwOnError: true,
	})

	const items = Array.isArray(response.data)
		? response.data.map((item) => slimProject(asRecord(item)))
		: []

	return {
		items,
		page: response.headers['x-page'] ?? null,
		nextPage: response.headers['x-next-page'] ?? null,
		total: response.headers['x-total'] ?? null,
		auth: response.auth,
	}
}