Skip to content

Commit

Permalink
Add outgoing episodes endpoint (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
cultpodcasts authored Aug 7, 2024
1 parent 8935fa0 commit ed0883e
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ type Env = {
secureDiscoveryCurationEndpoint: URL;
securePodcastIndexEndpoint: URL;
securePodcastEndpoint: URL;
secureSubjectEndpoint: URL
secureSubjectEndpoint: URL;
secureEpisodesOutgoingEndpoint: URL;
}

const allowedOrigins: Array<string> = [
Expand Down Expand Up @@ -460,6 +461,41 @@ app.post("/episode/:id", auth0Middleware, async (c) => {
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");
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")!;
console.log(`Using auth header '${authorisation.slice(0, 20)}..'`);
const url = `${c.env.secureEpisodesOutgoingEndpoint}`;
console.log(url);
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.secureEpisodeEndpoint).host
},
method: "GET"
});
if (resp.status == 200) {
console.log(`Successfully used secure-episodes-outgoing-endpoint.`);

return new Response(resp.body);
} else {
console.log(`Failed to use secure-episodes-outgoing-endpoint. Response code: '${resp.status}'.`);
return c.json({ error: "Error" }, 500);
}
}
return c.json({ error: "Unauthorised" }, 403);
});

app.get("/podcast/:name", auth0Middleware, async (c) => {
const auth0Payload: Auth0JwtPayload = c.var.auth0('payload');
const name = c.req.param('name')
Expand Down Expand Up @@ -667,10 +703,10 @@ app.put("/subject", auth0Middleware, async (c) => {
});
if (resp.status == 202) {
console.log(`Successfully used secure-subject-endpoint.`);
return new Response(resp.body, {status:resp.status});
return new Response(resp.body, { status: resp.status });
} else if (resp.status == 409) {
console.log(`Conflict reported on secure-subject-endpoint.`);
return new Response(resp.body, {status:resp.status});
return new Response(resp.body, { status: resp.status });
} else {
console.log(`Failed to use secure-subject-endpoint. Response code: '${resp.status}'.`);
return c.json({ error: "Error" }, 500);
Expand Down

0 comments on commit ed0883e

Please sign in to comment.