Skip to content

Commit

Permalink
Syntax fixes (directus#5367)
Browse files Browse the repository at this point in the history
* Declare return types on functions

And a very few other type related minor fixes

* Minor syntax fixes

* Remove unnecessary escape chars in regexes
* Remove unnecessary awaits
* Replace deprecated req.connection with req.socket
* Replace deprecated upload with uploadOne
* Remove unnecessary eslint-disable-next-line comments
* Comment empty functions / catch or finally clauses
* Fix irregular whitespaces
* Add missing returns (null)
* Remove unreachable code
* A few logical fixes
* Remove / Handle non-null assertions which are certainly unnecessary (e.g. in
tests)
  • Loading branch information
paescuj authored Apr 29, 2021
1 parent 40eba79 commit acd41eb
Show file tree
Hide file tree
Showing 231 changed files with 646 additions and 527 deletions.
2 changes: 1 addition & 1 deletion api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import { emitAsyncSafe } from './emitter';

import fse from 'fs-extra';

export default async function createApp() {
export default async function createApp(): Promise<express.Application> {
validateEnv(['KEY', 'SECRET']);

await validateDBConnection();
Expand Down
2 changes: 1 addition & 1 deletion api/src/cli/commands/bootstrap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import runMigrations from '../../../database/migrations/run';
import { getSchema } from '../../../utils/get-schema';
import { nanoid } from 'nanoid';

export default async function bootstrap() {
export default async function bootstrap(): Promise<void> {
logger.info('Initializing bootstrap...');

if ((await isDatabaseAvailable()) === false) {
Expand Down
2 changes: 1 addition & 1 deletion api/src/cli/commands/count/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default async function count(collection: string) {
export default async function count(collection: string): Promise<void> {
const database = require('../../../database/index').default;

if (!collection) {
Expand Down
2 changes: 1 addition & 1 deletion api/src/cli/commands/database/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Knex } from 'knex';
import installSeeds from '../../../database/seeds/run';
import runMigrations from '../../../database/migrations/run';

export default async function start() {
export default async function start(): Promise<void> {
const database = require('../../../database/index').default as Knex;

try {
Expand Down
2 changes: 1 addition & 1 deletion api/src/cli/commands/database/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import run from '../../../database/migrations/run';

export default async function migrate(direction: 'latest' | 'up' | 'down') {
export default async function migrate(direction: 'latest' | 'up' | 'down'): Promise<void> {
const database = require('../../../database').default;

try {
Expand Down
4 changes: 2 additions & 2 deletions api/src/cli/commands/init/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import runMigrations from '../../../database/migrations/run';
import createDBConnection, { Credentials } from '../../utils/create-db-connection';
import { Knex } from 'knex';

export default async function init(options: Record<string, any>) {
export default async function init(): Promise<void> {
const rootPath = process.cwd();

let { client } = await inquirer.prompt([
Expand All @@ -39,7 +39,7 @@ export default async function init(options: Record<string, any>) {

async function trySeed(): Promise<{ credentials: Credentials; db: Knex }> {
const credentials: Credentials = await inquirer.prompt(
(databaseQuestions[dbClient] as any[]).map((question: Function) =>
(databaseQuestions[dbClient] as any[]).map((question: ({ client, filepath }: any) => any) =>
question({ client: dbClient, filepath: rootPath })
)
);
Expand Down
14 changes: 7 additions & 7 deletions api/src/cli/commands/init/questions.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import path from 'path';

const filename = ({ filepath }: { filepath: string }) => ({
const filename = ({ filepath }: { filepath: string }): Record<string, string> => ({
type: 'input',
name: 'filename',
message: 'Database File Path:',
default: path.join(filepath, 'data.db'),
});

const host = () => ({
const host = (): Record<string, string> => ({
type: 'input',
name: 'host',
message: 'Database Host:',
default: '127.0.0.1',
});

const port = ({ client }: { client: string }) => ({
const port = ({ client }: { client: string }): Record<string, any> => ({
type: 'input',
name: 'port',
message: 'Port:',
Expand All @@ -30,27 +30,27 @@ const port = ({ client }: { client: string }) => ({
},
});

const database = () => ({
const database = (): Record<string, string> => ({
type: 'input',
name: 'database',
message: 'Database Name:',
default: 'directus',
});

const user = () => ({
const user = (): Record<string, string> => ({
type: 'input',
name: 'user',
message: 'Database User:',
});

const password = () => ({
const password = (): Record<string, string> => ({
type: 'password',
name: 'password',
message: 'Database Password:',
mask: '*',
});

const ssl = () => ({
const ssl = (): Record<string, string | boolean> => ({
type: 'confirm',
name: 'ssl',
message: 'Enable SSL:',
Expand Down
2 changes: 1 addition & 1 deletion api/src/cli/commands/roles/create.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getSchema } from '../../../utils/get-schema';

export default async function rolesCreate({ name, admin }: any) {
export default async function rolesCreate({ name, admin }: any): Promise<void> {
const { default: database } = require('../../../database/index');
const { RolesService } = require('../../../services/roles');

Expand Down
2 changes: 1 addition & 1 deletion api/src/cli/commands/users/create.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getSchema } from '../../../utils/get-schema';

export default async function usersCreate({ email, password, role }: any) {
export default async function usersCreate({ email, password, role }: any): Promise<void> {
const { default: database, schemaInspector } = require('../../../database/index');
const { UsersService } = require('../../../services/users');

Expand Down
2 changes: 1 addition & 1 deletion api/src/cli/commands/users/passwd.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import argon2 from 'argon2';
import { getSchema } from '../../../utils/get-schema';

export default async function usersPasswd({ email, password }: any) {
export default async function usersPasswd({ email, password }: any): Promise<void> {
const { default: database } = require('../../../database/index');
const { UsersService } = require('../../../services/users');

Expand Down
2 changes: 1 addition & 1 deletion api/src/cli/utils/create-db-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type Credentials = {
export default function createDBConnection(
client: 'sqlite3' | 'mysql' | 'pg' | 'oracledb' | 'mssql',
credentials: Credentials
) {
): Knex<any, unknown[]> {
let connection: Knex.Config['connection'] = {};

if (client === 'sqlite3') {
Expand Down
6 changes: 5 additions & 1 deletion api/src/cli/utils/create-env/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ const defaults = {
},
};

export default async function createEnv(client: keyof typeof drivers, credentials: Credentials, directory: string) {
export default async function createEnv(
client: keyof typeof drivers,
credentials: Credentials,
directory: string
): Promise<void> {
const config: Record<string, any> = {
...defaults,
database: {
Expand Down
4 changes: 3 additions & 1 deletion api/src/controllers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,9 @@ router.get(
try {
const email = getEmailFromProfile(req.params.provider, req.session.grant.response?.profile);

req.session?.destroy(() => {});
req.session?.destroy(() => {
// Do nothing
});

authResponse = await authenticationService.authenticate({
email,
Expand Down
6 changes: 3 additions & 3 deletions api/src/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ database
logger.trace(`[${delta.toFixed(3)}ms] ${queryInfo.sql} [${queryInfo.bindings.join(', ')}]`);
});

export async function hasDatabaseConnection() {
export async function hasDatabaseConnection(): Promise<boolean> {
try {
if (env.DB_CLIENT === 'oracledb') {
await database.raw('select 1 from DUAL');
Expand All @@ -68,7 +68,7 @@ export async function hasDatabaseConnection() {
}
}

export async function validateDBConnection() {
export async function validateDBConnection(): Promise<void> {
try {
await hasDatabaseConnection();
} catch (error) {
Expand All @@ -80,7 +80,7 @@ export async function validateDBConnection() {

export const schemaInspector = SchemaInspector(database);

export async function isInstalled() {
export async function isInstalled(): Promise<boolean> {
// The existence of a directus_collections table alone isn't a "proper" check to see if everything
// is installed correctly of course, but it's safe enough to assume that this collection only
// exists when using the installer CLI.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Knex } from 'knex';

export async function up(knex: Knex) {
export async function up(knex: Knex): Promise<void> {
await knex.schema.alterTable('directus_fields', (table) => {
table.dropForeign(['collection']);
});
Expand All @@ -27,7 +27,7 @@ export async function up(knex: Knex) {
});
}

export async function down(knex: Knex) {
export async function down(knex: Knex): Promise<void> {
await knex.schema.alterTable('directus_fields', (table) => {
table.foreign('collection').references('directus_collections.collection');
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Knex } from 'knex';
import { merge } from 'lodash';

export async function up(knex: Knex) {
export async function up(knex: Knex): Promise<void> {
await knex('directus_relations')
.delete()
.where('many_collection', 'like', 'directus_%')
.andWhere('one_collection', 'like', 'directus_%');
}

export async function down(knex: Knex) {
export async function down(knex: Knex): Promise<void> {
const defaults = {
many_collection: 'directus_users',
many_field: null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Knex } from 'knex';
import { merge } from 'lodash';

export async function up(knex: Knex) {
export async function up(knex: Knex): Promise<void> {
await knex('directus_collections').delete().where('collection', 'like', 'directus_%');
}

export async function down(knex: Knex) {
export async function down(knex: Knex): Promise<void> {
const defaults = {
collection: null,
hidden: false,
Expand Down
4 changes: 2 additions & 2 deletions api/src/database/migrations/20201029C-remove-system-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1639,12 +1639,12 @@ const systemFields = [
return merge({}, defaults, row);
});

export async function up(knex: Knex) {
export async function up(knex: Knex): Promise<void> {
const fieldKeys = uniq(systemFields.map((field: any) => field.field));

await knex('directus_fields').delete().where('collection', 'like', 'directus_%').whereIn('field', fieldKeys);
}

export async function down(knex: Knex) {
export async function down(knex: Knex): Promise<void> {
await knex.insert(systemFields).into('directus_fields');
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ const updates = [
* Postgres behaves erratic on those triggers, not sure if MySQL / Maria plays nice either.
*/

export async function up(knex: Knex) {
export async function up(knex: Knex): Promise<void> {
for (const update of updates) {
await knex.schema.alterTable(update.table, (table) => {
for (const constraint of update.constraints) {
Expand All @@ -125,7 +125,7 @@ export async function up(knex: Knex) {
}
}

export async function down(knex: Knex) {
export async function down(knex: Knex): Promise<void> {
for (const update of updates) {
await knex.schema.alterTable(update.table, (table) => {
for (const constraint of update.constraints) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Knex } from 'knex';

export async function up(knex: Knex) {
export async function up(knex: Knex): Promise<void> {
await knex.schema.alterTable('directus_webhooks', (table) => {
table.text('url').alter();
});
}

export async function down(knex: Knex) {
export async function down(knex: Knex): Promise<void> {
await knex.schema.alterTable('directus_webhooks', (table) => {
table.string('url').alter();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Knex } from 'knex';

export async function up(knex: Knex) {
export async function up(knex: Knex): Promise<void> {
await knex.schema.alterTable('directus_relations', (table) => {
table.string('sort_field');
});
Expand All @@ -26,7 +26,7 @@ export async function up(knex: Knex) {
}
}

export async function down(knex: Knex) {
export async function down(knex: Knex): Promise<void> {
await knex.schema.alterTable('directus_relations', (table) => {
table.dropColumn('sort_field');
});
Expand Down
4 changes: 2 additions & 2 deletions api/src/database/migrations/20210304A-remove-locked-fields.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Knex } from 'knex';

export async function up(knex: Knex) {
export async function up(knex: Knex): Promise<void> {
await knex.schema.alterTable('directus_fields', (table) => {
table.dropColumn('locked');
});
}

export async function down(knex: Knex) {
export async function down(knex: Knex): Promise<void> {
await knex.schema.alterTable('directus_fields', (table) => {
table.boolean('locked').defaultTo(false).notNullable();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Knex } from 'knex';

export async function up(knex: Knex) {
export async function up(knex: Knex): Promise<void> {
await knex.schema.alterTable('directus_webhooks', (table) => {
table.text('collections').alter();
});
}

export async function down(knex: Knex) {
export async function down(knex: Knex): Promise<void> {
await knex.schema.alterTable('directus_webhooks', (table) => {
table.string('collections').alter();
});
Expand Down
4 changes: 2 additions & 2 deletions api/src/database/migrations/20210331A-add-refresh-interval.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Knex } from 'knex';

export async function up(knex: Knex) {
export async function up(knex: Knex): Promise<void> {
await knex.schema.alterTable('directus_presets', (table) => {
table.integer('refresh_interval');
});
}

export async function down(knex: Knex) {
export async function down(knex: Knex): Promise<void> {
await knex.schema.alterTable('directus_presets', (table) => {
table.dropColumn('refresh_interval');
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Knex } from 'knex';

export async function up(knex: Knex) {
export async function up(knex: Knex): Promise<void> {
await knex.schema.alterTable('directus_files', (table) => {
table.integer('filesize').nullable().defaultTo(null).alter();
});
}

export async function down(knex: Knex) {
export async function down(knex: Knex): Promise<void> {
await knex.schema.alterTable('directus_files', (table) => {
table.integer('filesize').notNullable().defaultTo(0).alter();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Knex } from 'knex';

export async function up(knex: Knex) {
export async function up(knex: Knex): Promise<void> {
await knex.schema.alterTable('directus_collections', (table) => {
table.string('accountability').defaultTo('all');
});

await knex('directus_collections').update({ accountability: 'all' });
}

export async function down(knex: Knex) {
export async function down(knex: Knex): Promise<void> {
await knex.schema.alterTable('directus_collections', (table) => {
table.dropColumn('accountability');
});
Expand Down
Loading

0 comments on commit acd41eb

Please sign in to comment.