Skip to content

Commit

Permalink
Typings: more interfaces to types
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffwilcox committed Dec 17, 2023
1 parent bc08b43 commit ef55d36
Show file tree
Hide file tree
Showing 25 changed files with 119 additions and 67 deletions.
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,21 @@
"EXIT_IMMEDIATELY": "1"
}
},
{
"type": "node",
"request": "launch",
"name": "66: Order 66 Terminations",
"program": "${workspaceRoot}/dist/microsoft/jobs/terminations.js",
"args": ["all"],
"cwd": "${workspaceRoot}/dist",
"preLaunchTask": "tsbuild",
"sourceMaps": true,
"console": "integratedTerminal",
"env": {
"NODE_ENV": "development",
"DEBUG": "startup,appinsights"
}
},
{
"type": "node",
"request": "launch",
Expand Down
2 changes: 1 addition & 1 deletion api/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const router: Router = Router();

import { getProviders } from '../transitional';
import { setIdentity } from '../middleware/business/authentication';
import { AddLinkToRequest } from '../middleware/links';
import { AddLinkToRequest } from '../middleware/business/links';
import { jsonError } from '../middleware';
import { apiContextMiddleware } from '../middleware/business/setContext';
import { ILocalExtensionKeyProvider } from '../entities/localExtensionKey';
Expand Down
14 changes: 7 additions & 7 deletions interfaces/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
//

import { Application, Response, NextFunction } from 'express';
import { IProviders } from './providers';

import type { RuntimeConfiguration } from './config';
import { ReposAppRequest } from './web';
import type { IProviders } from './providers';
import type { RuntimeConfiguration, SiteConfiguration } from './config';
import type { ReposAppRequest } from './web';

export interface IApplicationProfile {
export type ApplicationProfile = {
applicationName: string;
customErrorHandlerRender?: (
errorView: unknown,
Expand All @@ -26,15 +26,15 @@ export interface IApplicationProfile {
startup?: (providers: IProviders) => Promise<void>;
sessions: boolean;
webServer: boolean;
}
};

export interface IReposApplication extends Application {
// Standard Express
set(settingName: string, settingValue: any);

// Local things
providers: IProviders;
config: any;
config: SiteConfiguration;
isBackgroundJob: boolean;
enableAllGitHubApps: boolean;
runtimeConfiguration: RuntimeConfiguration;
Expand All @@ -45,7 +45,7 @@ export interface IReposApplication extends Application {

initializeApplication: (
executionEnvironment: ExecutionEnvironment,
config: any,
config: SiteConfiguration,
configurationError: Error
) => Promise<IReposApplication>;

Expand Down
4 changes: 2 additions & 2 deletions interfaces/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import redis, { RedisClientType } from 'redis';
import { Pool as PostgresPool } from 'pg';

import {
IApplicationProfile,
ApplicationProfile,
ICorporationAdministrationSection,
IReposApplication,
SiteConfiguration,
Expand Down Expand Up @@ -48,7 +48,7 @@ type ProviderGenerator = (value: string) => IEntityMetadataProvider;

export interface IProviders {
app: IReposApplication;
applicationProfile: IApplicationProfile;
applicationProfile: ApplicationProfile;
authorizationCodeClient?: AuthorizationCode;
corporateAdministrationProfile?: ICorporationAdministrationSection;
corporateViews?: any;
Expand Down
2 changes: 1 addition & 1 deletion job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ function initializeJob(
);
}

const job = {
export const job = {
runBackgroundJob: async (
script: (providers: IProviders, jobParameters?: IReposJob) => Promise<IReposJobResult | void>,
options?: IReposJobOptions
Expand Down
6 changes: 3 additions & 3 deletions middleware/alternateApps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ const debug = Debug.debug('startup');

import path from 'path';

import { IApplicationProfile, IReposApplication } from '../interfaces';
import type { ApplicationProfile, IReposApplication, SiteConfiguration } from '../interfaces';

export default async function initializeAlternateApps(
config,
config: SiteConfiguration,
app: IReposApplication,
appName: string
): Promise<IApplicationProfile> {
): Promise<ApplicationProfile> {
const appPath = path.resolve(path.join(__dirname, '..', appName, '/'));
debug(`Alternate app requested: name=${appName}, path=${appPath}`);
try {
Expand Down
3 changes: 1 addition & 2 deletions middleware/business/administration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
//

import { NextFunction, Response, Router } from 'express';
const router: Router = Router();

import { ReposAppRequest } from '../../interfaces';

import { wrapError } from '../../utils';

function denyRoute(next) {
function denyRoute(next: NextFunction) {
next(
wrapError(
null,
Expand Down
11 changes: 6 additions & 5 deletions middleware/business/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

import { Response, NextFunction } from 'express';

import { ReposAppRequest, IAppSession } from '../../interfaces';

import Debug from 'debug';
Expand All @@ -17,13 +19,12 @@ import {
} from '../../business/user';
import { storeOriginalUrlAsReferrer } from '../../utils';
import getCompanySpecificDeployment from '../companySpecificDeployment';
import { Response, NextFunction } from 'express';

export async function requireAuthenticatedUserOrSignInExcluding(
exclusionPaths: string[],
req: ReposAppRequest,
res,
next
res: Response,
next: NextFunction
) {
const url = req.url;
for (let i = 0; i < exclusionPaths.length; i++) {
Expand Down Expand Up @@ -64,7 +65,7 @@ export async function requireAccessTokenClient(req: ReposAppRequest, res: Respon
return next();
}

function signoutThenSignIn(req: ReposAppRequest, res) {
function signoutThenSignIn(req: ReposAppRequest, res: Response) {
const { insights } = getProviders(req);
req.logout({ keepSessionInfo: false }, (err) => {
if (err) {
Expand All @@ -74,7 +75,7 @@ function signoutThenSignIn(req: ReposAppRequest, res) {
});
}

function redirectToSignIn(req, res) {
function redirectToSignIn(req: ReposAppRequest, res: Response) {
const config = getProviders(req).config;
storeOriginalUrlAsReferrer(
req,
Expand Down
6 changes: 3 additions & 3 deletions middleware/business/corporateAdministrators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface IReposAppRequestWithSystemAdministration extends ReposAppReques
isSystemAdministrator: boolean;
}

function denyRoute(next, isApi: boolean) {
function denyRoute(next: NextFunction, isApi: boolean) {
if (isApi) {
return next(jsonError('This API is unavailable for you', 403));
}
Expand Down Expand Up @@ -48,8 +48,8 @@ export async function AuthorizeOnlyCorporateAdministrators(

export async function checkIsCorporateAdministrator(
req: IReposAppRequestWithSystemAdministration,
res,
next
res: Response,
next: NextFunction
) {
await getIsCorporateAdministrator(req);
return next();
Expand Down
File renamed without changes.
8 changes: 6 additions & 2 deletions middleware/correlationId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

import { NextFunction, Response } from 'express';
import { NextFunction, Request, Response } from 'express';
import { randomUUID } from 'crypto';

export type WithCorrelationId<T> = T & {
correlationId?: string;
};

// Generate a correlation ID
export default function (req, res: Response, next: NextFunction) {
export default function (req: WithCorrelationId<Request>, res: Response, next: NextFunction) {
req.correlationId = randomUUID();
return next();
}
12 changes: 6 additions & 6 deletions middleware/error-routes.ts → middleware/errorRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

import { NextFunction, Response } from 'express';
import { NextFunction, Request, Response } from 'express';

import { IReposApplication, IReposError } from '../interfaces';
import RouteErrorHandler from './errorHandler';
import routeErrorHandler from './errorHandler';

export default async function configureErrorRoutes(app: IReposApplication, initializationError: Error) {
if (!app) {
Expand All @@ -19,7 +19,7 @@ export default async function configureErrorRoutes(app: IReposApplication, initi
// for any request. Should evaluate whether to hide for
// production scenarios or if there is a risk of the
// error message leaking sensitive data.
app.use((req, res: Response, next: NextFunction) => {
app.use((req: Request, res: Response, next: NextFunction) => {
const error: IReposError = new Error('Application initialization error', {
cause: initializationError,
});
Expand All @@ -28,12 +28,12 @@ export default async function configureErrorRoutes(app: IReposApplication, initi
});
}

app.use(function (req, res: Response, next: NextFunction) {
app.use(function (req: Request, res: Response, next: NextFunction) {
const err: IReposError = new Error('Not Found');
err.status = 404;
err.skipLog = true;
next(err);
return next(err);
});

app.use(RouteErrorHandler);
app.use(routeErrorHandler);
}
18 changes: 11 additions & 7 deletions middleware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import bodyParser from 'body-parser';
import compression from 'compression';
import path from 'path';
import { Express } from 'express';
import passport from 'passport';

import Debug from 'debug';
const debug = Debug.debug('startup');

export * from './react';
export * from './links';
export * from './business/links';
export * from './business';
export * from './jsonError';

Expand All @@ -20,7 +22,7 @@ import { StaticClientApp } from './staticClientApp';
import { StaticReactClientApp } from './staticClientApp2';
import { StaticSiteFavIcon, StaticSiteAssets } from './staticSiteAssets';
import connectSession from './session';
import passportConfig from './passport-config';
import passportConfig from './passportConfig';
import onboard from './onboarding';
import viewServices from '../lib/pugViewServices';

Expand All @@ -35,12 +37,13 @@ import routePassport from './passport-routes';

import routeApi from '../api';

import { IProviders, IReposApplication, SiteConfiguration } from '../interfaces';
import type { IProviders, IReposApplication, SiteConfiguration } from '../interfaces';
import { codespacesDevAssistant } from './codespaces';
import { ExpressWithStatic } from './types';

export default async function initMiddleware(
app: IReposApplication,
express,
express: Express,
providers: IProviders,
config: SiteConfiguration,
dirname: string,
Expand Down Expand Up @@ -78,14 +81,15 @@ export default async function initMiddleware(
if (applicationProfile.serveStaticAssets) {
StaticSiteAssets(app, express);
}
const expressWithStatic = express as ExpressWithStatic;
if (hasStaticReactClientApp()) {
StaticReactClientApp(app, express, config);
StaticReactClientApp(app, expressWithStatic, config);
}
if (applicationProfile.serveClientAssets) {
StaticClientApp(app, express);
StaticClientApp(app, expressWithStatic);
}
providers.campaign = campaign(app);
let passport;
let passport: passport.PassportStatic;
if (!initializationError) {
if (config.containers && config.containers.deployment) {
app.enable('trust proxy');
Expand Down
20 changes: 11 additions & 9 deletions middleware/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

import { NextFunction, Response } from 'express';
import { Express, NextFunction, Response } from 'express';
import path from 'path';

import CosmosSessionStore from '../lib/cosmosSession';
Expand All @@ -19,7 +19,7 @@ import { createAndInitializeRepositoryMetadataProviderInstance } from '../entiti
import createAndInitializeOrganizationAnnotationProviderInstance from '../entities/organizationAnnotation';
import { createMailAddressProviderInstance, IMailAddressProvider } from '../lib/mailAddressProvider';

import ErrorRoutes from './error-routes';
import ErrorRoutes from './errorRoutes';

import { createClient, RedisClientType } from 'redis';
import { Pool as PostgresPool } from 'pg';
Expand Down Expand Up @@ -76,7 +76,7 @@ import middlewareIndex from '.';
import type { ICacheHelper } from '../lib/caching';
import type {
ExecutionEnvironment,
IApplicationProfile,
ApplicationProfile,
IProviders,
IReposApplication,
SiteConfiguration,
Expand All @@ -85,7 +85,7 @@ import initializeRepositoryProvider from '../entities/repository';
import { tryGetImmutableStorageProvider } from '../lib/immutable';
import { GitHubAppPurposes } from '../lib/github/appPurposes';

const DefaultApplicationProfile: IApplicationProfile = {
const DefaultApplicationProfile: ApplicationProfile = {
applicationName: 'Open Source Management Portal',
serveStaticAssets: true,
serveClientAssets: true,
Expand Down Expand Up @@ -121,7 +121,6 @@ async function initializeAsync(
} else if (config.github.cache.provider === 'redis') {
const redisClient = await connectRedis(config, config.redis, 'cache');
const redisHelper = new RedisHelper({ redisClient, prefix: config.redis.prefix });
// providers.redisClient = redisClient;
providers.cacheProvider = redisHelper;
} else {
throw new Error('No cache provider available');
Expand Down Expand Up @@ -318,7 +317,7 @@ async function initializeAsync(
}
}

function configureGitHubLibrary(cacheProvider: ICacheHelper, config): RestLibrary {
function configureGitHubLibrary(cacheProvider: ICacheHelper, config: SiteConfiguration): RestLibrary {
const libraryContext = new RestLibrary({
config,
cacheProvider,
Expand All @@ -330,7 +329,7 @@ function configureGitHubLibrary(cacheProvider: ICacheHelper, config): RestLibrar
export default async function initialize(
executionEnvironment: ExecutionEnvironment,
app: IReposApplication,
express,
express: Express,
rootdir: string,
config: SiteConfiguration,
exception: Error
Expand Down Expand Up @@ -541,7 +540,7 @@ export default async function initialize(
return executionEnvironment;
}

function createGraphProvider(providers: IProviders, config: any): Promise<IGraphProvider> {
function createGraphProvider(providers: IProviders, config: SiteConfiguration): Promise<IGraphProvider> {
return new Promise((resolve, reject) => {
// The graph provider is optional. A graph provider can connect to a
// corporate directory to validate or lookup employees and other
Expand Down Expand Up @@ -642,7 +641,10 @@ async function connectRedis(
return redisClient;
}

async function createMailAddressProvider(config: any, providers: IProviders): Promise<IMailAddressProvider> {
async function createMailAddressProvider(
config: SiteConfiguration,
providers: IProviders
): Promise<IMailAddressProvider> {
const options = {
config: config,
providers: providers,
Expand Down
Loading

0 comments on commit ef55d36

Please sign in to comment.