Skip to content

Commit

Permalink
Fix the type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
AriaMinaei committed Nov 14, 2023
1 parent da5ab2e commit be366e5
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 27 deletions.
5 changes: 3 additions & 2 deletions packages/app/src/server/api/routes/studioAuthRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,10 @@ export const studioAuthRouter = t.createRouter({
return {canEdit: false, reason: 'AccessTokenInvalid'}
}
const {userId} = payload
const proj = await prisma.project.findFirst({
const proj = await prisma.workspace.findFirst({
where: {
userId,
// TODO check if user has access to project
// userId,
id: opts.input.projectId,
},
})
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/server/api/routes/workspaceRouter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {z} from 'zod'
import * as t from '../trpc'
import prisma from 'src/prisma'
import prisma from '../../../prisma'
import {TRPCError} from '@trpc/server'

export const workspaceRouter = t.createRouter({
Expand Down
1 change: 1 addition & 0 deletions packages/saaz/src/front/SaazFront.ts
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@ export class SaazFront<
await this._storage.transaction(async (t) => {
await t.deleteSession(emptySession.peerId)
})
void this._backend.closePeer({peerId: emptySession.peerId})
}
},
)
Expand Down
15 changes: 8 additions & 7 deletions packages/sync-server/src/appClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {AppTrpcRouter} from '@theatre/app/src/server/trpc/routes'
import type {AppRouter} from '@theatre/app/src/server/api/root'

import {createTRPCProxyClient, httpBatchLink} from '@trpc/client'
import superjson from 'superjson'

Expand All @@ -9,7 +10,7 @@ if (!appHost.startsWith('http')) {
appHost = 'https://' + appHost
}

const appClient = createTRPCProxyClient<AppTrpcRouter>({
const appClient = createTRPCProxyClient<AppRouter>({
links: [
httpBatchLink({
url: appHost + '/api/trpc',
Expand All @@ -18,10 +19,10 @@ const appClient = createTRPCProxyClient<AppTrpcRouter>({
transformer: superjson,
})

if (process.env.NODE_ENV === 'development') {
void appClient.healthCheck.query({name: 'the lib'}).then((res) => {
console.log('app/healthCheck', res)
})
}
// if (process.env.NODE_ENV === 'development') {
// void appClient..query({name: 'the lib'}).then((res) => {
// console.log('app/healthCheck', res)
// })
// }

export default appClient
30 changes: 30 additions & 0 deletions packages/sync-server/src/trpc/routes/projectStateRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ export const projectState = createRouter({
return getSaazBack(opts.input.dbName).updatePresence(opts.input.opts)
}),

saaz_closePeer: procedure
.input(
z.object({
dbName: z.string(),
opts: z.object({peerId: z.string()}),
studioAuth,
}),
)
.output(z.any())
.mutation(async (opts) => {
await opts.ctx.requireValidSession(opts)
return getSaazBack(opts.input.dbName).closePeer(opts.input.opts)
}),

saaz_getUpdatesSinceClock: procedure
.input(z.object({dbName: z.string(), opts: z.any(), studioAuth}))
.output(z.any())
Expand All @@ -50,6 +64,22 @@ export const projectState = createRouter({
)
}),

saaz_getLastIncorporatedPeerClock: procedure
.input(
z.object({
dbName: z.string(),
opts: z.object({peerId: z.string()}),
studioAuth,
}),
)
.output(z.any())
.query(async (opts) => {
await opts.ctx.requireValidSession(opts)
return getSaazBack(opts.input.dbName).getLastIncorporatedPeerClock(
opts.input.opts,
)
}),

saaz_subscribe: procedure
.input(z.object({dbName: z.string(), opts: z.any(), studioAuth}))
.output(z.any())
Expand Down
4 changes: 2 additions & 2 deletions packages/sync-server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"baseUrl": ".",
"composite": true,
"paths": {
"@prisma/client": ["./prisma/client-generated"],
"@theatre/utils/*": ["../utils/src/*"]
"@theatre/utils/*": ["../utils/src/*"],
"@theatre/app/*": ["../app/src/*"]
}
},
"references": [{"path": "../utils"}, {"path": "../app"}, {"path": "../saaz"}],
Expand Down
34 changes: 30 additions & 4 deletions theatre/studio/src/StudioStore/StudioStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import type {OnDiskState} from '@theatre/sync-server/state/types/core'
import * as Saaz from '@theatre/saaz'
import type {ProjectId} from '@theatre/sync-server/state/types/core'
import AppLink from '@theatre/studio/SyncStore/AppLink'
import type {
TrpcClientWrapped,
} from '@theatre/studio/SyncStore/SyncStoreAuth';
import SyncStoreAuth from '@theatre/studio/SyncStore/SyncStoreAuth'
import SyncServerLink from '@theatre/studio/SyncStore/SyncServerLink'
import {schema} from '@theatre/sync-server/state/schema'
Expand Down Expand Up @@ -111,7 +114,7 @@ export default class StudioStore {
}

const backend =
typeof window === 'undefined' || true
typeof window === 'undefined'
? new SaazBack({
storageAdapter: new Saaz.BackMemoryAdapter(),
dbName: 'test',
Expand Down Expand Up @@ -302,11 +305,11 @@ export default class StudioStore {
return this._auth.authenticate(opts)
}

get appApi() {
get appApi(): TrpcClientWrapped<AppLink['api']> {
return this._auth.appApi
}

get syncServerApi() {
get syncServerApi(): TrpcClientWrapped<SyncServerLink['api']> {
return this._auth.syncServerApi
}
}
Expand Down Expand Up @@ -342,6 +345,27 @@ function createTrpcBackend(
})
}

const getLastIncorporatedPeerClock: Saaz.SaazBackInterface['getLastIncorporatedPeerClock'] =
async (opts) => {
const dbName = await dbNamePromise

return await syncServerApi.projectState.saaz_getLastIncorporatedPeerClock.query(
{
dbName,
opts,
},
)
}

const closePeer: Saaz.SaazBackInterface['closePeer'] = async (opts) => {
const dbName = await dbNamePromise

return await syncServerApi.projectState.saaz_closePeer.mutate({
dbName,
opts,
})
}

const subscribe: Saaz.SaazBackInterface['subscribe'] = async (
opts,
onUpdate,
Expand All @@ -365,7 +389,9 @@ function createTrpcBackend(
return {
applyUpdates,
getUpdatesSinceClock,
subscribe: subscribe,
subscribe,
updatePresence,
closePeer,
getLastIncorporatedPeerClock,
}
}
12 changes: 3 additions & 9 deletions theatre/studio/src/SyncStore/AppLink.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type {AppTrpcRouter} from '@theatre/app/trpc/routes'
import type {AppRouter} from '@theatre/app/server/api/root'
import type {CreateTRPCProxyClient} from '@trpc/client'
import {createTRPCProxyClient, httpBatchLink} from '@trpc/client'
import superjson from 'superjson'

export default class AppLink {
private _client!: CreateTRPCProxyClient<AppTrpcRouter>
private _client!: CreateTRPCProxyClient<AppRouter>

constructor(private _webAppUrl: string) {
if (process.env.NODE_ENV === 'test') return
this._client = createTRPCProxyClient<AppTrpcRouter>({
this._client = createTRPCProxyClient<AppRouter>({
links: [
httpBatchLink({
url: _webAppUrl + '/api/trpc',
Expand All @@ -21,12 +21,6 @@ export default class AppLink {
],
transformer: superjson,
})

if (process.env.NODE_ENV === 'development' && false) {
void this._client.healthCheck.query({name: 'the lib'}).then((res) => {
console.log('app/healthCheck', res)
})
}
}

get api() {
Expand Down
2 changes: 1 addition & 1 deletion theatre/studio/src/SyncStore/SyncServerLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default class SyncServerLink {
}
}

get api() {
get api(): CreateTRPCProxyClient<SyncServerRootRouter> {
return this._client
}
}
2 changes: 1 addition & 1 deletion theatre/studio/src/SyncStore/SyncStoreAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ function wrapTrpcClientWithAuth<Client extends {}>(
return pr as $IntentionalAny
}

type TrpcClientWrapped<Client extends {}> = {
export type TrpcClientWrapped<Client extends {}> = {
[K in keyof Client]: K extends 'query' | 'mutate' | 'subscribe'
? TrpcProcedureWrapped<Client[K]>
: Client[K] extends {}
Expand Down

1 comment on commit be366e5

@vercel
Copy link

@vercel vercel bot commented on be366e5 Nov 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.