+16









Carina Wollendorfer
GitHub
Alex van Andel
sajanlamsal
CarinaWolli
alannnc
Leo Giovanetti
Peer Richelsen
Hariom Balhara
Udit Takkar
Nitin Panghal
Omar López
Peer Richelsen
Shivam Kalra
Richard Poelderl
Crowdin Bot
Joe Au-Yeung
Nafees Nazik
Chiranjeev Vishnoi
Denzil Samuel
Syed Ali Shahbaz
nitinpanghal
Ahmad
Annlee Fores
Keith Williams
Vijay
68bd877c5b
Co-authored-by: Alex van Andel <me@alexvanandel.com> Co-authored-by: sajanlamsal <saznlamsal@gmail.com> Co-authored-by: CarinaWolli <wollencarina@gmail.com> Co-authored-by: alannnc <alannnc@gmail.com> Co-authored-by: Leo Giovanetti <hello@leog.me> Co-authored-by: Peer Richelsen <peeroke@gmail.com> Co-authored-by: Hariom Balhara <hariombalhara@gmail.com> Co-authored-by: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com> Co-authored-by: Nitin Panghal <nitin.panghal@unthinkable.co> Co-authored-by: Omar López <zomars@me.com> Co-authored-by: Peer Richelsen <peer@cal.com> Co-authored-by: zomars <zomars@me.com> Co-authored-by: Shivam Kalra <shivamkalra98@gmail.com> Co-authored-by: Richard Poelderl <richard.poelderl@gmail.com> Co-authored-by: Crowdin Bot <support+bot@crowdin.com> Co-authored-by: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com> Co-authored-by: Nafees Nazik <84864519+G3root@users.noreply.github.com> Co-authored-by: Chiranjeev Vishnoi <66114276+Chiranjeev-droid@users.noreply.github.com> Co-authored-by: Denzil Samuel <71846487+samueldenzil@users.noreply.github.com> Co-authored-by: Syed Ali Shahbaz <52925846+alishaz-polymath@users.noreply.github.com> Co-authored-by: nitinpanghal <43965732+nitinpanghal@users.noreply.github.com> Co-authored-by: Ahmad <57593864+Ahmadkashif@users.noreply.github.com> Co-authored-by: Annlee Fores <annleefores@gmail.com> Co-authored-by: Keith Williams <keithwillcode@gmail.com> Co-authored-by: Vijay <vijayraghav22@gmail.com>
160 lines
5.2 KiB
TypeScript
160 lines
5.2 KiB
TypeScript
import type { NextPageContext } from "next/types";
|
||
import superjson from "superjson";
|
||
|
||
import { httpBatchLink } from "../client/links/httpBatchLink";
|
||
import { httpLink } from "../client/links/httpLink";
|
||
import { loggerLink } from "../client/links/loggerLink";
|
||
import { splitLink } from "../client/links/splitLink";
|
||
import { createTRPCNext } from "../next";
|
||
// ℹ️ Type-only import:
|
||
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export
|
||
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 = [
|
||
"admin",
|
||
"apiKeys",
|
||
"appRoutingForms",
|
||
"apps",
|
||
"auth",
|
||
"availability",
|
||
"appBasecamp3",
|
||
"bookings",
|
||
"deploymentSetup",
|
||
"eventTypes",
|
||
"features",
|
||
"insights",
|
||
"payments",
|
||
"public",
|
||
"saml",
|
||
"slots",
|
||
"teams",
|
||
"organizations",
|
||
"users",
|
||
"viewer",
|
||
"webhook",
|
||
"workflows",
|
||
"appsRouter",
|
||
"googleWorkspace",
|
||
"oAuth",
|
||
] 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
|
||
*/
|
||
export const trpc = createTRPCNext<AppRouter, NextPageContext, "ExperimentalSuspense">({
|
||
config() {
|
||
const url =
|
||
typeof window !== "undefined"
|
||
? "/api/trpc"
|
||
: process.env.VERCEL_URL
|
||
? `https://${process.env.VERCEL_URL}/api/trpc`
|
||
: `${process.env.NEXT_PUBLIC_WEBAPP_URL}/api/trpc`;
|
||
|
||
/**
|
||
* 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`
|
||
condition: (op) => !!op.context.skipBatch,
|
||
// when condition is true, use normal request
|
||
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);
|
||
},
|
||
}),
|
||
],
|
||
/**
|
||
* @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,
|
||
});
|
||
|
||
export const transformer = superjson;
|
||
|
||
export type RouterInputs = inferRouterInputs<AppRouter>;
|
||
export type RouterOutputs = inferRouterOutputs<AppRouter>;
|