Skip to content

Commit

Permalink
support env, region, and endpoints (#16)
Browse files Browse the repository at this point in the history
updated config to expose env, region and endpoints field to customize/override the default URLs to make requests to.

J=CLIP-288
TEST=manual&auto

see that chat endpoint works with clippy EU prod and US dev account using test-site.
see that jest tests passed
  • Loading branch information
yen-tt authored Jun 28, 2023
1 parent 5ba55a6 commit da7021d
Show file tree
Hide file tree
Showing 29 changed files with 440 additions and 96 deletions.
18 changes: 0 additions & 18 deletions docs/chat-core.chatconfig.apidomain.md

This file was deleted.

13 changes: 13 additions & 0 deletions docs/chat-core.chatconfig.endpoints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@yext/chat-core](./chat-core.md) &gt; [ChatConfig](./chat-core.chatconfig.md) &gt; [endpoints](./chat-core.chatconfig.endpoints.md)

## ChatConfig.endpoints property

Overrides for the URLs which are used when making requests to the Chat API.

**Signature:**

```typescript
endpoints?: Endpoints;
```
18 changes: 18 additions & 0 deletions docs/chat-core.chatconfig.env.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@yext/chat-core](./chat-core.md) &gt; [ChatConfig](./chat-core.chatconfig.md) &gt; [env](./chat-core.chatconfig.env.md)

## ChatConfig.env property

Defines the environment of the API domains.

**Signature:**

```typescript
env?: EnumOrLiteral<Environment>;
```

## Remarks

Default to PROD.

4 changes: 3 additions & 1 deletion docs/chat-core.chatconfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ export interface ChatConfig

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [apiDomain?](./chat-core.chatconfig.apidomain.md) | | string | _(Optional)_ Domain to use for the URL endpoints. |
| [apiKey](./chat-core.chatconfig.apikey.md) | | string | The api key of the Chat experience. |
| [botId](./chat-core.chatconfig.botid.md) | | string | ID of the bot to interface with. |
| [businessId?](./chat-core.chatconfig.businessid.md) | | number | _(Optional)_ ID of the account associated with this chat bot. |
| [endpoints?](./chat-core.chatconfig.endpoints.md) | | [Endpoints](./chat-core.endpoints.md) | _(Optional)_ Overrides for the URLs which are used when making requests to the Chat API. |
| [env?](./chat-core.chatconfig.env.md) | | [EnumOrLiteral](./chat-core.enumorliteral.md)<!-- -->&lt;[Environment](./chat-core.environment.md)<!-- -->&gt; | _(Optional)_ Defines the environment of the API domains. |
| [region?](./chat-core.chatconfig.region.md) | | [EnumOrLiteral](./chat-core.enumorliteral.md)<!-- -->&lt;[Region](./chat-core.region.md)<!-- -->&gt; | _(Optional)_ The region to send the requests to. |
| [version?](./chat-core.chatconfig.version.md) | | "STAGING" \| "PRODUCTION" \| number | _(Optional)_ The version of the chat bot configuration. |

18 changes: 18 additions & 0 deletions docs/chat-core.chatconfig.region.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@yext/chat-core](./chat-core.md) &gt; [ChatConfig](./chat-core.chatconfig.md) &gt; [region](./chat-core.chatconfig.region.md)

## ChatConfig.region property

The region to send the requests to.

**Signature:**

```typescript
region?: EnumOrLiteral<Region>;
```

## Remarks

Defaults to 'US'.

2 changes: 1 addition & 1 deletion docs/chat-core.chatconfig.version.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ version?: "STAGING" | "PRODUCTION" | number;

## Remarks

May be a configuration label (string) or a configuration version (number). Default to 'STAGING'
May be a configuration label (string) or a configuration version (number). Default to 'STAGING' in Chat API

## Example

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

[Home](./index.md) &gt; [@yext/chat-core](./chat-core.md) &gt; [Endpoints](./chat-core.endpoints.md) &gt; [chat](./chat-core.endpoints.chat.md)

## Endpoints.chat property

Chat API endpoint.

**Signature:**

```typescript
chat: string;
```
13 changes: 13 additions & 0 deletions docs/chat-core.endpoints.chatstream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@yext/chat-core](./chat-core.md) &gt; [Endpoints](./chat-core.endpoints.md) &gt; [chatStream](./chat-core.endpoints.chatstream.md)

## Endpoints.chatStream property

Chat streaming API endpoint.

**Signature:**

```typescript
chatStream: string;
```
21 changes: 21 additions & 0 deletions docs/chat-core.endpoints.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/chat-core](./chat-core.md) &gt; [Endpoints](./chat-core.endpoints.md)

## Endpoints interface

The URLs which are used when making requests to the Chat API.

**Signature:**

```typescript
export interface Endpoints
```

## Properties

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [chat](./chat-core.endpoints.chat.md) | | string | Chat API endpoint. |
| [chatStream](./chat-core.endpoints.chatstream.md) | | string | Chat streaming API endpoint. |

21 changes: 21 additions & 0 deletions docs/chat-core.environment.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/chat-core](./chat-core.md) &gt; [Environment](./chat-core.environment.md)

## Environment enum

Defines the environment of the API domains.

**Signature:**

```typescript
export declare enum Environment
```

## Enumeration Members

| Member | Value | Description |
| --- | --- | --- |
| PROD | <code>&quot;prod&quot;</code> | |
| SANDBOX | <code>&quot;sbx&quot;</code> | |

3 changes: 3 additions & 0 deletions docs/chat-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

| Enumeration | Description |
| --- | --- |
| [Environment](./chat-core.environment.md) | Defines the environment of the API domains. |
| [MessageSource](./chat-core.messagesource.md) | Types of sender of a message. |
| [Region](./chat-core.region.md) | The region to send the requests to. |
| [StreamEventName](./chat-core.streameventname.md) | Names of stream events returned from Chat Stream API. |

## Interfaces
Expand All @@ -24,6 +26,7 @@
| --- | --- |
| [ChatConfig](./chat-core.chatconfig.md) | The configuration options for [ChatCore](./chat-core.chatcore.md)<!-- -->. |
| [EndEvent](./chat-core.endevent.md) | An event that indicates end of Chat stream. |
| [Endpoints](./chat-core.endpoints.md) | The URLs which are used when making requests to the Chat API. |
| [Message](./chat-core.message.md) | Represents a message within a conversation. |
| [MessageNotes](./chat-core.messagenotes.md) | Information relevant to the current state of the conversation, serving as the bot’s "memory" regarding what work it previously did to help determine future actions. |
| [MessageRequest](./chat-core.messagerequest.md) | A request to Chat API. |
Expand Down
21 changes: 21 additions & 0 deletions docs/chat-core.region.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/chat-core](./chat-core.md) &gt; [Region](./chat-core.region.md)

## Region enum

The region to send the requests to.

**Signature:**

```typescript
export declare enum Region
```

## Enumeration Members

| Member | Value | Description |
| --- | --- | --- |
| EU | <code>&quot;eu&quot;</code> | |
| US | <code>&quot;us&quot;</code> | |

26 changes: 25 additions & 1 deletion etc/chat-core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import { Response as Response_2 } from 'node-fetch';

// @public
export interface ChatConfig {
apiDomain?: string;
apiKey: string;
botId: string;
businessId?: number;
endpoints?: Endpoints;
env?: EnumOrLiteral<Environment>;
region?: EnumOrLiteral<Region>;
version?: "STAGING" | "PRODUCTION" | number;
}

Expand All @@ -28,9 +30,23 @@ export interface EndEvent {
event: EnumOrLiteral<StreamEventName.EndEvent>;
}

// @public
export interface Endpoints {
chat: string;
chatStream: string;
}

// @public
export type EnumOrLiteral<T extends string> = T | `${T}`;

// @public
export enum Environment {
// (undocumented)
PROD = "prod",
// (undocumented)
SANDBOX = "sbx"
}

// @public
export interface Message {
responseId?: string;
Expand Down Expand Up @@ -73,6 +89,14 @@ export enum MessageSource {
// @public
export type RawResponse = Response | Response_2;

// @public
export enum Region {
// (undocumented)
EU = "eu",
// (undocumented)
US = "us"
}

// @public
export interface StartEvent {
data: MessageNotes;
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@yext/chat-core",
"version": "0.4.2",
"version": "0.5.0",
"description": "Typescript Networking Library for the Yext Chat API",
"main": "./dist/commonjs/index.js",
"module": "./dist/esm/index.js",
Expand Down
25 changes: 9 additions & 16 deletions src/ChatCore.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { defaultApiDomain, defaultApiVersion } from "./constants";
import { defaultApiVersion } from "./constants";
import { HttpService } from "./http/HttpService";
import { EndpointsFactory } from "./infra/EndpointsFactory";
import { StreamResponse } from "./infra/StreamResponse";
import { ChatConfig, MessageRequest, MessageResponse } from "./models";
import { Endpoints } from "./models/endpoints/Endpoints";
import { ApiMessageRequest } from "./models/endpoints/MessageRequest";
import { ApiResponse } from "./models/http/ApiResponse";
import { QueryParams } from "./models/http/params";
Expand All @@ -15,22 +17,13 @@ import { ApiResponseValidator } from "./validation/ApiResponseValidator";
export class ChatCore {
private chatConfig: ChatConfig;
private httpService: HttpService;
private endpoints: Endpoints;

constructor(chatConfig: ChatConfig) {
this.chatConfig = chatConfig;
this.httpService = new HttpService();
}

private getUrl({ businessId, botId, apiDomain }: ChatConfig) {
return `https://${apiDomain || defaultApiDomain}/v2/accounts/${
businessId || "me"
}/chat/${botId}/message`;
}

private getStreamUrl({ businessId, botId, apiDomain }: ChatConfig) {
return `https://${apiDomain || defaultApiDomain}/v2/accounts/${
businessId || "me"
}/chat/${botId}/message/streaming`;
this.endpoints =
chatConfig.endpoints ?? EndpointsFactory.getEndpoints(this.chatConfig);
}

/**
Expand All @@ -48,7 +41,7 @@ export class ChatCore {
version: this.chatConfig.version,
};
const rawResponse = await this.httpService.post(
this.getUrl(this.chatConfig),
this.endpoints.chat,
queryParams,
body,
this.chatConfig.apiKey
Expand Down Expand Up @@ -77,7 +70,7 @@ export class ChatCore {
/**
* Make a request to Chat streaming API to generate the next message
* and consume its tokens via server-sent events.
*
*
* @experimental
*
* @remarks
Expand All @@ -92,7 +85,7 @@ export class ChatCore {
version: this.chatConfig.version,
};
const rawResponse = await this.httpService.post(
this.getStreamUrl(this.chatConfig),
this.endpoints.chatStream,
queryParams,
body,
this.chatConfig.apiKey
Expand Down
1 change: 0 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export const defaultApiVersion = 20230101;
export const defaultApiDomain = "liveapi.yext.com";
43 changes: 43 additions & 0 deletions src/infra/EndpointsFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Region } from "../models/endpoints/Region";
import { EndpointConfig } from "../models/endpoints/EndpointConfig";
import { Endpoints } from "../models/endpoints/Endpoints";
import { Environment } from "../models/endpoints/Environment";

/**
* Provides methods for getting various endpoints.
*
* @internal
*/
export class EndpointsFactory {
private static getDomain(endpointConfig: EndpointConfig): string {
const { region = Region.US, env = Environment.PROD } = endpointConfig;
switch (region) {
case Region.US:
return env === Environment.PROD
? "liveapi.yext.com"
: `liveapi-${env}.yext.com`;
case Region.EU:
if (env === Environment.PROD) {
return "cdn.eu.yextapis.com";
}
throw new Error(
`Unsupported domain: invalid environment "${env}" for region EU`
);
default:
throw new Error(`Unsupported domain: unknown region "${region}"`);
}
}

public static getEndpoints(endpointConfig: EndpointConfig): Endpoints {
const { businessId, botId } = endpointConfig;
const domain = this.getDomain(endpointConfig);
return {
chat: `https://${domain}/v2/accounts/${
businessId || "me"
}/chat/${botId}/message`,
chatStream: `https://${domain}/v2/accounts/${
businessId || "me"
}/chat/${botId}/message/streaming`,
};
}
}
Loading

0 comments on commit da7021d

Please sign in to comment.