hey-api / openapi-ts

✨ Turn your OpenAPI specification into a beautiful TypeScript client
https://heyapi.vercel.app
Other
1.07k stars 88 forks source link

Allow passing OpenAPI config to individual request. #465

Closed seriouslag closed 4 months ago

seriouslag commented 5 months ago

Description

It would be nice to pass the OpenAPI config per request if needed. This can be helpful if individual requests need specific configurations.

/** Generated service.gen.ts */
// This file is auto-generated by @hey-api/openapi-ts

import type { CancelablePromise } from './core/CancelablePromise';
import { OpenAPI } from './core/OpenAPI';
import { request as __request } from './core/request';
import type { $OpenApiTs } from './types.gen';

export class DefaultService {
    /**
     * Creates a new pet in the store. Duplicates are allowed
     * @param data The data for the request.
     * @param data.requestBody Pet to add to the store
     * @returns Pet pet response
     * @returns Error unexpected error
     * @throws ApiError
     */
    public static addPet(data: $OpenApiTs['/pets']['post']['req']): CancelablePromise<$OpenApiTs['/pets']['post']['res'][200] | $OpenApiTs['/pets']['post']['res'][200]> {
        return __request(OpenAPI, {
            method: 'POST',
            url: '/pets',
            body: data.requestBody,
            mediaType: 'application/json'
        });
    }

}
/** Proposal of how the file should be generated service.gen.ts */
// This file is auto-generated by @hey-api/openapi-ts

import type { CancelablePromise } from './core/CancelablePromise';
import { OpenAPI, merge } from './core/OpenAPI';
import type { OpenAPIConfig } from './core/OpenAPI';
import { request as __request } from './core/request';
import type { $OpenApiTs } from './types.gen';

export class DefaultService {
    /**
     * Creates a new pet in the store. Duplicates are allowed
     * @param data The data for the request.
     * @param data.requestBody Pet to add to the store
     * @returns Pet pet response
     * @returns Error unexpected error
     * @throws ApiError
     */
    public static addPet({ requestBody }: $OpenApiTs['/pets']['post']['req'], overrideConfig?: OpenAPIConfig): CancelablePromise<$OpenApiTs['/pets']['post']['res'][200] | $OpenApiTs['/pets']['post']['res'][200]> {
        const config = merge(overrideConfig, config);
        return __request(config, {
            method: 'POST',
            url: '/pets',
            body: requestBody,
            mediaType: 'application/json'
        });
    }

}
mrlubos commented 5 months ago

Hey @seriouslag, do you have a specific use case in mind?

seriouslag commented 5 months ago

Some requests require specific headers or cookies. Would be nice to be able to set them at the method level.

mrlubos commented 5 months ago

Why can't/don't want you to set them using interceptors?

seriouslag commented 5 months ago

In the interceptor I would have to check for a specific url and method.

Passing the override configuration in the method, the consumer would not need to check anything. Cleaner API, easy to understand.

Interceptors should be used when something applies to every API call not a specific call in my opinion.

ArtemAstakhov commented 5 months ago

Hey @seriouslag, do you have a specific use case in mind?

Hi!

For example, it is not possible to use generated code on SSR because all requests use global configuration. Being able to pass the config to a specific request could solve this problem. The ability to create OpenAPI instances could also solve this problem

lavocatt commented 5 months ago

Hi!

I have a use case for this. I'm running a plugin for OpenShift that needs to be able to connect to all the pods that are deployed and issue requests to all the API servers to get some data to display.

Having some support to dynamically change the endpoint would be great for this!

mrlubos commented 5 months ago

@lavocatt We should be able to support this with the upcoming clients

mrlubos commented 4 months ago

@ArtemAstakhov @lavocatt I don't know if you're using Fetch API, but if yes, this is available in the new Fetch API client. You can play with the demo and see what you can pass to each request. Would love your feedback!

cyrilfr commented 2 months ago

is that available in the Angular client?

mrlubos commented 2 months ago

Hey @cyrilfr, this is available in the new, standalone clients (Fetch API and Axios). If you can make Angular work with either of them then the answer would be yes, but I did not explicitly try it yet https://github.com/hey-api/openapi-ts/issues/667#issuecomment-2233629443

cyrilfr commented 2 months ago

OK, I'm going to use the fetch client for now, but it's not injectable in the Angular way. I'll add a script that will remove the static keyword and add Angular's @Injectable() annotation to all generated services. It's pretty messy but it's the only way to have up-to-date support right now.