-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGenericFunctions.ts
More file actions
executable file
·55 lines (50 loc) · 1.44 KB
/
GenericFunctions.ts
File metadata and controls
executable file
·55 lines (50 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { IExecuteFunctions, IHttpRequestMethods, ILoadOptionsFunctions, NodeApiError } from 'n8n-workflow';
interface RequestOptions {
method?: 'GET' | 'POST';
headers?: HeadersInit;
body?: any;
params?: Record<string, string>;
}
export interface RobollyResponse {
acceptedModifications?: Array<{
elementType: string;
key: string;
type: string;
}>;
templates?: Array<{
id: string;
name: string;
transition: {
duration: number;
} | null;
}>;
value?: Array<any>;
hasMore?: boolean;
paginationCursorNext?: string;
}
export async function genericHttpRequest<T = RobollyResponse>(this: IExecuteFunctions | ILoadOptionsFunctions, method: IHttpRequestMethods, endpoint: string, options: RequestOptions = {}): Promise<T> {
try {
const credentials = await this.getCredentials('robollyApi');
const apiToken = credentials?.apikey as string;
let fullEndpoint = `https://api.robolly.com${endpoint}`;
if (options.params) {
const searchParams = new URLSearchParams(options.params);
fullEndpoint += fullEndpoint.includes('?') ? '&' : '?';
fullEndpoint += searchParams.toString();
}
const response = await this.helpers.httpRequest({
method,
url: fullEndpoint,
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiToken}`,
...options.headers,
},
body: options.body,
json: true,
});
return response as T;
} catch (error) {
throw new NodeApiError(this.getNode(), error as any);
}
}