Skip to content

API Handler Function Wrapper #501

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
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Conversation

rmainwork
Copy link
Contributor

@rmainwork rmainwork commented Jun 18, 2025

Our tests contain quite a few references(and various competing implementations) of the following:

const mockRequest = (url: string) => {
  return new Request(url)
}
const request = mockRequest(`http://localhost:8080/api/all-docs-paths`)
const response = await GET(request)

I decided this could probably be streamlined a lot more by extracting all of those mockRequest calls into a single callHandler wrapper function which you can pass the API handler you're testing to. From there, it infers the param types (if the handler uses params) giving us automatic type hinting when testing API route handlers.

20250618_16h18m26s_grim
image

I do also just want to call out that last commit I added in at the end to consolidate (where appropriate) all the API handler parameters and their respective types. It's not strictly in scope for this PR, but I was working in that general scope anyway and it wasn't too big of a lift, so I added it in.

This function should (hopefully) replace all the numerous calls to
`mockRequest` all over the code base...
Replace calls to `mockRequest` with the new type-aware `callHandler` wrapper
function
Most route handlers completely ignore the URL, so the URL itself typically
hasn't been given a lot of thought when writing tests. For that reason,
the initial implementation of the callHandler function simply used the
API base url hard coded in.

However, one or two API endpoints do retrieve parameters via URL search params
- therefore support was added to pass in a subpath(including query string) to
enable these API routes to also be tested using the new wrapper function
@rmainwork rmainwork requested a review from a team as a code owner June 18, 2025 20:23
@rmainwork rmainwork requested a review from RubenSandwich June 18, 2025 20:23
Copy link

github-actions bot commented Jun 18, 2025

Vercel Previews Deployed

Name Status Preview Updated (UTC)
Dev Portal ✅ Ready (Inspect) Visit Preview Wed Jun 18 21:11:45 UTC 2025
Unified Docs API ✅ Ready (Inspect) Visit Preview Wed Jun 18 21:06:51 UTC 2025

Extract `productSlug` and `version` request handler param types into an
external file for re-use as composables. This helps ensure consistency
between API endpoints as well as centralizing the source for docblock
documentation (each different instance of the params had their own
slightly different docblock documentation too)
* // Other assertions...
* ```
*/
export const callHandler = <
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rmainwork let's give this a more description name, so folks know that this is used on;y in testing.

Something like testRequestHandler or createTestRequestHandler.

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

Successfully merging this pull request may close these issues.

2 participants