@@ -19,22 +19,30 @@ import { TemplatesResult } from "../utils/types";
1919
2020/** Get the titles of the templates from the JSON file.
2121 * @param {string } URL The URL of the JSON file
22+ * @param {string } branch The branch of the repository
2223 * @throws {Error } If the URL is not defined in the environment
2324 * @async
2425 * @example
25- * const titles = await getTemplateTitles("https://example.com/templates.json");
26+ * const titles = await getTemplateTitles("https://example.com/templates.json", "main" );
2627 * console.log("Template titles:", titles);
2728 * @returns {Promise<TemplatesResult> } The titles of the templates with their images
2829 */
29- export async function getTemplateTitles ( URL : string ) : Promise < TemplatesResult > {
30+ export async function getTemplateTitles (
31+ URL : string ,
32+ branch : string ,
33+ ) : Promise < TemplatesResult > {
3034 if ( ! URL || URL === "" ) {
3135 throw new TemplateFetchError (
3236 "TEMPLATES_JSON_URL is not defined in the environment." ,
3337 ) ;
3438 }
3539
40+ if ( ! branch || branch === "" ) {
41+ throw new TemplateFetchError ( "Branch is not defined." ) ;
42+ }
43+
3644 try {
37- const response = await axios . get ( URL ) ;
45+ const response = await axios . get ( ` ${ URL } / ${ branch } /cookiecutter.json` ) ;
3846
3947 const result = Object . entries ( response . data . templates ) . map (
4048 // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -52,3 +60,38 @@ export async function getTemplateTitles(URL: string): Promise<TemplatesResult> {
5260 ) ;
5361 }
5462}
63+
64+ /**
65+ * Get the branches of the template repository.
66+ * @param {string } repoUrl The URL of the GitHub repository
67+ * @throws {Error } If the URL is not defined or if there's an error fetching the branches
68+ * @async
69+ * @example
70+ * const branches = await getTemplateBranches("https://github.com/username/repository");
71+ * console.log("Branches:", branches);
72+ * @returns {Promise<string[]> } The branches of the repository
73+ */
74+ export async function getTemplateBranches ( repoUrl : string ) : Promise < string [ ] > {
75+ if ( ! repoUrl || repoUrl === "" ) {
76+ throw new TemplateFetchError ( "Repository URL is not defined." ) ;
77+ }
78+
79+ const apiUrl = `${ repoUrl } /branches/all` ;
80+
81+ try {
82+ const response = await axios . get ( apiUrl , {
83+ headers : {
84+ Accept : "application/json" ,
85+ } ,
86+ } ) ;
87+ return response . data . payload . branches . map (
88+ ( branch : { name : string } ) => branch . name ,
89+ ) ;
90+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
91+ } catch ( error : any ) {
92+ throw new TemplateFetchError (
93+ `Error fetching template branches: ${ error . message } ` ,
94+ error ,
95+ ) ;
96+ }
97+ }
0 commit comments