Skip to content

Instantly share code, notes, and snippets.

@mizchi
Created December 8, 2023 11:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mizchi/06d2e1641213cd4355d5d8cec2f1ba33 to your computer and use it in GitHub Desktop.
Save mizchi/06d2e1641213cd4355d5d8cec2f1ba33 to your computer and use it in GitHub Desktop.
// subhosting
const API = "https://api.deno.com/v1";
type CreateDeploymentResponse = {
id: string;
projectId: string;
description: string;
status: string;
domains: string[];
databases: Record<string, any>;
createdAt: string;
updatedAt: string
}
export async function createDeployment(opts: {
accessToken: string;
orgId: string;
projectId: string;
}): Promise<CreateDeploymentResponse> {
const res = await fetch(`${API}/projects/${opts.projectId}/deployments`, {
method: "POST",
headers: {
Authorization: `Bearer ${opts.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
entryPointUrl: "main.ts",
assets: {
"main.ts": {
"kind": "file",
"content": `Deno.serve(() => new Response("Hello, World 2!"));`,
"encoding": "utf-8",
},
},
envVars: {},
}),
});
return res.json();
}
type CreateProjectResponse = {
"id": string;
"name": string;
"description": string;
"createdAt": string;
"updatedAt": string;
}
export async function createProject(opts: {
accessToken: string;
orgId: string;
}): Promise<CreateProjectResponse> {
// Create a new project
const res = await fetch(`${API}/organizations/${opts.orgId}/projects`, {
method: "POST",
headers: {
Authorization: `Bearer ${opts.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
name: null, // randomly generates project name
}),
});
return await res.json();
}
export async function deleteProject(opts: {
accessToken: string;
projectId: string;
}): Promise<number> {
// Create a new project
const res = await fetch(`${API}/projects/${opts.projectId}`, {
method: "DELETE",
headers: {
Authorization: `Bearer ${opts.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
name: null, // randomly generates project name
}),
});
return res.status;
}
// const accessToken = Deno.env.get("DEPLOY_ACCESS_TOKEN");
// Replace with your desired project ID
// const projectId = "72c41fbc-06aa-43fa-bc36-8423f38522f5";
async function getDeploymentDetail(opts: {
accessToken: string;
deploymentId: string;
}) {
// Create a new deployment
const res = await fetch(`${API}/deployments/${opts.deploymentId}`, {
method: "GET",
headers: {
Authorization: `Bearer ${opts.accessToken}`,
"Content-Type": "application/json",
},
});
return res.json();
}
{
const accessToken = Deno.env.get("DEPLOY_ACCESS_TOKEN")!;
const orgId = Deno.env.get("DEPLOY_ORG_ID")!;
const project = await createProject({
accessToken,
orgId,
});
console.log('[project]', project);
const deployment = await createDeployment({
accessToken,
orgId,
projectId: project.id
});
console.log('[deployment]', deployment);
const detail = await getDeploymentDetail({
accessToken,
deploymentId: deployment.id,
});
console.log('[detail]', detail);
// const result = await deleteProject({
// accessToken: accessToken!,
// projectId: '72c41fbc-06aa-43fa-bc36-8423f38522f5',
// });
// console.log('[result]', result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment