2022-03-10 06:59:48 +00:00
|
|
|
import { QueryError, QueryResult, QueryService } from '@tooljet-plugins/common';
|
|
|
|
|
import got, { Headers } from 'got';
|
|
|
|
|
import { SourceOptions } from './types';
|
2021-07-25 07:38:59 +00:00
|
|
|
|
|
|
|
|
export default class StripeQueryService implements QueryService {
|
2022-01-17 07:08:17 +00:00
|
|
|
authHeader(token: string): Headers {
|
2021-09-21 13:48:28 +00:00
|
|
|
return { Authorization: `Bearer ${token}` };
|
2021-07-25 07:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
2022-01-24 13:59:21 +00:00
|
|
|
async run(sourceOptions: SourceOptions, queryOptions: any, dataSourceId: string): Promise<QueryResult> {
|
2021-09-21 13:48:28 +00:00
|
|
|
let result = {};
|
2021-07-25 07:38:59 +00:00
|
|
|
const operation = queryOptions.operation;
|
|
|
|
|
|
2022-01-24 13:59:21 +00:00
|
|
|
const apiKey = sourceOptions.api_key;
|
2021-07-25 07:38:59 +00:00
|
|
|
const baseUrl = 'https://api.stripe.com';
|
|
|
|
|
const path = queryOptions['path'];
|
|
|
|
|
let url = `${baseUrl}${path}`;
|
|
|
|
|
|
|
|
|
|
const pathParams = queryOptions['params']['path'];
|
|
|
|
|
const queryParams = queryOptions['params']['query'];
|
|
|
|
|
const bodyParams = queryOptions['params']['request'];
|
|
|
|
|
|
|
|
|
|
// Replace path params of url
|
2021-09-21 13:48:28 +00:00
|
|
|
for (const param of Object.keys(pathParams)) {
|
2021-07-25 07:38:59 +00:00
|
|
|
url = url.replace(`{${param}}`, pathParams[param]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let response = null;
|
|
|
|
|
|
|
|
|
|
try {
|
2021-09-21 13:48:28 +00:00
|
|
|
if (operation === 'get') {
|
|
|
|
|
response = await got(url, {
|
|
|
|
|
method: operation,
|
2021-07-25 07:38:59 +00:00
|
|
|
headers: this.authHeader(apiKey),
|
2021-09-21 13:48:28 +00:00
|
|
|
searchParams: queryParams,
|
2021-07-25 07:38:59 +00:00
|
|
|
});
|
|
|
|
|
} else {
|
2022-09-20 08:11:18 +00:00
|
|
|
const resolvedBodyParams = this.resolveBodyparams(bodyParams);
|
2021-09-21 13:48:28 +00:00
|
|
|
response = await got(url, {
|
|
|
|
|
method: operation,
|
2021-07-25 07:38:59 +00:00
|
|
|
headers: this.authHeader(apiKey),
|
2022-09-20 08:11:18 +00:00
|
|
|
form: resolvedBodyParams,
|
2021-09-21 13:48:28 +00:00
|
|
|
searchParams: queryParams,
|
2021-07-25 07:38:59 +00:00
|
|
|
});
|
|
|
|
|
}
|
2021-09-21 13:48:28 +00:00
|
|
|
|
2021-07-25 07:38:59 +00:00
|
|
|
result = JSON.parse(response.body);
|
|
|
|
|
} catch (error) {
|
2022-07-06 05:40:12 +00:00
|
|
|
throw new QueryError('Query could not be completed', error.response.body, {});
|
2021-07-25 07:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
status: 'ok',
|
2021-09-21 13:48:28 +00:00
|
|
|
data: result,
|
|
|
|
|
};
|
2021-07-25 07:38:59 +00:00
|
|
|
}
|
2022-09-20 08:11:18 +00:00
|
|
|
|
|
|
|
|
private resolveBodyparams(bodyParams: object): object {
|
|
|
|
|
if (typeof bodyParams === 'string') {
|
|
|
|
|
return bodyParams;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const expectedResult = {};
|
|
|
|
|
|
|
|
|
|
for (const key of Object.keys(bodyParams)) {
|
|
|
|
|
if (typeof bodyParams[key] === 'object') {
|
|
|
|
|
for (const subKey of Object.keys(bodyParams[key])) {
|
|
|
|
|
expectedResult[`${key}[${subKey}]`] = bodyParams[key][subKey];
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
expectedResult[key] = bodyParams[key];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return expectedResult;
|
|
|
|
|
}
|
2021-07-25 07:38:59 +00:00
|
|
|
}
|