This repository has been archived by the owner on Oct 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Merging appRouter and depend on api from web
- Loading branch information
Showing
16 changed files
with
7,972 additions
and
2,055 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
Oops, something went wrong.