import { PluginData , RawPluginData } from '../types.js' ; import { fetchAsync } from './fetch-async.js' ; export async function fetchLivePluginsDataAsync ( ) { const data = await fetchRawPluginsDataAsync ( ) ; return parseRawPluginsData ( data ) ; } async function fetchRawPluginsDataAsync ( ) { let result = [ ] ; let url = 'https://www.figma.com/api/plugins/browse?sort_by=popular&sort_order=desc&resource_type=plugins&page_size=50' ; while ( typeof url !== 'undefined' ) { const response = await fetchAsync ( url ) ; const json = await response . json ( ) ; result = result . concat ( json . meta . plugins ) ; url = json . pagination . next_page ; } return deduplicate ( result ) ; } function deduplicate ( data ) { const result = [ ] ; const ids = { } ; for ( const item of data ) { const id = item . id ; if ( ids [ id ] !== true ) { result . push ( item ) ; ids [ id ] = true ; } } return result ; } function parseRawPluginsData ( data ) { const plugins = [ ] ; for ( const item of data ) { const metaData = Object . values ( item . versions ) [ 0 ] ; plugins . push ( { description : metaData . description , id : item . id , installCount : item . install_count , lastUpdateDate : metaData . created_at , likeCount : item . like_count , name : metaData . name , publisherHandle : item . publisher . profile_handle , publisherId : item . publisher . id , publisherName : item . publisher . name , viewCount : item . view_count } ) ; } return plugins . sort ( function ( a , b ) { return a . name . localeCompare ( b . name ) ; } ) ; }