mirror of
https://github.com/roleypoly/roleypoly-v1.git
synced 2025-04-25 12:19:10 +00:00
RPC: temporarily disable argument guards, add cookie support for SSR
(now with full isomorphism... almost!)
This commit is contained in:
parent
f30ca78e40
commit
c32ee37ca5
5 changed files with 47 additions and 10 deletions
|
@ -1,4 +1,13 @@
|
|||
// @flow
|
||||
import RPCClient from '../rpc'
|
||||
|
||||
export default (new RPCClient({ forceDev: false })).rpc
|
||||
const client = new RPCClient({ forceDev: false })
|
||||
|
||||
export default client.rpc
|
||||
export const withCookies = (ctx: any) => {
|
||||
if (ctx.req != null) {
|
||||
return client.withCookies(ctx.req.headers.cookie)
|
||||
} else {
|
||||
return client.rpc
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,14 +1,16 @@
|
|||
import * as React from 'react'
|
||||
import RPC from '../config/rpc'
|
||||
import RPC, { withCookies } from '../config/rpc'
|
||||
|
||||
export default class TestRPC extends React.Component {
|
||||
static async getInitialProps (ctx) {
|
||||
const user = await withCookies(ctx).getCurrentUser()
|
||||
console.log(user)
|
||||
return {
|
||||
// hello: await RPC.hello('world')
|
||||
user
|
||||
}
|
||||
}
|
||||
|
||||
componentDidMount () {
|
||||
async componentDidMount () {
|
||||
window.$RPC = RPC
|
||||
}
|
||||
|
||||
|
@ -19,6 +21,11 @@ export default class TestRPC extends React.Component {
|
|||
}
|
||||
|
||||
render () {
|
||||
return <div>hello, { this.props.hello }</div>
|
||||
if (this.props.user == null) {
|
||||
return <div>hello stranger OwO</div>
|
||||
}
|
||||
|
||||
const { username, avatar, discriminator } = this.props.user
|
||||
return <div>hello, {username}#{discriminator} <img src={avatar} width={50} height={50} /></div>
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@ export default class RPCClient {
|
|||
baseUrl: string
|
||||
firstKnownHash: string
|
||||
recentHash: string
|
||||
cookieHeader: string
|
||||
|
||||
rpc: {
|
||||
[fn: string]: (...args: any[]) => Promise<mixed> | string
|
||||
|
@ -52,6 +53,11 @@ export default class RPCClient {
|
|||
}
|
||||
}
|
||||
|
||||
withCookies = (h: string) => {
|
||||
this.cookieHeader = h
|
||||
return this.rpc
|
||||
}
|
||||
|
||||
async updateCalls () {
|
||||
// this is for development only. doing in prod is probably dumb.
|
||||
const rsp = await superagent.get(this.baseUrl)
|
||||
|
@ -78,7 +84,11 @@ export default class RPCClient {
|
|||
|
||||
async call (fn: string, ...args: any[]): mixed {
|
||||
const req: RPCRequest = { fn, args }
|
||||
const rsp = await superagent.post(this.baseUrl).send(req).ok(() => true)
|
||||
const rq = superagent.post(this.baseUrl)
|
||||
if (this.cookieHeader != null) {
|
||||
rq.cookies = this.cookieHeader
|
||||
}
|
||||
const rsp = await rq.send(req).ok(() => true)
|
||||
const body: RPCResponse = rsp.body
|
||||
if (body.error === true) {
|
||||
throw RPCError.fromResponse(body, rsp.status)
|
||||
|
|
|
@ -32,7 +32,7 @@ export default class RPCServer {
|
|||
this.mapHash = fnv.hash(Object.keys(this.rpcMap)).str()
|
||||
|
||||
// call map for the client.
|
||||
this.rpcCalls = Object.keys(this.rpcMap).map(fn => ({ name: this.rpcMap[fn].name, args: this.rpcMap[fn].length - 1 }))
|
||||
this.rpcCalls = Object.keys(this.rpcMap).map(fn => ({ name: this.rpcMap[fn].name, args: 0 }))
|
||||
}
|
||||
|
||||
hookRoutes (router: betterRouter) {
|
||||
|
@ -68,9 +68,9 @@ export default class RPCServer {
|
|||
|
||||
// if call.length (which is the solid args list)
|
||||
// is longer than args, we have too little to call the function.
|
||||
if (args.length < call.length) {
|
||||
return this.rpcError(ctx, null, new RPCError(`RPC call ${fn}() with ${args.length} arguments does not exist.`, 400))
|
||||
}
|
||||
// if (args.length < call.length) {
|
||||
// return this.rpcError(ctx, null, new RPCError(`RPC call ${fn}() with ${args.length} arguments does not exist.`, 400))
|
||||
// }
|
||||
|
||||
try {
|
||||
const response = await call(ctx, ...args)
|
||||
|
|
11
rpc/user.js
Normal file
11
rpc/user.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
// @flow
|
||||
import { type AppContext } from '../Roleypoly'
|
||||
import { type Context } from 'koa'
|
||||
// import { type Guild } from 'discord.js'
|
||||
|
||||
export default ($: AppContext) => ({
|
||||
getCurrentUser (ctx: Context) {
|
||||
const { userId } = ctx.session
|
||||
return $.discord.getUserPartial(ctx.session.userId)
|
||||
}
|
||||
})
|
Loading…
Add table
Reference in a new issue