Hono adapter
In backend
import { Hono, HonoRequest } from 'hono';
import { serve } from '@hono/node-server';
import { honoAdapterFactory } from 'tsdk-server-adapters/lib/hono-adapter';
import { checkMethodHasBody, ProtocolTypes } from '@/src/shared/tsdk-helper';
import { RequestInfo, routeBus } from './gen-route';
const port = 3000;
const app = new Hono();
app.get('/', (c) => {
return c.text('hi, from hono.');
});
app.all(
'/api/:type/*',
honoAdapterFactory<RequestInfo>({
routeBus,
async getReqInfo(req: HonoRequest) {
return {
ip: '',
lang: 'zh-CN',
type: (req.param() as { type: string }).type,
token: req.header['authorization'],
};
},
getType(reqInfo) {
return reqInfo.type;
},
async getData(req) {
return checkMethodHasBody(req.method) ? req.raw.body : req.query();
},
})
);
serve({
fetch: app.fetch,
port,
});
console.log(`Hono server running at http://localhost:${port}`);
In frontend (Use xior, based on fetch)
xior based on the fetch API similar 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);
Then update tsdk.config.js
, add conifg httpLib: 'xior'
tsdk.config.js
/** @type {import('tsdk').TSDKConfig} */
module.exports = {
...
httpLib: 'xior',
...
}
In frontend (Use axios)
💡
Hono and Express are web framework based on HTTP protocol
import axios from 'axios';
import { setHandler, setAxiosInstance, axiosHandler } from 'fe-sdk';
const axiosInstance = axios.create({ baseURL: `/api/user` });
setAxiosInstance(axiosInstance);
setHandler(axiosHandler);