From 9760ccc3265808ecbd57e67665771e54be3e82f6 Mon Sep 17 00:00:00 2001 From: Matthew Podwysocki Date: Mon, 3 Mar 2025 11:11:15 -0500 Subject: [PATCH 01/23] Migration: Update package.json, tsconfig.json, and api-extractor.json --- sdk/search/search-documents/package.json | 21 ++++++++++--------- sdk/search/search-documents/tsconfig.json | 12 ++++++++--- .../search-documents/tsconfig.samples.json | 16 ++++++-------- .../search-documents/tsconfig.test.json | 11 ++++------ 4 files changed, 30 insertions(+), 30 deletions(-) diff --git a/sdk/search/search-documents/package.json b/sdk/search/search-documents/package.json index 778e382f95f3..f6ce088090df 100644 --- a/sdk/search/search-documents/package.json +++ b/sdk/search/search-documents/package.json @@ -30,7 +30,7 @@ "test:node": "npm run build:test && npm run unit-test:node", "unit-test": "npm run unit-test:node && npm run unit-test:browser", "unit-test:browser": "npm run clean && dev-tool run build-package && dev-tool run build-test && dev-tool run test:vitest --browser", - "unit-test:node": "dev-tool run test:vitest --test-proxy-debug", + "unit-test:node": "dev-tool run test:vitest", "update-snippets": "echo skipped" }, "files": [ @@ -83,7 +83,7 @@ "@azure/core-client": "^1.9.2", "@azure/core-http-compat": "^2.1.2", "@azure/core-paging": "^1.6.2", - "@azure/core-rest-pipeline": "^1.18.0", + "@azure/core-rest-pipeline": "^1.19.0", "@azure/core-tracing": "^1.2.0", "@azure/core-util": "^1.11.0", "@azure/logger": "^1.1.4", @@ -96,17 +96,17 @@ "@azure-tools/test-utils-vitest": "^1.0.0", "@azure/dev-tool": "^1.0.0", "@azure/eslint-plugin-azure-sdk": "^3.0.0", - "@azure/identity": "^4.5.0", + "@azure/identity": "^4.7.0", "@azure/openai": "1.0.0-beta.12", "@types/node": "^18.0.0", - "@vitest/browser": "^3.0.3", - "@vitest/coverage-istanbul": "^3.0.3", + "@vitest/browser": "^3.0.6", + "@vitest/coverage-istanbul": "^3.0.6", "dotenv": "^16.0.0", "eslint": "^9.9.0", - "playwright": "^1.49.0", + "playwright": "^1.50.1", "type-plus": "^7.6.2", "typescript": "~5.7.2", - "vitest": "^3.0.3" + "vitest": "^3.0.6" }, "//sampleConfiguration": { "productName": "Azure Search Documents", @@ -121,6 +121,7 @@ }, "type": "module", "tshy": { + "project": "./tsconfig.src.json", "exports": { "./package.json": "./package.json", ".": "./src/index.ts" @@ -133,8 +134,7 @@ "browser", "react-native" ], - "selfLink": false, - "project": "./tsconfig.src.json" + "selfLink": false }, "exports": { "./package.json": "./package.json", @@ -156,5 +156,6 @@ "default": "./dist/commonjs/index.js" } } - } + }, + "react-native": "./dist/react-native/index.js" } diff --git a/sdk/search/search-documents/tsconfig.json b/sdk/search/search-documents/tsconfig.json index 273d9078a24a..19ceb382b521 100644 --- a/sdk/search/search-documents/tsconfig.json +++ b/sdk/search/search-documents/tsconfig.json @@ -1,7 +1,13 @@ { "references": [ - { "path": "./tsconfig.src.json" }, - { "path": "./tsconfig.samples.json" }, - { "path": "./tsconfig.test.json" } + { + "path": "./tsconfig.src.json" + }, + { + "path": "./tsconfig.samples.json" + }, + { + "path": "./tsconfig.test.json" + } ] } diff --git a/sdk/search/search-documents/tsconfig.samples.json b/sdk/search/search-documents/tsconfig.samples.json index 9e2fa97da615..808ccfefa24a 100644 --- a/sdk/search/search-documents/tsconfig.samples.json +++ b/sdk/search/search-documents/tsconfig.samples.json @@ -1,14 +1,10 @@ { - "extends": "../../../tsconfig", + "extends": "../../../tsconfig.samples.base.json", "compilerOptions": { "paths": { - "@azure/openai": ["./dist/esm"] - }, - "target": "ES2022", - "module": "NodeNext", - "moduleResolution": "NodeNext", - "noEmit": true, - "composite": true - }, - "include": ["./samples-dev"] + "@azure/search-documents": [ + "./dist/esm" + ] + } + } } diff --git a/sdk/search/search-documents/tsconfig.test.json b/sdk/search/search-documents/tsconfig.test.json index 48db94673987..3c2b783a8c1b 100644 --- a/sdk/search/search-documents/tsconfig.test.json +++ b/sdk/search/search-documents/tsconfig.test.json @@ -1,9 +1,6 @@ { - "extends": ["./tsconfig.src.json"], - "compilerOptions": { - "skipLibCheck": true, - "target": "ES2022", - "noEmit": true - }, - "include": ["./test", "./src"] + "extends": [ + "./tsconfig.src.json", + "../../../tsconfig.test.base.json" + ] } From 48751769920ca345bc53c03e738ece319f18b343 Mon Sep 17 00:00:00 2001 From: Matthew Podwysocki Date: Mon, 3 Mar 2025 11:11:15 -0500 Subject: [PATCH 02/23] Migration: Update test config --- .../search-documents/tsconfig.browser.config.json | 10 ++++------ sdk/search/search-documents/vitest.browser.config.ts | 7 ++++--- sdk/search/search-documents/vitest.config.ts | 5 +++-- sdk/search/search-documents/vitest.esm.config.ts | 1 + 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/sdk/search/search-documents/tsconfig.browser.config.json b/sdk/search/search-documents/tsconfig.browser.config.json index 3d7eb005c032..0c8bafa97126 100644 --- a/sdk/search/search-documents/tsconfig.browser.config.json +++ b/sdk/search/search-documents/tsconfig.browser.config.json @@ -1,8 +1,6 @@ { - "extends": ["./tsconfig.test.json"], - "exclude": ["./test/**/node", "./test/stress", "./test/snippets.spec.ts"], - "compilerOptions": { - "outDir": "./dist-test/browser", - "noEmit": false - } + "extends": [ + "./tsconfig.test.json", + "../../../tsconfig.browser.base.json" + ] } diff --git a/sdk/search/search-documents/vitest.browser.config.ts b/sdk/search/search-documents/vitest.browser.config.ts index 50ec2d5489b0..182729ab5ce9 100644 --- a/sdk/search/search-documents/vitest.browser.config.ts +++ b/sdk/search/search-documents/vitest.browser.config.ts @@ -1,3 +1,4 @@ + // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. @@ -8,9 +9,9 @@ export default mergeConfig( viteConfig, defineConfig({ test: { - include: ["dist-test/browser/test/**/*.spec.js"], - hookTimeout: 5000000, - testTimeout: 5000000, + include: ["dist-test/browser/test/**/*.spec.js",], + testTimeout: 1200000, + hookTimeout: 1200000, }, }), ); diff --git a/sdk/search/search-documents/vitest.config.ts b/sdk/search/search-documents/vitest.config.ts index 8849d7428190..40e031385dd8 100644 --- a/sdk/search/search-documents/vitest.config.ts +++ b/sdk/search/search-documents/vitest.config.ts @@ -1,3 +1,4 @@ + // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. @@ -8,8 +9,8 @@ export default mergeConfig( viteConfig, defineConfig({ test: { - hookTimeout: 5000000, - testTimeout: 5000000, + testTimeout: 1200000, + hookTimeout: 1200000, }, }), ); diff --git a/sdk/search/search-documents/vitest.esm.config.ts b/sdk/search/search-documents/vitest.esm.config.ts index 2f6e757a54f7..a70127279fc9 100644 --- a/sdk/search/search-documents/vitest.esm.config.ts +++ b/sdk/search/search-documents/vitest.esm.config.ts @@ -1,3 +1,4 @@ + // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. From 54602bb3706d01ddd25f13edea687f542516d9c3 Mon Sep 17 00:00:00 2001 From: Matthew Podwysocki Date: Mon, 3 Mar 2025 11:11:15 -0500 Subject: [PATCH 03/23] Migration: Clean up files From 4687214d21669b59ef7efc237350d21b6a6e63fc Mon Sep 17 00:00:00 2001 From: Matthew Podwysocki Date: Mon, 3 Mar 2025 11:11:24 -0500 Subject: [PATCH 04/23] Migration: Apply codemod: "fixSourceFile" --- .../src/bufferedSenderAutoFlushSize.ts | 10 +- .../src/bufferedSenderAutoFlushTimer.ts | 10 +- .../src/bufferedSenderManualFlush.ts | 10 +- .../src/dataSourceConnectionOperations.ts | 14 +- .../v11/typescript/src/indexOperations.ts | 18 +- .../v11/typescript/src/indexerOperations.ts | 20 +- .../samples/v11/typescript/src/setup.ts | 2 +- .../v11/typescript/src/skillSetOperations.ts | 14 +- .../typescript/src/synonymMapOperations.ts | 14 +- .../src/bufferedSenderAutoFlushSize.ts | 8 +- .../src/bufferedSenderAutoFlushTimer.ts | 8 +- .../src/bufferedSenderManualFlush.ts | 8 +- .../src/dataSourceConnectionOperations.ts | 4 +- .../typescript/src/indexOperations.ts | 4 +- .../typescript/src/indexerOperations.ts | 4 +- .../typescript/src/searchClientOperations.ts | 8 +- .../samples/v12-beta/typescript/src/setup.ts | 4 +- .../typescript/src/skillSetOperations.ts | 4 +- .../v12-beta/typescript/src/stickySession.ts | 8 +- .../typescript/src/synonymMapOperations.ts | 4 +- .../v12-beta/typescript/src/vectorSearch.ts | 10 +- .../src/bufferedSenderAutoFlushSize.ts | 8 +- .../src/bufferedSenderAutoFlushTimer.ts | 8 +- .../src/bufferedSenderManualFlush.ts | 8 +- .../src/dataSourceConnectionOperations.ts | 4 +- .../v12/typescript/src/indexOperations.ts | 4 +- .../v12/typescript/src/indexerOperations.ts | 4 +- .../typescript/src/searchClientOperations.ts | 8 +- .../samples/v12/typescript/src/setup.ts | 4 +- .../v12/typescript/src/skillSetOperations.ts | 4 +- .../v12/typescript/src/stickySession.ts | 8 +- .../typescript/src/synonymMapOperations.ts | 4 +- .../v12/typescript/src/vectorSearch.ts | 10 +- .../scripts/generateSampleEmbeddings.ts | 2 +- .../search-documents/test/narrowedTypes.ts | 421 ------------------ 35 files changed, 101 insertions(+), 582 deletions(-) diff --git a/sdk/search/search-documents/samples/v11/typescript/src/bufferedSenderAutoFlushSize.ts b/sdk/search/search-documents/samples/v11/typescript/src/bufferedSenderAutoFlushSize.ts index 2e2c8e4945b7..cc881f8c007a 100644 --- a/sdk/search/search-documents/samples/v11/typescript/src/bufferedSenderAutoFlushSize.ts +++ b/sdk/search/search-documents/samples/v11/typescript/src/bufferedSenderAutoFlushSize.ts @@ -12,12 +12,10 @@ import { GeographyPoint, SearchIndexClient } from "@azure/search-documents"; -import { createIndex, documentKeyRetriever, WAIT_TIME, delay } from "./setup"; -import { Hotel } from "./interfaces"; - -import * as dotenv from "dotenv"; -dotenv.config(); +import { createIndex, documentKeyRetriever, WAIT_TIME, delay } from "./setup.js"; +import { Hotel } from "./interfaces.js"; +import "dotenv/config"; /** * This sample is to demonstrate the use of SearchIndexingBufferedSender. * In this sample, the autoFlush is set to true. i.e. the user does not @@ -58,7 +56,7 @@ function getDocumentsArray(size: number): Hotel[] { return array; } -async function main() { +async function main(): Promise { if (!endpoint || !apiKey) { console.log("Make sure to set valid values for endpoint and apiKey with proper authorization."); return; diff --git a/sdk/search/search-documents/samples/v11/typescript/src/bufferedSenderAutoFlushTimer.ts b/sdk/search/search-documents/samples/v11/typescript/src/bufferedSenderAutoFlushTimer.ts index dfaa6d146b6d..0f3da7f47666 100644 --- a/sdk/search/search-documents/samples/v11/typescript/src/bufferedSenderAutoFlushTimer.ts +++ b/sdk/search/search-documents/samples/v11/typescript/src/bufferedSenderAutoFlushTimer.ts @@ -13,12 +13,10 @@ import { SearchIndexClient, DEFAULT_FLUSH_WINDOW } from "@azure/search-documents"; -import { createIndex, documentKeyRetriever, WAIT_TIME, delay } from "./setup"; -import { Hotel } from "./interfaces"; - -import * as dotenv from "dotenv"; -dotenv.config(); +import { createIndex, documentKeyRetriever, WAIT_TIME, delay } from "./setup.js"; +import { Hotel } from "./interfaces.js"; +import "dotenv/config"; /** * This sample is to demonstrate the use of SearchIndexingBufferedSender. * In this sample, the autoFlush is set to true. i.e. the user does not @@ -30,7 +28,7 @@ const endpoint = process.env.ENDPOINT || ""; const apiKey = process.env.SEARCH_API_ADMIN_KEY || ""; const TEST_INDEX_NAME = "example-index-sample-5"; -export async function main() { +export async function main(): Promise { if (!endpoint || !apiKey) { console.log("Make sure to set valid values for endpoint and apiKey with proper authorization."); return; diff --git a/sdk/search/search-documents/samples/v11/typescript/src/bufferedSenderManualFlush.ts b/sdk/search/search-documents/samples/v11/typescript/src/bufferedSenderManualFlush.ts index 9ce8d16bb78d..fe4e9c7c205f 100644 --- a/sdk/search/search-documents/samples/v11/typescript/src/bufferedSenderManualFlush.ts +++ b/sdk/search/search-documents/samples/v11/typescript/src/bufferedSenderManualFlush.ts @@ -12,12 +12,10 @@ import { GeographyPoint, SearchIndexClient } from "@azure/search-documents"; -import { createIndex, documentKeyRetriever, WAIT_TIME, delay } from "./setup"; -import { Hotel } from "./interfaces"; - -import * as dotenv from "dotenv"; -dotenv.config(); +import { createIndex, documentKeyRetriever, WAIT_TIME, delay } from "./setup.js"; +import { Hotel } from "./interfaces.js"; +import "dotenv/config"; /** * This sample is to demonstrate the use of SearchIndexingBufferedSender. * In this sample, the autoFlush is set to false. i.e. the user @@ -27,7 +25,7 @@ const endpoint = process.env.ENDPOINT || ""; const apiKey = process.env.SEARCH_API_ADMIN_KEY || ""; const TEST_INDEX_NAME = "example-index-sample-6"; -export async function main() { +export async function main(): Promise { if (!endpoint || !apiKey) { console.log("Make sure to set valid values for endpoint and apiKey with proper authorization."); return; diff --git a/sdk/search/search-documents/samples/v11/typescript/src/dataSourceConnectionOperations.ts b/sdk/search/search-documents/samples/v11/typescript/src/dataSourceConnectionOperations.ts index 386110cc4d17..0db3a3a505dc 100644 --- a/sdk/search/search-documents/samples/v11/typescript/src/dataSourceConnectionOperations.ts +++ b/sdk/search/search-documents/samples/v11/typescript/src/dataSourceConnectionOperations.ts @@ -11,9 +11,7 @@ import { SearchIndexerDataSourceConnection } from "@azure/search-documents"; -import * as dotenv from "dotenv"; -dotenv.config(); - +import "dotenv/config"; const endpoint = process.env.ENDPOINT || ""; const apiKey = process.env.SEARCH_API_ADMIN_KEY || ""; const connectionString = process.env.CONNECTION_STRING || ""; @@ -22,7 +20,7 @@ const dataSourceConnectionName = "example-ds-connection-sample-1"; async function createDataSourceConnection( dataSourceConnectionName: string, client: SearchIndexerClient -) { +): Promise { console.log(`Creating DS Connection Operation`); const dataSourceConnection: SearchIndexerDataSourceConnection = { name: dataSourceConnectionName, @@ -39,7 +37,7 @@ async function createDataSourceConnection( async function getAndUpdateDataSourceConnection( dataSourceConnectionName: string, client: SearchIndexerClient -) { +): Promise { console.log(`Get And Update DS Connection Operation`); const ds: SearchIndexerDataSourceConnection = await client.getDataSourceConnection( dataSourceConnectionName @@ -49,7 +47,7 @@ async function getAndUpdateDataSourceConnection( await client.createOrUpdateDataSourceConnection(ds); } -async function listDataSourceConnections(client: SearchIndexerClient) { +async function listDataSourceConnections(client: SearchIndexerClient): Promise { console.log(`List DS Connection Operation`); const listOfDataSourceConnections: Array = await client.listDataSourceConnections(); @@ -72,12 +70,12 @@ async function listDataSourceConnections(client: SearchIndexerClient) { async function deleteDataSourceConnection( dataSourceConnectionName: string, client: SearchIndexerClient -) { +): Promise { console.log(`Deleting DS Connection Operation`); await client.deleteDataSourceConnection(dataSourceConnectionName); } -async function main() { +async function main(): Promise { console.log(`Running DS Connection Operations Sample....`); if (!endpoint || !apiKey || !connectionString) { console.log("Make sure to set valid values for endpoint and apiKey with proper authorization."); diff --git a/sdk/search/search-documents/samples/v11/typescript/src/indexOperations.ts b/sdk/search/search-documents/samples/v11/typescript/src/indexOperations.ts index 9b3123a03039..e1927b1efa40 100644 --- a/sdk/search/search-documents/samples/v11/typescript/src/indexOperations.ts +++ b/sdk/search/search-documents/samples/v11/typescript/src/indexOperations.ts @@ -12,14 +12,12 @@ import { SearchIndexStatistics } from "@azure/search-documents"; -import * as dotenv from "dotenv"; -dotenv.config(); - +import "dotenv/config"; const endpoint = process.env.ENDPOINT || ""; const apiKey = process.env.SEARCH_API_ADMIN_KEY || ""; const indexName = "example-index-sample-1"; -async function createIndex(indexName: string, client: SearchIndexClient) { +async function createIndex(indexName: string, client: SearchIndexClient): Promise { console.log(`Creating Index Operation`); const index: SearchIndex = { name: indexName, @@ -62,7 +60,7 @@ async function createIndex(indexName: string, client: SearchIndexClient) { await client.createIndex(index); } -async function getAndUpdateIndex(indexName: string, client: SearchIndexClient) { +async function getAndUpdateIndex(indexName: string, client: SearchIndexClient): Promise { console.log(`Get And Update Index Operation`); const index: SearchIndex = await client.getIndex(indexName); index.fields.push({ @@ -73,14 +71,14 @@ async function getAndUpdateIndex(indexName: string, client: SearchIndexClient) { await client.createOrUpdateIndex(index); } -async function getIndexStatistics(indexName: string, client: SearchIndexClient) { +async function getIndexStatistics(indexName: string, client: SearchIndexClient): Promise { console.log(`Get Index Statistics Operation`); const statistics: SearchIndexStatistics = await client.getIndexStatistics(indexName); console.log(`Document Count: ${statistics.documentCount}`); console.log(`Storage Size: ${statistics.storageSize}`); } -async function getServiceStatistics(client: SearchIndexClient) { +async function getServiceStatistics(client: SearchIndexClient): Promise { console.log(`Get Service Statistics Operation`); const { counters, limits } = await client.getServiceStatistics(); console.log(`Counters`); @@ -116,7 +114,7 @@ async function getServiceStatistics(client: SearchIndexClient) { ); } -async function listIndexes(client: SearchIndexClient) { +async function listIndexes(client: SearchIndexClient): Promise { console.log(`List Indexes Operation`); const result = await client.listIndexes(); let listOfIndexes = await result.next(); @@ -132,12 +130,12 @@ async function listIndexes(client: SearchIndexClient) { } } -async function deleteIndex(indexName: string, client: SearchIndexClient) { +async function deleteIndex(indexName: string, client: SearchIndexClient): Promise { console.log(`Deleting Index Operation`); await client.deleteIndex(indexName); } -async function main() { +async function main(): Promise { console.log(`Running Index Operations Sample....`); if (!endpoint || !apiKey) { console.log("Make sure to set valid values for endpoint and apiKey with proper authorization."); diff --git a/sdk/search/search-documents/samples/v11/typescript/src/indexerOperations.ts b/sdk/search/search-documents/samples/v11/typescript/src/indexerOperations.ts index 8a79d0e146bd..deb8beeba655 100644 --- a/sdk/search/search-documents/samples/v11/typescript/src/indexerOperations.ts +++ b/sdk/search/search-documents/samples/v11/typescript/src/indexerOperations.ts @@ -12,9 +12,7 @@ import { SearchIndexerStatus } from "@azure/search-documents"; -import * as dotenv from "dotenv"; -dotenv.config(); - +import "dotenv/config"; const endpoint = process.env.ENDPOINT || ""; const apiKey = process.env.SEARCH_API_ADMIN_KEY || ""; const dataSourceName = process.env.DATA_SOURCE_NAME || ""; @@ -22,7 +20,7 @@ const targetIndexName = process.env.TARGET_INDEX_NAME || ""; const indexerName = "example-indexer-sample-1"; -async function createIndexer(indexerName: string, client: SearchIndexerClient) { +async function createIndexer(indexerName: string, client: SearchIndexerClient): Promise { console.log(`Creating Indexer Operation`); const indexer: SearchIndexer = { name: indexerName, @@ -34,7 +32,7 @@ async function createIndexer(indexerName: string, client: SearchIndexerClient) { await client.createIndexer(indexer); } -async function getAndUpdateIndexer(indexerName: string, client: SearchIndexerClient) { +async function getAndUpdateIndexer(indexerName: string, client: SearchIndexerClient): Promise { console.log(`Get And Update Indexer Operation`); const indexer: SearchIndexer = await client.getIndexer(indexerName); indexer.isDisabled = true; @@ -43,7 +41,7 @@ async function getAndUpdateIndexer(indexerName: string, client: SearchIndexerCli await client.createOrUpdateIndexer(indexer); } -async function getIndexerStatus(indexerName: string, client: SearchIndexerClient) { +async function getIndexerStatus(indexerName: string, client: SearchIndexerClient): Promise { console.log(`Get Indexer Status Operation`); const indexerStatus: SearchIndexerStatus = await client.getIndexerStatus(indexerName); console.log(`Status: ${indexerStatus.status}`); @@ -56,7 +54,7 @@ async function getIndexerStatus(indexerName: string, client: SearchIndexerClient console.log(`MaxRunTime: ${indexerStatus.limits.maxRunTime}`); } -async function listIndexers(client: SearchIndexerClient) { +async function listIndexers(client: SearchIndexerClient): Promise { console.log(`List Indexers Operation`); const listOfIndexers: Array = await client.listIndexers(); @@ -82,22 +80,22 @@ async function listIndexers(client: SearchIndexerClient) { } } -async function resetIndexer(indexerName: string, client: SearchIndexerClient) { +async function resetIndexer(indexerName: string, client: SearchIndexerClient): Promise { console.log(`Reset Indexer Operation`); await client.resetIndexer(indexerName); } -async function deleteIndexer(indexerName: string, client: SearchIndexerClient) { +async function deleteIndexer(indexerName: string, client: SearchIndexerClient): Promise { console.log(`Deleting Indexer Operation`); await client.deleteIndexer(indexerName); } -async function runIndexer(indexerName: string, client: SearchIndexerClient) { +async function runIndexer(indexerName: string, client: SearchIndexerClient): Promise { console.log(`Run Indexer Operation`); await client.runIndexer(indexerName); } -async function main() { +async function main(): Promise { console.log(`Running Indexer Operations Sample....`); if (!endpoint || !apiKey || !dataSourceName || !targetIndexName) { console.log("Make sure to set valid values for endpoint and apiKey with proper authorization."); diff --git a/sdk/search/search-documents/samples/v11/typescript/src/setup.ts b/sdk/search/search-documents/samples/v11/typescript/src/setup.ts index 5fe448c1f1bd..91f70c6404b3 100644 --- a/sdk/search/search-documents/samples/v11/typescript/src/setup.ts +++ b/sdk/search/search-documents/samples/v11/typescript/src/setup.ts @@ -6,7 +6,7 @@ */ import { SearchIndexClient, SearchIndex, KnownAnalyzerNames } from "@azure/search-documents"; -import { Hotel } from "./interfaces"; +import { Hotel } from "./interfaces.js"; export const WAIT_TIME = 4000; diff --git a/sdk/search/search-documents/samples/v11/typescript/src/skillSetOperations.ts b/sdk/search/search-documents/samples/v11/typescript/src/skillSetOperations.ts index fc5d76f531f4..25ff0bb954fa 100644 --- a/sdk/search/search-documents/samples/v11/typescript/src/skillSetOperations.ts +++ b/sdk/search/search-documents/samples/v11/typescript/src/skillSetOperations.ts @@ -11,15 +11,13 @@ import { SearchIndexerSkillset } from "@azure/search-documents"; -import * as dotenv from "dotenv"; -dotenv.config(); - +import "dotenv/config"; const endpoint = process.env.ENDPOINT || ""; const apiKey = process.env.SEARCH_API_ADMIN_KEY || ""; const skillsetName = "example-skillset-sample-1"; -async function createSkillset(skillsetName: string, client: SearchIndexerClient) { +async function createSkillset(skillsetName: string, client: SearchIndexerClient): Promise { console.log(`Creating Skillset Operation`); const skillset: SearchIndexerSkillset = { name: skillsetName, @@ -57,7 +55,7 @@ async function createSkillset(skillsetName: string, client: SearchIndexerClient) await client.createSkillset(skillset); } -async function getAndUpdateSkillset(skillsetName: string, client: SearchIndexerClient) { +async function getAndUpdateSkillset(skillsetName: string, client: SearchIndexerClient): Promise { console.log(`Get And Update Skillset Operation`); const skillset: SearchIndexerSkillset = await client.getSkillset(skillsetName); @@ -75,7 +73,7 @@ async function getAndUpdateSkillset(skillsetName: string, client: SearchIndexerC await client.createOrUpdateSkillset(skillset); } -async function listSkillsets(client: SearchIndexerClient) { +async function listSkillsets(client: SearchIndexerClient): Promise { console.log(`List Skillset Operation`); const listOfSkillsets: Array = await client.listSkillsets(); @@ -102,12 +100,12 @@ async function listSkillsets(client: SearchIndexerClient) { } } -async function deleteSkillset(skillsetName: string, client: SearchIndexerClient) { +async function deleteSkillset(skillsetName: string, client: SearchIndexerClient): Promise { console.log(`Deleting Skillset Operation`); await client.deleteSkillset(skillsetName); } -async function main() { +async function main(): Promise { console.log(`Running Skillset Operations Sample....`); if (!endpoint || !apiKey) { console.log("Make sure to set valid values for endpoint and apiKey with proper authorization."); diff --git a/sdk/search/search-documents/samples/v11/typescript/src/synonymMapOperations.ts b/sdk/search/search-documents/samples/v11/typescript/src/synonymMapOperations.ts index 9afa5e68a307..0bb9ab88ed68 100644 --- a/sdk/search/search-documents/samples/v11/typescript/src/synonymMapOperations.ts +++ b/sdk/search/search-documents/samples/v11/typescript/src/synonymMapOperations.ts @@ -7,14 +7,12 @@ import { SearchIndexClient, AzureKeyCredential, SynonymMap } from "@azure/search-documents"; -import * as dotenv from "dotenv"; -dotenv.config(); - +import "dotenv/config"; const endpoint = process.env.ENDPOINT || ""; const apiKey = process.env.SEARCH_API_ADMIN_KEY || ""; const synonymMapName = "example-synonymmap-sample-1"; -async function createSynonymMap(synonymMapName: string, client: SearchIndexClient) { +async function createSynonymMap(synonymMapName: string, client: SearchIndexClient): Promise { console.log(`Creating SynonymMap Operation`); const sm: SynonymMap = { name: synonymMapName, @@ -23,7 +21,7 @@ async function createSynonymMap(synonymMapName: string, client: SearchIndexClien await client.createSynonymMap(sm); } -async function getAndUpdateSynonymMap(synonymMapName: string, client: SearchIndexClient) { +async function getAndUpdateSynonymMap(synonymMapName: string, client: SearchIndexClient): Promise { console.log(`Get And Update SynonymMap Operation`); const sm: SynonymMap = await client.getSynonymMap(synonymMapName); console.log(`Update synonyms Synonym Map my-synonymmap`); @@ -31,7 +29,7 @@ async function getAndUpdateSynonymMap(synonymMapName: string, client: SearchInde await client.createOrUpdateSynonymMap(sm); } -async function listSynonymMaps(client: SearchIndexClient) { +async function listSynonymMaps(client: SearchIndexClient): Promise { console.log(`List SynonymMaps Operation`); const listOfSynonymMaps: Array = await client.listSynonymMaps(); @@ -46,12 +44,12 @@ async function listSynonymMaps(client: SearchIndexClient) { } } -async function deleteSynonymMap(synonymMapName: string, client: SearchIndexClient) { +async function deleteSynonymMap(synonymMapName: string, client: SearchIndexClient): Promise { console.log(`Deleting SynonymMap Operation`); await client.deleteSynonymMap(synonymMapName); } -async function main() { +async function main(): Promise { console.log(`Running Index Operations Sample....`); if (!endpoint || !apiKey) { console.log("Make sure to set valid values for endpoint and apiKey with proper authorization."); diff --git a/sdk/search/search-documents/samples/v12-beta/typescript/src/bufferedSenderAutoFlushSize.ts b/sdk/search/search-documents/samples/v12-beta/typescript/src/bufferedSenderAutoFlushSize.ts index 51dbdf1c1e90..e62815ec8050 100644 --- a/sdk/search/search-documents/samples/v12-beta/typescript/src/bufferedSenderAutoFlushSize.ts +++ b/sdk/search/search-documents/samples/v12-beta/typescript/src/bufferedSenderAutoFlushSize.ts @@ -12,12 +12,10 @@ import { SearchIndexClient, SearchIndexingBufferedSender, } from "@azure/search-documents"; -import type { Hotel } from "./interfaces"; -import { createIndex, delay, documentKeyRetriever, WAIT_TIME } from "./setup"; - -import * as dotenv from "dotenv"; -dotenv.config(); +import type { Hotel } from "./interfaces.js"; +import { createIndex, delay, documentKeyRetriever, WAIT_TIME } from "./setup.js"; +import "dotenv/config"; /** * This sample is to demonstrate the use of SearchIndexingBufferedSender. * In this sample, the autoFlush is set to true. i.e. the user does not diff --git a/sdk/search/search-documents/samples/v12-beta/typescript/src/bufferedSenderAutoFlushTimer.ts b/sdk/search/search-documents/samples/v12-beta/typescript/src/bufferedSenderAutoFlushTimer.ts index 1883e04b12c6..da5b726dc419 100644 --- a/sdk/search/search-documents/samples/v12-beta/typescript/src/bufferedSenderAutoFlushTimer.ts +++ b/sdk/search/search-documents/samples/v12-beta/typescript/src/bufferedSenderAutoFlushTimer.ts @@ -13,12 +13,10 @@ import { SearchIndexClient, SearchIndexingBufferedSender, } from "@azure/search-documents"; -import type { Hotel } from "./interfaces"; -import { createIndex, delay, documentKeyRetriever, WAIT_TIME } from "./setup"; - -import * as dotenv from "dotenv"; -dotenv.config(); +import type { Hotel } from "./interfaces.js"; +import { createIndex, delay, documentKeyRetriever, WAIT_TIME } from "./setup.js"; +import "dotenv/config"; /** * This sample is to demonstrate the use of SearchIndexingBufferedSender. * In this sample, the autoFlush is set to true. i.e. the user does not diff --git a/sdk/search/search-documents/samples/v12-beta/typescript/src/bufferedSenderManualFlush.ts b/sdk/search/search-documents/samples/v12-beta/typescript/src/bufferedSenderManualFlush.ts index 2fb26711fb63..1b31d3cdb263 100644 --- a/sdk/search/search-documents/samples/v12-beta/typescript/src/bufferedSenderManualFlush.ts +++ b/sdk/search/search-documents/samples/v12-beta/typescript/src/bufferedSenderManualFlush.ts @@ -12,12 +12,10 @@ import { SearchIndexClient, SearchIndexingBufferedSender, } from "@azure/search-documents"; -import type { Hotel } from "./interfaces"; -import { createIndex, delay, documentKeyRetriever, WAIT_TIME } from "./setup"; - -import * as dotenv from "dotenv"; -dotenv.config(); +import type { Hotel } from "./interfaces.js"; +import { createIndex, delay, documentKeyRetriever, WAIT_TIME } from "./setup.js"; +import "dotenv/config"; /** * This sample is to demonstrate the use of SearchIndexingBufferedSender. * In this sample, the autoFlush is set to false. i.e. the user diff --git a/sdk/search/search-documents/samples/v12-beta/typescript/src/dataSourceConnectionOperations.ts b/sdk/search/search-documents/samples/v12-beta/typescript/src/dataSourceConnectionOperations.ts index abcccef130f8..21c2487f61ca 100644 --- a/sdk/search/search-documents/samples/v12-beta/typescript/src/dataSourceConnectionOperations.ts +++ b/sdk/search/search-documents/samples/v12-beta/typescript/src/dataSourceConnectionOperations.ts @@ -9,9 +9,7 @@ import { DefaultAzureCredential } from "@azure/identity"; import type { SearchIndexerDataSourceConnection } from "@azure/search-documents"; import { SearchIndexerClient } from "@azure/search-documents"; -import * as dotenv from "dotenv"; -dotenv.config(); - +import "dotenv/config"; const endpoint = process.env.ENDPOINT || ""; const connectionString = process.env.CONNECTION_STRING || ""; const TEST_DATA_SOURCE_CONNECTION_NAME = "example-ds-connection-sample-1"; diff --git a/sdk/search/search-documents/samples/v12-beta/typescript/src/indexOperations.ts b/sdk/search/search-documents/samples/v12-beta/typescript/src/indexOperations.ts index 90bab68c6249..ea823b1d1b9a 100644 --- a/sdk/search/search-documents/samples/v12-beta/typescript/src/indexOperations.ts +++ b/sdk/search/search-documents/samples/v12-beta/typescript/src/indexOperations.ts @@ -9,9 +9,7 @@ import { DefaultAzureCredential } from "@azure/identity"; import type { SearchIndex, SearchIndexStatistics } from "@azure/search-documents"; import { SearchIndexClient } from "@azure/search-documents"; -import * as dotenv from "dotenv"; -dotenv.config(); - +import "dotenv/config"; const endpoint = process.env.ENDPOINT || ""; const TEST_INDEX_NAME = "example-index-sample-1"; diff --git a/sdk/search/search-documents/samples/v12-beta/typescript/src/indexerOperations.ts b/sdk/search/search-documents/samples/v12-beta/typescript/src/indexerOperations.ts index 4794a4401b0d..ad08777e1da2 100644 --- a/sdk/search/search-documents/samples/v12-beta/typescript/src/indexerOperations.ts +++ b/sdk/search/search-documents/samples/v12-beta/typescript/src/indexerOperations.ts @@ -9,9 +9,7 @@ import { DefaultAzureCredential } from "@azure/identity"; import type { SearchIndexer, SearchIndexerStatus } from "@azure/search-documents"; import { SearchIndexerClient } from "@azure/search-documents"; -import * as dotenv from "dotenv"; -dotenv.config(); - +import "dotenv/config"; const endpoint = process.env.ENDPOINT || ""; const dataSourceName = process.env.DATA_SOURCE_NAME || ""; const targetIndexName = process.env.TARGET_INDEX_NAME || ""; diff --git a/sdk/search/search-documents/samples/v12-beta/typescript/src/searchClientOperations.ts b/sdk/search/search-documents/samples/v12-beta/typescript/src/searchClientOperations.ts index 4a4ca1204aed..b308229a5baa 100644 --- a/sdk/search/search-documents/samples/v12-beta/typescript/src/searchClientOperations.ts +++ b/sdk/search/search-documents/samples/v12-beta/typescript/src/searchClientOperations.ts @@ -8,12 +8,10 @@ import { DefaultAzureCredential } from "@azure/identity"; import type { SelectFields } from "@azure/search-documents"; import { GeographyPoint, SearchClient, SearchIndexClient } from "@azure/search-documents"; -import type { Hotel } from "./interfaces"; -import { createIndex, delay, WAIT_TIME } from "./setup"; - -import * as dotenv from "dotenv"; -dotenv.config(); +import type { Hotel } from "./interfaces.js"; +import { createIndex, delay, WAIT_TIME } from "./setup.js"; +import "dotenv/config"; /** * This sample is to demonstrate the use of SearchClient. */ diff --git a/sdk/search/search-documents/samples/v12-beta/typescript/src/setup.ts b/sdk/search/search-documents/samples/v12-beta/typescript/src/setup.ts index 93e004bfde47..87a1d4736d78 100644 --- a/sdk/search/search-documents/samples/v12-beta/typescript/src/setup.ts +++ b/sdk/search/search-documents/samples/v12-beta/typescript/src/setup.ts @@ -7,8 +7,8 @@ import type { SearchIndex, SearchIndexClient } from "@azure/search-documents"; import { KnownAnalyzerNames } from "@azure/search-documents"; -import { env } from "process"; -import type { Hotel } from "./interfaces"; +import { env } from "node:process"; +import type { Hotel } from "./interfaces.js"; export const WAIT_TIME = 4000; diff --git a/sdk/search/search-documents/samples/v12-beta/typescript/src/skillSetOperations.ts b/sdk/search/search-documents/samples/v12-beta/typescript/src/skillSetOperations.ts index 5c7ba792ef96..1f5a3efe084f 100644 --- a/sdk/search/search-documents/samples/v12-beta/typescript/src/skillSetOperations.ts +++ b/sdk/search/search-documents/samples/v12-beta/typescript/src/skillSetOperations.ts @@ -9,9 +9,7 @@ import { DefaultAzureCredential } from "@azure/identity"; import type { SearchIndexerSkillset } from "@azure/search-documents"; import { SearchIndexerClient } from "@azure/search-documents"; -import * as dotenv from "dotenv"; -dotenv.config(); - +import "dotenv/config"; const endpoint = process.env.ENDPOINT || ""; const TEST_SKILLSET_NAME = "example-skillset-sample-1"; diff --git a/sdk/search/search-documents/samples/v12-beta/typescript/src/stickySession.ts b/sdk/search/search-documents/samples/v12-beta/typescript/src/stickySession.ts index 0d77e2d82a04..b245cc8b9e06 100644 --- a/sdk/search/search-documents/samples/v12-beta/typescript/src/stickySession.ts +++ b/sdk/search/search-documents/samples/v12-beta/typescript/src/stickySession.ts @@ -9,12 +9,10 @@ import { DefaultAzureCredential } from "@azure/identity"; import type { SearchClient } from "@azure/search-documents"; import { odata, SearchIndexClient } from "@azure/search-documents"; -import type { Hotel } from "./interfaces"; -import { createIndex, delay, WAIT_TIME } from "./setup"; - -import * as dotenv from "dotenv"; -dotenv.config(); +import type { Hotel } from "./interfaces.js"; +import { createIndex, delay, WAIT_TIME } from "./setup.js"; +import "dotenv/config"; /** * If you're querying a replicated index, Azure AI Search may target any replica with your queries. * As these replicas may not be in a consistent state, the service may appear to have inconsistent diff --git a/sdk/search/search-documents/samples/v12-beta/typescript/src/synonymMapOperations.ts b/sdk/search/search-documents/samples/v12-beta/typescript/src/synonymMapOperations.ts index 3a350971585d..2208f2205105 100644 --- a/sdk/search/search-documents/samples/v12-beta/typescript/src/synonymMapOperations.ts +++ b/sdk/search/search-documents/samples/v12-beta/typescript/src/synonymMapOperations.ts @@ -9,9 +9,7 @@ import { DefaultAzureCredential } from "@azure/identity"; import type { SynonymMap } from "@azure/search-documents"; import { SearchIndexClient } from "@azure/search-documents"; -import * as dotenv from "dotenv"; -dotenv.config(); - +import "dotenv/config"; const endpoint = process.env.ENDPOINT || ""; const TEST_SYNONYM_MAP_NAME = "example-synonymmap-sample-1"; diff --git a/sdk/search/search-documents/samples/v12-beta/typescript/src/vectorSearch.ts b/sdk/search/search-documents/samples/v12-beta/typescript/src/vectorSearch.ts index 5aaaf431cfca..f78291c7ba0e 100644 --- a/sdk/search/search-documents/samples/v12-beta/typescript/src/vectorSearch.ts +++ b/sdk/search/search-documents/samples/v12-beta/typescript/src/vectorSearch.ts @@ -7,13 +7,11 @@ import { DefaultAzureCredential } from "@azure/identity"; import { GeographyPoint, SearchClient, SearchIndexClient } from "@azure/search-documents"; -import type { Hotel } from "./interfaces"; -import { createIndex, delay, WAIT_TIME } from "./setup"; - -import * as dotenv from "dotenv"; -import { fancyStayEnVector, fancyStayFrVector, luxuryQueryVector } from "./vectors"; -dotenv.config(); +import type { Hotel } from "./interfaces.js"; +import { createIndex, delay, WAIT_TIME } from "./setup.js"; +import "dotenv/config"; +import { fancyStayEnVector, fancyStayFrVector, luxuryQueryVector } from "./vectors.js"; /** * This sample is to demonstrate the use of SearchClient's vector search feature. */ diff --git a/sdk/search/search-documents/samples/v12/typescript/src/bufferedSenderAutoFlushSize.ts b/sdk/search/search-documents/samples/v12/typescript/src/bufferedSenderAutoFlushSize.ts index 2347ef22f045..18c649d28714 100644 --- a/sdk/search/search-documents/samples/v12/typescript/src/bufferedSenderAutoFlushSize.ts +++ b/sdk/search/search-documents/samples/v12/typescript/src/bufferedSenderAutoFlushSize.ts @@ -12,12 +12,10 @@ import { SearchIndexClient, SearchIndexingBufferedSender, } from "@azure/search-documents"; -import { Hotel } from "./interfaces"; -import { createIndex, delay, documentKeyRetriever, WAIT_TIME } from "./setup"; - -import * as dotenv from "dotenv"; -dotenv.config(); +import { Hotel } from "./interfaces.js"; +import { createIndex, delay, documentKeyRetriever, WAIT_TIME } from "./setup.js"; +import "dotenv/config"; /** * This sample is to demonstrate the use of SearchIndexingBufferedSender. * In this sample, the autoFlush is set to true. i.e. the user does not diff --git a/sdk/search/search-documents/samples/v12/typescript/src/bufferedSenderAutoFlushTimer.ts b/sdk/search/search-documents/samples/v12/typescript/src/bufferedSenderAutoFlushTimer.ts index 983e0a4c8462..f6941b98c514 100644 --- a/sdk/search/search-documents/samples/v12/typescript/src/bufferedSenderAutoFlushTimer.ts +++ b/sdk/search/search-documents/samples/v12/typescript/src/bufferedSenderAutoFlushTimer.ts @@ -13,12 +13,10 @@ import { SearchIndexClient, SearchIndexingBufferedSender, } from "@azure/search-documents"; -import { Hotel } from "./interfaces"; -import { createIndex, delay, documentKeyRetriever, WAIT_TIME } from "./setup"; - -import * as dotenv from "dotenv"; -dotenv.config(); +import { Hotel } from "./interfaces.js"; +import { createIndex, delay, documentKeyRetriever, WAIT_TIME } from "./setup.js"; +import "dotenv/config"; /** * This sample is to demonstrate the use of SearchIndexingBufferedSender. * In this sample, the autoFlush is set to true. i.e. the user does not diff --git a/sdk/search/search-documents/samples/v12/typescript/src/bufferedSenderManualFlush.ts b/sdk/search/search-documents/samples/v12/typescript/src/bufferedSenderManualFlush.ts index e95562a998c4..5c3b3e612f29 100644 --- a/sdk/search/search-documents/samples/v12/typescript/src/bufferedSenderManualFlush.ts +++ b/sdk/search/search-documents/samples/v12/typescript/src/bufferedSenderManualFlush.ts @@ -12,12 +12,10 @@ import { SearchIndexClient, SearchIndexingBufferedSender, } from "@azure/search-documents"; -import { Hotel } from "./interfaces"; -import { createIndex, delay, documentKeyRetriever, WAIT_TIME } from "./setup"; - -import * as dotenv from "dotenv"; -dotenv.config(); +import { Hotel } from "./interfaces.js"; +import { createIndex, delay, documentKeyRetriever, WAIT_TIME } from "./setup.js"; +import "dotenv/config"; /** * This sample is to demonstrate the use of SearchIndexingBufferedSender. * In this sample, the autoFlush is set to false. i.e. the user diff --git a/sdk/search/search-documents/samples/v12/typescript/src/dataSourceConnectionOperations.ts b/sdk/search/search-documents/samples/v12/typescript/src/dataSourceConnectionOperations.ts index e3e6f9e983de..51ca69988f2e 100644 --- a/sdk/search/search-documents/samples/v12/typescript/src/dataSourceConnectionOperations.ts +++ b/sdk/search/search-documents/samples/v12/typescript/src/dataSourceConnectionOperations.ts @@ -8,9 +8,7 @@ import { DefaultAzureCredential } from "@azure/identity"; import { SearchIndexerClient, SearchIndexerDataSourceConnection } from "@azure/search-documents"; -import * as dotenv from "dotenv"; -dotenv.config(); - +import "dotenv/config"; const endpoint = process.env.ENDPOINT || ""; const connectionString = process.env.CONNECTION_STRING || ""; const TEST_DATA_SOURCE_CONNECTION_NAME = "example-ds-connection-sample-1"; diff --git a/sdk/search/search-documents/samples/v12/typescript/src/indexOperations.ts b/sdk/search/search-documents/samples/v12/typescript/src/indexOperations.ts index cae2cb2182bd..e3c18f748610 100644 --- a/sdk/search/search-documents/samples/v12/typescript/src/indexOperations.ts +++ b/sdk/search/search-documents/samples/v12/typescript/src/indexOperations.ts @@ -8,9 +8,7 @@ import { DefaultAzureCredential } from "@azure/identity"; import { SearchIndex, SearchIndexClient, SearchIndexStatistics } from "@azure/search-documents"; -import * as dotenv from "dotenv"; -dotenv.config(); - +import "dotenv/config"; const endpoint = process.env.ENDPOINT || ""; const TEST_INDEX_NAME = "example-index-sample-1"; diff --git a/sdk/search/search-documents/samples/v12/typescript/src/indexerOperations.ts b/sdk/search/search-documents/samples/v12/typescript/src/indexerOperations.ts index 28c4403869a9..b7e3f33716a9 100644 --- a/sdk/search/search-documents/samples/v12/typescript/src/indexerOperations.ts +++ b/sdk/search/search-documents/samples/v12/typescript/src/indexerOperations.ts @@ -8,9 +8,7 @@ import { DefaultAzureCredential } from "@azure/identity"; import { SearchIndexer, SearchIndexerClient, SearchIndexerStatus } from "@azure/search-documents"; -import * as dotenv from "dotenv"; -dotenv.config(); - +import "dotenv/config"; const endpoint = process.env.ENDPOINT || ""; const dataSourceName = process.env.DATA_SOURCE_NAME || ""; const targetIndexName = process.env.TARGET_INDEX_NAME || ""; diff --git a/sdk/search/search-documents/samples/v12/typescript/src/searchClientOperations.ts b/sdk/search/search-documents/samples/v12/typescript/src/searchClientOperations.ts index bfa5688a6714..7568d900e716 100644 --- a/sdk/search/search-documents/samples/v12/typescript/src/searchClientOperations.ts +++ b/sdk/search/search-documents/samples/v12/typescript/src/searchClientOperations.ts @@ -12,12 +12,10 @@ import { SearchIndexClient, SelectFields, } from "@azure/search-documents"; -import { Hotel } from "./interfaces"; -import { createIndex, delay, WAIT_TIME } from "./setup"; - -import * as dotenv from "dotenv"; -dotenv.config(); +import { Hotel } from "./interfaces.js"; +import { createIndex, delay, WAIT_TIME } from "./setup.js"; +import "dotenv/config"; /** * This sample is to demonstrate the use of SearchClient. */ diff --git a/sdk/search/search-documents/samples/v12/typescript/src/setup.ts b/sdk/search/search-documents/samples/v12/typescript/src/setup.ts index 1d3d51f7662a..3028b4b40c56 100644 --- a/sdk/search/search-documents/samples/v12/typescript/src/setup.ts +++ b/sdk/search/search-documents/samples/v12/typescript/src/setup.ts @@ -6,8 +6,8 @@ */ import { KnownAnalyzerNames, SearchIndex, SearchIndexClient } from "@azure/search-documents"; -import { env } from "process"; -import { Hotel } from "./interfaces"; +import { env } from "node:process"; +import { Hotel } from "./interfaces.js"; export const WAIT_TIME = 4000; diff --git a/sdk/search/search-documents/samples/v12/typescript/src/skillSetOperations.ts b/sdk/search/search-documents/samples/v12/typescript/src/skillSetOperations.ts index bc8f2894c42c..0f75080b2a2a 100644 --- a/sdk/search/search-documents/samples/v12/typescript/src/skillSetOperations.ts +++ b/sdk/search/search-documents/samples/v12/typescript/src/skillSetOperations.ts @@ -8,9 +8,7 @@ import { DefaultAzureCredential } from "@azure/identity"; import { SearchIndexerClient, SearchIndexerSkillset } from "@azure/search-documents"; -import * as dotenv from "dotenv"; -dotenv.config(); - +import "dotenv/config"; const endpoint = process.env.ENDPOINT || ""; const TEST_SKILLSET_NAME = "example-skillset-sample-1"; diff --git a/sdk/search/search-documents/samples/v12/typescript/src/stickySession.ts b/sdk/search/search-documents/samples/v12/typescript/src/stickySession.ts index 6b4ac3d8d8dd..86be91a03d14 100644 --- a/sdk/search/search-documents/samples/v12/typescript/src/stickySession.ts +++ b/sdk/search/search-documents/samples/v12/typescript/src/stickySession.ts @@ -8,12 +8,10 @@ import { DefaultAzureCredential } from "@azure/identity"; import { odata, SearchClient, SearchIndexClient } from "@azure/search-documents"; -import { Hotel } from "./interfaces"; -import { createIndex, delay, WAIT_TIME } from "./setup"; - -import * as dotenv from "dotenv"; -dotenv.config(); +import { Hotel } from "./interfaces.js"; +import { createIndex, delay, WAIT_TIME } from "./setup.js"; +import "dotenv/config"; /** * If you're querying a replicated index, Azure AI Search may target any replica with your queries. * As these replicas may not be in a consistent state, the service may appear to have inconsistent diff --git a/sdk/search/search-documents/samples/v12/typescript/src/synonymMapOperations.ts b/sdk/search/search-documents/samples/v12/typescript/src/synonymMapOperations.ts index deb7c9a61efc..8baf9ecd401b 100644 --- a/sdk/search/search-documents/samples/v12/typescript/src/synonymMapOperations.ts +++ b/sdk/search/search-documents/samples/v12/typescript/src/synonymMapOperations.ts @@ -8,9 +8,7 @@ import { DefaultAzureCredential } from "@azure/identity"; import { SearchIndexClient, SynonymMap } from "@azure/search-documents"; -import * as dotenv from "dotenv"; -dotenv.config(); - +import "dotenv/config"; const endpoint = process.env.ENDPOINT || ""; const TEST_SYNONYM_MAP_NAME = "example-synonymmap-sample-1"; diff --git a/sdk/search/search-documents/samples/v12/typescript/src/vectorSearch.ts b/sdk/search/search-documents/samples/v12/typescript/src/vectorSearch.ts index 36d79136d1dd..7f9faa01d671 100644 --- a/sdk/search/search-documents/samples/v12/typescript/src/vectorSearch.ts +++ b/sdk/search/search-documents/samples/v12/typescript/src/vectorSearch.ts @@ -7,13 +7,11 @@ import { DefaultAzureCredential } from "@azure/identity"; import { GeographyPoint, SearchClient, SearchIndexClient } from "@azure/search-documents"; -import { Hotel } from "./interfaces"; -import { createIndex, delay, WAIT_TIME } from "./setup"; - -import * as dotenv from "dotenv"; -import { fancyStayEnVector, fancyStayFrVector, luxuryQueryVector } from "./vectors"; -dotenv.config(); +import { Hotel } from "./interfaces.js"; +import { createIndex, delay, WAIT_TIME } from "./setup.js"; +import "dotenv/config"; +import { fancyStayEnVector, fancyStayFrVector, luxuryQueryVector } from "./vectors.js"; /** * This sample is to demonstrate the use of SearchClient's vector search feature. */ diff --git a/sdk/search/search-documents/scripts/generateSampleEmbeddings.ts b/sdk/search/search-documents/scripts/generateSampleEmbeddings.ts index 80d3fa72beee..25abf66cedab 100644 --- a/sdk/search/search-documents/scripts/generateSampleEmbeddings.ts +++ b/sdk/search/search-documents/scripts/generateSampleEmbeddings.ts @@ -25,7 +25,7 @@ const inputs = [ }, ]; -async function main() { +async function main(): Promise { const client = new OpenAIClient( process.env.AZURE_OPENAI_ENDPOINT!, new AzureKeyCredential(process.env.AZURE_OPENAI_KEY!), diff --git a/sdk/search/search-documents/test/narrowedTypes.ts b/sdk/search/search-documents/test/narrowedTypes.ts index dc7bde20c216..3f2d8af3759c 100644 --- a/sdk/search/search-documents/test/narrowedTypes.ts +++ b/sdk/search/search-documents/test/narrowedTypes.ts @@ -5,15 +5,6 @@ /* eslint-disable @typescript-eslint/ban-ts-comment */ /* eslint-disable @typescript-eslint/explicit-function-return-type */ -import type { SelectFields } from "../src/index.js"; -import { SearchClient } from "../src/index.js"; -import type { - NarrowedModel as GenericNarrowedModel, - SearchFieldArray, - SearchPick, - SelectArray, - SuggestNarrowedModel, -} from "../src/indexModels.js"; type Equals = (() => T extends T1 ? true : false) extends () => T extends T2 ? true : false ? any : never; @@ -49,427 +40,15 @@ type NarrowedModel = { type NarrowedModelFields = "key" | "a" | "b/a" | "d/b"; // @ts-expect-error -function testSelectFields() { - // SelectFields where T has properties should be T, - // otherwise string - const a: Equals, string> = "pass"; - const b: Equals, string> = "pass"; - const c: Equals, string> = "pass"; - const d: Equals, ModelFields> = "pass"; - - // SelectFields should be an error, as unknown should be cast - // @ts-expect-error - const e: Equals, string> = "fail"; - - return [a, b, c, d, e]; -} // @ts-expect-error -function testSearchPick() { - // SearchPick should be Model when Fields is not a strict subset of - // SelectFields - const a: Equals, object> = "pass"; - const b: Equals, Model> = "pass"; - const c: Equals, Model> = "pass"; - const d1: Model = {} as SearchPick; - const d2: SearchPick = {} as Model; - // SearchPick should correctly narrow the model to the specified fields - const e1: NarrowedModel = {} as SearchPick; - const e2: SearchPick = {} as NarrowedModel; - - // A narrowed model should be assignable to the base model type - const f: Model = {} as SearchPick; - - // If the model has unknown properties, SearchPick should yield the object type - const g: Equals, object> = "pass"; - const h: Equals, object> = "pass"; - const i: Equals, object> = "pass"; - - // SearchPick should reject the string type when using a model with known properties - // Ideally, this would just yield the model type, but we haven't found a way to make that happen - // without losing the type inference that powers the IntelliSense behavior we want. - // @ts-expect-error - const j: Equals, Model> = "fail"; - // @ts-expect-error - const k: Equals, Model> = "fail"; - - return [a, b, c, d1, d2, e1, e2, f, g, h, i, j, k]; -} // @ts-expect-error -function testNarrowedModel() { - const a1: GenericNarrowedModel = {} as NarrowedModel; - const a2: NarrowedModel = {} as GenericNarrowedModel; - - // Narrowing the never type should yield never - const b: Equals, never> = "pass"; - const c: Equals, never> = "pass"; - const d: Equals, never> = "pass"; - const e: Equals, never> = "pass"; - // Narrowing the object type should yield object - const f: Equals, object> = "pass"; - const g: Equals, object> = "pass"; - const h: Equals, object> = "pass"; - const i: Equals, object> = "pass"; - // Narrowing a model with never fields implies an empty array was used to select fields, - // so either never or an empty object are sensible. We're rolling with never since that behavior - // is less likely to break if we find a convincing reason to change it. - const j: Equals, never> = "pass"; - - // Narrowing a model with any fields should yield the model itself - const k: Equals, Model> = "pass"; - - const l1: SuggestNarrowedModel = {} as NarrowedModel; - const l2: NarrowedModel = {} as SuggestNarrowedModel; - - const m: Equals, never> = "pass"; - const n: Equals, never> = "pass"; - - const o: Equals, never> = "pass"; - const p: Equals, never> = "pass"; - const q: Equals, object> = "pass"; - const r: Equals, object> = "pass"; - const s: Equals, object> = "pass"; - const t: Equals, object> = "pass"; - // SuggestNarrowedModel is the same story as above, but this never case corresponds to the - // default behavior of the service, which is to return only the document key. To this end, - // SuggestNarrowedModel with never fields yields an interface with only the non-null properties - // of the model. - const u: Equals, { key?: string }> = "pass"; - const v: Equals, Model> = "pass"; - - // Passing unknown or string as fields are type errors - // @ts-expect-error - const w: Equals, object> = "fail"; - // @ts-expect-error - const x: Equals, never> = "fail"; - // @ts-expect-error - const y: Equals, Model> = "fail"; - // @ts-expect-error - const z: Equals, Model> = "fail"; - - // @ts-expect-error - const aa: Equals, never> = "fail"; - // @ts-expect-error - const ab: Equals, object> = "fail"; - // @ts-expect-error - const ac: Equals, Model> = "fail"; - // @ts-expect-error - const ad: Equals, Model> = "fail"; - - return [ - a1, - a2, - b, - c, - d, - e, - f, - g, - h, - i, - j, - k, - l1, - l2, - m, - n, - o, - p, - q, - r, - s, - t, - u, - v, - w, - x, - y, - z, - aa, - ab, - ac, - ad, - ]; -} // @ts-expect-error -function testSelectArray() { - const a: Equals, readonly string[]> = "pass"; - const b: Equals, readonly "field1"[]> = "pass"; - const c: Equals, readonly ("field1" | "field2")[]> = "pass"; - - // @ts-expect-error - const d: Equals, readonly string[]> = "fail"; - // @ts-expect-error - const e: Equals, readonly string[]> = "fail"; - return [a, b, c, d, e]; -} // @ts-expect-error -function testSearchFieldArray() { - const a: Equals, readonly string[]> = "pass"; - const b: Equals, readonly ModelFields[]> = "pass"; - const c: Equals, readonly string[]> = "pass"; - const d: Equals, readonly string[]> = "pass"; - - // @ts-expect-error - const e: Equals, readonly string[]> = "pass"; - - return [a, b, c, d, e]; -} // @ts-expect-error -function testNarrowedClient() { - const client = new SearchClient("azure", "sdk", "js" as any, {}); - - const select: SelectFields[] = ["key", "a", "b/a", "d/b"]; - const selectNarrowed = ["key", "a", "b/a", "d/b"] as const; - - async () => { - const suggest = await client.suggest("", "", {}); - for await (const result of suggest.results) { - const a: Equals< - (typeof result)["document"], - { - key?: string; - } - > = "pass"; - // @ts-expect-error - const b = result.document.a; - return [a, b]; - } - return; - }; - - async () => { - type VectorFields = NonNullable< - NonNullable< - NonNullable< - NonNullable[1]>["vectorSearchOptions"] - >["queries"] - >[number]["fields"] - >; - const a: Equals = "pass"; - return a; - }; - - async () => { - const select1 = await client.search("New", { - select, - }); - const select2 = await client.search("New", { - select: selectNarrowed, - }); - const select3 = await client.search("New", { - select: ["key", "a", "b/a", "d/b"], - }); - - const suppressUnusedWarning: any[] = []; - - for await (const result of select1.results) { - const a: Equals<(typeof result)["document"], Model> = "pass"; - const b = result.document.c; - suppressUnusedWarning.push(a); - suppressUnusedWarning.push(b); - } - for await (const result of select2.results) { - const a1: (typeof result)["document"] = {} as NarrowedModel; - const a2: NarrowedModel = {} as (typeof result)["document"]; - // @ts-expect-error - const b = result.document.c; - suppressUnusedWarning.push(a1); - suppressUnusedWarning.push(a2); - suppressUnusedWarning.push(b); - } - for await (const result of select3.results) { - const a1: (typeof result)["document"] = {} as NarrowedModel; - const a2: NarrowedModel = {} as (typeof result)["document"]; - // @ts-expect-error - const b = result.document.c; - suppressUnusedWarning.push(a1); - suppressUnusedWarning.push(a2); - suppressUnusedWarning.push(b); - } - return suppressUnusedWarning; - }; - async () => { - const select1 = await client.search("New", { - searchFields: select, - }); - const select2 = await client.search("New", { - searchFields: selectNarrowed, - }); - const select3 = await client.search("New", { - searchFields: ["key", "a", "b/a", "d/b"], - }); - - const suppressUnusedWarning: any[] = []; - - for await (const result of select1.results) { - const a: Equals<(typeof result)["document"], Model> = "pass"; - suppressUnusedWarning.push(a); - } - for await (const result of select2.results) { - const a: Equals<(typeof result)["document"], Model> = "pass"; - suppressUnusedWarning.push(a); - } - for await (const result of select3.results) { - const a: Equals<(typeof result)["document"], Model> = "pass"; - suppressUnusedWarning.push(a); - } - return suppressUnusedWarning; - }; - async () => { - const select1 = await client.search("New", { - select, - searchFields: select, - }); - const select2 = await client.search("New", { - select: selectNarrowed, - searchFields: selectNarrowed, - }); - const select3 = await client.search("New", { - select: ["key", "a", "b/a", "d/b"], - searchFields: ["key", "a", "b/a", "d/b"], - }); - - const suppressUnusedWarning: any[] = []; - - for await (const result of select1.results) { - const a: Equals<(typeof result)["document"], Model> = "pass"; - suppressUnusedWarning.push(a); - } - for await (const result of select2.results) { - const a1: (typeof result)["document"] = {} as NarrowedModel; - const a2: NarrowedModel = {} as (typeof result)["document"]; - // @ts-expect-error - const b = result.document.c; - suppressUnusedWarning.push(a1); - suppressUnusedWarning.push(a2); - suppressUnusedWarning.push(b); - } - for await (const result of select3.results) { - const a1: (typeof result)["document"] = {} as NarrowedModel; - const a2: NarrowedModel = {} as (typeof result)["document"]; - // @ts-expect-error - const b = result.document.c; - suppressUnusedWarning.push(a1); - suppressUnusedWarning.push(a2); - suppressUnusedWarning.push(b); - } - return suppressUnusedWarning; - }; -} // @ts-expect-error -function testWideClient() { - const client = new SearchClient("azure", "sdk", "js" as any, {}); - - const select: string[] = ["key", "a", "b/a", "d/b"]; - const selectNarrowed = ["key", "a", "b/a", "d/b"] as const; - - async () => { - const suggest = await client.suggest("", "", {}); - for await (const result of suggest.results) { - const a: Equals<(typeof result)["document"], object> = "pass"; - return a; - } - }; - - async () => { - type VectorFields = NonNullable< - NonNullable< - NonNullable< - NonNullable[1]>["vectorSearchOptions"] - >["queries"] - >[number]["fields"] - >; - const a: Equals = "pass"; - return a; - }; - - async () => { - const select1 = await client.search("New", { - select, - }); - const select2 = await client.search("New", { - select: selectNarrowed, - }); - const select3 = await client.search("New", { - select: ["key", "a", "b/a", "d/b"], - }); - - const suppressUnusedWarning: any[] = []; - - for await (const result of select1.results) { - const a: Equals<(typeof result)["document"], object> = "pass"; - suppressUnusedWarning.push(a); - } - for await (const result of select2.results) { - const a: Equals<(typeof result)["document"], object> = "pass"; - suppressUnusedWarning.push(a); - } - for await (const result of select3.results) { - const a: Equals<(typeof result)["document"], object> = "pass"; - suppressUnusedWarning.push(a); - } - }; - async () => { - const select1 = await client.search("New", { - searchFields: select, - }); - const select2 = await client.search("New", { - searchFields: selectNarrowed, - }); - const select3 = await client.search("New", { - searchFields: ["key", "a", "b/a", "d/b"], - }); - - const suppressUnusedWarning: any[] = []; - - for await (const result of select1.results) { - const a: Equals<(typeof result)["document"], object> = "pass"; - suppressUnusedWarning.push(a); - } - for await (const result of select2.results) { - const a: Equals<(typeof result)["document"], object> = "pass"; - suppressUnusedWarning.push(a); - } - for await (const result of select3.results) { - const a: Equals<(typeof result)["document"], object> = "pass"; - suppressUnusedWarning.push(a); - } - return suppressUnusedWarning; - }; - async () => { - const select1 = await client.search("New", { - select, - searchFields: select, - }); - const select2 = await client.search("New", { - select: selectNarrowed, - searchFields: selectNarrowed, - }); - const select3 = await client.search("New", { - select: ["key", "a", "b/a", "d/b"], - searchFields: ["key", "a", "b/a", "d/b"], - }); - - const suppressUnusedWarning: any[] = []; - - for await (const result of select1.results) { - const a: Equals<(typeof result)["document"], object> = "pass"; - suppressUnusedWarning.push(a); - } - for await (const result of select2.results) { - const a: Equals<(typeof result)["document"], object> = "pass"; - suppressUnusedWarning.push(a); - } - for await (const result of select3.results) { - const a: Equals<(typeof result)["document"], object> = "pass"; - suppressUnusedWarning.push(a); - } - return suppressUnusedWarning; - }; -} From b07fb0d191d385e1ddaa4d2ee5a1c2ea784313d4 Mon Sep 17 00:00:00 2001 From: Matthew Podwysocki Date: Mon, 3 Mar 2025 11:11:25 -0500 Subject: [PATCH 05/23] Migration: Apply codemod: "fixTestingImports" From a5acd545054ec93a749f0ade19c049cd31460399 Mon Sep 17 00:00:00 2001 From: Matthew Podwysocki Date: Mon, 3 Mar 2025 11:11:25 -0500 Subject: [PATCH 06/23] Migration: Apply codemod: "replaceAssertIsRejected" From 26ec9e64f8535f4b6a4215a8f010561437d9345a Mon Sep 17 00:00:00 2001 From: Matthew Podwysocki Date: Mon, 3 Mar 2025 11:11:26 -0500 Subject: [PATCH 07/23] Migration: Apply codemod: "replaceSinonStub" From 8410e208286e59365de5818514319c2654e9968a Mon Sep 17 00:00:00 2001 From: Matthew Podwysocki Date: Mon, 3 Mar 2025 11:11:26 -0500 Subject: [PATCH 08/23] Migration: Apply codemod: "addViHelper" From 7041b83a0fc899ef79173783fd36ce4c393b312f Mon Sep 17 00:00:00 2001 From: Matthew Podwysocki Date: Mon, 3 Mar 2025 11:11:27 -0500 Subject: [PATCH 09/23] Migration: Apply codemod: "replaceSupportTracing" From f80c73ff1c6ab99a22e5e7e00294e9b0d5cc9aea Mon Sep 17 00:00:00 2001 From: Matthew Podwysocki Date: Mon, 3 Mar 2025 11:11:27 -0500 Subject: [PATCH 10/23] Migration: Apply codemod: "replaceTestUtils" From dab641f959eaf7510f5f1e85514ee96bef5f79d0 Mon Sep 17 00:00:00 2001 From: Matthew Podwysocki Date: Mon, 3 Mar 2025 11:11:29 -0500 Subject: [PATCH 11/23] Migration: npm run format --- sdk/search/search-documents/test/narrowedTypes.ts | 1 - sdk/search/search-documents/tsconfig.browser.config.json | 5 +---- sdk/search/search-documents/tsconfig.samples.json | 4 +--- sdk/search/search-documents/tsconfig.test.json | 5 +---- 4 files changed, 3 insertions(+), 12 deletions(-) diff --git a/sdk/search/search-documents/test/narrowedTypes.ts b/sdk/search/search-documents/test/narrowedTypes.ts index 3f2d8af3759c..9f1bd20ad395 100644 --- a/sdk/search/search-documents/test/narrowedTypes.ts +++ b/sdk/search/search-documents/test/narrowedTypes.ts @@ -5,7 +5,6 @@ /* eslint-disable @typescript-eslint/ban-ts-comment */ /* eslint-disable @typescript-eslint/explicit-function-return-type */ - type Equals = (() => T extends T1 ? true : false) extends () => T extends T2 ? true : false ? any : never; diff --git a/sdk/search/search-documents/tsconfig.browser.config.json b/sdk/search/search-documents/tsconfig.browser.config.json index 0c8bafa97126..75871518e3a0 100644 --- a/sdk/search/search-documents/tsconfig.browser.config.json +++ b/sdk/search/search-documents/tsconfig.browser.config.json @@ -1,6 +1,3 @@ { - "extends": [ - "./tsconfig.test.json", - "../../../tsconfig.browser.base.json" - ] + "extends": ["./tsconfig.test.json", "../../../tsconfig.browser.base.json"] } diff --git a/sdk/search/search-documents/tsconfig.samples.json b/sdk/search/search-documents/tsconfig.samples.json index 808ccfefa24a..b3e4851d0085 100644 --- a/sdk/search/search-documents/tsconfig.samples.json +++ b/sdk/search/search-documents/tsconfig.samples.json @@ -2,9 +2,7 @@ "extends": "../../../tsconfig.samples.base.json", "compilerOptions": { "paths": { - "@azure/search-documents": [ - "./dist/esm" - ] + "@azure/search-documents": ["./dist/esm"] } } } diff --git a/sdk/search/search-documents/tsconfig.test.json b/sdk/search/search-documents/tsconfig.test.json index 3c2b783a8c1b..290ca214aebc 100644 --- a/sdk/search/search-documents/tsconfig.test.json +++ b/sdk/search/search-documents/tsconfig.test.json @@ -1,6 +1,3 @@ { - "extends": [ - "./tsconfig.src.json", - "../../../tsconfig.test.base.json" - ] + "extends": ["./tsconfig.src.json", "../../../tsconfig.test.base.json"] } From 1d7f9293b1eb8e36e052dcaa69cf8cdfd43af6fa Mon Sep 17 00:00:00 2001 From: Matthew Podwysocki Date: Mon, 3 Mar 2025 11:13:49 -0500 Subject: [PATCH 12/23] Migration: Update package.json, tsconfig.json, and api-extractor.json --- sdk/search/search-documents/package.json | 2 +- .../internal/node/synonymMap.node.spec.ts | 2 +- .../search-documents/test/narrowedTypes.ts | 53 ------------------- .../test/public/odata.spec.ts | 26 ++++----- .../search-documents/test/snippets.spec.ts | 28 ++++++++++ .../search-documents/tsconfig.samples.json | 4 +- .../search-documents/tsconfig.test.json | 5 +- 7 files changed, 50 insertions(+), 70 deletions(-) create mode 100644 sdk/search/search-documents/test/snippets.spec.ts diff --git a/sdk/search/search-documents/package.json b/sdk/search/search-documents/package.json index f6ce088090df..852ae038fde5 100644 --- a/sdk/search/search-documents/package.json +++ b/sdk/search/search-documents/package.json @@ -31,7 +31,7 @@ "unit-test": "npm run unit-test:node && npm run unit-test:browser", "unit-test:browser": "npm run clean && dev-tool run build-package && dev-tool run build-test && dev-tool run test:vitest --browser", "unit-test:node": "dev-tool run test:vitest", - "update-snippets": "echo skipped" + "update-snippets": "dev-tool run update-snippets" }, "files": [ "dist/", diff --git a/sdk/search/search-documents/test/internal/node/synonymMap.node.spec.ts b/sdk/search/search-documents/test/internal/node/synonymMap.node.spec.ts index d0fcfc404b34..3864c4f64b12 100644 --- a/sdk/search/search-documents/test/internal/node/synonymMap.node.spec.ts +++ b/sdk/search/search-documents/test/internal/node/synonymMap.node.spec.ts @@ -5,7 +5,7 @@ import type { SynonymMap } from "../../../src/serviceModels.js"; import { describe, it, assert } from "vitest"; describe("synonymmap", function () { - it("create synonymmap from file(node)", async function () { + it("create synonymmap from file(node)", async () => { const synonymMap: SynonymMap = await createSynonymMapFromFile( "my-synonym-map-1", "./test/internal/synonymMap.txt", diff --git a/sdk/search/search-documents/test/narrowedTypes.ts b/sdk/search/search-documents/test/narrowedTypes.ts index 9f1bd20ad395..e69de29bb2d1 100644 --- a/sdk/search/search-documents/test/narrowedTypes.ts +++ b/sdk/search/search-documents/test/narrowedTypes.ts @@ -1,53 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -/* eslint-disable no-unused-expressions */ -/* eslint-disable @typescript-eslint/ban-ts-comment */ -/* eslint-disable @typescript-eslint/explicit-function-return-type */ - -type Equals = - (() => T extends T1 ? true : false) extends () => T extends T2 ? true : false ? any : never; - -type Model = { - key?: string; - a?: string | null; - b?: { a?: string | null; b?: string | null } | null; - c?: Array; - d?: Array<{ a?: string | null; b?: { a?: string | null; b?: string | null } | null }>; -}; - -type ModelFields = - | "key" - | "a" - | "b" - | "b/a" - | "b/b" - | "c" - | "d" - | "d/a" - | "d/b" - | "d/b/a" - | "d/b/b"; - -type NarrowedModel = { - key?: string; - a?: string | null; - b?: { a?: string | null } | null; - d?: Array<{ b?: { a?: string | null; b?: string | null } | null }>; -}; - -type NarrowedModelFields = "key" | "a" | "b/a" | "d/b"; - -// @ts-expect-error - -// @ts-expect-error - -// @ts-expect-error - -// @ts-expect-error - -// @ts-expect-error - -// @ts-expect-error - -// @ts-expect-error diff --git a/sdk/search/search-documents/test/public/odata.spec.ts b/sdk/search/search-documents/test/public/odata.spec.ts index d2545cea382c..a10aeb60f3a5 100644 --- a/sdk/search/search-documents/test/public/odata.spec.ts +++ b/sdk/search/search-documents/test/public/odata.spec.ts @@ -4,74 +4,74 @@ import { odata } from "../../src/index.js"; import { describe, it, assert } from "vitest"; describe("odata", function () { - it("simple string isn't changed", function () { + it("simple string isn't changed", () => { const result = odata`Rooms/any(room: room/BaseRate lt 200) and Rating ge 4`; assert.strictEqual(result, "Rooms/any(room: room/BaseRate lt 200) and Rating ge 4"); }); - it("substitutions are incorporated", function () { + it("substitutions are incorporated", () => { const rate = 200; const rating = 4; const result = odata`Rooms/any(room: room/BaseRate lt ${rate}) and Rating ge ${rating}`; assert.strictEqual(result, "Rooms/any(room: room/BaseRate lt 200) and Rating ge 4"); }); - it("literals are escaped", function () { + it("literals are escaped", () => { const value = "you're"; const result = odata`search.ismatch('${value}', 'Description')`; assert.strictEqual(result, "search.ismatch('you''re', 'Description')"); }); - it("unquoted literals are quoted", function () { + it("unquoted literals are quoted", () => { const value = "you're"; const result = odata`search.ismatch(${value}, 'Description')`; assert.strictEqual(result, "search.ismatch('you''re', 'Description')"); }); - it("no arguments", function () { + it("no arguments", () => { assert.strictEqual(odata`Foo eq 2`, "Foo eq 2"); }); - it("one argument", function () { + it("one argument", () => { assert.strictEqual(odata`Foo eq ${2}`, "Foo eq 2"); }); - it("many arguments", function () { + it("many arguments", () => { assert.strictEqual( odata`Foo eq ${2} and Bar eq ${3} and Baz eq ${4} and Qux eq ${5} and Quux eq ${6}`, "Foo eq 2 and Bar eq 3 and Baz eq 4 and Qux eq 5 and Quux eq 6", ); }); - it("null", function () { + it("null", () => { assert.strictEqual(odata`Foo eq ${null}`, "Foo eq null"); }); - it("bool", function () { + it("bool", () => { const x: boolean = true; assert.strictEqual(odata`Foo eq ${x}`, "Foo eq true"); assert.strictEqual(odata`Foo eq ${true}`, "Foo eq true"); }); - it("numbers", function () { + it("numbers", () => { assert.strictEqual(odata`Foo eq ${0}`, "Foo eq 0"); assert.strictEqual(odata`Foo eq ${2}`, "Foo eq 2"); assert.strictEqual(odata`Foo eq ${-2}`, "Foo eq -2"); assert.strictEqual(odata`Foo eq ${2.5}`, "Foo eq 2.5"); }); - it("limits", function () { + it("limits", () => { assert.strictEqual(odata`Foo eq ${Number.NEGATIVE_INFINITY}`, "Foo eq -Infinity"); assert.strictEqual(odata`Foo eq ${Number.POSITIVE_INFINITY}`, "Foo eq Infinity"); assert.strictEqual(odata`Foo eq ${Number.NaN}`, "Foo eq NaN"); }); - it("dates", function () { + it("dates", () => { const result: string = odata`Foo eq ${new Date(1912, 6, 23, 11, 59, 59)}`; assert.strictEqual(result.includes("Tue Jul 23 1912 11:59:59"), true); }); - it("text", function () { + it("text", () => { assert.strictEqual(odata`Foo eq ${"x"}`, "Foo eq 'x'"); assert.strictEqual(odata`Foo eq ${"'"}`, "Foo eq ''''"); assert.strictEqual(odata`Foo eq ${'"'}`, "Foo eq '\"'"); diff --git a/sdk/search/search-documents/test/snippets.spec.ts b/sdk/search/search-documents/test/snippets.spec.ts new file mode 100644 index 000000000000..d93f96bed9b1 --- /dev/null +++ b/sdk/search/search-documents/test/snippets.spec.ts @@ -0,0 +1,28 @@ + +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +import { AstroManagementClient } from "../src/index.js"; +import { DefaultAzureCredential, InteractiveBrowserCredential } from "@azure/identity"; +import { setLogLevel } from "@azure/logger"; +import { describe, it } from "vitest"; + +describe("snippets", () => { + it("ReadmeSampleCreateClient_Node", async () => { + const subscriptionId = "00000000-0000-0000-0000-000000000000"; + const client = new AstroManagementClient(new DefaultAzureCredential(), subscriptionId); + }); + + it("ReadmeSampleCreateClient_Browser", async () => { + const subscriptionId = "00000000-0000-0000-0000-000000000000"; + const credential = new InteractiveBrowserCredential({ + tenantId: "", + clientId: "", + }); + const client = new AstroManagementClient(credential, subscriptionId); + }); + + it("SetLogLevel", async () => { + setLogLevel("info"); + }); +}); diff --git a/sdk/search/search-documents/tsconfig.samples.json b/sdk/search/search-documents/tsconfig.samples.json index b3e4851d0085..808ccfefa24a 100644 --- a/sdk/search/search-documents/tsconfig.samples.json +++ b/sdk/search/search-documents/tsconfig.samples.json @@ -2,7 +2,9 @@ "extends": "../../../tsconfig.samples.base.json", "compilerOptions": { "paths": { - "@azure/search-documents": ["./dist/esm"] + "@azure/search-documents": [ + "./dist/esm" + ] } } } diff --git a/sdk/search/search-documents/tsconfig.test.json b/sdk/search/search-documents/tsconfig.test.json index 290ca214aebc..3c2b783a8c1b 100644 --- a/sdk/search/search-documents/tsconfig.test.json +++ b/sdk/search/search-documents/tsconfig.test.json @@ -1,3 +1,6 @@ { - "extends": ["./tsconfig.src.json", "../../../tsconfig.test.base.json"] + "extends": [ + "./tsconfig.src.json", + "../../../tsconfig.test.base.json" + ] } From 5355546a42837c1ecb33e55e0615515d1b0fb1df Mon Sep 17 00:00:00 2001 From: Matthew Podwysocki Date: Mon, 3 Mar 2025 11:13:50 -0500 Subject: [PATCH 13/23] Migration: Update test config --- sdk/search/search-documents/tsconfig.browser.config.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sdk/search/search-documents/tsconfig.browser.config.json b/sdk/search/search-documents/tsconfig.browser.config.json index 75871518e3a0..0c8bafa97126 100644 --- a/sdk/search/search-documents/tsconfig.browser.config.json +++ b/sdk/search/search-documents/tsconfig.browser.config.json @@ -1,3 +1,6 @@ { - "extends": ["./tsconfig.test.json", "../../../tsconfig.browser.base.json"] + "extends": [ + "./tsconfig.test.json", + "../../../tsconfig.browser.base.json" + ] } From 2c122e5c93ce2c839b1d8305e6a3bfad7f2b83bf Mon Sep 17 00:00:00 2001 From: Matthew Podwysocki Date: Mon, 3 Mar 2025 11:13:50 -0500 Subject: [PATCH 14/23] Migration: Clean up files From fbc7e9b25e5d07e85c923db318de9c3bd6dc0e9f Mon Sep 17 00:00:00 2001 From: Matthew Podwysocki Date: Mon, 3 Mar 2025 11:13:57 -0500 Subject: [PATCH 15/23] Migration: Apply codemod: "fixSourceFile" From 99a4d56f64c4f05b0b658bae1abd1964baa82539 Mon Sep 17 00:00:00 2001 From: Matthew Podwysocki Date: Mon, 3 Mar 2025 11:13:58 -0500 Subject: [PATCH 16/23] Migration: Apply codemod: "fixTestingImports" From d8241a453f6cb8b37f9075e71840e57ec7878d00 Mon Sep 17 00:00:00 2001 From: Matthew Podwysocki Date: Mon, 3 Mar 2025 11:13:59 -0500 Subject: [PATCH 17/23] Migration: Apply codemod: "replaceAssertIsRejected" From 2a6b63a5ad94a06b956a88e70699e75740f8fbd0 Mon Sep 17 00:00:00 2001 From: Matthew Podwysocki Date: Mon, 3 Mar 2025 11:13:59 -0500 Subject: [PATCH 18/23] Migration: Apply codemod: "replaceSinonStub" From f21ebb30126660f44401f93380b1f4efb80005a2 Mon Sep 17 00:00:00 2001 From: Matthew Podwysocki Date: Mon, 3 Mar 2025 11:13:59 -0500 Subject: [PATCH 19/23] Migration: Apply codemod: "addViHelper" From 372a9fed2fb397f14b23144874fcaf607cbc5310 Mon Sep 17 00:00:00 2001 From: Matthew Podwysocki Date: Mon, 3 Mar 2025 11:14:00 -0500 Subject: [PATCH 20/23] Migration: Apply codemod: "replaceSupportTracing" From 264d96a119e52c1f8de22d21bb7444251d7651df Mon Sep 17 00:00:00 2001 From: Matthew Podwysocki Date: Mon, 3 Mar 2025 11:14:00 -0500 Subject: [PATCH 21/23] Migration: Apply codemod: "replaceTestUtils" From 4babbb07146bacf3d17d6e82f9c70e3c63bbbec6 Mon Sep 17 00:00:00 2001 From: Matthew Podwysocki Date: Mon, 3 Mar 2025 11:14:01 -0500 Subject: [PATCH 22/23] Migration: npm run format --- sdk/search/search-documents/test/snippets.spec.ts | 1 - sdk/search/search-documents/tsconfig.browser.config.json | 5 +---- sdk/search/search-documents/tsconfig.samples.json | 4 +--- sdk/search/search-documents/tsconfig.test.json | 5 +---- 4 files changed, 3 insertions(+), 12 deletions(-) diff --git a/sdk/search/search-documents/test/snippets.spec.ts b/sdk/search/search-documents/test/snippets.spec.ts index d93f96bed9b1..108b0287b5c1 100644 --- a/sdk/search/search-documents/test/snippets.spec.ts +++ b/sdk/search/search-documents/test/snippets.spec.ts @@ -1,4 +1,3 @@ - // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. diff --git a/sdk/search/search-documents/tsconfig.browser.config.json b/sdk/search/search-documents/tsconfig.browser.config.json index 0c8bafa97126..75871518e3a0 100644 --- a/sdk/search/search-documents/tsconfig.browser.config.json +++ b/sdk/search/search-documents/tsconfig.browser.config.json @@ -1,6 +1,3 @@ { - "extends": [ - "./tsconfig.test.json", - "../../../tsconfig.browser.base.json" - ] + "extends": ["./tsconfig.test.json", "../../../tsconfig.browser.base.json"] } diff --git a/sdk/search/search-documents/tsconfig.samples.json b/sdk/search/search-documents/tsconfig.samples.json index 808ccfefa24a..b3e4851d0085 100644 --- a/sdk/search/search-documents/tsconfig.samples.json +++ b/sdk/search/search-documents/tsconfig.samples.json @@ -2,9 +2,7 @@ "extends": "../../../tsconfig.samples.base.json", "compilerOptions": { "paths": { - "@azure/search-documents": [ - "./dist/esm" - ] + "@azure/search-documents": ["./dist/esm"] } } } diff --git a/sdk/search/search-documents/tsconfig.test.json b/sdk/search/search-documents/tsconfig.test.json index 3c2b783a8c1b..290ca214aebc 100644 --- a/sdk/search/search-documents/tsconfig.test.json +++ b/sdk/search/search-documents/tsconfig.test.json @@ -1,6 +1,3 @@ { - "extends": [ - "./tsconfig.src.json", - "../../../tsconfig.test.base.json" - ] + "extends": ["./tsconfig.src.json", "../../../tsconfig.test.base.json"] } From a7e908fe4da72dace7746588cff54a01a7d4b1de Mon Sep 17 00:00:00 2001 From: Matthew Podwysocki Date: Mon, 3 Mar 2025 11:57:36 -0500 Subject: [PATCH 23/23] [search] Migrate search projects to use snippets extraction --- sdk/search/search-documents/README.md | 327 +++++------- sdk/search/search-documents/src/odata.ts | 4 +- .../search-documents/src/searchClient.ts | 70 ++- .../search-documents/src/searchIndexClient.ts | 9 +- .../src/searchIndexerClient.ts | 9 +- .../search-documents/test/narrowedTypes.ts | 477 ++++++++++++++++++ .../test/public/node/searchClient.spec.ts | 3 +- .../search-documents/test/snippets.spec.ts | 375 +++++++++++++- 8 files changed, 1020 insertions(+), 254 deletions(-) diff --git a/sdk/search/search-documents/README.md b/sdk/search/search-documents/README.md index e464be20c766..e614c162e6af 100644 --- a/sdk/search/search-documents/README.md +++ b/sdk/search/search-documents/README.md @@ -88,13 +88,8 @@ _Note: The example Azure CLI snippet above retrieves an admin key so it's easier Once you have an api-key, you can use it as follows: -```js -const { - SearchClient, - SearchIndexClient, - SearchIndexerClient, - AzureKeyCredential, -} = require("@azure/search-documents"); +```ts snippet:ReadmeSampleCreateClient_ApiKey +import { SearchClient, SearchIndexClient, SearchIndexerClient } from "@azure/search-documents"; // To query and manipulate documents const searchClient = new SearchClient( @@ -116,14 +111,13 @@ To authenticate in a [National Cloud](https://learn.microsoft.com/azure/active-d - Set the `Audience` in `SearchClientOptions` -```js -const { +```ts snippet:ReadmeSampleCreateClient_NationalCloud +import { SearchClient, + KnownSearchAudience, SearchIndexClient, SearchIndexerClient, - AzureKeyCredential, - KnownSearchAudience, -} = require("@azure/search-documents"); +} from "@azure/search-documents"; // To query and manipulate documents const searchClient = new SearchClient( @@ -208,143 +202,123 @@ The following examples demonstrate the basics - please [check out our samples](h ### Create an Index -```js -const { SearchIndexClient, AzureKeyCredential } = require("@azure/search-documents"); +```ts snippet:ReadmeSampleCreateIndex +import { SearchIndexClient } from "@azure/search-documents"; const client = new SearchIndexClient("", new AzureKeyCredential("")); -async function main() { - const result = await client.createIndex({ - name: "example-index", - fields: [ - { - type: "Edm.String", - name: "id", - key: true, - }, - { - type: "Edm.Double", - name: "awesomenessLevel", - sortable: true, - filterable: true, - facetable: true, - }, - { - type: "Edm.String", - name: "description", - searchable: true, - }, - { - type: "Edm.ComplexType", - name: "details", - fields: [ - { - type: "Collection(Edm.String)", - name: "tags", - searchable: true, - }, - ], - }, - { - type: "Edm.Int32", - name: "hiddenWeight", - hidden: true, - }, - ], - }); - - console.log(result); -} +const result = await client.createIndex({ + name: "example-index", + fields: [ + { + type: "Edm.String", + name: "id", + key: true, + }, + { + type: "Edm.Double", + name: "awesomenessLevel", + sortable: true, + filterable: true, + facetable: true, + }, + { + type: "Edm.String", + name: "description", + searchable: true, + }, + { + type: "Edm.ComplexType", + name: "details", + fields: [ + { + type: "Collection(Edm.String)", + name: "tags", + searchable: true, + }, + ], + }, + { + type: "Edm.Int32", + name: "hiddenWeight", + hidden: true, + }, + ], +}); -main(); +console.log(`Created index with name ${result.name}`); ``` ### Retrieve a specific document from an index A specific document can be retrieved by its primary key value: -```js -const { SearchClient, AzureKeyCredential } = require("@azure/search-documents"); +```ts snippet:ReadmeSampleGetDocument +import { SearchClient } from "@azure/search-documents"; const client = new SearchClient("", "", new AzureKeyCredential("")); -async function main() { - const result = await client.getDocument("1234"); - console.log(result); -} - -main(); +const result = await client.getDocument("1234"); +console.log(result); ``` ### Adding documents into an index You can upload multiple documents into index inside a batch: -```js -const { SearchClient, AzureKeyCredential } = require("@azure/search-documents"); +```ts snippet:ReadmeSampleUploadDocuments +import { SearchClient } from "@azure/search-documents"; const client = new SearchClient("", "", new AzureKeyCredential("")); -async function main() { - const uploadResult = await client.uploadDocuments([ - // JSON objects matching the shape of the client's index - {}, - {}, - {}, - ]); - for (const result of uploadResult.results) { - console.log(`Uploaded ${result.key}; succeeded? ${result.succeeded}`); - } +const uploadResult = await client.uploadDocuments([ + // JSON objects matching the shape of the client's index + {}, + {}, + {}, +]); +for (const result of uploadResult.results) { + console.log(`Uploaded ${result.key}; succeeded? ${result.succeeded}`); } - -main(); ``` ### Perform a search on documents To list all results of a particular query, you can use `search` with a search string that uses [simple query syntax](https://learn.microsoft.com/azure/search/query-simple-syntax): -```js -const { SearchClient, AzureKeyCredential } = require("@azure/search-documents"); +```ts snippet:ReadmeSampleSearch +import { SearchClient } from "@azure/search-documents"; const client = new SearchClient("", "", new AzureKeyCredential("")); -async function main() { - const searchResults = await client.search("wifi -luxury"); - for await (const result of searchResults.results) { - console.log(result); - } +const searchResults = await client.search("wifi -luxury"); +for await (const result of searchResults.results) { + console.log(result); } - -main(); ``` For a more advanced search that uses [Lucene syntax](https://learn.microsoft.com/azure/search/query-lucene-syntax), specify `queryType` to be `full`: -```js -const { SearchClient, AzureKeyCredential } = require("@azure/search-documents"); +```ts snippet:ReadmeSampleSearchLucene +import { SearchClient } from "@azure/search-documents"; const client = new SearchClient("", "", new AzureKeyCredential("")); -async function main() { - const searchResults = await client.search('Category:budget AND "recently renovated"^3', { - queryType: "full", - searchMode: "all", - }); - for await (const result of searchResults.results) { - console.log(result); - } +const searchResults = await client.search('Category:budget AND "recently renovated"^3', { + queryType: "full", + searchMode: "all", +}); +for await (const result of searchResults.results) { + console.log(result); } - -main(); ``` #### Querying with TypeScript In TypeScript, `SearchClient` takes a generic parameter that is the model shape of your index documents. This allows you to perform strongly typed lookup of fields returned in results. TypeScript is also able to check for fields returned when specifying a `select` parameter. -```ts -import { SearchClient, AzureKeyCredential, SelectFields } from "@azure/search-documents"; +```ts snippet:ReadmeSampleSearchWithTypeScript +import { SearchClient, SelectFields } from "@azure/search-documents"; // An example schema for documents in the index interface Hotel { @@ -367,65 +341,57 @@ const client = new SearchClient( new AzureKeyCredential(""), ); -async function main() { - const searchResults = await client.search("wifi -luxury", { - // Only fields in Hotel can be added to this array. - // TS will complain if one is misspelled. - select: ["hotelId", "hotelName", "rooms/beds"], - }); - - // These are other ways to declare the correct type for `select`. - const select = ["hotelId", "hotelName", "rooms/beds"] as const; - // This declaration lets you opt out of narrowing the TypeScript type of your documents, - // though the AI Search service will still only return these fields. - const selectWide: SelectFields[] = ["hotelId", "hotelName", "rooms/beds"]; - // This is an invalid declaration. Passing this to `select` will result in a compiler error - // unless you opt out of including the model in the client constructor. - const selectInvalid = ["hotelId", "hotelName", "rooms/beds"]; - - for await (const result of searchResults.results) { - // result.document has hotelId, hotelName, and rating. - // Trying to access result.document.description would emit a TS error. - console.log(result.document.hotelName); - } -} +const searchResults = await client.search("wifi -luxury", { + // Only fields in Hotel can be added to this array. + // TS will complain if one is misspelled. + select: ["hotelId", "hotelName", "rooms/beds"], +}); -main(); +// These are other ways to declare the correct type for `select`. +const select = ["hotelId", "hotelName", "rooms/beds"] as const; +// This declaration lets you opt out of narrowing the TypeScript type of your documents, +// though the AI Search service will still only return these fields. +const selectWide: SelectFields[] = ["hotelId", "hotelName", "rooms/beds"]; +// This is an invalid declaration. Passing this to `select` will result in a compiler error +// unless you opt out of including the model in the client constructor. +const selectInvalid = ["hotelId", "hotelName", "rooms/beds"]; + +for await (const result of searchResults.results) { + // result.document has hotelId, hotelName, and rating. + // Trying to access result.document.description would emit a TS error. + console.log(result.document.hotelName); +} ``` #### Querying with OData filters Using the `filter` query parameter allows you to query an index using the syntax of an [OData \$filter expression](https://learn.microsoft.com/azure/search/search-query-odata-filter). -```js -const { SearchClient, AzureKeyCredential, odata } = require("@azure/search-documents"); +```ts snippet:ReadmeSampleSearchOData +import { SearchClient, odata } from "@azure/search-documents"; const client = new SearchClient("", "", new AzureKeyCredential("")); -async function main() { - const baseRateMax = 200; - const ratingMin = 4; - const searchResults = await client.search("WiFi", { - filter: odata`Rooms/any(room: room/BaseRate lt ${baseRateMax}) and Rating ge ${ratingMin}`, - orderBy: ["Rating desc"], - select: ["hotelId", "hotelName", "Rating"], - }); - for await (const result of searchResults.results) { - // Each result will have "HotelId", "HotelName", and "Rating" - // in addition to the standard search result property "score" - console.log(result); - } +const baseRateMax = 200; +const ratingMin = 4; +const searchResults = await client.search("WiFi", { + filter: odata`Rooms/any(room: room/BaseRate lt ${baseRateMax}) and Rating ge ${ratingMin}`, + orderBy: ["Rating desc"], + select: ["hotelId", "hotelName", "Rating"], +}); +for await (const result of searchResults.results) { + // Each result will have "HotelId", "HotelName", and "Rating" + // in addition to the standard search result property "score" + console.log(result); } - -main(); ``` #### Querying with vectors Text embeddings can be queried using the `vector` search parameter. See [Query vectors](https://learn.microsoft.com/azure/search/vector-search-how-to-query) and [Filter vector queries](https://learn.microsoft.com/azure/search/vector-search-filters) for more information. -```js -const { SearchClient, AzureKeyCredential } = require("@azure/search-documents"); +```ts snippet:ReadmeSampleSearchVector +import { SearchClient } from "@azure/search-documents"; const searchClient = new SearchClient( "", @@ -433,61 +399,40 @@ const searchClient = new SearchClient( new AzureKeyCredential(""), ); -async function main() { - const queryVector = [ - /*...*/ - ]; - const searchResults = await searchClient.search("*", { - vectorSearchOptions: { - queries: [ - { - kind: "vector", - vector: queryVector, - fields: ["descriptionVector"], - kNearestNeighborsCount: 3, - }, - ], - }, - }); - for await (const result of searchResults.results) { - // These results are the nearest neighbors to the query vector - console.log(result); - } +const queryVector = [ + /*...*/ +]; +const searchResults = await searchClient.search("*", { + vectorSearchOptions: { + queries: [ + { + kind: "vector", + vector: queryVector, + fields: ["descriptionVector"], + kNearestNeighborsCount: 3, + }, + ], + }, +}); +for await (const result of searchResults.results) { + // These results are the nearest neighbors to the query vector + console.log(result); } - -main(); ``` #### Querying with facets [Facets](https://learn.microsoft.com/azure/search/search-filters-facets) are used to help a user of your application refine a search along pre-configured dimensions. [Facet syntax](https://learn.microsoft.com/rest/api/searchservice/search-documents#facetstring-zero-or-more) provides the options to sort and bucket facet values. -```js -const { SearchClient, AzureKeyCredential } = require("@azure/search-documents"); +```ts snippet:ReadmeSampleSearchFacets +import { SearchClient } from "@azure/search-documents"; const client = new SearchClient("", "", new AzureKeyCredential("")); -async function main() { - const searchResults = await client.search("WiFi", { - facets: ["category,count:3,sort:count", "rooms/baseRate,interval:100"], - }); - console.log(searchResults.facets); - // Output will look like: - // { - // 'rooms/baseRate': [ - // { count: 16, value: 0 }, - // { count: 17, value: 100 }, - // { count: 17, value: 200 } - // ], - // category: [ - // { count: 5, value: 'Budget' }, - // { count: 5, value: 'Luxury' }, - // { count: 5, value: 'Resort and Spa' } - // ] - // } -} - -main(); +const searchResults = await client.search("WiFi", { + facets: ["category,count:3,sort:count", "rooms/baseRate,interval:100"], +}); +console.log(searchResults.facets); ``` When retrieving results, a `facets` property will be available that will indicate the number of results that fall into each facet bucket. This can be used to drive refinement (e.g. issuing a follow-up search that filters on the `Rating` being greater than or equal to 3 and less than 4.) @@ -498,7 +443,7 @@ When retrieving results, a `facets` property will be available that will indicat Enabling logging can 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`: -```javascript +```ts snippet:SetLogLevel import { setLogLevel } from "@azure/logger"; setLogLevel("info"); @@ -523,8 +468,6 @@ This project has adopted the [Microsoft Open Source Code of Conduct][coc]. For m - [Microsoft Azure SDK for JavaScript](https://github.com/Azure/azure-sdk-for-js) - - [azure_cli]: https://learn.microsoft.com/cli/azure [azure_sub]: https://azure.microsoft.com/free/ [search_resource]: https://learn.microsoft.com/azure/search/search-create-service-portal diff --git a/sdk/search/search-documents/src/odata.ts b/sdk/search/search-documents/src/odata.ts index d907d0115431..a44897722a05 100644 --- a/sdk/search/search-documents/src/odata.ts +++ b/sdk/search/search-documents/src/odata.ts @@ -26,7 +26,9 @@ function escapeQuotesIfString(input: unknown, previous: string): string | unknow /** * Escapes an odata filter expression to avoid errors with quoting string literals. * Example usage: - * ```ts + * ```ts snippet:ODataExample + * import { odata } from "@azure/search-documents"; + * * const baseRateMax = 200; * const ratingMin = 4; * const filter = odata`Rooms/any(room: room/BaseRate lt ${baseRateMax}) and Rating ge ${ratingMin}`; diff --git a/sdk/search/search-documents/src/searchClient.ts b/sdk/search/search-documents/src/searchClient.ts index 132fcb76c42a..3de86e2c76d0 100644 --- a/sdk/search/search-documents/src/searchClient.ts +++ b/sdk/search/search-documents/src/searchClient.ts @@ -128,26 +128,32 @@ export class SearchClient implements IndexDocumentsClient * Creates an instance of SearchClient. * * Example usage: - * ```ts - * const { SearchClient, AzureKeyCredential } = require("@azure/search-documents"); + * ```ts snippet:SearchClientCreate + * import { SearchClient } from "@azure/search-documents"; * - * const client = new SearchClient( + * const searchClient = new SearchClient( * "", * "", - * new AzureKeyCredential("") + * new AzureKeyCredential(""), * ); * ``` * * Optionally, the type of the model can be used to enable strong typing and type hints: - * ```ts + * ```ts snippet:SearchClientCreateWithModel + * import { SearchClient } from "@azure/search-documents"; + * * type TModel = { * keyName: string; * field1?: string | null; - * field2?: { anotherField?: string | null } | null; + * field2?: { + * anotherField?: string | null; + * } | null; * }; * * const client = new SearchClient( - * ... + * "", + * "", + * new AzureKeyCredential(""), * ); * ``` * @@ -252,31 +258,27 @@ export class SearchClient implements IndexDocumentsClient * @param suggesterName - The name of the suggester as specified in the suggesters collection that's part of the index definition. * @param options - Options to the autocomplete operation. * @example - * ```ts - * import { - * AzureKeyCredential, - * SearchClient, - * SearchFieldArray, - * } from "@azure/search-documents"; + * ```ts snippet:SearchClientAutocomplete + * import { SearchClient, SearchFieldArray } from "@azure/search-documents"; * * type TModel = { * key: string; - * azure?: { sdk: string | null } | null; + * azure?: { + * sdk: string | null; + * } | null; * }; * * const client = new SearchClient( * "endpoint.azure", * "indexName", - * new AzureKeyCredential("key") + * new AzureKeyCredential("key"), * ); * * const searchFields: SearchFieldArray = ["azure/sdk"]; * - * const autocompleteResult = await client.autocomplete( - * "searchText", - * "suggesterName", - * { searchFields } - * ); + * const autocompleteResult = await client.autocomplete("searchText", "suggesterName", { + * searchFields, + * }); * ``` */ public async autocomplete( @@ -480,22 +482,20 @@ export class SearchClient implements IndexDocumentsClient * @param searchText - Text to search * @param options - Options for the search operation. * @example - * ```ts - * import { - * AzureKeyCredential, - * SearchClient, - * SearchFieldArray, - * } from "@azure/search-documents"; + * ```ts snippet:SearchClientSearch + * import { SearchClient, SearchFieldArray } from "@azure/search-documents"; * * type TModel = { * key: string; - * azure?: { sdk: string | null } | null; + * azure?: { + * sdk: string | null; + * } | null; * }; * * const client = new SearchClient( * "endpoint.azure", * "indexName", - * new AzureKeyCredential("key") + * new AzureKeyCredential("key"), * ); * * const select = ["azure/sdk"] as const; @@ -538,22 +538,20 @@ export class SearchClient implements IndexDocumentsClient * @param suggesterName - The name of the suggester as specified in the suggesters collection that's part of the index definition. * @param options - Options for the suggest operation * @example - * ```ts - * import { - * AzureKeyCredential, - * SearchClient, - * SearchFieldArray, - * } from "@azure/search-documents"; + * ```ts snippet:SearchClientSuggest + * import { SearchClient, SearchFieldArray } from "@azure/search-documents"; * * type TModel = { * key: string; - * azure?: { sdk: string | null } | null; + * azure?: { + * sdk: string | null; + * } | null; * }; * * const client = new SearchClient( * "endpoint.azure", * "indexName", - * new AzureKeyCredential("key") + * new AzureKeyCredential("key"), * ); * * const select = ["azure/sdk"] as const; diff --git a/sdk/search/search-documents/src/searchIndexClient.ts b/sdk/search/search-documents/src/searchIndexClient.ts index a49ecebb777b..03ef57e22578 100644 --- a/sdk/search/search-documents/src/searchIndexClient.ts +++ b/sdk/search/search-documents/src/searchIndexClient.ts @@ -118,13 +118,10 @@ export class SearchIndexClient { * Creates an instance of SearchIndexClient. * * Example usage: - * ```ts - * const { SearchIndexClient, AzureKeyCredential } = require("@azure/search-documents"); + * ```ts snippet:SearchIndexClientCreate + * import { SearchIndexClient } from "@azure/search-documents"; * - * const client = new SearchIndexClient( - * "", - * new AzureKeyCredential(""); - * ); + * const indexClient = new SearchIndexClient("", new AzureKeyCredential("")); * ``` * @param endpoint - The endpoint of the search service * @param credential - Used to authenticate requests to the service. diff --git a/sdk/search/search-documents/src/searchIndexerClient.ts b/sdk/search/search-documents/src/searchIndexerClient.ts index e8a39d3be6ec..dd238134ad91 100644 --- a/sdk/search/search-documents/src/searchIndexerClient.ts +++ b/sdk/search/search-documents/src/searchIndexerClient.ts @@ -101,13 +101,10 @@ export class SearchIndexerClient { * Creates an instance of SearchIndexerClient. * * Example usage: - * ```ts - * const { SearchIndexerClient, AzureKeyCredential } = require("@azure/search-documents"); + * ```ts snippet:SearchIndexerClientCreate + * import { SearchIndexerClient } from "@azure/search-documents"; * - * const client = new SearchIndexerClient( - * "", - * new AzureKeyCredential(""); - * ); + * const indexerClient = new SearchIndexerClient("", new AzureKeyCredential("")); * ``` * @param endpoint - The endpoint of the search service * @param credential - Used to authenticate requests to the service. diff --git a/sdk/search/search-documents/test/narrowedTypes.ts b/sdk/search/search-documents/test/narrowedTypes.ts index e69de29bb2d1..9e31279120f1 100644 --- a/sdk/search/search-documents/test/narrowedTypes.ts +++ b/sdk/search/search-documents/test/narrowedTypes.ts @@ -0,0 +1,477 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +/* eslint-disable no-unused-expressions */ +/* eslint-disable @typescript-eslint/explicit-function-return-type */ + +import type { SelectFields } from "../src/index.js"; +import { SearchClient } from "../src/index.js"; +import type { + NarrowedModel as GenericNarrowedModel, + SearchFieldArray, + SearchPick, + SelectArray, + SuggestNarrowedModel, +} from "../src/indexModels.js"; + +type Equals = + (() => T extends T1 ? true : false) extends () => T extends T2 ? true : false ? any : never; + +type Model = { + key?: string; + a?: string | null; + b?: { a?: string | null; b?: string | null } | null; + c?: Array; + d?: Array<{ a?: string | null; b?: { a?: string | null; b?: string | null } | null }>; +}; + +type ModelFields = + | "key" + | "a" + | "b" + | "b/a" + | "b/b" + | "c" + | "d" + | "d/a" + | "d/b" + | "d/b/a" + | "d/b/b"; + +type NarrowedModel = { + key?: string; + a?: string | null; + b?: { a?: string | null } | null; + d?: Array<{ b?: { a?: string | null; b?: string | null } | null }>; +}; + +type NarrowedModelFields = "key" | "a" | "b/a" | "d/b"; + +// @ts-expect-error Needs TypeScript fixes +function testSelectFields() { + // SelectFields where T has properties should be T, + // otherwise string + const a: Equals, string> = "pass"; + const b: Equals, string> = "pass"; + const c: Equals, string> = "pass"; + // @ts-expect-error Needs TypeScript fixes + const d: Equals, ModelFields> = "pass"; + + // SelectFields should be an error, as unknown should be cast + // @ts-expect-error Needs TypeScript fixes + const e: Equals, string> = "fail"; + + return [a, b, c, d, e]; +} + +// @ts-expect-error Needs TypeScript fixes +function testSearchPick() { + // SearchPick should be Model when Fields is not a strict subset of + // SelectFields + const a: Equals, object> = "pass"; + const b: Equals, Model> = "pass"; + const c: Equals, Model> = "pass"; + const d1: Model = {} as SearchPick; + const d2: SearchPick = {} as Model; + // SearchPick should correctly narrow the model to the specified fields + const e1: NarrowedModel = {} as SearchPick; + const e2: SearchPick = {} as NarrowedModel; + + // A narrowed model should be assignable to the base model type + const f: Model = {} as SearchPick; + + // If the model has unknown properties, SearchPick should yield the object type + const g: Equals, object> = "pass"; + const h: Equals, object> = "pass"; + const i: Equals, object> = "pass"; + + // SearchPick should reject the string type when using a model with known properties + // Ideally, this would just yield the model type, but we haven't found a way to make that happen + // without losing the type inference that powers the IntelliSense behavior we want. + // @ts-expect-error Needs TypeScript fixes + const j: Equals, Model> = "fail"; + // @ts-expect-error Needs TypeScript fixes + const k: Equals, Model> = "fail"; + + return [a, b, c, d1, d2, e1, e2, f, g, h, i, j, k]; +} + +// @ts-expect-error Needs TypeScript fixes +function testNarrowedModel() { + const a1: GenericNarrowedModel = {} as NarrowedModel; + const a2: NarrowedModel = {} as GenericNarrowedModel; + + // Narrowing the never type should yield never + const b: Equals, never> = "pass"; + const c: Equals, never> = "pass"; + const d: Equals, never> = "pass"; + const e: Equals, never> = "pass"; + // Narrowing the object type should yield object + const f: Equals, object> = "pass"; + const g: Equals, object> = "pass"; + const h: Equals, object> = "pass"; + const i: Equals, object> = "pass"; + // Narrowing a model with never fields implies an empty array was used to select fields, + // so either never or an empty object are sensible. We're rolling with never since that behavior + // is less likely to break if we find a convincing reason to change it. + const j: Equals, never> = "pass"; + + // Narrowing a model with any fields should yield the model itself + const k: Equals, Model> = "pass"; + + const l1: SuggestNarrowedModel = {} as NarrowedModel; + const l2: NarrowedModel = {} as SuggestNarrowedModel; + + const m: Equals, never> = "pass"; + const n: Equals, never> = "pass"; + + const o: Equals, never> = "pass"; + const p: Equals, never> = "pass"; + const q: Equals, object> = "pass"; + const r: Equals, object> = "pass"; + const s: Equals, object> = "pass"; + const t: Equals, object> = "pass"; + // SuggestNarrowedModel is the same story as above, but this never case corresponds to the + // default behavior of the service, which is to return only the document key. To this end, + // SuggestNarrowedModel with never fields yields an interface with only the non-null properties + // of the model. + const u: Equals, { key?: string }> = "pass"; + const v: Equals, Model> = "pass"; + + // Passing unknown or string as fields are type errors + // @ts-expect-error Needs TypeScript fixes + const w: Equals, object> = "fail"; + // @ts-expect-error Needs TypeScript fixes + const x: Equals, never> = "fail"; + // @ts-expect-error Needs TypeScript fixes + const y: Equals, Model> = "fail"; + // @ts-expect-error Needs TypeScript fixes + const z: Equals, Model> = "fail"; + + // @ts-expect-error Needs TypeScript fixes + const aa: Equals, never> = "fail"; + // @ts-expect-error Needs TypeScript fixes + const ab: Equals, object> = "fail"; + // @ts-expect-error Needs TypeScript fixes + const ac: Equals, Model> = "fail"; + // @ts-expect-error Needs TypeScript fixes + const ad: Equals, Model> = "fail"; + + return [ + a1, + a2, + b, + c, + d, + e, + f, + g, + h, + i, + j, + k, + l1, + l2, + m, + n, + o, + p, + q, + r, + s, + t, + u, + v, + w, + x, + y, + z, + aa, + ab, + ac, + ad, + ]; +} + +// @ts-expect-error Needs TypeScript fixes +function testSelectArray() { + const a: Equals, readonly string[]> = "pass"; + const b: Equals, readonly "field1"[]> = "pass"; + const c: Equals, readonly ("field1" | "field2")[]> = "pass"; + + // @ts-expect-error Needs TypeScript fixes + const d: Equals, readonly string[]> = "fail"; + // @ts-expect-error Needs TypeScript fixes + const e: Equals, readonly string[]> = "fail"; + return [a, b, c, d, e]; +} + +// @ts-expect-error Needs TypeScript fixes +function testSearchFieldArray() { + const a: Equals, readonly string[]> = "pass"; + // @ts-expect-error Needs TypeScript fixes + const b: Equals, readonly ModelFields[]> = "pass"; + const c: Equals, readonly string[]> = "pass"; + const d: Equals, readonly string[]> = "pass"; + + // @ts-expect-error Needs TypeScript fixes + const e: Equals, readonly string[]> = "pass"; + + return [a, b, c, d, e]; +} + +// @ts-expect-error Needs TypeScript fixes +function testNarrowedClient() { + const client = new SearchClient("azure", "sdk", "js" as any, {}); + + const select: SelectFields[] = ["key", "a", "b/a", "d/b"]; + const selectNarrowed = ["key", "a", "b/a", "d/b"] as const; + + async () => { + const suggest = await client.suggest("", "", {}); + for await (const result of suggest.results) { + const a: Equals< + (typeof result)["document"], + { + key?: string; + } + > = "pass"; + // @ts-expect-error Needs TypeScript fixes + const b = result.document.a; + return [a, b]; + } + return; + }; + + async () => { + type VectorFields = NonNullable< + NonNullable< + NonNullable< + NonNullable[1]>["vectorSearchOptions"] + >["queries"] + >[number]["fields"] + >; + // @ts-expect-error Needs TypeScript fixes + const a: Equals = "pass"; + return a; + }; + + async () => { + const select1 = await client.search("New", { + select, + }); + const select2 = await client.search("New", { + select: selectNarrowed, + }); + const select3 = await client.search("New", { + select: ["key", "a", "b/a", "d/b"], + }); + + const suppressUnusedWarning: any[] = []; + + for await (const result of select1.results) { + const a: Equals<(typeof result)["document"], Model> = "pass"; + const b = result.document.c; + suppressUnusedWarning.push(a); + suppressUnusedWarning.push(b); + } + for await (const result of select2.results) { + const a1: (typeof result)["document"] = {} as NarrowedModel; + const a2: NarrowedModel = {} as (typeof result)["document"]; + // @ts-expect-error Needs TypeScript fixes + const b = result.document.c; + suppressUnusedWarning.push(a1); + suppressUnusedWarning.push(a2); + suppressUnusedWarning.push(b); + } + for await (const result of select3.results) { + const a1: (typeof result)["document"] = {} as NarrowedModel; + const a2: NarrowedModel = {} as (typeof result)["document"]; + // @ts-expect-error Needs TypeScript fixes + const b = result.document.c; + suppressUnusedWarning.push(a1); + suppressUnusedWarning.push(a2); + suppressUnusedWarning.push(b); + } + return suppressUnusedWarning; + }; + async () => { + const select1 = await client.search("New", { + searchFields: select, + }); + const select2 = await client.search("New", { + searchFields: selectNarrowed, + }); + const select3 = await client.search("New", { + searchFields: ["key", "a", "b/a", "d/b"], + }); + + const suppressUnusedWarning: any[] = []; + + for await (const result of select1.results) { + const a: Equals<(typeof result)["document"], Model> = "pass"; + suppressUnusedWarning.push(a); + } + for await (const result of select2.results) { + const a: Equals<(typeof result)["document"], Model> = "pass"; + suppressUnusedWarning.push(a); + } + for await (const result of select3.results) { + const a: Equals<(typeof result)["document"], Model> = "pass"; + suppressUnusedWarning.push(a); + } + return suppressUnusedWarning; + }; + async () => { + const select1 = await client.search("New", { + select, + searchFields: select, + }); + const select2 = await client.search("New", { + select: selectNarrowed, + searchFields: selectNarrowed, + }); + const select3 = await client.search("New", { + select: ["key", "a", "b/a", "d/b"], + searchFields: ["key", "a", "b/a", "d/b"], + }); + + const suppressUnusedWarning: any[] = []; + + for await (const result of select1.results) { + const a: Equals<(typeof result)["document"], Model> = "pass"; + suppressUnusedWarning.push(a); + } + for await (const result of select2.results) { + const a1: (typeof result)["document"] = {} as NarrowedModel; + const a2: NarrowedModel = {} as (typeof result)["document"]; + // @ts-expect-error Needs TypeScript fixes + const b = result.document.c; + suppressUnusedWarning.push(a1); + suppressUnusedWarning.push(a2); + suppressUnusedWarning.push(b); + } + for await (const result of select3.results) { + const a1: (typeof result)["document"] = {} as NarrowedModel; + const a2: NarrowedModel = {} as (typeof result)["document"]; + // @ts-expect-error Needs TypeScript fixes + const b = result.document.c; + suppressUnusedWarning.push(a1); + suppressUnusedWarning.push(a2); + suppressUnusedWarning.push(b); + } + return suppressUnusedWarning; + }; +} + +// @ts-expect-error Needs TypeScript fixes +function testWideClient() { + const client = new SearchClient("azure", "sdk", "js" as any, {}); + + const select: string[] = ["key", "a", "b/a", "d/b"]; + const selectNarrowed = ["key", "a", "b/a", "d/b"] as const; + + async () => { + const suggest = await client.suggest("", "", {}); + for await (const result of suggest.results) { + const a: Equals<(typeof result)["document"], object> = "pass"; + return a; + } + }; + + async () => { + type VectorFields = NonNullable< + NonNullable< + NonNullable< + NonNullable[1]>["vectorSearchOptions"] + >["queries"] + >[number]["fields"] + >; + const a: Equals = "pass"; + return a; + }; + + async () => { + const select1 = await client.search("New", { + select, + }); + const select2 = await client.search("New", { + select: selectNarrowed, + }); + const select3 = await client.search("New", { + select: ["key", "a", "b/a", "d/b"], + }); + + const suppressUnusedWarning: any[] = []; + + for await (const result of select1.results) { + const a: Equals<(typeof result)["document"], object> = "pass"; + suppressUnusedWarning.push(a); + } + for await (const result of select2.results) { + const a: Equals<(typeof result)["document"], object> = "pass"; + suppressUnusedWarning.push(a); + } + for await (const result of select3.results) { + const a: Equals<(typeof result)["document"], object> = "pass"; + suppressUnusedWarning.push(a); + } + }; + async () => { + const select1 = await client.search("New", { + searchFields: select, + }); + const select2 = await client.search("New", { + searchFields: selectNarrowed, + }); + const select3 = await client.search("New", { + searchFields: ["key", "a", "b/a", "d/b"], + }); + + const suppressUnusedWarning: any[] = []; + + for await (const result of select1.results) { + const a: Equals<(typeof result)["document"], object> = "pass"; + suppressUnusedWarning.push(a); + } + for await (const result of select2.results) { + const a: Equals<(typeof result)["document"], object> = "pass"; + suppressUnusedWarning.push(a); + } + for await (const result of select3.results) { + const a: Equals<(typeof result)["document"], object> = "pass"; + suppressUnusedWarning.push(a); + } + return suppressUnusedWarning; + }; + async () => { + const select1 = await client.search("New", { + select, + searchFields: select, + }); + const select2 = await client.search("New", { + select: selectNarrowed, + searchFields: selectNarrowed, + }); + const select3 = await client.search("New", { + select: ["key", "a", "b/a", "d/b"], + searchFields: ["key", "a", "b/a", "d/b"], + }); + + const suppressUnusedWarning: any[] = []; + + for await (const result of select1.results) { + const a: Equals<(typeof result)["document"], object> = "pass"; + suppressUnusedWarning.push(a); + } + for await (const result of select2.results) { + const a: Equals<(typeof result)["document"], object> = "pass"; + suppressUnusedWarning.push(a); + } + for await (const result of select3.results) { + const a: Equals<(typeof result)["document"], object> = "pass"; + suppressUnusedWarning.push(a); + } + return suppressUnusedWarning; + }; +} diff --git a/sdk/search/search-documents/test/public/node/searchClient.spec.ts b/sdk/search/search-documents/test/public/node/searchClient.spec.ts index 718ab5cb247a..3b0e3987c008 100644 --- a/sdk/search/search-documents/test/public/node/searchClient.spec.ts +++ b/sdk/search/search-documents/test/public/node/searchClient.spec.ts @@ -229,8 +229,7 @@ describe("SearchClient", { timeout: 20_000 }, () => { select: ["address/city"], }); for await (const result of response.results) { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-expect-error + // @ts-expect-error Needs fixes from TypeScript result.document.category = ""; } }; diff --git a/sdk/search/search-documents/test/snippets.spec.ts b/sdk/search/search-documents/test/snippets.spec.ts index 108b0287b5c1..84e1c24d8f3b 100644 --- a/sdk/search/search-documents/test/snippets.spec.ts +++ b/sdk/search/search-documents/test/snippets.spec.ts @@ -1,24 +1,377 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -import { AstroManagementClient } from "../src/index.js"; -import { DefaultAzureCredential, InteractiveBrowserCredential } from "@azure/identity"; +import { + KnownSearchAudience, + odata, + SearchClient, + SearchFieldArray, + SearchIndexClient, + SearchIndexerClient, + SelectFields, +} from "../src/index.js"; import { setLogLevel } from "@azure/logger"; import { describe, it } from "vitest"; describe("snippets", () => { - it("ReadmeSampleCreateClient_Node", async () => { - const subscriptionId = "00000000-0000-0000-0000-000000000000"; - const client = new AstroManagementClient(new DefaultAzureCredential(), subscriptionId); + it("ReadmeSampleCreateClient_ApiKey", async () => { + // To query and manipulate documents + const searchClient = new SearchClient( + "", + "", + new AzureKeyCredential(""), + ); + // @ts-preserve-whitespace + // To manage indexes and synonymmaps + const indexClient = new SearchIndexClient("", new AzureKeyCredential("")); + // @ts-preserve-whitespace + // To manage indexers, datasources and skillsets + const indexerClient = new SearchIndexerClient("", new AzureKeyCredential("")); }); - it("ReadmeSampleCreateClient_Browser", async () => { - const subscriptionId = "00000000-0000-0000-0000-000000000000"; - const credential = new InteractiveBrowserCredential({ - tenantId: "", - clientId: "", + it("ReadmeSampleCreateClient_NationalCloud", async () => { + // To query and manipulate documents + const searchClient = new SearchClient( + "", + "", + new AzureKeyCredential(""), + { + audience: KnownSearchAudience.AzureChina, + }, + ); + // @ts-preserve-whitespace + // To manage indexes and synonymmaps + const indexClient = new SearchIndexClient("", new AzureKeyCredential(""), { + audience: KnownSearchAudience.AzureChina, }); - const client = new AstroManagementClient(credential, subscriptionId); + // @ts-preserve-whitespace + // To manage indexers, datasources and skillsets + const indexerClient = new SearchIndexerClient( + "", + new AzureKeyCredential(""), + { + audience: KnownSearchAudience.AzureChina, + }, + ); + }); + + it("ReadmeSampleCreateIndex", async () => { + const client = new SearchIndexClient("", new AzureKeyCredential("")); + // @ts-preserve-whitespace + const result = await client.createIndex({ + name: "example-index", + fields: [ + { + type: "Edm.String", + name: "id", + key: true, + }, + { + type: "Edm.Double", + name: "awesomenessLevel", + sortable: true, + filterable: true, + facetable: true, + }, + { + type: "Edm.String", + name: "description", + searchable: true, + }, + { + type: "Edm.ComplexType", + name: "details", + fields: [ + { + type: "Collection(Edm.String)", + name: "tags", + searchable: true, + }, + ], + }, + { + type: "Edm.Int32", + name: "hiddenWeight", + hidden: true, + }, + ], + }); + // @ts-preserve-whitespace + console.log(`Created index with name ${result.name}`); + }); + + it("ReadmeSampleGetDocument", async () => { + const client = new SearchClient( + "", + "", + new AzureKeyCredential(""), + ); + // @ts-preserve-whitespace + const result = await client.getDocument("1234"); + console.log(result); + }); + + it("ReadmeSampleUploadDocuments", async () => { + const client = new SearchClient( + "", + "", + new AzureKeyCredential(""), + ); + // @ts-preserve-whitespace + const uploadResult = await client.uploadDocuments([ + // JSON objects matching the shape of the client's index + {}, + {}, + {}, + ]); + for (const result of uploadResult.results) { + console.log(`Uploaded ${result.key}; succeeded? ${result.succeeded}`); + } + }); + + it("ReadmeSampleSearch", async () => { + const client = new SearchClient( + "", + "", + new AzureKeyCredential(""), + ); + // @ts-preserve-whitespace + const searchResults = await client.search("wifi -luxury"); + for await (const result of searchResults.results) { + console.log(result); + } + }); + + it("ReadmeSampleSearchLucene", async () => { + const client = new SearchClient( + "", + "", + new AzureKeyCredential(""), + ); + // @ts-preserve-whitespace + const searchResults = await client.search('Category:budget AND "recently renovated"^3', { + queryType: "full", + searchMode: "all", + }); + for await (const result of searchResults.results) { + console.log(result); + } + }); + + it("ReadmeSampleSearchWithTypeScript", async () => { + // An example schema for documents in the index + interface Hotel { + hotelId?: string; + hotelName?: string | null; + description?: string | null; + descriptionVector?: Array; + parkingIncluded?: boolean | null; + lastRenovationDate?: Date | null; + rating?: number | null; + rooms?: Array<{ + beds?: number | null; + description?: string | null; + }>; + } + // @ts-preserve-whitespace + const client = new SearchClient( + "", + "", + new AzureKeyCredential(""), + ); + // @ts-preserve-whitespace + const searchResults = await client.search("wifi -luxury", { + // Only fields in Hotel can be added to this array. + // TS will complain if one is misspelled. + select: ["hotelId", "hotelName", "rooms/beds"], + }); + // @ts-preserve-whitespace + // These are other ways to declare the correct type for `select`. + const select = ["hotelId", "hotelName", "rooms/beds"] as const; + // This declaration lets you opt out of narrowing the TypeScript type of your documents, + // though the AI Search service will still only return these fields. + const selectWide: SelectFields[] = ["hotelId", "hotelName", "rooms/beds"]; + // This is an invalid declaration. Passing this to `select` will result in a compiler error + // unless you opt out of including the model in the client constructor. + const selectInvalid = ["hotelId", "hotelName", "rooms/beds"]; + // @ts-preserve-whitespace + for await (const result of searchResults.results) { + // result.document has hotelId, hotelName, and rating. + // Trying to access result.document.description would emit a TS error. + console.log(result.document.hotelName); + } + }); + + it("ReadmeSampleSearchOData", async () => { + const client = new SearchClient( + "", + "", + new AzureKeyCredential(""), + ); + // @ts-preserve-whitespace + const baseRateMax = 200; + const ratingMin = 4; + const searchResults = await client.search("WiFi", { + filter: odata`Rooms/any(room: room/BaseRate lt ${baseRateMax}) and Rating ge ${ratingMin}`, + orderBy: ["Rating desc"], + select: ["hotelId", "hotelName", "Rating"], + }); + for await (const result of searchResults.results) { + // Each result will have "HotelId", "HotelName", and "Rating" + // in addition to the standard search result property "score" + console.log(result); + } + }); + + it("ReadmeSampleSearchVector", async () => { + const searchClient = new SearchClient( + "", + "", + new AzureKeyCredential(""), + ); + // @ts-preserve-whitespace + const queryVector = [ + /*...*/ + ]; + const searchResults = await searchClient.search("*", { + vectorSearchOptions: { + queries: [ + { + kind: "vector", + vector: queryVector, + fields: ["descriptionVector"], + kNearestNeighborsCount: 3, + }, + ], + }, + }); + for await (const result of searchResults.results) { + // These results are the nearest neighbors to the query vector + console.log(result); + } + }); + + it("ReadmeSampleSearchFacets", async () => { + const client = new SearchClient( + "", + "", + new AzureKeyCredential(""), + ); + // @ts-preserve-whitespace + const searchResults = await client.search("WiFi", { + facets: ["category,count:3,sort:count", "rooms/baseRate,interval:100"], + }); + console.log(searchResults.facets); + // Output will look like: + // { + // 'rooms/baseRate': [ + // { count: 16, value: 0 }, + // { count: 17, value: 100 }, + // { count: 17, value: 200 } + // ], + // category: [ + // { count: 5, value: 'Budget' }, + // { count: 5, value: 'Luxury' }, + // { count: 5, value: 'Resort and Spa' } + // ] + // } + }); + + it("SearchClientCreate", async () => { + const searchClient = new SearchClient( + "", + "", + new AzureKeyCredential(""), + ); + }); + + it("SearchClientCreateWithModel", async () => { + type TModel = { + keyName: string; + field1?: string | null; + field2?: { anotherField?: string | null } | null; + }; + // @ts-preserve-whitespace + const client = new SearchClient( + "", + "", + new AzureKeyCredential(""), + ); + }); + + it("SearchClientAutocomplete", async () => { + type TModel = { + key: string; + azure?: { sdk: string | null } | null; + }; + // @ts-preserve-whitespace + const client = new SearchClient( + "endpoint.azure", + "indexName", + new AzureKeyCredential("key"), + ); + // @ts-preserve-whitespace + const searchFields: SearchFieldArray = ["azure/sdk"]; + // @ts-preserve-whitespace + const autocompleteResult = await client.autocomplete("searchText", "suggesterName", { + searchFields, + }); + }); + + it("SearchClientSearch", async () => { + type TModel = { + key: string; + azure?: { sdk: string | null } | null; + }; + // @ts-preserve-whitespace + const client = new SearchClient( + "endpoint.azure", + "indexName", + new AzureKeyCredential("key"), + ); + // @ts-preserve-whitespace + const select = ["azure/sdk"] as const; + const searchFields: SearchFieldArray = ["azure/sdk"]; + // @ts-preserve-whitespace + const searchResult = await client.search("searchText", { + select, + searchFields, + }); + }); + + it("SearchClientSuggest", async () => { + type TModel = { + key: string; + azure?: { sdk: string | null } | null; + }; + // @ts-preserve-whitespace + const client = new SearchClient( + "endpoint.azure", + "indexName", + new AzureKeyCredential("key"), + ); + // @ts-preserve-whitespace + const select = ["azure/sdk"] as const; + const searchFields: SearchFieldArray = ["azure/sdk"]; + // @ts-preserve-whitespace + const suggestResult = await client.suggest("searchText", "suggesterName", { + select, + searchFields, + }); + }); + + it("ODataExample", async () => { + const baseRateMax = 200; + const ratingMin = 4; + const filter = odata`Rooms/any(room: room/BaseRate lt ${baseRateMax}) and Rating ge ${ratingMin}`; + }); + + it("SearchIndexClientCreate", async () => { + const indexClient = new SearchIndexClient("", new AzureKeyCredential("")); + }); + + it("SearchIndexerClientCreate", async () => { + const indexerClient = new SearchIndexerClient("", new AzureKeyCredential("")); }); it("SetLogLevel", async () => {