n8n-vikunja-nodes/nodes/Vikunja/helper.ts

50 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-10-24 14:05:41 +00:00
import {OptionsWithUri} from 'request'
2023-10-24 16:24:53 +00:00
import {
IDataObject,
IHookFunctions,
ILoadOptionsFunctions,
INodeListSearchResult,
JsonObject,
NodeApiError,
2024-04-09 12:52:34 +00:00
IExecuteFunctions,
2023-10-24 16:24:53 +00:00
} from 'n8n-workflow'
2023-10-24 14:05:41 +00:00
2023-10-24 15:37:35 +00:00
export async function searchAndMap(context: ILoadOptionsFunctions, url: string, titleProperty: string = 'title'): Promise<INodeListSearchResult> {
2023-10-24 14:05:41 +00:00
try {
2023-10-24 16:24:53 +00:00
// @ts-ignore
const items = await apiRequest(context, 'GET', url)
2023-10-24 14:05:41 +00:00
return {
2023-10-24 15:37:35 +00:00
results: items.map((item: IDataObject) => ({
name: item[titleProperty],
value: item.id,
2023-10-24 14:05:41 +00:00
})),
}
} catch (error) {
throw new NodeApiError(context.getNode(), error as JsonObject)
}
}
2023-10-24 16:24:53 +00:00
export async function apiRequest(
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
method: string,
endpoint: string,
body?: object,
query?: IDataObject,
): Promise<any> {
query = query || {}
body = body || {}
const credentialType = 'vikunjaApi'
const {apiUrl} = await this.getCredentials(credentialType)
const options: OptionsWithUri = {
method,
body,
qs: query,
uri: apiUrl + endpoint,
json: true,
}
return this.helpers.requestWithAuthentication.call(this, credentialType, options)
}