From 0b1ae0a368b43931399a5c7cd2f07840c809ae33 Mon Sep 17 00:00:00 2001 From: Wanpeng Li Date: Mon, 17 Mar 2025 16:17:55 +0800 Subject: [PATCH 1/8] add test script --- .scripts/automation_generate.sh | 43 ++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/.scripts/automation_generate.sh b/.scripts/automation_generate.sh index 14072c7bc41f..8465b4be54f8 100644 --- a/.scripts/automation_generate.sh +++ b/.scripts/automation_generate.sh @@ -1,2 +1,43 @@ #!/usr/bin/env bash -code-gen-pipeline --inputJsonPath=$1 --outputJsonPath=$2 --use=@autorest/typescript@^6.0.12 --typespecEmitter=@azure-tools/typespec-ts + +cd .. + +file_path="azure-rest-api-specs/.js/branch.txt" +# Check if the file exists +if [ -f "$file_path" ]; then + # Load the content of the file into a variable + branch=$(cat "$file_path") + echo "get branch $branch" +else + echo "Branch file does not exist." +fi + +if [ -d "azure-sdk-tools" ]; then + echo "Delete folder azure-sdk-tools" + rm -rf azure-sdk-tools +else + echo "azure-sdk-tools folder does not exist." +fi + +echo 'clone azure-sdk-tools' +git clone https://github.com/Azure/azure-sdk-tools/ + +cd azure-sdk-tools +if [ -z "$branch" ]; then + echo 'branch is empty' +else + git checkout -b test $branch + echo git checkout -b test $branch +fi + +echo '-------------- git status start' +git status +echo '-------------- git status end' + +cd tools/js-sdk-release-tools +pnpm install +pnpm run build +cd ../../.. + +cd azure-sdk-for-js +node ../azure-sdk-tools/tools/js-sdk-release-tools/dist/autoGenerateInPipeline.js --inputJsonPath=$1 --outputJsonPath=$2 --use=@autorest/typescript@^6.0.12 --typespecEmitter=@azure-tools/typespec-ts From b2d0a873bf5cfa07ad88d3bbc457ab734bb7f409 Mon Sep 17 00:00:00 2001 From: Wanpeng Li Date: Mon, 17 Mar 2025 16:28:40 +0800 Subject: [PATCH 2/8] pnpm -> npm --- .scripts/automation_generate.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.scripts/automation_generate.sh b/.scripts/automation_generate.sh index 8465b4be54f8..89362e9dc6a4 100644 --- a/.scripts/automation_generate.sh +++ b/.scripts/automation_generate.sh @@ -35,8 +35,8 @@ git status echo '-------------- git status end' cd tools/js-sdk-release-tools -pnpm install -pnpm run build +npm install +npm run build cd ../../.. cd azure-sdk-for-js From 34aa5ed800f29f8d0d9913e69bad6248adedc7f1 Mon Sep 17 00:00:00 2001 From: Wanpeng Li Date: Thu, 8 May 2025 10:35:45 +0800 Subject: [PATCH 3/8] Update automation_init.sh --- .scripts/automation_init.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.scripts/automation_init.sh b/.scripts/automation_init.sh index 915b8c6a2080..800eed57710b 100644 --- a/.scripts/automation_init.sh +++ b/.scripts/automation_init.sh @@ -1,4 +1,4 @@ #!/usr/bin/env bash -npm install -g @azure-tools/typespec-client-generator-cli@0.14.1 +npm install -g @azure-tools/typespec-client-generator-cli@0.21.0 npm install -g @microsoft/rush@5.92.0 npm install -g @azure-tools/js-sdk-release-tools From 8c380cb69b5dec1d4fcc2a6945e7126ba4b00d30 Mon Sep 17 00:00:00 2001 From: ZiWei Chen <98569699+kazrael2119@users.noreply.github.com> Date: Tue, 19 Aug 2025 17:24:24 +0800 Subject: [PATCH 4/8] Update automation_init.sh --- .scripts/automation_init.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/.scripts/automation_init.sh b/.scripts/automation_init.sh index 28f7fc3c3aa3..afd07ef589f1 100644 --- a/.scripts/automation_init.sh +++ b/.scripts/automation_init.sh @@ -2,3 +2,4 @@ npm install -g @azure-tools/typespec-client-generator-cli@0.21.0 npm install -g pnpm npm install -g @azure-tools/js-sdk-release-tools +npm install -g eslint From 2ea21306e479f09b591e0e3fd60eba22001191f6 Mon Sep 17 00:00:00 2001 From: ZiWei Chen <98569699+kazrael2119@users.noreply.github.com> Date: Tue, 19 Aug 2025 17:55:24 +0800 Subject: [PATCH 5/8] Update automation_init.sh --- .scripts/automation_init.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/.scripts/automation_init.sh b/.scripts/automation_init.sh index afd07ef589f1..28f7fc3c3aa3 100644 --- a/.scripts/automation_init.sh +++ b/.scripts/automation_init.sh @@ -2,4 +2,3 @@ npm install -g @azure-tools/typespec-client-generator-cli@0.21.0 npm install -g pnpm npm install -g @azure-tools/js-sdk-release-tools -npm install -g eslint From 772bfcb22abab08117c4633d15d6c39f3d5a9909 Mon Sep 17 00:00:00 2001 From: ZiWei Chen <98569699+kazrael2119@users.noreply.github.com> Date: Wed, 20 Aug 2025 11:24:25 +0800 Subject: [PATCH 6/8] Update automation_init.sh --- .scripts/automation_init.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/.scripts/automation_init.sh b/.scripts/automation_init.sh index 28f7fc3c3aa3..afd07ef589f1 100644 --- a/.scripts/automation_init.sh +++ b/.scripts/automation_init.sh @@ -2,3 +2,4 @@ npm install -g @azure-tools/typespec-client-generator-cli@0.21.0 npm install -g pnpm npm install -g @azure-tools/js-sdk-release-tools +npm install -g eslint From 270aa0206456c894607805471256ba096d208ad8 Mon Sep 17 00:00:00 2001 From: ZiWei Chen <98569699+kazrael2119@users.noreply.github.com> Date: Fri, 22 Aug 2025 13:39:21 +0800 Subject: [PATCH 7/8] Update automation_init.sh --- .scripts/automation_init.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/.scripts/automation_init.sh b/.scripts/automation_init.sh index afd07ef589f1..28f7fc3c3aa3 100644 --- a/.scripts/automation_init.sh +++ b/.scripts/automation_init.sh @@ -2,4 +2,3 @@ npm install -g @azure-tools/typespec-client-generator-cli@0.21.0 npm install -g pnpm npm install -g @azure-tools/js-sdk-release-tools -npm install -g eslint From 690b64d04952a19cc1c5c430ecbfc9ba02e46569 Mon Sep 17 00:00:00 2001 From: azure-sdk Date: Fri, 24 Oct 2025 07:20:57 +0000 Subject: [PATCH 8/8] Configurations: 'specification/healthdataaiservices/HealthDataAIServices.DeidServices/tspconfig.yaml', API Version: 2025-07-15-preview, SDK Release Type: beta, and CommitSHA: 'e5c84e70cba98f4666825025e648f4184e6504a1' in SpecRepo: 'https://github.com/Azure/azure-rest-api-specs' Pipeline run: https://dev.azure.com/azure-sdk/internal/_build/results?buildId=5494781 Refer to https://eng.ms/docs/products/azure-developer-experience/develop/sdk-release/sdk-release-prerequisites to prepare for SDK release. --- pnpm-lock.yaml | 27 +- sdk/healthdataaiservices/ci.mgmt.yml | 6 + .../health-deidentification-rest/CHANGELOG.md | 5 + .../health-deidentification-rest/README.md | 217 ++----- .../metadata.json | 39 +- .../health-deidentification-rest/package.json | 81 ++- .../health-deidentification-api-node.api.md | 91 +++ ...health-deidentification-models-node.api.md | 145 +++++ .../health-deidentification-node.api.md | 613 ++---------------- .../health-deidentification-rest/sample.env | 6 +- .../samples-dev/createJob.ts | 2 +- .../samples-dev/helloWorld.ts | 6 +- .../samples-dev/listCompletedFiles.ts | 7 +- .../src/api/deidentificationContext.ts | 61 ++ .../src/api/index.ts | 26 + .../src/api/operations.ts | 395 +++++++++++ .../src/api/options.ts | 56 ++ .../src/clientDefinitions.ts | 98 --- .../src/deidentificationClient.ts | 164 +++-- .../health-deidentification-rest/src/index.ts | 52 +- .../src/isUnexpected.ts | 158 ----- .../src/models.ts | 140 ---- .../src/models/index.ts | 25 + .../src/models/models.ts | 514 +++++++++++++++ .../src/outputModels.ts | 199 ------ .../src/paginateHelper.ts | 267 -------- .../src/parameters.ts | 121 ---- .../src/pollingHelper.ts | 210 ------ .../src/responses.ts | 193 ------ .../src/restorePollerHelpers.ts | 150 +++++ .../src/static-helpers/pagingHelpers.ts | 242 +++++++ .../src/static-helpers/pollingHelpers.ts | 127 ++++ .../src/static-helpers/urlTemplate.ts | 210 ++++++ .../tsconfig.json | 12 +- .../tsp-location.yaml | 4 +- 35 files changed, 2428 insertions(+), 2241 deletions(-) create mode 100644 sdk/healthdataaiservices/health-deidentification-rest/review/health-deidentification-api-node.api.md create mode 100644 sdk/healthdataaiservices/health-deidentification-rest/review/health-deidentification-models-node.api.md create mode 100644 sdk/healthdataaiservices/health-deidentification-rest/src/api/deidentificationContext.ts create mode 100644 sdk/healthdataaiservices/health-deidentification-rest/src/api/index.ts create mode 100644 sdk/healthdataaiservices/health-deidentification-rest/src/api/operations.ts create mode 100644 sdk/healthdataaiservices/health-deidentification-rest/src/api/options.ts delete mode 100644 sdk/healthdataaiservices/health-deidentification-rest/src/clientDefinitions.ts delete mode 100644 sdk/healthdataaiservices/health-deidentification-rest/src/isUnexpected.ts delete mode 100644 sdk/healthdataaiservices/health-deidentification-rest/src/models.ts create mode 100644 sdk/healthdataaiservices/health-deidentification-rest/src/models/index.ts create mode 100644 sdk/healthdataaiservices/health-deidentification-rest/src/models/models.ts delete mode 100644 sdk/healthdataaiservices/health-deidentification-rest/src/outputModels.ts delete mode 100644 sdk/healthdataaiservices/health-deidentification-rest/src/paginateHelper.ts delete mode 100644 sdk/healthdataaiservices/health-deidentification-rest/src/parameters.ts delete mode 100644 sdk/healthdataaiservices/health-deidentification-rest/src/pollingHelper.ts delete mode 100644 sdk/healthdataaiservices/health-deidentification-rest/src/responses.ts create mode 100644 sdk/healthdataaiservices/health-deidentification-rest/src/restorePollerHelpers.ts create mode 100644 sdk/healthdataaiservices/health-deidentification-rest/src/static-helpers/pagingHelpers.ts create mode 100644 sdk/healthdataaiservices/health-deidentification-rest/src/static-helpers/pollingHelpers.ts create mode 100644 sdk/healthdataaiservices/health-deidentification-rest/src/static-helpers/urlTemplate.ts diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 445586abf9f2..ea841235111b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -14201,44 +14201,32 @@ importers: version: 2.8.1 devDependencies: '@azure-tools/test-credential': - specifier: workspace:^ + specifier: workspace:* version: link:../../test-utils/test-credential '@azure-tools/test-recorder': - specifier: workspace:^ + specifier: workspace:* version: link:../../test-utils/recorder '@azure-tools/test-utils-vitest': - specifier: workspace:^ + specifier: workspace:* version: link:../../test-utils/test-utils-vitest '@azure/dev-tool': - specifier: workspace:^ + specifier: workspace:* version: link:../../../common/tools/dev-tool '@azure/eslint-plugin-azure-sdk': - specifier: workspace:^ + specifier: workspace:* version: link:../../../common/tools/eslint-plugin-azure-sdk '@azure/identity': specifier: catalog:internal version: 4.11.1 '@types/node': - specifier: ^20.0.0 + specifier: 'catalog:' version: 20.19.17 - '@vitest/browser': - specifier: catalog:testing - version: 3.2.4(msw@2.7.3(@types/node@20.19.17)(typescript@5.9.3))(playwright@1.55.1)(vite@7.1.7(@types/node@20.19.17)(terser@5.39.0)(tsx@4.20.6)(yaml@2.8.1))(vitest@3.2.4) - '@vitest/coverage-istanbul': - specifier: catalog:testing - version: 3.2.4(vitest@3.2.4) cross-env: specifier: 'catalog:' version: 7.0.3 - dotenv: - specifier: catalog:testing - version: 16.6.1 eslint: specifier: 'catalog:' version: 9.36.0 - playwright: - specifier: catalog:testing - version: 1.55.1 prettier: specifier: 'catalog:' version: 3.6.2 @@ -14251,9 +14239,6 @@ importers: typescript: specifier: 'catalog:' version: 5.9.3 - vitest: - specifier: catalog:testing - version: 3.2.4(@types/debug@4.1.12)(@types/node@20.19.17)(@vitest/browser@3.2.4)(jsdom@16.7.0)(msw@2.7.3(@types/node@20.19.17)(typescript@5.9.3))(terser@5.39.0)(tsx@4.20.6)(yaml@2.8.1) sdk/healthinsights/health-insights-cancerprofiling-rest: dependencies: diff --git a/sdk/healthdataaiservices/ci.mgmt.yml b/sdk/healthdataaiservices/ci.mgmt.yml index f12fd43d2f24..fb7d4a3ba177 100644 --- a/sdk/healthdataaiservices/ci.mgmt.yml +++ b/sdk/healthdataaiservices/ci.mgmt.yml @@ -6,10 +6,13 @@ trigger: - main - release/* - hotfix/* + exclude: + - feature/v4 paths: include: - sdk/healthdataaiservices/ci.mgmt.yml - sdk/healthdataaiservices/arm-healthdataaiservices + - sdk/healthdataaiservices/health-deidentification-rest pr: branches: include: @@ -23,6 +26,7 @@ pr: include: - sdk/healthdataaiservices/ci.mgmt.yml - sdk/healthdataaiservices/arm-healthdataaiservices + - sdk/healthdataaiservices/health-deidentification-rest extends: template: /eng/pipelines/templates/stages/archetype-sdk-client.yml parameters: @@ -30,3 +34,5 @@ extends: Artifacts: - name: azure-arm-healthdataaiservices safeName: azurearmhealthdataaiservices + - name: azure-rest-health-deidentification + safeName: azureresthealthdeidentification diff --git a/sdk/healthdataaiservices/health-deidentification-rest/CHANGELOG.md b/sdk/healthdataaiservices/health-deidentification-rest/CHANGELOG.md index c60d298b7623..758239384834 100644 --- a/sdk/healthdataaiservices/health-deidentification-rest/CHANGELOG.md +++ b/sdk/healthdataaiservices/health-deidentification-rest/CHANGELOG.md @@ -1,5 +1,10 @@ # Release History +## 2.0.0-beta.1 (2025-10-24) +Compared with version 1.0.0 + +Please manually update the changelog with the appropriate changes. + ## 1.1.0-beta.1 (2025-09-30) ### Features Added diff --git a/sdk/healthdataaiservices/health-deidentification-rest/README.md b/sdk/healthdataaiservices/health-deidentification-rest/README.md index bcaef75c9a3f..f4ec39877dc3 100644 --- a/sdk/healthdataaiservices/health-deidentification-rest/README.md +++ b/sdk/healthdataaiservices/health-deidentification-rest/README.md @@ -1,23 +1,14 @@ -# Azure Health Data Services de-identification service REST client library for JavaScript +# Azure Deidentification client library for JavaScript -This package contains a client library for the de-identification service in Azure Health Data Services which -enables users to tag, redact, or surrogate health data containing Protected Health Information (PHI). +This package contains an isomorphic SDK (runs both in Node.js and in browsers) for Azure Deidentification client. -Use the client library for the de-identification service to: - -- Discover PHI in unstructured text -- Replace PHI in unstructured text with placeholder values -- Replace PHI in unstructured text with realistic surrogate values -- Manage asynchronous jobs to de-identify documents in Azure Storage - -**Please rely heavily on our [REST client docs](https://github.com/Azure/azure-sdk-for-js/blob/main/documentation/rest-clients.md) to use this library.** +Health Deidentification Service Key links: - [Source code](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/healthdataaiservices/health-deidentification-rest) - [Package (NPM)](https://www.npmjs.com/package/@azure-rest/health-deidentification) -- [API reference documentation](https://learn.microsoft.com/javascript/api/@azure-rest/health-deidentification) -- [Product documentation][product_documentation] +- [API reference documentation](https://learn.microsoft.com/javascript/api/@azure-rest/health-deidentification?view=azure-node-preview) ## Getting started @@ -26,15 +17,15 @@ Key links: - [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule) - Latest versions of Safari, Chrome, Edge and Firefox. +See our [support policy](https://github.com/Azure/azure-sdk-for-js/blob/main/SUPPORT.md) for more details. + ### Prerequisites -- You need an [Azure subscription][azure_sub] to use this package. -- [Deploy the de-identification service][deid_quickstart]. -- [Configure Azure role-based access control (RBAC)][deid_rbac] for the operations you will perform. +- An [Azure subscription][azure_sub]. ### Install the `@azure-rest/health-deidentification` package -Install the library with `npm`: +Install the Azure Deidentification client library for JavaScript with `npm`: ```bash npm install @azure-rest/health-deidentification @@ -42,178 +33,60 @@ npm install @azure-rest/health-deidentification ### Create and authenticate a `DeidentificationClient` -You can authenticate with Microsoft Entra ID using the [Azure Identity library][azure_identity]. To use the [DefaultAzureCredential][defaultazurecredential] provider shown below, or other credential providers provided with the Azure SDK, please install the `@azure/identity` package: +To create a client object to access the Azure Deidentification API, you will need the `endpoint` of your Azure Deidentification resource and a `credential`. The Azure Deidentification client can use Azure Active Directory credentials to authenticate. +You can find the endpoint for your Azure Deidentification resource in the [Azure Portal][azure_portal]. -```bash -npm install @azure/identity -``` +You can authenticate with Azure Active Directory using a credential from the [@azure/identity][azure_identity] library or [an existing AAD Token](https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/identity/identity/samples/AzureIdentityExamples.md#authenticating-with-a-pre-fetched-access-token). -After setup, you can choose which type of [credential](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity#credentials) from `@azure/identity` to use. -As an example, [DefaultAzureCredential](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity#defaultazurecredential) can be used to authenticate the client. - -Set the values of the client ID, tenant ID, and client secret of the AAD application as environment variables: -`AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, `AZURE_CLIENT_SECRET`. - -You will need a **service URL** to instantiate a client object. You can find the service URL for a particular resource in the [Azure portal][azure_portal], or using the [Azure CLI][azure_cli]. -Here's an example of setting an environment variable in Bash using Azure CLI: +To use the [DefaultAzureCredential][defaultazurecredential] provider shown below, or other credential providers provided with the Azure SDK, please install the `@azure/identity` package: ```bash -# Get the service URL for the resource -export HEALTHDATAAISERVICES_DEID_SERVICE_ENDPOINT=$(az deidservice show --name "" --resource-group "" --query "properties.serviceUrl") -``` - -Create a client with the endpoint and credential: - -```ts snippet:CreateClient -import { DefaultAzureCredential } from "@azure/identity"; -import DeidentificationClient from "@azure-rest/health-deidentification"; - -const credential = new DefaultAzureCredential(); -const serviceEndpoint = - process.env.HEALTHDATAAISERVICES_DEID_SERVICE_ENDPOINT || "https://example.api.deid.azure.com"; -const client = DeidentificationClient(serviceEndpoint, credential); +npm install @azure/identity ``` -## Key concepts - -### De-identification operations: - -Given an input text, the de-identification service can perform three main operations: - -- `Tag` returns the category and location within the text of detected PHI entities. -- `Redact` returns output text where detected PHI entities are replaced with placeholder text. For example `John` replaced with `[name]`. -- `Surrogate` returns output text where detected PHI entities are replaced with realistic replacement values. For example, `My name is John Smith` could become `My name is Tom Jones`. -- `SurrogateOnly` returns output text where user-defined PHI entities are replaced with realistic replacement values. - -### String Encoding - -When using the `Tag` operation, the service will return the locations of PHI entities in the input text. These locations will be represented as offsets and lengths, each of which is a [StringIndex][string_index] containing -three properties corresponding to three different text encodings. **JavaScript applications should use the `utf16` property.** - -For more on text encoding, see [Character encoding in .NET][character_encoding]. +You will also need to **register a new AAD application and grant access to Azure Deidentification** by assigning the suitable role to your service principal (note: roles such as `"Owner"` will not grant the necessary permissions). -### Available endpoints +For more information about how to create an Azure AD Application check out [this guide](https://learn.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal). -There are two ways to interact with the de-identification service. You can send text directly, or you can create jobs -to de-identify documents in Azure Storage. +Using Node.js and Node-like environments, you can use the `DefaultAzureCredential` class to authenticate the client. -You can de-identify text directly using the `DeidentificationClient`: - -```ts snippet:DeidentifyText +```ts +import { DeidentificationClient } from "@azure-rest/health-deidentification"; import { DefaultAzureCredential } from "@azure/identity"; -import DeidentificationClient, { - DeidentificationContent, - isUnexpected, -} from "@azure-rest/health-deidentification"; - -const credential = new DefaultAzureCredential(); -const serviceEndpoint = - process.env.HEALTHDATAAISERVICES_DEID_SERVICE_ENDPOINT || "https://example.api.deid.azure.com"; -const client = DeidentificationClient(serviceEndpoint, credential); - -const content: DeidentificationContent = { - inputText: "Hello John!", -}; -const response = await client.path("/deid").post({ body: content }); - -if (isUnexpected(response)) { - throw response.body.error; -} - -console.log(response.body.outputText); // Hello, Tom! +const client = new DeidentificationClient("", new DefaultAzureCredential()); ``` -To de-identify documents in Azure Storage, you'll need a storage account with a container to which the de-identification service has been granted an appropriate role. See [Tutorial: Configure Azure Storage to de-identify documents][deid_configure_storage] for prerequisites and configuration options. You can upload the files in the [test data folder][test_data] as blobs, like: `https://.blob.core.windows.net//example_patient_1/doctor_dictation.txt`. +For browser environments, use the `InteractiveBrowserCredential` from the `@azure/identity` package to authenticate. -You can create jobs to de-identify documents in the source Azure Storage account and container with an optional input prefix. If there's no input prefix, all blobs in the container will be de-identified. Azure Storage blobs can use `/` in the blob name to emulate a folder or directory layout. For more on blob naming, see [Naming and Referencing Containers, Blobs, and Metadata][blob_names]. The files you've uploaded can be de-identified by providing `example_patient_1` as the input prefix: +```ts +import { InteractiveBrowserCredential } from "@azure/identity"; +import { DeidentificationClient } from "@azure-rest/health-deidentification"; -``` -/ -├── example_patient_1/ - └──doctor_dictation.txt - └──row-2-data.txt - └──visit-summary.txt +const credential = new InteractiveBrowserCredential({ + tenantId: "", + clientId: "" + }); +const client = new DeidentificationClient("", credential); ``` -Your target Azure Storage account and container where documents will be written can be the same as the source, or a different account or container. In the examples below, the source and target account and container are the same. You can specify an output prefix to indicate where the job's output documents should be written (defaulting to `_output`). Each document processed by the job will have the same relative blob name with the input prefix replaced by the output prefix: -``` -/ -├── example_patient_1/ - └──doctor_dictation.txt - └──row-2-data.txt - └──visit-summary.txt -├── _output/ - └──doctor_dictation.txt - └──row-2-data.txt - └──visit-summary.txt -``` - -Set the following environment variables, updating the storage account and container with real values: +### JavaScript Bundle +To use this client library in the browser, first you need to use a bundler. For details on how to do this, please refer to our [bundling documentation](https://aka.ms/AzureSDKBundling). -```bash -export HEALTHDATAAISERVICES_STORAGE_ACCOUNT_LOCATION="https://.blob.core.windows.net/" -export INPUT_PREFIX="example_patient_1" -export OUTPUT_PREFIX="_output" -``` - -You can create and view job status using the client: - -```ts snippet:DeidentifyDocuments -import { DefaultAzureCredential } from "@azure/identity"; -import DeidentificationClient, { - DeidentificationJob, - DeidentifyDocumentsDefaultResponse, - isUnexpected, - getLongRunningPoller, -} from "@azure-rest/health-deidentification"; - -const credential = new DefaultAzureCredential(); -const serviceEndpoint = - process.env["HEALTHDATAAISERVICES_DEID_SERVICE_ENDPOINT"] || "https://example.api.deid.azure.com"; -const storageLocation = - process.env["HEALTHDATAAISERVICES_STORAGE_ACCOUNT_LOCATION"] || - "https://example.blob.core.windows.net/example-container"; -const inputPrefix = "example_patient_1"; -const outputPrefix = process.env["OUTPUT_PREFIX"] || "_output"; - -const client = DeidentificationClient(serviceEndpoint, credential); -const jobName = "sample-job-" + new Date().getTime().toString().slice(-8); - -const job: DeidentificationJob = { - operation: "Surrogate", - sourceLocation: { location: storageLocation, prefix: inputPrefix }, - targetLocation: { location: storageLocation, prefix: outputPrefix }, -}; -const response = (await client - .path("/jobs/{name}", jobName) - .put({ body: job })) as DeidentifyDocumentsDefaultResponse; - -if (isUnexpected(response)) { - throw response.body.error; -} - -const poller = await getLongRunningPoller(client, response); -const finalOutput = await poller.pollUntilDone(); -console.log(finalOutput.body); -``` +## Key concepts -## Next steps +### DeidentificationClient -Find a bug, or have feedback? Raise an issue with the [Health Deidentification][github_issue_label] label. +`DeidentificationClient` is the primary interface for developers using the Azure Deidentification client library. Explore the methods on this client object to understand the different features of the Azure Deidentification service that you can access. ## Troubleshooting -- **Unable to Access Source or Target Storage** - - Ensure you create your de-identification service with a system assigned managed identity - - Ensure your storage account has given permissions to that managed identity - ### Logging Enabling logging may help uncover useful information about failures. In order to see a log of HTTP requests and responses, set the `AZURE_LOG_LEVEL` environment variable to `info`. Alternatively, logging can be enabled at runtime by calling `setLogLevel` in the `@azure/logger`: -```ts snippet:SetLogLevel +```ts import { setLogLevel } from "@azure/logger"; setLogLevel("info"); @@ -221,16 +94,16 @@ setLogLevel("info"); For more detailed instructions on how to enable logs, you can look at the [@azure/logger package docs](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/core/logger). - + +## Contributing + +If you'd like to contribute to this library, please read the [contributing guide](https://github.com/Azure/azure-sdk-for-js/blob/main/CONTRIBUTING.md) to learn more about how to build and test the code. + +## Related projects + +- [Microsoft Azure SDK for JavaScript](https://github.com/Azure/azure-sdk-for-js) [azure_sub]: https://azure.microsoft.com/free/ -[deid_quickstart]: https://learn.microsoft.com/azure/healthcare-apis/deidentification/quickstart -[string_index]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/healthdataaiservices/health-deidentification-rest/src/outputModels.ts#L175 -[character_encoding]: https://learn.microsoft.com/dotnet/standard/base-types/character-encoding-introduction -[deid_redact]: https://learn.microsoft.com/azure/healthcare-apis/deidentification/redaction-format -[deid_rbac]: https://learn.microsoft.com/azure/healthcare-apis/deidentification/manage-access-rbac -[deid_managed_identity]: https://learn.microsoft.com/azure/healthcare-apis/deidentification/managed-identities -[deid_configure_storage]: https://learn.microsoft.com/azure/healthcare-apis/deidentification/configure-storage -[azure_cli]: https://learn.microsoft.com/cli/azure/healthcareapis/deidservice?view=azure-cli-latest -[azure_portal]: https://ms.portal.azure.com -[github_issue_label]: https://github.com/Azure/azure-sdk-for-js/labels/Health%20Deidentification +[azure_portal]: https://portal.azure.com +[azure_identity]: https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity +[defaultazurecredential]: https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity#defaultazurecredential diff --git a/sdk/healthdataaiservices/health-deidentification-rest/metadata.json b/sdk/healthdataaiservices/health-deidentification-rest/metadata.json index 74bbaa2a406f..8f9286e2a4c6 100644 --- a/sdk/healthdataaiservices/health-deidentification-rest/metadata.json +++ b/sdk/healthdataaiservices/health-deidentification-rest/metadata.json @@ -1,4 +1,41 @@ { "apiVersion": "2025-07-15-preview", - "emitterVersion": "0.44.1" + "emitterVersion": "0.45.1", + "crossLanguageDefinitions": { + "CrossLanguagePackageId": "HealthDataAIServices.DeidServices", + "CrossLanguageDefinitionId": { + "@azure-rest/health-deidentification!DeidentificationJob:interface": "HealthDataAIServices.DeidServices.DeidentificationJob", + "@azure-rest/health-deidentification!SourceStorageLocation:interface": "HealthDataAIServices.DeidServices.SourceStorageLocation", + "@azure-rest/health-deidentification!TargetStorageLocation:interface": "HealthDataAIServices.DeidServices.TargetStorageLocation", + "@azure-rest/health-deidentification!DeidentificationJobCustomizationOptions:interface": "HealthDataAIServices.DeidServices.DeidentificationJobCustomizationOptions", + "@azure-rest/health-deidentification!Error:interface": "Azure.Core.Foundations.Error", + "@azure-rest/health-deidentification!InnerError:interface": "Azure.Core.Foundations.InnerError", + "@azure-rest/health-deidentification!DeidentificationJobSummary:interface": "HealthDataAIServices.DeidServices.DeidentificationJobSummary", + "@azure-rest/health-deidentification!ErrorResponse:interface": "Azure.Core.Foundations.ErrorResponse", + "@azure-rest/health-deidentification!PagedDeidentificationJob:interface": "Azure.Core.Foundations.CustomPage", + "@azure-rest/health-deidentification!PagedDeidentificationDocumentDetails:interface": "Azure.Core.Foundations.CustomPage", + "@azure-rest/health-deidentification!DeidentificationDocumentDetails:interface": "HealthDataAIServices.DeidServices.DeidentificationDocumentDetails", + "@azure-rest/health-deidentification!DeidentificationDocumentLocation:interface": "HealthDataAIServices.DeidServices.DeidentificationDocumentLocation", + "@azure-rest/health-deidentification!DeidentificationContent:interface": "HealthDataAIServices.DeidServices.DeidentificationContent", + "@azure-rest/health-deidentification!TaggedPhiEntities:interface": "HealthDataAIServices.DeidServices.TaggedPhiEntities", + "@azure-rest/health-deidentification!SimplePhiEntity:interface": "HealthDataAIServices.DeidServices.SimplePhiEntity", + "@azure-rest/health-deidentification!DeidentificationCustomizationOptions:interface": "HealthDataAIServices.DeidServices.DeidentificationCustomizationOptions", + "@azure-rest/health-deidentification!DeidentificationResult:interface": "HealthDataAIServices.DeidServices.DeidentificationResult", + "@azure-rest/health-deidentification!PhiTaggerResult:interface": "HealthDataAIServices.DeidServices.PhiTaggerResult", + "@azure-rest/health-deidentification!PhiEntity:interface": "HealthDataAIServices.DeidServices.PhiEntity", + "@azure-rest/health-deidentification!StringIndex:interface": "HealthDataAIServices.DeidServices.StringIndex", + "@azure-rest/health-deidentification!KnownDeidentificationOperationType:enum": "HealthDataAIServices.DeidServices.DeidentificationOperationType", + "@azure-rest/health-deidentification!KnownOperationState:enum": "Azure.Core.Foundations.OperationState", + "@azure-rest/health-deidentification!KnownTextEncodingType:enum": "HealthDataAIServices.DeidServices.TextEncodingType", + "@azure-rest/health-deidentification!KnownPhiCategory:enum": "HealthDataAIServices.DeidServices.PhiCategory", + "@azure-rest/health-deidentification!KnownVersions:enum": "HealthDataAIServices.DeidServices.Versions", + "@azure-rest/health-deidentification!DeidentificationClient#deidentifyText:member(1)": "HealthDataAIServices.DeidServices.deidentifyText", + "@azure-rest/health-deidentification!DeidentificationClient#deleteJob:member(1)": "HealthDataAIServices.DeidServices.deleteJob", + "@azure-rest/health-deidentification!DeidentificationClient#cancelJob:member(1)": "HealthDataAIServices.DeidServices.cancelJob", + "@azure-rest/health-deidentification!DeidentificationClient#listJobDocuments:member(1)": "HealthDataAIServices.DeidServices.listJobDocuments", + "@azure-rest/health-deidentification!DeidentificationClient#listJobs:member(1)": "HealthDataAIServices.DeidServices.listJobs", + "@azure-rest/health-deidentification!DeidentificationClient#deidentifyDocuments:member(1)": "HealthDataAIServices.DeidServices.deidentifyDocuments", + "@azure-rest/health-deidentification!DeidentificationClient#getJob:member(1)": "HealthDataAIServices.DeidServices.getJob" + } + } } diff --git a/sdk/healthdataaiservices/health-deidentification-rest/package.json b/sdk/healthdataaiservices/health-deidentification-rest/package.json index 49a564e53231..a5ae8558bc36 100644 --- a/sdk/healthdataaiservices/health-deidentification-rest/package.json +++ b/sdk/healthdataaiservices/health-deidentification-rest/package.json @@ -1,6 +1,6 @@ { "name": "@azure-rest/health-deidentification", - "version": "1.1.0-beta.1", + "version": "2.0.0-beta.1", "description": "Health Deidentification Service", "engines": { "node": ">=20.0.0" @@ -10,7 +10,9 @@ "tshy": { "exports": { "./package.json": "./package.json", - ".": "./src/index.ts" + ".": "./src/index.ts", + "./api": "./src/api/index.ts", + "./models": "./src/models/index.ts" }, "dialects": [ "esm", @@ -52,57 +54,52 @@ "//metadata": { "constantPaths": [ { - "path": "src/deidentificationClient.ts", + "path": "src/api/deidentificationContext.ts", "prefix": "userAgentInfo" } ] }, "dependencies": { + "@azure/core-util": "^1.12.0", "@azure-rest/core-client": "^2.3.1", "@azure/abort-controller": "^2.1.2", "@azure/core-auth": "^1.9.0", "@azure/core-lro": "^3.1.0", "@azure/core-rest-pipeline": "^1.20.0", - "@azure/core-util": "^1.12.0", "@azure/logger": "^1.2.0", "tslib": "^2.8.1" }, "devDependencies": { - "@azure-tools/test-credential": "workspace:^", - "@azure-tools/test-recorder": "workspace:^", - "@azure-tools/test-utils-vitest": "workspace:^", - "@azure/dev-tool": "workspace:^", - "@azure/eslint-plugin-azure-sdk": "workspace:^", + "@azure-tools/test-credential": "workspace:*", + "@azure-tools/test-recorder": "workspace:*", + "@azure-tools/test-utils-vitest": "workspace:*", + "@azure/dev-tool": "workspace:*", + "tshy": "catalog:", + "@azure/eslint-plugin-azure-sdk": "workspace:*", "@azure/identity": "catalog:internal", - "@types/node": "^20.0.0", - "@vitest/browser": "catalog:testing", - "@vitest/coverage-istanbul": "catalog:testing", + "@types/node": "catalog:", "cross-env": "catalog:", - "dotenv": "catalog:testing", "eslint": "catalog:", - "playwright": "catalog:testing", "prettier": "catalog:", "rimraf": "catalog:", - "tshy": "catalog:", - "typescript": "catalog:", - "vitest": "catalog:testing" + "typescript": "catalog:" }, "scripts": { - "build": "npm run clean && dev-tool run build-package && dev-tool run extract-api", + "clean": "rimraf --glob dist dist-browser dist-esm test-dist temp types *.tgz *.log", + "extract-api": "rimraf review && dev-tool run extract-api", + "pack": "pnpm pack 2>&1", + "lint": "eslint package.json src test", + "lint:fix": "eslint package.json src test --fix --fix-type [problem,suggestion]", "build:samples": "echo skipped", "check-format": "prettier --list-different --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.{ts,cts,mts}\" \"test/**/*.{ts,cts,mts}\" \"*.{js,cjs,mjs,json}\" ", - "clean": "rimraf --glob dist dist-browser dist-esm test-dist temp types *.tgz *.log", "execute:samples": "echo skipped", - "extract-api": "rimraf review && dev-tool run extract-api", "format": "prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.{ts,cts,mts}\" \"test/**/*.{ts,cts,mts}\" \"*.{js,cjs,mjs,json}\" ", "generate:client": "echo skipped", - "lint": "eslint package.json src test", - "lint:fix": "eslint package.json src test --fix --fix-type [problem,suggestion]", - "pack": "pnpm pack 2>&1", - "test": "npm run test:node && npm run test:browser", "test:browser": "dev-tool run build-test && dev-tool run test:vitest --browser", + "build": "npm run clean && dev-tool run build-package && dev-tool run extract-api", "test:node": "dev-tool run test:vitest", "test:node:esm": "dev-tool run test:vitest --esm", + "test": "npm run test:node && npm run test:browser", "update-snippets": "dev-tool run update-snippets" }, "exports": { @@ -124,6 +121,42 @@ "types": "./dist/commonjs/index.d.ts", "default": "./dist/commonjs/index.js" } + }, + "./api": { + "browser": { + "types": "./dist/browser/api/index.d.ts", + "default": "./dist/browser/api/index.js" + }, + "react-native": { + "types": "./dist/react-native/api/index.d.ts", + "default": "./dist/react-native/api/index.js" + }, + "import": { + "types": "./dist/esm/api/index.d.ts", + "default": "./dist/esm/api/index.js" + }, + "require": { + "types": "./dist/commonjs/api/index.d.ts", + "default": "./dist/commonjs/api/index.js" + } + }, + "./models": { + "browser": { + "types": "./dist/browser/models/index.d.ts", + "default": "./dist/browser/models/index.js" + }, + "react-native": { + "types": "./dist/react-native/models/index.d.ts", + "default": "./dist/react-native/models/index.js" + }, + "import": { + "types": "./dist/esm/models/index.d.ts", + "default": "./dist/esm/models/index.js" + }, + "require": { + "types": "./dist/commonjs/models/index.d.ts", + "default": "./dist/commonjs/models/index.js" + } } }, "main": "./dist/commonjs/index.js", diff --git a/sdk/healthdataaiservices/health-deidentification-rest/review/health-deidentification-api-node.api.md b/sdk/healthdataaiservices/health-deidentification-rest/review/health-deidentification-api-node.api.md new file mode 100644 index 000000000000..81ffb5e4f0c4 --- /dev/null +++ b/sdk/healthdataaiservices/health-deidentification-rest/review/health-deidentification-api-node.api.md @@ -0,0 +1,91 @@ +## API Report File for "@azure-rest/health-deidentification" + +> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/). + +```ts + +import type { Client } from '@azure-rest/core-client'; +import type { ClientOptions } from '@azure-rest/core-client'; +import type { ErrorModel } from '@azure-rest/core-client'; +import type { OperationOptions } from '@azure-rest/core-client'; +import type { OperationState as OperationState_2 } from '@azure/core-lro'; +import type { PollerLike } from '@azure/core-lro'; +import type { TokenCredential } from '@azure/core-auth'; + +// @public +export function cancelJob(context: DeidentificationContext, name: string, options?: CancelJobOptionalParams): Promise; + +// @public +export interface CancelJobOptionalParams extends OperationOptions { + clientRequestId?: string; +} + +// @public (undocumented) +export function createDeidentification(endpointParam: string, credential: TokenCredential, options?: DeidentificationClientOptionalParams): DeidentificationContext; + +// @public +export interface DeidentificationClientOptionalParams extends ClientOptions { + apiVersion?: string; +} + +// @public (undocumented) +export interface DeidentificationContext extends Client { + apiVersion: string; +} + +// @public +export function deidentifyDocuments(context: DeidentificationContext, name: string, resource: DeidentificationJob, options?: DeidentifyDocumentsOptionalParams): PollerLike, DeidentificationJob>; + +// @public +export interface DeidentifyDocumentsOptionalParams extends OperationOptions { + clientRequestId?: string; + updateIntervalInMs?: number; +} + +// @public +export function deidentifyText(context: DeidentificationContext, body: DeidentificationContent, options?: DeidentifyTextOptionalParams): Promise; + +// @public +export interface DeidentifyTextOptionalParams extends OperationOptions { + clientRequestId?: string; +} + +// @public +export function deleteJob(context: DeidentificationContext, name: string, options?: DeleteJobOptionalParams): Promise; + +// @public +export interface DeleteJobOptionalParams extends OperationOptions { + clientRequestId?: string; +} + +// @public +export function getJob(context: DeidentificationContext, name: string, options?: GetJobOptionalParams): Promise; + +// @public +export interface GetJobOptionalParams extends OperationOptions { + clientRequestId?: string; +} + +// @public +export function listJobDocuments(context: DeidentificationContext, name: string, options?: ListJobDocumentsOptionalParams): PagedAsyncIterableIterator; + +// @public +export interface ListJobDocumentsOptionalParams extends OperationOptions { + clientRequestId?: string; + continuationToken?: string; + maxpagesize?: number; +} + +// @public +export function listJobs(context: DeidentificationContext, options?: ListJobsOptionalParams): PagedAsyncIterableIterator; + +// @public +export interface ListJobsOptionalParams extends OperationOptions { + clientRequestId?: string; + continuationToken?: string; + maxpagesize?: number; +} + +// (No @packageDocumentation comment for this package) + +``` diff --git a/sdk/healthdataaiservices/health-deidentification-rest/review/health-deidentification-models-node.api.md b/sdk/healthdataaiservices/health-deidentification-rest/review/health-deidentification-models-node.api.md new file mode 100644 index 000000000000..1dc1e59a9034 --- /dev/null +++ b/sdk/healthdataaiservices/health-deidentification-rest/review/health-deidentification-models-node.api.md @@ -0,0 +1,145 @@ +## API Report File for "@azure-rest/health-deidentification" + +> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/). + +```ts + +import type { ErrorModel } from '@azure-rest/core-client'; + +// @public +export interface DeidentificationContent { + customizations?: DeidentificationCustomizationOptions; + inputText: string; + operation?: DeidentificationOperationType; + taggedEntities?: TaggedPhiEntities; +} + +// @public +export interface DeidentificationCustomizationOptions { + inputLocale?: string; + redactionFormat?: string; + surrogateLocale?: string; +} + +// @public +export interface DeidentificationDocumentDetails { + error?: ErrorModel; + readonly id: string; + input: DeidentificationDocumentLocation; + output?: DeidentificationDocumentLocation; + status: OperationState; +} + +// @public +export interface DeidentificationDocumentLocation { + readonly etag: string; + location: string; +} + +// @public +export interface DeidentificationJob { + readonly createdAt: Date; + customizations?: DeidentificationJobCustomizationOptions; + readonly error?: ErrorModel; + readonly lastUpdatedAt: Date; + readonly name: string; + operation?: DeidentificationOperationType; + sourceLocation: SourceStorageLocation; + readonly startedAt?: Date; + readonly status: OperationState; + readonly summary?: DeidentificationJobSummary; + targetLocation: TargetStorageLocation; +} + +// @public +export interface DeidentificationJobCustomizationOptions { + inputLocale?: string; + redactionFormat?: string; + surrogateLocale?: string; +} + +// @public +export interface DeidentificationJobSummary { + bytesProcessed: number; + canceled: number; + failed: number; + successful: number; + total: number; +} + +// @public +export type DeidentificationOperationType = "Redact" | "Surrogate" | "Tag" | "SurrogateOnly"; + +// @public +export interface DeidentificationResult { + outputText?: string; + taggerResult?: PhiTaggerResult; +} + +// @public +export enum KnownVersions { + V20241115 = "2024-11-15", + V20250715Preview = "2025-07-15-preview" +} + +// @public +export type OperationState = "NotStarted" | "Running" | "Succeeded" | "Failed" | "Canceled"; + +// @public +export type PhiCategory = "Unknown" | "Account" | "Age" | "BioID" | "City" | "CountryOrRegion" | "Date" | "Device" | "Doctor" | "Email" | "Fax" | "HealthPlan" | "Hospital" | "IDNum" | "IPAddress" | "License" | "LocationOther" | "MedicalRecord" | "Organization" | "Patient" | "Phone" | "Profession" | "SocialSecurity" | "State" | "Street" | "Url" | "Username" | "Vehicle" | "Zip"; + +// @public +export interface PhiEntity { + category: PhiCategory; + confidenceScore?: number; + length: StringIndex; + offset: StringIndex; + text?: string; +} + +// @public +export interface PhiTaggerResult { + entities: PhiEntity[]; +} + +// @public +export interface SimplePhiEntity { + category: PhiCategory; + length: number; + offset: number; + text?: string; +} + +// @public +export interface SourceStorageLocation { + extensions?: string[]; + location: string; + prefix: string; +} + +// @public +export interface StringIndex { + codePoint: number; + utf16: number; + utf8: number; +} + +// @public +export interface TaggedPhiEntities { + encoding: TextEncodingType; + entities: SimplePhiEntity[]; +} + +// @public +export interface TargetStorageLocation { + location: string; + overwrite?: boolean; + prefix: string; +} + +// @public +export type TextEncodingType = "Utf8" | "Utf16" | "CodePoint"; + +// (No @packageDocumentation comment for this package) + +``` diff --git a/sdk/healthdataaiservices/health-deidentification-rest/review/health-deidentification-node.api.md b/sdk/healthdataaiservices/health-deidentification-rest/review/health-deidentification-node.api.md index 9077854a5267..f3f02eacb295 100644 --- a/sdk/healthdataaiservices/health-deidentification-rest/review/health-deidentification-node.api.md +++ b/sdk/healthdataaiservices/health-deidentification-rest/review/health-deidentification-node.api.md @@ -5,81 +5,40 @@ ```ts import type { AbortSignalLike } from '@azure/abort-controller'; -import type { CancelOnProgress } from '@azure/core-lro'; -import type { Client } from '@azure-rest/core-client'; import type { ClientOptions } from '@azure-rest/core-client'; -import type { CreateHttpPollerOptions } from '@azure/core-lro'; import type { ErrorModel } from '@azure-rest/core-client'; -import type { ErrorResponse } from '@azure-rest/core-client'; -import type { HttpResponse } from '@azure-rest/core-client'; +import type { OperationOptions } from '@azure-rest/core-client'; import type { OperationState as OperationState_2 } from '@azure/core-lro'; import type { PathUncheckedResponse } from '@azure-rest/core-client'; -import type { RawHttpHeaders } from '@azure/core-rest-pipeline'; -import type { RawHttpHeadersInput } from '@azure/core-rest-pipeline'; -import type { RequestParameters } from '@azure-rest/core-client'; -import type { StreamableMethod } from '@azure-rest/core-client'; +import type { Pipeline } from '@azure/core-rest-pipeline'; +import type { PollerLike } from '@azure/core-lro'; import type { TokenCredential } from '@azure/core-auth'; -// @public (undocumented) -export interface CancelJob { - post(options?: CancelJobParameters): StreamableMethod; -} - -// @public (undocumented) -export interface CancelJob200Headers { - "x-ms-client-request-id"?: string; -} - // @public -export interface CancelJob200Response extends HttpResponse { - // (undocumented) - body: DeidentificationJobOutput; - // (undocumented) - headers: RawHttpHeaders & CancelJob200Headers; - // (undocumented) - status: "200"; -} - -// @public (undocumented) -export interface CancelJobDefaultHeaders { - "x-ms-error-code"?: string; -} - -// @public (undocumented) -export interface CancelJobDefaultResponse extends HttpResponse { - // (undocumented) - body: ErrorResponse; - // (undocumented) - headers: RawHttpHeaders & CancelJobDefaultHeaders; - // (undocumented) - status: string; -} - -// @public (undocumented) -export interface CancelJobHeaderParam { - // (undocumented) - headers?: RawHttpHeadersInput & CancelJobHeaders; +export interface CancelJobOptionalParams extends OperationOptions { + clientRequestId?: string; } -// @public (undocumented) -export interface CancelJobHeaders { - "x-ms-client-request-id"?: string; -} - -// @public (undocumented) -export type CancelJobParameters = CancelJobHeaderParam & RequestParameters; - // @public -function createClient(endpointParam: string, credentials: TokenCredential, { apiVersion, ...options }?: DeidentificationClientOptions): DeidentificationClient; -export default createClient; +export type ContinuablePage = TPage & { + continuationToken?: string; +}; // @public (undocumented) -export type DeidentificationClient = Client & { - path: Routes; -}; +export class DeidentificationClient { + constructor(endpointParam: string, credential: TokenCredential, options?: DeidentificationClientOptionalParams); + cancelJob(name: string, options?: CancelJobOptionalParams): Promise; + deidentifyDocuments(name: string, resource: DeidentificationJob, options?: DeidentifyDocumentsOptionalParams): PollerLike, DeidentificationJob>; + deidentifyText(body: DeidentificationContent, options?: DeidentifyTextOptionalParams): Promise; + deleteJob(name: string, options?: DeleteJobOptionalParams): Promise; + getJob(name: string, options?: GetJobOptionalParams): Promise; + listJobDocuments(name: string, options?: ListJobDocumentsOptionalParams): PagedAsyncIterableIterator; + listJobs(options?: ListJobsOptionalParams): PagedAsyncIterableIterator; + readonly pipeline: Pipeline; +} // @public -export interface DeidentificationClientOptions extends ClientOptions { +export interface DeidentificationClientOptionalParams extends ClientOptions { apiVersion?: string; } @@ -99,25 +58,32 @@ export interface DeidentificationCustomizationOptions { } // @public -export interface DeidentificationDocumentDetailsOutput { +export interface DeidentificationDocumentDetails { error?: ErrorModel; readonly id: string; - input: DeidentificationDocumentLocationOutput; - output?: DeidentificationDocumentLocationOutput; - status: OperationStateOutput; + input: DeidentificationDocumentLocation; + output?: DeidentificationDocumentLocation; + status: OperationState; } // @public -export interface DeidentificationDocumentLocationOutput { +export interface DeidentificationDocumentLocation { readonly etag: string; location: string; } // @public export interface DeidentificationJob { + readonly createdAt: Date; customizations?: DeidentificationJobCustomizationOptions; + readonly error?: ErrorModel; + readonly lastUpdatedAt: Date; + readonly name: string; operation?: DeidentificationOperationType; sourceLocation: SourceStorageLocation; + readonly startedAt?: Date; + readonly status: OperationState; + readonly summary?: DeidentificationJobSummary; targetLocation: TargetStorageLocation; } @@ -128,28 +94,6 @@ export interface DeidentificationJobCustomizationOptions { surrogateLocale?: string; } -// @public -export interface DeidentificationJobCustomizationOptionsOutput { - inputLocale?: string; - redactionFormat?: string; - surrogateLocale?: string; -} - -// @public -export interface DeidentificationJobOutput { - readonly createdAt: string; - customizations?: DeidentificationJobCustomizationOptionsOutput; - readonly error?: ErrorModel; - readonly lastUpdatedAt: string; - readonly name: string; - operation?: DeidentificationOperationTypeOutput; - sourceLocation: SourceStorageLocationOutput; - readonly startedAt?: string; - readonly status: OperationStateOutput; - readonly summary?: DeidentificationJobSummaryOutput; - targetLocation: TargetStorageLocationOutput; -} - // @public export interface DeidentificationJobSummary { bytesProcessed: number; @@ -160,474 +104,95 @@ export interface DeidentificationJobSummary { } // @public -export interface DeidentificationJobSummaryOutput { - bytesProcessed: number; - canceled: number; - failed: number; - successful: number; - total: number; -} - -// @public -export type DeidentificationOperationType = string; +export type DeidentificationOperationType = "Redact" | "Surrogate" | "Tag" | "SurrogateOnly"; // @public -export type DeidentificationOperationTypeOutput = string; - -// @public -export interface DeidentificationResultOutput { +export interface DeidentificationResult { outputText?: string; - taggerResult?: PhiTaggerResultOutput; -} - -// @public (undocumented) -export interface DeidentifyDocuments200Headers { - "operation-location": string; - "x-ms-client-request-id"?: string; -} - -// @public -export interface DeidentifyDocuments200Response extends HttpResponse { - // (undocumented) - body: DeidentificationJobOutput; - // (undocumented) - headers: RawHttpHeaders & DeidentifyDocuments200Headers; - // (undocumented) - status: "200"; -} - -// @public (undocumented) -export interface DeidentifyDocuments201Headers { - "operation-location": string; - "x-ms-client-request-id"?: string; -} - -// @public -export interface DeidentifyDocuments201Response extends HttpResponse { - // (undocumented) - body: DeidentificationJobOutput; - // (undocumented) - headers: RawHttpHeaders & DeidentifyDocuments201Headers; - // (undocumented) - status: "201"; -} - -// @public (undocumented) -export interface DeidentifyDocumentsBodyParam { - body: DeidentificationJob; -} - -// @public (undocumented) -export interface DeidentifyDocumentsDefaultHeaders { - "x-ms-error-code"?: string; -} - -// @public (undocumented) -export interface DeidentifyDocumentsDefaultResponse extends HttpResponse { - // (undocumented) - body: ErrorResponse; - // (undocumented) - headers: RawHttpHeaders & DeidentifyDocumentsDefaultHeaders; - // (undocumented) - status: string; -} - -// @public (undocumented) -export interface DeidentifyDocumentsHeaderParam { - // (undocumented) - headers?: RawHttpHeadersInput & DeidentifyDocumentsHeaders; -} - -// @public (undocumented) -export interface DeidentifyDocumentsHeaders { - "x-ms-client-request-id"?: string; -} - -// @public -export interface DeidentifyDocumentsLogicalResponse extends HttpResponse { - // (undocumented) - body: DeidentificationJobOutput; - // (undocumented) - status: "200"; -} - -// @public (undocumented) -export type DeidentifyDocumentsParameters = DeidentifyDocumentsHeaderParam & DeidentifyDocumentsBodyParam & RequestParameters; - -// @public (undocumented) -export interface DeidentifyText { - post(options: DeidentifyTextParameters): StreamableMethod; -} - -// @public (undocumented) -export interface DeidentifyText200Headers { - "x-ms-client-request-id"?: string; + taggerResult?: PhiTaggerResult; } // @public -export interface DeidentifyText200Response extends HttpResponse { - // (undocumented) - body: DeidentificationResultOutput; - // (undocumented) - headers: RawHttpHeaders & DeidentifyText200Headers; - // (undocumented) - status: "200"; -} - -// @public (undocumented) -export interface DeidentifyTextBodyParam { - body: DeidentificationContent; -} - -// @public (undocumented) -export interface DeidentifyTextDefaultHeaders { - "x-ms-error-code"?: string; -} - -// @public (undocumented) -export interface DeidentifyTextDefaultResponse extends HttpResponse { - // (undocumented) - body: ErrorResponse; - // (undocumented) - headers: RawHttpHeaders & DeidentifyTextDefaultHeaders; - // (undocumented) - status: string; -} - -// @public (undocumented) -export interface DeidentifyTextHeaderParam { - // (undocumented) - headers?: RawHttpHeadersInput & DeidentifyTextHeaders; -} - -// @public (undocumented) -export interface DeidentifyTextHeaders { - "x-ms-client-request-id"?: string; -} - -// @public (undocumented) -export type DeidentifyTextParameters = DeidentifyTextHeaderParam & DeidentifyTextBodyParam & RequestParameters; - -// @public (undocumented) -export interface DeleteJob204Headers { - "x-ms-client-request-id"?: string; +export interface DeidentifyDocumentsOptionalParams extends OperationOptions { + clientRequestId?: string; + updateIntervalInMs?: number; } // @public -export interface DeleteJob204Response extends HttpResponse { - // (undocumented) - headers: RawHttpHeaders & DeleteJob204Headers; - // (undocumented) - status: "204"; +export interface DeidentifyTextOptionalParams extends OperationOptions { + clientRequestId?: string; } -// @public (undocumented) -export interface DeleteJobDefaultHeaders { - "x-ms-error-code"?: string; -} - -// @public (undocumented) -export interface DeleteJobDefaultResponse extends HttpResponse { - // (undocumented) - body: ErrorResponse; - // (undocumented) - headers: RawHttpHeaders & DeleteJobDefaultHeaders; - // (undocumented) - status: string; -} - -// @public (undocumented) -export interface DeleteJobHeaderParam { - // (undocumented) - headers?: RawHttpHeadersInput & DeleteJobHeaders; -} - -// @public (undocumented) -export interface DeleteJobHeaders { - "x-ms-client-request-id"?: string; -} - -// @public (undocumented) -export type DeleteJobParameters = DeleteJobHeaderParam & RequestParameters; - // @public -export type GetArrayType = T extends Array ? TData : never; - -// @public (undocumented) -export interface GetJob { - delete(options?: DeleteJobParameters): StreamableMethod; - get(options?: GetJobParameters): StreamableMethod; - put(options: DeidentifyDocumentsParameters): StreamableMethod; -} - -// @public (undocumented) -export interface GetJob200Headers { - "x-ms-client-request-id"?: string; +export interface DeleteJobOptionalParams extends OperationOptions { + clientRequestId?: string; } // @public -export interface GetJob200Response extends HttpResponse { - // (undocumented) - body: DeidentificationJobOutput; - // (undocumented) - headers: RawHttpHeaders & GetJob200Headers; - // (undocumented) - status: "200"; -} - -// @public (undocumented) -export interface GetJobDefaultHeaders { - "x-ms-error-code"?: string; -} - -// @public (undocumented) -export interface GetJobDefaultResponse extends HttpResponse { - // (undocumented) - body: ErrorResponse; - // (undocumented) - headers: RawHttpHeaders & GetJobDefaultHeaders; - // (undocumented) - status: string; -} - -// @public (undocumented) -export interface GetJobHeaderParam { - // (undocumented) - headers?: RawHttpHeadersInput & GetJobHeaders; -} - -// @public (undocumented) -export interface GetJobHeaders { - "x-ms-client-request-id"?: string; +export interface GetJobOptionalParams extends OperationOptions { + clientRequestId?: string; } -// @public (undocumented) -export type GetJobParameters = GetJobHeaderParam & RequestParameters; - -// @public -export function getLongRunningPoller(client: Client, initialResponse: DeidentifyDocuments200Response | DeidentifyDocuments201Response | DeidentifyDocumentsDefaultResponse, options?: CreateHttpPollerOptions>): Promise, TResult>>; - // @public -export type GetPage = (pageLink: string) => Promise<{ - page: TPage; - nextPageLink?: string; -}>; - -// @public (undocumented) -export function isUnexpected(response: GetJob200Response | GetJobDefaultResponse): response is GetJobDefaultResponse; - -// @public (undocumented) -export function isUnexpected(response: DeidentifyDocuments200Response | DeidentifyDocuments201Response | DeidentifyDocumentsLogicalResponse | DeidentifyDocumentsDefaultResponse): response is DeidentifyDocumentsDefaultResponse; - -// @public (undocumented) -export function isUnexpected(response: DeleteJob204Response | DeleteJobDefaultResponse): response is DeleteJobDefaultResponse; - -// @public (undocumented) -export function isUnexpected(response: ListJobs200Response | ListJobsDefaultResponse): response is ListJobsDefaultResponse; - -// @public (undocumented) -export function isUnexpected(response: ListJobDocuments200Response | ListJobDocumentsDefaultResponse): response is ListJobDocumentsDefaultResponse; - -// @public (undocumented) -export function isUnexpected(response: CancelJob200Response | CancelJobDefaultResponse): response is CancelJobDefaultResponse; - -// @public (undocumented) -export function isUnexpected(response: DeidentifyText200Response | DeidentifyTextDefaultResponse): response is DeidentifyTextDefaultResponse; - -// @public (undocumented) -export interface ListJobDocuments { - get(options?: ListJobDocumentsParameters): StreamableMethod; -} - -// @public (undocumented) -export interface ListJobDocuments200Headers { - "x-ms-client-request-id"?: string; +export enum KnownVersions { + V20241115 = "2024-11-15", + V20250715Preview = "2025-07-15-preview" } // @public -export interface ListJobDocuments200Response extends HttpResponse { - // (undocumented) - body: PagedDeidentificationDocumentDetailsOutput; - // (undocumented) - headers: RawHttpHeaders & ListJobDocuments200Headers; - // (undocumented) - status: "200"; -} - -// @public (undocumented) -export interface ListJobDocumentsDefaultHeaders { - "x-ms-error-code"?: string; -} - -// @public (undocumented) -export interface ListJobDocumentsDefaultResponse extends HttpResponse { - // (undocumented) - body: ErrorResponse; - // (undocumented) - headers: RawHttpHeaders & ListJobDocumentsDefaultHeaders; - // (undocumented) - status: string; -} - -// @public (undocumented) -export interface ListJobDocumentsHeaderParam { - // (undocumented) - headers?: RawHttpHeadersInput & ListJobDocumentsHeaders; -} - -// @public (undocumented) -export interface ListJobDocumentsHeaders { - "x-ms-client-request-id"?: string; -} - -// @public (undocumented) -export type ListJobDocumentsParameters = ListJobDocumentsQueryParam & ListJobDocumentsHeaderParam & RequestParameters; - -// @public (undocumented) -export interface ListJobDocumentsQueryParam { - // (undocumented) - queryParameters?: ListJobDocumentsQueryParamProperties; -} - -// @public (undocumented) -export interface ListJobDocumentsQueryParamProperties { +export interface ListJobDocumentsOptionalParams extends OperationOptions { + clientRequestId?: string; continuationToken?: string; maxpagesize?: number; } -// @public (undocumented) -export interface ListJobs { - get(options?: ListJobsParameters): StreamableMethod; -} - -// @public (undocumented) -export interface ListJobs200Headers { - "x-ms-client-request-id"?: string; -} - // @public -export interface ListJobs200Response extends HttpResponse { - // (undocumented) - body: PagedDeidentificationJobOutput; - // (undocumented) - headers: RawHttpHeaders & ListJobs200Headers; - // (undocumented) - status: "200"; -} - -// @public (undocumented) -export interface ListJobsDefaultHeaders { - "x-ms-error-code"?: string; -} - -// @public (undocumented) -export interface ListJobsDefaultResponse extends HttpResponse { - // (undocumented) - body: ErrorResponse; - // (undocumented) - headers: RawHttpHeaders & ListJobsDefaultHeaders; - // (undocumented) - status: string; -} - -// @public (undocumented) -export interface ListJobsHeaderParam { - // (undocumented) - headers?: RawHttpHeadersInput & ListJobsHeaders; -} - -// @public (undocumented) -export interface ListJobsHeaders { - "x-ms-client-request-id"?: string; -} - -// @public (undocumented) -export type ListJobsParameters = ListJobsQueryParam & ListJobsHeaderParam & RequestParameters; - -// @public (undocumented) -export interface ListJobsQueryParam { - // (undocumented) - queryParameters?: ListJobsQueryParamProperties; -} - -// @public (undocumented) -export interface ListJobsQueryParamProperties { +export interface ListJobsOptionalParams extends OperationOptions { + clientRequestId?: string; continuationToken?: string; maxpagesize?: number; } // @public -export type OperationState = string; - -// @public -export type OperationStateOutput = string; +export type OperationState = "NotStarted" | "Running" | "Succeeded" | "Failed" | "Canceled"; // @public -export interface PagedAsyncIterableIterator { +export interface PagedAsyncIterableIterator { [Symbol.asyncIterator](): PagedAsyncIterableIterator; - byPage: (settings?: TPageSettings) => AsyncIterableIterator; + byPage: (settings?: TPageSettings) => AsyncIterableIterator>; next(): Promise>; } -// @public -export interface PagedDeidentificationDocumentDetailsOutput { - nextLink?: string; - value: Array; -} - -// @public -export interface PagedDeidentificationJobOutput { - nextLink?: string; - value: Array; -} - // @public export interface PageSettings { continuationToken?: string; } // @public -export function paginate(client: Client, initialResponse: TResponse, options?: PagingOptions): PagedAsyncIterableIterator>; - -// @public -export type PaginateReturn = TResult extends { - body: { - value?: infer TPage; - }; -} ? GetArrayType : Array; - -// @public -export interface PagingOptions { - customGetPage?: GetPage[]>; -} +export type PhiCategory = "Unknown" | "Account" | "Age" | "BioID" | "City" | "CountryOrRegion" | "Date" | "Device" | "Doctor" | "Email" | "Fax" | "HealthPlan" | "Hospital" | "IDNum" | "IPAddress" | "License" | "LocationOther" | "MedicalRecord" | "Organization" | "Patient" | "Phone" | "Profession" | "SocialSecurity" | "State" | "Street" | "Url" | "Username" | "Vehicle" | "Zip"; // @public -export type PhiCategory = string; - -// @public -export type PhiCategoryOutput = string; - -// @public -export interface PhiEntityOutput { - category: PhiCategoryOutput; +export interface PhiEntity { + category: PhiCategory; confidenceScore?: number; - length: StringIndexOutput; - offset: StringIndexOutput; + length: StringIndex; + offset: StringIndex; text?: string; } // @public -export interface PhiTaggerResultOutput { - entities: Array; +export interface PhiTaggerResult { + entities: PhiEntity[]; } +// @public +export function restorePoller(client: DeidentificationClient, serializedState: string, sourceOperation: (...args: any[]) => PollerLike, TResult>, options?: RestorePollerOptions): PollerLike, TResult>; + // @public (undocumented) -export interface Routes { - (path: "/jobs/{name}", name: string): GetJob; - (path: "/jobs"): ListJobs; - (path: "/jobs/{name}/documents", name: string): ListJobDocuments; - (path: "/jobs/{name}:cancel", name: string): CancelJob; - (path: "/deid"): DeidentifyText; +export interface RestorePollerOptions extends OperationOptions { + abortSignal?: AbortSignalLike; + processResponseBody?: (result: TResponse) => Promise; + updateIntervalInMs?: number; } // @public @@ -638,28 +203,6 @@ export interface SimplePhiEntity { text?: string; } -// @public -export interface SimplePollerLike, TResult> { - getOperationState(): TState; - getResult(): TResult | undefined; - isDone(): boolean; - // @deprecated - isStopped(): boolean; - onProgress(callback: (state: TState) => void): CancelOnProgress; - poll(options?: { - abortSignal?: AbortSignalLike; - }): Promise; - pollUntilDone(pollOptions?: { - abortSignal?: AbortSignalLike; - }): Promise; - serialize(): Promise; - // @deprecated - stopPolling(): void; - submitted(): Promise; - // @deprecated - toString(): string; -} - // @public export interface SourceStorageLocation { extensions?: string[]; @@ -668,14 +211,7 @@ export interface SourceStorageLocation { } // @public -export interface SourceStorageLocationOutput { - extensions?: string[]; - location: string; - prefix: string; -} - -// @public -export interface StringIndexOutput { +export interface StringIndex { codePoint: number; utf16: number; utf8: number; @@ -684,7 +220,7 @@ export interface StringIndexOutput { // @public export interface TaggedPhiEntities { encoding: TextEncodingType; - entities: Array; + entities: SimplePhiEntity[]; } // @public @@ -695,14 +231,7 @@ export interface TargetStorageLocation { } // @public -export interface TargetStorageLocationOutput { - location: string; - overwrite?: boolean; - prefix: string; -} - -// @public -export type TextEncodingType = string; +export type TextEncodingType = "Utf8" | "Utf16" | "CodePoint"; // (No @packageDocumentation comment for this package) diff --git a/sdk/healthdataaiservices/health-deidentification-rest/sample.env b/sdk/healthdataaiservices/health-deidentification-rest/sample.env index 2ff41595523b..508439fc7d62 100644 --- a/sdk/healthdataaiservices/health-deidentification-rest/sample.env +++ b/sdk/healthdataaiservices/health-deidentification-rest/sample.env @@ -1,5 +1 @@ -# Your De-identification Service URL can be found in the overview section of the Azure Portal. -DEID_SERVICE_ENDPOINT= -# Storage account and container name -STORAGE_ACCOUNT_NAME= -STORAGE_CONTAINER_NAME= +# Feel free to add your own environment variables. \ No newline at end of file diff --git a/sdk/healthdataaiservices/health-deidentification-rest/samples-dev/createJob.ts b/sdk/healthdataaiservices/health-deidentification-rest/samples-dev/createJob.ts index 2375e16c6423..fd778e1c4900 100644 --- a/sdk/healthdataaiservices/health-deidentification-rest/samples-dev/createJob.ts +++ b/sdk/healthdataaiservices/health-deidentification-rest/samples-dev/createJob.ts @@ -5,8 +5,8 @@ * @summary This sample demonstrates how to create a job which will deidentify all files within a specific folder of a blob storage container. */ +import type { DeidentificationJob } from "@azure-rest/health-deidentification"; import DeidentificationClient, { - DeidentificationJob, getLongRunningPoller, isUnexpected, } from "@azure-rest/health-deidentification"; diff --git a/sdk/healthdataaiservices/health-deidentification-rest/samples-dev/helloWorld.ts b/sdk/healthdataaiservices/health-deidentification-rest/samples-dev/helloWorld.ts index c39763f36a40..dbc8c44a468d 100644 --- a/sdk/healthdataaiservices/health-deidentification-rest/samples-dev/helloWorld.ts +++ b/sdk/healthdataaiservices/health-deidentification-rest/samples-dev/helloWorld.ts @@ -5,10 +5,8 @@ * @summary This sample demonstrates how to create a `DeidentificationClient` and then deidentify a `string` */ -import createClient, { - DeidentificationContent, - isUnexpected, -} from "@azure-rest/health-deidentification"; +import type { DeidentificationContent } from "@azure-rest/health-deidentification"; +import createClient, { isUnexpected } from "@azure-rest/health-deidentification"; import { DefaultAzureCredential } from "@azure/identity"; import "dotenv/config"; diff --git a/sdk/healthdataaiservices/health-deidentification-rest/samples-dev/listCompletedFiles.ts b/sdk/healthdataaiservices/health-deidentification-rest/samples-dev/listCompletedFiles.ts index e273a4d94ca2..0d78a9a96a52 100644 --- a/sdk/healthdataaiservices/health-deidentification-rest/samples-dev/listCompletedFiles.ts +++ b/sdk/healthdataaiservices/health-deidentification-rest/samples-dev/listCompletedFiles.ts @@ -5,11 +5,8 @@ * @summary This sample demonstrates how to list files that were completed by a job. */ -import createClient, { - DeidentificationJob, - isUnexpected, - paginate, -} from "@azure-rest/health-deidentification"; +import type { DeidentificationJob } from "@azure-rest/health-deidentification"; +import createClient, { isUnexpected, paginate } from "@azure-rest/health-deidentification"; import { DefaultAzureCredential } from "@azure/identity"; import "dotenv/config"; diff --git a/sdk/healthdataaiservices/health-deidentification-rest/src/api/deidentificationContext.ts b/sdk/healthdataaiservices/health-deidentification-rest/src/api/deidentificationContext.ts new file mode 100644 index 000000000000..62d7c7a3e517 --- /dev/null +++ b/sdk/healthdataaiservices/health-deidentification-rest/src/api/deidentificationContext.ts @@ -0,0 +1,61 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +import { logger } from "../logger.js"; +import { KnownVersions } from "../models/models.js"; +import type { Client, ClientOptions } from "@azure-rest/core-client"; +import { getClient } from "@azure-rest/core-client"; +import type { TokenCredential } from "@azure/core-auth"; + +export interface DeidentificationContext extends Client { + /** The API version to use for this operation. */ + /** Known values of {@link KnownVersions} that the service accepts. */ + apiVersion: string; +} + +/** Optional parameters for the client. */ +export interface DeidentificationClientOptionalParams extends ClientOptions { + /** The API version to use for this operation. */ + /** Known values of {@link KnownVersions} that the service accepts. */ + apiVersion?: string; +} + +export function createDeidentification( + endpointParam: string, + credential: TokenCredential, + options: DeidentificationClientOptionalParams = {}, +): DeidentificationContext { + const endpointUrl = options.endpoint ?? String(endpointParam); + const prefixFromOptions = options?.userAgentOptions?.userAgentPrefix; + const userAgentInfo = `azsdk-js-health-deidentification/1.0.0-beta.1`; + const userAgentPrefix = prefixFromOptions + ? `${prefixFromOptions} azsdk-js-api ${userAgentInfo}` + : `azsdk-js-api ${userAgentInfo}`; + const { apiVersion: _, ...updatedOptions } = { + ...options, + userAgentOptions: { userAgentPrefix }, + loggingOptions: { logger: options.loggingOptions?.logger ?? logger.info }, + credentials: { + scopes: options.credentials?.scopes ?? ["https://deid.azure.com/.default"], + }, + }; + const clientContext = getClient(endpointUrl, credential, updatedOptions); + clientContext.pipeline.removePolicy({ name: "ApiVersionPolicy" }); + const apiVersion = options.apiVersion ?? "2025-07-15-preview"; + clientContext.pipeline.addPolicy({ + name: "ClientApiVersionPolicy", + sendRequest: (req, next) => { + // Use the apiVersion defined in request url directly + // Append one if there is no apiVersion and we have one at client options + const url = new URL(req.url); + if (!url.searchParams.get("api-version")) { + req.url = `${req.url}${ + Array.from(url.searchParams.keys()).length > 0 ? "&" : "?" + }api-version=${apiVersion}`; + } + + return next(req); + }, + }); + return { ...clientContext, apiVersion } as DeidentificationContext; +} diff --git a/sdk/healthdataaiservices/health-deidentification-rest/src/api/index.ts b/sdk/healthdataaiservices/health-deidentification-rest/src/api/index.ts new file mode 100644 index 000000000000..59182680fed0 --- /dev/null +++ b/sdk/healthdataaiservices/health-deidentification-rest/src/api/index.ts @@ -0,0 +1,26 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +export { + createDeidentification, + DeidentificationContext, + DeidentificationClientOptionalParams, +} from "./deidentificationContext.js"; +export { + deidentifyText, + deleteJob, + cancelJob, + listJobDocuments, + listJobs, + deidentifyDocuments, + getJob, +} from "./operations.js"; +export { + DeidentifyTextOptionalParams, + DeleteJobOptionalParams, + CancelJobOptionalParams, + ListJobDocumentsOptionalParams, + ListJobsOptionalParams, + DeidentifyDocumentsOptionalParams, + GetJobOptionalParams, +} from "./options.js"; diff --git a/sdk/healthdataaiservices/health-deidentification-rest/src/api/operations.ts b/sdk/healthdataaiservices/health-deidentification-rest/src/api/operations.ts new file mode 100644 index 000000000000..4da123c87212 --- /dev/null +++ b/sdk/healthdataaiservices/health-deidentification-rest/src/api/operations.ts @@ -0,0 +1,395 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +import type { DeidentificationContext as Client } from "./index.js"; +import type { + DeidentificationJob, + _PagedDeidentificationJob, + _PagedDeidentificationDocumentDetails, + DeidentificationDocumentDetails, + DeidentificationContent, + DeidentificationResult, +} from "../models/models.js"; +import { + deidentificationJobSerializer, + deidentificationJobDeserializer, + _pagedDeidentificationJobDeserializer, + _pagedDeidentificationDocumentDetailsDeserializer, + deidentificationContentSerializer, + deidentificationResultDeserializer, +} from "../models/models.js"; +import type { PagedAsyncIterableIterator } from "../static-helpers/pagingHelpers.js"; +import { buildPagedAsyncIterator } from "../static-helpers/pagingHelpers.js"; +import { getLongRunningPoller } from "../static-helpers/pollingHelpers.js"; +import { expandUrlTemplate } from "../static-helpers/urlTemplate.js"; +import type { + DeidentifyTextOptionalParams, + DeleteJobOptionalParams, + CancelJobOptionalParams, + ListJobDocumentsOptionalParams, + ListJobsOptionalParams, + DeidentifyDocumentsOptionalParams, + GetJobOptionalParams, +} from "./options.js"; +import type { StreamableMethod, PathUncheckedResponse } from "@azure-rest/core-client"; +import { createRestError, operationOptionsToRequestParameters } from "@azure-rest/core-client"; +import type { PollerLike, OperationState } from "@azure/core-lro"; + +export function _deidentifyTextSend( + context: Client, + body: DeidentificationContent, + options: DeidentifyTextOptionalParams = { requestOptions: {} }, +): StreamableMethod { + const path = expandUrlTemplate( + "/deid{?api%2Dversion}", + { + "api%2Dversion": context.apiVersion, + }, + { + allowReserved: options?.requestOptions?.skipUrlEncoding, + }, + ); + return context.path(path).post({ + ...operationOptionsToRequestParameters(options), + contentType: "application/json", + headers: { + ...(options?.clientRequestId !== undefined + ? { "x-ms-client-request-id": options?.clientRequestId } + : {}), + accept: "application/json", + ...options.requestOptions?.headers, + }, + body: deidentificationContentSerializer(body), + }); +} + +export async function _deidentifyTextDeserialize( + result: PathUncheckedResponse, +): Promise { + const expectedStatuses = ["200"]; + if (!expectedStatuses.includes(result.status)) { + throw createRestError(result); + } + + return deidentificationResultDeserializer(result.body); +} + +/** A remote procedure call (RPC) operation. */ +export async function deidentifyText( + context: Client, + body: DeidentificationContent, + options: DeidentifyTextOptionalParams = { requestOptions: {} }, +): Promise { + const result = await _deidentifyTextSend(context, body, options); + return _deidentifyTextDeserialize(result); +} + +export function _deleteJobSend( + context: Client, + name: string, + options: DeleteJobOptionalParams = { requestOptions: {} }, +): StreamableMethod { + const path = expandUrlTemplate( + "/jobs/{name}{?api%2Dversion}", + { + name: name, + "api%2Dversion": context.apiVersion, + }, + { + allowReserved: options?.requestOptions?.skipUrlEncoding, + }, + ); + return context.path(path).delete({ + ...operationOptionsToRequestParameters(options), + headers: { + ...(options?.clientRequestId !== undefined + ? { "x-ms-client-request-id": options?.clientRequestId } + : {}), + ...options.requestOptions?.headers, + }, + }); +} + +export async function _deleteJobDeserialize(result: PathUncheckedResponse): Promise { + const expectedStatuses = ["204"]; + if (!expectedStatuses.includes(result.status)) { + throw createRestError(result); + } + + return; +} + +/** Removes the record of the job from the service. Does not delete any documents. */ +export async function deleteJob( + context: Client, + name: string, + options: DeleteJobOptionalParams = { requestOptions: {} }, +): Promise { + const result = await _deleteJobSend(context, name, options); + return _deleteJobDeserialize(result); +} + +export function _cancelJobSend( + context: Client, + name: string, + options: CancelJobOptionalParams = { requestOptions: {} }, +): StreamableMethod { + const path = expandUrlTemplate( + "/jobs/{name}:cancel{?api%2Dversion}", + { + name: name, + "api%2Dversion": context.apiVersion, + }, + { + allowReserved: options?.requestOptions?.skipUrlEncoding, + }, + ); + return context.path(path).post({ + ...operationOptionsToRequestParameters(options), + headers: { + ...(options?.clientRequestId !== undefined + ? { "x-ms-client-request-id": options?.clientRequestId } + : {}), + accept: "application/json", + ...options.requestOptions?.headers, + }, + }); +} + +export async function _cancelJobDeserialize( + result: PathUncheckedResponse, +): Promise { + const expectedStatuses = ["200"]; + if (!expectedStatuses.includes(result.status)) { + throw createRestError(result); + } + + return deidentificationJobDeserializer(result.body); +} + +/** + * Cancels a job that is in progress. + * + * The job will be marked as canceled and the service will stop processing the job. The service will not delete any documents that have already been processed. + * + * If the job is already complete, this will have no effect. + */ +export async function cancelJob( + context: Client, + name: string, + options: CancelJobOptionalParams = { requestOptions: {} }, +): Promise { + const result = await _cancelJobSend(context, name, options); + return _cancelJobDeserialize(result); +} + +export function _listJobDocumentsSend( + context: Client, + name: string, + options: ListJobDocumentsOptionalParams = { requestOptions: {} }, +): StreamableMethod { + const path = expandUrlTemplate( + "/jobs/{name}/documents{?api%2Dversion,maxpagesize,continuationToken}", + { + name: name, + "api%2Dversion": context.apiVersion, + maxpagesize: options?.maxpagesize, + continuationToken: options?.continuationToken, + }, + { + allowReserved: options?.requestOptions?.skipUrlEncoding, + }, + ); + return context.path(path).get({ + ...operationOptionsToRequestParameters(options), + headers: { + ...(options?.clientRequestId !== undefined + ? { "x-ms-client-request-id": options?.clientRequestId } + : {}), + accept: "application/json", + ...options.requestOptions?.headers, + }, + }); +} + +export async function _listJobDocumentsDeserialize( + result: PathUncheckedResponse, +): Promise<_PagedDeidentificationDocumentDetails> { + const expectedStatuses = ["200"]; + if (!expectedStatuses.includes(result.status)) { + throw createRestError(result); + } + + return _pagedDeidentificationDocumentDetailsDeserializer(result.body); +} + +/** Resource list operation template. */ +export function listJobDocuments( + context: Client, + name: string, + options: ListJobDocumentsOptionalParams = { requestOptions: {} }, +): PagedAsyncIterableIterator { + return buildPagedAsyncIterator( + context, + () => _listJobDocumentsSend(context, name, options), + _listJobDocumentsDeserialize, + ["200"], + { itemName: "value", nextLinkName: "nextLink" }, + ); +} + +export function _listJobsSend( + context: Client, + options: ListJobsOptionalParams = { requestOptions: {} }, +): StreamableMethod { + const path = expandUrlTemplate( + "/jobs{?api%2Dversion,maxpagesize,continuationToken}", + { + "api%2Dversion": context.apiVersion, + maxpagesize: options?.maxpagesize, + continuationToken: options?.continuationToken, + }, + { + allowReserved: options?.requestOptions?.skipUrlEncoding, + }, + ); + return context.path(path).get({ + ...operationOptionsToRequestParameters(options), + headers: { + ...(options?.clientRequestId !== undefined + ? { "x-ms-client-request-id": options?.clientRequestId } + : {}), + accept: "application/json", + ...options.requestOptions?.headers, + }, + }); +} + +export async function _listJobsDeserialize( + result: PathUncheckedResponse, +): Promise<_PagedDeidentificationJob> { + const expectedStatuses = ["200"]; + if (!expectedStatuses.includes(result.status)) { + throw createRestError(result); + } + + return _pagedDeidentificationJobDeserializer(result.body); +} + +/** Resource list operation template. */ +export function listJobs( + context: Client, + options: ListJobsOptionalParams = { requestOptions: {} }, +): PagedAsyncIterableIterator { + return buildPagedAsyncIterator( + context, + () => _listJobsSend(context, options), + _listJobsDeserialize, + ["200"], + { itemName: "value", nextLinkName: "nextLink" }, + ); +} + +export function _deidentifyDocumentsSend( + context: Client, + name: string, + resource: DeidentificationJob, + options: DeidentifyDocumentsOptionalParams = { requestOptions: {} }, +): StreamableMethod { + const path = expandUrlTemplate( + "/jobs/{name}{?api%2Dversion}", + { + name: name, + "api%2Dversion": context.apiVersion, + }, + { + allowReserved: options?.requestOptions?.skipUrlEncoding, + }, + ); + return context.path(path).put({ + ...operationOptionsToRequestParameters(options), + contentType: "application/json", + headers: { + ...(options?.clientRequestId !== undefined + ? { "x-ms-client-request-id": options?.clientRequestId } + : {}), + accept: "application/json", + ...options.requestOptions?.headers, + }, + body: deidentificationJobSerializer(resource), + }); +} + +export async function _deidentifyDocumentsDeserialize( + result: PathUncheckedResponse, +): Promise { + const expectedStatuses = ["201", "200", "202"]; + if (!expectedStatuses.includes(result.status)) { + throw createRestError(result); + } + + return deidentificationJobDeserializer(result.body); +} + +/** Long-running resource create or replace operation template. */ +export function deidentifyDocuments( + context: Client, + name: string, + resource: DeidentificationJob, + options: DeidentifyDocumentsOptionalParams = { requestOptions: {} }, +): PollerLike, DeidentificationJob> { + return getLongRunningPoller(context, _deidentifyDocumentsDeserialize, ["201", "200", "202"], { + updateIntervalInMs: options?.updateIntervalInMs, + abortSignal: options?.abortSignal, + getInitialResponse: () => _deidentifyDocumentsSend(context, name, resource, options), + resourceLocationConfig: "original-uri", + }) as PollerLike, DeidentificationJob>; +} + +export function _getJobSend( + context: Client, + name: string, + options: GetJobOptionalParams = { requestOptions: {} }, +): StreamableMethod { + const path = expandUrlTemplate( + "/jobs/{name}{?api%2Dversion}", + { + name: name, + "api%2Dversion": context.apiVersion, + }, + { + allowReserved: options?.requestOptions?.skipUrlEncoding, + }, + ); + return context.path(path).get({ + ...operationOptionsToRequestParameters(options), + headers: { + ...(options?.clientRequestId !== undefined + ? { "x-ms-client-request-id": options?.clientRequestId } + : {}), + accept: "application/json", + ...options.requestOptions?.headers, + }, + }); +} + +export async function _getJobDeserialize( + result: PathUncheckedResponse, +): Promise { + const expectedStatuses = ["200"]; + if (!expectedStatuses.includes(result.status)) { + throw createRestError(result); + } + + return deidentificationJobDeserializer(result.body); +} + +/** Resource read operation template. */ +export async function getJob( + context: Client, + name: string, + options: GetJobOptionalParams = { requestOptions: {} }, +): Promise { + const result = await _getJobSend(context, name, options); + return _getJobDeserialize(result); +} diff --git a/sdk/healthdataaiservices/health-deidentification-rest/src/api/options.ts b/sdk/healthdataaiservices/health-deidentification-rest/src/api/options.ts new file mode 100644 index 000000000000..08d0f3d43a15 --- /dev/null +++ b/sdk/healthdataaiservices/health-deidentification-rest/src/api/options.ts @@ -0,0 +1,56 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +import type { OperationOptions } from "@azure-rest/core-client"; + +/** Optional parameters. */ +export interface DeidentifyTextOptionalParams extends OperationOptions { + /** An opaque, globally-unique, client-generated string identifier for the request. */ + clientRequestId?: string; +} + +/** Optional parameters. */ +export interface DeleteJobOptionalParams extends OperationOptions { + /** An opaque, globally-unique, client-generated string identifier for the request. */ + clientRequestId?: string; +} + +/** Optional parameters. */ +export interface CancelJobOptionalParams extends OperationOptions { + /** An opaque, globally-unique, client-generated string identifier for the request. */ + clientRequestId?: string; +} + +/** Optional parameters. */ +export interface ListJobDocumentsOptionalParams extends OperationOptions { + /** The maximum number of result items per page. */ + maxpagesize?: number; + /** Token to continue a previous query. */ + continuationToken?: string; + /** An opaque, globally-unique, client-generated string identifier for the request. */ + clientRequestId?: string; +} + +/** Optional parameters. */ +export interface ListJobsOptionalParams extends OperationOptions { + /** The maximum number of result items per page. */ + maxpagesize?: number; + /** Token to continue a previous query. */ + continuationToken?: string; + /** An opaque, globally-unique, client-generated string identifier for the request. */ + clientRequestId?: string; +} + +/** Optional parameters. */ +export interface DeidentifyDocumentsOptionalParams extends OperationOptions { + /** Delay to wait until next poll, in milliseconds. */ + updateIntervalInMs?: number; + /** An opaque, globally-unique, client-generated string identifier for the request. */ + clientRequestId?: string; +} + +/** Optional parameters. */ +export interface GetJobOptionalParams extends OperationOptions { + /** An opaque, globally-unique, client-generated string identifier for the request. */ + clientRequestId?: string; +} diff --git a/sdk/healthdataaiservices/health-deidentification-rest/src/clientDefinitions.ts b/sdk/healthdataaiservices/health-deidentification-rest/src/clientDefinitions.ts deleted file mode 100644 index 0c4ea8db8310..000000000000 --- a/sdk/healthdataaiservices/health-deidentification-rest/src/clientDefinitions.ts +++ /dev/null @@ -1,98 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -import type { - GetJobParameters, - DeidentifyDocumentsParameters, - DeleteJobParameters, - ListJobsParameters, - ListJobDocumentsParameters, - CancelJobParameters, - DeidentifyTextParameters, -} from "./parameters.js"; -import type { - GetJob200Response, - GetJobDefaultResponse, - DeidentifyDocuments200Response, - DeidentifyDocuments201Response, - DeidentifyDocumentsDefaultResponse, - DeleteJob204Response, - DeleteJobDefaultResponse, - ListJobs200Response, - ListJobsDefaultResponse, - ListJobDocuments200Response, - ListJobDocumentsDefaultResponse, - CancelJob200Response, - CancelJobDefaultResponse, - DeidentifyText200Response, - DeidentifyTextDefaultResponse, -} from "./responses.js"; -import type { Client, StreamableMethod } from "@azure-rest/core-client"; - -export interface GetJob { - /** Resource read operation template. */ - get(options?: GetJobParameters): StreamableMethod; - /** Long-running resource create or replace operation template. */ - put( - options: DeidentifyDocumentsParameters, - ): StreamableMethod< - | DeidentifyDocuments200Response - | DeidentifyDocuments201Response - | DeidentifyDocumentsDefaultResponse - >; - /** Removes the record of the job from the service. Does not delete any documents. */ - delete( - options?: DeleteJobParameters, - ): StreamableMethod; -} - -export interface ListJobs { - /** Resource list operation template. */ - get( - options?: ListJobsParameters, - ): StreamableMethod; -} - -export interface ListJobDocuments { - /** Resource list operation template. */ - get( - options?: ListJobDocumentsParameters, - ): StreamableMethod; -} - -export interface CancelJob { - /** - * Cancels a job that is in progress. - * - * The job will be marked as canceled and the service will stop processing the job. The service will not delete any documents that have already been processed. - * - * If the job is already complete, this will have no effect. - */ - post( - options?: CancelJobParameters, - ): StreamableMethod; -} - -export interface DeidentifyText { - /** A remote procedure call (RPC) operation. */ - post( - options: DeidentifyTextParameters, - ): StreamableMethod; -} - -export interface Routes { - /** Resource for '/jobs/\{name\}' has methods for the following verbs: get, put, delete */ - (path: "/jobs/{name}", name: string): GetJob; - /** Resource for '/jobs' has methods for the following verbs: get */ - (path: "/jobs"): ListJobs; - /** Resource for '/jobs/\{name\}/documents' has methods for the following verbs: get */ - (path: "/jobs/{name}/documents", name: string): ListJobDocuments; - /** Resource for '/jobs/\{name\}:cancel' has methods for the following verbs: post */ - (path: "/jobs/{name}:cancel", name: string): CancelJob; - /** Resource for '/deid' has methods for the following verbs: post */ - (path: "/deid"): DeidentifyText; -} - -export type DeidentificationClient = Client & { - path: Routes; -}; diff --git a/sdk/healthdataaiservices/health-deidentification-rest/src/deidentificationClient.ts b/sdk/healthdataaiservices/health-deidentification-rest/src/deidentificationClient.ts index 03a2135ab1cc..1280f3dd3a7e 100644 --- a/sdk/healthdataaiservices/health-deidentification-rest/src/deidentificationClient.ts +++ b/sdk/healthdataaiservices/health-deidentification-rest/src/deidentificationClient.ts @@ -1,65 +1,119 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -import type { ClientOptions } from "@azure-rest/core-client"; -import { getClient } from "@azure-rest/core-client"; -import { logger } from "./logger.js"; +import type { DeidentificationContext, DeidentificationClientOptionalParams } from "./api/index.js"; +import { createDeidentification } from "./api/index.js"; +import { + deidentifyText, + deleteJob, + cancelJob, + listJobDocuments, + listJobs, + deidentifyDocuments, + getJob, +} from "./api/operations.js"; +import type { + DeidentifyTextOptionalParams, + DeleteJobOptionalParams, + CancelJobOptionalParams, + ListJobDocumentsOptionalParams, + ListJobsOptionalParams, + DeidentifyDocumentsOptionalParams, + GetJobOptionalParams, +} from "./api/options.js"; +import type { + DeidentificationJob, + DeidentificationDocumentDetails, + DeidentificationContent, + DeidentificationResult, +} from "./models/models.js"; +import type { PagedAsyncIterableIterator } from "./static-helpers/pagingHelpers.js"; import type { TokenCredential } from "@azure/core-auth"; -import type { DeidentificationClient } from "./clientDefinitions.js"; +import type { PollerLike, OperationState } from "@azure/core-lro"; +import type { Pipeline } from "@azure/core-rest-pipeline"; -/** The optional parameters for the client */ -export interface DeidentificationClientOptions extends ClientOptions { - /** The api version option of the client */ - apiVersion?: string; -} +export { DeidentificationClientOptionalParams } from "./api/deidentificationContext.js"; + +export class DeidentificationClient { + private _client: DeidentificationContext; + /** The pipeline used by this client to make requests */ + public readonly pipeline: Pipeline; + + constructor( + endpointParam: string, + credential: TokenCredential, + options: DeidentificationClientOptionalParams = {}, + ) { + const prefixFromOptions = options?.userAgentOptions?.userAgentPrefix; + const userAgentPrefix = prefixFromOptions + ? `${prefixFromOptions} azsdk-js-client` + : `azsdk-js-client`; + this._client = createDeidentification(endpointParam, credential, { + ...options, + userAgentOptions: { userAgentPrefix }, + }); + this.pipeline = this._client.pipeline; + } + + /** A remote procedure call (RPC) operation. */ + deidentifyText( + body: DeidentificationContent, + options: DeidentifyTextOptionalParams = { requestOptions: {} }, + ): Promise { + return deidentifyText(this._client, body, options); + } + + /** Removes the record of the job from the service. Does not delete any documents. */ + deleteJob( + name: string, + options: DeleteJobOptionalParams = { requestOptions: {} }, + ): Promise { + return deleteJob(this._client, name, options); + } + + /** + * Cancels a job that is in progress. + * + * The job will be marked as canceled and the service will stop processing the job. The service will not delete any documents that have already been processed. + * + * If the job is already complete, this will have no effect. + */ + cancelJob( + name: string, + options: CancelJobOptionalParams = { requestOptions: {} }, + ): Promise { + return cancelJob(this._client, name, options); + } -/** - * Initialize a new instance of `DeidentificationClient` - * @param endpointParam - Url of your De-identification Service. - * @param credentials - uniquely identify client credential - * @param options - the parameter for all optional parameters - */ -export default function createClient( - endpointParam: string, - credentials: TokenCredential, - { apiVersion = "2025-07-15-preview", ...options }: DeidentificationClientOptions = {}, -): DeidentificationClient { - const endpointUrl = options.endpoint ?? `${endpointParam}`; - const userAgentInfo = `azsdk-js-health-deidentification-rest/1.0.0-beta.1`; - const userAgentPrefix = - options.userAgentOptions && options.userAgentOptions.userAgentPrefix - ? `${options.userAgentOptions.userAgentPrefix} ${userAgentInfo}` - : `${userAgentInfo}`; - options = { - ...options, - userAgentOptions: { - userAgentPrefix, - }, - loggingOptions: { - logger: options.loggingOptions?.logger ?? logger.info, - }, - credentials: { - scopes: options.credentials?.scopes ?? ["https://deid.azure.com/.default"], - }, - }; - const client = getClient(endpointUrl, credentials, options) as DeidentificationClient; + /** Resource list operation template. */ + listJobDocuments( + name: string, + options: ListJobDocumentsOptionalParams = { requestOptions: {} }, + ): PagedAsyncIterableIterator { + return listJobDocuments(this._client, name, options); + } - client.pipeline.removePolicy({ name: "ApiVersionPolicy" }); - client.pipeline.addPolicy({ - name: "ClientApiVersionPolicy", - sendRequest: (req, next) => { - // Use the apiVersion defined in request url directly - // Append one if there is no apiVersion and we have one at client options - const url = new URL(req.url); - if (!url.searchParams.get("api-version") && apiVersion) { - req.url = `${req.url}${ - Array.from(url.searchParams.keys()).length > 0 ? "&" : "?" - }api-version=${apiVersion}`; - } + /** Resource list operation template. */ + listJobs( + options: ListJobsOptionalParams = { requestOptions: {} }, + ): PagedAsyncIterableIterator { + return listJobs(this._client, options); + } - return next(req); - }, - }); + /** Long-running resource create or replace operation template. */ + deidentifyDocuments( + name: string, + resource: DeidentificationJob, + options: DeidentifyDocumentsOptionalParams = { requestOptions: {} }, + ): PollerLike, DeidentificationJob> { + return deidentifyDocuments(this._client, name, resource, options); + } - return client; + /** Resource read operation template. */ + getJob( + name: string, + options: GetJobOptionalParams = { requestOptions: {} }, + ): Promise { + return getJob(this._client, name, options); + } } diff --git a/sdk/healthdataaiservices/health-deidentification-rest/src/index.ts b/sdk/healthdataaiservices/health-deidentification-rest/src/index.ts index de1723896f8a..e05de862dbe0 100644 --- a/sdk/healthdataaiservices/health-deidentification-rest/src/index.ts +++ b/sdk/healthdataaiservices/health-deidentification-rest/src/index.ts @@ -1,16 +1,44 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -import DeidentificationClient from "./deidentificationClient.js"; +import { + PageSettings, + ContinuablePage, + PagedAsyncIterableIterator, +} from "./static-helpers/pagingHelpers.js"; -export * from "./deidentificationClient.js"; -export * from "./parameters.js"; -export * from "./responses.js"; -export * from "./clientDefinitions.js"; -export * from "./isUnexpected.js"; -export * from "./models.js"; -export * from "./outputModels.js"; -export * from "./paginateHelper.js"; -export * from "./pollingHelper.js"; - -export default DeidentificationClient; +export { DeidentificationClient } from "./deidentificationClient.js"; +export { restorePoller, RestorePollerOptions } from "./restorePollerHelpers.js"; +export { + DeidentificationJob, + DeidentificationOperationType, + SourceStorageLocation, + TargetStorageLocation, + DeidentificationJobCustomizationOptions, + OperationState, + DeidentificationJobSummary, + DeidentificationDocumentDetails, + DeidentificationDocumentLocation, + DeidentificationContent, + TaggedPhiEntities, + TextEncodingType, + SimplePhiEntity, + PhiCategory, + DeidentificationCustomizationOptions, + DeidentificationResult, + PhiTaggerResult, + PhiEntity, + StringIndex, + KnownVersions, +} from "./models/index.js"; +export { + DeidentificationClientOptionalParams, + DeidentifyTextOptionalParams, + DeleteJobOptionalParams, + CancelJobOptionalParams, + ListJobDocumentsOptionalParams, + ListJobsOptionalParams, + DeidentifyDocumentsOptionalParams, + GetJobOptionalParams, +} from "./api/index.js"; +export { PageSettings, ContinuablePage, PagedAsyncIterableIterator }; diff --git a/sdk/healthdataaiservices/health-deidentification-rest/src/isUnexpected.ts b/sdk/healthdataaiservices/health-deidentification-rest/src/isUnexpected.ts deleted file mode 100644 index fc34501e8af7..000000000000 --- a/sdk/healthdataaiservices/health-deidentification-rest/src/isUnexpected.ts +++ /dev/null @@ -1,158 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -import type { - GetJob200Response, - GetJobDefaultResponse, - DeidentifyDocuments200Response, - DeidentifyDocuments201Response, - DeidentifyDocumentsLogicalResponse, - DeidentifyDocumentsDefaultResponse, - DeleteJob204Response, - DeleteJobDefaultResponse, - ListJobs200Response, - ListJobsDefaultResponse, - ListJobDocuments200Response, - ListJobDocumentsDefaultResponse, - CancelJob200Response, - CancelJobDefaultResponse, - DeidentifyText200Response, - DeidentifyTextDefaultResponse, -} from "./responses.js"; - -const responseMap: Record = { - "GET /jobs/{name}": ["200"], - "PUT /jobs/{name}": ["200", "201"], - "DELETE /jobs/{name}": ["204"], - "GET /jobs": ["200"], - "GET /jobs/{name}/documents": ["200"], - "POST /jobs/{name}:cancel": ["200"], - "POST /deid": ["200"], -}; - -export function isUnexpected( - response: GetJob200Response | GetJobDefaultResponse, -): response is GetJobDefaultResponse; -export function isUnexpected( - response: - | DeidentifyDocuments200Response - | DeidentifyDocuments201Response - | DeidentifyDocumentsLogicalResponse - | DeidentifyDocumentsDefaultResponse, -): response is DeidentifyDocumentsDefaultResponse; -export function isUnexpected( - response: DeleteJob204Response | DeleteJobDefaultResponse, -): response is DeleteJobDefaultResponse; -export function isUnexpected( - response: ListJobs200Response | ListJobsDefaultResponse, -): response is ListJobsDefaultResponse; -export function isUnexpected( - response: ListJobDocuments200Response | ListJobDocumentsDefaultResponse, -): response is ListJobDocumentsDefaultResponse; -export function isUnexpected( - response: CancelJob200Response | CancelJobDefaultResponse, -): response is CancelJobDefaultResponse; -export function isUnexpected( - response: DeidentifyText200Response | DeidentifyTextDefaultResponse, -): response is DeidentifyTextDefaultResponse; -export function isUnexpected( - response: - | GetJob200Response - | GetJobDefaultResponse - | DeidentifyDocuments200Response - | DeidentifyDocuments201Response - | DeidentifyDocumentsLogicalResponse - | DeidentifyDocumentsDefaultResponse - | DeleteJob204Response - | DeleteJobDefaultResponse - | ListJobs200Response - | ListJobsDefaultResponse - | ListJobDocuments200Response - | ListJobDocumentsDefaultResponse - | CancelJob200Response - | CancelJobDefaultResponse - | DeidentifyText200Response - | DeidentifyTextDefaultResponse, -): response is - | GetJobDefaultResponse - | DeidentifyDocumentsDefaultResponse - | DeleteJobDefaultResponse - | ListJobsDefaultResponse - | ListJobDocumentsDefaultResponse - | CancelJobDefaultResponse - | DeidentifyTextDefaultResponse { - const lroOriginal = response.headers["x-ms-original-url"]; - const url = new URL(lroOriginal ?? response.request.url); - const method = response.request.method; - let pathDetails = responseMap[`${method} ${url.pathname}`]; - if (!pathDetails) { - pathDetails = getParametrizedPathSuccess(method, url.pathname); - } - return !pathDetails.includes(response.status); -} - -function getParametrizedPathSuccess(method: string, path: string): string[] { - const pathParts = path.split("/"); - - // Traverse list to match the longest candidate - // matchedLen: the length of candidate path - // matchedValue: the matched status code array - let matchedLen = -1, - matchedValue: string[] = []; - - // Iterate the responseMap to find a match - for (const [key, value] of Object.entries(responseMap)) { - // Extracting the path from the map key which is in format - // GET /path/foo - if (!key.startsWith(method)) { - continue; - } - const candidatePath = getPathFromMapKey(key); - // Get each part of the url path - const candidateParts = candidatePath.split("/"); - - // track if we have found a match to return the values found. - let found = true; - for (let i = candidateParts.length - 1, j = pathParts.length - 1; i >= 1 && j >= 1; i--, j--) { - if (candidateParts[i]?.startsWith("{") && candidateParts[i]?.indexOf("}") !== -1) { - const start = candidateParts[i]!.indexOf("}") + 1, - end = candidateParts[i]?.length; - // If the current part of the candidate is a "template" part - // Try to use the suffix of pattern to match the path - // {guid} ==> $ - // {guid}:export ==> :export$ - const isMatched = new RegExp(`${candidateParts[i]?.slice(start, end)}`).test( - pathParts[j] || "", - ); - - if (!isMatched) { - found = false; - break; - } - continue; - } - - // If the candidate part is not a template and - // the parts don't match mark the candidate as not found - // to move on with the next candidate path. - if (candidateParts[i] !== pathParts[j]) { - found = false; - break; - } - } - - // We finished evaluating the current candidate parts - // Update the matched value if and only if we found the longer pattern - if (found && candidatePath.length > matchedLen) { - matchedLen = candidatePath.length; - matchedValue = value; - } - } - - return matchedValue; -} - -function getPathFromMapKey(mapKey: string): string { - const pathStart = mapKey.indexOf("/"); - return mapKey.slice(pathStart); -} diff --git a/sdk/healthdataaiservices/health-deidentification-rest/src/models.ts b/sdk/healthdataaiservices/health-deidentification-rest/src/models.ts deleted file mode 100644 index cd2277dc4b37..000000000000 --- a/sdk/healthdataaiservices/health-deidentification-rest/src/models.ts +++ /dev/null @@ -1,140 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -/** A job containing a batch of documents to de-identify. */ -export interface DeidentificationJob { - /** - * Operation to perform on the input documents. - * - * Possible values: "Redact", "Surrogate", "Tag", "SurrogateOnly" - */ - operation?: DeidentificationOperationType; - /** Storage location to perform the operation on. */ - sourceLocation: SourceStorageLocation; - /** Target location to store output of operation. */ - targetLocation: TargetStorageLocation; - /** Customization parameters to override default service behaviors. */ - customizations?: DeidentificationJobCustomizationOptions; -} - -/** Storage location. */ -export interface SourceStorageLocation { - /** URL to storage location. */ - location: string; - /** Prefix to filter path by. */ - prefix: string; - /** List of extensions to filter path by. */ - extensions?: string[]; -} - -/** Storage location. */ -export interface TargetStorageLocation { - /** URL to storage location. */ - location: string; - /** - * Replaces the input prefix of a file path with the output prefix, preserving the rest of the path structure. - * - * Example: - * File full path: documents/user/note.txt - * Input Prefix: "documents/user/" - * Output Prefix: "output_docs/" - * - * Output file: "output_docs/note.txt" - */ - prefix: string; - /** When set to true during a job, the service will overwrite the output location if it already exists. */ - overwrite?: boolean; -} - -/** Customizations options to override default service behaviors for job usage. */ -export interface DeidentificationJobCustomizationOptions { - /** - * Format of the redacted output. Only valid when Operation is Redact. - * Please refer to https://learn.microsoft.com/azure/healthcare-apis/deidentification/redaction-format for more details. - */ - redactionFormat?: string; - /** Locale in which the output surrogates are written. */ - surrogateLocale?: string; - /** Locale of the input text. Used for better PHI detection. Defaults to 'en-US'. */ - inputLocale?: string; -} - -/** Summary metrics of a job. */ -export interface DeidentificationJobSummary { - /** Number of documents that have completed. */ - successful: number; - /** Number of documents that have failed. */ - failed: number; - /** Number of documents that have been canceled. */ - canceled: number; - /** Number of documents total. */ - total: number; - /** Number of bytes processed. */ - bytesProcessed: number; -} - -/** Request body for de-identification operation. */ -export interface DeidentificationContent { - /** Input text to de-identify. */ - inputText: string; - /** - * Operation to perform on the input documents. - * - * Possible values: "Redact", "Surrogate", "Tag", "SurrogateOnly" - */ - operation?: DeidentificationOperationType; - /** Grouped PHI entities with single encoding specification for SurrogateOnly operation. */ - taggedEntities?: TaggedPhiEntities; - /** Customization parameters to override default service behaviors. */ - customizations?: DeidentificationCustomizationOptions; -} - -/** Grouped PHI entities with shared encoding specification. */ -export interface TaggedPhiEntities { - /** - * The encoding type used for all entities in this group. - * - * Possible values: "Utf8", "Utf16", "CodePoint" - */ - encoding: TextEncodingType; - /** List of PHI entities using the specified encoding. */ - entities: Array; -} - -/** Simple PHI entity with encoding-specific offset and length values. */ -export interface SimplePhiEntity { - /** - * PHI Category of the entity. - * - * Possible values: "Unknown", "Account", "Age", "BioID", "City", "CountryOrRegion", "Date", "Device", "Doctor", "Email", "Fax", "HealthPlan", "Hospital", "IDNum", "IPAddress", "License", "LocationOther", "MedicalRecord", "Organization", "Patient", "Phone", "Profession", "SocialSecurity", "State", "Street", "Url", "Username", "Vehicle", "Zip" - */ - category: PhiCategory; - /** Starting index of the location from within the input text using the group's encoding. */ - offset: number; - /** Length of the input text using the group's encoding. */ - length: number; - /** Text of the entity (optional). */ - text?: string; -} - -/** Customizations options to override default service behaviors for synchronous usage. */ -export interface DeidentificationCustomizationOptions { - /** - * Format of the redacted output. Only valid when Operation is Redact. - * Please refer to https://learn.microsoft.com/azure/healthcare-apis/deidentification/redaction-format for more details. - */ - redactionFormat?: string; - /** Locale in which the output surrogates are written. */ - surrogateLocale?: string; - /** Locale of the input text. Used for better PHI detection. Defaults to 'en-US'. */ - inputLocale?: string; -} - -/** Alias for DeidentificationOperationType */ -export type DeidentificationOperationType = string; -/** Alias for OperationState */ -export type OperationState = string; -/** Alias for TextEncodingType */ -export type TextEncodingType = string; -/** Alias for PhiCategory */ -export type PhiCategory = string; diff --git a/sdk/healthdataaiservices/health-deidentification-rest/src/models/index.ts b/sdk/healthdataaiservices/health-deidentification-rest/src/models/index.ts new file mode 100644 index 000000000000..58b7a0dd3a5d --- /dev/null +++ b/sdk/healthdataaiservices/health-deidentification-rest/src/models/index.ts @@ -0,0 +1,25 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +export { + DeidentificationJob, + DeidentificationOperationType, + SourceStorageLocation, + TargetStorageLocation, + DeidentificationJobCustomizationOptions, + OperationState, + DeidentificationJobSummary, + DeidentificationDocumentDetails, + DeidentificationDocumentLocation, + DeidentificationContent, + TaggedPhiEntities, + TextEncodingType, + SimplePhiEntity, + PhiCategory, + DeidentificationCustomizationOptions, + DeidentificationResult, + PhiTaggerResult, + PhiEntity, + StringIndex, + KnownVersions, +} from "./models.js"; diff --git a/sdk/healthdataaiservices/health-deidentification-rest/src/models/models.ts b/sdk/healthdataaiservices/health-deidentification-rest/src/models/models.ts new file mode 100644 index 000000000000..e212d397ee1d --- /dev/null +++ b/sdk/healthdataaiservices/health-deidentification-rest/src/models/models.ts @@ -0,0 +1,514 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +import type { ErrorModel } from "@azure-rest/core-client"; + +/** A job containing a batch of documents to de-identify. */ +export interface DeidentificationJob { + /** The name of a job. */ + readonly name: string; + /** Operation to perform on the input documents. */ + operation?: DeidentificationOperationType; + /** Storage location to perform the operation on. */ + sourceLocation: SourceStorageLocation; + /** Target location to store output of operation. */ + targetLocation: TargetStorageLocation; + /** Customization parameters to override default service behaviors. */ + customizations?: DeidentificationJobCustomizationOptions; + /** Current status of a job. */ + readonly status: OperationState; + /** Error when job fails in it's entirety. */ + readonly error?: ErrorModel; + /** + * Date and time when the job was completed. + * + * If the job is canceled, this is the time when the job was canceled. + * + * If the job failed, this is the time when the job failed. + */ + readonly lastUpdatedAt: Date; + /** Date and time when the job was created. */ + readonly createdAt: Date; + /** Date and time when the job was started. */ + readonly startedAt?: Date; + /** Summary of a job. Exists only when the job is completed. */ + readonly summary?: DeidentificationJobSummary; +} + +export function deidentificationJobSerializer(item: DeidentificationJob): any { + return { + operation: item["operation"], + sourceLocation: sourceStorageLocationSerializer(item["sourceLocation"]), + targetLocation: targetStorageLocationSerializer(item["targetLocation"]), + customizations: !item["customizations"] + ? item["customizations"] + : deidentificationJobCustomizationOptionsSerializer(item["customizations"]), + }; +} + +export function deidentificationJobDeserializer(item: any): DeidentificationJob { + return { + name: item["name"], + operation: item["operation"], + sourceLocation: sourceStorageLocationDeserializer(item["sourceLocation"]), + targetLocation: targetStorageLocationDeserializer(item["targetLocation"]), + customizations: !item["customizations"] + ? item["customizations"] + : deidentificationJobCustomizationOptionsDeserializer(item["customizations"]), + status: item["status"], + error: !item["error"] ? item["error"] : item["error"], + lastUpdatedAt: new Date(item["lastUpdatedAt"]), + createdAt: new Date(item["createdAt"]), + startedAt: !item["startedAt"] ? item["startedAt"] : new Date(item["startedAt"]), + summary: !item["summary"] + ? item["summary"] + : deidentificationJobSummaryDeserializer(item["summary"]), + }; +} + +/** Enum of supported Operation Types. */ +export type DeidentificationOperationType = "Redact" | "Surrogate" | "Tag" | "SurrogateOnly"; + +/** Storage location. */ +export interface SourceStorageLocation { + /** URL to storage location. */ + location: string; + /** Prefix to filter path by. */ + prefix: string; + /** List of extensions to filter path by. */ + extensions?: string[]; +} + +export function sourceStorageLocationSerializer(item: SourceStorageLocation): any { + return { + location: item["location"], + prefix: item["prefix"], + extensions: !item["extensions"] + ? item["extensions"] + : item["extensions"].map((p: any) => { + return p; + }), + }; +} + +export function sourceStorageLocationDeserializer(item: any): SourceStorageLocation { + return { + location: item["location"], + prefix: item["prefix"], + extensions: !item["extensions"] + ? item["extensions"] + : item["extensions"].map((p: any) => { + return p; + }), + }; +} + +/** Storage location. */ +export interface TargetStorageLocation { + /** URL to storage location. */ + location: string; + /** + * Replaces the input prefix of a file path with the output prefix, preserving the rest of the path structure. + * + * Example: + * File full path: documents/user/note.txt + * Input Prefix: "documents/user/" + * Output Prefix: "output_docs/" + * + * Output file: "output_docs/note.txt" + */ + prefix: string; + /** When set to true during a job, the service will overwrite the output location if it already exists. */ + overwrite?: boolean; +} + +export function targetStorageLocationSerializer(item: TargetStorageLocation): any { + return { + location: item["location"], + prefix: item["prefix"], + overwrite: item["overwrite"], + }; +} + +export function targetStorageLocationDeserializer(item: any): TargetStorageLocation { + return { + location: item["location"], + prefix: item["prefix"], + overwrite: item["overwrite"], + }; +} + +/** Customizations options to override default service behaviors for job usage. */ +export interface DeidentificationJobCustomizationOptions { + /** + * Format of the redacted output. Only valid when Operation is Redact. + * Please refer to https://learn.microsoft.com/azure/healthcare-apis/deidentification/redaction-format for more details. + */ + redactionFormat?: string; + /** Locale in which the output surrogates are written. */ + surrogateLocale?: string; + /** Locale of the input text. Used for better PHI detection. Defaults to 'en-US'. */ + inputLocale?: string; +} + +export function deidentificationJobCustomizationOptionsSerializer( + item: DeidentificationJobCustomizationOptions, +): any { + return { + redactionFormat: item["redactionFormat"], + surrogateLocale: item["surrogateLocale"], + inputLocale: item["inputLocale"], + }; +} + +export function deidentificationJobCustomizationOptionsDeserializer( + item: any, +): DeidentificationJobCustomizationOptions { + return { + redactionFormat: item["redactionFormat"], + surrogateLocale: item["surrogateLocale"], + inputLocale: item["inputLocale"], + }; +} + +/** Enum describing allowed operation states. */ +export type OperationState = "NotStarted" | "Running" | "Succeeded" | "Failed" | "Canceled"; + +/** Summary metrics of a job. */ +export interface DeidentificationJobSummary { + /** Number of documents that have completed. */ + successful: number; + /** Number of documents that have failed. */ + failed: number; + /** Number of documents that have been canceled. */ + canceled: number; + /** Number of documents total. */ + total: number; + /** Number of bytes processed. */ + bytesProcessed: number; +} + +export function deidentificationJobSummaryDeserializer(item: any): DeidentificationJobSummary { + return { + successful: item["successful"], + failed: item["failed"], + canceled: item["canceled"], + total: item["total"], + bytesProcessed: item["bytesProcessed"], + }; +} + +/** Paged collection of DeidentificationJob items */ +export interface _PagedDeidentificationJob { + /** The DeidentificationJob items on this page */ + value: DeidentificationJob[]; + /** The link to the next page of items */ + nextLink?: string; +} + +export function _pagedDeidentificationJobDeserializer(item: any): _PagedDeidentificationJob { + return { + value: deidentificationJobArrayDeserializer(item["value"]), + nextLink: item["nextLink"], + }; +} + +export function deidentificationJobArraySerializer(result: Array): any[] { + return result.map((item) => { + return deidentificationJobSerializer(item); + }); +} + +export function deidentificationJobArrayDeserializer(result: Array): any[] { + return result.map((item) => { + return deidentificationJobDeserializer(item); + }); +} + +/** Paged collection of DeidentificationDocumentDetails items */ +export interface _PagedDeidentificationDocumentDetails { + /** The DeidentificationDocumentDetails items on this page */ + value: DeidentificationDocumentDetails[]; + /** The link to the next page of items */ + nextLink?: string; +} + +export function _pagedDeidentificationDocumentDetailsDeserializer( + item: any, +): _PagedDeidentificationDocumentDetails { + return { + value: deidentificationDocumentDetailsArrayDeserializer(item["value"]), + nextLink: item["nextLink"], + }; +} + +export function deidentificationDocumentDetailsArrayDeserializer( + result: Array, +): any[] { + return result.map((item) => { + return deidentificationDocumentDetailsDeserializer(item); + }); +} + +/** Details of a single document in a job. */ +export interface DeidentificationDocumentDetails { + /** Id of the document details. */ + readonly id: string; + /** Location for the input. */ + input: DeidentificationDocumentLocation; + /** Location for the output. */ + output?: DeidentificationDocumentLocation; + /** Status of the document. */ + status: OperationState; + /** Error when document fails. */ + error?: ErrorModel; +} + +export function deidentificationDocumentDetailsDeserializer( + item: any, +): DeidentificationDocumentDetails { + return { + id: item["id"], + input: deidentificationDocumentLocationDeserializer(item["input"]), + output: !item["output"] + ? item["output"] + : deidentificationDocumentLocationDeserializer(item["output"]), + status: item["status"], + error: !item["error"] ? item["error"] : item["error"], + }; +} + +/** Location of a document. */ +export interface DeidentificationDocumentLocation { + /** Location of document in storage. */ + location: string; + /** The entity tag for this resource. */ + readonly etag: string; +} + +export function deidentificationDocumentLocationDeserializer( + item: any, +): DeidentificationDocumentLocation { + return { + location: item["location"], + etag: item["etag"], + }; +} + +/** Request body for de-identification operation. */ +export interface DeidentificationContent { + /** Input text to de-identify. */ + inputText: string; + /** Operation to perform on the input documents. */ + operation?: DeidentificationOperationType; + /** Grouped PHI entities with single encoding specification for SurrogateOnly operation. */ + taggedEntities?: TaggedPhiEntities; + /** Customization parameters to override default service behaviors. */ + customizations?: DeidentificationCustomizationOptions; +} + +export function deidentificationContentSerializer(item: DeidentificationContent): any { + return { + inputText: item["inputText"], + operation: item["operation"], + taggedEntities: !item["taggedEntities"] + ? item["taggedEntities"] + : taggedPhiEntitiesSerializer(item["taggedEntities"]), + customizations: !item["customizations"] + ? item["customizations"] + : deidentificationCustomizationOptionsSerializer(item["customizations"]), + }; +} + +/** Grouped PHI entities with shared encoding specification. */ +export interface TaggedPhiEntities { + /** The encoding type used for all entities in this group. */ + encoding: TextEncodingType; + /** List of PHI entities using the specified encoding. */ + entities: SimplePhiEntity[]; +} + +export function taggedPhiEntitiesSerializer(item: TaggedPhiEntities): any { + return { + encoding: item["encoding"], + entities: simplePhiEntityArraySerializer(item["entities"]), + }; +} + +/** Encoding type for text offset and length calculations. */ +export type TextEncodingType = "Utf8" | "Utf16" | "CodePoint"; + +export function simplePhiEntityArraySerializer(result: Array): any[] { + return result.map((item) => { + return simplePhiEntitySerializer(item); + }); +} + +/** Simple PHI entity with encoding-specific offset and length values. */ +export interface SimplePhiEntity { + /** PHI Category of the entity. */ + category: PhiCategory; + /** Starting index of the location from within the input text using the group's encoding. */ + offset: number; + /** Length of the input text using the group's encoding. */ + length: number; + /** Text of the entity (optional). */ + text?: string; +} + +export function simplePhiEntitySerializer(item: SimplePhiEntity): any { + return { + category: item["category"], + offset: item["offset"], + length: item["length"], + text: item["text"], + }; +} + +/** List of PHI Entities. */ +export type PhiCategory = + | "Unknown" + | "Account" + | "Age" + | "BioID" + | "City" + | "CountryOrRegion" + | "Date" + | "Device" + | "Doctor" + | "Email" + | "Fax" + | "HealthPlan" + | "Hospital" + | "IDNum" + | "IPAddress" + | "License" + | "LocationOther" + | "MedicalRecord" + | "Organization" + | "Patient" + | "Phone" + | "Profession" + | "SocialSecurity" + | "State" + | "Street" + | "Url" + | "Username" + | "Vehicle" + | "Zip"; + +/** Customizations options to override default service behaviors for synchronous usage. */ +export interface DeidentificationCustomizationOptions { + /** + * Format of the redacted output. Only valid when Operation is Redact. + * Please refer to https://learn.microsoft.com/azure/healthcare-apis/deidentification/redaction-format for more details. + */ + redactionFormat?: string; + /** Locale in which the output surrogates are written. */ + surrogateLocale?: string; + /** Locale of the input text. Used for better PHI detection. Defaults to 'en-US'. */ + inputLocale?: string; +} + +export function deidentificationCustomizationOptionsSerializer( + item: DeidentificationCustomizationOptions, +): any { + return { + redactionFormat: item["redactionFormat"], + surrogateLocale: item["surrogateLocale"], + inputLocale: item["inputLocale"], + }; +} + +/** Response body for de-identification operation. */ +export interface DeidentificationResult { + /** Output text after de-identification. Not available for "Tag" operation. */ + outputText?: string; + /** Result of the "Tag" operation. Only available for "Tag" Operation. */ + taggerResult?: PhiTaggerResult; +} + +export function deidentificationResultDeserializer(item: any): DeidentificationResult { + return { + outputText: item["outputText"], + taggerResult: !item["taggerResult"] + ? item["taggerResult"] + : phiTaggerResultDeserializer(item["taggerResult"]), + }; +} + +/** Result of the "Tag" operation. */ +export interface PhiTaggerResult { + /** List of entities detected in the input. */ + entities: PhiEntity[]; +} + +export function phiTaggerResultDeserializer(item: any): PhiTaggerResult { + return { + entities: phiEntityArrayDeserializer(item["entities"]), + }; +} + +export function phiEntityArrayDeserializer(result: Array): any[] { + return result.map((item) => { + return phiEntityDeserializer(item); + }); +} + +/** PHI Entity tag in the input. */ +export interface PhiEntity { + /** PHI Category of the entity. */ + category: PhiCategory; + /** Starting index of the location from within the input text. */ + offset: StringIndex; + /** Length of the input text. */ + length: StringIndex; + /** Text of the entity. */ + text?: string; + /** Confidence score of the category match. */ + confidenceScore?: number; +} + +export function phiEntityDeserializer(item: any): PhiEntity { + return { + category: item["category"], + offset: stringIndexDeserializer(item["offset"]), + length: stringIndexDeserializer(item["length"]), + text: item["text"], + confidenceScore: item["confidenceScore"], + }; +} + +/** String index encoding model. */ +export interface StringIndex { + /** The offset or length of the substring in UTF-8 encoding */ + utf8: number; + /** + * The offset or length of the substring in UTF-16 encoding. + * + * Primary encoding used by .NET, Java, and JavaScript. + */ + utf16: number; + /** + * The offset or length of the substring in CodePoint encoding. + * + * Primary encoding used by Python. + */ + codePoint: number; +} + +export function stringIndexDeserializer(item: any): StringIndex { + return { + utf8: item["utf8"], + utf16: item["utf16"], + codePoint: item["codePoint"], + }; +} + +/** Azure Health Data Services de-identification service versions. */ +export enum KnownVersions { + /** 2024-11-15 */ + V20241115 = "2024-11-15", + /** 2025-07-15-preview */ + V20250715Preview = "2025-07-15-preview", +} diff --git a/sdk/healthdataaiservices/health-deidentification-rest/src/outputModels.ts b/sdk/healthdataaiservices/health-deidentification-rest/src/outputModels.ts deleted file mode 100644 index 92ffb4900b50..000000000000 --- a/sdk/healthdataaiservices/health-deidentification-rest/src/outputModels.ts +++ /dev/null @@ -1,199 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -import type { ErrorModel } from "@azure-rest/core-client"; - -/** A job containing a batch of documents to de-identify. */ -export interface DeidentificationJobOutput { - /** The name of a job. */ - readonly name: string; - /** - * Operation to perform on the input documents. - * - * Possible values: "Redact", "Surrogate", "Tag", "SurrogateOnly" - */ - operation?: DeidentificationOperationTypeOutput; - /** Storage location to perform the operation on. */ - sourceLocation: SourceStorageLocationOutput; - /** Target location to store output of operation. */ - targetLocation: TargetStorageLocationOutput; - /** Customization parameters to override default service behaviors. */ - customizations?: DeidentificationJobCustomizationOptionsOutput; - /** - * Current status of a job. - * - * Possible values: "NotStarted", "Running", "Succeeded", "Failed", "Canceled" - */ - readonly status: OperationStateOutput; - /** Error when job fails in it's entirety. */ - readonly error?: ErrorModel; - /** - * Date and time when the job was completed. - * - * If the job is canceled, this is the time when the job was canceled. - * - * If the job failed, this is the time when the job failed. - */ - readonly lastUpdatedAt: string; - /** Date and time when the job was created. */ - readonly createdAt: string; - /** Date and time when the job was started. */ - readonly startedAt?: string; - /** Summary of a job. Exists only when the job is completed. */ - readonly summary?: DeidentificationJobSummaryOutput; -} - -/** Storage location. */ -export interface SourceStorageLocationOutput { - /** URL to storage location. */ - location: string; - /** Prefix to filter path by. */ - prefix: string; - /** List of extensions to filter path by. */ - extensions?: string[]; -} - -/** Storage location. */ -export interface TargetStorageLocationOutput { - /** URL to storage location. */ - location: string; - /** - * Replaces the input prefix of a file path with the output prefix, preserving the rest of the path structure. - * - * Example: - * File full path: documents/user/note.txt - * Input Prefix: "documents/user/" - * Output Prefix: "output_docs/" - * - * Output file: "output_docs/note.txt" - */ - prefix: string; - /** When set to true during a job, the service will overwrite the output location if it already exists. */ - overwrite?: boolean; -} - -/** Customizations options to override default service behaviors for job usage. */ -export interface DeidentificationJobCustomizationOptionsOutput { - /** - * Format of the redacted output. Only valid when Operation is Redact. - * Please refer to https://learn.microsoft.com/azure/healthcare-apis/deidentification/redaction-format for more details. - */ - redactionFormat?: string; - /** Locale in which the output surrogates are written. */ - surrogateLocale?: string; - /** Locale of the input text. Used for better PHI detection. Defaults to 'en-US'. */ - inputLocale?: string; -} - -/** Summary metrics of a job. */ -export interface DeidentificationJobSummaryOutput { - /** Number of documents that have completed. */ - successful: number; - /** Number of documents that have failed. */ - failed: number; - /** Number of documents that have been canceled. */ - canceled: number; - /** Number of documents total. */ - total: number; - /** Number of bytes processed. */ - bytesProcessed: number; -} - -/** Paged collection of DeidentificationJob items */ -export interface PagedDeidentificationJobOutput { - /** The DeidentificationJob items on this page */ - value: Array; - /** The link to the next page of items */ - nextLink?: string; -} - -/** Paged collection of DeidentificationDocumentDetails items */ -export interface PagedDeidentificationDocumentDetailsOutput { - /** The DeidentificationDocumentDetails items on this page */ - value: Array; - /** The link to the next page of items */ - nextLink?: string; -} - -/** Details of a single document in a job. */ -export interface DeidentificationDocumentDetailsOutput { - /** Id of the document details. */ - readonly id: string; - /** Location for the input. */ - input: DeidentificationDocumentLocationOutput; - /** Location for the output. */ - output?: DeidentificationDocumentLocationOutput; - /** - * Status of the document. - * - * Possible values: "NotStarted", "Running", "Succeeded", "Failed", "Canceled" - */ - status: OperationStateOutput; - /** Error when document fails. */ - error?: ErrorModel; -} - -/** Location of a document. */ -export interface DeidentificationDocumentLocationOutput { - /** Location of document in storage. */ - location: string; - /** The entity tag for this resource. */ - readonly etag: string; -} - -/** Response body for de-identification operation. */ -export interface DeidentificationResultOutput { - /** Output text after de-identification. Not available for "Tag" operation. */ - outputText?: string; - /** Result of the "Tag" operation. Only available for "Tag" Operation. */ - taggerResult?: PhiTaggerResultOutput; -} - -/** Result of the "Tag" operation. */ -export interface PhiTaggerResultOutput { - /** List of entities detected in the input. */ - entities: Array; -} - -/** PHI Entity tag in the input. */ -export interface PhiEntityOutput { - /** - * PHI Category of the entity. - * - * Possible values: "Unknown", "Account", "Age", "BioID", "City", "CountryOrRegion", "Date", "Device", "Doctor", "Email", "Fax", "HealthPlan", "Hospital", "IDNum", "IPAddress", "License", "LocationOther", "MedicalRecord", "Organization", "Patient", "Phone", "Profession", "SocialSecurity", "State", "Street", "Url", "Username", "Vehicle", "Zip" - */ - category: PhiCategoryOutput; - /** Starting index of the location from within the input text. */ - offset: StringIndexOutput; - /** Length of the input text. */ - length: StringIndexOutput; - /** Text of the entity. */ - text?: string; - /** Confidence score of the category match. */ - confidenceScore?: number; -} - -/** String index encoding model. */ -export interface StringIndexOutput { - /** The offset or length of the substring in UTF-8 encoding */ - utf8: number; - /** - * The offset or length of the substring in UTF-16 encoding. - * - * Primary encoding used by .NET, Java, and JavaScript. - */ - utf16: number; - /** - * The offset or length of the substring in CodePoint encoding. - * - * Primary encoding used by Python. - */ - codePoint: number; -} - -/** Alias for DeidentificationOperationTypeOutput */ -export type DeidentificationOperationTypeOutput = string; -/** Alias for OperationStateOutput */ -export type OperationStateOutput = string; -/** Alias for PhiCategoryOutput */ -export type PhiCategoryOutput = string; diff --git a/sdk/healthdataaiservices/health-deidentification-rest/src/paginateHelper.ts b/sdk/healthdataaiservices/health-deidentification-rest/src/paginateHelper.ts deleted file mode 100644 index 9ea946d9d6c5..000000000000 --- a/sdk/healthdataaiservices/health-deidentification-rest/src/paginateHelper.ts +++ /dev/null @@ -1,267 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -import type { Client, PathUncheckedResponse } from "@azure-rest/core-client"; -import { createRestError } from "@azure-rest/core-client"; - -/** - * returns an async iterator that iterates over results. It also has a `byPage` - * method that returns pages of items at once. - * - * @param pagedResult - an object that specifies how to get pages. - * @returns a paged async iterator that iterates over results. - */ -function getPagedAsyncIterator< - TElement, - TPage = TElement[], - TPageSettings = PageSettings, - TLink = string, ->( - pagedResult: PagedResult, -): PagedAsyncIterableIterator { - const iter = getItemAsyncIterator(pagedResult); - return { - next() { - return iter.next(); - }, - [Symbol.asyncIterator]() { - return this; - }, - byPage: - pagedResult?.byPage ?? - (((settings?: PageSettings) => { - const { continuationToken } = settings ?? {}; - return getPageAsyncIterator(pagedResult, { - pageLink: continuationToken as unknown as TLink | undefined, - }); - }) as unknown as (settings?: TPageSettings) => AsyncIterableIterator), - }; -} - -async function* getItemAsyncIterator( - pagedResult: PagedResult, -): AsyncIterableIterator { - const pages = getPageAsyncIterator(pagedResult); - const firstVal = await pages.next(); - // if the result does not have an array shape, i.e. TPage = TElement, then we return it as is - if (!Array.isArray(firstVal.value)) { - // can extract elements from this page - const { toElements } = pagedResult; - if (toElements) { - yield* toElements(firstVal.value) as TElement[]; - for await (const page of pages) { - yield* toElements(page) as TElement[]; - } - } else { - yield firstVal.value; - // `pages` is of type `AsyncIterableIterator` but TPage = TElement in this case - yield* pages as unknown as AsyncIterableIterator; - } - } else { - yield* firstVal.value; - for await (const page of pages) { - // pages is of type `AsyncIterableIterator` so `page` is of type `TPage`. In this branch, - // it must be the case that `TPage = TElement[]` - yield* page as unknown as TElement[]; - } - } -} - -async function* getPageAsyncIterator( - pagedResult: PagedResult, - options: { - pageLink?: TLink; - } = {}, -): AsyncIterableIterator { - const { pageLink } = options; - let response = await pagedResult.getPage(pageLink ?? pagedResult.firstPageLink); - if (!response) { - return; - } - yield response.page; - while (response.nextPageLink) { - response = await pagedResult.getPage(response.nextPageLink); - if (!response) { - return; - } - yield response.page; - } -} - -/** - * An interface that tracks the settings for paged iteration - */ -export interface PageSettings { - /** - * The token that keeps track of where to continue the iterator - */ - continuationToken?: string; -} - -/** - * An interface that allows async iterable iteration both to completion and by page. - */ -export interface PagedAsyncIterableIterator< - TElement, - TPage = TElement[], - TPageSettings = PageSettings, -> { - /** - * The next method, part of the iteration protocol - */ - next(): Promise>; - /** - * The connection to the async iterator, part of the iteration protocol - */ - [Symbol.asyncIterator](): PagedAsyncIterableIterator; - /** - * Return an AsyncIterableIterator that works a page at a time - */ - byPage: (settings?: TPageSettings) => AsyncIterableIterator; -} - -/** - * An interface that describes how to communicate with the service. - */ -interface PagedResult { - /** - * Link to the first page of results. - */ - firstPageLink: TLink; - /** - * A method that returns a page of results. - */ - getPage: (pageLink: TLink) => Promise<{ page: TPage; nextPageLink?: TLink } | undefined>; - /** - * a function to implement the `byPage` method on the paged async iterator. - */ - byPage?: (settings?: TPageSettings) => AsyncIterableIterator; - - /** - * A function to extract elements from a page. - */ - toElements?: (page: TPage) => unknown[]; -} - -/** - * Helper type to extract the type of an array - */ -export type GetArrayType = T extends Array ? TData : never; - -/** - * The type of a custom function that defines how to get a page and a link to the next one if any. - */ -export type GetPage = (pageLink: string) => Promise<{ - page: TPage; - nextPageLink?: string; -}>; - -/** - * Options for the paging helper - */ -export interface PagingOptions { - /** - * Custom function to extract pagination details for crating the PagedAsyncIterableIterator - */ - customGetPage?: GetPage[]>; -} - -/** - * Helper type to infer the Type of the paged elements from the response type - * This type is generated based on the swagger information for x-ms-pageable - * specifically on the itemName property which indicates the property of the response - * where the page items are found. The default value is `value`. - * This type will allow us to provide strongly typed Iterator based on the response we get as second parameter - */ -export type PaginateReturn = TResult extends { - body: { value?: infer TPage }; -} - ? GetArrayType - : Array; - -/** - * Helper to paginate results from an initial response that follows the specification of Autorest `x-ms-pageable` extension - * @param client - Client to use for sending the next page requests - * @param initialResponse - Initial response containing the nextLink and current page of elements - * @param customGetPage - Optional - Function to define how to extract the page and next link to be used to paginate the results - * @returns - PagedAsyncIterableIterator to iterate the elements - */ -export function paginate( - client: Client, - initialResponse: TResponse, - options: PagingOptions = {}, -): PagedAsyncIterableIterator> { - // Extract element type from initial response - type TElement = PaginateReturn; - let firstRun = true; - const itemName = "value"; - const nextLinkName = "nextLink"; - const { customGetPage } = options; - const pagedResult: PagedResult = { - firstPageLink: "", - getPage: - typeof customGetPage === "function" - ? customGetPage - : async (pageLink: string) => { - const result = firstRun ? initialResponse : await client.pathUnchecked(pageLink).get(); - firstRun = false; - checkPagingRequest(result); - const nextLink = getNextLink(result.body, nextLinkName); - const values = getElements(result.body, itemName); - return { - page: values, - nextPageLink: nextLink, - }; - }, - }; - - return getPagedAsyncIterator(pagedResult); -} - -/** - * Gets for the value of nextLink in the body - */ -function getNextLink(body: unknown, nextLinkName?: string): string | undefined { - if (!nextLinkName) { - return undefined; - } - - const nextLink = (body as Record)[nextLinkName]; - - if (typeof nextLink !== "string" && typeof nextLink !== "undefined") { - throw new Error(`Body Property ${nextLinkName} should be a string or undefined`); - } - - return nextLink; -} - -/** - * Gets the elements of the current request in the body. - */ -function getElements(body: unknown, itemName: string): T[] { - const value = (body as Record)[itemName] as T[]; - - // value has to be an array according to the x-ms-pageable extension. - // The fact that this must be an array is used above to calculate the - // type of elements in the page in PaginateReturn - if (!Array.isArray(value)) { - throw new Error( - `Couldn't paginate response\n Body doesn't contain an array property with name: ${itemName}`, - ); - } - - return value ?? []; -} - -/** - * Checks if a request failed - */ -function checkPagingRequest(response: PathUncheckedResponse): void { - const Http2xxStatusCodes = ["200", "201", "202", "203", "204", "205", "206", "207", "208", "226"]; - if (!Http2xxStatusCodes.includes(response.status)) { - throw createRestError( - `Pagination failed with unexpected statusCode ${response.status}`, - response, - ); - } -} diff --git a/sdk/healthdataaiservices/health-deidentification-rest/src/parameters.ts b/sdk/healthdataaiservices/health-deidentification-rest/src/parameters.ts deleted file mode 100644 index 59212ac5a0b7..000000000000 --- a/sdk/healthdataaiservices/health-deidentification-rest/src/parameters.ts +++ /dev/null @@ -1,121 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -import type { RawHttpHeadersInput } from "@azure/core-rest-pipeline"; -import type { RequestParameters } from "@azure-rest/core-client"; -import type { DeidentificationJob, DeidentificationContent } from "./models.js"; - -export interface GetJobHeaders { - /** An opaque, globally-unique, client-generated string identifier for the request. */ - "x-ms-client-request-id"?: string; -} - -export interface GetJobHeaderParam { - headers?: RawHttpHeadersInput & GetJobHeaders; -} - -export type GetJobParameters = GetJobHeaderParam & RequestParameters; - -export interface DeidentifyDocumentsHeaders { - /** An opaque, globally-unique, client-generated string identifier for the request. */ - "x-ms-client-request-id"?: string; -} - -export interface DeidentifyDocumentsBodyParam { - /** The resource instance. */ - body: DeidentificationJob; -} - -export interface DeidentifyDocumentsHeaderParam { - headers?: RawHttpHeadersInput & DeidentifyDocumentsHeaders; -} - -export type DeidentifyDocumentsParameters = DeidentifyDocumentsHeaderParam & - DeidentifyDocumentsBodyParam & - RequestParameters; - -export interface ListJobsHeaders { - /** An opaque, globally-unique, client-generated string identifier for the request. */ - "x-ms-client-request-id"?: string; -} - -export interface ListJobsQueryParamProperties { - /** The maximum number of result items per page. */ - maxpagesize?: number; - /** Token to continue a previous query. */ - continuationToken?: string; -} - -export interface ListJobsQueryParam { - queryParameters?: ListJobsQueryParamProperties; -} - -export interface ListJobsHeaderParam { - headers?: RawHttpHeadersInput & ListJobsHeaders; -} - -export type ListJobsParameters = ListJobsQueryParam & ListJobsHeaderParam & RequestParameters; - -export interface ListJobDocumentsHeaders { - /** An opaque, globally-unique, client-generated string identifier for the request. */ - "x-ms-client-request-id"?: string; -} - -export interface ListJobDocumentsQueryParamProperties { - /** The maximum number of result items per page. */ - maxpagesize?: number; - /** Token to continue a previous query. */ - continuationToken?: string; -} - -export interface ListJobDocumentsQueryParam { - queryParameters?: ListJobDocumentsQueryParamProperties; -} - -export interface ListJobDocumentsHeaderParam { - headers?: RawHttpHeadersInput & ListJobDocumentsHeaders; -} - -export type ListJobDocumentsParameters = ListJobDocumentsQueryParam & - ListJobDocumentsHeaderParam & - RequestParameters; - -export interface CancelJobHeaders { - /** An opaque, globally-unique, client-generated string identifier for the request. */ - "x-ms-client-request-id"?: string; -} - -export interface CancelJobHeaderParam { - headers?: RawHttpHeadersInput & CancelJobHeaders; -} - -export type CancelJobParameters = CancelJobHeaderParam & RequestParameters; - -export interface DeleteJobHeaders { - /** An opaque, globally-unique, client-generated string identifier for the request. */ - "x-ms-client-request-id"?: string; -} - -export interface DeleteJobHeaderParam { - headers?: RawHttpHeadersInput & DeleteJobHeaders; -} - -export type DeleteJobParameters = DeleteJobHeaderParam & RequestParameters; - -export interface DeidentifyTextHeaders { - /** An opaque, globally-unique, client-generated string identifier for the request. */ - "x-ms-client-request-id"?: string; -} - -export interface DeidentifyTextBodyParam { - /** Request body for de-identification operation. */ - body: DeidentificationContent; -} - -export interface DeidentifyTextHeaderParam { - headers?: RawHttpHeadersInput & DeidentifyTextHeaders; -} - -export type DeidentifyTextParameters = DeidentifyTextHeaderParam & - DeidentifyTextBodyParam & - RequestParameters; diff --git a/sdk/healthdataaiservices/health-deidentification-rest/src/pollingHelper.ts b/sdk/healthdataaiservices/health-deidentification-rest/src/pollingHelper.ts deleted file mode 100644 index e7db0a59c545..000000000000 --- a/sdk/healthdataaiservices/health-deidentification-rest/src/pollingHelper.ts +++ /dev/null @@ -1,210 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -import type { Client, HttpResponse } from "@azure-rest/core-client"; -import type { AbortSignalLike } from "@azure/abort-controller"; -import type { - CancelOnProgress, - CreateHttpPollerOptions, - RunningOperation, - OperationResponse, - OperationState, -} from "@azure/core-lro"; -import { createHttpPoller } from "@azure/core-lro"; -import type { - DeidentifyDocuments200Response, - DeidentifyDocuments201Response, - DeidentifyDocumentsDefaultResponse, - DeidentifyDocumentsLogicalResponse, -} from "./responses.js"; - -/** - * A simple poller that can be used to poll a long running operation. - */ -export interface SimplePollerLike, TResult> { - /** - * Returns true if the poller has finished polling. - */ - isDone(): boolean; - /** - * Returns the state of the operation. - */ - getOperationState(): TState; - /** - * Returns the result value of the operation, - * regardless of the state of the poller. - * It can return undefined or an incomplete form of the final TResult value - * depending on the implementation. - */ - getResult(): TResult | undefined; - /** - * Returns a promise that will resolve once a single polling request finishes. - * It does this by calling the update method of the Poller's operation. - */ - poll(options?: { abortSignal?: AbortSignalLike }): Promise; - /** - * Returns a promise that will resolve once the underlying operation is completed. - */ - pollUntilDone(pollOptions?: { abortSignal?: AbortSignalLike }): Promise; - /** - * Invokes the provided callback after each polling is completed, - * sending the current state of the poller's operation. - * - * It returns a method that can be used to stop receiving updates on the given callback function. - */ - onProgress(callback: (state: TState) => void): CancelOnProgress; - - /** - * Returns a promise that could be used for serialized version of the poller's operation - * by invoking the operation's serialize method. - */ - serialize(): Promise; - - /** - * Wait the poller to be submitted. - */ - submitted(): Promise; - - /** - * Returns a string representation of the poller's operation. Similar to serialize but returns a string. - * @deprecated Use serialize() instead. - */ - toString(): string; - - /** - * Stops the poller from continuing to poll. Please note this will only stop the client-side polling - * @deprecated Use abortSignal to stop polling instead. - */ - stopPolling(): void; - - /** - * Returns true if the poller is stopped. - * @deprecated Use abortSignal status to track this instead. - */ - isStopped(): boolean; -} - -/** - * Helper function that builds a Poller object to help polling a long running operation. - * @param client - Client to use for sending the request to get additional pages. - * @param initialResponse - The initial response. - * @param options - Options to set a resume state or custom polling interval. - * @returns - A poller object to poll for operation state updates and eventually get the final response. - */ -export async function getLongRunningPoller< - TResult extends DeidentifyDocumentsLogicalResponse | DeidentifyDocumentsDefaultResponse, ->( - client: Client, - initialResponse: - | DeidentifyDocuments200Response - | DeidentifyDocuments201Response - | DeidentifyDocumentsDefaultResponse, - options?: CreateHttpPollerOptions>, -): Promise, TResult>>; -export async function getLongRunningPoller( - client: Client, - initialResponse: TResult, - options: CreateHttpPollerOptions> = {}, -): Promise, TResult>> { - const abortController = new AbortController(); - const poller: RunningOperation = { - sendInitialRequest: async () => { - // In the case of Rest Clients we are building the LRO poller object from a response that's the reason - // we are not triggering the initial request here, just extracting the information from the - // response we were provided. - return getLroResponse(initialResponse); - }, - sendPollRequest: async (path: string, pollOptions?: { abortSignal?: AbortSignalLike }) => { - // This is the callback that is going to be called to poll the service - // to get the latest status. We use the client provided and the polling path - // which is an opaque URL provided by caller, the service sends this in one of the following headers: operation-location, azure-asyncoperation or location - // depending on the lro pattern that the service implements. If non is provided we default to the initial path. - function abortListener(): void { - abortController.abort(); - } - const inputAbortSignal = pollOptions?.abortSignal; - const abortSignal = abortController.signal; - if (inputAbortSignal?.aborted) { - abortController.abort(); - } else if (!abortSignal.aborted) { - inputAbortSignal?.addEventListener("abort", abortListener, { - once: true, - }); - } - let response; - try { - response = await client - .pathUnchecked(path ?? initialResponse.request.url) - .get({ abortSignal }); - } finally { - inputAbortSignal?.removeEventListener("abort", abortListener); - } - const lroResponse = getLroResponse(response as TResult); - lroResponse.rawResponse.headers["x-ms-original-url"] = initialResponse.request.url; - return lroResponse; - }, - }; - - options.resolveOnUnsuccessful = options.resolveOnUnsuccessful ?? true; - const httpPoller = createHttpPoller(poller, options); - const simplePoller: SimplePollerLike, TResult> = { - isDone() { - return httpPoller.isDone; - }, - isStopped() { - return abortController.signal.aborted; - }, - getOperationState() { - if (!httpPoller.operationState) { - throw new Error( - "Operation state is not available. The poller may not have been started and you could await submitted() before calling getOperationState().", - ); - } - return httpPoller.operationState; - }, - getResult() { - return httpPoller.result; - }, - toString() { - if (!httpPoller.operationState) { - throw new Error( - "Operation state is not available. The poller may not have been started and you could await submitted() before calling getOperationState().", - ); - } - return JSON.stringify({ - state: httpPoller.operationState, - }); - }, - stopPolling() { - abortController.abort(); - }, - onProgress: httpPoller.onProgress, - poll: httpPoller.poll, - pollUntilDone: httpPoller.pollUntilDone, - serialize: httpPoller.serialize, - submitted: httpPoller.submitted, - }; - return simplePoller; -} - -/** - * Converts a Rest Client response to a response that the LRO implementation understands - * @param response - a rest client http response - * @returns - An LRO response that the LRO implementation understands - */ -function getLroResponse( - response: TResult, -): OperationResponse { - if (Number.isNaN(response.status)) { - throw new TypeError(`Status code of the response is not a number. Value: ${response.status}`); - } - - return { - flatResponse: response, - rawResponse: { - ...response, - statusCode: Number.parseInt(response.status), - body: response.body, - }, - }; -} diff --git a/sdk/healthdataaiservices/health-deidentification-rest/src/responses.ts b/sdk/healthdataaiservices/health-deidentification-rest/src/responses.ts deleted file mode 100644 index 4ec5fb7f433e..000000000000 --- a/sdk/healthdataaiservices/health-deidentification-rest/src/responses.ts +++ /dev/null @@ -1,193 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -import type { RawHttpHeaders } from "@azure/core-rest-pipeline"; -import type { HttpResponse, ErrorResponse } from "@azure-rest/core-client"; -import type { - DeidentificationJobOutput, - PagedDeidentificationJobOutput, - PagedDeidentificationDocumentDetailsOutput, - DeidentificationResultOutput, -} from "./outputModels.js"; - -export interface GetJob200Headers { - /** An opaque, globally-unique, client-generated string identifier for the request. */ - "x-ms-client-request-id"?: string; -} - -/** The request has succeeded. */ -export interface GetJob200Response extends HttpResponse { - status: "200"; - body: DeidentificationJobOutput; - headers: RawHttpHeaders & GetJob200Headers; -} - -export interface GetJobDefaultHeaders { - /** String error code indicating what went wrong. */ - "x-ms-error-code"?: string; -} - -export interface GetJobDefaultResponse extends HttpResponse { - status: string; - body: ErrorResponse; - headers: RawHttpHeaders & GetJobDefaultHeaders; -} - -export interface DeidentifyDocuments200Headers { - /** An opaque, globally-unique, client-generated string identifier for the request. */ - "x-ms-client-request-id"?: string; - /** The location for monitoring the operation state. */ - "operation-location": string; -} - -/** The request has succeeded. */ -export interface DeidentifyDocuments200Response extends HttpResponse { - status: "200"; - body: DeidentificationJobOutput; - headers: RawHttpHeaders & DeidentifyDocuments200Headers; -} - -export interface DeidentifyDocuments201Headers { - /** An opaque, globally-unique, client-generated string identifier for the request. */ - "x-ms-client-request-id"?: string; - /** The location for monitoring the operation state. */ - "operation-location": string; -} - -/** The request has succeeded and a new resource has been created as a result. */ -export interface DeidentifyDocuments201Response extends HttpResponse { - status: "201"; - body: DeidentificationJobOutput; - headers: RawHttpHeaders & DeidentifyDocuments201Headers; -} - -export interface DeidentifyDocumentsDefaultHeaders { - /** String error code indicating what went wrong. */ - "x-ms-error-code"?: string; -} - -export interface DeidentifyDocumentsDefaultResponse extends HttpResponse { - status: string; - body: ErrorResponse; - headers: RawHttpHeaders & DeidentifyDocumentsDefaultHeaders; -} - -/** The final response for long-running deidentifyDocuments operation */ -export interface DeidentifyDocumentsLogicalResponse extends HttpResponse { - status: "200"; - body: DeidentificationJobOutput; -} - -export interface ListJobs200Headers { - /** An opaque, globally-unique, client-generated string identifier for the request. */ - "x-ms-client-request-id"?: string; -} - -/** The request has succeeded. */ -export interface ListJobs200Response extends HttpResponse { - status: "200"; - body: PagedDeidentificationJobOutput; - headers: RawHttpHeaders & ListJobs200Headers; -} - -export interface ListJobsDefaultHeaders { - /** String error code indicating what went wrong. */ - "x-ms-error-code"?: string; -} - -export interface ListJobsDefaultResponse extends HttpResponse { - status: string; - body: ErrorResponse; - headers: RawHttpHeaders & ListJobsDefaultHeaders; -} - -export interface ListJobDocuments200Headers { - /** An opaque, globally-unique, client-generated string identifier for the request. */ - "x-ms-client-request-id"?: string; -} - -/** The request has succeeded. */ -export interface ListJobDocuments200Response extends HttpResponse { - status: "200"; - body: PagedDeidentificationDocumentDetailsOutput; - headers: RawHttpHeaders & ListJobDocuments200Headers; -} - -export interface ListJobDocumentsDefaultHeaders { - /** String error code indicating what went wrong. */ - "x-ms-error-code"?: string; -} - -export interface ListJobDocumentsDefaultResponse extends HttpResponse { - status: string; - body: ErrorResponse; - headers: RawHttpHeaders & ListJobDocumentsDefaultHeaders; -} - -export interface CancelJob200Headers { - /** An opaque, globally-unique, client-generated string identifier for the request. */ - "x-ms-client-request-id"?: string; -} - -/** The request has succeeded. */ -export interface CancelJob200Response extends HttpResponse { - status: "200"; - body: DeidentificationJobOutput; - headers: RawHttpHeaders & CancelJob200Headers; -} - -export interface CancelJobDefaultHeaders { - /** String error code indicating what went wrong. */ - "x-ms-error-code"?: string; -} - -export interface CancelJobDefaultResponse extends HttpResponse { - status: string; - body: ErrorResponse; - headers: RawHttpHeaders & CancelJobDefaultHeaders; -} - -export interface DeleteJob204Headers { - /** An opaque, globally-unique, client-generated string identifier for the request. */ - "x-ms-client-request-id"?: string; -} - -/** There is no content to send for this request, but the headers may be useful. */ -export interface DeleteJob204Response extends HttpResponse { - status: "204"; - headers: RawHttpHeaders & DeleteJob204Headers; -} - -export interface DeleteJobDefaultHeaders { - /** String error code indicating what went wrong. */ - "x-ms-error-code"?: string; -} - -export interface DeleteJobDefaultResponse extends HttpResponse { - status: string; - body: ErrorResponse; - headers: RawHttpHeaders & DeleteJobDefaultHeaders; -} - -export interface DeidentifyText200Headers { - /** An opaque, globally-unique, client-generated string identifier for the request. */ - "x-ms-client-request-id"?: string; -} - -/** The request has succeeded. */ -export interface DeidentifyText200Response extends HttpResponse { - status: "200"; - body: DeidentificationResultOutput; - headers: RawHttpHeaders & DeidentifyText200Headers; -} - -export interface DeidentifyTextDefaultHeaders { - /** String error code indicating what went wrong. */ - "x-ms-error-code"?: string; -} - -export interface DeidentifyTextDefaultResponse extends HttpResponse { - status: string; - body: ErrorResponse; - headers: RawHttpHeaders & DeidentifyTextDefaultHeaders; -} diff --git a/sdk/healthdataaiservices/health-deidentification-rest/src/restorePollerHelpers.ts b/sdk/healthdataaiservices/health-deidentification-rest/src/restorePollerHelpers.ts new file mode 100644 index 000000000000..4fe0f13db630 --- /dev/null +++ b/sdk/healthdataaiservices/health-deidentification-rest/src/restorePollerHelpers.ts @@ -0,0 +1,150 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +import type { DeidentificationClient } from "./deidentificationClient.js"; +import { _deidentifyDocumentsDeserialize } from "./api/operations.js"; +import { getLongRunningPoller } from "./static-helpers/pollingHelpers.js"; +import type { OperationOptions, PathUncheckedResponse } from "@azure-rest/core-client"; +import type { AbortSignalLike } from "@azure/abort-controller"; +import type { PollerLike, OperationState, ResourceLocationConfig } from "@azure/core-lro"; +import { deserializeState } from "@azure/core-lro"; + +export interface RestorePollerOptions< + TResult, + TResponse extends PathUncheckedResponse = PathUncheckedResponse, +> extends OperationOptions { + /** Delay to wait until next poll, in milliseconds. */ + updateIntervalInMs?: number; + /** + * The signal which can be used to abort requests. + */ + abortSignal?: AbortSignalLike; + /** Deserialization function for raw response body */ + processResponseBody?: (result: TResponse) => Promise; +} + +/** + * Creates a poller from the serialized state of another poller. This can be + * useful when you want to create pollers on a different host or a poller + * needs to be constructed after the original one is not in scope. + */ +export function restorePoller( + client: DeidentificationClient, + serializedState: string, + sourceOperation: (...args: any[]) => PollerLike, TResult>, + options?: RestorePollerOptions, +): PollerLike, TResult> { + const pollerConfig = deserializeState(serializedState).config; + const { initialRequestUrl, requestMethod, metadata } = pollerConfig; + if (!initialRequestUrl || !requestMethod) { + throw new Error( + `Invalid serialized state: ${serializedState} for sourceOperation ${sourceOperation?.name}`, + ); + } + const resourceLocationConfig = metadata?.["resourceLocationConfig"] as + | ResourceLocationConfig + | undefined; + const { deserializer, expectedStatuses = [] } = + getDeserializationHelper(initialRequestUrl, requestMethod) ?? {}; + const deserializeHelper = options?.processResponseBody ?? deserializer; + if (!deserializeHelper) { + throw new Error( + `Please ensure the operation is in this client! We can't find its deserializeHelper for ${sourceOperation?.name}.`, + ); + } + return getLongRunningPoller( + (client as any)["_client"] ?? client, + deserializeHelper as (result: TResponse) => Promise, + expectedStatuses, + { + updateIntervalInMs: options?.updateIntervalInMs, + abortSignal: options?.abortSignal, + resourceLocationConfig, + restoreFrom: serializedState, + initialRequestUrl, + }, + ); +} + +interface DeserializationHelper { + // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type + deserializer: Function; + expectedStatuses: string[]; +} + +const deserializeMap: Record = { + "PUT /jobs/{name}": { + deserializer: _deidentifyDocumentsDeserialize, + expectedStatuses: ["201", "200", "202"], + }, +}; + +function getDeserializationHelper( + urlStr: string, + method: string, +): DeserializationHelper | undefined { + const path = new URL(urlStr).pathname; + const pathParts = path.split("/"); + + // Traverse list to match the longest candidate + // matchedLen: the length of candidate path + // matchedValue: the matched status code array + let matchedLen = -1, + matchedValue: DeserializationHelper | undefined; + + // Iterate the responseMap to find a match + for (const [key, value] of Object.entries(deserializeMap)) { + // Extracting the path from the map key which is in format + // GET /path/foo + if (!key.startsWith(method)) { + continue; + } + const candidatePath = getPathFromMapKey(key); + // Get each part of the url path + const candidateParts = candidatePath.split("/"); + + // track if we have found a match to return the values found. + let found = true; + for (let i = candidateParts.length - 1, j = pathParts.length - 1; i >= 1 && j >= 1; i--, j--) { + if (candidateParts[i]?.startsWith("{") && candidateParts[i]?.indexOf("}") !== -1) { + const start = candidateParts[i]!.indexOf("}") + 1, + end = candidateParts[i]?.length; + // If the current part of the candidate is a "template" part + // Try to use the suffix of pattern to match the path + // {guid} ==> $ + // {guid}:export ==> :export$ + const isMatched = new RegExp(`${candidateParts[i]?.slice(start, end)}`).test( + pathParts[j] || "", + ); + + if (!isMatched) { + found = false; + break; + } + continue; + } + + // If the candidate part is not a template and + // the parts don't match mark the candidate as not found + // to move on with the next candidate path. + if (candidateParts[i] !== pathParts[j]) { + found = false; + break; + } + } + + // We finished evaluating the current candidate parts + // Update the matched value if and only if we found the longer pattern + if (found && candidatePath.length > matchedLen) { + matchedLen = candidatePath.length; + matchedValue = value; + } + } + + return matchedValue; +} + +function getPathFromMapKey(mapKey: string): string { + const pathStart = mapKey.indexOf("/"); + return mapKey.slice(pathStart); +} diff --git a/sdk/healthdataaiservices/health-deidentification-rest/src/static-helpers/pagingHelpers.ts b/sdk/healthdataaiservices/health-deidentification-rest/src/static-helpers/pagingHelpers.ts new file mode 100644 index 000000000000..11248b3804e4 --- /dev/null +++ b/sdk/healthdataaiservices/health-deidentification-rest/src/static-helpers/pagingHelpers.ts @@ -0,0 +1,242 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +import type { Client, PathUncheckedResponse } from "@azure-rest/core-client"; +import { createRestError } from "@azure-rest/core-client"; +import { RestError } from "@azure/core-rest-pipeline"; + +/** + * Options for the byPage method + */ +export interface PageSettings { + /** + * A reference to a specific page to start iterating from. + */ + continuationToken?: string; +} + +/** + * An interface that describes a page of results. + */ +export type ContinuablePage = TPage & { + /** + * The token that keeps track of where to continue the iterator + */ + continuationToken?: string; +}; + +/** + * An interface that allows async iterable iteration both to completion and by page. + */ +export interface PagedAsyncIterableIterator< + TElement, + TPage = TElement[], + TPageSettings extends PageSettings = PageSettings, +> { + /** + * The next method, part of the iteration protocol + */ + next(): Promise>; + /** + * The connection to the async iterator, part of the iteration protocol + */ + [Symbol.asyncIterator](): PagedAsyncIterableIterator; + /** + * Return an AsyncIterableIterator that works a page at a time + */ + byPage: (settings?: TPageSettings) => AsyncIterableIterator>; +} + +/** + * An interface that describes how to communicate with the service. + */ +export interface PagedResult< + TElement, + TPage = TElement[], + TPageSettings extends PageSettings = PageSettings, +> { + /** + * Link to the first page of results. + */ + firstPageLink?: string; + /** + * A method that returns a page of results. + */ + getPage: (pageLink?: string) => Promise<{ page: TPage; nextPageLink?: string } | undefined>; + /** + * a function to implement the `byPage` method on the paged async iterator. + */ + byPage?: (settings?: TPageSettings) => AsyncIterableIterator>; + + /** + * A function to extract elements from a page. + */ + toElements?: (page: TPage) => TElement[]; +} + +/** + * Options for the paging helper + */ +export interface BuildPagedAsyncIteratorOptions { + itemName?: string; + nextLinkName?: string; +} + +/** + * Helper to paginate results in a generic way and return a PagedAsyncIterableIterator + */ +export function buildPagedAsyncIterator< + TElement, + TPage = TElement[], + TPageSettings extends PageSettings = PageSettings, + TResponse extends PathUncheckedResponse = PathUncheckedResponse, +>( + client: Client, + getInitialResponse: () => PromiseLike, + processResponseBody: (result: TResponse) => PromiseLike, + expectedStatuses: string[], + options: BuildPagedAsyncIteratorOptions = {}, +): PagedAsyncIterableIterator { + const itemName = options.itemName ?? "value"; + const nextLinkName = options.nextLinkName ?? "nextLink"; + const pagedResult: PagedResult = { + getPage: async (pageLink?: string) => { + const result = + pageLink === undefined + ? await getInitialResponse() + : await client.pathUnchecked(pageLink).get(); + checkPagingRequest(result, expectedStatuses); + const results = await processResponseBody(result as TResponse); + const nextLink = getNextLink(results, nextLinkName); + const values = getElements(results, itemName) as TPage; + return { + page: values, + nextPageLink: nextLink, + }; + }, + byPage: (settings?: TPageSettings) => { + const { continuationToken } = settings ?? {}; + return getPageAsyncIterator(pagedResult, { + pageLink: continuationToken, + }); + }, + }; + return getPagedAsyncIterator(pagedResult); +} + +/** + * returns an async iterator that iterates over results. It also has a `byPage` + * method that returns pages of items at once. + * + * @param pagedResult - an object that specifies how to get pages. + * @returns a paged async iterator that iterates over results. + */ + +function getPagedAsyncIterator< + TElement, + TPage = TElement[], + TPageSettings extends PageSettings = PageSettings, +>( + pagedResult: PagedResult, +): PagedAsyncIterableIterator { + const iter = getItemAsyncIterator(pagedResult); + return { + next() { + return iter.next(); + }, + [Symbol.asyncIterator]() { + return this; + }, + byPage: + pagedResult?.byPage ?? + ((settings?: TPageSettings) => { + const { continuationToken } = settings ?? {}; + return getPageAsyncIterator(pagedResult, { + pageLink: continuationToken, + }); + }), + }; +} + +async function* getItemAsyncIterator( + pagedResult: PagedResult, +): AsyncIterableIterator { + const pages = getPageAsyncIterator(pagedResult); + for await (const page of pages) { + yield* page as unknown as TElement[]; + } +} + +async function* getPageAsyncIterator( + pagedResult: PagedResult, + options: { + pageLink?: string; + } = {}, +): AsyncIterableIterator> { + const { pageLink } = options; + let response = await pagedResult.getPage(pageLink ?? pagedResult.firstPageLink); + if (!response) { + return; + } + let result = response.page as ContinuablePage; + result.continuationToken = response.nextPageLink; + yield result; + while (response.nextPageLink) { + response = await pagedResult.getPage(response.nextPageLink); + if (!response) { + return; + } + result = response.page as ContinuablePage; + result.continuationToken = response.nextPageLink; + yield result; + } +} + +/** + * Gets for the value of nextLink in the body + */ +function getNextLink(body: unknown, nextLinkName?: string): string | undefined { + if (!nextLinkName) { + return undefined; + } + + const nextLink = (body as Record)[nextLinkName]; + + if (typeof nextLink !== "string" && typeof nextLink !== "undefined" && nextLink !== null) { + throw new RestError( + `Body Property ${nextLinkName} should be a string or undefined or null but got ${typeof nextLink}`, + ); + } + + if (nextLink === null) { + return undefined; + } + + return nextLink; +} + +/** + * Gets the elements of the current request in the body. + */ +function getElements(body: unknown, itemName: string): T[] { + const value = (body as Record)[itemName] as T[]; + if (!Array.isArray(value)) { + throw new RestError( + `Couldn't paginate response\n Body doesn't contain an array property with name: ${itemName}`, + ); + } + + return value ?? []; +} + +/** + * Checks if a request failed + */ +function checkPagingRequest(response: PathUncheckedResponse, expectedStatuses: string[]): void { + if (!expectedStatuses.includes(response.status)) { + throw createRestError( + `Pagination failed with unexpected statusCode ${response.status}`, + response, + ); + } +} diff --git a/sdk/healthdataaiservices/health-deidentification-rest/src/static-helpers/pollingHelpers.ts b/sdk/healthdataaiservices/health-deidentification-rest/src/static-helpers/pollingHelpers.ts new file mode 100644 index 000000000000..63b3c201e5a8 --- /dev/null +++ b/sdk/healthdataaiservices/health-deidentification-rest/src/static-helpers/pollingHelpers.ts @@ -0,0 +1,127 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +import type { + PollerLike, + OperationState, + ResourceLocationConfig, + RunningOperation, + OperationResponse, +} from "@azure/core-lro"; +import { createHttpPoller } from "@azure/core-lro"; + +import type { Client, PathUncheckedResponse } from "@azure-rest/core-client"; +import { createRestError } from "@azure-rest/core-client"; +import type { AbortSignalLike } from "@azure/abort-controller"; + +export interface GetLongRunningPollerOptions { + /** Delay to wait until next poll, in milliseconds. */ + updateIntervalInMs?: number; + /** + * The signal which can be used to abort requests. + */ + abortSignal?: AbortSignalLike; + /** + * The potential location of the result of the LRO if specified by the LRO extension in the swagger. + */ + resourceLocationConfig?: ResourceLocationConfig; + /** + * The original url of the LRO + * Should not be null when restoreFrom is set + */ + initialRequestUrl?: string; + /** + * A serialized poller which can be used to resume an existing paused Long-Running-Operation. + */ + restoreFrom?: string; + /** + * The function to get the initial response + */ + getInitialResponse?: () => PromiseLike; +} +export function getLongRunningPoller( + client: Client, + processResponseBody: (result: TResponse) => Promise, + expectedStatuses: string[], + options: GetLongRunningPollerOptions, +): PollerLike, TResult> { + const { restoreFrom, getInitialResponse } = options; + if (!restoreFrom && !getInitialResponse) { + throw new Error("Either restoreFrom or getInitialResponse must be specified"); + } + let initialResponse: TResponse | undefined = undefined; + const pollAbortController = new AbortController(); + const poller: RunningOperation = { + sendInitialRequest: async () => { + if (!getInitialResponse) { + throw new Error("getInitialResponse is required when initializing a new poller"); + } + initialResponse = await getInitialResponse(); + return getLroResponse(initialResponse, expectedStatuses); + }, + sendPollRequest: async ( + path: string, + pollOptions?: { + abortSignal?: AbortSignalLike; + }, + ) => { + // The poll request would both listen to the user provided abort signal and the poller's own abort signal + function abortListener(): void { + pollAbortController.abort(); + } + const abortSignal = pollAbortController.signal; + if (options.abortSignal?.aborted) { + pollAbortController.abort(); + } else if (pollOptions?.abortSignal?.aborted) { + pollAbortController.abort(); + } else if (!abortSignal.aborted) { + options.abortSignal?.addEventListener("abort", abortListener, { + once: true, + }); + pollOptions?.abortSignal?.addEventListener("abort", abortListener, { + once: true, + }); + } + let response; + try { + response = await client.pathUnchecked(path).get({ abortSignal }); + } finally { + options.abortSignal?.removeEventListener("abort", abortListener); + pollOptions?.abortSignal?.removeEventListener("abort", abortListener); + } + + return getLroResponse(response as TResponse, expectedStatuses); + }, + }; + return createHttpPoller(poller, { + intervalInMs: options?.updateIntervalInMs, + resourceLocationConfig: options?.resourceLocationConfig, + restoreFrom: options?.restoreFrom, + processResult: (result: unknown) => { + return processResponseBody(result as TResponse); + }, + }); +} +/** + * Converts a Rest Client response to a response that the LRO implementation understands + * @param response - a rest client http response + * @param deserializeFn - deserialize function to convert Rest response to modular output + * @returns - An LRO response that the LRO implementation understands + */ +function getLroResponse( + response: TResponse, + expectedStatuses: string[], +): OperationResponse { + if (!expectedStatuses.includes(response.status)) { + throw createRestError(response); + } + + return { + flatResponse: response, + rawResponse: { + ...response, + statusCode: Number.parseInt(response.status), + body: response.body, + }, + }; +} diff --git a/sdk/healthdataaiservices/health-deidentification-rest/src/static-helpers/urlTemplate.ts b/sdk/healthdataaiservices/health-deidentification-rest/src/static-helpers/urlTemplate.ts new file mode 100644 index 000000000000..657898dd38ff --- /dev/null +++ b/sdk/healthdataaiservices/health-deidentification-rest/src/static-helpers/urlTemplate.ts @@ -0,0 +1,210 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +// --------------------- +// interfaces +// --------------------- +interface ValueOptions { + isFirst: boolean; // is first value in the expression + op?: string; // operator + varValue?: any; // variable value + varName?: string; // variable name + modifier?: string; // modifier e.g * + reserved?: boolean; // if true we'll keep reserved words with not encoding +} + +export interface UrlTemplateOptions { + // if set to true, reserved characters will not be encoded + allowReserved?: boolean; +} + +// --------------------- +// helpers +// --------------------- +function encodeComponent(val: string, reserved?: boolean, op?: string): string { + return (reserved ?? op === "+") || op === "#" + ? encodeReservedComponent(val) + : encodeRFC3986URIComponent(val); +} + +function encodeReservedComponent(str: string): string { + return str + .split(/(%[0-9A-Fa-f]{2})/g) + .map((part) => (!/%[0-9A-Fa-f]/.test(part) ? encodeURI(part) : part)) + .join(""); +} + +function encodeRFC3986URIComponent(str: string): string { + return encodeURIComponent(str).replace( + /[!'()*]/g, + (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`, + ); +} + +function isDefined(val: any): boolean { + return val !== undefined && val !== null; +} + +function getNamedAndIfEmpty(op?: string): [boolean, string] { + return [!!op && [";", "?", "&"].includes(op), !!op && ["?", "&"].includes(op) ? "=" : ""]; +} + +function getFirstOrSep(op?: string, isFirst = false): string { + if (isFirst) { + return !op || op === "+" ? "" : op; + } else if (!op || op === "+" || op === "#") { + return ","; + } else if (op === "?") { + return "&"; + } else { + return op; + } +} + +function getExpandedValue(option: ValueOptions): string { + let isFirst = option.isFirst; + const { op, varName, varValue: value, reserved } = option; + const vals: string[] = []; + const [named, ifEmpty] = getNamedAndIfEmpty(op); + + if (Array.isArray(value)) { + for (const val of value.filter(isDefined)) { + // prepare the following parts: separator, varName, value + vals.push(`${getFirstOrSep(op, isFirst)}`); + if (named && varName) { + vals.push(`${encodeURIComponent(varName)}`); + if (val === "") { + vals.push(ifEmpty); + } else { + vals.push("="); + } + } + vals.push(encodeComponent(val, reserved, op)); + isFirst = false; + } + } else if (typeof value === "object") { + for (const key of Object.keys(value)) { + const val = value[key]; + if (!isDefined(val)) { + continue; + } + // prepare the following parts: separator, key, value + vals.push(`${getFirstOrSep(op, isFirst)}`); + if (key) { + vals.push(`${encodeURIComponent(key)}`); + if (named && val === "") { + vals.push(ifEmpty); + } else { + vals.push("="); + } + } + vals.push(encodeComponent(val, reserved, op)); + isFirst = false; + } + } + return vals.join(""); +} + +function getNonExpandedValue(option: ValueOptions): string | undefined { + const { op, varName, varValue: value, isFirst, reserved } = option; + const vals: string[] = []; + const first = getFirstOrSep(op, isFirst); + const [named, ifEmpty] = getNamedAndIfEmpty(op); + if (named && varName) { + vals.push(encodeComponent(varName, reserved, op)); + if (value === "") { + if (!ifEmpty) { + vals.push(ifEmpty); + } + return !vals.join("") ? undefined : `${first}${vals.join("")}`; + } + vals.push("="); + } + + const items = []; + if (Array.isArray(value)) { + for (const val of value.filter(isDefined)) { + items.push(encodeComponent(val, reserved, op)); + } + } else if (typeof value === "object") { + for (const key of Object.keys(value)) { + if (!isDefined(value[key])) { + continue; + } + items.push(encodeRFC3986URIComponent(key)); + items.push(encodeComponent(value[key], reserved, op)); + } + } + vals.push(items.join(",")); + return !vals.join(",") ? undefined : `${first}${vals.join("")}`; +} + +function getVarValue(option: ValueOptions): string | undefined { + const { op, varName, modifier, isFirst, reserved, varValue: value } = option; + + if (!isDefined(value)) { + return undefined; + } else if (["string", "number", "boolean"].includes(typeof value)) { + let val = value.toString(); + const [named, ifEmpty] = getNamedAndIfEmpty(op); + const vals: string[] = [getFirstOrSep(op, isFirst)]; + if (named && varName) { + // No need to encode varName considering it is already encoded + vals.push(varName); + if (val === "") { + vals.push(ifEmpty); + } else { + vals.push("="); + } + } + if (modifier && modifier !== "*") { + val = val.substring(0, parseInt(modifier, 10)); + } + vals.push(encodeComponent(val, reserved, op)); + return vals.join(""); + } else if (modifier === "*") { + return getExpandedValue(option); + } else { + return getNonExpandedValue(option); + } +} + +// --------------------------------------------------------------------------------------------------- +// This is an implementation of RFC 6570 URI Template: https://datatracker.ietf.org/doc/html/rfc6570. +// --------------------------------------------------------------------------------------------------- +export function expandUrlTemplate( + template: string, + context: Record, + option?: UrlTemplateOptions, +): string { + return template.replace(/\{([^{}]+)\}|([^{}]+)/g, (_, expr, text) => { + if (!expr) { + return encodeReservedComponent(text); + } + let op; + if (["+", "#", ".", "/", ";", "?", "&"].includes(expr[0])) { + op = expr[0]; + expr = expr.slice(1); + } + const varList = expr.split(/,/g); + const result = []; + for (const varSpec of varList) { + const varMatch = /([^:*]*)(?::(\d+)|(\*))?/.exec(varSpec); + if (!varMatch || !varMatch[1]) { + continue; + } + const varValue = getVarValue({ + isFirst: result.length === 0, + op, + varValue: context[varMatch[1]], + varName: varMatch[1], + modifier: varMatch[2] || varMatch[3], + reserved: option?.allowReserved, + }); + if (varValue) { + result.push(varValue); + } + } + return result.join(""); + }); +} diff --git a/sdk/healthdataaiservices/health-deidentification-rest/tsconfig.json b/sdk/healthdataaiservices/health-deidentification-rest/tsconfig.json index d466f1460665..7e2de3350abe 100644 --- a/sdk/healthdataaiservices/health-deidentification-rest/tsconfig.json +++ b/sdk/healthdataaiservices/health-deidentification-rest/tsconfig.json @@ -2,16 +2,6 @@ "references": [ { "path": "./tsconfig.src.json" - }, - { - "path": "./tsconfig.samples.json" - }, - { - "path": "./tsconfig.test.json" - }, - { - "path": "./tsconfig.snippets.json" } - ], - "files": [] + ] } diff --git a/sdk/healthdataaiservices/health-deidentification-rest/tsp-location.yaml b/sdk/healthdataaiservices/health-deidentification-rest/tsp-location.yaml index 1902f0ee135d..eb120f56a984 100644 --- a/sdk/healthdataaiservices/health-deidentification-rest/tsp-location.yaml +++ b/sdk/healthdataaiservices/health-deidentification-rest/tsp-location.yaml @@ -1,4 +1,4 @@ directory: specification/healthdataaiservices/HealthDataAIServices.DeidServices +commit: e5c84e70cba98f4666825025e648f4184e6504a1 repo: Azure/azure-rest-api-specs -commit: a0a2a2ccc3b8cbf61d6044db2b434091ec769acf -additionalDirectories: [] +additionalDirectories: