Documentation
Guide
Express adapter

Express adapter

In backend

import http from 'http';
import express, { Request } from 'express';
import multer from 'multer';
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;
 
const app = express();
const server = http.createServer(app);
 
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(multer().none());
 
app.get('/', (req, res) => {
  res.end('hi, from express.');
});
 
app.use(
  '/api/:type',
  expressAdapterFactory<RequestInfo>({
    routeBus,
    async getReqInfo(req: Request) {
      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;
    },
  })
);
 
server.listen(port, () => {
  console.log(`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)

import axios from 'axios';
import { setHandler, setAxiosInstance, axiosHandler } from 'fe-sdk';
 
const axiosInstance = axios.create({ baseURL: `/api/user` });
setAxiosInstance(axiosInstance);
setHandler(axiosHandler);