feat: implement createPayer function and enhance /payer route with error handling
This commit is contained in:
parent
a273bba786
commit
d183b82a7c
|
@ -3,6 +3,7 @@ import dotenv from 'dotenv'
|
||||||
import route from 'koa-route'
|
import route from 'koa-route'
|
||||||
import Debug from 'debug'
|
import Debug from 'debug'
|
||||||
import koaBody from 'koa-body'
|
import koaBody from 'koa-body'
|
||||||
|
import func from './func'
|
||||||
|
|
||||||
dotenv.config()
|
dotenv.config()
|
||||||
console.log = Debug('invoiceIssuer:app.ts')
|
console.log = Debug('invoiceIssuer:app.ts')
|
||||||
|
@ -19,21 +20,27 @@ const getRoot = route.get('/', (ctx) => {
|
||||||
ctx.body = 'Hello World'
|
ctx.body = 'Hello World'
|
||||||
})
|
})
|
||||||
|
|
||||||
/**
|
|
||||||
* POST /payer
|
|
||||||
* @summary 创建一个新的付款人。这个付款人与收据上标识的收款人信息分离,但内部数据库关联(以便检索)。
|
|
||||||
* @param {Koa.Context} ctx - Koa context object
|
|
||||||
*/
|
|
||||||
const postPayer = route.post('/payer', async (ctx) => {
|
const postPayer = route.post('/payer', async (ctx) => {
|
||||||
// TODO: 请求头验证 bearer token
|
// TODO: 请求头验证 bearer token
|
||||||
|
|
||||||
const { name, address, abn, email } = ctx.request.body
|
const { name, address, email, abn } = ctx.request.body
|
||||||
|
if (!name || !address || !email) {
|
||||||
|
ctx.throw(400, 'required_fields_missing')
|
||||||
|
}
|
||||||
|
|
||||||
if (!name || !address || !email) {
|
try {
|
||||||
ctx.status = 400;
|
const payer = await func.createPayer(name, address, email, abn)
|
||||||
ctx.body = { error: 'required_fields_missing' }
|
ctx.body = payer
|
||||||
return;
|
} catch (e) {
|
||||||
}
|
if (e instanceof HttpError) {
|
||||||
|
ctx.status = e.status
|
||||||
|
ctx.body = e.message
|
||||||
|
} else {
|
||||||
|
console.error(e)
|
||||||
|
ctx.throw(500, 'unknown_issues')
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// 导出路由
|
// 导出路由
|
||||||
|
|
10
backend/src/func/createPayer.ts
Normal file
10
backend/src/func/createPayer.ts
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
/**
|
||||||
|
* POST /payer
|
||||||
|
* @summary 创建一个新的付款人。这个付款人与收据上标识的收款人信息分离,但内部数据库关联(以便检索)。
|
||||||
|
* @param {string} name - 付款人的名字
|
||||||
|
* @param {string} address - 付款人的地址
|
||||||
|
* @param {string} email - 付款人的电子邮件地址
|
||||||
|
* @param {string} abn - 付款人的澳大利亚商业号码
|
||||||
|
*/
|
||||||
|
export default async (name: string, address: string, email: string, abn?: string) => {
|
||||||
|
}
|
5
backend/src/func/index.ts
Normal file
5
backend/src/func/index.ts
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
import createPayer from "./createPayer"
|
||||||
|
|
||||||
|
export default {
|
||||||
|
createPayer
|
||||||
|
}
|
13
backend/src/types/HttpError.d.ts
vendored
Normal file
13
backend/src/types/HttpError.d.ts
vendored
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
/**
|
||||||
|
* 用于规范化向用户返回的错误信息。信息与状态码皆为枚举类型,以便前端进行 i18n、保证错误信息的准确性,
|
||||||
|
* 同时保证返回的格式符合 HTTP 规范。
|
||||||
|
*/
|
||||||
|
class HttpError extends Error {
|
||||||
|
/**
|
||||||
|
* 创建一个新的 HTTP 错误。
|
||||||
|
* @param message 错误的消息。
|
||||||
|
* @param status 错误的状态码。
|
||||||
|
*/
|
||||||
|
constructor(message: 'required_fields_missing' | 'unknown_issues', status: number);
|
||||||
|
status: 400 | 401 | 403 | 404 | 500;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user