Fastify 适配器使用
这里虽然是 fastify,但是依旧使用 Express 适配器,例子包扩后端使用和前端使用。
后端使用
import http from 'http';
import Fastify from 'fastify';
import fastifyExpress from '@fastify/express';
import { expressAdapterFactory } from 'tsdk-server-adapters/lib/express-adapter';
import { checkMethodHasBody, ProtocolTypes } from '@/src/shared/tsdk-helper';
import { RequestInfo, routeBus } from './gen-route';
const port = 3000;
(async () => {
let server: http.Server;
const serverFactory = (handler, opts) => {
server = http.createServer((req, res) => {
handler(req, res);
});
return server;
};
const app = Fastify({ serverFactory });
await app.register(fastifyExpress);
app.get('/', (req, res) => {
res.send('hi, from fastify.');
});
app.use(
'/api/:type',
expressAdapterFactory<RequestInfo>({
routeBus,
async getReqInfo(req) {
return {
ip: req.ip as string,
lang: 'zh-CN',
type: req.params.type,
token: req.headers.authorization,
};
},
getType(reqInfo) {
return reqInfo.type;
},
async getData(req) {
return checkMethodHasBody(req.method) ? req.body : req.query;
},
})
);
app.ready(() => {
server.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});
});
})();
前端使用 xior(fetch)
xior 基于 fetch, API 类似 axios,https://www.npmjs.com/package/xior (opens in a new tab)
import xior from 'xior';
import { setHandler, setXiorInstance, xiorHandler } from 'fe-sdk';
const xiorInstance = xior.create({ baseURL: `/api/user` });
setXiorInstance(xiorInstance);
setHandler(xiorHandler);
再更新 tsdk.config.js
,添加配置 httpLib: 'xior'
tsdk.config.js
/** @type {import('tsdk').TSDKConfig} */
module.exports = {
...
httpLib: 'xior',
...
}
前端使用 (axios)
import axios from 'axios';
import { setHandler, setAxiosInstance, axiosHandler } from 'fe-sdk';
const axiosInstance = axios.create({ baseURL: `/api/user` });
setAxiosInstance(axiosInstance);
setHandler(axiosHandler);