mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-14 08:34:07 -08:00
⚡ add search operation
This commit is contained in:
parent
956632d321
commit
40b1aa9d83
|
@ -13,6 +13,7 @@ import {
|
|||
|
||||
import {
|
||||
boxApiRequest,
|
||||
boxApiRequestAllItems,
|
||||
} from './GenericFunctions';
|
||||
|
||||
import {
|
||||
|
@ -25,6 +26,8 @@ import {
|
|||
folderOperations,
|
||||
} from './FolderDescription';
|
||||
|
||||
import * as moment from 'moment-timezone';
|
||||
|
||||
export class Box implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
displayName: 'Box',
|
||||
|
@ -156,6 +159,45 @@ export class Box implements INodeType {
|
|||
responseData = await boxApiRequest.call(this, 'GET', `/files/${fileId}`, {}, qs);
|
||||
returnData.push(responseData as IDataObject);
|
||||
}
|
||||
//https://developer.box.com/reference/get-search/
|
||||
if (operation === 'search') {
|
||||
const query = this.getNodeParameter('query', i) as string;
|
||||
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
||||
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
||||
const timezone = this.getTimezone();
|
||||
qs.type = 'file';
|
||||
qs.query = query;
|
||||
Object.assign(qs, additionalFields);
|
||||
|
||||
if (qs.content_types) {
|
||||
qs.content_types = (qs.content_types as string).split(',');
|
||||
}
|
||||
|
||||
if (additionalFields.createdRangeUi) {
|
||||
const createdRangeValues = (additionalFields.createdRangeUi as IDataObject).createdRangeValuesUi as IDataObject;
|
||||
if (createdRangeValues) {
|
||||
qs.created_at_range = `${moment.tz(createdRangeValues.from, timezone).format()},${moment.tz(createdRangeValues.to, timezone).format()}`;
|
||||
}
|
||||
delete qs.createdRangeUi;
|
||||
}
|
||||
|
||||
if (additionalFields.updatedRangeUi) {
|
||||
const updateRangeValues = (additionalFields.updatedRangeUi as IDataObject).updatedRangeValuesUi as IDataObject;
|
||||
if (updateRangeValues) {
|
||||
qs.updated_at_range = `${moment.tz(updateRangeValues.from, timezone).format()},${moment.tz(updateRangeValues.to, timezone).format()}`;
|
||||
}
|
||||
delete qs.updatedRangeUi;
|
||||
}
|
||||
|
||||
if (returnAll) {
|
||||
responseData = await boxApiRequestAllItems.call(this, 'entries', 'GET', `/search`, {}, qs);
|
||||
} else {
|
||||
qs.limit = this.getNodeParameter('limit', i) as number;
|
||||
responseData = await boxApiRequest.call(this, 'GET', `/search`, {}, qs);
|
||||
responseData = responseData.entries;
|
||||
}
|
||||
returnData.push.apply(returnData, responseData as IDataObject[]);
|
||||
}
|
||||
//https://developer.box.com/reference/post-files-content
|
||||
if (operation === 'upload') {
|
||||
const parentId = this.getNodeParameter('parentId', i) as string;
|
||||
|
@ -267,7 +309,45 @@ export class Box implements INodeType {
|
|||
responseData = await boxApiRequest.call(this, 'DELETE', `/folders/${folderId}`, qs);
|
||||
responseData = { success: true };
|
||||
returnData.push(responseData as IDataObject);
|
||||
}
|
||||
//https://developer.box.com/reference/get-search/
|
||||
if (operation === 'search') {
|
||||
const query = this.getNodeParameter('query', i) as string;
|
||||
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
||||
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
||||
const timezone = this.getTimezone();
|
||||
qs.type = 'folder';
|
||||
qs.query = query;
|
||||
Object.assign(qs, additionalFields);
|
||||
|
||||
if (qs.content_types) {
|
||||
qs.content_types = (qs.content_types as string).split(',');
|
||||
}
|
||||
|
||||
if (additionalFields.createdRangeUi) {
|
||||
const createdRangeValues = (additionalFields.createdRangeUi as IDataObject).createdRangeValuesUi as IDataObject;
|
||||
if (createdRangeValues) {
|
||||
qs.created_at_range = `${moment.tz(createdRangeValues.from, timezone).format()},${moment.tz(createdRangeValues.to, timezone).format()}`;
|
||||
}
|
||||
delete qs.createdRangeUi;
|
||||
}
|
||||
|
||||
if (additionalFields.updatedRangeUi) {
|
||||
const updateRangeValues = (additionalFields.updatedRangeUi as IDataObject).updatedRangeValuesUi as IDataObject;
|
||||
if (updateRangeValues) {
|
||||
qs.updated_at_range = `${moment.tz(updateRangeValues.from, timezone).format()},${moment.tz(updateRangeValues.to, timezone).format()}`;
|
||||
}
|
||||
delete qs.updatedRangeUi;
|
||||
}
|
||||
|
||||
if (returnAll) {
|
||||
responseData = await boxApiRequestAllItems.call(this, 'entries', 'GET', `/search`, {}, qs);
|
||||
} else {
|
||||
qs.limit = this.getNodeParameter('limit', i) as number;
|
||||
responseData = await boxApiRequest.call(this, 'GET', `/search`, {}, qs);
|
||||
responseData = responseData.entries;
|
||||
}
|
||||
returnData.push.apply(returnData, responseData as IDataObject[]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,6 +35,11 @@ export const fileOperations = [
|
|||
value: 'get',
|
||||
description: 'Get a file',
|
||||
},
|
||||
{
|
||||
name: 'Search',
|
||||
value: 'search',
|
||||
description: 'Search files',
|
||||
},
|
||||
{
|
||||
name: 'Upload',
|
||||
value: 'upload',
|
||||
|
@ -231,6 +236,262 @@ export const fileFields = [
|
|||
],
|
||||
},
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* file:search */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
{
|
||||
displayName: 'Query',
|
||||
name: 'query',
|
||||
type: 'string',
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: [
|
||||
'search',
|
||||
],
|
||||
resource: [
|
||||
'file',
|
||||
],
|
||||
},
|
||||
},
|
||||
default: '',
|
||||
description: 'The string to search for. This query is matched against item names, descriptions, text content of files, and various other fields of the different item types.',
|
||||
},
|
||||
{
|
||||
displayName: 'Return All',
|
||||
name: 'returnAll',
|
||||
type: 'boolean',
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: [
|
||||
'search',
|
||||
],
|
||||
resource: [
|
||||
'file',
|
||||
],
|
||||
},
|
||||
},
|
||||
default: false,
|
||||
description: 'If all results should be returned or only up to a given limit.',
|
||||
},
|
||||
{
|
||||
displayName: 'Limit',
|
||||
name: 'limit',
|
||||
type: 'number',
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: [
|
||||
'search',
|
||||
],
|
||||
resource: [
|
||||
'file',
|
||||
],
|
||||
returnAll: [
|
||||
false,
|
||||
],
|
||||
},
|
||||
},
|
||||
typeOptions: {
|
||||
minValue: 1,
|
||||
maxValue: 500,
|
||||
},
|
||||
default: 100,
|
||||
description: 'How many results to return.',
|
||||
},
|
||||
{
|
||||
displayName: 'Additional Fields',
|
||||
name: 'additionalFields',
|
||||
type: 'collection',
|
||||
placeholder: 'Add Field',
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: [
|
||||
'search',
|
||||
],
|
||||
resource: [
|
||||
'file',
|
||||
],
|
||||
},
|
||||
},
|
||||
default: {},
|
||||
options: [
|
||||
{
|
||||
displayName: 'Content Types',
|
||||
name: 'contet_types',
|
||||
type: 'string',
|
||||
default: '',
|
||||
description: `Limits search results to items with the given content types.</br>
|
||||
Content types are defined as a comma separated lists of Box recognized content types.`,
|
||||
},
|
||||
{
|
||||
displayName: 'Created At Range',
|
||||
name: 'createdRangeUi',
|
||||
type: 'fixedCollection',
|
||||
typeOptions: {
|
||||
multipleValues: false,
|
||||
},
|
||||
placeholder: 'Add Range',
|
||||
default: {},
|
||||
options: [
|
||||
{
|
||||
displayName: 'Range',
|
||||
name: 'createdRangeValuesUi',
|
||||
values: [
|
||||
{
|
||||
displayName: 'From',
|
||||
name: 'from',
|
||||
type: 'dateTime',
|
||||
default: '',
|
||||
},
|
||||
{
|
||||
displayName: 'To',
|
||||
name: 'to',
|
||||
type: 'dateTime',
|
||||
default: '',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
displayName: 'Direction',
|
||||
name: 'direction',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'ASC',
|
||||
value: 'ASC',
|
||||
},
|
||||
{
|
||||
name: 'DESC',
|
||||
value: 'DESC',
|
||||
},
|
||||
],
|
||||
default: '',
|
||||
description: 'Defines the direction in which search results are ordered. Default value is DESC.',
|
||||
},
|
||||
{
|
||||
displayName: 'Fields',
|
||||
name: 'fields',
|
||||
type: 'string',
|
||||
default: '',
|
||||
description: 'A comma-separated list of attributes to include in the response. This can be used to request fields that are not normally returned in a standard response.',
|
||||
},
|
||||
{
|
||||
displayName: 'File Extensions',
|
||||
name: 'file_extensions',
|
||||
type: 'string',
|
||||
default: '',
|
||||
placeholder: 'pdf,png,gif',
|
||||
description: 'Limits search results to a comma-separated list of file extensions.',
|
||||
},
|
||||
{
|
||||
displayName: 'Folder IDs',
|
||||
name: 'ancestor_folder_ids',
|
||||
type: 'string',
|
||||
default: '',
|
||||
description: `Limits search results to items within the given list of folders.</br>
|
||||
Folders are defined as a comma separated lists of folder IDs.`,
|
||||
},
|
||||
{
|
||||
displayName: 'Scope',
|
||||
name: 'scope',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'User Content',
|
||||
value: 'user_content',
|
||||
},
|
||||
{
|
||||
name: 'Enterprise Content',
|
||||
value: 'enterprise_content',
|
||||
},
|
||||
],
|
||||
default: '',
|
||||
description: 'Limits search results to a user scope.',
|
||||
},
|
||||
{
|
||||
displayName: 'Size Range',
|
||||
name: 'size_range',
|
||||
type: 'string',
|
||||
default: '',
|
||||
placeholder: '1000000,5000000',
|
||||
description: `Limits search results to items within a given file size range.</br>
|
||||
File size ranges are defined as comma separated byte sizes.`,
|
||||
},
|
||||
{
|
||||
displayName: 'Sort',
|
||||
name: 'sort',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'Relevance',
|
||||
value: 'relevance',
|
||||
},
|
||||
{
|
||||
name: 'Modified At',
|
||||
value: 'modified_at',
|
||||
},
|
||||
],
|
||||
default: 'relevance',
|
||||
description: 'returns the results ordered in descending order by date at which the item was last modified.',
|
||||
},
|
||||
{
|
||||
displayName: 'Trash Content',
|
||||
name: 'trash_content',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'Non Trashed Only',
|
||||
value: 'non_trashed_only',
|
||||
},
|
||||
{
|
||||
name: 'Trashed Only',
|
||||
value: 'trashed_only',
|
||||
},
|
||||
],
|
||||
default: 'non_trashed_only',
|
||||
description: 'Controls if search results include the trash.',
|
||||
},
|
||||
{
|
||||
displayName: 'Update At Range',
|
||||
name: 'updatedRangeUi',
|
||||
type: 'fixedCollection',
|
||||
typeOptions: {
|
||||
multipleValues: false,
|
||||
},
|
||||
placeholder: 'Add Range',
|
||||
default: {},
|
||||
options: [
|
||||
{
|
||||
displayName: 'Range',
|
||||
name: 'updatedRangeValuesUi',
|
||||
values: [
|
||||
{
|
||||
displayName: 'From',
|
||||
name: 'from',
|
||||
type: 'dateTime',
|
||||
default: '',
|
||||
},
|
||||
{
|
||||
displayName: 'To',
|
||||
name: 'to',
|
||||
type: 'dateTime',
|
||||
default: '',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
displayName: 'User IDs',
|
||||
name: 'owner_user_ids',
|
||||
type: 'string',
|
||||
default: '',
|
||||
description: `Limits search results to items owned by the given list of owners..</br>
|
||||
Owners are defined as a comma separated list of user IDs.`,
|
||||
},
|
||||
],
|
||||
},
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* file:upload */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
{
|
||||
|
|
|
@ -25,6 +25,11 @@ export const folderOperations = [
|
|||
value: 'delete',
|
||||
description: 'Delete a folder',
|
||||
},
|
||||
{
|
||||
name: 'Search',
|
||||
value: 'search',
|
||||
description: 'Search files',
|
||||
},
|
||||
],
|
||||
default: 'create',
|
||||
description: 'The operation to perform.',
|
||||
|
@ -153,4 +158,260 @@ export const folderFields = [
|
|||
default: false,
|
||||
description: 'Delete a folder that is not empty by recursively deleting the folder and all of its content.',
|
||||
},
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* file:search */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
{
|
||||
displayName: 'Query',
|
||||
name: 'query',
|
||||
type: 'string',
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: [
|
||||
'search',
|
||||
],
|
||||
resource: [
|
||||
'folder',
|
||||
],
|
||||
},
|
||||
},
|
||||
default: '',
|
||||
description: 'The string to search for. This query is matched against item names, descriptions, text content of files, and various other fields of the different item types.',
|
||||
},
|
||||
{
|
||||
displayName: 'Return All',
|
||||
name: 'returnAll',
|
||||
type: 'boolean',
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: [
|
||||
'search',
|
||||
],
|
||||
resource: [
|
||||
'folder',
|
||||
],
|
||||
},
|
||||
},
|
||||
default: false,
|
||||
description: 'If all results should be returned or only up to a given limit.',
|
||||
},
|
||||
{
|
||||
displayName: 'Limit',
|
||||
name: 'limit',
|
||||
type: 'number',
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: [
|
||||
'search',
|
||||
],
|
||||
resource: [
|
||||
'folder',
|
||||
],
|
||||
returnAll: [
|
||||
false,
|
||||
],
|
||||
},
|
||||
},
|
||||
typeOptions: {
|
||||
minValue: 1,
|
||||
maxValue: 500,
|
||||
},
|
||||
default: 100,
|
||||
description: 'How many results to return.',
|
||||
},
|
||||
{
|
||||
displayName: 'Additional Fields',
|
||||
name: 'additionalFields',
|
||||
type: 'collection',
|
||||
placeholder: 'Add Field',
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: [
|
||||
'search',
|
||||
],
|
||||
resource: [
|
||||
'folder',
|
||||
],
|
||||
},
|
||||
},
|
||||
default: {},
|
||||
options: [
|
||||
{
|
||||
displayName: 'Content Types',
|
||||
name: 'contet_types',
|
||||
type: 'string',
|
||||
default: '',
|
||||
description: `Limits search results to items with the given content types.</br>
|
||||
Content types are defined as a comma separated lists of Box recognized content types.`,
|
||||
},
|
||||
{
|
||||
displayName: 'Created At Range',
|
||||
name: 'createdRangeUi',
|
||||
type: 'fixedCollection',
|
||||
typeOptions: {
|
||||
multipleValues: false,
|
||||
},
|
||||
placeholder: 'Add Range',
|
||||
default: {},
|
||||
options: [
|
||||
{
|
||||
displayName: 'Range',
|
||||
name: 'createdRangeValuesUi',
|
||||
values: [
|
||||
{
|
||||
displayName: 'From',
|
||||
name: 'from',
|
||||
type: 'dateTime',
|
||||
default: '',
|
||||
},
|
||||
{
|
||||
displayName: 'To',
|
||||
name: 'to',
|
||||
type: 'dateTime',
|
||||
default: '',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
displayName: 'Direction',
|
||||
name: 'direction',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'ASC',
|
||||
value: 'ASC',
|
||||
},
|
||||
{
|
||||
name: 'DESC',
|
||||
value: 'DESC',
|
||||
},
|
||||
],
|
||||
default: '',
|
||||
description: 'Defines the direction in which search results are ordered. Default value is DESC.',
|
||||
},
|
||||
{
|
||||
displayName: 'Fields',
|
||||
name: 'fields',
|
||||
type: 'string',
|
||||
default: '',
|
||||
description: 'A comma-separated list of attributes to include in the response. This can be used to request fields that are not normally returned in a standard response.',
|
||||
},
|
||||
{
|
||||
displayName: 'File Extensions',
|
||||
name: 'file_extensions',
|
||||
type: 'string',
|
||||
default: '',
|
||||
placeholder: 'pdf,png,gif',
|
||||
description: 'Limits search results to a comma-separated list of file extensions.',
|
||||
},
|
||||
{
|
||||
displayName: 'Folder IDs',
|
||||
name: 'ancestor_folder_ids',
|
||||
type: 'string',
|
||||
default: '',
|
||||
description: `Limits search results to items within the given list of folders.</br>
|
||||
Folders are defined as a comma separated lists of folder IDs.`,
|
||||
},
|
||||
{
|
||||
displayName: 'Scope',
|
||||
name: 'scope',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'User Content',
|
||||
value: 'user_content',
|
||||
},
|
||||
{
|
||||
name: 'Enterprise Content',
|
||||
value: 'enterprise_content',
|
||||
},
|
||||
],
|
||||
default: '',
|
||||
description: 'Limits search results to a user scope.',
|
||||
},
|
||||
{
|
||||
displayName: 'Size Range',
|
||||
name: 'size_range',
|
||||
type: 'string',
|
||||
default: '',
|
||||
placeholder: '1000000,5000000',
|
||||
description: `Limits search results to items within a given file size range.</br>
|
||||
File size ranges are defined as comma separated byte sizes.`,
|
||||
},
|
||||
{
|
||||
displayName: 'Sort',
|
||||
name: 'sort',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'Relevance',
|
||||
value: 'relevance',
|
||||
},
|
||||
{
|
||||
name: 'Modified At',
|
||||
value: 'modified_at',
|
||||
},
|
||||
],
|
||||
default: 'relevance',
|
||||
description: 'returns the results ordered in descending order by date at which the item was last modified.',
|
||||
},
|
||||
{
|
||||
displayName: 'Trash Content',
|
||||
name: 'trash_content',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'Non Trashed Only',
|
||||
value: 'non_trashed_only',
|
||||
},
|
||||
{
|
||||
name: 'Trashed Only',
|
||||
value: 'trashed_only',
|
||||
},
|
||||
],
|
||||
default: 'non_trashed_only',
|
||||
description: 'Controls if search results include the trash.',
|
||||
},
|
||||
{
|
||||
displayName: 'Update At Range',
|
||||
name: 'updatedRangeUi',
|
||||
type: 'fixedCollection',
|
||||
typeOptions: {
|
||||
multipleValues: false,
|
||||
},
|
||||
placeholder: 'Add Range',
|
||||
default: {},
|
||||
options: [
|
||||
{
|
||||
displayName: 'Range',
|
||||
name: 'updatedRangeValuesUi',
|
||||
values: [
|
||||
{
|
||||
displayName: 'From',
|
||||
name: 'from',
|
||||
type: 'dateTime',
|
||||
default: '',
|
||||
},
|
||||
{
|
||||
displayName: 'To',
|
||||
name: 'to',
|
||||
type: 'dateTime',
|
||||
default: '',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
displayName: 'User IDs',
|
||||
name: 'owner_user_ids',
|
||||
type: 'string',
|
||||
default: '',
|
||||
description: `Limits search results to items owned by the given list of owners..</br>
|
||||
Owners are defined as a comma separated list of user IDs.`,
|
||||
},
|
||||
],
|
||||
},
|
||||
] as INodeProperties[];
|
||||
|
|
|
@ -41,7 +41,6 @@ export async function boxApiRequest(this: IExecuteFunctions | IExecuteSingleFunc
|
|||
return await this.helpers.requestOAuth2.call(this, 'boxOAuth2Api', options, oAuth2Options);
|
||||
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
|
||||
let errorMessage;
|
||||
|
||||
|
@ -73,14 +72,13 @@ export async function boxApiRequestAllItems(this: IExecuteFunctions | ILoadOptio
|
|||
|
||||
let responseData;
|
||||
query.limit = 100;
|
||||
|
||||
query.offset = 0;
|
||||
do {
|
||||
responseData = await boxApiRequest.call(this, method, endpoint, body, query);
|
||||
query.marker = responseData['next_marker'];
|
||||
query.offset = responseData['offset'] + query.limit;
|
||||
returnData.push.apply(returnData, responseData[propertyName]);
|
||||
} while (
|
||||
responseData['next_marker'] !== undefined &&
|
||||
responseData['next_marker'] !== ''
|
||||
responseData[propertyName].length !== 0
|
||||
);
|
||||
|
||||
return returnData;
|
||||
|
|
Loading…
Reference in a new issue