Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript support #220

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16,065 changes: 11,054 additions & 5,011 deletions package-lock.json

Large diffs are not rendered by default.

15 changes: 12 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
"publish:beta": "npm publish --tag beta",
"publish:canary": "npm publish --tag canary",
"lint": "standard",
"lint:fix": "standard --fix"
"lint:fix": "standard --fix",
"lint:types": "dtslint types ",
"test:db:start": "docker-compose -f test/docker/docker-compose.yml up",
"test:db:stop": "docker-compose -f test/docker/docker-compose.yml down"
},
"files": [
"dist",
"index.js",
"providers.js",
"adapters.js",
"client.js",
Expand All @@ -52,15 +54,22 @@
"@babel/cli": "^7.8.4",
"@babel/core": "^7.9.6",
"@babel/preset-env": "^7.9.6",
"@types/react": "^16.9.35",
"@types/react-dom": "^16.9.8",
"@types/webpack": "^4.41.17",
"autoprefixer": "^9.7.6",
"babel-preset-minify": "^0.5.1",
"babel-preset-preact": "^2.0.0",
"cssnano": "^4.1.10",
"mongodb": "^3.5.9",
"dtslint": "^3.6.10",
"mysql": "^2.18.1",
"pg": "^8.2.1",
"next": "^9.4.4",
"postcss-cli": "^7.1.1",
"postcss-nested": "^4.2.1",
"standard": "^14.3.3"
"standard": "^14.3.3",
"tsd": "^0.11.0",
"typescript": "^3.9.5"
}
}
25 changes: 25 additions & 0 deletions types/adapters/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Minimum TypeScript Version: 3.8

import type { ConnectionOptions } from 'typeorm';

/**
* TODO: type adapters correctly
* @see https://next-auth.js.org/schemas/adapters
*/
interface GenericObject {
[key: string]: any;
}

type Adapter = (config: ConnectionOptions) => Promise<GenericObject>;

interface Adapters {
Default: Adapter;
TypeORM: {
Adapter: Adapter;
Models: GenericObject;
};
}

declare const Adapters: Adapters;

export default Adapters;
3 changes: 3 additions & 0 deletions types/adapters/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Adapters from 'next-auth/adapters';

console.log(Adapters.Default);
65 changes: 65 additions & 0 deletions types/client/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Minimum TypeScript Version: 3.8
import { IncomingMessage } from 'http';

interface Session {
user: {
name: string;
email: string;
image: string;
};
accessToken: string;
expires: string;
}

interface GetProvidersResponse {
[provider: string]: Provider;
}

interface Provider {
id: string;
name: string;
type: string;
signinUrl: string;
callbackUrl: string;
}

interface GenericObject {
[key: string]: any;
}

declare function useSession(): [Session, boolean];
declare function getSession(context: NextContext): Promise<Session | null>;
declare function session(context: NextContext): Promise<Session | null>;
declare function getProviders(context: NextContext): Promise<GetProvidersResponse | null>;
declare function providers(context: NextContext): Promise<GetProvidersResponse | null>;
declare function getCsrfToken(context: NextContext): Promise<string | null>;
declare function csrfToken(context: NextContext): Promise<string | null>;
declare function signin(provider: Provider, data: GenericObject): Promise<void>;
declare function signout(context: NextContext): Promise<void>;

export { useSession, getSession, session, getProviders, providers, getCsrfToken, csrfToken, signin, signout };
export type { Session };

/**
* TODO: `dtslint` throws when parsing Next types... the following types are copied directly from `next/types` ...
* @see https://github.com/microsoft/dtslint/issues/297
*/

interface NextApiRequest extends IncomingMessage {
query: {
[key: string]: string | string[];
};
cookies: {
[key: string]: string;
};
body: any;
env: Env;
}

interface NextContext {
req: NextApiRequest;
}

interface Env {
[key: string]: string;
}
3 changes: 3 additions & 0 deletions types/client/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { useSession } from 'next-auth/client';

useSession();
109 changes: 109 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Minimum TypeScript Version: 3.8
/// <reference types="node" />
import type { ConnectionOptions } from 'typeorm';
import type { IncomingMessage, ServerResponse } from 'http';
import type { PossibleProviders } from './providers';
import Adapter from './adapters';

interface InitOptions {
site: string;
ubbe-xyz marked this conversation as resolved.
Show resolved Hide resolved
providers: PossibleProviders[];
ubbe-xyz marked this conversation as resolved.
Show resolved Hide resolved
database: ConnectionOptions | string;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a required option? From the docs they say it is optional (only required to persist accounts)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch 👏🏻 , @iaincollins could you confirm this? 🙏🏻

secret?: string;
jwt?: boolean;
jwtSecret?: string;
sessionMaxAge?: number;
sessionUpdateAge?: number;
verificationMaxAge?: number;
pages?: PageOptions;
debug?: boolean;
basePath?: string;
callbackUrlHandler?: (url: string, options: CallbackURLOptions) => Promise<void>;
adapter?: Adapter;
useSecureCookies?: boolean;
cookies?: Cookies;
}

interface PageOptions {
signin?: string;
signout?: string;
error?: string;
verifyRequest?: string;
newUsers?: string | null;
}

interface Cookies {
[cookieKey: string]: Cookie;
}

interface Cookie {
name: string;
options: CookieOptions;
}

interface CookieOptions {
httpOnly?: boolean;
// TODO: type available `sameSite` identifiers
sameSite: 'lax';
path: string;
secure: boolean;
}

interface CallbackURLOptions {
site: string;
defaultCallbackUrl?: string;
cookies?: Cookies;
callbacks?: Callbacks;
}

interface GenericObject {
[key: string]: any;
}

// TODO: Improve callback typings
interface Callbacks {
signin(profile: GenericObject, account: GenericObject, metadata: GenericObject): Promise<void>;
redirect(url: string, baseUrl: string): Promise<string>;
session(session: GenericObject, token: GenericObject): Promise<GenericObject>;
jwt(token: GenericObject, oAuthProfile: GenericObject): Promise<GenericObject>;
}

declare function NextAuth(req: NextApiRequest, res: NextApiResponse, options?: InitOptions): Promise<void>;

export default NextAuth;
export type { InitOptions, PageOptions };

/**
* TODO: `dtslint` throws when parsing Next types... the following types are copied directly from `next/types` ...
* @see https://github.com/microsoft/dtslint/issues/297
*/

interface NextApiRequest extends IncomingMessage {
query: {
[key: string]: string | string[];
};
cookies: {
[key: string]: string;
};
body: any;
env: Env;
}

type NextApiResponse<T = any> = ServerResponse & {
send: Send<T>;
json: Send<T>;
status: (statusCode: number) => NextApiResponse<T>;
setPreviewData: (
data: object | string,
options?: {
maxAge?: number;
},
) => NextApiResponse<T>;
clearPreviewData: () => NextApiResponse<T>;
};

interface Env {
[key: string]: string;
}

type Send<T> = (body: T) => void;
Loading