-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
159 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,19 @@ | ||
import { Realtime } from 'ably/promises'; | ||
import { Conversations } from './Conversations'; | ||
|
||
const DEFAULT_BASE_URL = | ||
process.env.NODE_ENV === 'production' ? 'https://rest.ably.io/conversation' : 'http://localhost:8281/conversations'; | ||
|
||
export class Chat { | ||
private ably: Realtime; | ||
constructor(ably: Realtime) { | ||
this.ably = ably; | ||
private readonly realtime: Realtime; | ||
|
||
readonly conversations: Conversations; | ||
constructor(realtime: Realtime, baseUrl = DEFAULT_BASE_URL) { | ||
this.realtime = realtime; | ||
this.conversations = new Conversations(realtime, baseUrl); | ||
} | ||
|
||
get connection() { | ||
return this.realtime.connection; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
export interface CreateConversationOptions { | ||
ttl: number; | ||
} | ||
|
||
export interface Conversation { | ||
id: string; | ||
} | ||
|
||
export class ChatApi { | ||
private readonly baseUrl: string; | ||
|
||
constructor(baseUrl: string) { | ||
this.baseUrl = baseUrl; | ||
} | ||
async getConversation(conversationId: string): Promise<Conversation> { | ||
const response = await fetch(`${this.baseUrl}/v1/conversation/${conversationId}`); | ||
return response.json(); | ||
} | ||
|
||
async createConversation(conversationId: string, body?: CreateConversationOptions): Promise<Conversation> { | ||
const response = await fetch(`${this.baseUrl}/v1/conversation/${conversationId}`, { | ||
method: 'POST', | ||
body: body ? JSON.stringify(body) : undefined, | ||
}); | ||
return response.json(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Realtime } from 'ably/promises'; | ||
import { ChatApi } from './ChatApi'; | ||
import { Messages } from './Messages'; | ||
|
||
export class Conversation { | ||
private readonly conversationId: string; | ||
private readonly realtime: Realtime; | ||
private readonly chatApi: ChatApi; | ||
readonly messages: Messages; | ||
|
||
constructor(conversationId: string, realtime: Realtime, chatApi: ChatApi) { | ||
this.conversationId = conversationId; | ||
this.realtime = realtime; | ||
this.chatApi = chatApi; | ||
this.messages = new Messages(conversationId, realtime, this.chatApi); | ||
} | ||
|
||
async create() { | ||
await this.chatApi.createConversation(this.conversationId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Realtime } from 'ably/promises'; | ||
import { ChatApi } from './ChatApi'; | ||
import { Conversation } from './Conversation'; | ||
|
||
export class Conversations { | ||
private readonly realtime: Realtime; | ||
private readonly chatApi: ChatApi; | ||
|
||
constructor(realtime: Realtime, baseUrl: string) { | ||
this.realtime = realtime; | ||
this.chatApi = new ChatApi(baseUrl); | ||
} | ||
|
||
get(conversationId: string) { | ||
return new Conversation(conversationId, this.realtime, this.chatApi); | ||
} | ||
} |
Oops, something went wrong.