-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
42b78a4
commit 346ad17
Showing
4 changed files
with
171 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
"name": "chrome-extension-starter", | ||
"private": true, | ||
"type": "module", | ||
"packageManager": "[email protected]", | ||
"packageManager": "[email protected]", | ||
"scripts": { | ||
"prepare": "husky install", | ||
"dev": "vite build --watch", | ||
|
@@ -13,7 +13,7 @@ | |
"author": "MichaelYuhe", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@zeabur/zeabur-js": "0.0.1-alpha1", | ||
"@zeabur/zeabur-js": "0.0.1-alpha2", | ||
"jszip": "^3.10.1", | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0", | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,185 @@ | ||
import JSZip from "jszip"; | ||
import { createClient } from "@zeabur/zeabur-js"; | ||
|
||
const API_KEY = process.env.API_KEY || "your_api_key_here"; | ||
|
||
export const zeabur = createClient(API_KEY); | ||
const API_URL = "https://gateway.zeabur.com/graphql"; | ||
|
||
async function deployToZeabur(codes: string, name: string, language: string) { | ||
const convertedName = convertTitle(name); | ||
// 0. Create a file ends with .language | ||
const file = new File([codes], `index.${language}`, { | ||
type: "text/plain", | ||
}); | ||
|
||
// 1. Compress codes to a zip with one single file | ||
const zip = new JSZip(); | ||
|
||
zip.file(file.name, file); | ||
|
||
const content = await zip.generateAsync({ type: "blob" }); | ||
const domain = await zeabur.deploy(content, "hkg1", convertedName); | ||
const domain = await deploy(content, convertedName); | ||
|
||
return domain; | ||
} | ||
|
||
async function createTemporaryProject(): Promise<string> { | ||
try { | ||
const res = await fetch(API_URL, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
query: `mutation CreateTemporaryProject() { | ||
createTemporaryProject() { | ||
_id | ||
} | ||
}`, | ||
}), | ||
}); | ||
|
||
const { data } = await res.json(); | ||
|
||
return data.createTemporaryProject._id; | ||
} catch (error) { | ||
console.error(error); | ||
throw error; | ||
} | ||
} | ||
|
||
async function createService( | ||
projectID: string, | ||
serviceName: string | ||
): Promise<string> { | ||
try { | ||
const res = await fetch(API_URL, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
query: `mutation CreateService($projectID: ObjectID!, $template: ServiceTemplate!, $name: String!) { | ||
createService(projectID: $projectID, template: $template, name: $name) { | ||
_id | ||
} | ||
}`, | ||
variables: { | ||
projectID, | ||
template: "GIT", | ||
name: serviceName, | ||
}, | ||
}), | ||
}); | ||
|
||
const { data } = await res.json(); | ||
|
||
return data.createService._id; | ||
} catch (error) { | ||
console.error(error); | ||
throw error; | ||
} | ||
} | ||
|
||
async function getEnvironment(projectID: string): Promise<string> { | ||
try { | ||
const res = await fetch(API_URL, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
query: `query GetEnvironment($projectID: ObjectID!) { | ||
environments(projectID: $projectID) { | ||
_id | ||
} | ||
}`, | ||
variables: { | ||
projectID, | ||
}, | ||
}), | ||
}); | ||
|
||
const { data } = await res.json(); | ||
|
||
return data.environments[0]._id; | ||
} catch (error) { | ||
console.error(error); | ||
throw error; | ||
} | ||
} | ||
|
||
async function createDomain( | ||
serviceID: string, | ||
environmentID: string, | ||
serviceName: string, | ||
domainName?: string | ||
): Promise<string> { | ||
try { | ||
const res = await fetch(API_URL, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
query: `mutation CreateDomain($serviceID: ObjectID!, $environmentID: ObjectID!, $domain: String!, $isGenerated: Boolean!) { | ||
addDomain(serviceID: $serviceID, environmentID: $environmentID, domain: $domain, isGenerated: $isGenerated) { | ||
domain | ||
} | ||
}`, | ||
variables: { | ||
serviceID, | ||
environmentID, | ||
domain: domainName ?? `${serviceName + generateRandomString()}`, | ||
isGenerated: true, | ||
}, | ||
}), | ||
}); | ||
|
||
const { data } = await res.json(); | ||
return data.addDomain.domain; | ||
} catch (error) { | ||
console.error(error); | ||
throw error; | ||
} | ||
} | ||
|
||
async function deploy(code: Blob, serviceName: string): Promise<string> { | ||
try { | ||
if (!code) throw new Error("Code is required"); | ||
|
||
const projectID = await createTemporaryProject(); | ||
const environmentID = await getEnvironment(projectID); | ||
const serviceID = await createService(projectID, serviceName); | ||
|
||
const formData = new FormData(); | ||
formData.append("environment", environmentID); | ||
formData.append("code", code, "code.zip"); | ||
|
||
await fetch( | ||
`https://gateway.zeabur.com/projects/${projectID}/services/${serviceID}/deploy`, | ||
{ | ||
method: "POST", | ||
body: formData, | ||
} | ||
); | ||
|
||
const domain = await createDomain(serviceID, environmentID, serviceName); | ||
|
||
return domain; | ||
} catch (error) { | ||
console.error(error); | ||
throw error; | ||
} | ||
} | ||
|
||
function convertTitle(title: string) { | ||
return title.replace(/[^a-zA-Z0-9]/g, "-").toLowerCase(); | ||
} | ||
|
||
function generateRandomString() { | ||
let result = ""; | ||
const characters = "abcdefghijklmnopqrstuvwxyz"; | ||
const charactersLength = characters.length; | ||
for (let i = 0; i < 6; i++) { | ||
result += characters.charAt(Math.floor(Math.random() * charactersLength)); | ||
} | ||
return result; | ||
} | ||
|
||
export { deployToZeabur }; |