Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
chore: Merging appRouter and depend on api from web
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyxiao committed Apr 20, 2024
1 parent 88482df commit 85d0ac3
Show file tree
Hide file tree
Showing 16 changed files with 7,972 additions and 2,055 deletions.
2 changes: 1 addition & 1 deletion apps/web/lib-client/trpcReact.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {TRPCReact} from '@openint/engine-frontend'
import {_trpcReact} from '@openint/engine-frontend'
import type {AppRouter} from '../lib-server/appRouter'
import type {AppRouter} from '@openint/api'

/** Move this somewhere where other components can access */
export const trpcReact = _trpcReact as unknown as TRPCReact<AppRouter>
43 changes: 2 additions & 41 deletions apps/web/lib-common/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,7 @@
import type {clerkClient} from '@clerk/nextjs'
import {
kApikeyMetadata,
kWebhookUrlMetadata,
} from '@openint/app-config/constants'
import {zId} from '@openint/cdk'
import type {RouterOutput} from '@openint/engine-backend'
import {z, zRecord} from '@openint/util'

export type ClerkOrg = Awaited<
ReturnType<(typeof clerkClient)['organizations']['getOrganization']>
>

export type ClerkUser = Awaited<
ReturnType<(typeof clerkClient)['users']['getUser']>
>

export function zOrgMetadata() {
return z.object({})
}

export const zAuth = {
organization: z.object({
id: zId('org'),
slug: z.string(),
publicMetadata: zOrgMetadata(),
privateMetadata: z.object({
[kApikeyMetadata]: z.string().optional(),
[kWebhookUrlMetadata]: z.string().optional(),
}),
}),

user: z.object({
id: zId('user'),
publicMetadata: z.object({}),
privateMetadata: z.object({}),
unsafeMetadata: z.object({}),
}),
}

export type ZAuth = {
[k in keyof typeof zAuth]: z.infer<(typeof zAuth)[k]>
}
import type {z} from '@openint/util'
import {zRecord} from '@openint/util'

type Pipeline = RouterOutput['listPipelines'][number]
type Resource = RouterOutput['listConnections'][number]
Expand Down
128 changes: 0 additions & 128 deletions apps/web/lib-server/appRouter.ts

This file was deleted.

6 changes: 3 additions & 3 deletions apps/web/pages/api/trpc/[...trpc].ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {contextFactory} from '@openint/app-config/backendConfig'
import {respondToCORS, serverGetViewer} from '@/lib-server/server-helpers'
import '@openint/app-config/register.node'
import type {TRPCError} from '@trpc/server'
import * as trpcNext from '@trpc/server/adapters/next'
import type {NextApiHandler} from 'next'
import {contextFactory} from '@openint/app-config/backendConfig'
import {appRouter} from '@openint/api'
import type {Id} from '@openint/cdk'
import type {RouterContext} from '@openint/engine-backend'
import {parseWebhookRequest} from '@openint/engine-backend'
import {fromMaybeArray, HTTPError} from '@openint/util'
import {appRouter} from '@/lib-server/appRouter'
import {respondToCORS, serverGetViewer} from '@/lib-server/server-helpers'

/** https://trpc.io/docs/server/error-handling */
const HTTP_CODE_TO_TRPC_CODE = {
Expand Down
2 changes: 1 addition & 1 deletion apps/web/pages/api/trpc/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {NextApiRequest, NextApiResponse} from 'next'
import {renderTrpcPanel} from 'trpc-panel'
import {appRouter} from '@/lib-server/appRouter'
import {appRouter} from '@openint/api'

export default function handler(req: NextApiRequest, res: NextApiResponse) {
// req.url is normally `/api/trpc` already which is the right place
Expand Down
2 changes: 1 addition & 1 deletion apps/web/pages/api/v0/[...trpc].ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {createOpenApiNextHandler} from '@lilyrose2798/trpc-openapi'
import {appRouter} from '@openint/api'
import {respondToCORS} from '@/lib-server'
import {appRouter} from '@/lib-server/appRouter'
import '@openint/app-config/register.node'
import type {NextApiHandler} from 'next'
import {createContext, onError} from '../trpc/[...trpc]'
Expand Down
91 changes: 88 additions & 3 deletions new-pattern/api/appRouter.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,97 @@
// not sure about directly depending on vdk from api, but anyways
import type {
ZodOpenApiComponentsObject,
ZodOpenApiPathsObject,
} from '@lilyrose2798/trpc-openapi/dist/generator'
import {generateOpenApiDocument} from '@lilyrose2798/trpc-openapi/dist/generator'
import {getServerUrl} from 'apps/app-config/constants'
import {flatRouter, outgoingWebhookEventMap} from 'packages/engine-backend'
import {mgmtRouter} from '@openint/mgmt'
import {trpc} from '@openint/vdk'
import {mapKeys, mapValues, publicProcedure, trpc, z} from '@openint/vdk'
import {crmRouter} from '@openint/vertical-crm'
import {salesEngagementRouter} from '@openint/vertical-sales-engagement2'
import {authRouter} from './authRouter'

export const appRouter = trpc.router({
// public: publicRouter,
export const publicRouter = trpc.router({
getOpenapiDocument: publicProcedure
.meta({openapi: {method: 'GET', path: '/openapi.json'}})
.input(z.void())
.output(z.unknown())
.query((): unknown => getOpenAPISpec()),
})

export const _appRouter = trpc.router({
public: publicRouter,
mgmt: mgmtRouter,
salesEngagement: salesEngagementRouter,
crm: crmRouter,
})

export const appRouter = trpc.mergeRouters(flatRouter, authRouter, _appRouter)

export type AppRouter = typeof appRouter

function assertNoSlash(name: string) {
if (name.includes('/')) {
throw new Error(`Event name ${name} containing '/' is not supported`)
}
return name
}

export function oasWebhooksEventsMap(
eMap: Record<string, {data: z.AnyZodObject}>,
) {
const webhooks = mapValues(
eMap,
(_, name): ZodOpenApiPathsObject[string] => ({
post: {
requestBody: {
content: {
'application/json': {
schema: {
$ref: `#/components/schemas/webhooks.${assertNoSlash(name)}`,
},
},
},
},
responses: {},
},
}),
)
type Schemas = NonNullable<ZodOpenApiComponentsObject['schemas']>
const components = {
schemas: mapKeys(
mapValues(eMap, (shape, name): Schemas[string] =>
z.object({...shape, name: z.literal(name), id: z.string().optional()}),
),
(name) => `webhooks.${name}`,
),
}
return {webhooks, components}
}

export function getOpenAPISpec() {
const {webhooks, components} = oasWebhooksEventsMap(outgoingWebhookEventMap)
const oas = generateOpenApiDocument(appRouter, {
openApiVersion: '3.1.0', // Want jsonschema
title: 'OpenInt OpenAPI',
version: '0.0.0',
securitySchemes: {
apikey: {
type: 'apiKey',
name: 'x-apikey',
in: 'header',
},
},
baseUrl: getServerUrl(null) + '/api/v0',
webhooks,
components,
})
// Unfortunately trpc-openapi is missing bunch of options...
oas.security = [{apikey: []}]
return oas
}

if (require.main === module) {
console.log(JSON.stringify(getOpenAPISpec(), null, 2))
}
Loading

0 comments on commit 85d0ac3

Please sign in to comment.