🎥 Make videos programmatically with React
rendering on API now accepts serviceName, without cloudRunUrl
Matt McGillivraye67287d65d0b
diff --git a/packages/cloudrun/src/api/render-media-on-cloudrun.ts b/packages/cloudrun/src/api/render-media-on-cloudrun.tsindex 2513eea537..97345fd511 100644--- a/packages/cloudrun/src/api/render-media-on-cloudrun.ts+++ b/packages/cloudrun/src/api/render-media-on-cloudrun.ts@@ -2,14 +2,18 @@ import got from 'got/dist/source';import {validateCloudRunUrl} from '../shared/validate-cloudrun-url';import type {CloudrunCodec} from '../shared/validate-gcp-codec';import {validateCloudrunCodec} from '../shared/validate-gcp-codec';+import {validateRegion} from '../shared/validate-region';import {validateServeUrl} from '../shared/validate-serveurl';+import {validateServiceName} from '../shared/validate-service-name';+import {getServiceInfo} from './get-service-info';import {getAuthClientForUrl} from './helpers/get-auth-client-for-url';export type RenderMediaOnCloudrunInput = {authenticatedRequest: boolean;cloudRunUrl: string;- // serviceName?: string;- serveUrl: string;+ serviceName?: string;+ region?: string;+ serveUrl?: string;composition: string;inputProps?: unknown;codec: CloudrunCodec;@@ -49,7 +53,8 @@ export type RenderMediaOnCloudrunOutput = {export const renderMediaOnCloudrun = async ({authenticatedRequest,cloudRunUrl,- // serviceName,+ serviceName,+ region,serveUrl,composition,inputProps,@@ -60,9 +65,23 @@ export const renderMediaOnCloudrun = async ({}: RenderMediaOnCloudrunInput): Promise<RenderMediaOnCloudrunOutput> => {const actualCodec = validateCloudrunCodec(codec);validateServeUrl(serveUrl);- validateCloudRunUrl(cloudRunUrl);+ if (!cloudRunUrl && !serviceName)+ throw new Error('Either cloudRunUrl or serviceName must be provided');+ if (cloudRunUrl && serviceName)+ throw new Error(+ 'Either cloudRunUrl or serviceName must be provided, not both'+ );- // todo: allow serviceName to be passed in, and fetch the cloud run URL based on the name+ if (cloudRunUrl) {+ validateCloudRunUrl(cloudRunUrl);+ }++ if (serviceName) {+ validateServiceName(serviceName);+ const validatedRegion = validateRegion(region);+ const {uri} = await getServiceInfo({serviceName, region: validatedRegion});+ cloudRunUrl = uri;+ }const data = {type: 'media',diff --git a/packages/cloudrun/src/api/render-still-on-cloudrun.ts b/packages/cloudrun/src/api/render-still-on-cloudrun.tsindex ce559d3be8..4152923f97 100644--- a/packages/cloudrun/src/api/render-still-on-cloudrun.ts+++ b/packages/cloudrun/src/api/render-still-on-cloudrun.ts@@ -1,12 +1,16 @@import got from 'got';import {validateCloudRunUrl} from '../shared/validate-cloudrun-url';+import {validateRegion} from '../shared/validate-region';import {validateServeUrl} from '../shared/validate-serveurl';+import {validateServiceName} from '../shared/validate-service-name';+import {getServiceInfo} from './get-service-info';import {getAuthClientForUrl} from './helpers/get-auth-client-for-url';export type RenderStillOnCloudrunInput = {authenticatedRequest: boolean;cloudRunUrl: string;- // serviceName?: string;+ serviceName?: string;+ region?: string;serveUrl: string;composition: string;inputProps?: unknown;@@ -43,7 +47,8 @@ export type RenderStillOnCloudrunOutput = {export const renderStillOnCloudrun = async ({authenticatedRequest,cloudRunUrl,- // serviceName,+ serviceName,+ region,serveUrl,composition,inputProps,@@ -51,9 +56,23 @@ export const renderStillOnCloudrun = async ({outputFile,}: RenderStillOnCloudrunInput): Promise<RenderStillOnCloudrunOutput> => {validateServeUrl(serveUrl);- validateCloudRunUrl(cloudRunUrl);+ if (!cloudRunUrl && !serviceName)+ throw new Error('Either cloudRunUrl or serviceName must be provided');+ if (cloudRunUrl && serviceName)+ throw new Error(+ 'Either cloudRunUrl or serviceName must be provided, not both'+ );- // todo: allow serviceName to be passed in, and fetch the cloud run URL based on the name+ if (cloudRunUrl) {+ validateCloudRunUrl(cloudRunUrl);+ }++ if (serviceName) {+ validateServiceName(serviceName);+ const validatedRegion = validateRegion(region);+ const {uri} = await getServiceInfo({serviceName, region: validatedRegion});+ cloudRunUrl = uri;+ }const data = {type: 'still',diff --git a/packages/cloudrun/src/shared/validate-region.ts b/packages/cloudrun/src/shared/validate-region.tsnew file mode 100644index 0000000000..bbab89a0a0--- /dev/null+++ b/packages/cloudrun/src/shared/validate-region.ts@@ -0,0 +1,21 @@+import type {GcpRegion} from '../pricing/gcp-regions';+import {GCP_REGIONS} from '../pricing/gcp-regions';++export const validateRegion = (region: unknown) => {+ if (typeof region !== 'string') {+ throw new TypeError(+ `"region" parameter must be a string, but is ${JSON.stringify(region)}`+ );+ }++ // check region is part of GCP_REGIONS list+ if (!GCP_REGIONS.includes(region as GcpRegion)) {+ throw new TypeError(+ `"region" parameter must be one of ${GCP_REGIONS.join(+ ', '+ )}, but is ${JSON.stringify(region)}`+ );+ }++ return region as GcpRegion;+};diff --git a/packages/cloudrun/src/shared/validate-service-name.ts b/packages/cloudrun/src/shared/validate-service-name.tsnew file mode 100644index 0000000000..e19278d024--- /dev/null+++ b/packages/cloudrun/src/shared/validate-service-name.ts@@ -0,0 +1,9 @@+export const validateServiceName = (serviceName: unknown) => {+ if (typeof serviceName !== 'string') {+ throw new TypeError(+ `"serviceName" parameter must be a string, but is ${JSON.stringify(+ serviceName+ )}`+ );+ }+};