Stripe integration flow
This commit is contained in:
@@ -2,16 +2,30 @@ import { WebSocket, WebSocketServer } from 'ws';
|
||||
import { z } from 'zod';
|
||||
import jwt from 'jsonwebtoken';
|
||||
|
||||
import * as serverFunctions from "../../ui/_/code/bridge/serverFunctions.js"
|
||||
import server from "../../ui/_/code/bridge/serverFunctions.js"
|
||||
import ForumHandler from "./handlers/ForumHandler.js"
|
||||
import MessagesHandler from "./handlers/MessagesHandler.js"
|
||||
|
||||
export default class Socket {
|
||||
wss;
|
||||
messageSchema = z.object({
|
||||
|
||||
functionCallSchema = z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
args: z.array(z.any()),
|
||||
|
||||
data: z.union([
|
||||
z.object({}).passthrough(), // allows any object
|
||||
z.array(z.any()) // allows any array
|
||||
]).optional()
|
||||
})
|
||||
.strict()
|
||||
|
||||
appOperationSchema = z.object({
|
||||
id: z.string(),
|
||||
app: z.string().optional(),
|
||||
operation: z.string().optional(),
|
||||
|
||||
msg: z.union([
|
||||
z.object({}).passthrough(), // allows any object
|
||||
z.array(z.any()) // allows any array
|
||||
@@ -69,43 +83,64 @@ export default class Socket {
|
||||
// Build a system where the ws obj is updated every time on navigate, so it already has context
|
||||
// this way, we can only send broadcast messages to clients that actually have that app / subapp open
|
||||
handleMessage = async (msg, ws) => {
|
||||
|
||||
try {
|
||||
const text = msg.toString();
|
||||
const req = JSON.parse(text);
|
||||
if(!this.messageSchema.safeParse(req).success) throw new Error("Socket.handleMessage: Incoming ws message has incorrect format!")
|
||||
|
||||
let responseData;
|
||||
switch (req.app) {
|
||||
case "FORUM":
|
||||
responseData = await ForumHandler.handle(req.operation, req.msg, ws)
|
||||
break;
|
||||
console.log(req)
|
||||
|
||||
case "MESSAGES":
|
||||
responseData = MessagesHandler.handle(req.operation, req.msg, ws)
|
||||
break;
|
||||
|
||||
default:
|
||||
if(!req.app) {
|
||||
let func = req.msg
|
||||
responseData = serverFunctions[func.name](...args)
|
||||
} else {
|
||||
console.error("unknown ws message")
|
||||
}
|
||||
if(this.appOperationSchema.safeParse(req).success) {
|
||||
this.handleAppOperation(req, ws)
|
||||
} else if(this.functionCallSchema.safeParse(req).success) {
|
||||
this.handleFunction(req, ws)
|
||||
} else {
|
||||
throw new Error("Socket.handleMessage: Incoming ws message has incorrect format!")
|
||||
}
|
||||
|
||||
let response = {
|
||||
...req
|
||||
}
|
||||
response.msg = responseData
|
||||
|
||||
if(!this.messageSchema.safeParse(response).success) throw new Error("Socket.handleMessage: Outgoing ws message has incorrect format!")
|
||||
ws.send(JSON.stringify(response))
|
||||
|
||||
} catch (e) {
|
||||
console.error("Invalid WS message:", e);
|
||||
}
|
||||
}
|
||||
|
||||
async handleAppOperation(req, ws) {
|
||||
let responseData;
|
||||
switch (req.app) {
|
||||
case "FORUM":
|
||||
responseData = await ForumHandler.handle(req.operation, req.msg, ws)
|
||||
break;
|
||||
|
||||
case "MESSAGES":
|
||||
responseData = MessagesHandler.handle(req.operation, req.msg, ws)
|
||||
break;
|
||||
|
||||
default:
|
||||
console.log("unknown ws message")
|
||||
}
|
||||
|
||||
let response = {
|
||||
...req
|
||||
}
|
||||
response.msg = responseData
|
||||
|
||||
if(!this.appOperationSchema.safeParse(response).success) throw new Error("Socket.handleMessage: Outgoing ws message has incorrect format!")
|
||||
ws.send(JSON.stringify(response))
|
||||
}
|
||||
|
||||
async handleFunction(req, ws) {
|
||||
console.log("func call: ", req.name, req.args)
|
||||
let responseData = await server[req.name](...req.args)
|
||||
|
||||
let response = {
|
||||
...req
|
||||
}
|
||||
response.data = responseData
|
||||
|
||||
console.log(response)
|
||||
|
||||
if(!this.functionCallSchema.safeParse(response).success) throw new Error("Socket.handleMessage: Outgoing ws message has incorrect format!")
|
||||
ws.send(JSON.stringify(response))
|
||||
}
|
||||
|
||||
broadcast(event) {
|
||||
if (!this.wss) return;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user