Skip to content
This repository has been archived by the owner on Apr 14, 2020. It is now read-only.

Commit

Permalink
Merge pull request #3 from lightyears1998/refactor-api
Browse files Browse the repository at this point in the history
移除Babel而直接使用TypeScript编译器
  • Loading branch information
lightyears1998 authored Oct 3, 2019
2 parents 2c74218 + db6b0a7 commit 2fd4003
Show file tree
Hide file tree
Showing 16 changed files with 377 additions and 1,376 deletions.
11 changes: 0 additions & 11 deletions .babelrc

This file was deleted.

2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Ignore build result.
/bin/**
/lib/**
11 changes: 6 additions & 5 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
env:
browser: true
commonjs: true
es6: true
node: true
parser: 'babel-eslint'
parser: '@typescript-eslint/parser'
parserOptions:
project: './tsconfig.json'
plugins:
- '@typescript-eslint'
extends:
- 'eslint:recommended'
- 'plugin:@typescript-eslint/recommended'
globals:
Atomics: readonly
SharedArrayBuffer: readonly
parserOptions:
ecmaVersion: 2018
rules:
brace-style:
- warn
Expand Down
8 changes: 3 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
# Compiled file
bin
# Compiled files and runtime files
lib
log

# Configuration
conf/*.yml
!conf/*.example.yml

# Runtime Information
log/*

# Node.js
node_modules
yarn-error.log
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

### 常用指令

1. `yarn watch` 启动Watch服务,包括lint-watch和build-watch
1. `yarn lint:watch``yarn build:watch`
2. `yarn start:dev` 启动开发服务器。
3. `yarn build` 编译`src`文件夹下的TypeScript源代码。
4. `yarn clean` 删除`bin``log`文件夹。
Expand Down
34 changes: 14 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
{
"name": "paperstack-api-server",
"version": "0.1.2",
"version": "0.1.3",
"description": "作业收集平台PaperStack的API服务器",
"main": "bin/app.js",
"main": "lib/app.js",
"repository": "https://github.com/super-dash/paperstack-api-server.git",
"author": "github.com/super-dash",
"license": "MIT",
"scripts": {
"clean": "rm -rf ./bin && rm -rf ./log",
"lint": "eslint src --ext .ts",
"clean": "rm -rf ./lib && rm -rf ./log",
"lint": "esw src --ext .ts",
"lint:fix": "yarn lint --fix",
"lint:watch": "watch \"yarn lint:fix\" src --wait 10",
"build": "babel src --extensions .ts --out-dir bin",
"lint:watch": "yarn lint:fix --watch --changed --clear",
"build": "tsc",
"build:watch": "yarn build --watch",
"watch": "watch \"npm-run-all lint:fix build\" src --wait 10",
"start": "node bin/app.js",
"start:dev": "nodemon bin/app.js --watch bin --delay 10",
"start": "node lib/app.js",
"start:dev": "nodemon lib/app.js --watch lib",
"test": "echo \"No test sepcified.\""
},
"devDependencies": {
Expand All @@ -28,19 +27,13 @@
"@types/randomstring": "^1.1.6",
"@types/sequelize": "^4.28.4",
"@types/validator": "^10.11.3",
"@types/winston": "^2.4.4",
"babel-eslint": "^10.0.3",
"@typescript-eslint/eslint-plugin": "^2.3.2",
"@typescript-eslint/parser": "^2.3.2",
"eslint": "^6.4.0",
"nodemon": "^1.19.2",
"npm-run-all": "^4.1.5",
"watch": "^1.0.2"
"eslint-watch": "^6.0.1",
"nodemon": "^1.19.2"
},
"dependencies": {
"@babel/cli": "^7.6.0",
"@babel/core": "^7.6.0",
"@babel/preset-env": "^7.6.0",
"@babel/preset-typescript": "^7.6.0",
"@babel/register": "^7.6.0",
"bcrypt": "^3.0.6",
"cors": "^2.8.5",
"express": "^4.17.1",
Expand All @@ -49,6 +42,7 @@
"pg-hstore": "^2.3.3",
"randomstring": "^1.1.5",
"sequelize": "^5.19.0",
"typescript": "^3.6.3"
"typescript": "^3.6.3",
"winston": "^3.2.1"
}
}
6 changes: 3 additions & 3 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import express from "express";
import cors from "cors";


const version = JSON.parse(fs.readFileSync(path.resolve(`${__dirname}/../package.json`), "utf8")).version;
const appVersion = JSON.parse(fs.readFileSync(path.resolve(`${__dirname}/../package.json`), "utf8")).version;
const serverConfig = config.server;

const app = express();
Expand All @@ -19,13 +19,13 @@ app.use(express.json());
app.all("/", (req, res) => {
res.json({
"message": "欢迎使用PaperStack API Server。",
"version": version
"version": appVersion
});
});

app.use(router);

app.listen(serverConfig.port, () => {
logger.info(`PaperStack API服务器 v${version}`);
logger.info(`PaperStack API服务器 v${appVersion}`);
logger.info(`正在监听${serverConfig.port}端口。`);
});
14 changes: 7 additions & 7 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import process from "process";


interface Configuration {
server: ServerConfiguration,
database: DatabaseConfiguration
server: ServerConfiguration;
database: DatabaseConfiguration;
}

interface DatabaseConfiguration {
host: string,
user: string,
pass: string,
db: string
host: string;
user: string;
pass: string;
db: string;
}

interface ServerConfiguration {
port: Number
port: number;
}


Expand Down
8 changes: 4 additions & 4 deletions src/model/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ User.init({
* 检查邮箱地址是否已被注册
* @param email 待检查的邮箱
*/
export async function checkEmailAvailabilty(email: string) : Promise<boolean> {
export async function checkEmailAvailabilty(email: string): Promise<boolean> {
if (email === "") {
return false;
}
let emailCount: number = await User.count({ where: { email: { [Sequelize.Op.iLike]: email } } });
const emailCount: number = await User.count({ where: { email: { [Sequelize.Op.iLike]: email } } });
return emailCount === 0;
}

Expand All @@ -48,7 +48,7 @@ export async function checkEmailAvailabilty(email: string) : Promise<boolean> {
* @param password 待哈希的密码
* @returns 密码哈希
*/
export async function hashPassword(password: string) : Promise<string> {
export async function hashPassword(password: string): Promise<string> {
const saltRounds = 10;

let passwordHash = "";
Expand All @@ -70,7 +70,7 @@ export async function hashPassword(password: string) : Promise<string> {
* @param hash 哈希
* @returns 密码与哈希是否相符
*/
export async function comparePassword(password, hash) : Promise<boolean> {
export async function comparePassword(password, hash): Promise<boolean> {
let result = false;

await bcrypt.compare(password, hash)
Expand Down
2 changes: 1 addition & 1 deletion src/route/check-email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { RequestHandler } from "express";
* 检查由`req.body.email`指定邮箱地址是否未被占用。
*/
const checkEmail: RequestHandler = async function (req, res) {
let email: string = typeof req.body.email === "string" ? req.body.email : "";
const email: string = typeof req.body.email === "string" ? req.body.email : "";

/*
* @TODO
Expand Down
8 changes: 4 additions & 4 deletions src/route/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { RequestHandler } from "express";


const login: RequestHandler = async function (req, res) {
let email: string = typeof req.body.email === "string" ? req.body.email : "";
let password: string = typeof req.body.password === "string" ? req.body.password : "";
const email: string = typeof req.body.email === "string" ? req.body.email : "";
const password: string = typeof req.body.password === "string" ? req.body.password : "";

const codeSuccess = 1;
const codeEmailNotExist = 2;
Expand Down Expand Up @@ -40,15 +40,15 @@ const login: RequestHandler = async function (req, res) {
return;
}

let passwordMatched: boolean = await comparePassword(password, user.passwordHash);
const passwordMatched: boolean = await comparePassword(password, user.passwordHash);
if (!passwordMatched) {
res.json({
result: codeEmailPasswordMismatch
});
return;
}

let authkey: string = Authkey.generateNewKey();
const authkey: string = Authkey.generateNewKey();
user.createAuthkey({
value: authkey
});
Expand Down
2 changes: 1 addition & 1 deletion src/route/logout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { RequestHandler } from "express";


const logout: RequestHandler = async function (req, res) {
let authkey: string = typeof req.body.authkey === "string" ? req.body.authkey : "";
const authkey: string = typeof req.body.authkey === "string" ? req.body.authkey : "";

Authkey.destroy({
where: {
Expand Down
14 changes: 7 additions & 7 deletions src/route/modify-password.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { RequestHandler } from "express";


const modifyPassword: RequestHandler = async function (req, res) {
let authkey: string = typeof req.body.authkey === "string" ? req.body.authkey : "";
let oldPassword: string = typeof req.body.oldPassword === "string" ? req.body.oldPassword : "";
let newPassword: string = typeof req.body.newPassword === "string" ? req.body.newPassword : "";
const authkey: string = typeof req.body.authkey === "string" ? req.body.authkey : "";
const oldPassword: string = typeof req.body.oldPassword === "string" ? req.body.oldPassword : "";
const newPassword: string = typeof req.body.newPassword === "string" ? req.body.newPassword : "";

if (authkey === "" || oldPassword === "" || newPassword === "") {
res.json({
Expand All @@ -16,7 +16,7 @@ const modifyPassword: RequestHandler = async function (req, res) {
return;
}

let key: Authkey = await Authkey.findOne({
const key: Authkey = await Authkey.findOne({
where: {
value: authkey
}
Expand All @@ -28,7 +28,7 @@ const modifyPassword: RequestHandler = async function (req, res) {
return;
}

let user = await User.findOne({
const user = await User.findOne({
where: {
id: key.userId
}
Expand All @@ -40,15 +40,15 @@ const modifyPassword: RequestHandler = async function (req, res) {
return;
}

let passwordMatch: boolean = await comparePassword(oldPassword, user.passwordHash);
const passwordMatch: boolean = await comparePassword(oldPassword, user.passwordHash);
if (!passwordMatch) {
res.json({
message: "修改失败"
});
return;
}

let newPasswordHash: string = await hashPassword(newPassword);
const newPasswordHash: string = await hashPassword(newPassword);

user.update({
passwordHash: newPasswordHash
Expand Down
6 changes: 3 additions & 3 deletions src/route/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { User } from "../model";
* 注册新用户
*/
const register: RequestHandler = async function (req, res) {
let email: string = typeof req.body.email === "string" ? req.body.email : "";
let password: string = typeof req.body.password === "string" ? req.body.password : "";
const email: string = typeof req.body.email === "string" ? req.body.email : "";
const password: string = typeof req.body.password === "string" ? req.body.password : "";

/*
* @TODO
Expand All @@ -25,7 +25,7 @@ const register: RequestHandler = async function (req, res) {
return;
}

let emailOk = checkEmailAvailabilty(email);
const emailOk = checkEmailAvailabilty(email);
if (!emailOk) {
res.json({
result: codeFail
Expand Down
14 changes: 14 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "esnext",
"module": "commonjs",
"allowJs": true,
"outDir": "lib",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"inlineSourceMap": true,
},
"include": [
"src/**/*"
]
}
Loading

0 comments on commit 2fd4003

Please sign in to comment.