Skip to content

Commit

Permalink
Fix "inngest/next" types not inferring from defineProperties (#348)
Browse files Browse the repository at this point in the history
## Summary
<!-- Succinctly describe your change, providing context, what you've
changed, and why. -->

Fixes #340, ensuring the return type of `"inngest/next"`'s `serve()` is
correctly inferred.

Fuller integration tests for the app router will come in #341.

## Checklist
<!-- Tick these items off as you progress. -->
<!-- If an item isn't applicable, ideally please strikeout the item by
wrapping it in "~~"" and suffix it with "N/A My reason for skipping
this." -->
<!-- e.g. "- [ ] ~~Added tests~~ N/A Only touches docs" -->

- [ ] ~~Added a [docs PR](https://github.com/inngest/website) that
references this PR~~ N/A
- [ ] ~~Added unit/integration tests~~ N/A #341
- [x] Added changesets if applicable

## Related
<!-- A space for any related links, issues, or PRs. -->
<!-- Linear issues are autolinked. -->
<!-- e.g. - INN-123 -->
<!-- GitHub issues/PRs can be linked using shorthand. -->
<!-- e.g. "- inngest/inngest#123" -->
<!-- Feel free to remove this section if there are no applicable related
links.-->
- Fixes #340
  • Loading branch information
jpwilliams authored Oct 6, 2023
1 parent c14bbb3 commit ebb245f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/dirty-mangos-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"inngest": patch
---

Fix `"inngest/next"` types not inferring from `defineProperties`
27 changes: 19 additions & 8 deletions packages/inngest/src/next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const isNextEdgeRequest = (
* In Next.js, serve and register any declared functions with Inngest, making
* them available to be triggered by events.
*
* @example Next.js <=12 can export the handler directly
* @example Next.js <=12 or the pages router can export the handler directly
* ```ts
* export default serve({ client: inngest, functions: [fn1, fn2] });
* ```
Expand All @@ -40,9 +40,11 @@ export const serve = (options: ServeHandlerOptions) => {
...options,
handler: (
reqMethod: "GET" | "POST" | "PUT" | undefined,
req: Either<NextApiRequest, NextRequest>,
expectedReq: NextRequest,
res: NextApiResponse
) => {
const req = expectedReq as Either<NextApiRequest, NextRequest>;

const isEdge = isNextEdgeRequest(req);

return {
Expand Down Expand Up @@ -151,11 +153,20 @@ export const serve = (options: ServeHandlerOptions) => {
*
* See {@link https://beta.nextjs.org/docs/routing/route-handlers}
*/
const fn = handler.createHandler();
const baseFn = handler.createHandler();

return Object.defineProperties(fn.bind(null, undefined), {
GET: { value: fn.bind(null, "GET") },
POST: { value: fn.bind(null, "POST") },
PUT: { value: fn.bind(null, "PUT") },
});
const fn = baseFn.bind(null, undefined);
type Fn = typeof fn;

const handlerFn = Object.defineProperties(fn, {
GET: { value: baseFn.bind(null, "GET") },
POST: { value: baseFn.bind(null, "POST") },
PUT: { value: baseFn.bind(null, "PUT") },
}) as Fn & {
GET: Fn;
POST: Fn;
PUT: Fn;
};

return handlerFn;
};

0 comments on commit ebb245f

Please sign in to comment.