@@ -19,22 +19,30 @@ import { TemplatesResult } from "../utils/types";
19
19
20
20
/** Get the titles of the templates from the JSON file.
21
21
* @param {string } URL The URL of the JSON file
22
+ * @param {string } branch The branch of the repository
22
23
* @throws {Error } If the URL is not defined in the environment
23
24
* @async
24
25
* @example
25
- * const titles = await getTemplateTitles("https://example.com/templates.json");
26
+ * const titles = await getTemplateTitles("https://example.com/templates.json", "main" );
26
27
* console.log("Template titles:", titles);
27
28
* @returns {Promise<TemplatesResult> } The titles of the templates with their images
28
29
*/
29
- export async function getTemplateTitles ( URL : string ) : Promise < TemplatesResult > {
30
+ export async function getTemplateTitles (
31
+ URL : string ,
32
+ branch : string ,
33
+ ) : Promise < TemplatesResult > {
30
34
if ( ! URL || URL === "" ) {
31
35
throw new TemplateFetchError (
32
36
"TEMPLATES_JSON_URL is not defined in the environment." ,
33
37
) ;
34
38
}
35
39
40
+ if ( ! branch || branch === "" ) {
41
+ throw new TemplateFetchError ( "Branch is not defined." ) ;
42
+ }
43
+
36
44
try {
37
- const response = await axios . get ( URL ) ;
45
+ const response = await axios . get ( ` ${ URL } / ${ branch } /cookiecutter.json` ) ;
38
46
39
47
const result = Object . entries ( response . data . templates ) . map (
40
48
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -52,3 +60,38 @@ export async function getTemplateTitles(URL: string): Promise<TemplatesResult> {
52
60
) ;
53
61
}
54
62
}
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