-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to make it easy to add new api endpoints
- Loading branch information
Showing
6 changed files
with
160 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,14 @@ | ||
# cloudflare-pages-react-tailwind-tmpl | ||
# Listen Notes ChatGPT Plugin | ||
|
||
## Environment Variables | ||
|
||
* LISTEN_API_KEY | ||
* CHATGPT_SECRET | ||
* NODE_VERSION | ||
* CHATGPT_VERIFICATION_TOKEN | ||
|
||
## How to add a new API endpoint | ||
|
||
1. For a new endpoint, implement a new Def file in `edge-src/api-definitions`, like JustListenDef.js | ||
2. Create a new function file under `functions/api/v2`, like just_listen.js | ||
3. Add the new Def object to params.paths in `chatgpt-plugin/openapi.json/index.js`, like `...new JustListenDef().openApiPathSpec()` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export default class BaseDef { | ||
constructor() { | ||
} | ||
|
||
transformResultFunc(result) { | ||
return {} | ||
} | ||
|
||
apiFunctionName() { | ||
return '' | ||
} | ||
|
||
openApiPathSpec() { | ||
return {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import BaseDef from "./BaseDef"; | ||
|
||
export default class JustListenDef extends BaseDef { | ||
apiFunctionName() { | ||
return 'justListen' | ||
} | ||
|
||
transformResultFunc(result) { | ||
return ({ | ||
title: result.title, | ||
description: result.description, | ||
image: result.image, | ||
audio: result.audio, | ||
audio_length_sec: result.audio_length_sec, | ||
pub_date_ms: result.pub_date_ms, | ||
listennotes_url: result.listennotes_url, | ||
podcast: { | ||
title: result.podcast.title, | ||
publisher: result.podcast.publisher, | ||
image: result.podcast.image, | ||
listen_score: result.podcast.listen_score, | ||
listen_score_global_rank: result.podcast.listen_score_global_rank, | ||
listennotes_url: result.podcast.listennotes_url, | ||
} | ||
}) | ||
} | ||
|
||
openApiPathSpec() { | ||
const justListenEndpoint = { | ||
get: { | ||
operationId: 'justListen', | ||
description: 'Get a random podcast episode, ' + | ||
'with all necessary metadata to describe this episode and stream the audio.' + | ||
'Recently published episodes are more likely to be fetched.', | ||
parameters: [], | ||
responses: { | ||
'200': { | ||
description: 'Returns a json object with the podcast episode data', | ||
content: { | ||
'application/json': { | ||
schema: { | ||
type: 'object', | ||
properties: { | ||
title: { | ||
type: 'string', | ||
description: 'title of the podcast episode', | ||
}, | ||
description: { | ||
type: 'string', | ||
description: 'description of the podcast episode in html', | ||
}, | ||
image: { | ||
type: 'string', | ||
description: 'image url of the podcast episode', | ||
}, | ||
audio: { | ||
type: 'string', | ||
description: 'audio url of the podcast episode, used for playing the audio', | ||
}, | ||
audio_length_sec: { | ||
type: 'integer', | ||
description: 'audio length of the podcast episode in seconds', | ||
}, | ||
pub_date_ms: { | ||
type: 'integer', | ||
description: 'published date of the podcast episode in milliseconds of the Unix epoch', | ||
}, | ||
listennotes_url: { | ||
type: 'string', | ||
description: 'the canonical url of the podcast episode on Listen Notes, ' + | ||
'which can be used to manually share this episode', | ||
}, | ||
podcast: { | ||
type: 'object', | ||
properties: { | ||
title: { | ||
type: 'string', | ||
description: 'title of the podcast that this episode belongs to', | ||
}, | ||
publisher: { | ||
type: 'string', | ||
description: 'publisher of the podcast that this episode belongs to', | ||
}, | ||
image: { | ||
type: 'string', | ||
description: 'image url of the podcast that this episode belongs to', | ||
}, | ||
listen_score: { | ||
type: 'integer', | ||
description: 'Listen Score of the podcast that this episode belongs to, ' + | ||
'which indicates the estimated popularity of the podcast (similar to nielsen ratings)', | ||
}, | ||
listen_score_global_rank: { | ||
type: 'string', | ||
description: 'the global rank of the podcast according to Listen Score', | ||
}, | ||
listennotes_url: { | ||
type: 'string', | ||
description: 'the canonical url of the podcast on Listen Notes, ' + | ||
'which can be used to manually share this podcast', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
return { | ||
'/just_listen': justListenEndpoint, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,7 @@ | ||
import ListenApiManager from '../../../edge-src/common/ListenApiManager.js' | ||
import JustListenDef from "../../../edge-src/api-definitions/JustListenDef"; | ||
|
||
export async function onRequestGet(context) { | ||
const mgr = new ListenApiManager(context) | ||
return await mgr.justListen((result) => ({ | ||
title: result.title, | ||
description: result.description, | ||
image: result.image, | ||
audio: result.audio, | ||
audio_length_sec: result.audio_length_sec, | ||
pub_date_ms: result.pub_date_ms, | ||
listennotes_url: result.listennotes_url, | ||
podcast: { | ||
title: result.podcast.title, | ||
publisher: result.podcast.publisher, | ||
image: result.podcast.image, | ||
listen_score: result.podcast.listen_score, | ||
listen_score_global_rank: result.podcast.listen_score_global_rank, | ||
listennotes_url: result.podcast.listennotes_url, | ||
}, | ||
})) | ||
const mgr = new ListenApiManager(context, JustListenDef) | ||
return await mgr.getResponse() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters