Skip to content

Instantly share code, notes, and snippets.

@djfarrelly
Last active January 11, 2024 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djfarrelly/4ba51651d9d69b29d775449012825771 to your computer and use it in GitHub Desktop.
Save djfarrelly/4ba51651d9d69b29d775449012825771 to your computer and use it in GitHub Desktop.
Inngest SDK: Using middleware to access search params from request
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],
});
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