-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericFunctions.ts
More file actions
143 lines (125 loc) · 3.61 KB
/
Copy pathGenericFunctions.ts
File metadata and controls
143 lines (125 loc) · 3.61 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import get from 'lodash/get';
import type {
ICredentialDataDecryptedObject,
ICredentialTestFunctions,
IDataObject,
IExecuteFunctions,
ILoadOptionsFunctions,
IHookFunctions,
IWebhookFunctions,
IHttpRequestOptions,
} from 'n8n-workflow';
import { NodeApiError } from 'n8n-workflow';
import { query } from './Queries';
export async function linearApiRequest(
this: IExecuteFunctions | IWebhookFunctions | IHookFunctions | ILoadOptionsFunctions,
body: any = {},
option: IDataObject = {},
): Promise<any> {
const endpoint = 'https://api.linear.app/graphql';
const authenticationMethod = this.getNodeParameter('authentication', 0, 'apiToken') as string;
let options: IHttpRequestOptions = {
headers: {
'Content-Type': 'application/json',
},
method: 'POST',
body,
url: endpoint,
json: true,
};
options = Object.assign({}, options, option);
try {
const response = await this.helpers.httpRequestWithAuthentication.call(
this,
authenticationMethod === 'apiToken' ? 'linearApi' : 'linearOAuth2Api',
options,
);
if (response?.errors) {
const errorMessage = response.errors[0].message ?? 'Unknown API Error';
const description = response.errors[0].extensions?.userPresentableMessage;
throw new NodeApiError(this.getNode(), response.errors, {
message: `Linear API error: ${errorMessage}`,
description,
});
}
return response;
} catch (error) {
// If this is already a NodeApiError with custom formatting, re-throw it as-is
if (error instanceof NodeApiError) {
throw error;
}
throw new NodeApiError(
this.getNode(),
{},
{
message:
error.errorResponse?.[0]?.message ||
error.context.data.errors[0]?.message ||
'Unknown API error',
description:
error.errorResponse?.[0]?.extensions?.userPresentableMessage ||
error.context.data.errors[0]?.extensions?.userPresentableMessage,
},
);
}
}
export function capitalizeFirstLetter(data: string) {
return data.charAt(0).toUpperCase() + data.slice(1);
}
export async function linearApiRequestAllItems(
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
propertyName: string,
body: any = {},
limit?: number,
): Promise<any> {
const returnData: IDataObject[] = [];
let responseData;
body.variables.first = limit && limit < 50 ? limit : 50;
body.variables.after = null;
const propertyPath = propertyName.split('.');
const nodesPath = [...propertyPath, 'nodes'];
const endCursorPath = [...propertyPath, 'pageInfo', 'endCursor'];
const hasNextPagePath = [...propertyPath, 'pageInfo', 'hasNextPage'];
do {
responseData = await linearApiRequest.call(this, body);
const nodes = get(responseData, nodesPath) as IDataObject[];
returnData.push(...nodes);
body.variables.after = get(responseData, endCursorPath);
if (limit && returnData.length >= limit) {
return returnData;
}
} while (get(responseData, hasNextPagePath));
return returnData;
}
export async function validateCredentials(
this: ICredentialTestFunctions,
decryptedCredentials: ICredentialDataDecryptedObject,
): Promise<any> {
const credentials = decryptedCredentials;
const options: IHttpRequestOptions = {
headers: {
'Content-Type': 'application/json',
Authorization: credentials.apiKey,
},
method: 'POST',
body: {
query: query.getIssues(),
variables: {
first: 1,
},
},
url: 'https://api.linear.app/graphql',
json: true,
};
return await this.helpers.request(options);
}
//@ts-ignore
export const sort = (a, b) => {
if (a.name.toLocaleLowerCase() < b.name.toLocaleLowerCase()) {
return -1;
}
if (a.name.toLocaleLowerCase() > b.name.toLocaleLowerCase()) {
return 1;
}
return 0;
};