From 7c37724f8889e43c506108a3c9836efe3eb5bce5 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Fri, 3 Dec 2021 10:34:32 -0600 Subject: [PATCH] types --- app/typings/index.d.ts | 2 + server/commands/collectionImporter.test.ts | 1 - server/commands/collectionImporter.ts | 7 +- server/commands/documentCreator.ts | 3 +- server/commands/documentImporter.test.ts | 1 - server/commands/documentImporter.ts | 1 - server/commands/userCreator.ts | 3 +- server/mailer.tsx | 5 +- server/middlewares/passport.ts | 2 - server/queues/processors/imports.ts | 1 - server/routes/auth/index.test.ts | 2 + server/routes/auth/index.ts | 1 - server/routes/auth/providers/azure.ts | 1 - server/routes/auth/providers/google.ts | 1 - server/routes/auth/providers/oidc.ts | 1 - server/routes/auth/providers/slack.ts | 1 - server/typings/outlinewiki__koa-passport.d.ts | 204 ++++++++++++++++++ 17 files changed, 216 insertions(+), 21 deletions(-) create mode 100644 server/typings/outlinewiki__koa-passport.d.ts diff --git a/app/typings/index.d.ts b/app/typings/index.d.ts index 962401984..00dfa1fa3 100644 --- a/app/typings/index.d.ts +++ b/app/typings/index.d.ts @@ -8,6 +8,8 @@ declare module "string-replace-to-array"; declare module "styled-components-breakpoint"; +declare module "formidable/lib/file"; + declare module "socket.io-redis" { import { Redis } from "ioredis"; diff --git a/server/commands/collectionImporter.test.ts b/server/commands/collectionImporter.test.ts index e81a2931d..f685cb3c9 100644 --- a/server/commands/collectionImporter.test.ts +++ b/server/commands/collectionImporter.test.ts @@ -1,5 +1,4 @@ import path from "path"; -// @ts-expect-error ts-migrate(7016) FIXME: Could not find a declaration file for module 'form... Remove this comment to see the full error message import File from "formidable/lib/file"; import { Attachment, Document, Collection } from "@server/models"; import { buildUser } from "@server/test/factories"; diff --git a/server/commands/collectionImporter.ts b/server/commands/collectionImporter.ts index bfd21bf4e..d24e8cde2 100644 --- a/server/commands/collectionImporter.ts +++ b/server/commands/collectionImporter.ts @@ -1,7 +1,6 @@ import fs from "fs"; import os from "os"; import path from "path"; -// @ts-expect-error ts-migrate(7016) FIXME: Could not find a declaration file for module 'form... Remove this comment to see the full error message import File from "formidable/lib/file"; import invariant from "invariant"; import { values, keys } from "lodash"; @@ -14,13 +13,17 @@ import attachmentCreator from "./attachmentCreator"; import documentCreator from "./documentCreator"; import documentImporter from "./documentImporter"; +type FileWithPath = File & { + path: string; +}; + export default async function collectionImporter({ file, type, user, ip, }: { - file: File; + file: FileWithPath; // @ts-expect-error ts-migrate(2749) FIXME: 'User' refers to a value, but is being used as a t... Remove this comment to see the full error message user: User; type: "outline"; diff --git a/server/commands/documentCreator.ts b/server/commands/documentCreator.ts index 97091f6bc..5ad666549 100644 --- a/server/commands/documentCreator.ts +++ b/server/commands/documentCreator.ts @@ -32,8 +32,7 @@ export default async function documentCreator({ editorVersion?: string; source?: "import"; ip: string; - // @ts-expect-error ts-migrate(1064) FIXME: The return type of an async function or method mus... Remove this comment to see the full error message -}): Document { +}): Promise { // @ts-expect-error ts-migrate(2339) FIXME: Property 'id' does not exist on type 'Document'. const templateId = templateDocument ? templateDocument.id : undefined; const document = await Document.create({ diff --git a/server/commands/documentImporter.test.ts b/server/commands/documentImporter.test.ts index fee65c762..253e048c8 100644 --- a/server/commands/documentImporter.test.ts +++ b/server/commands/documentImporter.test.ts @@ -1,5 +1,4 @@ import path from "path"; -// @ts-expect-error ts-migrate(7016) FIXME: Could not find a declaration file for module 'form... Remove this comment to see the full error message import File from "formidable/lib/file"; import { Attachment } from "@server/models"; import { buildUser } from "@server/test/factories"; diff --git a/server/commands/documentImporter.ts b/server/commands/documentImporter.ts index 8170ccdce..6a64620e6 100644 --- a/server/commands/documentImporter.ts +++ b/server/commands/documentImporter.ts @@ -1,6 +1,5 @@ import fs from "fs"; import path from "path"; -// @ts-expect-error ts-migrate(7016) FIXME: Could not find a declaration file for module 'form... Remove this comment to see the full error message import File from "formidable/lib/file"; // @ts-expect-error ts-migrate(7016) FIXME: Could not find a declaration file for module 'jopl... Remove this comment to see the full error message import { strikethrough, tables } from "joplin-turndown-plugin-gfm"; diff --git a/server/commands/userCreator.ts b/server/commands/userCreator.ts index 348e701db..116355bca 100644 --- a/server/commands/userCreator.ts +++ b/server/commands/userCreator.ts @@ -1,8 +1,7 @@ -import Sequelize from "sequelize"; +import { Op } from "sequelize"; import { Event, Team, User, UserAuthentication } from "@server/models"; import { sequelize } from "../sequelize"; -const Op = Sequelize.Op; type UserCreatorResult = { // @ts-expect-error ts-migrate(2749) FIXME: 'User' refers to a value, but is being used as a t... Remove this comment to see the full error message user: User; diff --git a/server/mailer.tsx b/server/mailer.tsx index 6f0e4f6dd..2b95db312 100644 --- a/server/mailer.tsx +++ b/server/mailer.tsx @@ -127,10 +127,7 @@ export class Mailer { } } - sendMail = async ( - data: EmailSendOptions - // @ts-expect-error ts-migrate(1064) FIXME: The return type of an async function or method mus... Remove this comment to see the full error message - ): Promise | null | undefined => { + sendMail = async (data: EmailSendOptions): Promise => { const { transporter } = this; if (transporter) { diff --git a/server/middlewares/passport.ts b/server/middlewares/passport.ts index b59b19043..dd138cd16 100644 --- a/server/middlewares/passport.ts +++ b/server/middlewares/passport.ts @@ -1,4 +1,3 @@ -// @ts-expect-error ts-migrate(7016) FIXME: Could not find a declaration file for module '@out... Remove this comment to see the full error message import passport from "@outlinewiki/koa-passport"; import { Context } from "koa"; import Logger from "@server/logging/logger"; @@ -12,7 +11,6 @@ export default function createMiddleware(providerName: string) { { session: false, }, - // @ts-expect-error ts-migrate(7006) FIXME: Parameter 'err' implicitly has an 'any' type. async (err, user, result: AccountProvisionerResult) => { if (err) { Logger.error("Error during authentication", err); diff --git a/server/queues/processors/imports.ts b/server/queues/processors/imports.ts index d75f72212..96d69138c 100644 --- a/server/queues/processors/imports.ts +++ b/server/queues/processors/imports.ts @@ -1,6 +1,5 @@ import fs from "fs"; import os from "os"; -// @ts-expect-error ts-migrate(7016) FIXME: Could not find a declaration file for module 'form... Remove this comment to see the full error message import File from "formidable/lib/file"; import collectionImporter from "@server/commands/collectionImporter"; import { Attachment, User } from "@server/models"; diff --git a/server/routes/auth/index.test.ts b/server/routes/auth/index.test.ts index 9beaf1851..f779b15c3 100644 --- a/server/routes/auth/index.test.ts +++ b/server/routes/auth/index.test.ts @@ -6,8 +6,10 @@ import { flushdb } from "@server/test/support"; const app = webService(); const server = new TestServer(app.callback()); + beforeEach(() => flushdb()); afterAll(() => server.close()); + describe("auth/redirect", () => { it("should redirect to home", async () => { const user = await buildUser(); diff --git a/server/routes/auth/index.ts b/server/routes/auth/index.ts index 5fffbf416..e0aad9501 100644 --- a/server/routes/auth/index.ts +++ b/server/routes/auth/index.ts @@ -1,4 +1,3 @@ -// @ts-expect-error ts-migrate(7016) FIXME: Could not find a declaration file for module '@out... Remove this comment to see the full error message import passport from "@outlinewiki/koa-passport"; import { addMonths } from "date-fns"; import Koa from "koa"; diff --git a/server/routes/auth/providers/azure.ts b/server/routes/auth/providers/azure.ts index 8b42dd996..91dbb2456 100644 --- a/server/routes/auth/providers/azure.ts +++ b/server/routes/auth/providers/azure.ts @@ -1,4 +1,3 @@ -// @ts-expect-error ts-migrate(7016) FIXME: Could not find a declaration file for module '@out... Remove this comment to see the full error message import passport from "@outlinewiki/koa-passport"; // @ts-expect-error ts-migrate(7016) FIXME: Could not find a declaration file for module '@out... Remove this comment to see the full error message import { Strategy as AzureStrategy } from "@outlinewiki/passport-azure-ad-oauth2"; diff --git a/server/routes/auth/providers/google.ts b/server/routes/auth/providers/google.ts index 320e514ca..e5d34cb5d 100644 --- a/server/routes/auth/providers/google.ts +++ b/server/routes/auth/providers/google.ts @@ -1,4 +1,3 @@ -// @ts-expect-error ts-migrate(7016) FIXME: Could not find a declaration file for module '@out... Remove this comment to see the full error message import passport from "@outlinewiki/koa-passport"; import Router from "koa-router"; import { capitalize } from "lodash"; diff --git a/server/routes/auth/providers/oidc.ts b/server/routes/auth/providers/oidc.ts index d0c6cca46..7a6f9e3f1 100644 --- a/server/routes/auth/providers/oidc.ts +++ b/server/routes/auth/providers/oidc.ts @@ -1,4 +1,3 @@ -// @ts-expect-error ts-migrate(7016) FIXME: Could not find a declaration file for module '@out... Remove this comment to see the full error message import passport from "@outlinewiki/koa-passport"; import Router from "koa-router"; import { get } from "lodash"; diff --git a/server/routes/auth/providers/slack.ts b/server/routes/auth/providers/slack.ts index 53b2be93e..5bf3848fe 100644 --- a/server/routes/auth/providers/slack.ts +++ b/server/routes/auth/providers/slack.ts @@ -1,4 +1,3 @@ -// @ts-expect-error ts-migrate(7016) FIXME: Could not find a declaration file for module '@out... Remove this comment to see the full error message import passport from "@outlinewiki/koa-passport"; import Router from "koa-router"; // @ts-expect-error ts-migrate(7016) FIXME: Could not find a declaration file for module 'pass... Remove this comment to see the full error message diff --git a/server/typings/outlinewiki__koa-passport.d.ts b/server/typings/outlinewiki__koa-passport.d.ts new file mode 100644 index 000000000..80eb405ce --- /dev/null +++ b/server/typings/outlinewiki__koa-passport.d.ts @@ -0,0 +1,204 @@ +declare module "@outlinewiki/koa-passport" { + // Type definitions for Passport 1.0 + // Project: http://passportjs.org + // Definitions by: Horiuchi_H + // Eric Naeseth + // Igor Belagorudsky + // Tomek Łaziuk + // Daniel Perez Alvarez + // Kevin Stiehl + // Oleg Vaskevich + // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + // TypeScript Version: 2.3 + + import { IncomingMessage } from "http"; + // eslint-disable-next-line + import Express from "express"; + import { Request } from "koa"; + import { IMiddleware } from "koa-router"; + + namespace passport { + interface AuthenticateOptions { + authInfo?: boolean | undefined; + assignProperty?: string | undefined; + failureFlash?: string | boolean | undefined; + failureMessage?: boolean | string | undefined; + failureRedirect?: string | undefined; + failWithError?: boolean | undefined; + session?: boolean | undefined; + scope?: string | string[] | undefined; + successFlash?: string | boolean | undefined; + successMessage?: boolean | string | undefined; + successRedirect?: string | undefined; + successReturnToOrRedirect?: string | undefined; + state?: string | undefined; + pauseStream?: boolean | undefined; + userProperty?: string | undefined; + passReqToCallback?: boolean | undefined; + prompt?: string | undefined; + accessType?: string | undefined; + } + + interface Authenticator< + InitializeRet = IMiddleware, + AuthenticateRet = any, + AuthorizeRet = AuthenticateRet, + AuthorizeOptions = AuthenticateOptions + > { + use(strategy: Strategy): this; + use(name: string, strategy: Strategy): this; + unuse(name: string): this; + framework(fw: Framework): Authenticator; + initialize(options?: { userProperty: string }): InitializeRet; + session(options?: { pauseStream: boolean }): AuthenticateRet; + + authenticate( + strategy: string | string[] | Strategy, + callback?: (...args: any[]) => any + ): AuthenticateRet; + authenticate( + strategy: string | string[] | Strategy, + options: AuthenticateOptions, + callback?: (...args: any[]) => any + ): AuthenticateRet; + authorize( + strategy: string | string[], + callback?: (...args: any[]) => any + ): AuthorizeRet; + authorize( + strategy: string | string[], + options: AuthorizeOptions, + callback?: (...args: any[]) => any + ): AuthorizeRet; + serializeUser( + fn: (user: any, done: (err: any, id?: TID) => void) => void + ): void; + serializeUser( + fn: (req: TR, user: any, done: (err: any, id?: TID) => void) => void + ): void; + deserializeUser( + fn: (id: TID, done: (err: any, user?: any) => void) => void + ): void; + deserializeUser( + fn: (req: TR, id: TID, done: (err: any, user?: any) => void) => void + ): void; + transformAuthInfo( + fn: (info: any, done: (err: any, info: any) => void) => void + ): void; + } + + interface PassportStatic extends Authenticator { + Authenticator: { new (): Authenticator }; + Passport: PassportStatic["Authenticator"]; + Strategy: { new (): Strategy & StrategyCreatedStatic }; + } + + interface Strategy { + name?: string | undefined; + authenticate( + this: StrategyCreated, + req: Request | Express.Request, + options?: any + ): any; + } + + interface StrategyCreatedStatic { + /** + * Authenticate `user`, with optional `info`. + * + * Strategies should call this function to successfully authenticate a + * user. `user` should be an object supplied by the application after it + * has been given an opportunity to verify credentials. `info` is an + * optional argument containing additional user information. This is + * useful for third-party authentication strategies to pass profile + * details. + */ + success(user: any, info?: object): void; + /** + * Fail authentication, with optional `challenge` and `status`, defaulting + * to 401. + * + * Strategies should call this function to fail an authentication attempt. + */ + fail(challenge?: string | number, status?: number): void; + /** + * Redirect to `url` with optional `status`, defaulting to 302. + * + * Strategies should call this function to redirect the user (via their + * user agent) to a third-party website for authentication. + */ + redirect(url: string, status?: number): void; + /** + * Pass without making a success or fail decision. + * + * Under most circumstances, Strategies should not need to call this + * function. It exists primarily to allow previous authentication state + * to be restored, for example from an HTTP session. + */ + pass(): void; + /** + * Internal error while performing authentication. + * + * Strategies should call this function when an internal error occurs + * during the process of performing authentication; for example, if the + * user directory is not available. + */ + error(err: any): void; + } + + type StrategyCreated = { + [P in keyof O]: O[P]; + }; + + interface Profile { + provider: string; + id: string; + displayName: string; + username?: string | undefined; + name?: + | { + familyName: string; + givenName: string; + middleName?: string | undefined; + } + | undefined; + emails?: + | Array<{ + value: string; + type?: string | undefined; + }> + | undefined; + photos?: + | Array<{ + value: string; + }> + | undefined; + } + + interface Framework< + InitializeRet = any, + AuthenticateRet = any, + AuthorizeRet = AuthenticateRet + > { + initialize( + passport: Authenticator, + options?: any + ): (...args: any[]) => InitializeRet; + authenticate( + passport: Authenticator, + name: string, + options?: any, + callback?: (...args: any[]) => any + ): (...args: any[]) => AuthenticateRet; + authorize?( + passport: Authenticator, + name: string, + options?: any, + callback?: (...args: any[]) => any + ): (...args: any[]) => AuthorizeRet; + } + } + + const passport: passport.PassportStatic; + export = passport; +}