Skip to content

Commit

Permalink
use all_url to fix cors
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelYuhe committed Jun 25, 2024
1 parent 42b78a4 commit 346ad17
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 16 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"content_scripts": [
{
"matches": ["https://chatgpt.com/*"],
"matches": ["<all_urls>"],
"js": ["content_script.js"]
}
],
Expand All @@ -24,5 +24,5 @@
"service_worker": "service_worker.js"
},
"permissions": ["sidePanel", "storage"],
"host_permissions": ["https://chatgpt.com/*"]
"host_permissions": ["<all_urls>"]
}
169 changes: 162 additions & 7 deletions src/deploy.ts
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 };

0 comments on commit 346ad17

Please sign in to comment.