Skip to content

Commit

Permalink
Specify pageSize param to conversation APIs (#978)
Browse files Browse the repository at this point in the history
* Specify pageSize param to conversation APIs

* include changeset
  • Loading branch information
iAmmar7 authored Mar 5, 2024
1 parent c8deace commit 0f4f2b3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 66 deletions.
6 changes: 6 additions & 0 deletions .changeset/kind-keys-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@signalwire/core': patch
'@signalwire/js': patch
---

Introduce pageSize param for Conversation APIs
5 changes: 4 additions & 1 deletion internal/playground-js/src/fabric-http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ async function fetchAddresses() {
const addressData = await client.address.getAddresses({
type: selectedType === 'all' ? undefined : selectedType,
displayName: !searchText.length ? undefined : searchText,
pageSize: 10,
})
window.__addressData = addressData
updateAddressUI()
Expand Down Expand Up @@ -301,7 +302,9 @@ function updateHistoryUI() {
async function fetchHistories() {
if (!client) return
try {
const historyData = await client.conversation.getConversations()
const historyData = await client.conversation.getConversations({
pageSize: 10,
})
window.__historyData = historyData
updateHistoryUI()
subscribeToNewMessages()
Expand Down
15 changes: 3 additions & 12 deletions packages/core/src/types/callfabric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ export interface FetchAddressResponse extends PaginatedResponse<Address> {}
* Conversations
*/
export interface GetConversationsOptions {
limit?: number
since?: number
until?: number
cursor?: string
pageSize?: number
}

export interface Conversation {
Expand All @@ -59,10 +56,7 @@ export interface FetchConversationsResponse
*/

export interface GetMessagesOptions {
limit?: number
since?: number
until?: number
cursor?: string
pageSize?: number
}

export interface ConversationMessage {
Expand All @@ -81,10 +75,7 @@ export interface FetchConversationMessagesResponse

export interface GetConversationMessagesOptions {
addressId: string
limit?: number
since?: number
until?: number
cursor?: string
pageSize?: number
}

export interface SubscriberInfoResponse {
Expand Down
45 changes: 9 additions & 36 deletions packages/js/src/fabric/Conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,12 @@ export class Conversation {

public async getConversations(options?: GetConversationsOptions) {
try {
const { limit, since, until, cursor } = options || {}
const { pageSize } = options || {}

const path = '/api/fabric/conversations'
const queryParams = new URLSearchParams()
if (limit) {
queryParams.append('limit', limit.toString())
}
if (since) {
queryParams.append('since', since.toString())
}
if (until) {
queryParams.append('until', until.toString())
}
if (cursor) {
queryParams.append('cursor', cursor)
if (pageSize) {
queryParams.append('page_size', pageSize.toString())
}

const { body } = await this.httpClient.fetch<FetchConversationsResponse>(
Expand All @@ -70,21 +61,12 @@ export class Conversation {

public async getMessages(options?: GetMessagesOptions) {
try {
const { limit, since, until, cursor } = options || {}
const { pageSize } = options || {}

const path = '/api/fabric/messages'
const queryParams = new URLSearchParams()
if (limit) {
queryParams.append('limit', limit.toString())
}
if (since) {
queryParams.append('since', since.toString())
}
if (until) {
queryParams.append('until', until.toString())
}
if (cursor) {
queryParams.append('cursor', cursor)
if (pageSize) {
queryParams.append('page_size', pageSize.toString())
}

const { body } =
Expand All @@ -105,21 +87,12 @@ export class Conversation {
options: GetConversationMessagesOptions
) {
try {
const { addressId, limit, since, until, cursor } = options || {}
const { addressId, pageSize } = options || {}

const path = `/api/fabric/conversations/${addressId}/messages`
const queryParams = new URLSearchParams()
if (limit) {
queryParams.append('limit', limit.toString())
}
if (since) {
queryParams.append('since', since.toString())
}
if (until) {
queryParams.append('until', until.toString())
}
if (cursor) {
queryParams.append('cursor', cursor)
if (pageSize) {
queryParams.append('page_size', pageSize.toString())
}

const { body } =
Expand Down
30 changes: 13 additions & 17 deletions packages/js/src/fabric/HTTPClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '@signalwire/core'
import { CreateHttpClient, createHttpClient } from './createHttpClient'
import { buildPaginatedResult } from '../utils/paginatedResult'
import { makeQueryParamsUrls } from '../utils/makeQueryParamsUrl'

type JWTHeader = { ch?: string; typ?: string }

Expand Down Expand Up @@ -56,25 +57,20 @@ export class HTTPClient {

let path = '/api/fabric/addresses'

if (type || displayName || pageSize) {
const queryParams = new URLSearchParams()

if (type) {
queryParams.append('type', type)
}

if (displayName) {
queryParams.append('display_name', displayName)
}

if (pageSize) {
queryParams.append('page_size', pageSize.toString())
}

path += `?${queryParams.toString()}`
const queryParams = new URLSearchParams()
if (type) {
queryParams.append('type', type)
}
if (displayName) {
queryParams.append('display_name', displayName)
}
if (pageSize) {
queryParams.append('page_size', pageSize.toString())
}

const { body } = await this.httpClient<FetchAddressResponse>(path)
const { body } = await this.httpClient<FetchAddressResponse>(
makeQueryParamsUrls(path, queryParams)
)

return buildPaginatedResult<Address>(body, this.httpClient)
}
Expand Down

0 comments on commit 0f4f2b3

Please sign in to comment.