🎥 Make videos programmatically with React
add function for next.js pages router
Hunain Ahmed136b0dbe7a24
diff --git a/packages/lambda/package.json b/packages/lambda/package.jsonindex cc0949951c..e7d265feef 100644--- a/packages/lambda/package.json+++ b/packages/lambda/package.json@@ -35,6 +35,7 @@"@remotion/streaming": "workspace:*","@remotion/serverless": "workspace:*","mime-types": "2.1.34",+ "next": "^15.1.3","remotion": "workspace:*","zod": "3.22.3"},diff --git a/packages/lambda/src/client.ts b/packages/lambda/src/client.tsindex 97b4ab166f..166c8d7ecf 100644--- a/packages/lambda/src/client.ts+++ b/packages/lambda/src/client.ts@@ -7,7 +7,7 @@ import {getRenderProgress} from './api/get-render-progress';import {getSites} from './api/get-sites';import type {PresignUrlInput} from './api/presign-url';import {presignUrl} from './api/presign-url';-import { appRouterWebhooks } from './simplifiers';+import { appRouterWebhooks, pageRouterWebhooks } from './simplifiers';import type {RenderMediaOnLambdaInput,RenderMediaOnLambdaOutput,@@ -46,7 +46,8 @@ export {renderVideoOnLambda,speculateFunctionName,validateWebhookSignature,- appRouterWebhooks+ appRouterWebhooks,+ pageRouterWebhooks,};export type {DeleteRenderInput,diff --git a/packages/lambda/src/simplifiers/index.ts b/packages/lambda/src/simplifiers/index.tsindex f99d6b604f..ba14d0f0cc 100644--- a/packages/lambda/src/simplifiers/index.ts+++ b/packages/lambda/src/simplifiers/index.ts@@ -1,6 +1,6 @@import { validateWebhookSignature } from "../client";import type { WebhookPayload } from "../client";-+import type {NextApiRequest, NextApiResponse} from 'next'type AppRouterWebhookArgs = {testing?: boolean;extraHeaders?: Record<string, string>;@@ -55,4 +55,54 @@ const appRouterWebhooks = (options: AppRouterWebhookArgs): (req: Request) => Pro}}-export {appRouterWebhooks}\ No newline at end of file+const headersMutator = (res: NextApiResponse, headers: Record<string, string>) => {+ Object.entries(headers).forEach(([key, value]) => {+ res.setHeader(key, value);+ })+ };++ const pageRouterWebhooks = (options: AppRouterWebhookArgs) => {+ const {testing, extraHeaders, secret, onSuccess, onTimeout, onError} = options;+ return async function(req: NextApiRequest, res: NextApiResponse): Promise<void> {+ // set the headers+ headersMutator(res, extraHeaders || {})++ if (testing) {+ const testingheaders = {+ 'Access-Control-Allow-Origin': 'https://www.remotion.dev',+ 'Access-Control-Allow-Headers':+ 'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version, X-Remotion-Status, X-Remotion-Signature, X-Remotion-Mode',+ 'Access-Control-Allow-Methods': 'OPTIONS,POST',+ };+ // add the headers for testing+ headersMutator(res, testingheaders)+ }++ if (req.method === 'OPTIONS') {+ res.status(200).end();+ return;+ }++ validateWebhookSignature({+ secret,+ body: req.body,+ signatureHeader: req.headers['x-remotion-signature'] as string,+ });++ // If code reaches this path, the webhook is authentic.+ const payload = req.body as WebhookPayload;+ if (payload.type === 'success' && onSuccess) {+ onSuccess(payload)+ } else if (payload.type === 'timeout' && onTimeout) {+ onTimeout(payload)++ }else if (payload.type === 'error' && onError) {+ onError(payload)+ }++ res.status(200).json({+ success: true,+ });+ }}++export {appRouterWebhooks, pageRouterWebhooks}\ No newline at end of file