import { RequestOptions , RESTDataSource } from 'apollo-datasource-rest' ; import { CACHE_TTL } from 'common/enums' ; import { environment } from 'common/environment' ; import logger from 'common/logger' ; export class OpenSeaService extends RESTDataSource { apiKey ? : string | undefined ; constructor ( ) { super ( ) ; this . baseURL = environment . openseaAPIBase ; this . apiKey = environment . openseaAPIKey ; } willSendRequest ( request ) { if ( this . apiKey ) { request . headers . set ( 'X-API-KEY' , this . apiKey ) ; } } async getAssets ( { owner , asset_contract_address = environment . nftContractAddress , } ) { try { const data = await this . get ( 'assets' , { owner , asset_contract_address , order_direction : 'desc' , offset : 0 , limit : 20 , } , { cacheOptions : { ttl : CACHE_TTL . MEDIUM , } , } ) ; logger . info ( `fetched ${ data . assets ?. length } assets for owner: "${ owner }".` ) ; return data . assets ; } catch ( err ) { logger . error ( err ) ; console . error ( new Date ( ) , 'ERROR:' , err ) ; return [ ] ; } } }