|
| 1 | +import { IDataConnector } from '../entities'; |
| 2 | +import { |
| 3 | + IDeleteResponse, |
| 4 | + IPaginatedResponse, |
| 5 | + IRestClient, |
| 6 | +} from '../rest-client'; |
| 7 | +import type { AtLeast } from '../utility-types'; |
| 8 | + |
| 9 | +export interface IDataConnectors { |
| 10 | + list<T extends IDataConnector>(appRef: string): Promise<T[]>; |
| 11 | + delete(connectorId: string, appRef: string): Promise<IDeleteResponse>; |
| 12 | + read<T extends IDataConnector>( |
| 13 | + connectorId: string, |
| 14 | + appRef: string, |
| 15 | + ): Promise<T>; |
| 16 | + create<T extends IDataConnector>( |
| 17 | + newConnector: Partial<T>, |
| 18 | + appRef: string, |
| 19 | + ): Promise<T>; |
| 20 | + update<T extends IDataConnector>( |
| 21 | + entity: AtLeast<T, 'id'>, |
| 22 | + appRef: string, |
| 23 | + ): Promise<T>; |
| 24 | +} |
| 25 | +export interface IDataConnectorsOptions { |
| 26 | + client: IRestClient; |
| 27 | +} |
| 28 | + |
| 29 | +export class DataConnectors implements IDataConnectors { |
| 30 | + private readonly _client: IRestClient; |
| 31 | + |
| 32 | + private readonly APPS_URL = '/apps'; |
| 33 | + private readonly DATA_CONNECTORS_URL = '/dataconnectors'; |
| 34 | + |
| 35 | + constructor(options: IDataConnectorsOptions) { |
| 36 | + this._client = options.client; |
| 37 | + } |
| 38 | + |
| 39 | + private _makeBaseUrl(appRef: string) { |
| 40 | + return `${this.APPS_URL}/${appRef}${this.DATA_CONNECTORS_URL}`; |
| 41 | + } |
| 42 | + |
| 43 | + private _makeEntityUrl(connectorId: string, appRef: string) { |
| 44 | + return `${this._makeBaseUrl(appRef)}/${connectorId}`; |
| 45 | + } |
| 46 | + |
| 47 | + public async create<T extends IDataConnector>( |
| 48 | + newConnector: Partial<T>, |
| 49 | + appRef: string, |
| 50 | + ): Promise<T> { |
| 51 | + return await this._client.post(this._makeBaseUrl(appRef), newConnector); |
| 52 | + } |
| 53 | + |
| 54 | + public async read<T extends IDataConnector>( |
| 55 | + connectorId: string, |
| 56 | + appRef: string, |
| 57 | + ): Promise<T> { |
| 58 | + return await this._client.get<T>(this._makeEntityUrl(connectorId, appRef)); |
| 59 | + } |
| 60 | + |
| 61 | + public async list<T extends IDataConnector>(appRef: string): Promise<T[]> { |
| 62 | + let response = await this._client.get<IPaginatedResponse<T>>( |
| 63 | + this._makeBaseUrl(appRef), |
| 64 | + ); |
| 65 | + |
| 66 | + return response.results; |
| 67 | + } |
| 68 | + |
| 69 | + public async update<T extends IDataConnector>( |
| 70 | + payload: AtLeast<T, 'id'>, |
| 71 | + appRef: string, |
| 72 | + ): Promise<T> { |
| 73 | + return await this._client.put( |
| 74 | + this._makeEntityUrl(payload.id, appRef), |
| 75 | + payload, |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + public async delete( |
| 80 | + connectorId: string, |
| 81 | + appRef: string, |
| 82 | + ): Promise<IDeleteResponse> { |
| 83 | + let response: IDeleteResponse = await this._client.delete( |
| 84 | + this._makeEntityUrl(connectorId, appRef), |
| 85 | + ); |
| 86 | + |
| 87 | + if (response.nrOfItems === 0) { |
| 88 | + return Promise.reject(response); |
| 89 | + } |
| 90 | + |
| 91 | + return response; |
| 92 | + } |
| 93 | +} |
0 commit comments