Skip to content

Commit

Permalink
Add delete-episode endpoint (#50)
Browse files Browse the repository at this point in the history
* Add delete-episode endpoint

* Dev tested endpoint
  • Loading branch information
cultpodcasts authored Oct 22, 2024
1 parent 9083ff7 commit 3f28446
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/corsOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const corsOptions = {
return getOrigin(origin, c.env.stagingHostSuffix);
},
allowHeaders: ['content-type', 'authorization'],
allowMethods: ['GET', 'HEAD', 'POST', 'OPTIONS', 'PUT'],
allowMethods: ['GET', 'HEAD', 'POST', 'OPTIONS', 'PUT', 'DELETE'],
maxAge: 86400,
credentials: true,
exposeHeaders: ['X-Origin']
Expand Down
32 changes: 32 additions & 0 deletions src/deleteEpisode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { AddResponseHeaders } from "./AddResponseHeaders";
import { Auth0ActionContext } from "./Auth0ActionContext";
import { Auth0JwtPayload } from "./Auth0JwtPayload";
import { buildFetchHeaders } from "./buildFetchHeaders";


export async function deleteEpisode(c: Auth0ActionContext): Promise<Response> {
const auth0Payload: Auth0JwtPayload = c.var.auth0('payload');
const id = c.req.param('id');
AddResponseHeaders(c, { methods: ["POST", "GET", "OPTIONS", "DELETE"] });
if (auth0Payload?.permissions && auth0Payload.permissions.includes('admin')) {
const url = `${c.env.secureEpisodeEndpoint}/${id}`;
const resp = await fetch(url, {
headers: buildFetchHeaders(c.req, c.env.secureEpisodeEndpoint),
method: "DELETE"
});
if (resp.status == 200) {
console.log(`Successfully used secure-episode-endpoint.`);
return new Response(resp.body);
} else if (resp.status == 404) {
console.log(`Failed to use secure-episode-endpoint. Episode not found.`);
return new Response(resp.body, {status: resp.status});
} else if (resp.status == 300) {
console.log(`Failed to use secure-episode-endpoint. Multple podcast/episodes found.`);
return new Response(resp.body, {status: resp.status});
} else {
console.log(`Failed to use secure-episode-endpoint. Response code: '${resp.status}'.`);
return c.json({ error: "Error" }, 500);
}
}
return c.json({ error: "Unauthorised" }, 403);
}
2 changes: 1 addition & 1 deletion src/getEpisode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { buildFetchHeaders } from './buildFetchHeaders';
export async function getEpisode(c: Auth0ActionContext): Promise<Response> {
const auth0Payload: Auth0JwtPayload = c.var.auth0('payload');
const id = c.req.param('id');
AddResponseHeaders(c, { methods: ["POST", "GET", "OPTIONS"] });
AddResponseHeaders(c, { methods: ["POST", "GET", "OPTIONS", "DELETE"] });

if (auth0Payload?.permissions && auth0Payload.permissions.includes('curate')) {
const authorisation: string = c.req.header("Authorization")!;
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { submitDiscovery } from './submitDiscovery';
import { runSearchIndexer } from './runSearchIndexer';
import { publishHomepage } from './publishHomepage';
import { publishTerm } from './publishTerm';
import { deleteEpisode } from './deleteEpisode';

const app = new Hono<{ Bindings: Env }>();

Expand All @@ -35,6 +36,7 @@ app.post("/search", search);
app.post("/submit", Auth0Middleware, submit);
app.get("/episode/:id", Auth0Middleware, getEpisode);
app.post("/episode/:id", Auth0Middleware, updateEpisode);
app.delete("/episode/:id", Auth0Middleware, deleteEpisode);
app.post("/episode/publish/:id", Auth0Middleware, publish);
app.get("/episodes/outgoing", Auth0Middleware, getOutgoing);
app.get("/podcast/:name", Auth0Middleware, getPodcastByName);
Expand Down
2 changes: 1 addition & 1 deletion src/updateEpisode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { buildFetchHeaders } from "./buildFetchHeaders";
export async function updateEpisode(c: Auth0ActionContext): Promise<Response> {
const auth0Payload: Auth0JwtPayload = c.var.auth0('payload');
const id = c.req.param('id');
AddResponseHeaders(c, { methods: ["POST", "GET", "OPTIONS"] });
AddResponseHeaders(c, { methods: ["POST", "GET", "OPTIONS", "DELETE"] });
if (auth0Payload?.permissions && auth0Payload.permissions.includes('curate')) {
const url = `${c.env.secureEpisodeEndpoint}/${id}`;
const data: any = await c.req.json();
Expand Down

0 comments on commit 3f28446

Please sign in to comment.