UNPKG

@buildable/connections

Version:

Buildable is a fully managed event bus lets you send and receive data across mission-critical cloud apps, databases and warehouses.

123 lines (109 loc) 2.17 kB
import { CreateSourceConnectionPayload, DeleteSourcePayload, IntegrationRecord, Integrations, ListSourcesPayload, SourcePlatforms, SubscribeSourceEventPayload, } from '@buildable/types'; import { getIntegrationDefinitionId, makeHttpNetworkCall, } from '@buildable/utils'; export const createSourceConnectionApi = <T extends SourcePlatforms>( headers: Record<string, string>, url: string, payload: CreateSourceConnectionPayload<T> ) => { const authFormData = payload.type === 'webhook' || payload.type === 'nodejs' ? { name: payload.label, } : { ...payload.config, name: payload.label, }; const integrationDefinitionId = getIntegrationDefinitionId( payload.type, 'source' ); const finalPayload = { integrationDefinitionId, authFormData, type: payload.type, group: payload.group, name: payload.label, key: payload.key, }; return makeHttpNetworkCall<IntegrationRecord>({ method: 'POST', url, data: finalPayload, headers, }); }; export const deleteSourceConnectionApi = ( headers: Record<string, string>, url: string, payload: DeleteSourcePayload ) => { return makeHttpNetworkCall({ method: 'POST', url, data: payload, headers, }); }; export const subscribeEventsApi = async <T extends SourcePlatforms>( headers: Record<string, string>, url: string, payload: SubscribeSourceEventPayload<T> ) => { const { events, type, key } = payload; if (type === 'nodejs') { const eventList = events.join(','); return makeHttpNetworkCall({ method: 'POST', url, data: { key, events: eventList, }, headers, }); } const result = makeHttpNetworkCall({ method: 'POST', url, data: { events, key, }, headers, }); //TODO: Create a second function await new Promise((resolve) => setTimeout(resolve, 5000)); return result; }; export const listSourcesApi = ( headers: Record<string, string>, url: string, payload: ListSourcesPayload ) => { return makeHttpNetworkCall<Integrations>({ method: 'POST', url, data: { ...payload, query: { ...payload?.query, _id: { $regex: 'conn_src', }, }, }, headers, }); };