ToolJet/plugins/packages/s3/lib/index.ts
Gandharv d367720c66
Add source options types for plugins (#1861)
* add source options types

* Remove query type

* fix source options

* add accesstoken - slack

* add types.ts file

* add idx files

* Add query options type for plugins (#1878)

* add query options type

* add missing import
2022-01-24 19:29:21 +05:30

61 lines
2 KiB
TypeScript

import { getObject, uploadObject, listBuckets, listObjects, signedUrlForGet, signedUrlForPut } from './operations';
import { S3Client } from '@aws-sdk/client-s3';
import { QueryError, QueryResult, QueryService, ConnectionTestResult } from '@tooljet-plugins/common'
import { SourceOptions, QueryOptions } from './types'
export default class S3QueryService implements QueryService {
async run(sourceOptions: SourceOptions, queryOptions: QueryOptions, dataSourceId: string): Promise<QueryResult> {
const operation = queryOptions.operation;
const client = await this.getConnection(sourceOptions, { operation });
let result = {};
try {
switch (operation) {
case 'list_buckets':
result = await listBuckets(client, {});
break;
case 'list_objects':
result = await listObjects(client, queryOptions);
break;
case 'get_object':
result = await getObject(client, queryOptions);
break;
case 'upload_object':
result = await uploadObject(client, queryOptions);
break;
case 'signed_url_for_get':
result = await signedUrlForGet(client, queryOptions);
break;
case 'signed_url_for_put':
result = await signedUrlForPut(client, queryOptions);
break;
}
} catch (error) {
throw new QueryError('Query could not be completed', error.message, {});
}
return {
status: 'ok',
data: result,
};
}
async testConnection(sourceOptions: SourceOptions): Promise<ConnectionTestResult> {
const client: S3Client = await this.getConnection(sourceOptions, {
operation: 'list_objects',
});
await listBuckets(client, {});
return {
status: 'ok',
};
}
async getConnection(sourceOptions: SourceOptions, options?: object): Promise<any> {
const credentials = {
accessKeyId: sourceOptions['access_key'],
secretAccessKey: sourceOptions['secret_key'],
};
return new S3Client({ region: sourceOptions['region'], credentials });
}
}