feat: basic code and configurations

This commit is contained in:
Riccardo
2023-07-18 21:48:58 +02:00
parent bb34d5a2b5
commit 88854532a5
17 changed files with 4807 additions and 1 deletions

33
.eslintrc.json Normal file
View File

@@ -0,0 +1,33 @@
{
"env": {
"browser": true,
"es2021": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": ["@typescript-eslint", "eslint-plugin-tsdoc"],
"rules": {
"@typescript-eslint/no-unused-vars": "error",
"@typescript-eslint/consistent-type-definitions": ["error", "type"],
"require-jsdoc": [
"error",
{
"require": {
"FunctionDeclaration": true,
"MethodDefinition": true,
"ClassDeclaration": true,
"ArrowFunctionExpression": true,
"FunctionExpression": true
}
}
]
}
}

1
.github/CODEOWNERS vendored Normal file
View File

@@ -0,0 +1 @@
* @RiccardoSenica

51
.github/workflows/main.yml vendored Normal file
View File

@@ -0,0 +1,51 @@
name: Pipeline
on: [push]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
- run: yarn
- run: yarn lint
typecheck:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
- run: yarn
- run: yarn typecheck
audit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
- run: yarn
- run: yarn audit
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
- run: yarn
- run: yarn test
build:
runs-on: ubuntu-latest
needs: [lint, typecheck, audit, test]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
- run: yarn
- run: yarn build

3
.gitignore vendored
View File

@@ -128,3 +128,6 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
# Custom
build

4
.husky/commit-msg Executable file
View File

@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
npx --no-install commitlint --edit $1

7
.husky/pre-commit Executable file
View File

@@ -0,0 +1,7 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
yarn audit
yarn format
yarn lint
yarn typecheck

1
.nvmrc Normal file
View File

@@ -0,0 +1 @@
v18.16.1

6
.prettierrc Normal file
View File

@@ -0,0 +1,6 @@
{
"semi": true,
"trailingComma": "none",
"singleQuote": true,
"printWidth": 80
}

View File

@@ -1 +1,62 @@
# node-with-typescript
# This is a template for a Node project with TypeScript
It contains basic configurations for the following:
- TypeScript (typechecking and building)
- Eslint (linting)
- Prettier (formatting)
- Jest (testing)
- Husky (pre-commit hooks to run linting, typechecking, and commit message format)
- GitHub Actions (CI/CD)
## Possible future changes
- Enforcing commit messages format
- Switching bundler to ESBuild, SWC, or Babel
- Deployment to AWS Lambda or Google Cloud Functions
## Commands
Install dependencies:
`yarn`
Audit:
`yarn audit`
Lint:
`yarn lint`
Typecheck:
`yarn typecheck`
Format:
`yarn format`
Husky hooks:
`yarn prepare`
Test:
`yarn test`
Run in development mode:
`yarn dev`
Build:
`yarn build`
Run:
`node build/index.js`
To commit a WIP skipping checks:
`git commit -m "WIP: <message>" --no-verify`

1
commitlint.config.ts Normal file
View File

@@ -0,0 +1 @@
module.exports = { extends: ['@commitlint/config-conventional'] };

16
jest.config.ts Normal file
View File

@@ -0,0 +1,16 @@
import type { Config } from 'jest';
const config: Config = {
preset: 'ts-jest',
clearMocks: true,
collectCoverage: true,
verbose: true,
moduleFileExtensions: ['js', 'ts'],
collectCoverageFrom: ['src/**/*.ts'],
testMatch: ['**/*.test.ts'],
transform: {
'^.+\\.ts$': '@swc/jest'
}
};
export default config;

51
package.json Normal file
View File

@@ -0,0 +1,51 @@
{
"name": "node-with-typescript",
"version": "1.0.0",
"description": "Backend with NodeJS in TypeScript",
"main": "index.ts",
"repository": "https://github.com/RiccardoSenica/node-with-typescript.git",
"author": "riccardo.s@hey.com",
"license": "MIT",
"scripts": {
"dev": "nodemon --watch 'src/**/*.ts' --exec 'ts-node' src/index.ts",
"build": "tsc",
"lint": "eslint --ext .ts . --fix",
"typecheck": "tsc",
"format": "prettier --config .prettierrc 'src/**/*.ts' --write",
"test": "jest --runInBand",
"prepare": "husky install"
},
"lint-staged": {
"*.ts": [
"eslint --quiet --fix"
],
"*.{json,ts}": [
"prettier --write --ignore-unknown"
]
},
"dependencies": {
"express": "^4.18.2"
},
"devDependencies": {
"@commitlint/cli": "^17.6.6",
"@commitlint/config-conventional": "^17.6.6",
"@swc/core": "^1.3.69",
"@swc/jest": "^0.2.26",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.3",
"@types/node": "^20.4.2",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"eslint": "^8.44.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-tsdoc": "^0.2.17",
"husky": "^8.0.0",
"jest": "^29.6.1",
"lint-staged": "^13.2.3",
"nodemon": "^3.0.1",
"prettier": "^3.0.0",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"typescript": "^5.1.6"
}
}

9
src/call/call.test.ts Normal file
View File

@@ -0,0 +1,9 @@
import { formatResponse } from './call';
describe('call', () => {
it('returns the formatted response string', async () => {
const response = formatResponse('test');
expect(response).toBe('This is the string from GET: test');
});
});

8
src/call/call.ts Normal file
View File

@@ -0,0 +1,8 @@
/**
* Format a response message containing a user input.
* @param string - The user input string.
* @returns The formatted message.
*/
export function formatResponse(str: string) {
return `This is the string from GET: ${str}`;
}

16
src/index.ts Normal file
View File

@@ -0,0 +1,16 @@
import express, { Request, Response } from 'express';
import { formatResponse } from './call/call';
const app = express();
app.get('/', (req: Request, res: Response) => {
if (!req.query.string) {
return res.status(400).send('Missing query parameter: str');
}
return res.send(formatResponse(req.query.string as string));
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});

13
tsconfig.json Normal file
View File

@@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "es2021",
"module": "commonjs",
"esModuleInterop": true,
"strict": true,
"outDir": "./build"
},
"include": [
"src/**/*.ts" // Update this to match your project structure
],
"exclude": ["node_modules"]
}

4525
yarn.lock Normal file

File diff suppressed because it is too large Load Diff