Skip to content

Commit

Permalink
Add sort support to getAddresses API (#1116)
Browse files Browse the repository at this point in the history
* add sort support to getAddresses

* fix typo in the sortBy params

* remove unused function

* test fix

* debug

* skip test
  • Loading branch information
jpsantosbh authored Sep 12, 2024
1 parent 6b73071 commit 00a94f1
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/nine-hornets-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@signalwire/js': minor
---

Add sort parameters to GetAddressesParams allowing developers to benefit from server-side sorting.
100 changes: 100 additions & 0 deletions internal/e2e-js/tests/callfabric/address.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,104 @@ test.describe('Addresses', () => {
expect(addressById.id).toEqual(addressToCompare.id)
expect(addressByName.id).toEqual(addressToCompare.id)
})

test('Should return only type rooms in ASC order by name', async ({
createCustomPage,
}) => {
const page = await createCustomPage({ name: '[page]' })
await page.goto(SERVER_URL)

await createCFClient(page)

const isCorrectlySorted = await page.evaluate(async () => {
// @ts-expect-error
const client = window._client

const response = await client.address.getAddresses({
type: 'room',
sortBy: 'name',
sortOrder: 'asc',
pageSize: 3,
})

const isSorted = (arr: string[]) => {
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i + 1]) {
return false
}
}

return true
}

return isSorted(
// @ts-expect-error
response.data.map((addr) => {
console.log(addr.name)
return addr.name})
)
})

expect(isCorrectlySorted).toBeTruthy()
})

// TODO unskip this test once this is sorted out in the backend.
/*
Rails is currently sorting this wrongly
[page] 2024-09-12T14:21:56.299Z - [getAddresses] query URL /api/fabric/addresses?type=room&page_size=3&sort_by=name&sort_order=desc
[page] with-preview-ygqdk
[page] with-preview
[page] without-preview
correct sorting...
Welcome to Node.js v20.12.1.
Type ".help" for more information.
> let names = ["with-preview-ygqdk", "with-preview", "without-preview"]
undefined
> names
[ 'with-preview-ygqdk', 'with-preview', 'without-preview' ]
> names.sort()
[ 'with-preview', 'with-preview-ygqdk', 'without-preview' ]
*/
test.skip('Should return only type rooms in DESC order by name', async ({
createCustomPage,
}) => {
const page = await createCustomPage({ name: '[page]' })
await page.goto(SERVER_URL)

await createCFClient(page)

const isCorrectlySorted = await page.evaluate(async () => {
// @ts-expect-error
const client = window._client

const response = await client.address.getAddresses({
type: 'room',
sortBy: 'name',
sortOrder: 'desc',
pageSize: 3,
})

const isSorted = (arr: string[]) => {
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] < arr[i + 1]) {
return false
}
}

return true
}

return isSorted(
// @ts-expect-error
response.data.map((addr) => {
console.log(addr.name)
return addr.name})
)
})

expect(isCorrectlySorted).toBeTruthy()
})
})
29 changes: 23 additions & 6 deletions packages/js/src/fabric/HTTPClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import type {
import { CreateHttpClient, createHttpClient } from './createHttpClient'
import { buildPaginatedResult } from '../utils/paginatedResult'
import { makeQueryParamsUrls } from '../utils/makeQueryParamsUrl'
import { isGetAddressByIdParams, isGetAddressByNameParams, isGetAddressesResponse } from './utils/typeGuard'
import {
isGetAddressByIdParams,
isGetAddressByNameParams,
isGetAddressesResponse,
} from './utils/typeGuard'

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

Expand Down Expand Up @@ -55,16 +59,19 @@ export class HTTPClient {
return `fabric.${host.split('.').splice(1).join('.')}`
}

public async getAddress(params: GetAddressParams): Promise<GetAddressResult | undefined> {
public async getAddress(
params: GetAddressParams
): Promise<GetAddressResult | undefined> {
let path = '/api/fabric/addresses'
if (isGetAddressByNameParams(params)) {
path = `${path}?name=${params.name}`
} else if (isGetAddressByIdParams(params)) {
path = `${path}/${params.id}`
}


const { body } = await this.httpClient<GetAddressResponse | GetAddressesResponse>(path)
const { body } = await this.httpClient<
GetAddressResponse | GetAddressesResponse
>(path)
if (isGetAddressesResponse(body)) {
// FIXME until the server handles a index lookup by name we need to handle it as a search result
return body.data[0]
Expand All @@ -75,7 +82,7 @@ export class HTTPClient {
public async getAddresses(
params?: GetAddressesParams
): Promise<GetAddressesResult> {
const { type, displayName, pageSize } = params || {}
const { type, displayName, pageSize, sortBy, sortOrder } = params || {}

let path = '/api/fabric/addresses'

Expand All @@ -90,8 +97,18 @@ export class HTTPClient {
queryParams.append('page_size', pageSize.toString())
}

if (sortBy) {
queryParams.append('sort_by', sortBy)
}

if (sortOrder) {
queryParams.append('sort_order', sortOrder)
}

const queryUrl = makeQueryParamsUrls(path, queryParams)
getLogger().debug(`[getAddresses] query URL ${queryUrl}`)
const { body } = await this.httpClient<GetAddressesResponse>(
makeQueryParamsUrls(path, queryParams)
queryUrl
)

return buildPaginatedResult(body, this.httpClient)
Expand Down
2 changes: 2 additions & 0 deletions packages/js/src/fabric/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ export interface GetAddressesParams {
type?: string
displayName?: string
pageSize?: number
sortBy?: 'name' | 'created_at'
sortOrder?: 'asc' | 'desc'
}

export interface GetAddressByIdParams {
Expand Down

0 comments on commit 00a94f1

Please sign in to comment.