Skip to content

[nuxt-client] Access response header #2067

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
lelmarir opened this issue May 23, 2025 · 3 comments
Open

[nuxt-client] Access response header #2067

lelmarir opened this issue May 23, 2025 · 3 comments
Labels
feature 🚀 New feature or request

Comments

@lelmarir
Copy link

lelmarir commented May 23, 2025

Description

Hi,

how can I access the headers of a response using nuxt-client?

Using composable "$fetch" I get directly my dto

		const myResult /*MyDto*/ = await getDocument({
			composable: "$fetch",
			path: { id: doc.id }
		})

Using "useAsyncData" the response is not exposed

		const { data /*MyDto*/, error, status, clear, execute, refresh } = await getDocument({
			composable: "useAsyncData",
			key: "test",
			path: { id: doc.id }
		})
@lelmarir lelmarir added the feature 🚀 New feature or request label May 23, 2025
@mrlubos
Copy link
Member

mrlubos commented May 23, 2025

@lelmarir how would you do this without the codegen layer? (using Nuxt directly)

@lelmarir
Copy link
Author

lelmarir commented May 23, 2025

Something like this, I suppose

		const endpoint = `/api/documents/${doc.id}`;
		const res = await fetch(endpoint);
		if (!res.ok) {
			const text = await res.text();
			throw new Error(text || `HTTP ${res.status}`);
		}

		const contentDisp = res.headers.get('content-disposition') || '';

@lelmarir
Copy link
Author

lelmarir commented May 23, 2025

Meanwhile, I've found a dirty trick; It's not great, but it works

	let filename = null;
	getDocument({
			composable: "$fetch",
			path: { id: doc.id },
			onResponse: response => {
				const contentDisposition = response.response.headers.get('Content-Disposition');
				if (contentDisposition) {
					const filenameMatch = contentDisposition.match(/filename="(.+)"/);
					if (filenameMatch && filenameMatch.length > 1) {
						filename = filenameMatch[1];
					}
				}
			}
		})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature 🚀 New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants