You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

183 lines
5.9 KiB

// | ------------------------------------------------------------
// | @版本: version 0.1
// | @创建人: 【Nie-x7129】
// | @E-mail: x71291@outlook.com
// | @所在项目: fastify-template
// | @文件描述: index.js -
// | @创建时间: 2023-12-16 22:41
// | @更新时间: 2023-12-16 22:41
// | @修改记录:
// | -*-*-*- (时间--修改人--修改说明) -*-*-*-
// | =
// | ------------------------------------------------------------
import user from './user/index.js';
import system from './system/index.js';
function rootRoute(fastify, options, done){
fastify.decorate('projectName', fastify.conf.projectName);
fastify.get('/', async function (request, reply){
return {
path: request.url,
timestamp: new Date().getZH()
};
});
const mySchema = {
type: 'object',
properties: {
name: {
type: 'string'
},
age: {
type: 'integer'
},
love: {
type: 'string'
}
},
required: [ 'name', 'age' ]
};
const mySchema2 = {
body: {
type: 'object',
additionalProperties: false, // 不允许读取
required: [ 'string' ], // 必填
minProperties: 1, // 最小属性数量
maxProperties: 25, // 最大属性数量
properties: {
array: {
type: 'array',
minItems: 2,
maxItems: 2,
uniqueItems: true, // 元素唯一
items: [
{ type: 'number' },
{ type: 'string' }
], // 数组的第一个元素必须是数字,第二个元素必须是字符串。
// items: {
// anyOf: [
// { type: 'number' },
// { type: 'string' }
// ]
// }, // 数组任意位置都可以是number和string
// additionalItems: {
// anyOf: [
// { type: 'number' },
// { type: 'string' }
// ]
// }, // items定义之外的元素,必须定义items
errorMessage: {
minItems: '数组长度不能小于2',
maxItems: '数组长度不能大于5'
}
},
string: {
type: 'string',
maxLength: 200,
minLength: 1,
// pattern: '', // 支持正则表达式
errorMessage: {
type: 'string必须为字符串',
maxLength: '字符长度不能大于20',
minLength: '字符长度不能小于10'
}
},
number: {
type: 'number',
maximum: 2000,
minimum: 1,
// multipleOf: 3, // 必须是3的倍数
errorMessage: {
type: 'string必须为数字',
maximum: '数字大小最大为20',
minimum: '数字大小最小为10'
}
},
integer: {
type: 'integer',
exclusiveMaximum: 20,
exclusiveMinimum: 2,
errorMessage: {
type: 'string必须为数字',
exclusiveMaximum: '数字大小最大为19',
exclusiveMinimum: '数字大小最小为11'
}
},
enum1: {
type: 'number',
enum: [ 1, 2, 3 ]
},
enum2: {
type: [ 'number', 'string' ],
enum: [ 1, 2, 'xs' ],
errorMessage: {
enum: '不在枚举范围内',
}
},
enum3: {
oneOf: [
{ 'type': 'string', 'enum': [ 'temp' ] },
{ 'type': 'number', 'enum': [ 25 ] },
{ 'type': 'boolean', 'enum': [ true, false ] }
],
errorMessage: {
enum: '不在枚举范围内',
}
},
boolean: {
type: 'boolean'
},
// xsx: 'number',
studentList: {
type: 'array',
items: mySchema
}
}
},
response: {
200: {
type: 'object',
properties: {
hello: { type: 'string' }
}
}
}
};
fastify.route({
url: '/testSchema',
method: 'POST',
schema: mySchema2,
handler: async function(request, reply){
console.log(request.body);
return 'A';
}
});
fastify.post('/testSchema2', {schema: mySchema2}, async function(response, reply){
return 'A';
});
fastify.register(user, {
prefix: 'user'
});
fastify.register(system, {
prefix: 'system'
});
done();
}
export default function routes(fastify, option, done){
fastify.get('/', async function(request, reply){
return reply.view('/index.pug', { title: this.conf.projectName });
// return {
// project: fastify.conf.projectName,
// framework: 'Fastify ' + fastify.version,
// timestamp: new Date().getZH()
// };
});
fastify.register(rootRoute, {
prefix: '/api'
});
done();
}