UNPKG

@medplum/definitions

Version:

Medplum Data Definitions

8 lines (7 loc) 5.21 kB
{ "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": "yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,mCAAAE,EAAA,sCAAAC,EAAA,6BAAAC,EAAA,eAAAC,EAAA,0BAAAC,EAAA,aAAAC,EAAA,kBAAAC,IAAA,eAAAC,EAAAT,GAGA,IAAAU,EAA6B,cAC7BC,EAA2B,mBAC3BC,EAAyB,4BACzBC,EAAiC,qBACjCC,EAA8B,oBAP9BC,EAAA,GASMC,EAAqB,OAAO,OAAO,CAAC,CAAC,EAEpC,SAAST,EAASU,EAAuB,CAC9C,IAAMC,KAAe,WAAQb,EAAW,EAAGY,CAAQ,EACnD,OAAO,KAAK,SAAM,gBAAaC,EAAc,MAAM,CAAC,CACtD,CAEA,eAAsBV,EAAcS,EAAgC,CAClE,IAAMC,KAAe,WAAQb,EAAW,EAAGY,CAAQ,EAC7CE,EAAO,QAAM,YAASD,EAAc,MAAM,EAChD,OAAO,KAAK,MAAMC,CAAI,CACxB,CAEA,IAAIC,EAEG,SAASf,GAAqB,CACnC,GAAIe,EACF,OAAOA,EAGT,IAAMC,EAAaC,EAAc,EAM3BC,EAAgB,CAAC,KAAM,MAAO,SAAU,QAAQ,EACtD,QAAWC,KAAgBD,EAAe,CACxC,IAAME,KAAW,WAAQJ,EAAYG,CAAY,EAC3CE,KAAW,WAAQD,EAAU,MAAM,EACzC,MAAI,cAAWC,CAAQ,EACrB,OAAAN,EAAgBK,EACTA,CAEX,CACA,MAAM,IAAI,MAAM,yBAAyB,CAC3C,CAEA,SAASH,GAAwB,CAC/B,GAAI,OAAO,UAAc,IACvB,SAAO,WAAQ,SAAS,EACnB,GAAIP,EAAY,IACrB,SAAO,cAAQ,cAAQ,iBAAcA,EAAY,GAAG,CAAC,CAAC,EAEtD,MAAM,IAAI,MAAM,yBAAyB,CAE7C,CAMO,IAAMb,EAAgC,CAC3C,iCACA,yCACA,uCACF,EAEaC,EAAoC,CAC/C,8BACA,kCACA,gCACA,8BACF,EAEaC,EAA2B,CACtC,yBACA,8BACA,yBACA,iCACA,0CACF,EAEO,SAASE,EACdqB,EACAC,EACAC,EACG,CACH,IAAMC,EAASvB,EAASoB,CAAI,EAC5B,QAAWI,KAASD,EAAO,OAASd,EAAO,CACzC,IAAMgB,EAAWD,EAAM,SACvB,GAAIC,GAAU,eAAiBJ,GAAgBI,EAAS,MAAQH,EAC9D,OAAOG,CAEX,CACA,MAAM,IAAI,MAAM,gCAAgCJ,CAAY,QAAQC,CAAG,EAAE,CAC3E", "names": ["index_exports", "__export", "SEARCH_PARAMETER_BUNDLE_FILES", "STRUCTURE_DEFINITION_BUNDLE_FILES", "TERMINOLOGY_BUNDLE_FILES", "getDataDir", "getDefinitionResource", "readJson", "readJsonAsync", "__toCommonJS", "import_fs", "import_node_fs", "import_promises", "import_node_path", "import_node_url", "import_meta", "EMPTY", "filename", "filenamePath", "data", "cachedDataDir", "currentDir", "getCurrentDir", "relativePaths", "relativePath", "fullPath", "fhirPath", "file", "resourceType", "url", "bundle", "entry", "resource"] }