import { Storage } from '@google-cloud/storage' ; import { logger } from 'gcf-utils' ; /**
 * A simple library for parsing snippets.json file.
 *
 * Here is an example data of this file:
 * {
 *   "accessapproval_install_without_bom": {
 *     "title": "Install without bom",
 *     "description": null,
 *     "languages": {
 *       "MAVEN_POM": {
 *         "status": "IMPLEMENTED", "CONFLICT", or "UNTRACKED"
 *         "current_locations": [
 *           {
 *             "repository_path": "googleapis/java-accessapproval",
 *             "filename": "samples/install-without-bom/pom.xml",
 *             "commit": "a4b4143ee94d70a5486d96b9df2903fbba57e0d6",
 *             "branch": "master",
 *             "devsite_urls": ["https://example.com/usage.html"],
 *             "sample_browser_urls": ["https://example.com/docs/samples/region_tag"],
 *             "frozen_devsite_urls": ["https://example.com/usage.html"]
 *           }
 *         ]
 *       }
 *     }
 *   },
 * }
 */ const storage = new Storage ( ) ; export interface SnippetLocation { repository_path : string ; filename : string ; commit : string ; branch : string ; devsite_urls : string [ ] ; sample_browser_urls : string [ ] ; frozen_devsite_urls : string [ ] ; } export interface SnippetLanguage { status : string ; current_locations : Array < SnippetLocation > ; } export interface Snippet { title : string ; description : string ; languages : { [ index ] : SnippetLanguage ; } ; } export interface Snippets { [ index ] : Snippet ; } let cachedSnippets ; let cacheTimestamp = 0 ; export const invalidateCache = ( ) => { cacheTimestamp = 0 ; } ; export const getSnippets = async ( dataBucket ) : Promise < Snippets > => { const cacheExpiration = Math . floor ( Date . now ( ) / 1000 ) - 3600 ; if ( cacheTimestamp < cacheExpiration ) { logger . info ( `Fetching snippets json from ${ dataBucket }.` ) ; const snippets = await storage . bucket ( dataBucket ) . file ( 'snippets.json' ) . download ( ) ; cachedSnippets = JSON . parse ( snippets [ 0 ] . toString ( ) ) as Snippets ; cacheTimestamp = Math . floor ( Date . now ( ) / 1000 ) ; } else { logger . info ( 'Reusing cache for Snippets.' ) ; } return cachedSnippets ; } ;