UNPKG

@kne/fastify-shorten

Version:

Fastify Shorten 是一个专为 Fastify 框架设计的 URL 缩短服务插件,提供完整的短链接生成和管理功能

54 lines (51 loc) 1.43 kB
const fp = require('fastify-plugin'); const path = require('node:path'); const { Unauthorized } = require('http-error'); module.exports = fp( async (fastify, options) => { options = Object.assign( {}, { name: 'shorten', headerName: 'x-user-code', maxAttempts: 10, length: 6, dbTableNamePrefix: 't_shorten_' }, options ); fastify.register(require('@kne/fastify-namespace'), { name: options.name, options, modules: [ [ 'models', await fastify.sequelize.addModels(path.resolve(__dirname, './libs/models'), { prefix: options.dbTableNamePrefix }) ], ['services', path.resolve(__dirname, './libs/services')], [ 'authenticate', { code: async request => { const code = request.headers[options.headerName]; if (!code) { throw new Unauthorized(`${options.headerName} is required`); } try { request.shortenCode = code; request.authenticatePayload = JSON.parse(await fastify[options.name].services.decode(code)); } catch (e) { throw new Unauthorized(`${options.headerName} is invalid`); } } } ] ] }); }, { name: 'fastify-shorten' } );