Skip to content

Commit

Permalink
Merge 206867a into 3183412
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilyZhang777 committed May 4, 2023
2 parents 3183412 + 206867a commit c0d77f0
Show file tree
Hide file tree
Showing 23 changed files with 234 additions and 51 deletions.
3 changes: 2 additions & 1 deletion docs/search-core.basesearchconfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ The base configuration options for [SearchCore](./search-core.searchcore.md)<!--
<b>Signature:</b>

```typescript
export interface BaseSearchConfig
export interface BaseSearchConfig extends ServingConfig
```
<b>Extends:</b> [ServingConfig](./search-core.servingconfig.md)
## Properties
Expand Down
2 changes: 2 additions & 0 deletions docs/search-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
| Function | Description |
| --- | --- |
| [provideCore(config)](./search-core.providecore.md) | The entrypoint to the search-core library. |
| [provideEndpoints(config)](./search-core.provideendpoints.md) | Provides all endpoints based on environment and cloud region. |

## Interfaces

Expand Down Expand Up @@ -100,6 +101,7 @@
| [SearchParameterField](./search-core.searchparameterfield.md) | Indicates which entity field to perform the autocomplete request on. |
| [SearchRequest](./search-core.searchrequest.md) | Options for a Search API request. |
| [SearchService](./search-core.searchservice.md) | A service which performs Yext Search. |
| [ServingConfig](./search-core.servingconfig.md) | The configuration options for [provideEndpoints()](./search-core.provideendpoints.md)<!-- -->. |
| [Snippet](./search-core.snippet.md) | The section of text where a [FeaturedSnippetDirectAnswer](./search-core.featuredsnippetdirectanswer.md) was found. |
| [SortBy](./search-core.sortby.md) | Represents a criterion that can be used to sort results. |
| [SpellCheck](./search-core.spellcheck.md) | A spellcheck response from a search query. |
Expand Down
28 changes: 28 additions & 0 deletions docs/search-core.provideendpoints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@yext/search-core](./search-core.md) &gt; [provideEndpoints](./search-core.provideendpoints.md)

## provideEndpoints() function

Provides all endpoints based on environment and cloud region.

<b>Signature:</b>

```typescript
export declare function provideEndpoints(config?: ServingConfig): Required<Endpoints>;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| config | [ServingConfig](./search-core.servingconfig.md) | serving config to use for the domain, defaults to Prod and US if not provided. |

<b>Returns:</b>

Required&lt;[Endpoints](./search-core.endpoints.md)<!-- -->&gt;

## Remarks

Returns an [Endpoints](./search-core.endpoints.md) instance.

5 changes: 5 additions & 0 deletions docs/search-core.sandboxendpoints.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

## SandboxEndpoints variable

> Warning: This API is now obsolete.
>
> Use [provideEndpoints()](./search-core.provideendpoints.md) instead
>
The endpoints to use for sandbox experiences.

<b>Signature:</b>
Expand Down
12 changes: 12 additions & 0 deletions docs/search-core.servingconfig.cloudregion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@yext/search-core](./search-core.md) &gt; [ServingConfig](./search-core.servingconfig.md) &gt; [cloudRegion](./search-core.servingconfig.cloudregion.md)

## ServingConfig.cloudRegion property


<b>Signature:</b>

```typescript
cloudRegion?: CloudRegion;
```
12 changes: 12 additions & 0 deletions docs/search-core.servingconfig.environment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@yext/search-core](./search-core.md) &gt; [ServingConfig](./search-core.servingconfig.md) &gt; [environment](./search-core.servingconfig.environment.md)

## ServingConfig.environment property


<b>Signature:</b>

```typescript
environment?: Environment;
```
21 changes: 21 additions & 0 deletions docs/search-core.servingconfig.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@yext/search-core](./search-core.md) &gt; [ServingConfig](./search-core.servingconfig.md)

## ServingConfig interface

The configuration options for [provideEndpoints()](./search-core.provideendpoints.md)<!-- -->.

<b>Signature:</b>

```typescript
export interface ServingConfig
```

## Properties

| Property | Type | Description |
| --- | --- | --- |
| [cloudRegion?](./search-core.servingconfig.cloudregion.md) | CloudRegion | <i>(Optional)</i> |
| [environment?](./search-core.servingconfig.environment.md) | Environment | <i>(Optional)</i> |

21 changes: 19 additions & 2 deletions etc/search-core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export interface BaseFieldValueDirectAnswer<T = unknown> extends DirectAnswer<T>
}

// @public
export interface BaseSearchConfig {
export interface BaseSearchConfig extends ServingConfig {
additionalQueryParams?: {
[key: string]: string | number | boolean;
};
Expand Down Expand Up @@ -511,6 +511,9 @@ export interface PhoneDirectAnswer extends BaseFieldValueDirectAnswer<string> {
// @public
export function provideCore(config: SearchConfig): SearchCore;

// @public
export function provideEndpoints(config?: ServingConfig): Required<Endpoints>;

// @public
export interface QueryRulesActionsData {
data?: Record<string, unknown>;
Expand Down Expand Up @@ -581,7 +584,7 @@ export interface RichTextSnippetDirectAnswer extends Omit<BaseFeaturedSnippetDir
fieldType: EnumOrLiteral<BuiltInFieldType.RichText>;
}

// @public
// @public @deprecated
export const SandboxEndpoints: Required<Endpoints>;

// @public
Expand Down Expand Up @@ -642,6 +645,20 @@ export interface SearchService {
verticalSearch(request: VerticalSearchRequest): Promise<VerticalSearchResponse>;
}

// @public
export interface ServingConfig {
// Warning: (ae-forgotten-export) The symbol "CloudRegion" needs to be exported by the entry point index.d.ts
// Warning: (ae-unresolved-inheritdoc-reference) The @inheritDoc reference could not be resolved: The package "@yext/search-core" does not have an export "CloudRegion"
//
// (undocumented)
cloudRegion?: CloudRegion;
// Warning: (ae-forgotten-export) The symbol "Environment" needs to be exported by the entry point index.d.ts
// Warning: (ae-unresolved-inheritdoc-reference) The @inheritDoc reference could not be resolved: The package "@yext/search-core" does not have an export "Environment"
//
// (undocumented)
environment?: Environment;
}

// @public
export interface Snippet {
matchedSubstrings: {
Expand Down
28 changes: 0 additions & 28 deletions src/constants.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// main symbols
export { SearchCore } from './SearchCore';
export { provideCore } from './provideCore';
export { SandboxEndpoints } from './constants';
export { provideEndpoints, SandboxEndpoints } from './provideEndpoints';
export * from './models';
export * from './services';
2 changes: 1 addition & 1 deletion src/infra/AutocompleteServiceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
UniversalAutocompleteRequest
} from '../models/autocompleteservice/AutocompleteRequest';
import { AutocompleteResponse, FilterSearchResponse } from '../models/autocompleteservice/AutocompleteResponse';
import { defaultApiVersion } from '../constants';
import { defaultApiVersion } from '../provideEndpoints';
import { SearchConfigWithDefaulting } from '../models/core/SearchConfig';
import { HttpService } from '../services/HttpService';
import { AutocompleteQueryParams } from '../models/autocompleteservice/AutocompleteQueryParams';
Expand Down
2 changes: 1 addition & 1 deletion src/infra/QuestionSubmissionServiceImpl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defaultApiVersion } from '../constants';
import { defaultApiVersion } from '../provideEndpoints';
import { QuestionSubmissionService } from '../services/QuestionSubmissionService';
import { HttpService } from '../services/HttpService';
import { SearchConfigWithDefaulting } from '../models/core/SearchConfig';
Expand Down
2 changes: 1 addition & 1 deletion src/infra/SearchServiceImpl.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createVerticalSearchResponse } from '../transformers/searchservice/createVerticalSearchResponse';
import { SearchService } from '../services/SearchService';
import { defaultApiVersion } from '../constants';
import { defaultApiVersion } from '../provideEndpoints';
import { QueryParams } from '../models/http/params';
import { QueryTrigger } from '../models/searchservice/request/QueryTrigger';
import { QuerySource } from '../models/searchservice/request/QuerySource';
Expand Down
9 changes: 9 additions & 0 deletions src/models/core/CloudRegion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Defines the cloud region of the API domains.
*
* @public
*/
export enum CloudRegion {
US = 'us',
EU = 'eu',
}
9 changes: 9 additions & 0 deletions src/models/core/Environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Defines the environment of the API domains.
*
* @public
*/
export enum Environment {
PROD = 'prod',
SANDBOX = 'sbx',
}
24 changes: 23 additions & 1 deletion src/models/core/SearchConfig.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
import { Endpoints } from './Endpoints';
import { Visitor } from './Visitor';
import { Environment } from './Environment';
import { CloudRegion } from './CloudRegion';

/**
* The configuration options for {@link provideEndpoints}.
*
* @public
*/
export interface ServingConfig {
/**
* {@inheritDoc Environment}
*
* @public
*/
environment?: Environment,
/**
* {@inheritDoc CloudRegion}
*
* @public
*/
cloudRegion?: CloudRegion
}

/**
* The base configuration options for {@link SearchCore}.
* @public
*/
export interface BaseSearchConfig {
export interface BaseSearchConfig extends ServingConfig {
/** The experience key of the search experience. */
experienceKey: string,
/** The locale of the search experience. */
Expand Down
1 change: 1 addition & 0 deletions src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export { SearchError } from './searchapi/SearchError';

// Core models
export {
ServingConfig,
SearchConfig,
BaseSearchConfig,
SearchConfigWithApiKey,
Expand Down
4 changes: 2 additions & 2 deletions src/provideCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { SearchConfig, SearchConfigWithDefaulting } from './models/core/SearchCo
import { AutocompleteServiceImpl } from './infra/AutocompleteServiceImpl';
import { ApiResponseValidator } from './validation/ApiResponseValidator';
import { SearchCore } from './SearchCore';
import { defaultEndpoints } from './constants';
import { provideEndpoints } from './provideEndpoints';

/**
* The entrypoint to the search-core library.
Expand All @@ -25,7 +25,7 @@ export function provideCore(config: SearchConfig): SearchCore {
const defaultedConfig: SearchConfigWithDefaulting = {
...config,
endpoints: {
...defaultEndpoints,
...provideEndpoints(config),
...config.endpoints
}
};
Expand Down
69 changes: 69 additions & 0 deletions src/provideEndpoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Endpoints } from './models/core/Endpoints';
import { Environment } from './models/core/Environment';
import { CloudRegion } from './models/core/CloudRegion';
import { ServingConfig } from './models/core/SearchConfig';

export const defaultApiVersion = 20220511;

/**
* Provides methods for getting various endpoints.
*
* @internal
*/
export class EndpointsAdapter {
private readonly environment: Environment;
private readonly cloudRegion: CloudRegion;

constructor(config?: ServingConfig) {
this.environment = config?.environment || Environment.PROD;
this.cloudRegion = config?.cloudRegion || CloudRegion.US;
}

/** Provides the domain based on environment and cloud region.
* @internal
*/
getDomain() {
return `https://${this.environment}-cdn.${this.cloudRegion}.yextapis.com`;
}

/**
* Provides all endpoints based on environment and cloud region.
* @internal
*/
getEndpoints() {
return {
universalSearch: `${this.getDomain()}/v2/accounts/me/search/query`,
verticalSearch: `${this.getDomain()}/v2/accounts/me/search/vertical/query`,
questionSubmission: `${this.getDomain()}/v2/accounts/me/createQuestion`,
status: 'https://answersstatus.pagescdn.com',
universalAutocomplete: `${this.getDomain()}/v2/accounts/me/search/autocomplete`,
verticalAutocomplete: `${this.getDomain()}/v2/accounts/me/search/vertical/autocomplete`,
filterSearch: `${this.getDomain()}/v2/accounts/me/search/filtersearch`,
};
}
}

/**
* The endpoints to use for sandbox experiences.
*
* @deprecated Use {@link provideEndpoints} instead
*
* @public
*/
export const SandboxEndpoints: Required<Endpoints> =
new EndpointsAdapter({ environment: Environment.SANDBOX, cloudRegion: CloudRegion.US })
.getEndpoints();

/**
* Provides all endpoints based on environment and cloud region.
*
* @remarks
* Returns an {@link Endpoints} instance.
*
* @param config - serving config to use for the domain, defaults to Prod and US if not provided.
*
* @public
*/
export function provideEndpoints(config?: ServingConfig): Required<Endpoints> {
return new EndpointsAdapter(config).getEndpoints();
}
5 changes: 4 additions & 1 deletion tests/infra/AutocompleteServiceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ import mockAutocompleteResponse from '../fixtures/autocompleteresponse.json';
import mockAutocompleteResponseWithSections from '../fixtures/autocompleteresponsewithsections.json';
import mockAutocompleteResponseWithFailedVerticals from '../fixtures/autocompleteresponsewithfailedverticals.json';
import mockAutocompleteResponseWithVerticalKeys from '../fixtures/autocompleteresponsewithverticalkeys.json';
import { defaultEndpoints, defaultApiVersion } from '../../src/constants';
import { defaultApiVersion, provideEndpoints } from '../../src/provideEndpoints';
import { ApiResponseValidator } from '../../src/validation/ApiResponseValidator';
import { ApiResponse } from '../../src/models/searchapi/ApiResponse';
import { SearchError } from '../../src/models/searchapi/SearchError';
import { getClientSdk } from '../../src/utils/getClientSdk';
import { Matcher } from '../../src/models/searchservice/common/Matcher';
import { Endpoints } from '../../src/models/core/Endpoints';

const defaultEndpoints: Required<Endpoints> = provideEndpoints();

describe('AutocompleteService', () => {
const config: SearchConfigWithDefaulting = {
Expand Down
Loading

0 comments on commit c0d77f0

Please sign in to comment.