Skip to content

Commit

Permalink
feat: improved deployment protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariano Goldman committed May 31, 2024
1 parent 6d287bd commit e3b3e42
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/client/ContentClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ export async function downloadContent(
)
}

async function supportsDeploymentsV2(serverBaseUrl: string): Promise<boolean> {
try {
const response = await fetch(`${serverBaseUrl}/entities/:hash`, { method: 'OPTIONS' })
return response.ok // returns true if response status is 200-299
} catch (error) {
console.error(`Error: ${error}`)
return false
}
}

export function createContentClient(options: ClientOptions): ContentClient {
const { fetcher } = options
const contentUrl = sanitizeUrl(options.url)
Expand Down Expand Up @@ -96,6 +106,28 @@ export function createContentClient(options: ClientOptions): ContentClient {
}

async function deploy(deployData: DeploymentData, options?: RequestOptions): Promise<unknown> {
// TODO We could also check the deployment size (if too small, may not be worth to use V2)
if (deployData.files.size > 0) {
const supportsV2 = await supportsDeploymentsV2(contentUrl)
if (supportsV2) {
return deployV2(deployData, options)
}
}
return deployTraditional(deployData, options)
}

async function deployTraditional(deployData: DeploymentData, options?: RequestOptions): Promise<unknown> {
const form = await buildEntityFormDataForDeployment(deployData, options)

const requestOptions = mergeRequestOptions(options ? options : {}, {
body: form as any,
method: 'POST'
})

return await fetcher.fetch(`${contentUrl}/entities`, requestOptions)
}

async function deployV2(deployData: DeploymentData, options?: RequestOptions): Promise<unknown> {
const form = await buildEntityFormDataForDeployment(deployData, options)

const requestOptions = mergeRequestOptions(options ? options : {}, {
Expand Down

0 comments on commit e3b3e42

Please sign in to comment.