mirror of
https://github.com/hanzoai/azure-sdk-for-js.git
synced 2026-10-30 19:51:51 +00:00
PR #32689 added `await` incorrectly to some samples' call expressions. This PR removes `await`s that have no effect from samples. Blocking issues in some packages are addressed to unblock sample publishing.
64 lines
2.4 KiB
TypeScript
64 lines
2.4 KiB
TypeScript
// Copyright (c) Microsoft Corporation.
|
|
// Licensed under the MIT License.
|
|
|
|
/**
|
|
* This sample shows how to extract elements of a receipt from a URL to a file using the prebuilt receipt model. Rather
|
|
* than using the `PrebuiltModels.Receipt` document model, this sample shows the use of the prebuilt model by ID,
|
|
* resulting in a weaker type that exactly mirrors the model's field schema at runtime.
|
|
*
|
|
* The prebuilt receipt model can return several fields. For a detailed list of the fields supported by the
|
|
* receipt model, see the `Receipt` type in the documentation, or refer to the following link:
|
|
*
|
|
* https://aka.ms/azsdk/documentitelligence/receiptfieldschema
|
|
*
|
|
* @summary use the "prebuilt-receipt" model ID to extract data from a receipt document (weakly-typed)
|
|
*/
|
|
|
|
import type { AnalyzeOperationOutput } from "@azure-rest/ai-document-intelligence";
|
|
import DocumentIntelligence, {
|
|
getLongRunningPoller,
|
|
isUnexpected,
|
|
} from "@azure-rest/ai-document-intelligence";
|
|
import { DefaultAzureCredential } from "@azure/identity";
|
|
import "dotenv/config";
|
|
|
|
async function main(): Promise<void> {
|
|
const client = DocumentIntelligence(
|
|
process.env["DOCUMENT_INTELLIGENCE_ENDPOINT"] || "<cognitive services endpoint>",
|
|
new DefaultAzureCredential(),
|
|
);
|
|
|
|
const initialResponse = await client
|
|
.path("/documentModels/{modelId}:analyze", "prebuilt-receipt")
|
|
.post({
|
|
contentType: "application/json",
|
|
body: {
|
|
// The Document Intelligence service will access the following URL to a receipt image and extract data from it
|
|
urlSource:
|
|
"https://raw.githubusercontent.com/Azure/azure-sdk-for-js/main/sdk/formrecognizer/ai-form-recognizer/assets/receipt/contoso-receipt.png",
|
|
},
|
|
});
|
|
if (isUnexpected(initialResponse)) {
|
|
throw initialResponse.body.error;
|
|
}
|
|
const poller = getLongRunningPoller(client, initialResponse);
|
|
|
|
poller.onProgress((state) => console.log("Operation:", state.result, state.status));
|
|
const analyzeResult = ((await poller.pollUntilDone()).body as AnalyzeOperationOutput)
|
|
.analyzeResult;
|
|
|
|
const documents = analyzeResult?.documents;
|
|
|
|
const result = documents && documents[0];
|
|
if (result) {
|
|
console.log(result.fields);
|
|
} else {
|
|
throw new Error("Expected at least one receipt in the result.");
|
|
}
|
|
}
|
|
|
|
main().catch((error) => {
|
|
console.error("An error occurred:", error);
|
|
process.exit(1);
|
|
});
|