Skip to content

Commit

Permalink
Add types.
Browse files Browse the repository at this point in the history
  • Loading branch information
yoonge committed Apr 15, 2023
1 parent 7006cb1 commit e9f4bfa
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 14 deletions.
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export default {
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended'
],
ignorePatterns: ['tsconfig.json'],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
root: true
Expand Down
11 changes: 6 additions & 5 deletions app.js → app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import bodyparser from 'koa-bodyparser'
import json from 'koa-json'
import koaStatic from 'koa-static'
import logger from 'koa-logger'
// @ts-ignore
import onerror from 'koa-onerror'
// @ts-ignore
import render from 'koa-art-template'

import index from './routes/index.js'
import user from './routes/user.js'

const _dirName = dirname(fileURLToPath(import.meta.url))
console.log(_dirName)

// error handler
onerror(app)
Expand Down Expand Up @@ -46,15 +47,15 @@ render(app, {

// logger
app.use(async (ctx, next) => {
const start = new Date()
const start = Number(new Date())
await next()
const ms = new Date() - start
const ms = Number(new Date()) - start
console.log(`${ctx.method} ${ctx.url} - ${ms}ms`)
})

// routes
app.use(index.routes(), index.allowedMethods())
app.use(user.routes(), user.allowedMethods())
app.use(index.routes()).use(index.allowedMethods())
app.use(user.routes()).use(user.allowedMethods())

// error-handling
app.on('error', (err, ctx) => {
Expand Down
3 changes: 2 additions & 1 deletion bin/www.js → bin/www.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/usr/bin/env node
// @ts-nocheck

/**
* Module dependencies.
*/

import app from '../app.js'
import app from '../app.ts'
import http from 'http'
import debugModule from 'debug'
const debug = new debugModule('demo:server')
Expand Down
13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
"type": "module",
"version": "0.1.0",
"scripts": {
"dev": "npx nodemon bin/www",
"dev": "npx nodemon bin/www.ts",
"format": "prettier --plugin-search-dir . --write .",
"lint": "eslint .",
"prd": "pm2 start bin/www",
"start": "node bin/www",
"prd": "pm2 start bin/www.ts",
"start": "npx ts-node bin/www.ts",
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
Expand All @@ -35,6 +35,13 @@
"koa-static": "^5.0.0"
},
"devDependencies": {
"@types/debug": "^4.1.7",
"@types/koa": "^2.13.6",
"@types/koa-bodyparser": "^4.3.10",
"@types/koa-json": "^2.0.20",
"@types/koa-logger": "^3.1.2",
"@types/koa-router": "^7.4.4",
"@types/koa-static": "^4.0.2",
"@typescript-eslint/eslint-plugin": "^5.58.0",
"@typescript-eslint/parser": "^5.58.0",
"eslint": "^8.38.0",
Expand Down
215 changes: 215 additions & 0 deletions pnpm-lock.yaml

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

3 changes: 2 additions & 1 deletion routes/index.js → routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ParameterizedContext } from 'koa'
import koaRouter from 'koa-router'
const router = new koaRouter()

router.get('/', async (ctx, next) => {
router.get('/', async (ctx: ParameterizedContext, next) => {
await ctx.render('index', {
title: 'Hello Koa 2!'
})
Expand Down
File renamed without changes.
9 changes: 6 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
// "extends": "./config/base",
// "files": ['app.ts', 'index.ts'],
"include": ["./src/**/*"],
"include": ["./**/*"],
// "exclude": [ "./src/temp/**/*" ],
"compilerOptions": {
"baseUrl": "./",
"module": "ESNext",
"target": "ESNext",
// "lib": [ "ESNext", "DOM" ],
"typeRoots": ["src/types", "node_modules/@types"],
// "outDir": "./dist",
"typeRoots": ["./types", "node_modules/@types"],
"outDir": "./dist",
// "outFile": "./dist/bundle.js",
"sourceMap": true,
"allowJs": true,
Expand All @@ -31,5 +31,8 @@
// "strictNullChecks": true,
// "forceConsistentCasingInFileNames": true,
// "noFallthroughCasesInSwitch": false,
},
"ts-node": {
"esm": true
}
}
2 changes: 2 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare module 'koa-onerror'
declare module 'koa-art-template'

0 comments on commit e9f4bfa

Please sign in to comment.