openapistack / openapi-client-axios

JavaScript client library for consuming OpenAPI-enabled APIs with axios
https://openapistack.co
MIT License
558 stars 67 forks source link

Initialize client using typegen type, excluding openapi definition #80

Closed ryan-henness-trimble closed 3 years ago

ryan-henness-trimble commented 3 years ago

I was wondering if there is a way that I am able to initialize a client without providing the OpenAPIClientAxios a definition when I've already got a generated type using typegen?

My issue is that the definition will not be public, so I cannot access it at runtime.

const api = new OpenAPIClientAxios({ definition: './oc-openapi.json' }); // Don't want this definition to be passed
api.withServer(OcOpenApiClient.environments[environment]);
api.getClient<Client>().then(client => OcOpenApiClient.instance = client);
anttiviljami commented 3 years ago

@ryan-henness-trimble apologies but I don't really understand the question 😅

A client without a definition would be just a plain axios client with no operation methods, right?

ryan-henness-trimble commented 3 years ago

@anttiviljami ahh yea sorry, I'm trying to figure a better way to ask the question

I think I'm wondering if there is a way to instantiate OpenAPIClientAxios without passing a definition, since calling api.getClient<Client> contains the generated Client type with all of the same information that's within oc-openapi.json

anttiviljami commented 3 years ago

Sorry that still doesn’t seem to make any sense to me.

What’s your use case exactly?

The definition being passed to generate operation methods is the main functionality of this library. I really don’t see a good reason to use this library without passing the definition file

ryan-henness-trimble commented 3 years ago

Ahh okay, I think I'm just misunderstanding

Our use case is: create an NPM package that contains code to initialize a client specific to our API. So basically, this NPM package is a Client SDK for our API

We've got a .NET app that generates the oc-openapi.d.ts, and oc-openapi.json files. The .NET app also packages those two files along with code to generate a client into the NPM package and then publishes it. This works out fine, but the one weird thing that I had to do was copy the oc-openapi.json to the NPM package consumer's public folder after installing the NPM package so that it could read it at runtime. It seems like there should be a way to generate the client only using the oc-openapi.d.ts Client type so that we don't need to worry about exposing the oc-openapi.json at runtime

Thanks for taking the time to reply :) it sounds like generating the client using the .d.ts instead of the .json might not work, we may be able to close this now

npdev453 commented 3 years ago

@ryan-henness-trimble You can clean all from oc-openapi.json except operationId and path of methods, other info doesn't use in runtime.

Working with types -- it's an additional feature of this module. Also. d.ts provide only types and can't be used in realtime to generate client (without extra magic).

Another idea, that probably we can generate an static client.js.ts for schema to not rebuild it every time on client. Especially, it can be actual for large schemas.

markoivanovski commented 3 years ago

@anttiviljami I've just come across the same issue so I understand what OP was meaning.

Basically, do we have to specify the URL or Path of our OpenAPI document, given that we've already used typegen to generate the necessary types?

In my example, I am using typegen at build time from a remote host which generates my API types correctly. Then when I initialize my APIClient, I would like to remove the reliance of connecting to the remote host again to obtain the OpenAPI document.

So, after having used typegen, is there a way to initialize OpenAPIClientAxios without specifying an OpenAPI document inside definition, given we already have all the necessary types?

busheezy commented 2 years ago

I had the same curiosity which lead me here.