vault/ui/lib/sync/addon/utils/api-transforms.ts
Jordan Reimer 712d3338b7
[UI] Ember Data Migration - Sync Overview/Destinations (#30524)
* improves path handling in capabilities service

* converts has-capability to class helper and adds pathKey and params args

* adds api service to sync engine

* updates sync types

* improves typings in paginate-list util

* adds api client error handling to error page component

* adds api utils for sync

* updates sync overview route and page component to use api service

* updates sync destinations route and page component to use api service

* adds missing copyright header

* fixes paginate-list regression

* fixes return type for has-capability helper

* Apply suggestions from code review

Co-authored-by: Angel Garbarino <Monkeychip@users.noreply.github.com>

* fixes page error tests

* resolves suggestions from review

* fixes has-capability usage errors

* fixes comment in capabilities service

* more test fixes

---------

Co-authored-by: Angel Garbarino <Monkeychip@users.noreply.github.com>
2025-05-07 10:53:56 -06:00

54 lines
1.9 KiB
TypeScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import { findDestination } from 'core/helpers/sync-destinations';
import type { DestinationType, ListDestination } from 'vault/sync';
import type { SystemListSyncDestinationsResponse } from '@hashicorp/vault-client-typescript';
// transforms the systemListSyncDestinations response to a flat array
// destination name and type are the only properties returned from the request
// to satisfy the list views, combine this data with static properties icon and typeDisplayName
// the flat array is then filtered by name and type if filter values are provided
export const listDestinationsTransform = (
response: SystemListSyncDestinationsResponse,
nameFilter?: string,
typeFilter?: string
) => {
const { keyInfo } = response;
const destinations: ListDestination[] = [];
// build ListDestination objects from keyInfo
for (const key in keyInfo) {
// iterate through each type's destination names
const names = (keyInfo as Record<string, string[]>)[key];
// remove trailing slash from key
const type = key.replace(/\/$/, '') as DestinationType;
names?.forEach((name: string) => {
const id = `${type}/${name}`;
const { icon, name: typeDisplayName } = findDestination(type) || {};
// create object with destination's id and attributes
destinations.push({ id, name, type, icon, typeDisplayName });
});
}
// optionally filter by name and type
let filteredDestinations = [...destinations];
const filter = (key: 'type' | 'name', value: string) => {
return filteredDestinations.filter((destination: ListDestination) => {
return destination[key].toLowerCase().includes(value.toLowerCase());
});
};
if (typeFilter) {
filteredDestinations = filter('type', typeFilter);
}
if (nameFilter) {
filteredDestinations = filter('name', nameFilter);
}
return filteredDestinations;
};