Skip to content

Commit

Permalink
fix: improve types
Browse files Browse the repository at this point in the history
This change resolves some incorrect types, which don't trigger problems unless TypeScript is in a stricter mode.

Refs #398
  • Loading branch information
thewilkybarkid committed Sep 7, 2021
1 parent a637a6d commit d542241
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 30 deletions.
10 changes: 2 additions & 8 deletions src/backend/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { Command } from 'commander';
import dotenv from 'dotenv';
import { from } from 'env-var';
import { Joi } from 'koa-joi-router';
import { isString } from 'lodash';
import { ORCID as orcidUtils } from 'orcid-utils';
import log4js from 'koa-log4';
import { isString } from './utils/strings';
import { ServerError } from './utils/http-errors';

// Configure logging
Expand All @@ -29,9 +29,6 @@ const env = from(process.env, {}, (varname, str) =>
);

function getEnv(postfix: string, fallback?: string) {
if (!isString(postfix)) {
throw new ServerError('Must specify an environment variable to fetch.');
}
if (fallback) {
return env
.get(
Expand All @@ -50,7 +47,7 @@ const defaultEnv = isString(process.env.NODE_ENV)
: 'development';
const defaultPort = isString(process.env.PORT) ? process.env.PORT : '3000';

const defaults = {
const defaults: Partial<Record<string, string>> = {
admin_user: 'admin',
db_name:
defaultEnv !== 'production'
Expand All @@ -71,9 +68,6 @@ const defaults = {
};

function getEnvOrDefault(postfix: string) {
if (!isString(postfix)) {
throw new ServerError('Must specify an environment variable to fetch.');
}
const defaultValue = defaults[postfix.toLowerCase()];

if (defaultValue) {
Expand Down
2 changes: 1 addition & 1 deletion src/backend/controllers/user.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { v4 as uuidv4 } from 'uuid';
import router from 'koa-joi-router';
import { isString } from 'lodash';
import { Key } from '../models/entities/index.ts';
import { isString } from '../utils/strings';
import { getLogger } from '../log.js';

const log = getLogger('backend:controllers:user');
Expand Down
2 changes: 1 addition & 1 deletion src/backend/middleware/auth.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Roles from 'koa-roles';
import { isString } from 'lodash';
import { ORCID as orcidUtils } from 'orcid-utils';
import config from '../config.ts';
import { isString } from '../utils/strings';
import { getLogger } from '../log.js';

const log = getLogger('backend:middleware:auth');
Expand Down
10 changes: 6 additions & 4 deletions src/backend/scripts/dbMigrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { SearchPostgresql } from '../db/migrations/searchIndexes';

async function main() {
try {
const migrationsList = Object.keys(migrations).map(migrationName => ({
name: migrationName,
class: migrations[migrationName],
}));
const migrationsList = Object.entries(migrations).map(
([migrationName, migrationClass]) => ({
name: migrationName,
class: migrationClass,
}),
);

migrationsList.splice(1, 0, {
name: 'SearchPostgresql',
Expand Down
6 changes: 5 additions & 1 deletion src/backend/utils/getFields.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import 'reflect-metadata';
import * as entities from '../models/entities';

export function getFields(type: string, includes?: string[], recurse = 2): any {
export function getFields(
type: keyof typeof entities,
includes?: string[],
recurse = 2,
): unknown {
return Object.values(entities[type].prototype.__meta.properties).reduce(
(props: any[], prop: any) => {
if (!(prop.hidden === true)) {
Expand Down
24 changes: 12 additions & 12 deletions src/backend/utils/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,18 +214,6 @@ export async function resolvePreprint(
const baseUrlArxivHtml = 'https://arxiv.org/abs/';
const baseUrlDoi = 'https://doi.org/';

// checks if the publication is DOI or arXiv
let url: string, type: string;
if (isDoi) {
log.debug('Resolving preprint with a DOI');
url = `${baseUrlDoi}${handle}`;
type = 'doi';
} else if (isArxiv) {
log.debug('Resolving preprint with an arXivId');
url = `${baseUrlArxivHtml}${handle}`;
type = 'arxiv';
}

// as a last resort check Google Scholar
resolvers.push(
searchGoogleScholar(handle).catch(err =>
Expand All @@ -244,6 +232,18 @@ export async function resolvePreprint(

// fetch data based on publication type (DOI / arXiv)
if (isDoi || isArxiv) {
// checks if the publication is DOI or arXiv
let url: string, type: string;
if (isDoi) {
log.debug('Resolving preprint with a DOI');
url = `${baseUrlDoi}${handle}`;
type = 'doi';
} else {
log.debug('Resolving preprint with an arXivId');
url = `${baseUrlArxivHtml}${handle}`;
type = 'arxiv';
}

resolvers.push(
scrapeUrl(url, handle, type).catch(err =>
log.warn('No metadata found via scrape: ', err),
Expand Down
3 changes: 0 additions & 3 deletions src/backend/utils/strings.js

This file was deleted.

0 comments on commit d542241

Please sign in to comment.