question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Option to dependency inject the request function

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
jesperkristensencommented, Mar 1, 2021

Unfortunately it looks like the --request parameter can only be specified when generating the API client, but not when calling the API, so it does not really solve my problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use dependency injection in .NET Azure Functions
Use injected dependencies​​ Constructor injection is used to make your dependencies available in a function. The use of constructor injection ...
Read more >
How to use Dependency Injection in Functional Programming
Dependency Injection is a technique to make the classes in Object Oriented Programming easier to test and configure.
Read more >
Dependencies - First Steps - FastAPI - tiangolo
"Dependency Injection" means, in programming, that there is a way for your code (in this case, your path operation functions) to declare things...
Read more >
Understanding Dependency Injection in .NET Core - Auth0
Here you are asking the IoC Container to create an instance of the HttpOrderSender class whenever a request for the IOrderSender type is...
Read more >
How would I pass a Web API Request through Dependency ...
IPrincipal is not appropriate to be injected into our services as it's web application related . When we pass this information down to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found