openapi-typescript-codegen icon indicating copy to clipboard operation
openapi-typescript-codegen copied to clipboard

Support NextJS 13 fetch options

Open benzsuankularb opened this issue 2 years ago • 6 comments

Since NextJS fetch has been introduced.

There is no way yet for the generated code to input the cache and next.revalidate fetch's options.

benzsuankularb avatar Apr 14 '23 17:04 benzsuankularb

Since next.revalidate and cache options aren't even part of OpenAPI. I think we should introduce nextjs-fetch client.

Maybe something like

export class NextJSFetchHttpRequest extends BaseHttpRequest {

    cache?: string;
    revalidate?: number;
    
    constructor(config: OpenAPIConfig) {
        super(config);
    }

    public override request<T>(options: ApiRequestOptions): CancelablePromise<T> {
        return __request(this.config, options); // TODO inject cache and revalidate
    }
}

benzsuankularb avatar Apr 14 '23 18:04 benzsuankularb

Or even better if we could add ApiRequest options which tie to each client.

For example, I could do something like this when I make an API request. But only when I generate the code using nextjs-fetch client.

myOpenApi.getUsers({ cache: 'no-store' });

benzsuankularb avatar Apr 14 '23 18:04 benzsuankularb

I faced the same issue and this is how i solved it for now

https://beta.nextjs.org/docs/api-reference/segment-config#dynamic

omermecitoglu avatar Apr 18 '23 18:04 omermecitoglu

Needed this too. I forked it and added quick hacky support on branch nextjsfetch in my fork: https://github.com/ferdikoomen/openapi-typescript-codegen/compare/master...justin-calleja:openapi-typescript-codegen:nextjsfetch

justin-calleja avatar Sep 18 '23 16:09 justin-calleja

Needed this too. I forked it and added quick hacky support on branch nextjsfetch in my fork: master...justin-calleja:openapi-typescript-codegen:nextjsfetch

@justin-calleja I might be missing something obvious, but I'm trying to use your patch but can't figure out how to pass a cache option to my request... Could you give an example?

fullyherge avatar Feb 15 '24 09:02 fullyherge

@fullyherge you first need to generate your TS by passing --client nextjs_fetch to the cli. Then, you should be able to pass in a second arg to your generated "SDK" which gets passed to the underlying fetch call - so you can pass:

        {
          next: { revalidate: 15 },
        },

for e.g. and it should get passed to fetch

justin-calleja avatar Feb 15 '24 10:02 justin-calleja