Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code format #69

Merged
merged 6 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
{
"env": {
"browser": true,
"es2021": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
],
"rules": {
}
"env": {
"browser": true,
"es2021": true
},
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": ["@typescript-eslint"],
"rules": {}
}
8 changes: 4 additions & 4 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

version: 2
updates:
- package-ecosystem: "npm" # See documentation for possible values
directory: "/" # Location of package manifests
target-branch: "dependabot"
- package-ecosystem: 'npm' # See documentation for possible values
directory: '/' # Location of package manifests
target-branch: 'dependabot'
schedule:
interval: "weekly"
interval: 'weekly'
19 changes: 19 additions & 0 deletions .github/workflows/format-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: format-check
on: pull_request
jobs:
check-formatting:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '14'

- name: Install dependencies
run: npm install

- name: Check formatting
run: npm run format:check
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"trailingComma": "es5",
"tabWidth": 2,
"singleQuote": true
}
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
version: "3"
version: '3'

services:
txtdot:
image: ghcr.io/txtdot/txtdot:latest
ports:
- "8080:8080"
- '8080:8080'
restart: unless-stopped
volumes:
- ".env:/app/.env"
- '.env:/app/.env'
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"clean-css-cli": "^5.6.2",
"copyfiles": "^2.4.1",
"eslint": "^8.47.0",
"prettier": "^3.1.0",
"tsc-watch": "^6.0.4",
"typescript": "^5.1.6"
},
Expand All @@ -41,7 +42,9 @@
"start": "cd ./dist && node ./src/app.js",
"start:docker": "node ./src/app.js",
"clean-css": "cleancss --batch static/*.css -o dist/static --batch-suffix \"\"",
"dev": "tsc-watch --onSuccess \"node ./dist/src/app.js\""
"dev": "tsc-watch --onSuccess \"node ./dist/src/app.js\"",
"format": "prettier --write .",
"format:check": "prettier --check ."
},
"keywords": [],
"authors": [
Expand Down
52 changes: 24 additions & 28 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import path from "path";
import path from 'path';

import Fastify from "fastify";
import fastifyStatic from "@fastify/static";
import fastifyView from "@fastify/view";
import fastifySwagger from "@fastify/swagger";
import fastifySwaggerUi from "@fastify/swagger-ui";
import ejs from "ejs";
import Fastify from 'fastify';
import fastifyStatic from '@fastify/static';
import fastifyView from '@fastify/view';
import fastifySwagger from '@fastify/swagger';
import fastifySwaggerUi from '@fastify/swagger-ui';
import ejs from 'ejs';

import indexRoute from "./routes/browser/index";
import getRoute from "./routes/browser/get";
import proxyRoute from "./routes/browser/proxy";
import parseRoute from "./routes/api/parse";
import rawHtml from "./routes/api/raw-html";
import indexRoute from './routes/browser/index';
import getRoute from './routes/browser/get';
import proxyRoute from './routes/browser/proxy';
import parseRoute from './routes/api/parse';
import rawHtml from './routes/api/raw-html';

import publicConfig from "./publicConfig";
import errorHandler from "./errors/handler";
import getConfig from "./config/main";
import publicConfig from './publicConfig';
import errorHandler from './errors/handler';
import getConfig from './config/main';

class App {
async init() {
Expand All @@ -27,8 +27,8 @@ class App {
});

fastify.register(fastifyStatic, {
root: path.join(process.cwd(), "static"),
prefix: "/static/",
root: path.join(process.cwd(), 'static'),
prefix: '/static/',
});

fastify.register(fastifyView, {
Expand All @@ -41,32 +41,28 @@ class App {
await fastify.register(fastifySwagger, {
swagger: {
info: {
title: "TXTDot API",
title: 'TXTDot API',
description: publicConfig.description,
version: publicConfig.version,
},
}
},
});
await fastify.register(fastifySwaggerUi, { routePrefix: "/doc" });
await fastify.register(fastifySwaggerUi, { routePrefix: '/doc' });
}

fastify.register(indexRoute);
fastify.register(getRoute);

if (config.proxy_res)
fastify.register(proxyRoute);
if (config.proxy_res) fastify.register(proxyRoute);

fastify.register(parseRoute);
fastify.register(rawHtml);

fastify.setErrorHandler(errorHandler);

fastify.listen(
{ host: config.host, port: config.port },
(err) => {
err && console.log(err);
}
);
fastify.listen({ host: config.host, port: config.port }, (err) => {
err && console.log(err);
});
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/config/config.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { config } from "dotenv";
import { config } from 'dotenv';

export class ConfigService {
public readonly host: string;
Expand All @@ -10,7 +10,7 @@ export class ConfigService {
constructor() {
config();

this.host = process.env.HOST || "0.0.0.0";
this.host = process.env.HOST || '0.0.0.0';
this.port = Number(process.env.PORT) || 8080;

this.reverse_proxy = this.parseBool(process.env.REVERSE_PROXY, false);
Expand All @@ -21,6 +21,6 @@ export class ConfigService {

parseBool(value: string | undefined, def: boolean): boolean {
if (!value) return def;
return value === "true" || value === "1";
return value === 'true' || value === '1';
}
}
2 changes: 1 addition & 1 deletion src/config/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConfigService } from "./config.service";
import { ConfigService } from './config.service';

let configSvc: ConfigService | undefined;

Expand Down
18 changes: 9 additions & 9 deletions src/errors/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@ export interface IApiError {
}

export const errorSchema = {
type: "object",
type: 'object',
properties: {
code: {
type: "number",
description: "HTTP error code",
type: 'number',
description: 'HTTP error code',
},
name: {
type: "string",
description: "Exception class name",
type: 'string',
description: 'Exception class name',
},
message: {
type: "string",
description: "Exception message",
type: 'string',
description: 'Exception message',
},
},
};

export const errorResponseSchema = {
type: "object",
type: 'object',
properties: {
data: {
type: "object",
type: 'object',
nullable: true,
},
error: errorSchema,
Expand Down
25 changes: 11 additions & 14 deletions src/errors/handler.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { NotHtmlMimetypeError, TxtDotError } from "./main";
import { getFastifyError } from "./validation";
import { FastifyReply, FastifyRequest } from 'fastify';
import { NotHtmlMimetypeError, TxtDotError } from './main';
import { getFastifyError } from './validation';

import { IGetSchema } from "../types/requests/browser";
import getConfig from "../config/main";
import { IGetSchema } from '../types/requests/browser';
import getConfig from '../config/main';

export default function errorHandler(
error: Error,
req: FastifyRequest,
reply: FastifyReply
) {
if (req.originalUrl.startsWith("/api/")) {
if (req.originalUrl.startsWith('/api/')) {
return apiErrorHandler(error, reply);
}

Expand Down Expand Up @@ -43,26 +43,23 @@ function apiErrorHandler(error: Error, reply: FastifyReply) {

function htmlErrorHandler(error: Error, reply: FastifyReply, url: string) {
if (getFastifyError(error)?.statusCode === 400) {
return reply.code(400).view("/templates/error.ejs", {
return reply.code(400).view('/templates/error.ejs', {
url,
code: 400,
description: `Invalid parameter specified: ${error.message}`,
})
});
}

if (error instanceof TxtDotError) {
return reply.code(error.code).view("/templates/error.ejs", {
return reply.code(error.code).view('/templates/error.ejs', {
url,
code: error.code,
description: error.description,
proxyBtn: (
error instanceof NotHtmlMimetypeError &&
getConfig().proxy_res
),
proxyBtn: error instanceof NotHtmlMimetypeError && getConfig().proxy_res,
});
}

return reply.code(500).view("/templates/error.ejs", {
return reply.code(500).view('/templates/error.ejs', {
url,
code: 500,
description: `${error.name}: ${error.message}`,
Expand Down
Loading
Loading