@medplum/definitions
Version:
Medplum Data Definitions
8 lines (7 loc) • 5.08 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/index.ts"],
"sourcesContent": ["// SPDX-FileCopyrightText: Copyright Orangebot, Inc. and Medplum contributors\n// SPDX-License-Identifier: Apache-2.0\nimport type { Bundle, Resource } from '@medplum/fhirtypes';\nimport { readFileSync } from 'fs';\nimport { existsSync } from 'node:fs';\nimport { readFile } from 'node:fs/promises';\nimport { dirname, resolve } from 'node:path';\nimport { fileURLToPath } from 'node:url';\n\nconst EMPTY: readonly [] = Object.freeze([]);\n\nexport function readJson(filename: string): any {\n const filenamePath = resolve(getDataDir(), filename);\n return JSON.parse(readFileSync(filenamePath, 'utf8'));\n}\n\nexport async function readJsonAsync(filename: string): Promise<any> {\n const filenamePath = resolve(getDataDir(), filename);\n const data = await readFile(filenamePath, 'utf8');\n return JSON.parse(data);\n}\n\nlet cachedDataDir: string | undefined = undefined;\n\nexport function getDataDir(): string {\n if (cachedDataDir) {\n return cachedDataDir;\n }\n\n const currentDir = getCurrentDir();\n\n // Need to handle the following cases:\n // v4 and earlier: `index.js` in `dist/`, data in `dist/fhir/`\n // v5.0.0: data in `index.js` in `dist/cjs/` and `dist/esm/`, data in `/dist/cjs/fhir/` and `/dist/esm/fhir/`\n // v5.0.1 and after: `index.js` in `dist/cjs/` and `dist/esm/`, data back in `/dist/fhir/`\n const relativePaths = ['./', '../', './cjs/', './esm/'];\n for (const relativePath of relativePaths) {\n const fullPath = resolve(currentDir, relativePath);\n const fhirPath = resolve(fullPath, 'fhir');\n if (existsSync(fhirPath)) {\n cachedDataDir = fullPath;\n return fullPath;\n }\n }\n throw new Error('No data directory found');\n}\n\nfunction getCurrentDir(): string {\n if (typeof __dirname !== 'undefined') {\n return resolve(__dirname);\n } else if (import.meta.url) {\n return resolve(dirname(fileURLToPath(import.meta.url)));\n } else {\n throw new Error('No data directory found');\n }\n}\n\n/**\n * The list of all known search parameter definition bundle file paths relative to the\n * `@medplum/definitions` package. Typically used in conjunction with `readJson`.\n */\nexport const SEARCH_PARAMETER_BUNDLE_FILES = [\n 'fhir/r4/search-parameters.json',\n 'fhir/r4/search-parameters-medplum.json',\n 'fhir/r4/search-parameters-uscore.json',\n];\n\nexport const STRUCTURE_DEFINITION_BUNDLE_FILES = [\n 'fhir/r4/profiles-types.json',\n 'fhir/r4/profiles-resources.json',\n 'fhir/r4/profiles-medplum.json',\n 'fhir/r4/profiles-others.json',\n];\n\nexport const TERMINOLOGY_BUNDLE_FILES = [\n 'fhir/r4/v2-tables.json',\n 'fhir/r4/v3-codesystems.json',\n 'fhir/r4/valuesets.json',\n 'fhir/r4/valuesets-medplum.json',\n 'fhir/r4/valuesets-medplum-generated.json',\n];\n\nexport function getDefinitionResource<T extends Resource & { url?: string }>(\n file: string,\n resourceType: T['resourceType'],\n url: string\n): T {\n const bundle = readJson(file) as Bundle;\n for (const entry of bundle.entry ?? EMPTY) {\n const resource = entry.resource as Resource & { url: string };\n if (resource?.resourceType === resourceType && resource.url === url) {\n return resource as T;\n }\n }\n throw new Error(`Missing definition resource: ${resourceType}?url=${url}`);\n}\n"],
"mappings": "AAGA,OAAS,gBAAAA,MAAoB,KAC7B,OAAS,cAAAC,MAAkB,UAC3B,OAAS,YAAAC,MAAgB,mBACzB,OAAS,WAAAC,EAAS,WAAAC,MAAe,YACjC,OAAS,iBAAAC,MAAqB,WAE9B,IAAMC,EAAqB,OAAO,OAAO,CAAC,CAAC,EAEpC,SAASC,EAASC,EAAuB,CAC9C,IAAMC,EAAeL,EAAQM,EAAW,EAAGF,CAAQ,EACnD,OAAO,KAAK,MAAMR,EAAaS,EAAc,MAAM,CAAC,CACtD,CAEA,eAAsBE,EAAcH,EAAgC,CAClE,IAAMC,EAAeL,EAAQM,EAAW,EAAGF,CAAQ,EAC7CI,EAAO,MAAMV,EAASO,EAAc,MAAM,EAChD,OAAO,KAAK,MAAMG,CAAI,CACxB,CAEA,IAAIC,EAEG,SAASH,GAAqB,CACnC,GAAIG,EACF,OAAOA,EAGT,IAAMC,EAAaC,EAAc,EAM3BC,EAAgB,CAAC,KAAM,MAAO,SAAU,QAAQ,EACtD,QAAWC,KAAgBD,EAAe,CACxC,IAAME,EAAWd,EAAQU,EAAYG,CAAY,EAC3CE,EAAWf,EAAQc,EAAU,MAAM,EACzC,GAAIjB,EAAWkB,CAAQ,EACrB,OAAAN,EAAgBK,EACTA,CAEX,CACA,MAAM,IAAI,MAAM,yBAAyB,CAC3C,CAEA,SAASH,GAAwB,CAC/B,GAAI,OAAO,UAAc,IACvB,OAAOX,EAAQ,SAAS,EACnB,GAAI,YAAY,IACrB,OAAOA,EAAQD,EAAQE,EAAc,YAAY,GAAG,CAAC,CAAC,EAEtD,MAAM,IAAI,MAAM,yBAAyB,CAE7C,CAMO,IAAMe,EAAgC,CAC3C,iCACA,yCACA,uCACF,EAEaC,EAAoC,CAC/C,8BACA,kCACA,gCACA,8BACF,EAEaC,EAA2B,CACtC,yBACA,8BACA,yBACA,iCACA,0CACF,EAEO,SAASC,EACdC,EACAC,EACAC,EACG,CACH,IAAMC,EAASpB,EAASiB,CAAI,EAC5B,QAAWI,KAASD,EAAO,OAASrB,EAAO,CACzC,IAAMuB,EAAWD,EAAM,SACvB,GAAIC,GAAU,eAAiBJ,GAAgBI,EAAS,MAAQH,EAC9D,OAAOG,CAEX,CACA,MAAM,IAAI,MAAM,gCAAgCJ,CAAY,QAAQC,CAAG,EAAE,CAC3E",
"names": ["readFileSync", "existsSync", "readFile", "dirname", "resolve", "fileURLToPath", "EMPTY", "readJson", "filename", "filenamePath", "getDataDir", "readJsonAsync", "data", "cachedDataDir", "currentDir", "getCurrentDir", "relativePaths", "relativePath", "fullPath", "fhirPath", "SEARCH_PARAMETER_BUNDLE_FILES", "STRUCTURE_DEFINITION_BUNDLE_FILES", "TERMINOLOGY_BUNDLE_FILES", "getDefinitionResource", "file", "resourceType", "url", "bundle", "entry", "resource"]
}