Skip to content

Commit

Permalink
Add podcast index endpoint (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
cultpodcasts committed Aug 4, 2024
1 parent e69345f commit 852cd36
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Env = {
secureSubmitEndpoint: URL;
secureEpisodeEndpoint: URL;
secureDiscoveryCurationEndpoint: URL;
securePodcastIndexEndpoint: URL;
}

const allowedOrigins: Array<string> = [
Expand Down Expand Up @@ -456,6 +457,43 @@ app.post("/episode/:id", auth0Middleware, async (c) => {
return c.json({ error: "Unauthorised" }, 403);
});

app.post("/podcast/index/:name", auth0Middleware, async (c) => {
const auth0Payload: Auth0JwtPayload = c.var.auth0('payload');
const name = c.req.param('name')
c.header("Cache-Control", "max-age=600");
c.header("Content-Type", "application/json");
c.header("Access-Control-Allow-Origin", getOrigin(c.req.header("Origin")));
c.header("Access-Control-Allow-Methods", "POST,GET,OPTIONS");

if (auth0Payload?.permissions && auth0Payload.permissions.includes('curate')) {
const authorisation: string = c.req.header("Authorization")!;
const url = `${c.env.securePodcastIndexEndpoint}/${encodeURIComponent(name)}`;
console.log("URl is "+url);
const data: any = await c.req.json();
const body: string = JSON.stringify(data)
const resp = await fetch(url, {
headers: {
'Accept': "*/*",
'Authorization': authorisation,
"Content-type": "application/json",
"Cache-Control": "no-cache",
"User-Agent": "cult-podcasts-api",
"Host": new URL(c.env.securePodcastIndexEndpoint).host
},
method: "POST",
body: body
});
if (resp.status == 202) {
console.log(`Successfully used secure-podcast-index-endpoint.`);
return new Response(resp.body);
} else {
console.log(`Failed to use secure-podcast-index-endpoint. Response code: '${resp.status}'.`);
return c.json({ error: "Error" }, 500);
}
}
return c.json({ error: "Unauthorised" }, 403);
});

app.get("/discovery-curation", auth0Middleware, async (c) => {
const auth0Payload: Auth0JwtPayload = c.var.auth0('payload');
c.header("Cache-Control", "max-age=600");
Expand Down

0 comments on commit 852cd36

Please sign in to comment.