Refactor: code cleanup and xss fix (#270)

* fix: remove unnecessary xssable text

* chore: remove typo

* fix: ignored promise

* fix: imports can be summarized and the repository already has the typing

* fix: cleanup and remove domain

* chore: lint

* tests: `assert` is deprecated, replace to `with`

docs reference: https://lint.deno.land/rules/no-import-assertions

* ci: bump checkout and Deno

* chore: fix readme tags

* refactor: remove unnecessary constructor

* chore: remove import duplicate

* refactor: replace sort with toSorted

https://rules.sonarsource.com/typescript/RSPEC-4043/?search=Array-mutating%20methods%20should%20not%20be%20used%20misleadingly

* chore: format
This commit is contained in:
Breno A 2024-06-19 01:37:03 -03:00 • committed by GitHub
parent a50a8b71fd
commit 5aa2caf7c9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 39 additions and 53 deletions

View file

@ -7,19 +7,19 @@ on:
pull_request: pull_request:
branches: branches:
- master - master
workflow_dispatch:
jobs: jobs:
install-dependencies: install-dependencies:
runs-on: ubuntu-latest runs-on: ubuntu-latest
strategy: strategy:
matrix: matrix:
deno-version: [1.36.1] deno-version: [1.43.6]
steps: steps:
- name: Git Checkout Deno Module - name: Git Checkout Deno Module
uses: actions/checkout@v2 uses: actions/checkout@v4
- name: Use Deno Version ${{ matrix.deno-version }} - uses: denoland/setup-deno@v1
uses: denolib/setup-deno@v2
with: with:
deno-version: ${{ matrix.deno-version }} deno-version: ${{ matrix.deno-version }}
- name: Deno format check - name: Deno format check

View file

@ -45,8 +45,8 @@ Read the [.editorconfig](./.editorconfig)
## Pull Requests ## Pull Requests
Pull requests are are always welcome! In general, they should a single concern Pull requests are always welcome! In general, they should a single concern in
in the least number of changed lines as possible. For changes that address core the least number of changed lines as possible. For changes that address core
functionality, it is best to open an issue to discuss your proposal first. I functionality, it is best to open an issue to discuss your proposal first. I
look forward to seeing what you come up with! look forward to seeing what you come up with!

View file

@ -1,36 +1,35 @@
<p align="center"> <div align="center">
<img width="140" src="https://user-images.githubusercontent.com/6661165/91657958-61b4fd00-eb00-11ea-9def-dc7ef5367e34.png" /> <img width="140" src="https://user-images.githubusercontent.com/6661165/91657958-61b4fd00-eb00-11ea-9def-dc7ef5367e34.png" alt="GitHub Profile Trophy"/>
<h2 align="center">GitHub Profile Trophy</h2> <h2 align="center">GitHub Profile Trophy</h2>
<p align="center">🏆 Add dynamically generated GitHub Stat Trophies on your readme</p> <p align="center">🏆 Add dynamically generated GitHub Stat Trophies on your readme</p>
</p> </div>
<p align="center"> <div align="center">
<a href="https://github.com/ryo-ma/github-profile-trophy/issues"> <a href="https://github.com/ryo-ma/github-profile-trophy/issues">
<img src="https://img.shields.io/github/issues/ryo-ma/github-profile-trophy"/> <img src="https://img.shields.io/github/issues/ryo-ma/github-profile-trophy" alt="Issues"/>
</a> </a>
<a href="https://github.com/ryo-ma/github-profile-trophy/network/members"> <a href="https://github.com/ryo-ma/github-profile-trophy/network/members">
<img src="https://img.shields.io/github/forks/ryo-ma/github-profile-trophy"/> <img src="https://img.shields.io/github/forks/ryo-ma/github-profile-trophy" alt="Forks"/>
</a> </a>
<a href="https://github.com/ryo-ma/github-profile-trophy/stargazers"> <a href="https://github.com/ryo-ma/github-profile-trophy/stargazers">
<img src="https://img.shields.io/github/stars/ryo-ma/github-profile-trophy"/> <img src="https://img.shields.io/github/stars/ryo-ma/github-profile-trophy" alt="Stargazers"/>
</a> </a>
<a href="https://github.com/ryo-ma/github-profile-trophy/LICENSE"> <a href="https://github.com/ryo-ma/github-profile-trophy/LICENSE">
<img src="https://img.shields.io/github/license/ryo-ma/github-profile-trophy"/> <img src="https://img.shields.io/github/license/ryo-ma/github-profile-trophy" alt="License"/>
</a>
</p>
<p align="center">
</a> </a>
</div>
<div align="center">
<a href="https://twitter.com/intent/tweet?text=Add%20dynamically%20generated%20GitHub%20Trophy%20on%20your%20readme%0D%0A&url=https%3A%2F%2Fgithub.com%2Fryo-ma%2Fgithub-profile-trophy"> <a href="https://twitter.com/intent/tweet?text=Add%20dynamically%20generated%20GitHub%20Trophy%20on%20your%20readme%0D%0A&url=https%3A%2F%2Fgithub.com%2Fryo-ma%2Fgithub-profile-trophy">
<img src="https://img.shields.io/twitter/url?style=social&url=https%3A%2F%2Fgithub.com%2Fryo-ma%2Fgithub-profile-trophy"/> <img src="https://img.shields.io/twitter/url?style=social&url=https%3A%2F%2Fgithub.com%2Fryo-ma%2Fgithub-profile-trophy" alt="Share"/>
</a> </a>
</p> </div>
<p align="center"> <p align="center">
You can use this service for free. I'm looking for sponsors to help us keep up with this service❤️ You can use this service for free. I'm looking for sponsors to help us keep up with this service❤️
</p> </p>
<p align="center"> <div align="center">
<a href="https://github.com/sponsors/ryo-ma"> <a href="https://github.com/sponsors/ryo-ma">
<img src="https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D%A4&logo=GitHub&color=ff69b4"/> <img src="https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D%A4&logo=GitHub&color=ff69b4" alt="Sponsor"/>
</a> </a>
</p> </div>
# Quick Start # Quick Start
@ -42,7 +41,7 @@ readme, change the `?username=` value to your GitHub's username.
``` ```
<p align="center"> <p align="center">
<img alig src="https://github-profile-trophy.vercel.app/?username=ryo-ma&column=8&rank=SSS,SS,S,AAA,AA,A,B,C" /> <img src="https://github-profile-trophy.vercel.app/?username=ryo-ma&column=8&rank=SSS,SS,S,AAA,AA,A,B,C" />
</p> </p>
## Use theme ## Use theme

View file

@ -117,7 +117,7 @@ async function app(req: Request): Promise<Response> {
const userResponseInfo = await client.requestUserInfo(username); const userResponseInfo = await client.requestUserInfo(username);
if (userResponseInfo instanceof ServiceError) { if (userResponseInfo instanceof ServiceError) {
return new Response( return new Response(
ErrorPage({ error: userResponseInfo, username }).render(), ErrorPage({ error: userResponseInfo }).render(),
{ {
status: userResponseInfo.code, status: userResponseInfo.code,
headers: new Headers({ "Content-Type": "text" }), headers: new Headers({ "Content-Type": "text" }),

View file

@ -14,8 +14,7 @@ import {
} from "../Schemas/index.ts"; } from "../Schemas/index.ts";
import { Retry } from "../Helpers/Retry.ts"; import { Retry } from "../Helpers/Retry.ts";
import { CONSTANTS } from "../utils.ts"; import { CONSTANTS } from "../utils.ts";
import { EServiceKindError } from "../Types/EServiceKindError.ts"; import { EServiceKindError, ServiceError } from "../Types/index.ts";
import { ServiceError } from "../Types/ServiceError.ts";
import { Logger } from "../Helpers/Logger.ts"; import { Logger } from "../Helpers/Logger.ts";
import { requestGithubData } from "./request.ts"; import { requestGithubData } from "./request.ts";
@ -85,7 +84,7 @@ export class GithubApiService extends GithubRepository {
(activity as PromiseFulfilledResult<GitHubUserActivity>).value, (activity as PromiseFulfilledResult<GitHubUserActivity>).value,
(issue as PromiseFulfilledResult<GitHubUserIssue>).value, (issue as PromiseFulfilledResult<GitHubUserIssue>).value,
(pullRequest as PromiseFulfilledResult<GitHubUserPullRequest>).value, (pullRequest as PromiseFulfilledResult<GitHubUserPullRequest>).value,
repository as GitHubUserRepository, repository,
); );
} }
@ -98,15 +97,13 @@ export class GithubApiService extends GithubRepository {
TOKENS.length, TOKENS.length,
CONSTANTS.DEFAULT_GITHUB_RETRY_DELAY, CONSTANTS.DEFAULT_GITHUB_RETRY_DELAY,
); );
const response = await retry.fetch<Promise<T>>(async ({ attempt }) => { return await retry.fetch<Promise<T>>(async ({ attempt }) => {
return await requestGithubData( return await requestGithubData(
query, query,
variables, variables,
TOKENS[attempt], TOKENS[attempt],
); );
}); });
return response;
} catch (error) { } catch (error) {
if (error.cause instanceof ServiceError) { if (error.cause instanceof ServiceError) {
Logger.error(error.cause.message); Logger.error(error.cause.message);

View file

@ -3,17 +3,17 @@ import { assertEquals, returnsNext, soxa, stub } from "../../../deps.ts";
import { GitHubUserRepository } from "../../user_info.ts"; import { GitHubUserRepository } from "../../user_info.ts";
const rateLimitMock = await import("../__mocks__/rateLimitMock.json", { const rateLimitMock = await import("../__mocks__/rateLimitMock.json", {
assert: { type: "json" }, with: { type: "json" },
}); });
const successGithubResponseMock = await import( const successGithubResponseMock = await import(
"../__mocks__/successGithubResponse.json", "../__mocks__/successGithubResponse.json",
{ assert: { type: "json" } } { with: { type: "json" } }
); );
const notFoundGithubResponseMock = await import( const notFoundGithubResponseMock = await import(
"../__mocks__/notFoundUserMock.json", "../__mocks__/notFoundUserMock.json",
{ assert: { type: "json" } } { with: { type: "json" } }
); );
import { ServiceError } from "../../Types/index.ts"; import { ServiceError } from "../../Types/index.ts";

View file

@ -29,7 +29,7 @@ export async function staticRenderRegeneration(
const response = await render(request); const response = await render(request);
if (response.status >= 200 && response.status < 300) { if (response.status >= 200 && response.status < 300) {
cacheManager.save(response); void cacheManager.save(response);
} }
return response; return response;

View file

@ -66,7 +66,7 @@ export class Card {
return row; return row;
} }
private getHeight(row: number) { private getHeight(row: number) {
// Calculate the height of card from turns // Calculate the height of the card from turns
return this.panelSize * row + this.marginHeight * (row - 1); return this.panelSize * row + this.marginHeight * (row - 1);
} }

View file

@ -3,12 +3,11 @@ abstract class BaseError {
readonly message!: string; readonly message!: string;
constructor(readonly content?: string) {} constructor(readonly content?: string) {}
render() { render() {
const page = this.renderPage(); return this.renderPage();
return page;
} }
private renderPage() { private renderPage() {
const htmlPage = `<!DOCTYPE html> return `<!DOCTYPE html>
<html lang="en"><head> <html lang="en"><head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
@ -46,7 +45,7 @@ abstract class BaseError {
border-radius: 5px; border-radius: 5px;
padding: 20px; padding: 20px;
margin-bottom: 20px; margin-bottom: 20px;
box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.1); box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.1);
} }
form { form {
display: flex; display: flex;
@ -108,12 +107,10 @@ abstract class BaseError {
<p style="text-align: center;">${this.content ?? ""}</p> <p style="text-align: center;">${this.content ?? ""}</p>
${ ${
this.content && this.content &&
'<a id="back-link" href="https://github-profile-trophy.vercel.app/">Go back</a>' '<a id="back-link" href="/">Go back</a>'
} }
</body> </body>
</html>`; </html>`;
return htmlPage;
} }
} }

View file

@ -3,10 +3,9 @@ import { Error400, Error404, Error419 } from "../error_page.ts";
interface ErrorPageProps { interface ErrorPageProps {
error: ServiceError; error: ServiceError;
username: string;
} }
export function ErrorPage({ error, username }: ErrorPageProps) { export function ErrorPage({ error }: ErrorPageProps) {
let cause: Error400 | Error404 | Error419 = new Error400(); let cause: Error400 | Error404 | Error419 = new Error400();
if (error.cause === EServiceKindError.RATE_LIMIT) { if (error.cause === EServiceKindError.RATE_LIMIT) {
@ -15,7 +14,7 @@ export function ErrorPage({ error, username }: ErrorPageProps) {
if (error.cause === EServiceKindError.NOT_FOUND) { if (error.cause === EServiceKindError.NOT_FOUND) {
cause = new Error404( cause = new Error404(
"Can not find a user with username: " + username, "Sorry, the user you are looking for was not found.",
); );
} }

View file

@ -26,7 +26,7 @@ export class Trophy {
this.setRank(); this.setRank();
} }
setRank() { setRank() {
const sortedRankConditions = this.rankConditions.sort((a, b) => const sortedRankConditions = this.rankConditions.toSorted((a, b) =>
RANK_ORDER.indexOf(a.rank) - RANK_ORDER.indexOf(b.rank) RANK_ORDER.indexOf(a.rank) - RANK_ORDER.indexOf(b.rank)
); );
// Set the rank that hit the first condition // Set the rank that hit the first condition
@ -314,7 +314,6 @@ export class AccountDurationTrophy extends Trophy {
super(score, rankConditions); super(score, rankConditions);
this.title = "Experience"; this.title = "Experience";
this.filterTitles = ["Experience", "Duration", "Since"]; this.filterTitles = ["Experience", "Duration", "Since"];
// this.hidden = true;
} }
} }

View file

@ -77,7 +77,7 @@ export class TrophyList {
); );
} }
sortByRank() { sortByRank() {
this.trophies = this.trophies.sort((a: Trophy, b: Trophy) => this.trophies = this.trophies.toSorted((a: Trophy, b: Trophy) =>
RANK_ORDER.indexOf(a.rank) - RANK_ORDER.indexOf(b.rank) RANK_ORDER.indexOf(a.rank) - RANK_ORDER.indexOf(b.rank)
); );
} }

View file

@ -1,9 +1,4 @@
export class CustomURLSearchParams extends URLSearchParams { export class CustomURLSearchParams extends URLSearchParams {
constructor(
init?: string[][] | Record<string, string> | string | URLSearchParams,
) {
super(init);
}
getStringValue(key: string, defaultValue: string): string { getStringValue(key: string, defaultValue: string): string {
if (super.has(key)) { if (super.has(key)) {
const param = super.get(key); const param = super.get(key);