first commit

This commit is contained in:
JasterV 2021-07-24 01:37:17 +02:00
commit 9f418f84d1
21 changed files with 15130 additions and 0 deletions

10
.env.example Normal file
View file

@ -0,0 +1,10 @@
FB_CERT_TYPE=
FB_CERT_PROJECT_ID=
FB_CERT_PRIVATE_KEY_ID=
FB_CERT_PRIVATE_KEY=
FB_CERT_CLIENT_EMAIL=
FB_CERT_CLIENT_ID=
FB_CERT_AUTH_URI=
FB_CERT_TOKEN_URI=
FB_CERT_AUTH_PROVIDER_X_509_CERT_URL=
FB_CERT_CLIENT_X_509_CERT_URL=

20
.eslintrc.json Normal file
View file

@ -0,0 +1,20 @@
{
"env": {
"es2020": true,
"node": true,
"jest/globals": true
},
"extends": [
"airbnb-base"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 12,
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
],
"rules": {
}
}

31
.github/workflows/node.js.yml vendored Normal file
View file

@ -0,0 +1,31 @@
# This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
name: Node.js CI
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x, 16.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build --if-present
- run: npm test

125
.gitignore vendored Normal file
View file

@ -0,0 +1,125 @@
# Created by https://www.toptal.com/developers/gitignore/api/node
# Edit at https://www.toptal.com/developers/gitignore?templates=node
### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
.env.test
.env.production
# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
# End of https://www.toptal.com/developers/gitignore/api/node

7
.husky/pre-commit Executable file
View file

@ -0,0 +1,7 @@
#!/bin/sh
[ -n "$CI" ] && exit 0
. "$(dirname "$0")/_/husky.sh"
npm test

70
README.md Normal file
View file

@ -0,0 +1,70 @@
<h1 align="center">Welcome to node ts template 👋</h1>
<p>
<a href="https://mit-license.org/" target="_blank">
<img alt="License: MIT" src="https://img.shields.io/badge/License-MIT-yellow.svg" />
</a>
</p>
> A template for typescript nodejs projects with configuration files such as
> tsconfig & eslint.
## ESLint
Extends the airbnb javascript styleguide adding also typescript support.
Generated with `npx eslint --init`
## tsconfig.json
This tsconfig.json comes from
[Ben awad recommended tsconfig](https://github.com/benawad/tsconfig.json) Thanks
Ben <3
## Jest
jest.json configured to handle *.test.ts files under the tests & src folders.
Also we use ts-jest to run our tests without compiling typescript to javascript
## package.json
I use ts-node-dev to run my code on development mode as it is super fast (sorry
nodemon I don't need you anymore)
## Prettier
I don't think I need prettier as this ESLint config works totally fine for me.
If you want to make a pull request adding ESLint prettier config you are welcome
## .gitignore
.gitignore generated with gitignore.io, works fine without any further change :D
## Github action
I've just added the default nodejs github workflow as it works perfectly fine
for simple nodejs projects (Just install dependencies, build & test)
## Husky
I love husky as it can automate jobs on any git command such as executing tests
each time you create a commit
## Author
👤 **Victor Martinez**
- Github: [@JasterV](https://github.com/JasterV)
- LinkedIn: [@Victor Martinez](https://linkedin.com/in/victor-martinez-montane)
## Show your support
Give a ⭐️ if this project helped you!
## 📝 License
Copyright © 2021 [Victor Martinez](https://github.com/JasterV).<br /> This
project is [MIT](https://mit-license.org/) licensed.
---
_This README was generated with ❤️ by
[readme-md-generator](https://github.com/kefranabg/readme-md-generator)_

13
jest.json Normal file
View file

@ -0,0 +1,13 @@
{
"preset": "ts-jest",
"coverageDirectory": "./coverage",
"collectCoverageFrom": [
"src/**/*.{ts,tsx,js,jsx}",
"!src/**/*.d.ts"
],
"testMatch": [
"<rootDir>/**/*.test.ts"
],
"resetMocks": true,
"clearMocks": true
}

14683
package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

46
package.json Normal file
View file

@ -0,0 +1,46 @@
{
"name": "node-ts-template",
"version": "0.1.0",
"description": "A template for nodejs projects with configuration files for typescript support",
"main": "dist/index.js",
"scripts": {
"build": "tsc",
"start": "node dist/index.js",
"dev": "ts-node-dev --respawn src/index.ts",
"test": "jest -c ./jest.json",
"test:coverage": "jest --coverage -c ./jest.json",
"prepare": "husky install"
},
"keywords": [
"typescript",
"tsc",
"ts-node",
"nodejs",
"node",
"template"
],
"author": "Victor Martinez",
"license": "MIT",
"devDependencies": {
"@types/convict": "^6.1.1",
"@types/express": "^4.17.13",
"@types/jest": "^26.0.24",
"@types/node": "^16.3.3",
"@typescript-eslint/eslint-plugin": "^4.28.4",
"@typescript-eslint/parser": "^4.28.4",
"eslint": "^7.31.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.23.4",
"husky": "^7.0.0",
"jest": "^27.0.6",
"ts-jest": "^27.0.3",
"ts-node-dev": "^1.1.8",
"typescript": "^4.3.5"
},
"dependencies": {
"axios": "^0.21.1",
"dotenv": "^10.0.0",
"express": "^4.17.1",
"firebase-admin": "^9.11.0"
}
}

5
src/@types/global.d.ts vendored Normal file
View file

@ -0,0 +1,5 @@
import config from '../config'
declare global {
export type Config = typeof config
}

View file

View file

0
src/api/routers/index.ts Normal file
View file

11
src/api/server.ts Normal file
View file

@ -0,0 +1,11 @@
import express from 'express'
const app = express()
app.use(express.json())
app.get('/dummy/hi', (_req, res) => {
res.json({success: true, data: 'Hello, World'})
})
export default app

51
src/config/index.ts Normal file
View file

@ -0,0 +1,51 @@
import dotenv from 'dotenv'
import convict from 'convict'
dotenv.config()
const {
NODE_ENV = "development",
PORT = 8080,
FB_CERT_TYPE,
FB_CERT_PROJECT_ID,
FB_CERT_PRIVATE_KEY_ID,
FB_CERT_PRIVATE_KEY,
FB_CERT_CLIENT_EMAIL,
FB_CERT_CLIENT_ID,
FB_CERT_AUTH_URI,
FB_CERT_TOKEN_URI,
FB_CERT_AUTH_PROVIDER_X_509_CERT_URL,
FB_CERT_CLIENT_X_509_CERT_URL
} = process.env;
if(!FB_CERT_TYPE) throw new Error(`FB_CERT_TYPE required but not found`)
if(!FB_CERT_PROJECT_ID) throw new Error(`FB_CERT_PROJECT_ID required but not found`)
if(!FB_CERT_PRIVATE_KEY_ID) throw new Error(`FB_CERT_PRIVATE_KEY_ID required but not found`)
if(!FB_CERT_PRIVATE_KEY) throw new Error(`FB_CERT_PRIVATE_KEY required but not found`)
if(!FB_CERT_CLIENT_EMAIL) throw new Error(`FB_CERT_CLIENT_EMAIL required but not found`)
if(!FB_CERT_CLIENT_ID) throw new Error(`FB_CERT_CLIENT_ID required but not found`)
if(!FB_CERT_AUTH_URI) throw new Error(`FB_CERT_AUTH_URI required but not found`)
if(!FB_CERT_TOKEN_URI) throw new Error(`FB_CERT_TOKEN_URI required but not found`)
if(!FB_CERT_AUTH_PROVIDER_X_509_CERT_URL) throw new Error(`FB_CERT_AUTH_PROVIDER_X_509_CERT_URL required but not found`)
if(!FB_CERT_CLIENT_X_509_CERT_URL) throw new Error(`FB_CERT_CLIENT_X_509_CERT_URL required but not found`)
const config = {
env: NODE_ENV,
port: PORT,
firebase: {
certConfig: {
type: FB_CERT_TYPE,
project_id: FB_CERT_PROJECT_ID,
private_key_id: FB_CERT_PRIVATE_KEY_ID,
private_key: FB_CERT_PRIVATE_KEY.replace(/\\n/g, '\n'),
client_email: FB_CERT_CLIENT_EMAIL,
client_id: FB_CERT_CLIENT_ID,
auth_uri: FB_CERT_AUTH_URI,
token_uri: FB_CERT_TOKEN_URI,
auth_provider_x509_cert_url: FB_CERT_AUTH_PROVIDER_X_509_CERT_URL,
client_x509_cert_url: FB_CERT_CLIENT_X_509_CERT_URL,
}
}
};
export default config;

10
src/db/index.ts Normal file
View file

@ -0,0 +1,10 @@
import * as admin from 'firebase-admin'
import config from '../config'
admin.initializeApp({
credential: admin.credential.cert(config.firebase.certConfig as any)
});
const db = admin.firestore()
export default db;

8
src/index.ts Normal file
View file

@ -0,0 +1,8 @@
import app from './api/server'
import config from './config'
const port = config.port
app.listen(port, () => {
console.log(`Listening on port: ${port}`)
})

0
src/models/index.ts Normal file
View file

0
src/services/index.ts Normal file
View file

5
test/app.test.ts Normal file
View file

@ -0,0 +1,5 @@
describe('Dummy tests', () => {
it('1 + 1 equals 2', () => {
expect(1 + 1).toBe(2);
});
});

35
tsconfig.json Normal file
View file

@ -0,0 +1,35 @@
{
"compilerOptions": {
"target": "es2020",
"module": "es2020",
"lib": [
"es2020"
],
"skipLibCheck": true,
"sourceMap": true,
"outDir": "./dist",
"moduleResolution": "node",
"removeComments": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"resolveJsonModule": true,
"baseUrl": "."
},
"exclude": [
"node_modules",
"**/*.test.ts"
],
"include": [
"./src/**/*.ts",
]
}