Issues with interceptors in Next.js #2996
Replies: 1 comment
-
|
The Hey API SDK client instance is initialized in a generated file (like If you add interceptors in files like For multiple auth methods, the SDK supports an If you need different auth/interceptor setups per project or per request, use Example for import type { CreateClientConfig } from './client/client.gen';
export const createClientConfig: CreateClientConfig = (config) => {
// Add a request interceptor for APIKEY
config.interceptors?.request.use((request) => {
request.headers.set('x-api-key', process.env.API_KEY!);
return request;
});
// Set up dynamic auth if needed
return {
...config,
auth: (authType) => {
if (authType === 'oauth') return process.env.OAUTH_TOKEN!;
if (authType === 'apikey') return process.env.API_KEY!;
return '';
},
};
};This approach ensures interceptors and auth are consistently applied, even in server actions and across monorepo projects. If you need per-request logic, create a new client instance with To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm guessing this is a setup issue.
I have a monorepo. Most of the projects in the monorepo will be using 1 or 2 API's we have access to. Some of them will use APIKEY as auth, others will use OAUTH/OpenID. So the apis have correct OpenAPI config for those.
Hey API seems to only support one Auth method through the SDK.
So for the projects with APIKEY I will have to use an interceptor.
Note that there is a single JIT project, separate from the Nextjs files inside the PNPM Workpsaces monorepo for the generated code from Hey API. It will be reused by all the nextjs projects.
Problem is where to initialize this interceptor. I have tried to set the interceptor via instrumentation.ts and in rootlayout.tsx but it seems I'm not getting the same instance of the client. At least when I run a call from a SDK method no interceptor is present.
They are running inside server actions so they might be a part of the problem.
if I add the interceptor inside the RunTimePathConfig file it only works after the first call is made.
We really need a way to use multiple Auth methods, and add Interceptors in a consistent manner.
Beta Was this translation helpful? Give feedback.
All reactions