diff --git a/.env.example b/.env.example index 19383ec6..f48c3e3d 100644 --- a/.env.example +++ b/.env.example @@ -6,3 +6,5 @@ NUXT_PUBLIC_SITE_URL= # these are needed for the google calendar api GOOGLE_CALENDAR_ID= GOOGLE_SERVICE_ACCOUNT_CREDENTIALS_JSON= +# needed for the contributors endpoint +GITHUB_APP_KEY= \ No newline at end of file diff --git a/nuxt.config.ts b/nuxt.config.ts index e658862c..7dcdcd5c 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -71,6 +71,9 @@ export default defineNuxtConfig({ googleCalendarId: process.env.GOOGLE_CALENDAR_ID, serviceAccountCredentialsJSON: process.env.GOOGLE_SERVICE_ACCOUNT_CREDENTIALS_JSON, }, + githubAPI: { + githubAppAPIKey: process.env.GITHUB_APP_KEY, + }, }, // route redirects diff --git a/server/api/contributors.get.ts b/server/api/contributors.get.ts new file mode 100644 index 00000000..02d7524c --- /dev/null +++ b/server/api/contributors.get.ts @@ -0,0 +1,42 @@ +import type { + GitHubUser, + RESTApiResponse, +} from '~/types' + +/** + * API endpoint for fetching contributors. + * @example + * // Example usage: + * // GET /api/contributors + */ +export default defineEventHandler(async (event) => { + // we're going to use our key + const { githubAppAPIKey } = useRuntimeConfig(event).githubAPI + + // and the api endpoint + const CONTRIBUTORS_ENDPOINT = 'https://api.github.com/repos/devedmonton/DES-Website/collaborators' + + // make the request + try { + // await the collaborators endpoint from github using nitros fetch + const data: GitHubUser[] = await $fetch(CONTRIBUTORS_ENDPOINT, { + method: 'GET', + headers: { + Authorization: `Bearer ${githubAppAPIKey}`, + }, + }) + const response = >{ + statusCode: 200, + message: 'Contributors fetch successfully', + data, + } + return response + } + catch { + throw createError({ + statusCode: 400, + statusMessage: 'Bad Request', + message: 'Error while fetching contributors.', + }) + } +}) diff --git a/types/index.ts b/types/index.ts index 17988e22..0eb10a98 100644 --- a/types/index.ts +++ b/types/index.ts @@ -21,3 +21,43 @@ export interface Sponsor { link?: string category: 'terabyte' | 'byte' | 'bit' } + +// Interfaces for contributor page. +export interface UserPermissions { + admin: boolean + maintain: boolean + push: boolean + triage: boolean + pull: boolean +} + +export interface GitHubUser { + login: string + id: number + node_id: string + avatar_url: string + gravatar_id: string + url: string + html_url: string + followers_url: string + following_url: string + gists_url: string + starred_url: string + subscriptions_url: string + organizations_url: string + repos_url: string + events_url: string + received_events_url: string + type: string + user_view_type: string + site_admin: boolean + permissions: UserPermissions + role_name: string +} + +// interface for the rest api response. +export interface RESTApiResponse { + statusCode: number + message: string + data: T +}