Skip to content

Commit

Permalink
fix(#26): removed simple-oauth2 dependecy
Browse files Browse the repository at this point in the history
fixed typing errors and removed unnecessary importings
  • Loading branch information
sametcodes committed Mar 7, 2023
1 parent 293fbaf commit ae89fc5
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 92 deletions.
3 changes: 1 addition & 2 deletions app/account/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use client";
import { useSession, signOut } from "next-auth/react";
import { useState } from "react";
import { FormEvent, ChangeEvent } from "react";
import { useState, FormEvent, ChangeEvent } from "react";

export default function Account() {
const { data: session } = useSession({ required: true });
Expand Down
32 changes: 11 additions & 21 deletions components/svgs/document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,19 @@ export const getDocumentStyle = () => {
</style>`;
};

type IDocument = {
(props: {
width: number;
height: number;
children: JSX.Element | JSX.Element[];
}): JSX.Element;
};
type IDocument = (props: {
width: number;
height: number;
children: JSX.Element | Array<JSX.Element>;
}) => JSX.Element;

type IDocumentHeader = {
(props: { title: string; desc: string }): JSX.Element;
};
type IDocumentHeader = (props: { title: string; desc: string }) => JSX.Element;

type IDocumentTitle = {
(props: { children: string }): JSX.Element;
};
type IDocumentTitle = (props: { children: string }) => JSX.Element;

type IList = {
(props: { children: any }): JSX.Element;
};
type IList = (props: { children: any }) => JSX.Element;

export type IListItem = {
(props: IItem): JSX.Element;
};
export type IListItem = (props: IItem) => JSX.Element;

type IItem = {
icon: JSX.Element;
Expand Down Expand Up @@ -133,7 +123,7 @@ export const List: IList = ({ children }) => {
};
};

const calculateGap = (elements: ReactElement<IItem, IListItem>[]) => {
const calculateGap = (elements: Array<ReactElement<IItem, IListItem>>) => {
if (!children) {
return [];
}
Expand All @@ -143,7 +133,7 @@ export const List: IList = ({ children }) => {
.reduce((a, b) => Math.max(a, b), 0) + 45;
return elements.map((child, index) => ({
...child,
props: { ...child.props, index, gap: gap },
props: { ...child.props, index, gap },
}));
};

Expand Down
2 changes: 1 addition & 1 deletion pages/api/data/[service].ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default async function handler(
.json({ message: "Bad request: unknown data API service" });

try {
const result = await dataService({ session: session, payload: req.body });
const result = await dataService({ session, payload: req.body });
return res.status(200).json({ success: true, data: result });
} catch (err) {
if (err instanceof Error) {
Expand Down
12 changes: 6 additions & 6 deletions pages/api/oauth/[...route].ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { NextApiRequest, NextApiResponse, NextApiHandler } from "next";
import { getServerSession } from "next-auth";
import { authOptions } from "@pages/api/auth/[...nextauth]";
import { authOptions } from "@/pages/api/auth/[...nextauth]";

import actions from "@services/oauth/actions";
import actions from "@/services/oauth/actions";
import passport from "passport";
import nextConnect from "next-connect";

import StackOverflowProvider from "@services/oauth/providers/stackoverflow";
import GithubProvider from "@services/oauth/providers/github";
import WakatimeProvider from "@services/oauth/providers/wakatime";
import StackOverflowProvider from "@/services/oauth/providers/stackoverflow";
import GithubProvider from "@/services/oauth/providers/github";
import WakatimeProvider from "@/services/oauth/providers/wakatime";

async function handler(req: NextApiRequest, res: NextApiResponse, next: any) {
const session = await getServerSession(req, res, authOptions);
if (!session) return res.redirect("/login");

const [action, platform]: string[] = req.query.route as string[];
const [action, platform]: Array<string> = req.query.route as Array<string>;

if (req.method !== "GET") return res.status(405).end();

Expand Down
4 changes: 2 additions & 2 deletions pages/api/platform/github.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as services from "@/services/platform/github";
import * as templates from "@/components/svgs/github";

import handlePlatformAPI from "@services/api/handler";
import handlePlatformAPI from "@/services/api/handler";

import nextConnect from "next-connect";
import GithubProvider from "@services/oauth/providers/github";
import GithubProvider from "@/services/oauth/providers/github";
import passport from "passport";
import refresh from "passport-oauth2-refresh";

Expand Down
4 changes: 2 additions & 2 deletions pages/api/platform/stackoverflow.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as services from "@/services/platform/stackoverflow";
import * as templates from "@/components/svgs/stackoverflow";

import handlePlatformAPI from "@services/api/handler";
import handlePlatformAPI from "@/services/api/handler";

import nextConnect from "next-connect";
import StackOverflowProvider from "@services/oauth/providers/stackoverflow";
import StackOverflowProvider from "@/services/oauth/providers/stackoverflow";
import passport from "passport";
import refresh from "passport-oauth2-refresh";

Expand Down
2 changes: 1 addition & 1 deletion pages/api/platform/wakatime.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as services from "@/services/platform/wakatime";
import * as templates from "@/components/svgs/wakatime";

import handlePlatformAPI from "@services/api/handler";
import handlePlatformAPI from "@/services/api/handler";

import nextConnect from "next-connect";
import WakatimeProvider from "@/services/oauth/providers/wakatime";
Expand Down
10 changes: 5 additions & 5 deletions scripts/migrates/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ type File = {

export interface JSDocMinified {
name: string;
tags: { title: string; text: string }[];
tags: Array<{ title: string; text: string }>;
description: string;
}

const getFiles = (path: string): File[] => {
const getFiles = (path: string): Array<File> => {
return fs
.readdirSync(path, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
Expand All @@ -28,7 +28,7 @@ const getFiles = (path: string): File[] => {
}));
};

const compileTs = (files: File[], cb: (files: File[]) => void) => {
const compileTs = (files: Array<File>, cb: (files: Array<File>) => void) => {
const options: ts.CompilerOptions = {
target: ts.ScriptTarget.ES2017,
module: ts.ModuleKind.CommonJS,
Expand Down Expand Up @@ -57,7 +57,7 @@ const migrate = async ({
docs,
code,
}: {
docs: JSDocMinified[];
docs: Array<JSDocMinified>;
code: string;
}): Promise<void> => {
let platform = await prisma.platform.findUnique({ where: { code } });
Expand Down Expand Up @@ -89,7 +89,7 @@ const migrate = async ({
});
};

const explainAndMigrateJSDoc = async (files: File[]): Promise<void> => {
const explainAndMigrateJSDoc = async (files: Array<File>): Promise<void> => {
console.log(`― Extracting JSDocs as JSON`);
const docs_promise = files.map((file) => {
const docs = jsdoc.explainSync({ files: file.js });
Expand Down
12 changes: 7 additions & 5 deletions services/api/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { getPlatformResponse } from "@/services/platform/response";
import prisma from "@/services/prisma";

import { requestNewAccessToken } from "passport-oauth2-refresh";
import actions from "@services/oauth/actions";
import actions from "@/services/oauth/actions";
import { Connection } from "@prisma/client";

type PlatformAPIHandler = {
(platformCode: string, services: any, templates: any): NextApiHandler;
};
type PlatformAPIHandler = (
platformCode: string,
services: any,
templates: any
) => NextApiHandler;

const handlePlatformAPI: PlatformAPIHandler = (
platformCode,
Expand Down Expand Up @@ -36,7 +38,7 @@ const handlePlatformAPI: PlatformAPIHandler = (
where: { userId: uid, platformId: platform.id },
});

var connection = await prisma.connection.findFirst({
let connection = await prisma.connection.findFirst({
where: { userId: uid, platformId: platform.id },
});

Expand Down
15 changes: 9 additions & 6 deletions services/nextauth/callbacks.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import prisma from "@/services/prisma";
import { CallbacksOptions } from "next-auth";
import { Awaitable, CallbacksOptions, Session } from "next-auth";

const session: CallbacksOptions["session"] = async ({ session }) => {
const session: CallbacksOptions["session"] = ({
session,
}): Awaitable<Session> => {
if (!session) return Promise.resolve(session);
if (!session?.user?.email) return Promise.resolve(session);

const user = await prisma.user.findUnique({
where: { email: session.user.email },
});
return Promise.resolve(Object.assign({}, session, { user }));
return prisma.user
.findUnique({
where: { email: session.user.email },
})
.then((user) => ({ ...session, user })) as Awaitable<Session>;
};

const callbacks = {
Expand Down
3 changes: 2 additions & 1 deletion services/oauth/actions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Connection, User } from "@/prisma/client";
import { Connection, User } from "@prisma/client";
import prisma from "@/services/prisma";

import { Session } from "next-auth";

type ISignIn = {
Expand Down
39 changes: 0 additions & 39 deletions services/oauth/index.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,5 @@
import { AuthorizationCode, AccessToken, Token } from "simple-oauth2";
import wakatime from "./providers/wakatime";
import github from "./providers/github";
import stackoverflow from "./providers/stackoverflow";

export type ConnectionProfile = {
name: string;
email: string;
image: string;
};

export type Provider = {
code: string;
client: {
id: string;
secret: string;
};
authorization: AuthorizationCode;
getTokenParam: (
provider: any,
params: any
) => {
code: string;
redirect_uri: string;

client_id?: string;
client_secret?: string;

state?: string;
grant_type?: string;
login?: string;
allow_signup?: boolean;
};
connect_url?: string;
getProfile: (token: Token) => Promise<ConnectionProfile | undefined>;
redirect_uri: string;
scope: string;
};

export const providers: { [key: string]: Provider } = {
wakatime,
github,
stackoverflow,
};
2 changes: 1 addition & 1 deletion utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export const trimChars = (body: string) => {
};

type ValueOf<T> = T[keyof T];
type Entries<T> = [keyof T, ValueOf<T>][];
type Entries<T> = Array<[keyof T, ValueOf<T>]>;

export function ObjectEntries<T extends object>(obj: T): Entries<T> {
return Object.entries(obj) as Entries<T>;
Expand Down

0 comments on commit ae89fc5

Please sign in to comment.