Your API may require dynamic authentication where credentials need to be generated or refreshed for each request, such as signing short-lived JWTs or rotating tokens. TypeScript SDKs support this pattern through custom fetcher middleware.
The recommended way to implement dynamic authentication in TypeScript SDKs is to use a custom fetcher. This acts as middleware for all requests, allowing you to inject authentication logic in a single place without overriding individual methods.
When you enable allowCustomFetcher in your generator configuration, the generated SDK accepts a fetcher parameter in the client options. This fetcher wraps all HTTP requests, giving you a single injection point for authentication logic.
Here’s how to implement JWT signing with token memoization:
Add allowCustomFetcher: true to your generators.yml:
Add the jsonwebtoken dependency using the packageJson config option so it gets merged into the generated package.json:
See the TypeScript configuration page for all available packageJson options.
Create a fetcher function that wraps the default fetcher and injects JWT authentication:
Extend the generated client to use the custom fetcher:
This pattern uses ConstructorParameters to infer the exact options type from the generated client, ensuring compatibility with all client options (headers, timeoutInSeconds, maxRetries, etc.) without hardcoding them. This keeps the wrapper future-proof as the generator adds new options.
Update your index.ts to export the wrapper instead of the generated client:
This same pattern works for other dynamic authentication scenarios:
allowCustomFetcher option must be enabled in your generators.yml for the fetcher parameter to be available in BaseClientOptions../core/fetcher (or the appropriate path in your generated SDK) to wrap it with your custom logicAuthorization header. Consider using a different header name or conditionally setting the headeriat and exp claims to match JWT standards