Skip to content

πŸŽ₯ Make videos programmatically with React

make test API calls typesafe

Jonny Burger7615be192da2
diff --git a/bun.lock b/bun.lock
index 5d25ebc155..9b7de17d37 100644
--- a/bun.lock
+++ b/bun.lock
@@ -704,6 +704,7 @@
"@remotion/skia": "workspace:*",
"@remotion/starburst": "workspace:*",
"@remotion/studio": "workspace:*",
+ "@remotion/studio-shared": "workspace:*",
"@remotion/svg-3d-engine": "workspace:*",
"@remotion/tailwind-v4": "workspace:*",
"@remotion/three": "workspace:*",
diff --git a/packages/example/e2e/api-call.mts b/packages/example/e2e/api-call.mts
new file mode 100644
index 0000000000..257db20ef5
--- /dev/null
+++ b/packages/example/e2e/api-call.mts
@@ -0,0 +1,18 @@
+import type {ApiRoutes} from '@remotion/studio-shared';
+import {STUDIO_URL} from './constants.mts';
+
+type ApiResponse<Endpoint extends keyof ApiRoutes> =
+ | {success: true; data: ApiRoutes[Endpoint]['Response']}
+ | {success: false; error: string};
+
+export const apiCall = async <Endpoint extends keyof ApiRoutes>(
+ endpoint: Endpoint,
+ body: ApiRoutes[Endpoint]['Request'],
+): Promise<ApiResponse<Endpoint>> => {
+ const res = await fetch(`${STUDIO_URL}${endpoint}`, {
+ method: 'POST',
+ headers: {'content-type': 'application/json'},
+ body: JSON.stringify(body),
+ });
+ return (await res.json()) as ApiResponse<Endpoint>;
+};
diff --git a/packages/example/e2e/node-path-cache.test.mts b/packages/example/e2e/node-path-cache.test.mts
index 18af975bc7..15f1780e5b 100644
--- a/packages/example/e2e/node-path-cache.test.mts
+++ b/packages/example/e2e/node-path-cache.test.mts
@@ -1,6 +1,9 @@
+import assert from 'node:assert';
import fs from 'fs';
import {expect, test} from '@playwright/test';
-import {newVideoFile, STUDIO_URL} from './constants.mts';
+import {Internals} from 'remotion';
+import {apiCall} from './api-call.mts';
+import {newVideoFile} from './constants.mts';
import {startStudio, stopStudio} from './studio-server.mts';
test.describe('node-path cache for stale source maps', () => {
@@ -49,19 +52,17 @@ test.describe('node-path cache for stale source maps', () => {
const videoLine = videoLineIndex + 1; // 1-indexed
// 1. Initial subscription β†’ resolves line to AST nodePath and caches it
- const res1 = await fetch(`${STUDIO_URL}/api/subscribe-to-sequence-props`, {
- method: 'POST',
- headers: {'content-type': 'application/json'},
- body: JSON.stringify({
- fileName: 'src/NewVideo.tsx',
- line: videoLine,
- column: 0,
- keys: ['src'],
- clientId: 'e2e-cache-test-1',
- }),
+ const result1 = await apiCall('/api/subscribe-to-sequence-props', {
+ fileName: 'src/NewVideo.tsx',
+ line: videoLine,
+ column: 0,
+ schema: Internals.sequenceSchema,
+ clientId: 'e2e-cache-test-1',
});
- const result1 = await res1.json();
+ expect(result1.success).toBe(true);
+ assert(result1.success);
expect(result1.data.canUpdate).toBe(true);
+ assert(result1.data.canUpdate);
expect(result1.data.nodePath).toBeTruthy();
// 2. Simulate prettier wrapping the return in parentheses,
@@ -83,19 +84,17 @@ test.describe('node-path cache for stale source maps', () => {
// 3. Subscribe again with the ORIGINAL (stale) line number.
// Without the cache, this would fail because the tag is no longer on this line.
// With the cache, the previously resolved nodePath is reused.
- const res2 = await fetch(`${STUDIO_URL}/api/subscribe-to-sequence-props`, {
- method: 'POST',
- headers: {'content-type': 'application/json'},
- body: JSON.stringify({
- fileName: 'src/NewVideo.tsx',
- line: videoLine, // stale line number
- column: 0,
- keys: ['src'],
- clientId: 'e2e-cache-test-2',
- }),
+ const result2 = await apiCall('/api/subscribe-to-sequence-props', {
+ fileName: 'src/NewVideo.tsx',
+ line: videoLine, // stale line number
+ column: 0,
+ schema: Internals.sequenceSchema,
+ clientId: 'e2e-cache-test-2',
});
- const result2 = await res2.json();
+ expect(result2.success).toBe(true);
+ assert(result2.success);
expect(result2.data.canUpdate).toBe(true);
+ assert(result2.data.canUpdate);
expect(result2.data.nodePath).toBeTruthy();
// The nodePath should be the same β€” both refer to the same <Video> element
diff --git a/packages/example/package.json b/packages/example/package.json
index 1ea719b4e2..9cfb137d9d 100644
--- a/packages/example/package.json
+++ b/packages/example/package.json
@@ -70,6 +70,7 @@
"@remotion/shapes": "workspace:*",
"@remotion/skia": "workspace:*",
"@remotion/studio": "workspace:*",
+ "@remotion/studio-shared": "workspace:*",
"@remotion/svg-3d-engine": "workspace:*",
"@remotion/tailwind-v4": "workspace:*",
"@remotion/three": "workspace:*",