-
Notifications
You must be signed in to change notification settings - Fork 0
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
8cf9105
commit c6d36d6
Showing
24 changed files
with
705 additions
and
620 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
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 +1 @@ | ||
# UI and API to configure custom domains | ||
# UI and API to configure custom domains |
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,97 +1,128 @@ | ||
|
||
import { addDomainToEdgeConfig, getEdgeconfigItem, updateEdgeConfigItem, removeDomainFromEdgeConfig, getAllItemsFromEdgeConfig } from "@customdomainready/sdk"; | ||
import { NextResponse } from "next/server"; | ||
import { z } from "zod"; | ||
import { | ||
addDomainToEdgeConfig, | ||
getEdgeconfigItem, | ||
updateEdgeConfigItem, | ||
removeDomainFromEdgeConfig, | ||
getAllItemsFromEdgeConfig, | ||
} from '@customdomainready/sdk'; | ||
import { NextResponse } from 'next/server'; | ||
import { z } from 'zod'; | ||
|
||
const customDomainSchema = z.object({ | ||
domain: z.string(), | ||
slug: z.string(), | ||
destination: z.string() | ||
}) | ||
|
||
export async function GET( | ||
req: Request, | ||
) { | ||
try { | ||
const response = await getAllItemsFromEdgeConfig(process.env.VERCEL_CUSTOM_DOMAIN_PROXY_EDGE_CONFIG_ID!, process.env.VERCEL_TEAM_ID, process.env.AUTH_BEARER_TOKEN) | ||
console.log(response) | ||
return NextResponse.json({ | ||
response: response.map((item: any) => ({ | ||
id: item.key, | ||
sourceDomain: `https://${item.key.split('-')[0].replace(/_/g, '.')}`, | ||
slug: item.key.split('-').slice(1).join('/').replace(/_/g, '.'), | ||
destinationPath: item.value, | ||
})) | ||
}); | ||
} catch (error) { | ||
console.error(error) | ||
|
||
return new Response("Internal Server Error", { status: 500 }) | ||
} | ||
domain: z.string(), | ||
slug: z.string(), | ||
destination: z.string(), | ||
}); | ||
|
||
export async function GET(req: Request) { | ||
try { | ||
const response = await getAllItemsFromEdgeConfig( | ||
process.env.VERCEL_CUSTOM_DOMAIN_PROXY_EDGE_CONFIG_ID!, | ||
process.env.VERCEL_TEAM_ID, | ||
process.env.AUTH_BEARER_TOKEN, | ||
); | ||
console.log(response); | ||
return NextResponse.json({ | ||
response: response.map((item: any) => ({ | ||
id: item.key, | ||
sourceDomain: `https://${item.key.split('-')[0].replace(/_/g, '.')}`, | ||
slug: item.key.split('-').slice(1).join('/').replace(/_/g, '.'), | ||
destinationPath: item.value, | ||
})), | ||
}); | ||
} catch (error) { | ||
console.error(error); | ||
|
||
return new Response('Internal Server Error', { status: 500 }); | ||
} | ||
} | ||
|
||
|
||
|
||
export async function PATCH( | ||
req: Request | ||
) { | ||
try { | ||
const body = await req.json() | ||
const payload = customDomainSchema.parse(body) | ||
|
||
const domainWithoutProtocol = payload.domain.replace(/^https?:\/\//, ''); | ||
const key = `${domainWithoutProtocol.replace(/\./g, '_')}${payload.slug.replace(/\//g, '-')}`; | ||
|
||
// validate if key already exist and if yes update it instead of creating | ||
const existingDomain = await getEdgeconfigItem(key, process.env.VERCEL_CUSTOM_DOMAIN_PROXY_EDGE_CONFIG_ID!, process.env.VERCEL_TEAM_ID, process.env.AUTH_BEARER_TOKEN); | ||
|
||
if (existingDomain){ | ||
const updateResponse = await updateEdgeConfigItem(key, payload.destination, process.env.VERCEL_CUSTOM_DOMAIN_PROXY_EDGE_CONFIG_ID!, process.env.VERCEL_TEAM_ID, process.env.AUTH_BEARER_TOKEN); | ||
console.log(updateResponse); | ||
} else { | ||
const createResponse = await addDomainToEdgeConfig(key, payload.destination, process.env.VERCEL_CUSTOM_DOMAIN_PROXY_EDGE_CONFIG_ID!, process.env.VERCEL_TEAM_ID, process.env.AUTH_BEARER_TOKEN); | ||
console.log(createResponse); | ||
} | ||
|
||
return new Response('created', { status: 201}) | ||
} catch (error) { | ||
console.log(error) | ||
if (error instanceof z.ZodError) { | ||
return new Response(JSON.stringify(error.issues), { status: 422 }) | ||
} | ||
|
||
return new Response(null, { status: 500 }) | ||
export async function PATCH(req: Request) { | ||
try { | ||
const body = await req.json(); | ||
const payload = customDomainSchema.parse(body); | ||
|
||
const domainWithoutProtocol = payload.domain.replace(/^https?:\/\//, ''); | ||
const key = `${domainWithoutProtocol.replace( | ||
/\./g, | ||
'_', | ||
)}${payload.slug.replace(/\//g, '-')}`; | ||
|
||
// validate if key already exist and if yes update it instead of creating | ||
const existingDomain = await getEdgeconfigItem( | ||
key, | ||
process.env.VERCEL_CUSTOM_DOMAIN_PROXY_EDGE_CONFIG_ID!, | ||
process.env.VERCEL_TEAM_ID, | ||
process.env.AUTH_BEARER_TOKEN, | ||
); | ||
|
||
if (existingDomain) { | ||
const updateResponse = await updateEdgeConfigItem( | ||
key, | ||
payload.destination, | ||
process.env.VERCEL_CUSTOM_DOMAIN_PROXY_EDGE_CONFIG_ID!, | ||
process.env.VERCEL_TEAM_ID, | ||
process.env.AUTH_BEARER_TOKEN, | ||
); | ||
console.log(updateResponse); | ||
} else { | ||
const createResponse = await addDomainToEdgeConfig( | ||
key, | ||
payload.destination, | ||
process.env.VERCEL_CUSTOM_DOMAIN_PROXY_EDGE_CONFIG_ID!, | ||
process.env.VERCEL_TEAM_ID, | ||
process.env.AUTH_BEARER_TOKEN, | ||
); | ||
console.log(createResponse); | ||
} | ||
} | ||
|
||
export async function DELETE( | ||
req: Request | ||
) { | ||
try { | ||
const body = await req.json(); | ||
const payload = customDomainSchema.parse(body); | ||
|
||
const domainWithoutProtocol = payload.domain.replace(/^https?:\/\//, ''); | ||
const key = `${domainWithoutProtocol.replace(/\./g, '_')}${payload.slug.replace(/\//g, '-')}`; | ||
|
||
const existingDomain = await getEdgeconfigItem(key, process.env.VERCEL_CUSTOM_DOMAIN_PROXY_EDGE_CONFIG_ID!, process.env.VERCEL_TEAM_ID, process.env.AUTH_BEARER_TOKEN); | ||
|
||
if (existingDomain) { | ||
const deleteResponse = await removeDomainFromEdgeConfig(key, process.env.VERCEL_CUSTOM_DOMAIN_PROXY_EDGE_CONFIG_ID!, process.env.VERCEL_TEAM_ID, process.env.AUTH_BEARER_TOKEN); | ||
console.log(deleteResponse); | ||
return NextResponse.json('deleted', { status: 204 }); | ||
} else { | ||
return NextResponse.json('Domain not found', { status: 404 }); | ||
} | ||
} catch (error) { | ||
console.log(error); | ||
if (error instanceof z.ZodError) { | ||
return NextResponse.json(JSON.stringify(error.issues), { status: 422 }); | ||
} | ||
|
||
return NextResponse.json(null, { status: 500 }); | ||
return new Response('created', { status: 201 }); | ||
} catch (error) { | ||
console.log(error); | ||
if (error instanceof z.ZodError) { | ||
return new Response(JSON.stringify(error.issues), { status: 422 }); | ||
} | ||
|
||
return new Response(null, { status: 500 }); | ||
} | ||
} | ||
|
||
export async function DELETE(req: Request) { | ||
try { | ||
const body = await req.json(); | ||
const payload = customDomainSchema.parse(body); | ||
|
||
const domainWithoutProtocol = payload.domain.replace(/^https?:\/\//, ''); | ||
const key = `${domainWithoutProtocol.replace( | ||
/\./g, | ||
'_', | ||
)}${payload.slug.replace(/\//g, '-')}`; | ||
|
||
const existingDomain = await getEdgeconfigItem( | ||
key, | ||
process.env.VERCEL_CUSTOM_DOMAIN_PROXY_EDGE_CONFIG_ID!, | ||
process.env.VERCEL_TEAM_ID, | ||
process.env.AUTH_BEARER_TOKEN, | ||
); | ||
|
||
if (existingDomain) { | ||
const deleteResponse = await removeDomainFromEdgeConfig( | ||
key, | ||
process.env.VERCEL_CUSTOM_DOMAIN_PROXY_EDGE_CONFIG_ID!, | ||
process.env.VERCEL_TEAM_ID, | ||
process.env.AUTH_BEARER_TOKEN, | ||
); | ||
console.log(deleteResponse); | ||
return NextResponse.json('deleted', { status: 204 }); | ||
} else { | ||
return NextResponse.json('Domain not found', { status: 404 }); | ||
} | ||
} catch (error) { | ||
console.log(error); | ||
if (error instanceof z.ZodError) { | ||
return NextResponse.json(JSON.stringify(error.issues), { status: 422 }); | ||
} | ||
|
||
|
||
return NextResponse.json(null, { status: 500 }); | ||
} | ||
} |
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,17 +1,20 @@ | ||
import { | ||
removeDomainFromVercelProject, | ||
} from "@customdomainready/sdk"; | ||
import { removeDomainFromVercelProject } from '@customdomainready/sdk'; | ||
|
||
export async function DELETE( | ||
req: Request, | ||
{ params }: { params: { slug: string } }, | ||
req: Request, | ||
{ params }: { params: { slug: string } }, | ||
) { | ||
const domain = decodeURIComponent(params.slug); | ||
const response = await removeDomainFromVercelProject(domain, process.env.VERCEL_PROJECT_ID!, process.env.VERCEL_TEAM_ID, process.env.AUTH_BEARER_TOKEN); | ||
const domain = decodeURIComponent(params.slug); | ||
const response = await removeDomainFromVercelProject( | ||
domain, | ||
process.env.VERCEL_PROJECT_ID!, | ||
process.env.VERCEL_TEAM_ID, | ||
process.env.AUTH_BEARER_TOKEN, | ||
); | ||
|
||
if (response.error) { | ||
return new Response(response.error.message, { status: 400 }) | ||
} | ||
if (response.error) { | ||
return new Response(response.error.message, { status: 400 }); | ||
} | ||
|
||
return new Response(null, { status: 204 }) | ||
} | ||
return new Response(null, { status: 204 }); | ||
} |
Oops, something went wrong.