|
| 1 | +import fs from 'fs/promises'; |
| 2 | +import path from 'path'; |
| 3 | + |
| 4 | +import express from 'express'; |
| 5 | +import yaml from 'js-yaml'; |
| 6 | + |
| 7 | +import Service from '../../archivist/services/service.js'; |
| 8 | + |
| 9 | +const METADATA_FILENAME = 'metadata.yml'; |
| 10 | +const PACKAGE_JSON_PATH = '../../../package.json'; |
| 11 | + |
| 12 | +/** |
| 13 | + * @swagger |
| 14 | + * tags: |
| 15 | + * name: Metadata |
| 16 | + * description: Collection metadata API |
| 17 | + * components: |
| 18 | + * schemas: |
| 19 | + * Metadata: |
| 20 | + * type: object |
| 21 | + * description: Collection metadata |
| 22 | + * properties: |
| 23 | + * id: |
| 24 | + * type: string |
| 25 | + * description: Unique identifier of the collection |
| 26 | + * name: |
| 27 | + * type: string |
| 28 | + * description: Display name of the collection |
| 29 | + * tagline: |
| 30 | + * type: string |
| 31 | + * description: Short description of the collection |
| 32 | + * description: |
| 33 | + * type: string |
| 34 | + * nullable: true |
| 35 | + * description: Detailed description of the collection |
| 36 | + * totalTerms: |
| 37 | + * type: integer |
| 38 | + * description: Total number of terms tracked in the collection |
| 39 | + * totalServices: |
| 40 | + * type: integer |
| 41 | + * description: Total number of services tracked in the collection |
| 42 | + * engineVersion: |
| 43 | + * type: string |
| 44 | + * description: Version of the Open Terms Archive engine in SemVer format (MAJOR.MINOR.PATCH) |
| 45 | + * dataset: |
| 46 | + * type: string |
| 47 | + * format: uri |
| 48 | + * description: URL to the dataset releases |
| 49 | + * declarations: |
| 50 | + * type: string |
| 51 | + * format: uri |
| 52 | + * description: URL to the declarations repository |
| 53 | + * versions: |
| 54 | + * type: string |
| 55 | + * format: uri |
| 56 | + * description: URL to the versions repository |
| 57 | + * snapshots: |
| 58 | + * type: string |
| 59 | + * format: uri |
| 60 | + * description: URL to the snapshots repository |
| 61 | + * logo: |
| 62 | + * type: string |
| 63 | + * format: uri |
| 64 | + * nullable: true |
| 65 | + * description: URL to the collection logo |
| 66 | + * languages: |
| 67 | + * type: array |
| 68 | + * items: |
| 69 | + * type: string |
| 70 | + * description: List of ISO 639 language codes representing languages allowed by the collection |
| 71 | + * jurisdictions: |
| 72 | + * type: array |
| 73 | + * items: |
| 74 | + * type: string |
| 75 | + * description: List of ISO 3166-2 country codes representing jurisdictions covered by the collection |
| 76 | + * trackingPeriods: |
| 77 | + * type: array |
| 78 | + * items: |
| 79 | + * type: object |
| 80 | + * properties: |
| 81 | + * startDate: |
| 82 | + * type: string |
| 83 | + * format: date |
| 84 | + * description: The date when tracking started for this period |
| 85 | + * schedule: |
| 86 | + * type: string |
| 87 | + * description: A cron expression defining when terms are tracked (e.g. "0 0 * * *" for daily at midnight) |
| 88 | + * serverLocation: |
| 89 | + * type: string |
| 90 | + * description: The geographic location of the server used for tracking |
| 91 | + * endDate: |
| 92 | + * type: string |
| 93 | + * format: date |
| 94 | + * description: The date when tracking ended for this period |
| 95 | + * governance: |
| 96 | + * type: object |
| 97 | + * properties: |
| 98 | + * hosts: |
| 99 | + * type: array |
| 100 | + * items: |
| 101 | + * $ref: '#/components/schemas/Organization' |
| 102 | + * administrators: |
| 103 | + * type: array |
| 104 | + * items: |
| 105 | + * $ref: '#/components/schemas/Organization' |
| 106 | + * curators: |
| 107 | + * type: array |
| 108 | + * items: |
| 109 | + * $ref: '#/components/schemas/Organization' |
| 110 | + * maintainers: |
| 111 | + * type: array |
| 112 | + * items: |
| 113 | + * $ref: '#/components/schemas/Organization' |
| 114 | + * sponsors: |
| 115 | + * type: array |
| 116 | + * items: |
| 117 | + * $ref: '#/components/schemas/Organization' |
| 118 | + * Organization: |
| 119 | + * type: object |
| 120 | + * properties: |
| 121 | + * name: |
| 122 | + * type: string |
| 123 | + * description: Name of the organization |
| 124 | + * url: |
| 125 | + * type: string |
| 126 | + * format: uri |
| 127 | + * description: URL to the organization's website |
| 128 | + * logo: |
| 129 | + * type: string |
| 130 | + * format: uri |
| 131 | + * description: URL to the organization's logo |
| 132 | + */ |
| 133 | +export default async function metadataRouter(collectionPath, services) { |
| 134 | + const router = express.Router(); |
| 135 | + |
| 136 | + const STATIC_METADATA = yaml.load(await fs.readFile(path.join(collectionPath, METADATA_FILENAME), 'utf8')); |
| 137 | + const { version: engineVersion } = JSON.parse(await fs.readFile(new URL(PACKAGE_JSON_PATH, import.meta.url))); |
| 138 | + |
| 139 | + /** |
| 140 | + * @swagger |
| 141 | + * /metadata: |
| 142 | + * get: |
| 143 | + * summary: Get collection metadata |
| 144 | + * tags: [Metadata] |
| 145 | + * produces: |
| 146 | + * - application/json |
| 147 | + * responses: |
| 148 | + * 200: |
| 149 | + * description: Collection metadata |
| 150 | + */ |
| 151 | + router.get('/metadata', (req, res) => { |
| 152 | + const dynamicMetadata = { |
| 153 | + totalServices: Object.keys(services).length, |
| 154 | + totalTerms: Service.getNumberOfTerms(services), |
| 155 | + engineVersion, |
| 156 | + }; |
| 157 | + |
| 158 | + res.json({ |
| 159 | + ...STATIC_METADATA, |
| 160 | + ...dynamicMetadata, |
| 161 | + }); |
| 162 | + }); |
| 163 | + |
| 164 | + return router; |
| 165 | +} |
0 commit comments