11import { ApiPlugin } from '@/interfaces/ApiPlugin' ;
2+ import { Plugin } from '@/interfaces/Plugin' ;
23
34import { Endpoints } from '../../constants/Endpoints' ;
45
@@ -7,36 +8,75 @@ import { Endpoints } from '../../constants/Endpoints';
78 *
89 * @param themeId id of theme to fetch data for
910 */
10- const getNpmPluginData = async ( plugin : ApiPlugin ) => {
11+ const getNpmPluginData = async ( apiPlugin : ApiPlugin ) : Promise < Plugin | Partial < Plugin > > => {
1112 // check if plugin is valid
12- if ( ! plugin . id || typeof plugin . id !== 'string' ) {
13- throw new Error ( 'Invalid plugin ID provided' ) ;
13+ if ( ! apiPlugin . id || typeof apiPlugin . id !== 'string' ) {
14+ // This case should ideally not happen if data is validated upstream
15+ // but as a safeguard:
16+ console . error ( 'Invalid plugin ID provided to getNpmPluginData:' , apiPlugin ) ;
17+ return {
18+ id : apiPlugin . id || 'unknown-id' ,
19+ name : apiPlugin . name || apiPlugin . id || 'Plugin Name Unavailable' ,
20+ authorName : 'N/A' ,
21+ description : apiPlugin . description || 'No description available.' ,
22+ version : 'N/A' ,
23+ isDataMissing : true ,
24+ favoritesCount : apiPlugin . favoritesCount ,
25+ isFavorite : apiPlugin . isFavorite ?? false ,
26+ userId : apiPlugin . userId ,
27+ createdAt : apiPlugin . createdAt ,
28+ updatedAt : apiPlugin . updatedAt ,
29+ keywords : [ ] ,
30+ packageUrl : apiPlugin . packageUrl ,
31+ } ;
1432 }
1533
16- // fetch package data from npm registry
17- const response = await fetch ( `${ Endpoints . fetchNpmPlugins } /${ encodeURIComponent ( plugin . id ) } /latest` ) ;
18- const pluginData = await response . json ( ) ;
34+ try {
35+ // fetch package data from npm registry
36+ const response = await fetch ( `${ Endpoints . fetchNpmPlugins } /${ encodeURIComponent ( apiPlugin . id ) } /latest` ) ;
37+ if ( ! response . ok ) {
38+ throw new Error ( `NPM API request failed with status ${ response . status } ` ) ;
39+ }
40+ const pluginData = await response . json ( ) ;
41+ const authorImg = `https://avatars.githubusercontent.com/${ pluginData . author . name } ` ;
42+ const imageUrl = pluginData . pluginLogo || null ; // Assuming pluginLogo can be null/undefined
1943
20- const authorImg = `https://avatars.githubusercontent.com/${ pluginData . author . name } ` ;
21- const imageUrl = pluginData . pluginLogo || '' ;
22-
23- return {
24- authorImg,
25- authorName : pluginData . author . name ,
26- createdAt : plugin . createdAt ,
27- description : pluginData . description ,
28- favoritesCount : plugin . favoritesCount ,
29- github : pluginData . repository ,
30- id : plugin . id ,
31- imageUrl,
32- isFavorite : plugin . isFavorite ?? false ,
33- keywords : pluginData . keywords ,
34- name : pluginData . name ,
35- packageUrl : plugin . packageUrl ,
36- updatedAt : plugin . updatedAt ,
37- userId : plugin . userId ,
38- version : pluginData . version ,
39- } ;
44+ return {
45+ id : apiPlugin . id ,
46+ name : pluginData . name || apiPlugin . name || apiPlugin . id , // Use fetched name, fallback to apiPlugin name then id
47+ authorName : pluginData . author ?. name || 'N/A' ,
48+ description : pluginData . description || 'No description available.' ,
49+ version : pluginData . version || 'N/A' ,
50+ isDataMissing : false ,
51+ favoritesCount : apiPlugin . favoritesCount ,
52+ isFavorite : apiPlugin . isFavorite ?? false ,
53+ userId : apiPlugin . userId ,
54+ createdAt : apiPlugin . createdAt ,
55+ updatedAt : apiPlugin . updatedAt ,
56+ imageUrl,
57+ authorImg,
58+ github : pluginData . repository ?. url || pluginData . repository || null , // Handle repository being object or string
59+ keywords : pluginData . keywords || [ ] ,
60+ packageUrl : apiPlugin . packageUrl ,
61+ } ;
62+ } catch ( error ) {
63+ console . error ( `Error fetching plugin data from NPM for ${ apiPlugin . id } :` , error ) ;
64+ return {
65+ id : apiPlugin . id ,
66+ name : apiPlugin . name || apiPlugin . id || 'Plugin Name Unavailable' ,
67+ authorName : 'N/A' ,
68+ description : 'Plugin details are currently unavailable due to an external service issue.' ,
69+ version : 'N/A' ,
70+ isDataMissing : true ,
71+ favoritesCount : apiPlugin . favoritesCount ,
72+ isFavorite : apiPlugin . isFavorite ?? false ,
73+ userId : apiPlugin . userId ,
74+ createdAt : apiPlugin . createdAt ,
75+ updatedAt : apiPlugin . updatedAt ,
76+ keywords : [ ] ,
77+ packageUrl : apiPlugin . packageUrl ,
78+ } ;
79+ }
4080} ;
4181
4282export { getNpmPluginData } ;
0 commit comments