Skip to content

Option to dependency inject the request function #465

@jesperkristensen

Description

@jesperkristensen

I would like to have an option to dependency inject the request function in each operation, such that the services don't have a dependency on the core modules, and so that --exportCore false will produce typescript code that compiles.

For example:

type __request = (options: ApiRequestOptions) => Promise<ApiResult>

export class MyService {
    public static async myOperation(
        request: __request,
        myParam: string,
        requestBody: MyRequestModel,
    ): Promise<MyResponseModel> {
        const result = await request({
            method: 'POST',
            path: `/my-api/${myParam}`,
            body: requestBody,
        });
        return result.body;
    }
}

Use case 1:

I consume multiple OpenAPIs from multiple different microservices. I need to make modifications to the core request module to make it support React Native. I need to make modifications to the core OpenAPI module to fetch the correct token. I want to maintain one copy of these modifications across the apis generated for all the microservices, instead of duplicating the modifications per microservice.

Use case 2:

I modified the core request module to add a timeout. I want to use different timeouts in different operation calls. If the request function was given as a parameter to the operation, I could pass a modified request function that included the timeout.

Example:

const r = await MyService.myOperation(makeRequest({timeout: 10}), "string", {object: true})

May be related to #428 and #400

Thanks for a great library.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions