Skip to content

Commit

Permalink
Support deleting records via API
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba2k2 committed Oct 23, 2024
1 parent 4c0419a commit 308b822
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/routes/licenses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ export async function licenses(
): Promise<LicenseData | RequestData[] | null | string[] | Response> {
const url = new URL(request.url)

if (request.method == "GET") {
if (request.method == "GET" || request.method == "DELETE") {
let licenseKey: string
if ((licenseKey = url.pathname.split("/")[2])) {
const license = await env.LICENSES.get<LicenseData>(
licenseKey,
"json",
)
if (!license) return new Response(null, { status: 404 })
if (request.method == "DELETE") {
await env.LICENSES.delete(licenseKey)
return new Response(null, { status: 204 })
}
return license
}
return (await env.LICENSES.list()).keys.map((k) => k.name)
Expand Down
6 changes: 5 additions & 1 deletion src/routes/products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ export async function products(
): Promise<ObjectType | null | string[] | Response> {
const url = new URL(request.url)

if (request.method == "GET") {
if (request.method == "GET" || request.method == "DELETE") {
let productKey: string
if ((productKey = url.pathname.split("/")[2])) {
const product = await env.PRODUCTS.get<ObjectType>(
productKey,
"json",
)
if (!product) return new Response(null, { status: 404 })
if (request.method == "DELETE") {
await env.PRODUCTS.delete(productKey)
return new Response(null, { status: 204 })
}
return product
}
return (await env.PRODUCTS.list()).keys.map((k) => k.name)
Expand Down
6 changes: 5 additions & 1 deletion src/routes/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ export async function projects(
): Promise<ProjectData | RequestData | null | string[] | Response> {
const url = new URL(request.url)

if (request.method == "GET") {
if (request.method == "GET" || request.method == "DELETE") {
let projectKey: string
if ((projectKey = url.pathname.split("/")[2])) {
const project = await env.PROJECTS.get<ProjectData>(
projectKey,
"json",
)
if (!project) return new Response(null, { status: 404 })
if (request.method == "DELETE") {
await env.PROJECTS.delete(projectKey)
return new Response(null, { status: 204 })
}
return project
}
return (await env.PROJECTS.list()).keys.map((k) => k.name)
Expand Down
6 changes: 5 additions & 1 deletion src/routes/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,15 @@ export async function schemas(
return Response.json(result)
}

if (request.method == "GET") {
if (request.method == "GET" || request.method == "DELETE") {
let schemaKey: string
if ((schemaKey = url.pathname.split("/")[2])) {
const schema = await env.SCHEMAS.get<ObjectType>(schemaKey, "json")
if (!schema) return new Response(null, { status: 404 })
if (request.method == "DELETE") {
await env.SCHEMAS.delete(schemaKey)
return new Response(null, { status: 204 })
}
return schema
}
return (await env.SCHEMAS.list()).keys.map((k) => k.name)
Expand Down

0 comments on commit 308b822

Please sign in to comment.