diff --git a/healthcare/fhir/createFhirResource.js b/healthcare/fhir/createFhirResource.js index 1457b1079f..92a796f672 100644 --- a/healthcare/fhir/createFhirResource.js +++ b/healthcare/fhir/createFhirResource.js @@ -30,6 +30,7 @@ function main( scopes: ['https://www.googleapis.com/auth/cloud-platform'], }), headers: {'Content-Type': 'application/fhir+json'}, + responseType: 'json', }); async function createFhirResource() { diff --git a/healthcare/fhir/deleteFhirResource.js b/healthcare/fhir/deleteFhirResource.js index 49a432aab2..52016b5df8 100644 --- a/healthcare/fhir/deleteFhirResource.js +++ b/healthcare/fhir/deleteFhirResource.js @@ -29,6 +29,7 @@ const main = ( auth: new google.auth.GoogleAuth({ scopes: ['https://www.googleapis.com/auth/cloud-platform'], }), + responseType: 'json', }); const deleteFhirResource = async () => { @@ -46,10 +47,14 @@ const main = ( // fails, the server returns a 200 OK HTTP status code. To check that the // resource was successfully deleted, search for or get the resource and // see if it exists. - await healthcare.projects.locations.datasets.fhirStores.fhir.delete( - request - ); - console.log('Deleted FHIR resource'); + try { + await healthcare.projects.locations.datasets.fhirStores.fhir.delete( + request + ); + console.log(`Deleted FHIR resource: ${resourceId}`); + } catch (error) { + console.error('Error deleting FHIR resource:', error.message || error); + } }; deleteFhirResource(); diff --git a/healthcare/fhir/deleteFhirResourcePurge.js b/healthcare/fhir/deleteFhirResourcePurge.js index 16601b4f8b..cf1023aacb 100644 --- a/healthcare/fhir/deleteFhirResourcePurge.js +++ b/healthcare/fhir/deleteFhirResourcePurge.js @@ -29,6 +29,7 @@ const main = ( auth: new google.auth.GoogleAuth({ scopes: ['https://www.googleapis.com/auth/cloud-platform'], }), + responseType: 'json', }); const deleteFhirResourcePurge = async () => { @@ -42,10 +43,14 @@ const main = ( const name = `projects/${projectId}/locations/${cloudRegion}/datasets/${datasetId}/fhirStores/${fhirStoreId}/fhir/${resourceType}/${resourceId}`; const request = {name}; - await healthcare.projects.locations.datasets.fhirStores.fhir.ResourcePurge( - request - ); - console.log('Deleted all historical versions of resource'); + try { + await healthcare.projects.locations.datasets.fhirStores.fhir.ResourcePurge( + request + ); + console.log(`Purged all historical versions of resource: ${resourceId}`); + } catch (error) { + console.error('Error purging FHIR resource:', error.message || error); + } }; deleteFhirResourcePurge(); diff --git a/healthcare/fhir/exportFhirResources.js b/healthcare/fhir/exportFhirResources.js index 1d538096f6..ded96655da 100644 --- a/healthcare/fhir/exportFhirResources.js +++ b/healthcare/fhir/exportFhirResources.js @@ -28,6 +28,7 @@ const main = ( auth: new google.auth.GoogleAuth({ scopes: ['https://www.googleapis.com/auth/cloud-platform'], }), + responseType: 'json', }); const sleep = ms => { return new Promise(resolve => setTimeout(resolve, ms)); @@ -51,23 +52,33 @@ const main = ( }, }; - const operation = - await healthcare.projects.locations.datasets.fhirStores.export(request); - const operationName = operation.data.name; + try { + const operation = + await healthcare.projects.locations.datasets.fhirStores.export(request); + const operationName = operation.data.name; - // Wait ten seconds for the LRO to finish - await sleep(10000); + let done = false; + let operationStatus; - // Check the LRO's status - const operationStatus = - await healthcare.projects.locations.datasets.operations.get({ - name: operationName, - }); + while (!done) { + console.log('Waiting for operation to complete...'); + await sleep(5000); - if (typeof operationStatus.data.metadata.counter !== 'undefined') { - console.log('Exported FHIR resources successfully'); - } else { - console.log('Export failed'); + operationStatus = + await healthcare.projects.locations.datasets.operations.get({ + name: operationName, + }); + + done = operationStatus.data.done; + } + + if (operationStatus.data.error) { + console.log('Export failed with error:', operationStatus.data.error); + } else { + console.log('Exported FHIR resources successfully'); + } + } catch (error) { + console.error('Error exporting FHIR resources:', error.message || error); } }; diff --git a/healthcare/fhir/getFhirResourceHistory.js b/healthcare/fhir/getFhirResourceHistory.js index 8dd56d6997..8a16badaf3 100644 --- a/healthcare/fhir/getFhirResourceHistory.js +++ b/healthcare/fhir/getFhirResourceHistory.js @@ -30,6 +30,7 @@ const main = ( auth: new google.auth.GoogleAuth({ scopes: ['https://www.googleapis.com/auth/cloud-platform'], }), + responseType: 'json', }); const getFhirResourceHistory = async () => { diff --git a/healthcare/fhir/getFhirStoreIamPolicy.js b/healthcare/fhir/getFhirStoreIamPolicy.js index e31be8c2fc..27058590bc 100644 --- a/healthcare/fhir/getFhirStoreIamPolicy.js +++ b/healthcare/fhir/getFhirStoreIamPolicy.js @@ -27,6 +27,7 @@ const main = ( auth: new google.auth.GoogleAuth({ scopes: ['https://www.googleapis.com/auth/cloud-platform'], }), + responseType: 'json', }); const getFhirStoreIamPolicy = async () => { @@ -38,14 +39,21 @@ const main = ( const resource_ = `projects/${projectId}/locations/${cloudRegion}/datasets/${datasetId}/fhirStores/${fhirStoreId}`; const request = {resource_}; - const fhirStore = - await healthcare.projects.locations.datasets.fhirStores.getIamPolicy( - request + try { + const fhirStore = + await healthcare.projects.locations.datasets.fhirStores.getIamPolicy( + request + ); + console.log( + 'Got FHIR store IAM policy:', + JSON.stringify(fhirStore.data, null, 2) ); - console.log( - 'Got FHIR store IAM policy:', - JSON.stringify(fhirStore.data, null, 2) - ); + } catch (error) { + console.error( + 'Error getting FHIR store IAM policy:', + error.message || error + ); + } }; getFhirStoreIamPolicy(); diff --git a/healthcare/fhir/listFhirResourceHistory.js b/healthcare/fhir/listFhirResourceHistory.js index acee5552e0..a4a207b740 100644 --- a/healthcare/fhir/listFhirResourceHistory.js +++ b/healthcare/fhir/listFhirResourceHistory.js @@ -29,6 +29,7 @@ const main = ( auth: new google.auth.GoogleAuth({ scopes: ['https://www.googleapis.com/auth/cloud-platform'], }), + responseType: 'json', }); const listFhirResourceHistory = async () => { diff --git a/healthcare/fhir/package.json b/healthcare/fhir/package.json index abb74033ed..683d4bec14 100644 --- a/healthcare/fhir/package.json +++ b/healthcare/fhir/package.json @@ -9,7 +9,7 @@ "node": ">=12.0.0" }, "scripts": { - "test": "c8 mocha -p -j 2 system-test/*.test.js --timeout=60000" + "test": "c8 mocha system-test/*.test.js --timeout=60000" }, "devDependencies": { "@google-cloud/pubsub": "^4.0.0", diff --git a/healthcare/fhir/patchFhirStore.js b/healthcare/fhir/patchFhirStore.js index c432731f8c..4b99a2267d 100644 --- a/healthcare/fhir/patchFhirStore.js +++ b/healthcare/fhir/patchFhirStore.js @@ -28,6 +28,7 @@ const main = ( auth: new google.auth.GoogleAuth({ scopes: ['https://www.googleapis.com/auth/cloud-platform'], }), + responseType: 'json', }); const patchFhirStore = async () => { @@ -50,10 +51,16 @@ const main = ( }, }; - await healthcare.projects.locations.datasets.fhirStores.patch(request); - console.log( - `Patched FHIR store ${fhirStoreId} with Cloud Pub/Sub topic ${pubsubTopic}` - ); + try { + const response = + await healthcare.projects.locations.datasets.fhirStores.patch(request); + console.log( + `Patched FHIR store ${fhirStoreId} with Cloud Pub/Sub topic ${pubsubTopic}` + ); + console.log(JSON.stringify(response.data, null, 2)); + } catch (error) { + console.error('Error patching FHIR store:', error.message || error); + } }; patchFhirStore(); diff --git a/healthcare/fhir/searchFhirResourcesPost.js b/healthcare/fhir/searchFhirResourcesPost.js index e7756d457f..2d6caa22bf 100644 --- a/healthcare/fhir/searchFhirResourcesPost.js +++ b/healthcare/fhir/searchFhirResourcesPost.js @@ -28,8 +28,6 @@ const main = ( const searchFhirResourcesPost = async () => { const auth = new GoogleAuth({ scopes: 'https://www.googleapis.com/auth/cloud-platform', - // Set application/fhir+json header because this is a POST request. - headers: {'Content-Type': 'application/fhir+json'}, }); // TODO(developer): uncomment these lines before running the sample // const cloudRegion = 'us-central1'; @@ -44,11 +42,27 @@ const main = ( // Patient with the last name "Smith", set resourceType to "Patient" and // specify the following params: // params = {'family:exact' : 'Smith'}; - const client = await auth.getClient(); - const response = await client.request({url, method: 'POST', params}); - const resources = response.data.entry; - console.log(`Resources found: ${resources.length}`); - console.log(JSON.stringify(resources, null, 2)); + + try { + const client = await auth.getClient(); + const response = await client.request({ + url, + method: 'POST', + headers: { + 'Content-Type': 'application/fhir+json', + }, + params, + responseType: 'json', + }); + const resources = response.data.entry || []; + console.log('Resources found: ' + resources.length); + console.log(JSON.stringify(resources, null, 2)); + } catch (error) { + console.error( + `Error searching ${resourceType} resources:`, + error.response ? error.response.data : error.message + ); + } }; searchFhirResourcesPost(); diff --git a/healthcare/fhir/setFhirStoreIamPolicy.js b/healthcare/fhir/setFhirStoreIamPolicy.js index 3290f05db4..f750968f53 100644 --- a/healthcare/fhir/setFhirStoreIamPolicy.js +++ b/healthcare/fhir/setFhirStoreIamPolicy.js @@ -46,7 +46,7 @@ const main = ( policy: { bindings: [ { - members: member, + members: [member], role: role, }, ], diff --git a/healthcare/fhir/system-test/fhir_resources.test.js b/healthcare/fhir/system-test/fhir_resources.test.js index 61c09595fa..a793663af6 100644 --- a/healthcare/fhir/system-test/fhir_resources.test.js +++ b/healthcare/fhir/system-test/fhir_resources.test.js @@ -163,7 +163,9 @@ it('should purge all historical versions of a FHIR resource', () => { {cwd} ); assert.strictEqual( - new RegExp('Deleted all historical versions of resource').test(output), + new RegExp( + `Purged all historical versions of resource: ${resourceId}` + ).test(output), true ); }); @@ -181,7 +183,10 @@ it('should delete a FHIR resource', () => { `node deleteFhirResource.js ${projectId} ${cloudRegion} ${datasetId} ${fhirStoreId} ${resourceType} ${resourceId}`, {cwd} ); - assert.strictEqual(new RegExp('Deleted FHIR resource').test(output), true); + assert.strictEqual( + new RegExp(`Deleted FHIR resource: ${resourceId}`).test(output), + true + ); // Clean up execSync(