Giter VIP home page Giter VIP logo

Comments (19)

mrlubos avatar mrlubos commented on August 10, 2024

You can do this with the new Fetch API client, is that an option for you?

from openapi-ts.

mfinelli avatar mfinelli commented on August 10, 2024

I'm using the new fetch api client (I think, I have client: "@hey-api/client-fetch" set in my openapi-ts.config.ts file), but the examples on the page all seem to work with the request headers, but I need access to the response headers. The generated client functions all return types like ...get<GetSomeResponse, GetSomeError> so it's not clear how I can get at the raw response.

from openapi-ts.

mrlubos avatar mrlubos commented on August 10, 2024

@mfinelli the new Fetch API client returns 4 things: data, error, request, and response. Response is the raw object containing your headers. They won't be properly typed, but they are there

from openapi-ts.

mfinelli avatar mfinelli commented on August 10, 2024

Ok, I see how it works now. For anyone else finding this issue I'm able to get the raw response like so:

const { data, error, response } = await yourClientFunction(...);

Then you can pull individual headers like this

response.headers.get("X-Your-Header");

@mrlubos I still think it would be a good idea if the generated client could read the headers from the openapi spec and expose them in a more standard way along with the proper types, but if you disagree then you can close this issue as resolved. Thanks for your help!

from openapi-ts.

mrlubos avatar mrlubos commented on August 10, 2024

@mfinelli It depends on how you imagine the standardised way. My understanding is that getting headers from response is straightforward, minus the missing type support. I'd argue that the more stuff you put in the data field (if that was your proposal), the closer it gets to being a typed response object

from openapi-ts.

mfinelli avatar mfinelli commented on August 10, 2024

I'm not sure that I'd like it in the data object unless the data object had two keys body and headers. My usecase is to add extra information on the server side into some custom headers (e.g., pagination details) without affecting the response body itself which corresponds directly to a model that exists in the database and in the openapi spec (e.g., GET /category returns an array of Category objects, I wouldn't want to try and stuff the pagination information in there too)

from openapi-ts.

mrlubos avatar mrlubos commented on August 10, 2024

If you have any ideas around implementation feel free to propose, I didn't spend too much time thinking about this feature

from openapi-ts.

bielas avatar bielas commented on August 10, 2024

can you tell me how to get headers from that kind of code using v.0.49 of "@hey-api/openapi-ts"?

image

from openapi-ts.

mrlubos avatar mrlubos commented on August 10, 2024

@bielas looks like you're using Angular with the new Fetch API client? See my comment in #667 (comment). I didn't prioritise this use case yet, sorry!

from openapi-ts.

bielas avatar bielas commented on August 10, 2024

@mrlubos nope, I am using react and default client

from openapi-ts.

mrlubos avatar mrlubos commented on August 10, 2024

I am confused then, parseAs is available only in the new Fetch API client?

from openapi-ts.

bielas avatar bielas commented on August 10, 2024

@mrlubos I am using client version 0.1.3 and hey api version 0.49. Seems that is is possible...

from openapi-ts.

bielas avatar bielas commented on August 10, 2024

How then I should proceed?

from openapi-ts.

bielas avatar bielas commented on August 10, 2024

If you provide me any other solution I can stop using parseAs and go with the other one

from openapi-ts.

mrlubos avatar mrlubos commented on August 10, 2024

@bielas here's how you can obtain response headers https://stackblitz.com/edit/hey-api-client-fetch-example?file=src%2FApp.tsx%3AL106

from openapi-ts.

bielas avatar bielas commented on August 10, 2024

how this is possible to work as open api generated code looks like:

export const downloadSubscription = (options: Options<DownloadSubscriptionData>) => {
	return (options?.client ?? client).get<DownloadSubscriptionResponse, DownloadSubscriptionError>(
		{
			...options,
			url: '/v1/self/trainer/subscription/{subscriptionId}/invoice'
		}
	);
};

export type DownloadSubscriptionResponse = Blob | File;

i am not able to extract it like you do here:

const { data, error, response } = await getPetById({

from openapi-ts.

mrlubos avatar mrlubos commented on August 10, 2024

@bielas please create a StackBlitz example, hard to tell from the code, it looks fine to me

from openapi-ts.

bielas avatar bielas commented on August 10, 2024

@mrlubos I hope you will get my point: https://stackblitz.com/edit/vitejs-vite-kemti5?file=src%2FApp.tsx

from openapi-ts.

mrlubos avatar mrlubos commented on August 10, 2024

@bielas I can't reproduce it with that. However, I see you're accessing data in the tap() function. data will not contain the headers, you need the raw response object for that

from openapi-ts.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.