Skip to content

Commit

Permalink
Add episode post endpoint (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
cultpodcasts authored Aug 11, 2024
1 parent ed0883e commit a72df2a
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Env = {
securePodcastEndpoint: URL;
secureSubjectEndpoint: URL;
secureEpisodesOutgoingEndpoint: URL;
secureEpisodePublishEndpoint: URL;
}

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

app.post("/episode/publish/:id", auth0Middleware, async (c) => {
const auth0Payload: Auth0JwtPayload = c.var.auth0('payload');
const id = c.req.param('id')
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.secureEpisodePublishEndpoint}/${id}`;
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.secureEpisodePublishEndpoint).host
},
method: "POST",
body: body
});
if (resp.status == 202) {
console.log(`Successfully used secure-episode-endpoint.`);
return new Response(resp.body);
} 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);
});

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

0 comments on commit a72df2a

Please sign in to comment.