|
| 1 | +import axios, { AxiosInstance } from 'axios'; |
| 2 | +import { |
| 3 | + AddLocationRequest, |
| 4 | + AddLocationResponse, |
| 5 | + CatalogApi, |
| 6 | + CatalogRequestOptions, |
| 7 | + GetEntitiesByRefsRequest, |
| 8 | + GetEntitiesByRefsResponse, |
| 9 | + GetEntitiesRequest, |
| 10 | + GetEntitiesResponse, |
| 11 | + GetEntityAncestorsRequest, |
| 12 | + GetEntityAncestorsResponse, |
| 13 | + GetEntityFacetsRequest, |
| 14 | + GetEntityFacetsResponse, |
| 15 | + Location, |
| 16 | + QueryEntitiesRequest, |
| 17 | + QueryEntitiesResponse, |
| 18 | + ValidateEntityResponse, |
| 19 | +} from '@backstage/catalog-client'; |
| 20 | +import { |
| 21 | + CompoundEntityRef, |
| 22 | + Entity, |
| 23 | + stringifyEntityRef, |
| 24 | +} from '@backstage/catalog-model'; |
| 25 | +import { isString } from '../utils/guards'; |
| 26 | + |
| 27 | +interface BackstageCatalogApiOptions { |
| 28 | + baseUrl: string; |
| 29 | + token?: string; |
| 30 | +} |
| 31 | + |
| 32 | +export class BackstageCatalogApi implements CatalogApi { |
| 33 | + private readonly client: AxiosInstance; |
| 34 | + |
| 35 | + constructor({ baseUrl, token }: BackstageCatalogApiOptions) { |
| 36 | + this.client = axios.create({ |
| 37 | + baseURL: `${baseUrl.replace(/\/$/, '')}/v1`, |
| 38 | + headers: token ? { Authorization: `Bearer ${token}` } : {}, |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + async getEntities( |
| 43 | + request?: GetEntitiesRequest, |
| 44 | + _options?: CatalogRequestOptions |
| 45 | + ): Promise<GetEntitiesResponse> { |
| 46 | + const { data } = await this.client.get<GetEntitiesResponse>('/entities', { |
| 47 | + params: request, |
| 48 | + }); |
| 49 | + return data; |
| 50 | + } |
| 51 | + |
| 52 | + async getEntitiesByRefs( |
| 53 | + request: GetEntitiesByRefsRequest, |
| 54 | + _options?: CatalogRequestOptions |
| 55 | + ): Promise<GetEntitiesByRefsResponse> { |
| 56 | + const { entityRefs } = request; |
| 57 | + const { data } = await this.client.post<GetEntitiesByRefsResponse>( |
| 58 | + '/entities/by-refs', |
| 59 | + { entityRefs } |
| 60 | + ); |
| 61 | + return data; |
| 62 | + } |
| 63 | + |
| 64 | + async queryEntities( |
| 65 | + request?: QueryEntitiesRequest, |
| 66 | + _options?: CatalogRequestOptions |
| 67 | + ): Promise<QueryEntitiesResponse> { |
| 68 | + const { data } = await this.client.post<QueryEntitiesResponse>( |
| 69 | + '/entities/query', |
| 70 | + request |
| 71 | + ); |
| 72 | + return data; |
| 73 | + } |
| 74 | + |
| 75 | + async getEntityAncestors( |
| 76 | + request: GetEntityAncestorsRequest, |
| 77 | + _options?: CatalogRequestOptions |
| 78 | + ): Promise<GetEntityAncestorsResponse> { |
| 79 | + const { entityRef } = request; |
| 80 | + const { data } = await this.client.get<GetEntityAncestorsResponse>( |
| 81 | + `/entities/by-ref/${encodeURIComponent(entityRef)}/ancestry` |
| 82 | + ); |
| 83 | + return data; |
| 84 | + } |
| 85 | + |
| 86 | + async getEntityByRef( |
| 87 | + entityRef: string | CompoundEntityRef, |
| 88 | + _options?: CatalogRequestOptions |
| 89 | + ): Promise<Entity | undefined> { |
| 90 | + const refString = isString(entityRef) |
| 91 | + ? entityRef |
| 92 | + : this.formatCompoundEntityRef(entityRef); |
| 93 | + try { |
| 94 | + const { data } = await this.client.get<Entity>( |
| 95 | + `/entities/by-ref/${encodeURIComponent(refString)}` |
| 96 | + ); |
| 97 | + return data; |
| 98 | + } catch (error) { |
| 99 | + if (axios.isAxiosError(error) && error.response?.status === 404) |
| 100 | + return undefined; |
| 101 | + throw error; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + async removeEntityByUid( |
| 106 | + uid: string, |
| 107 | + _options?: CatalogRequestOptions |
| 108 | + ): Promise<void> { |
| 109 | + await this.client.delete(`/entities/by-uid/${encodeURIComponent(uid)}`); |
| 110 | + } |
| 111 | + |
| 112 | + async refreshEntity( |
| 113 | + entityRef: string, |
| 114 | + _options?: CatalogRequestOptions |
| 115 | + ): Promise<void> { |
| 116 | + await this.client.post(`/refresh`, { entityRef }); |
| 117 | + } |
| 118 | + |
| 119 | + async getEntityFacets( |
| 120 | + request: GetEntityFacetsRequest, |
| 121 | + _options?: CatalogRequestOptions |
| 122 | + ): Promise<GetEntityFacetsResponse> { |
| 123 | + const { data } = await this.client.post<GetEntityFacetsResponse>( |
| 124 | + '/entities/facets', |
| 125 | + request |
| 126 | + ); |
| 127 | + return data; |
| 128 | + } |
| 129 | + |
| 130 | + async getLocationById( |
| 131 | + id: string, |
| 132 | + _options?: CatalogRequestOptions |
| 133 | + ): Promise<Location | undefined> { |
| 134 | + try { |
| 135 | + const { data } = await this.client.get<Location>( |
| 136 | + `/locations/${encodeURIComponent(id)}` |
| 137 | + ); |
| 138 | + return data; |
| 139 | + } catch (error) { |
| 140 | + if (axios.isAxiosError(error) && error.response?.status === 404) |
| 141 | + return undefined; |
| 142 | + throw error; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + async getLocationByRef( |
| 147 | + locationRef: string, |
| 148 | + _options?: CatalogRequestOptions |
| 149 | + ): Promise<Location | undefined> { |
| 150 | + try { |
| 151 | + const { data } = await this.client.get<Location>( |
| 152 | + `/locations/by-ref/${encodeURIComponent(locationRef)}` |
| 153 | + ); |
| 154 | + return data; |
| 155 | + } catch (error) { |
| 156 | + if (axios.isAxiosError(error) && error.response?.status === 404) |
| 157 | + return undefined; |
| 158 | + throw error; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + async addLocation( |
| 163 | + location: AddLocationRequest, |
| 164 | + _options?: CatalogRequestOptions |
| 165 | + ): Promise<AddLocationResponse> { |
| 166 | + const { data } = await this.client.post<AddLocationResponse>( |
| 167 | + '/locations', |
| 168 | + location |
| 169 | + ); |
| 170 | + return data; |
| 171 | + } |
| 172 | + |
| 173 | + async removeLocationById( |
| 174 | + id: string, |
| 175 | + _options?: CatalogRequestOptions |
| 176 | + ): Promise<void> { |
| 177 | + await this.client.delete(`/locations/${encodeURIComponent(id)}`); |
| 178 | + } |
| 179 | + |
| 180 | + async getLocationByEntity( |
| 181 | + entityRef: string | CompoundEntityRef, |
| 182 | + _options?: CatalogRequestOptions |
| 183 | + ): Promise<Location | undefined> { |
| 184 | + const refString = isString(entityRef) |
| 185 | + ? entityRef |
| 186 | + : this.formatCompoundEntityRef(entityRef); |
| 187 | + try { |
| 188 | + const { data } = await this.client.get<Location>( |
| 189 | + `/locations/by-entity/${encodeURIComponent(refString)}` |
| 190 | + ); |
| 191 | + return data; |
| 192 | + } catch (error) { |
| 193 | + if (axios.isAxiosError(error) && error.response?.status === 404) |
| 194 | + return undefined; |
| 195 | + throw error; |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + async validateEntity( |
| 200 | + entity: Entity, |
| 201 | + locationRef: string, |
| 202 | + _options?: CatalogRequestOptions |
| 203 | + ): Promise<ValidateEntityResponse> { |
| 204 | + const { data } = await this.client.post<ValidateEntityResponse>( |
| 205 | + '/validate-entity', |
| 206 | + { entity, locationRef } |
| 207 | + ); |
| 208 | + return data; |
| 209 | + } |
| 210 | + |
| 211 | + private formatCompoundEntityRef(entityRef: CompoundEntityRef): string { |
| 212 | + return stringifyEntityRef(entityRef); |
| 213 | + } |
| 214 | +} |
0 commit comments