adding api

This commit is contained in:
2025-02-10 18:55:08 -06:00
parent f7273cdc0b
commit 9c8468eca8
28 changed files with 693 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './app.controller';
import { AppService } from './app.service';
describe('AppController', () => {
let appController: AppController;
beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
controllers: [AppController],
providers: [AppService],
}).compile();
appController = app.get<AppController>(AppController);
});
describe('root', () => {
it('should return "Hello World!"', () => {
expect(appController.getHello()).toBe('Hello World!');
});
});
});

17
api/src/app.module.ts Normal file
View File

@@ -0,0 +1,17 @@
import { Module } from '@nestjs/common';
import { GitHubApiModule } from './github/github.module';
import { APP_FILTER } from '@nestjs/core';
import { HttpExceptionFilter } from './filters/http-exception.filter';
@Module({
imports: [
GitHubApiModule
],
providers: [
{
provide: APP_FILTER,
useClass: HttpExceptionFilter
}
]
})
export class AppModule {}

View File

@@ -0,0 +1,4 @@
export default () => ({
githubApiUsername: process.env.GITHUB_API_USERNAME,
githubApiPassword: process.env.GITHUB_API_PAT
})

View File

@@ -0,0 +1,25 @@
import {
ExceptionFilter,
Catch,
ArgumentsHost,
HttpException
} from '@nestjs/common';
import { Request, Response } from 'express';
@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter {
catch(excpetion: HttpException, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
const request = ctx.getRequest<Request>();
const status = excpetion.getStatus();
response.status(status).json({
name: excpetion.cause,
message: excpetion.message,
statusCode: status,
timestamp: new Date().toISOString(),
path: request.url
});
}
}

View File

@@ -0,0 +1,20 @@
export interface Content {
name: string
path: string
sha: string
size: number
url: string
html_url: string
git_url: string
download_url: string
type: string
content: string
encoding: string
_links: Links
}
interface Links {
self: string
git: string
html: string
}

View File

@@ -0,0 +1,120 @@
export interface Repo {
id: number
node_id: string
name: string
full_name: string
private: boolean
owner: Owner
html_url: string
description?: string
fork: boolean
url: string
forks_url: string
keys_url: string
collaborators_url: string
teams_url: string
hooks_url: string
issue_events_url: string
events_url: string
assignees_url: string
branches_url: string
tags_url: string
blobs_url: string
git_tags_url: string
git_refs_url: string
trees_url: string
statuses_url: string
languages_url: string
stargazers_url: string
contributors_url: string
subscribers_url: string
subscription_url: string
commits_url: string
git_commits_url: string
comments_url: string
issue_comment_url: string
contents_url: string
compare_url: string
merges_url: string
archive_url: string
downloads_url: string
issues_url: string
pulls_url: string
milestones_url: string
notifications_url: string
labels_url: string
releases_url: string
deployments_url: string
created_at: string
updated_at: string
pushed_at: string
git_url: string
ssh_url: string
clone_url: string
svn_url: string
homepage?: string
size: number
stargazers_count: number
watchers_count: number
language?: string
has_issues: boolean
has_projects: boolean
has_downloads: boolean
has_wiki: boolean
has_pages: boolean
has_discussions: boolean
forks_count: number
mirror_url: any
archived: boolean
disabled: boolean
open_issues_count: number
license?: License
allow_forking: boolean
is_template: boolean
web_commit_signoff_required: boolean
topics: any[]
visibility: string
forks: number
open_issues: number
watchers: number
default_branch: string
permissions: Permissions
}
interface Owner {
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
}
interface License {
key: string
name: string
spdx_id: string
url: string
node_id: string
}
interface Permissions {
admin: boolean
maintain: boolean
push: boolean
triage: boolean
pull: boolean
}

View File

@@ -0,0 +1,35 @@
import { Controller, Get, HttpException, Query } from '@nestjs/common';
import { GitHubApiService } from './github.service';
import { AxiosError } from 'axios'
import { Content } from './github-content.interface';
import { Repo } from './github-repo.interface';
@Controller('github')
export class GitHubApiController {
constructor(private readonly githubApiService: GitHubApiService) {}
@Get('readme')
async getReadMe(@Query() query: any): Promise<Content> {
try {
const repoName = query['repoName'];
return await this.githubApiService.getReadMeContent(repoName)
} catch (error) {
const axiosError = error as AxiosError;
throw new HttpException(axiosError.message, axiosError.status)
}
}
@Get('repos')
async getRepos(): Promise<Repo[]> {
try {
return await this.githubApiService.getRepos();
} catch (error) {
const axiosError = error as AxiosError
throw new HttpException(axiosError.message, axiosError.status)
}
}
}

View File

@@ -0,0 +1,18 @@
import { Module } from '@nestjs/common';
import { GitHubApiController } from './github.controller';
import { GitHubApiService } from './github.service';
import { ConfigModule } from '@nestjs/config';
import configuration from '../config/configuration';
import { HttpModule } from '@nestjs/axios';
@Module({
imports: [
ConfigModule.forRoot({
load: [configuration]
}),
HttpModule
],
controllers: [GitHubApiController],
providers: [GitHubApiService],
})
export class GitHubApiModule {}

View File

@@ -0,0 +1,39 @@
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { HttpService } from '@nestjs/axios';
import { AxiosResponse } from 'axios';
import { Repo } from './github-repo.interface';
import { Content } from './github-content.interface';
@Injectable()
export class GitHubApiService {
constructor(
private readonly configService: ConfigService,
private readonly httpService: HttpService
) {}
async getReadMeContent(repoName: string): Promise<Content> {
const githubApiUsername = this.configService.get<string>('githubApiUsername')
const response: AxiosResponse = await this.httpService.axiosRef.get(`https://api.github.com/repos/${githubApiUsername}/${repoName}/contents/README.md`, {
auth: {
username: this.configService.get<string>('githubApiUsername'),
password: this.configService.get<string>('githubApiPassword')
}
})
const content: Content = response.data
return content;
}
async getRepos(): Promise<Repo[]> {
const response: AxiosResponse = await this.httpService.axiosRef.get('https://api.github.com/user/repos', {
auth: {
username: this.configService.get<string>('githubApiUsername'),
password: this.configService.get<string>('githubApiPassword')
}
})
const repos: Repo[] = response.data
return repos;
}
}

26
api/src/main.azure.ts Normal file
View File

@@ -0,0 +1,26 @@
import { INestApplication, InternalServerErrorException } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { HttpService } from '@nestjs/axios'
import { HttpExceptionFilter } from './filters/http-exception.filter';
export async function createApp(): Promise<INestApplication> {
const httpService = new HttpService();
const app = await NestFactory.create(AppModule);
app.setGlobalPrefix('api');
app.useGlobalFilters(new HttpExceptionFilter());
httpService.axiosRef.interceptors.response.use(
(response) => {
return response;
},
(error) => {
console.error('Internal server error exception', error);
throw new InternalServerErrorException();
}
);
await app.init();
return app;
}

9
api/src/main.ts Normal file
View File

@@ -0,0 +1,9 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap();