Skip to content

Commit

Permalink
feat: update code
Browse files Browse the repository at this point in the history
  • Loading branch information
marcolivierbouch committed Nov 26, 2024
1 parent 8cf9105 commit c6d36d6
Show file tree
Hide file tree
Showing 24 changed files with 705 additions and 620 deletions.
9 changes: 3 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
TURBO_TEAM: ${{ vars.TURBO_TEAM }}
strategy:
matrix:
node-version: [18.x, 20.x]
node-version: [20.x]
steps:
- name: Checkout
uses: actions/checkout@v3
Expand All @@ -34,8 +34,5 @@ jobs:
- name: Install dependencies
run: pnpm install --frozen-lockfile

- name: Install Playwright Browsers
run: pnpm exec playwright install --with-deps

- name: Run tests
run: pnpm test
- name: Build
run: pnpm build
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ For this tutorial you'll need a [Vercel](https://vercel.com/) account.
1. Create a new storage edge config.
2. Create a new vercel project and import configuration-api.
3. Create a new project and import custom-domain-proxy.
4. Connect the storage to the project custom-domain-proxy.
4. Connect the storage to the project custom-domain-proxy.
5. Add the required env vars for the configuration-api

```bash
Expand All @@ -20,4 +20,4 @@ AUTH_BEARER_TOKEN= # create your own bearer token that can access the projects
```

6. Add a new domain using the configuration-api
7. Finish the domain configuration
7. Finish the domain configuration
2 changes: 1 addition & 1 deletion apps/configuration-api/README.md
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
205 changes: 118 additions & 87 deletions apps/configuration-api/app/api/assign/route.ts
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 });
}
}
27 changes: 15 additions & 12 deletions apps/configuration-api/app/api/domain/[slug]/route.ts
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 });
}
Loading

0 comments on commit c6d36d6

Please sign in to comment.