2023-03-25 00:59:04 +00:00
|
|
|
|
import type { NextPageContext } from "next/types";
|
2021-10-07 15:43:20 +00:00
|
|
|
|
import superjson from "superjson";
|
|
|
|
|
|
2023-04-25 22:39:47 +00:00
|
|
|
|
import { httpBatchLink } from "../client/links/httpBatchLink";
|
2023-02-10 20:05:02 +00:00
|
|
|
|
import { httpLink } from "../client/links/httpLink";
|
|
|
|
|
import { loggerLink } from "../client/links/loggerLink";
|
|
|
|
|
import { splitLink } from "../client/links/splitLink";
|
|
|
|
|
import { createTRPCNext } from "../next";
|
2022-07-22 17:27:06 +00:00
|
|
|
|
// ℹ️ Type-only import:
|
|
|
|
|
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export
|
2023-02-10 20:05:02 +00:00
|
|
|
|
import type { TRPCClientErrorLike } from "../react";
|
|
|
|
|
import type { inferRouterInputs, inferRouterOutputs, Maybe } from "../server";
|
2022-07-22 17:27:06 +00:00
|
|
|
|
import type { AppRouter } from "../server/routers/_app";
|
2021-09-27 14:47:55 +00:00
|
|
|
|
|
2023-05-05 16:19:10 +00:00
|
|
|
|
/**
|
|
|
|
|
* 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",
|
2023-05-06 07:50:52 +00:00
|
|
|
|
"appsRouter",
|
2023-05-05 16:19:10 +00:00
|
|
|
|
] 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;
|
2023-05-06 07:50:52 +00:00
|
|
|
|
let path = "";
|
2023-05-05 16:19:10 +00:00
|
|
|
|
if (parts.length == 2) {
|
|
|
|
|
endpoint = parts[0] as keyof typeof links;
|
|
|
|
|
path = parts[1];
|
|
|
|
|
} else {
|
|
|
|
|
endpoint = parts[1] as keyof typeof links;
|
2023-05-06 07:50:52 +00:00
|
|
|
|
path = parts.splice(2, parts.length - 2).join(".");
|
2023-05-05 16:19:10 +00:00
|
|
|
|
}
|
|
|
|
|
return links[endpoint]({ ...ctx, op: { ...ctx.op, path } });
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2021-09-27 14:47:55 +00:00
|
|
|
|
/**
|
2022-11-10 23:40:01 +00:00
|
|
|
|
* 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
|
2021-09-27 14:47:55 +00:00
|
|
|
|
*/
|
2023-03-25 00:59:04 +00:00
|
|
|
|
export const trpc = createTRPCNext<AppRouter, NextPageContext, "ExperimentalSuspense">({
|
2023-02-10 20:05:02 +00:00
|
|
|
|
config() {
|
|
|
|
|
const url =
|
|
|
|
|
typeof window !== "undefined"
|
|
|
|
|
? "/api/trpc"
|
|
|
|
|
: process.env.VERCEL_URL
|
|
|
|
|
? `https://${process.env.VERCEL_URL}/api/trpc`
|
2023-04-11 16:19:27 +00:00
|
|
|
|
: `${process.env.NEXT_PUBLIC_WEBAPP_URL}/api/trpc`;
|
2023-02-10 20:05:02 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If you want to use SSR, you need to use the server's full URL
|
|
|
|
|
* @link https://trpc.io/docs/ssr
|
|
|
|
|
*/
|
|
|
|
|
return {
|
|
|
|
|
/**
|
|
|
|
|
* @link https://trpc.io/docs/links
|
|
|
|
|
*/
|
|
|
|
|
links: [
|
|
|
|
|
// adds pretty logs to your console in development and logs errors in production
|
|
|
|
|
loggerLink({
|
|
|
|
|
enabled: (opts) =>
|
|
|
|
|
!!process.env.NEXT_PUBLIC_DEBUG || (opts.direction === "down" && opts.result instanceof Error),
|
|
|
|
|
}),
|
|
|
|
|
splitLink({
|
|
|
|
|
// check for context property `skipBatch`
|
2023-05-05 16:19:10 +00:00
|
|
|
|
condition: (op) => !!op.context.skipBatch,
|
2023-02-10 20:05:02 +00:00
|
|
|
|
// when condition is true, use normal request
|
2023-05-05 16:19:10 +00:00
|
|
|
|
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);
|
|
|
|
|
},
|
2023-02-10 20:05:02 +00:00
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
/**
|
|
|
|
|
* @link https://react-query.tanstack.com/reference/QueryClient
|
|
|
|
|
*/
|
|
|
|
|
queryClientConfig: {
|
|
|
|
|
defaultOptions: {
|
|
|
|
|
queries: {
|
|
|
|
|
/**
|
|
|
|
|
* 1s should be enough to just keep identical query waterfalls low
|
|
|
|
|
* @example if one page components uses a query that is also used further down the tree
|
|
|
|
|
*/
|
|
|
|
|
staleTime: 1000,
|
|
|
|
|
/**
|
|
|
|
|
* Retry `useQuery()` calls depending on this function
|
|
|
|
|
*/
|
|
|
|
|
retry(failureCount, _err) {
|
|
|
|
|
const err = _err as never as Maybe<TRPCClientErrorLike<AppRouter>>;
|
|
|
|
|
const code = err?.data?.code;
|
|
|
|
|
if (code === "BAD_REQUEST" || code === "FORBIDDEN" || code === "UNAUTHORIZED") {
|
|
|
|
|
// if input data is wrong or you're not authorized there's no point retrying a query
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
const MAX_QUERY_RETRIES = 3;
|
|
|
|
|
return failureCount < MAX_QUERY_RETRIES;
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
/**
|
|
|
|
|
* @link https://trpc.io/docs/data-transformers
|
|
|
|
|
*/
|
|
|
|
|
transformer: superjson,
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
/**
|
|
|
|
|
* @link https://trpc.io/docs/ssr
|
|
|
|
|
*/
|
|
|
|
|
ssr: false,
|
|
|
|
|
});
|
2022-09-29 16:58:29 +00:00
|
|
|
|
|
2021-10-07 15:43:20 +00:00
|
|
|
|
export const transformer = superjson;
|
2021-09-27 16:09:19 +00:00
|
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
|
export type RouterInputs = inferRouterInputs<AppRouter>;
|
|
|
|
|
export type RouterOutputs = inferRouterOutputs<AppRouter>;
|