Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubno committed Dec 19, 2024
1 parent dbe569d commit b72817d
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 39 deletions.
30 changes: 17 additions & 13 deletions apps/web/src/app/(dashboard)/dashboard/utils.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
export function getBaseUrl(domain: string) {
function getUrl(domain: string, subdomain: string, path: string) {
let url = domain
const local = domain.startsWith('localhost') || domain.startsWith('127.0.0.')

if (!domain.startsWith('http')) {
const local = domain === 'localhost' || domain.startsWith('127.0.0.')
url = `http${local ? '' : 's'}://${domain}`
}

const parsedUrl = new URL(url)

return parsedUrl.toString()
}

export function getBillingUrl(domain: string) {
let url = domain
const local = domain === 'localhost' || domain.startsWith('127.0.0.')

if (!domain.startsWith('http')) {
url = `http${local ? '' : 's'}://${domain}`
if (path) {
const decodedUrl = decodeURIComponent(path)
const [pathname, queryString] = decodedUrl.split('?')
parsedUrl.pathname = pathname
if (queryString) parsedUrl.search = queryString
}

const parsedUrl = new URL(url)
if (!local) {
parsedUrl.hostname = `billing.${parsedUrl.hostname}`
parsedUrl.hostname = `${subdomain}.${parsedUrl.hostname}`
}

return parsedUrl.toString()
}

export function getAPIUrl(domain: string, path: string) {
return getUrl(domain, 'api', path)
}

export function getBillingUrl(domain: string, path: string) {
return getUrl(domain, 'billing', path)
}
4 changes: 2 additions & 2 deletions apps/web/src/components/Dashboard/Billing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const BillingContent = ({
const getInvoices = async function getInvoices() {
setInvoices([])
const res = await fetch(
`${getBillingUrl(domain)}/teams/${team.id}/invoices`,
getBillingUrl(domain, `/teams/${team.id}/invoices`),
{
headers: {
'X-Team-API-Key': team.apiKeys[0],
Expand All @@ -60,7 +60,7 @@ export const BillingContent = ({

setCredits(null)
const creditsRes = await fetch(
`${getBillingUrl(domain)}/teams/${team.id}/usage`,
getBillingUrl(domain, `/teams/${team.id}/usage`),
{
headers: {
'X-Team-API-Key': team.apiKeys[0],
Expand Down
3 changes: 0 additions & 3 deletions apps/web/src/components/Dashboard/Developer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ export const DeveloperContent = ({
hostParts = hostParts.slice(1)
}
setDomain(hostParts.join('.'))
} else {
console.log('Invalid URL')
setDomain('Invalid URL')
}
}

Expand Down
18 changes: 10 additions & 8 deletions apps/web/src/components/Dashboard/Keys.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const KeysContent = ({
useEffect(() => {
async function fetchApiKeys() {
const res = await fetch(
`${getBillingUrl(domain)}/teams/${currentTeam.id}/api-keys`,
getBillingUrl(domain, `/teams/${currentTeam.id}/api-keys`),
{
headers: {
'X-USER-ACCESS-TOKEN': user.accessToken,
Expand Down Expand Up @@ -98,9 +98,10 @@ export const KeysContent = ({
}

const res = await fetch(
`${getBillingUrl(domain)}/teams/${currentTeam.id}/api-keys/${
currentKey?.id
}`,
getBillingUrl(
domain,
`/teams/${currentTeam.id}/api-keys/${currentKey?.id}`
),
{
method: 'DELETE',
headers: {
Expand All @@ -123,7 +124,7 @@ export const KeysContent = ({

async function createApiKey() {
const res = await fetch(
`${getBillingUrl(domain)}/teams/${currentTeam.id}/api-keys`,
getBillingUrl(domain, `/teams/${currentTeam.id}/api-keys`),
{
method: 'POST',
headers: {
Expand Down Expand Up @@ -155,9 +156,10 @@ export const KeysContent = ({

async function updateApiKey() {
const res = await fetch(
`${getBillingUrl(domain)}/teams/${currentTeam.id}/api-keys/${
currentKey?.id
}`,
getBillingUrl(
domain,
`/teams/${currentTeam.id}/api-keys/${currentKey?.id}`
),
{
method: 'PATCH',
headers: {
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/Dashboard/Personal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const PersonalContent = ({
}

const updateUserEmail = async () => {
const res = await fetch(`${getBillingUrl(domain)}/users`, {
const res = await fetch(getBillingUrl(domain, '/users'), {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/components/Dashboard/Sandboxes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { useState } from 'react'
import { useEffect } from 'react'
import { Team } from '@/utils/useUser'
import { getBaseUrl } from '@/app/(dashboard)/dashboard/utils'
import { getAPIUrl } from '@/app/(dashboard)/dashboard/utils'

interface Sandbox {
alias: string
Expand Down Expand Up @@ -105,7 +105,7 @@ async function fetchSandboxes(
domain: string,
apiKey: string
): Promise<Sandbox[]> {
const res = await fetch(`${getBaseUrl(domain)}/sandboxes`, {
const res = await fetch(getAPIUrl(domain, '/sandboxes'), {
method: 'GET',
headers: {
'X-API-KEY': apiKey,
Expand Down
8 changes: 4 additions & 4 deletions apps/web/src/components/Dashboard/Team.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const TeamContent = ({
useEffect(() => {
const getTeamMembers = async () => {
const res = await fetch(
`${getBillingUrl(domain)}/teams/${team.id}/users`,
getBillingUrl(domain, `/teams/${team.id}/users`),
{
headers: {
'X-User-Access-Token': user.accessToken,
Expand Down Expand Up @@ -105,7 +105,7 @@ export const TeamContent = ({
}

const deleteUserFromTeam = async () => {
const res = await fetch(`${getBillingUrl(domain)}/teams/${team.id}/users`, {
const res = await fetch(getBillingUrl(domain, `/teams/${team.id}/users`), {
method: 'DELETE',
headers: {
'X-User-Access-Token': user.accessToken,
Expand All @@ -128,7 +128,7 @@ export const TeamContent = ({
}

const changeTeamName = async () => {
const res = await fetch(`${getBillingUrl(domain)}/teams/${team.id}`, {
const res = await fetch(getBillingUrl(domain, `/teams/${team.id}`), {
headers: {
'X-Team-API-Key': team.apiKeys[0],
'Content-Type': 'application/json',
Expand Down Expand Up @@ -165,7 +165,7 @@ export const TeamContent = ({
return
}

const res = await fetch(`${getBillingUrl(domain)}/teams/${team.id}/users`, {
const res = await fetch(getBillingUrl(domain, `/teams/${team.id}/users`), {
headers: {
'X-User-Access-Token': user.accessToken,
'Content-Type': 'application/json',
Expand Down
8 changes: 4 additions & 4 deletions apps/web/src/components/Dashboard/Templates.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
AlertDialogAction,
} from '../ui/alert-dialog'
import { toast } from '../ui/use-toast'
import { getBaseUrl } from '@/app/(dashboard)/dashboard/utils'
import { getAPIUrl } from '@/app/(dashboard)/dashboard/utils'

interface Template {
aliases: string[]
Expand Down Expand Up @@ -78,7 +78,7 @@ export function TemplatesContent({

async function deleteTemplate(domain: string) {
const res = await fetch(
`${getBaseUrl(domain)}/templates/${currentTemplate?.templateID}`,
getAPIUrl(domain, `/templates/${currentTemplate?.templateID}`),
{
method: 'DELETE',
headers: {
Expand All @@ -105,7 +105,7 @@ export function TemplatesContent({

async function publishTemplate(domain: string) {
const res = await fetch(
`${getBaseUrl(domain)}/templates/${currentTemplate?.templateID}`,
getAPIUrl(domain, `/templates/${currentTemplate?.templateID}`),
{
method: 'PATCH',
headers: {
Expand Down Expand Up @@ -306,7 +306,7 @@ async function fetchTemplates(
accessToken: string,
teamId: string
): Promise<Template[]> {
const res = await fetch(`${getBaseUrl(domain)}/templates?teamID=${teamId}`, {
const res = await fetch(getAPIUrl(domain, `templates?teamID=${teamId}`), {
method: 'GET',
headers: {
Authorization: `Bearer ${accessToken}`,
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/Dashboard/Usage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const UsageContent = ({
setCostUsage([])

const response = await fetch(
`${getBillingUrl(domain)}/teams/${team.id}/usage`,
getBillingUrl(domain, `/teams/${team.id}/usage`),
{
headers: {
'X-Team-API-Key': team.apiKeys[0],
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/Pricing/SwitchToProButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function createCheckout(domain: string, tierID: string, teamID: string) {
})
}

return fetch(`${getBillingUrl(domain)}/checkouts`, {
return fetch(getBillingUrl(domain, '/checkouts'), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down

0 comments on commit b72817d

Please sign in to comment.