Skip to content

🎥 Make videos programmatically with React

add next.js app router simplifier

Hunain Ahmed5f0b912c236b
diff --git a/packages/lambda/src/client.ts b/packages/lambda/src/client.ts
index efad02e928..97b4ab166f 100644
--- a/packages/lambda/src/client.ts
+++ b/packages/lambda/src/client.ts
@@ -7,6 +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 type {
RenderMediaOnLambdaInput,
RenderMediaOnLambdaOutput,
@@ -45,6 +46,7 @@ export {
renderVideoOnLambda,
speculateFunctionName,
validateWebhookSignature,
+ appRouterWebhooks
};
export type {
DeleteRenderInput,
diff --git a/packages/lambda/src/simplifiers/index.ts b/packages/lambda/src/simplifiers/index.ts
new file mode 100644
index 0000000000..f99d6b604f
--- /dev/null
+++ b/packages/lambda/src/simplifiers/index.ts
@@ -0,0 +1,58 @@
+import { validateWebhookSignature } from "../client";
+import type { WebhookPayload } from "../client";
+
+type AppRouterWebhookArgs = {
+ testing?: boolean;
+ extraHeaders?: Record<string, string>;
+ secret: string;
+ onSuccess?: (payload: WebhookPayload) => void;
+ onTimeout?: (payload: WebhookPayload) => void;
+ onError?: (payload: WebhookPayload) => void;
+}
+
+const appRouterWebhooks = (options: AppRouterWebhookArgs): (req: Request) => Promise<Response> => {
+ const {testing, extraHeaders, secret, onSuccess, onTimeout, onError} = options;
+ return async function(req: Request): Promise<Response> {
+ let headers = 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',
+ };
+ headers = {...headers, ...testingheaders};
+ }
+
+ if (req.method === 'OPTIONS') {
+ // do we have any use of the OPTIONS method other than the tester on webhooks page ? if so we can add a condition here to only return this if testing mode enabled
+ return new Response(null, {
+ headers,
+ });
+ }
+
+ // Parse the body properly
+ const body = await req.json();
+
+ validateWebhookSignature({
+ secret,
+ body,
+ signatureHeader: req.headers.get('X-Remotion-Signature') as string,
+ });
+
+ const payload = 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);
+ }
+
+ return new Response(JSON.stringify({success: true}), {headers});
+ }
+}
+
+export {appRouterWebhooks}
\ No newline at end of file