Last active
January 11, 2024 17:47
-
-
Save djfarrelly/4ba51651d9d69b29d775449012825771 to your computer and use it in GitHub Desktop.
Inngest SDK: Using middleware to access search params from request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { type NextRequest } from 'next/server'; | |
import { Inngest, InngestMiddleware } from 'inngest'; | |
import { schemas } from './types'; | |
export const searchParamsMiddleware = new InngestMiddleware({ | |
name: 'query-params-pass-through', | |
init({}) { | |
return { | |
onFunctionRun() { | |
return { | |
transformInput({ reqArgs }) { | |
// The second arg in the array is the request itself | |
const req = reqArgs[1] as NextRequest; | |
const url = new URL(req.url); | |
return { | |
ctx: { | |
// Adding fields to "ctx" adds them to your function handler's args (see function below) | |
searchParams: url.searchParams, | |
}, | |
}; | |
}, | |
}; | |
}, | |
}; | |
}, | |
}); | |
export const inngest = new Inngest({ | |
id: 'my-nextjs-app', | |
schemas, | |
middleware: [searchParamsMiddleware], | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { inngest, searchParamsMiddleware } from './client'; | |
export default inngest.createFunction( | |
{ id: 'hello-world' }, | |
{ event: 'demo/event.sent' }, | |
// The new field added to "ctx" in the middleware is now available in your function args: | |
async ({ event, step, searchParams }) => { | |
console.log('searchParams', searchParams); | |
return { | |
message: `Hello ${event.name}!`, | |
}; | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment