100 ms services

node v14.16.0
version: 1.0.0
endpointsharetweet
const { json, send } = require("micro") const query = require("micro-query") const axios = require("axios") const jwt = require("jsonwebtoken") const uuid4 = require("uuid4") function getJWT(payload, secret) { const issuer = process.env.HMS_CUSTOMER_ID return jwt.sign( payload, secret, { algorithm: "HS256", expiresIn: "1y", issuer: issuer, jwtid: uuid4() } ) } function getAuthToken(roomId, userId, role) { const secret = process.env.HMS_SECRET return getJWT({ app_id: process.env.HMS_APP_ID, access_key: process.env.HMS_ACCESS_KEY, room_id: roomId, user_id: userId, role: role }, secret) } function getManagementToken() { const accessKey = process.env.HMS_MANAGEMENT_KEY const secret = process.env.HMS_MANAGEMENT_SECRET const payload = { access_key: accessKey } return getJWT(payload, secret) } async function createRoom({ name, description = "sample room", recording_info = { enabled: false } }) { const managementToken = getManagementToken() const options = { method: "POST", url: "https://prod-in.100ms.live/api/v1/rooms", data: { name, description, recording_info }, headers : { Authorization: `Bearer ${managementToken}`, "Content-Type": "application/json" } } const response = await axios(options) if(response.status === 200) { return response.data } else { throw new Error(response.data) } } const apiHandlers = { token: payload => { const { room_id, user_name, peer_id, role } = payload const user_id = uuid4() + user_name ? user_name : peer_id return { token: getAuthToken(room_id, user_id, role) } }, room: async payload => { const { room_name } = payload return await createRoom({ name: room_name }) } } exports.endpoint = async (req, res) => { res.setHeader("Access-Control-Allow-Origin", "*") res.setHeader("Access-Control-Allow-Methods", "*") json(req).then(async payload => { const { api } = query(req) const response = await apiHandlers[api](payload) send(res, 200, response) }).catch(error => { send(res, 400, {error}) }) }
Loading…

no comments

    sign in to comment