UNPKG

@skriptfabrik/n8n-nodes-google-enhanced

Version:
262 lines 15.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GoogleCloudStorageEnhanced = void 0; const tslib_1 = require("tslib"); const BucketDescription_1 = require("n8n-nodes-base/dist/nodes/Google/CloudStorage/BucketDescription"); const ObjectDescription_1 = require("n8n-nodes-base/dist/nodes/Google/CloudStorage/ObjectDescription"); const n8n_workflow_1 = require("n8n-workflow"); const GenericFunctions_1 = require("../GenericFunctions"); const GenericFunctions_2 = require("./GenericFunctions"); class GoogleCloudStorageEnhanced { constructor() { this.description = { displayName: 'Google Cloud Storage Enhanced', name: 'googleCloudStorageEnhanced', icon: 'file:googleCloudStorageEnhanced.svg', group: ['transform'], version: 1, subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', description: 'Use the Google Cloud Storage API', defaults: { name: 'Google Cloud Storage Enhanced', }, inputs: [n8n_workflow_1.NodeConnectionTypes.Main], outputs: [n8n_workflow_1.NodeConnectionTypes.Main], credentials: [ { name: 'googleApi', required: true, displayOptions: { show: { authentication: ['serviceAccount'], }, }, }, { name: 'googleCloudStorageOAuth2Api', required: true, displayOptions: { show: { authentication: ['oAuth2'], }, }, }, ], properties: [ { displayName: 'Authentication', name: 'authentication', type: 'options', options: [ { name: 'Service Account', value: 'serviceAccount', }, { name: 'OAuth2', value: 'oAuth2', }, ], default: 'serviceAccount', }, { displayName: 'Resource', name: 'resource', type: 'options', noDataExpression: true, options: [ { name: 'Bucket', value: 'bucket', }, { name: 'Object', value: 'object', }, ], default: 'bucket', }, // BUCKET ...BucketDescription_1.bucketOperations, ...BucketDescription_1.bucketFields, { displayName: 'Enable Object Retention', name: 'enableObjectRetention', type: 'boolean', displayOptions: { show: { resource: ['bucket'], operation: ['create'], }, }, default: false, description: 'Whether to permanently enable object retention for this bucket', }, // OBJECT ...ObjectDescription_1.objectOperations, ...ObjectDescription_1.objectFields, ], }; } execute() { return tslib_1.__awaiter(this, void 0, void 0, function* () { var _a; const items = this.getInputData(); const returnData = []; let responseData = {}; const resource = this.getNodeParameter('resource', 0); const operation = this.getNodeParameter('operation', 0); for (let item = 0; item < items.length; item++) { try { if (resource === 'bucket') { const bodyDataFields = [ 'acl', 'billing', 'cors', 'customPlacementConfig', 'dataLocations', 'defaultObjectAcl', 'encryption', 'iamConfiguration', 'labels', 'lifecycle', 'logging', 'retentionPolicy', 'versioning', 'website', ]; if (operation === 'create') { const bucketName = this.getNodeParameter('bucketName', item); const createData = this.getNodeParameter('createBody', item); const project = this.getNodeParameter('projectId', item); const enableObjectRetention = this.getNodeParameter('enableObjectRetention', item); const createAcl = this.getNodeParameter('createAcl', item); const projection = this.getNodeParameter('projection', item); responseData = yield GenericFunctions_2.googleApiRequest.call(this, 'POST', '/b', Object.assign({ name: bucketName }, (0, GenericFunctions_1.parseBodyData)(createData, bodyDataFields)), Object.assign(Object.assign({ project, enableObjectRetention }, createAcl), { projection })); } if (operation === 'delete') { const bucketName = this.getNodeParameter('bucketName', item); const deleteFilters = this.getNodeParameter('getFilters', item); responseData = yield GenericFunctions_2.googleApiRequest.call(this, 'DELETE', `/b/${bucketName}`, undefined, deleteFilters); } if (operation === 'get') { const bucketName = this.getNodeParameter('bucketName', item); const getFilters = this.getNodeParameter('getFilters', item); const projection = this.getNodeParameter('projection', item); responseData = yield GenericFunctions_2.googleApiRequest.call(this, 'GET', `/b/${bucketName}`, undefined, Object.assign(Object.assign({}, getFilters), { projection })); } if (operation === 'getAll') { const returnAll = this.getNodeParameter('returnAll', item); const project = this.getNodeParameter('projectId', item); const prefix = this.getNodeParameter('prefix', item); const projection = this.getNodeParameter('projection', item); responseData = yield GenericFunctions_2.googleApiRequestAllItems.call(this, 'GET', '/b', undefined, Object.assign({ project, prefix, projection }, (!returnAll && { maxResults: projection === 'noAcl' ? 1000 : 200, }))); } if (operation === 'update') { const bucketName = this.getNodeParameter('bucketName', item); const updateData = this.getNodeParameter('createBody', item); const updateFilters = this.getNodeParameter('getFilters', item); const createAcl = this.getNodeParameter('createAcl', item); const projection = this.getNodeParameter('projection', item); responseData = yield GenericFunctions_2.googleApiRequest.call(this, 'PATCH', `/b/${bucketName}`, (0, GenericFunctions_1.parseBodyData)(updateData, bodyDataFields), Object.assign(Object.assign(Object.assign({}, updateFilters), createAcl), { projection })); } } if (resource === 'object') { const bodyDataFields = ['acl', 'metadata']; if (operation === 'create') { const bucketName = this.getNodeParameter('bucketName', item); const objectName = this.getNodeParameter('objectName', item); const createData = this.getNodeParameter('createData', item); const createFilters = this.getNodeParameter('createQuery', item); const projection = this.getNodeParameter('updateProjection', item); const encryptionHeaders = this.getNodeParameter('encryptionHeaders', item); // Determine content, content type and known length let content; let contentType; let knownLength; const useBinary = this.getNodeParameter('createFromBinary', item); if (useBinary) { const binaryPropertyName = this.getNodeParameter('createBinaryPropertyName', item); const binaryData = this.helpers.assertBinaryData(item, binaryPropertyName); if (binaryData.id) { content = yield this.helpers.getBinaryStream(binaryData.id); const binaryMetadata = yield this.helpers.getBinaryMetadata(binaryData.id); contentType = (_a = binaryMetadata.mimeType) !== null && _a !== void 0 ? _a : 'application/octet-stream'; knownLength = binaryMetadata.fileSize; } else { content = Buffer.from(binaryData.data, n8n_workflow_1.BINARY_ENCODING); contentType = binaryData.mimeType; knownLength = content.length; } } else { content = this.getNodeParameter('createContent', item); contentType = createData['contentType'] || 'text/plain'; knownLength = content.length; } const body = (0, GenericFunctions_1.createMultipartForm)((0, GenericFunctions_1.parseBodyData)(createData, bodyDataFields), content, contentType, knownLength); responseData = yield GenericFunctions_2.googleApiRequest.call(this, 'POST', new URL(`https://storage.googleapis.com/upload/storage/v1/b/${bucketName}/o`), body, Object.assign(Object.assign({ name: objectName, uploadType: 'multipart' }, createFilters), { projection }), Object.assign({ 'Content-Length': body.getLengthSync(), 'Content-Type': `multipart/related; boundary=${body.getBoundary()}` }, encryptionHeaders)); } if (operation === 'delete') { const bucketName = this.getNodeParameter('bucketName', item); const objectName = this.getNodeParameter('objectName', item); const deleteParameters = this.getNodeParameter('getParameters', item); responseData = yield GenericFunctions_2.googleApiRequest.call(this, 'DELETE', `/b/${bucketName}/o/${objectName}`, undefined, deleteParameters); } if (operation === 'get') { const bucketName = this.getNodeParameter('bucketName', item); const objectName = this.getNodeParameter('objectName', item); const alt = this.getNodeParameter('alt', item); const getParameters = this.getNodeParameter('getParameters', item); const projection = this.getNodeParameter('projection', item); const encryptionHeaders = this.getNodeParameter('encryptionHeaders', item); responseData = yield GenericFunctions_2.googleApiRequest.call(this, 'GET', `/b/${bucketName}/o/${objectName}`, undefined, Object.assign(Object.assign({ alt }, getParameters), { projection }), encryptionHeaders); } if (operation === 'getAll') { const bucketName = this.getNodeParameter('bucketName', item); const listFilters = this.getNodeParameter('listFilters', item); const projection = this.getNodeParameter('projection', item); const returnAll = this.getNodeParameter('returnAll', item); const maxResults = this.getNodeParameter('maxResults', item); responseData = yield GenericFunctions_2.googleApiRequestAllItems.call(this, 'GET', `/b/${bucketName}/o`, undefined, Object.assign(Object.assign(Object.assign({}, listFilters), { projection }), (!returnAll && { maxResults, }))); } if (operation === 'update') { const bucketName = this.getNodeParameter('bucketName', item); const objectName = this.getNodeParameter('objectName', item); const updateData = this.getNodeParameter('updateData', item); const updateFilters = this.getNodeParameter('metagenAndAclQuery', item); const projection = this.getNodeParameter('updateProjection', item); const encryptionHeaders = this.getNodeParameter('encryptionHeaders', item); responseData = yield GenericFunctions_2.googleApiRequest.call(this, 'PATCH', `/b/${bucketName}/o/${objectName}`, (0, GenericFunctions_1.parseBodyData)(updateData, bodyDataFields), Object.assign(Object.assign({}, updateFilters), { projection }), encryptionHeaders); } } const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(responseData), { itemData: { item } }); returnData.push(...executionData); } catch (error) { if (this.continueOnFail()) { returnData.push({ json: { error: error.message }, pairedItem: { item }, }); continue; } throw error; } } return [returnData]; }); } } exports.GoogleCloudStorageEnhanced = GoogleCloudStorageEnhanced; //# sourceMappingURL=GoogleCloudStorageEnhanced.node.js.map