feat: Convert all app files to JS

This commit is contained in:
Faruk AYDIN
2024-01-05 17:44:21 +01:00
parent b95478b635
commit 43dba351c3
1030 changed files with 5114 additions and 6436 deletions

View File

@@ -1,23 +1,25 @@
import path from 'node:path';
import defineAction from '../../../../helpers/define-action';
import defineAction from '../../../../helpers/define-action.js';
export default defineAction({
name: 'Create folder',
key: 'createFolder',
description: 'Create a new folder with the given parent folder and folder name',
description:
'Create a new folder with the given parent folder and folder name',
arguments: [
{
label: 'Folder',
key: 'parentFolder',
type: 'string' as const,
type: 'string',
required: true,
description: 'Enter the parent folder path, like /TextFiles/ or /Documents/Taxes/',
description:
'Enter the parent folder path, like /TextFiles/ or /Documents/Taxes/',
variables: true,
},
{
label: 'Folder Name',
key: 'folderName',
type: 'string' as const,
type: 'string',
required: true,
description: 'Enter the name for the new folder',
variables: true,
@@ -25,11 +27,13 @@ export default defineAction({
],
async run($) {
const parentFolder = $.step.parameters.parentFolder as string;
const folderName = $.step.parameters.folderName as string;
const parentFolder = $.step.parameters.parentFolder;
const folderName = $.step.parameters.folderName;
const folderPath = path.join(parentFolder, folderName);
const response = await $.http.post('/2/files/create_folder_v2', { path: folderPath });
const response = await $.http.post('/2/files/create_folder_v2', {
path: folderPath,
});
$.setActionItem({ raw: response.data });
},

View File

@@ -0,0 +1,4 @@
import createFolder from './create-folder/index.js';
import renameFile from './rename-file/index.js';
export default [createFolder, renameFile];

View File

@@ -1,4 +0,0 @@
import createFolder from "./create-folder";
import renameFile from "./rename-file";
export default [createFolder, renameFile];

View File

@@ -1,5 +1,5 @@
import path from 'node:path';
import defineAction from '../../../../helpers/define-action';
import defineAction from '../../../../helpers/define-action.js';
export default defineAction({
name: 'Rename file',
@@ -9,25 +9,25 @@ export default defineAction({
{
label: 'File Path',
key: 'filePath',
type: 'string' as const,
type: 'string',
required: true,
description:
'Write the full path to the file such as /Folder1/File.pdf',
description: 'Write the full path to the file such as /Folder1/File.pdf',
variables: true,
},
{
label: 'New Name',
key: 'newName',
type: 'string' as const,
type: 'string',
required: true,
description: "Enter the new name for the file (without the extension, e.g., '.pdf')",
description:
"Enter the new name for the file (without the extension, e.g., '.pdf')",
variables: true,
},
],
async run($) {
const filePath = $.step.parameters.filePath as string;
const newName = $.step.parameters.newName as string;
const filePath = $.step.parameters.filePath;
const newName = $.step.parameters.newName;
const fileObject = path.parse(filePath);
const newPath = path.format({
dir: fileObject.dir,

View File

@@ -1,15 +1,15 @@
import { URLSearchParams } from 'url';
import { IField, IGlobalVariable } from '@automatisch/types';
import scopes from '../common/scopes';
import scopes from '../common/scopes.js';
export default async function generateAuthUrl($: IGlobalVariable) {
export default async function generateAuthUrl($) {
const oauthRedirectUrlField = $.app.auth.fields.find(
(field: IField) => field.key == 'oAuthRedirectUrl'
(field) => field.key == 'oAuthRedirectUrl'
);
const callbackUrl = oauthRedirectUrlField.value as string;
const callbackUrl = oauthRedirectUrlField.value;
const searchParams = new URLSearchParams({
client_id: $.auth.data.clientId as string,
client_id: $.auth.data.clientId,
redirect_uri: callbackUrl,
response_type: 'code',
scope: scopes.join(' '),

View File

@@ -1,14 +1,14 @@
import generateAuthUrl from './generate-auth-url';
import verifyCredentials from './verify-credentials';
import isStillVerified from './is-still-verified';
import refreshToken from './refresh-token';
import generateAuthUrl from './generate-auth-url.js';
import verifyCredentials from './verify-credentials.js';
import isStillVerified from './is-still-verified.js';
import refreshToken from './refresh-token.js';
export default {
fields: [
{
key: 'oAuthRedirectUrl',
label: 'OAuth Redirect URL',
type: 'string' as const,
type: 'string',
required: true,
readOnly: true,
value: '{WEB_APP_URL}/app/dropbox/connections/add',
@@ -20,7 +20,7 @@ export default {
{
key: 'clientId',
label: 'App Key',
type: 'string' as const,
type: 'string',
required: true,
readOnly: false,
value: null,
@@ -31,7 +31,7 @@ export default {
{
key: 'clientSecret',
label: 'App Secret',
type: 'string' as const,
type: 'string',
required: true,
readOnly: false,
value: null,

View File

@@ -0,0 +1,8 @@
import getCurrentAccount from '../common/get-current-account.js';
const isStillVerified = async ($) => {
const account = await getCurrentAccount($);
return !!account;
};
export default isStillVerified;

View File

@@ -1,9 +0,0 @@
import { IGlobalVariable } from '@automatisch/types';
import getCurrentAccount from '../common/get-current-account';
const isStillVerified = async ($: IGlobalVariable) => {
const account = await getCurrentAccount($);
return !!account;
};
export default isStillVerified;

View File

@@ -0,0 +1,36 @@
import { Buffer } from 'node:buffer';
const refreshToken = async ($) => {
const params = {
grant_type: 'refresh_token',
refresh_token: $.auth.data.refreshToken,
};
const basicAuthToken = Buffer.from(
`${$.auth.data.clientId}:${$.auth.data.clientSecret}`
).toString('base64');
const { data } = await $.http.post('oauth2/token', null, {
params,
headers: {
Authorization: `Basic ${basicAuthToken}`,
},
additionalProperties: {
skipAddingAuthHeader: true,
},
});
const {
access_token: accessToken,
expires_in: expiresIn,
token_type: tokenType,
} = data;
await $.auth.set({
accessToken,
expiresIn,
tokenType,
});
};
export default refreshToken;

View File

@@ -1,41 +0,0 @@
import { Buffer } from 'node:buffer';
import { IGlobalVariable } from '@automatisch/types';
const refreshToken = async ($: IGlobalVariable) => {
const params = {
grant_type: 'refresh_token',
refresh_token: $.auth.data.refreshToken as string,
};
const basicAuthToken = Buffer
.from(`${$.auth.data.clientId}:${$.auth.data.clientSecret}`)
.toString('base64');
const { data } = await $.http.post(
'oauth2/token',
null,
{
params,
headers: {
Authorization: `Basic ${basicAuthToken}`
},
additionalProperties: {
skipAddingAuthHeader: true
}
}
);
const {
access_token: accessToken,
expires_in: expiresIn,
token_type: tokenType,
} = data;
await $.auth.set({
accessToken,
expiresIn,
tokenType,
});
};
export default refreshToken;

View File

@@ -1,44 +1,20 @@
import { IGlobalVariable, IField } from '@automatisch/types';
import getCurrentAccount from '../common/get-current-account';
import getCurrentAccount from '../common/get-current-account.js';
type TAccount = {
account_id: string,
name: {
given_name: string,
surname: string,
familiar_name: string,
display_name: string,
abbreviated_name: string,
},
email: string,
email_verified: boolean,
disabled: boolean,
country: string,
locale: string,
referral_link: string,
is_paired: boolean,
account_type: {
".tag": string,
},
root_info: {
".tag": string,
root_namespace_id: string,
home_namespace_id: string,
},
}
const verifyCredentials = async ($: IGlobalVariable) => {
const verifyCredentials = async ($) => {
const oauthRedirectUrlField = $.app.auth.fields.find(
(field: IField) => field.key == 'oAuthRedirectUrl'
(field) => field.key == 'oAuthRedirectUrl'
);
const redirectUrl = oauthRedirectUrlField.value as string;
const redirectUrl = oauthRedirectUrlField.value;
const params = {
client_id: $.auth.data.clientId as string,
client_id: $.auth.data.clientId,
redirect_uri: redirectUrl,
client_secret: $.auth.data.clientSecret as string,
code: $.auth.data.code as string,
client_secret: $.auth.data.clientSecret,
code: $.auth.data.code,
grant_type: 'authorization_code',
}
};
const { data: verifiedCredentials } = await $.http.post(
'/oauth2/token',
null,
@@ -66,10 +42,10 @@ const verifyCredentials = async ($: IGlobalVariable) => {
accountId,
teamId,
idToken,
uid
uid,
});
const account = await getCurrentAccount($) as TAccount;
const account = await getCurrentAccount($);
await $.auth.set({
accountId: account.account_id,
@@ -88,10 +64,10 @@ const verifyCredentials = async ($: IGlobalVariable) => {
referralLink: account.referral_link,
isPaired: account.is_paired,
accountType: {
".tag": account.account_type['.tag'],
'.tag': account.account_type['.tag'],
},
rootInfo: {
".tag": account.root_info['.tag'],
'.tag': account.root_info['.tag'],
rootNamespaceId: account.root_info.root_namespace_id,
homeNamespaceId: account.root_info.home_namespace_id,
},

View File

@@ -0,0 +1,14 @@
const addAuthHeader = ($, requestConfig) => {
requestConfig.headers['Content-Type'] = 'application/json';
if (
!requestConfig.additionalProperties?.skipAddingAuthHeader &&
$.auth.data?.accessToken
) {
requestConfig.headers.Authorization = `Bearer ${$.auth.data.accessToken}`;
}
return requestConfig;
};
export default addAuthHeader;

View File

@@ -1,13 +0,0 @@
import { TBeforeRequest } from '@automatisch/types';
const addAuthHeader: TBeforeRequest = ($, requestConfig) => {
requestConfig.headers['Content-Type'] = 'application/json';
if (!requestConfig.additionalProperties?.skipAddingAuthHeader && $.auth.data?.accessToken) {
requestConfig.headers.Authorization = `Bearer ${$.auth.data.accessToken}`;
}
return requestConfig;
};
export default addAuthHeader;

View File

@@ -0,0 +1,6 @@
const getCurrentAccount = async ($) => {
const response = await $.http.post('/2/users/get_current_account', null);
return response.data;
};
export default getCurrentAccount;

View File

@@ -1,8 +0,0 @@
import { IGlobalVariable, IJSONObject } from '@automatisch/types';
const getCurrentAccount = async ($: IGlobalVariable): Promise<IJSONObject> => {
const response = await $.http.post('/2/users/get_current_account', null);
return response.data;
};
export default getCurrentAccount;

View File

@@ -1,7 +1,7 @@
import defineApp from '../../helpers/define-app';
import addAuthHeader from './common/add-auth-header';
import auth from './auth';
import actions from './actions';
import defineApp from '../../helpers/define-app.js';
import addAuthHeader from './common/add-auth-header.js';
import auth from './auth/index.js';
import actions from './actions/index.js';
export default defineApp({
name: 'Dropbox',