refactor: split up routers to separate lambdas (#8041)

* split up routers to separate lambdas

* fix responsemeta

* move

* add typeguards to make sure all endpoints are covered in the approuter

* prettier

* move slotsrouter

* split ssg/ssr

* make sure correct headers are sent on viewer.public

* make sure correct headers are sent + use ctx.prisma

* Fixed new prefetch broken by merge

* Fixes after merge

* Created separate API route for all tRPC routers

* More fixes from refactor

* Fixed tRPC query for slots

* Put back extra line

* Fixed type checks

* Removed Endpoint type check since it loads from client

* Reverted change in getSchedule test

* Fix trpc routes in expectations

* Fix one more route test

---------

Co-authored-by: Efraín Rochín <[email protected]>
Co-authored-by: Alex van Andel <[email protected]>
Co-authored-by: Peer Richelsen <[email protected]>
Co-authored-by: Keith Williams <[email protected]>
Co-authored-by: zomars <[email protected]>
Co-authored-by: Hariom Balhara <[email protected]>
This commit is contained in:
authored and GitHub committed 2023-05-05 13:19:10 -03:00
1 parent 98a3508b8f
commit cdba1920fc
27 files changed
+235 -96

No files matched your search

+66 -10
View File
@@ -12,6 +12,58 @@ import type { TRPCClientErrorLike } from "../react";
import type { inferRouterInputs, inferRouterOutputs, Maybe } from "../server";
import type { AppRouter } from "../server/routers/_app";
/**
* We deploy our tRPC router on multiple lambdas to keep number of imports as small as possible
* TODO: Make this dynamic based on folders in trpc server?
*/
const ENDPOINTS = [
"apiKeys",
"appRoutingForms",
"apps",
"auth",
"availability",
"bookings",
"deploymentSetup",
"eth",
"eventTypes",
"features",
"insights",
"payments",
"public",
"saml",
"slots",
"teams",
"users",
"viewer",
"webhook",
"workflows",
] as const;
export type Endpoint = (typeof ENDPOINTS)[number];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const resolveEndpoint = (links: any) => {
// TODO: Update our trpc routes so they are more clear.
// This function parses paths like the following and maps them
// to the correct API endpoints.
// - viewer.me - 2 segment paths like this are for logged in requests
// - viewer.public.i18n - 3 segments paths can be public or authed
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (ctx: any) => {
const parts = ctx.op.path.split(".");
let endpoint;
let path = '';
if (parts.length == 2) {
endpoint = parts[0] as keyof typeof links;
path = parts[1];
} else {
endpoint = parts[1] as keyof typeof links;
path = parts.splice(2, parts.length - 2).join('.');
}
return links[endpoint]({ ...ctx, op: { ...ctx.op, path } });
};
};
/**
* A set of strongly-typed React hooks from your `AppRouter` type signature with `createTRPCReact`.
* @link https://trpc.io/docs/v10/react#2-create-trpc-hooks
@@ -41,17 +93,21 @@ export const trpc = createTRPCNext<AppRouter, NextPageContext, "ExperimentalSusp
}),
splitLink({
// check for context property `skipBatch`
condition: (op) => {
return op.context.skipBatch === true;
},
condition: (op) => !!op.context.skipBatch,
// when condition is true, use normal request
true: httpLink({ url }),
// when condition is false, use batching
false: httpBatchLink({
url,
/** @link https://github.com/trpc/trpc/issues/2008 */
// maxBatchSize: 7
}),
true: (runtime) => {
const links = Object.fromEntries(
ENDPOINTS.map((endpoint) => [endpoint, httpLink({ url: url + "/" + endpoint })(runtime)])
);
return resolveEndpoint(links);
},
// when condition is false, use batch request
false: (runtime) => {
const links = Object.fromEntries(
ENDPOINTS.map((endpoint) => [endpoint, httpBatchLink({ url: url + "/" + endpoint })(runtime)])
);
return resolveEndpoint(links);
},
}),
],
/**