tsdk-server-adapters
Version:
[](https://www.npmjs.com/package/tsdk-server-adapters) [](https://packagephobia.com/result?p=tsdk-server-adapters) ); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.genRouteFactory = exports.getRouteEventName = exports.PROTOCOL_KEYs = exports.PROTOCOLs = void 0;
// @ts-ignore
const eventemitter3_1 = __importDefault(require("eventemitter3"));
exports.PROTOCOLs = {
/** express.js */
express: 'express',
/** honojs */
hono: 'hono',
'socket.io': 'io',
/** @deprecated not recommend */
ws: 'ws',
};
exports.PROTOCOL_KEYs = Object.keys(exports.PROTOCOLs);
function getRouteEventName(config) {
return `${exports.PROTOCOLs[config.protocol]}:${config.type}:${config.method}:${config.path}`;
}
exports.getRouteEventName = getRouteEventName;
function sendFactory(protocol, response, protocolType, callback) {
return function send(payload) {
// default http is express.js
if (protocol === 'express') {
const { status, result } = payload;
response.status(status || 200).send(result);
}
else if (protocol === 'hono') {
const { status, result } = payload;
response.status((status || 200));
if (typeof result === 'string') {
callback === null || callback === void 0 ? void 0 : callback(response.text(result));
}
else {
callback === null || callback === void 0 ? void 0 : callback(response.json(result || {}));
}
}
else if (protocol === 'socket.io') {
// for socket.io
if (response.connected) {
response.emit(protocolType.response, payload);
}
}
else if (protocol === 'ws') {
// for websocket
response.send(protocolType.response + JSON.stringify(payload));
}
};
}
function genRouteFactory(onErrorHandler, protocolType, middlewares,
/** API types */
types) {
const routeBus = new eventemitter3_1.default();
const routesMap = Object.create(null);
function genRoute(apiConfig, cb) {
function onEvent(protocol_1, reqInfo_1, response_1, _a, callback_1) {
return __awaiter(this, arguments, void 0, function* (protocol, reqInfo, response, { _id: msgId, payload }, callback) {
const send = sendFactory(protocol, response, protocolType, callback);
try {
// middlewares
// will throw error if one of middlewares throw error
if (middlewares && (middlewares === null || middlewares === void 0 ? void 0 : middlewares.length) > 0) {
yield middlewares.reduce((previousPromise, nextMiddleware) => {
return previousPromise.then(() => nextMiddleware(protocol, apiConfig, reqInfo));
}, Promise.resolve());
}
const data = apiConfig.schema ? apiConfig.schema.parse(payload) : payload;
const result = yield cb(data, reqInfo, response);
send({ result, _id: msgId, callback });
}
catch (e) {
onErrorHandler(e, {
protocol,
msgId,
send,
config: apiConfig,
payload,
});
}
});
}
exports.PROTOCOL_KEYs.forEach((i) => {
const routeEventName = getRouteEventName({
protocol: i,
type: apiConfig.type,
method: apiConfig.method.toLowerCase(),
path: apiConfig.path,
});
if (routesMap[routeEventName]) {
throw new Error(`\`${routeEventName}\` already used.`);
}
else {
routesMap[routeEventName] = 1;
}
routeBus.on(routeEventName, (reqInfo, resOrSocket, body, callback) => {
onEvent(i, reqInfo, resOrSocket, body, callback);
});
});
}
return {
routeBus,
genRoute: function g(apiConfig, cb) {
if (apiConfig.type === 'common' || !apiConfig.type) {
if (!types || (types === null || types === void 0 ? void 0 : types.length) === 0) {
throw new Error(`\`genRouteFactory\` \`types\` param is required`);
}
types === null || types === void 0 ? void 0 : types.forEach((item) => {
if (item !== 'common') {
genRoute(Object.assign(Object.assign({}, apiConfig), { type: item }), cb);
}
});
}
else {
genRoute(apiConfig, cb);
}
},
getRouteEventName,
};
}
exports.genRouteFactory = genRouteFactory;