Skip to content
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

feat: add readFormData util #421

Merged
merged 5 commits into from
Jul 24, 2023
Merged

feat: add readFormData util #421

merged 5 commits into from
Jul 24, 2023

Conversation

Hebilicious
Copy link
Member

@Hebilicious Hebilicious commented Jul 2, 2023

Resolves #419

πŸ”— Linked issue

❓ Type of change

  • πŸ“– Documentation (updates to the documentation, readme or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

This PR adds support for Request and FormData in event handler.

2 new helpers are introduced :

  • getRequestFromEvent
  • getFormData

This helpers relies on Request and FormData to be available in the global scope as per the Fetch specification, which means the tests are disabled for node < 18 (which is fine as 18 reaches EOL in 3 months)

// With a JSON body ...
const handler = eventHandler(async (event) => {
	const nativeRequest = await getRequestFromEvent(event);
	expect(nativeRequest instanceof Request).toBe(true);
	expect(nativeRequest.method).toBe("POST");
	expect(nativeRequest.headers.get("hello")).toBe("world");
	const body = await nativeRequest.json();
	expect(body).toMatchObject({
	  user: "john",
	});
	return "ok";
})
// With x-www-form-urlencoded data
const handler = eventHandler(async (event) => {
	const formData = await getFormData(event);
	const user = formData.get("user");
	expect(formData instanceof FormData).toBe(true);
	expect(user).toBe("john");
	return { user };
})

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@codecov
Copy link

codecov bot commented Jul 2, 2023

Codecov Report

Merging #421 (c4581fd) into main (5c4521f) will increase coverage by 0.13%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main     #421      +/-   ##
==========================================
+ Coverage   78.23%   78.36%   +0.13%     
==========================================
  Files          26       26              
  Lines        2775     2792      +17     
  Branches      407      407              
==========================================
+ Hits         2171     2188      +17     
  Misses        604      604              
Impacted Files Coverage Ξ”
src/utils/body.ts 95.13% <100.00%> (+0.49%) ⬆️

@Hebilicious Hebilicious requested review from danielroe and pi0 July 2, 2023 18:07
@Hebilicious Hebilicious added enhancement New feature or request helpers labels Jul 2, 2023
@Hebilicious Hebilicious added ready and removed helpers labels Jul 4, 2023 — with Volta.net
@pi0
Copy link
Member

pi0 commented Jul 5, 2023

I think getRequestFromEvent is a nice addition but we might find a more straightforward solution to handle form data that does not require creating a whole Request object to parse form-data. (splitting into two PRs would be nice πŸ™πŸΌ )

Update: Checking MDN docs, it seems we cannot directly construct FormData from body...

src/utils/body.ts Outdated Show resolved Hide resolved
@pi0 pi0 changed the title feat: add formdata and request helpers feat: add readFormData and eventToRequest Jul 5, 2023
@Hebilicious Hebilicious requested a review from pi0 July 6, 2023 07:19
@Hebilicious
Copy link
Member Author

Hebilicious commented Jul 6, 2023

@pi0 Updated the method names to readFormData and eventToRequest πŸ‘πŸ½

@franklin-tina
Copy link

Hi @Hebilicious,
Lovely PR 😍
I was wondering if this will handle forms with multimedia data as well?

@Hebilicious
Copy link
Member Author

Hebilicious commented Jul 10, 2023

@neo-franklin-tina Are you talking about multipart/form-data ? It should work by default yes.

@franklin-tina
Copy link

@neo-franklin-tina Are you talking about multipart/form-data ? You're right we should also support this.

Yes, something like that.
Thanks for the response

@pi0 pi0 changed the title feat: add readFormData and eventToRequest feat: add readFormData util Jul 24, 2023
@pi0
Copy link
Member

pi0 commented Jul 24, 2023

Rebased util with event.request from #454. Thanks for the ideas!

@pi0 pi0 merged commit 57dcba8 into unjs:main Jul 24, 2023
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request ready
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[helpers] Native Request and FormData helpers
3 participants