Skip to content

Commit

Permalink
rest(/, /api, /repository, /project): improved rest service (closes #20
Browse files Browse the repository at this point in the history
…, closes #21, closes #16, closes #18, closes #12, closes #17)
  • Loading branch information
jonasfroeller committed Jan 23, 2024
1 parent 85041ad commit eee88de
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 62 deletions.
Binary file modified rest/bun.lockb
Binary file not shown.
2 changes: 2 additions & 0 deletions rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
},
"dependencies": {
"@elysiajs/cors": "^0.8.0",
"@elysiajs/html": "^0.8.0",
"@elysiajs/swagger": "^0.8.3",
"@octokit/graphql-schema": "^14.47.1",
"dotenv": "^16.3.2",
"elysia": "latest",
Expand Down
211 changes: 149 additions & 62 deletions rest/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Elysia } from "elysia"; // https://elysiajs.com/introduction.html
import { cors } from '@elysiajs/cors'; // https://elysiajs.com/plugins/cors.html#cors-plugin
import { Octokit } from "octokit"; // unused: App // https://github.com/octokit/octokit.js
import { GraphqlResponseError } from "@octokit/graphql";
import { Elysia, t } from "elysia"; // https://elysiajs.com/introduction.html
import { cors } from '@elysiajs/cors'; // https://elysiajs.com/plugins/cors.html
import { html } from '@elysiajs/html'; // https://elysiajs.com/plugins/html.html
import { Octokit } from "octokit"; // { App } // https://github.com/octokit/octokit.js
import { GraphqlResponseError } from "@octokit/graphql"; // Testing GraphQL Queries: https://docs.github.com/en/graphql/overview/explorer
import { swagger } from '@elysiajs/swagger'; // https://elysiajs.com/plugins/swagger
import { Organization } from "@octokit/graphql-schema"; // https://www.npmjs.com/package/@octokit/graphql-schema
import 'dotenv/config'; // process.env.<ENV_VAR_NAME>

Expand All @@ -12,13 +14,36 @@ const app = new Elysia()
.use(cors({
origin: 'https://propromo.duckdns.org'
}))
.get("/", () => "Hello Elysia")
.get('/organization/:organization_name', async ({ params: { organization_name } }) => {
try {
const {
organization,
} = await octokit.graphql<{ organization: Organization }>(`{
organization(login: ${"\"" + organization_name + "\""}) {
.use(swagger({
path: "/api"
}))
.use(html())
.get('/', () => `
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
<title>Propromo RestAPI</title>
</head>
<body>
<h1>Propromo API</h1>
<h2>Routes:</h2>
<ul>
<li><a href="/api">swagger rest-api-docs</a></li>
<li><a href="https://propromo.duckdns.org">website</a></li>
</ul>
</body>
</html>`
)
.get('/organization/:organization_name', async ({ params: { organization_name } }: { params: { organization_name: string } }) => {
try {
const {
organization,
} = await octokit.graphql<{ organization: Organization }>(`{
organization(login: "${organization_name}") {
name
description
url
Expand Down Expand Up @@ -50,69 +75,131 @@ const app = new Elysia()
}
}`);

return JSON.stringify(organization, null, 2);
} catch (error) {
if (error instanceof GraphqlResponseError) {
console.log("Request failed:", error.request);
console.log(error.message);
return error;
} else {
console.error("ERROR 500");
return "ERROR 500";
return JSON.stringify(organization, null, 2);
} catch (error) {
if (error instanceof GraphqlResponseError) {
console.log("Request failed:", error.request);
console.log(error.message);
return error;
} else {
console.error("ERROR 500");
return "ERROR 500";
}
}
}
}, {
params: t.Object({
organization_name: t.String()
})
})
.listen(process.env.PORT || 3000);

console.log(
`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`
);

/* Testing query (test on https://docs.github.com/en/graphql/overview/explorer)
query {
repository(owner: "propromo-software", name: "propromo") {
projectV2(number: 1) {
title
shortDescription
url
public
readme
createdAt
updatedAt
items(first: 10) {
totalCount # total issues
}
view(number: 1) {
.get('/organization/:organization_name/repository/:repository_name', async (
{ params: { organization_name, repository_name } }:
{ params: { organization_name: string, repository_name: string } }) => {
try {
const {
organization,
} = await octokit.graphql<{ organization: Organization }>(`{
organization(login: "${organization_name}") {
name
filter
}
creator {
login
}
repositories(first: 10) {
totalCount
nodes {
description
url
repository(name: "${repository_name}") {
name
issues(first: 50, states: [OPEN]) {
languages(first: 10) {
nodes {
name
}
}
open_issues: issues(first: 50, states: [OPEN]) {
totalCount
nodes {
title
}
}
closed_issues: issues(first: 50, states: [CLOSED]) {
totalCount
nodes {
title
}
}
}
}
}`);

return JSON.stringify(organization, null, 2);
} catch (error) {
if (error instanceof GraphqlResponseError) {
console.log("Request failed:", error.request);
console.log(error.message);
return error;
} else {
console.error("ERROR 500");
return "ERROR 500";
}
}
projectsV2(first: 10) {
totalCount
nodes {
number
title
shortDescription
views {
totalCount
}, {
params: t.Object({
organization_name: t.String(),
repository_name: t.String()
})
})
.get('/organization/:organization_name/repository/:repository_name/project/:project_name', async (
{ query, params: { organization_name, repository_name, project_name } }:
{ query: { view: number }, params: { organization_name: string, repository_name: string, project_name: string } }) => {
try {
const {
organization,
} = await octokit.graphql<{ organization: Organization }>(`{
repository(owner: "${organization_name}", name: "${repository_name}") {
projectsV2(query: "${project_name}", first: 1) {
nodes {
title
shortDescription
url
public
createdAt
updatedAt
teams(first: 4) {
nodes {
name
members(first: 32) {
nodes {
name
}
}
}
}
items(first: 100) {
totalCount
}
view(number: ${(query?.view ?? 1)}) {
name
filter
}
}
}
}
}`);

return JSON.stringify(organization, null, 2);
} catch (error) {
if (error instanceof GraphqlResponseError) {
console.log("Request failed:", error.request);
console.log(error.message);
return error;
} else {
console.error("ERROR 500");
return "ERROR 500";
}
}
}
}
*/
}, {
params: t.Object({
organization_name: t.String(),
repository_name: t.String(),
project_name: t.String()
})
})
.listen(process.env.PORT || 3000);

console.log(
`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`
);

0 comments on commit eee88de

Please sign in to comment.