Skip to content

Commit

Permalink
Merge pull request #62 from nookit-dev/biome-setup
Browse files Browse the repository at this point in the history
Biome setup
  • Loading branch information
brandon-schabel authored Nov 27, 2023
2 parents 4d4d5f5 + 1fe7f7a commit 35c4333
Show file tree
Hide file tree
Showing 114 changed files with 832 additions and 1,722 deletions.
26 changes: 16 additions & 10 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
{
// The path to the `bun` executable.
"bun.runtime": "/Users/brandon/.bun/_bun",

// If support for Bun should be added to the default "JavaScript Debug Terminal".
"bun.debugTerminal.enabled": true,

// If the debugger should stop on the first line of the program.
"bun.debugTerminal.stopOnEntry": false,
"editor.showDeprecated": false,
// The path to the `bun` executable.
"bun.runtime": "/Users/brandon/.bun/_bun",

// If support for Bun should be added to the default "JavaScript Debug Terminal".
"bun.debugTerminal.enabled": true,

// If the debugger should stop on the first line of the program.
"bun.debugTerminal.stopOnEntry": false,
"editor.showDeprecated": false,
"[javascript]": {
"editor.defaultFormatter": "biomejs.biome"
},
"editor.codeActionsOnSave": {
"quickfix.biome": true,
"source.organizeImports.biome": true
}
}
1 change: 0 additions & 1 deletion auth/example/google-oauth-server-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { serverFactory } from "server";
const googleClientId = Bun.env.GOOGLE_OAUTH_CLIENT_ID || "";
const googleClientSecret = Bun.env.GOOGLE_OAUTH_CLIENT_SECRET || "";


const googleOAuthConfig = initGoogleOAuth({
clientId: googleClientId,
clientSecret: googleClientSecret,
Expand Down
2 changes: 1 addition & 1 deletion auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export {
createSecurityToken,
createToken,
getTokenExpireEpoch,
verifyToken
verifyToken,
} from "./security-token";

export { oAuthFactory } from "./oauth";
Expand Down
19 changes: 4 additions & 15 deletions auth/oauth-providers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { OAuthConfig, OAuthProviderFn } from "./oauth-types";

export type ProvidersConfigRecord = Record<
string,
Omit<OAuthConfig, "clientId" | "clientSecret">
>;
export type ProvidersConfigRecord = Record<string, Omit<OAuthConfig, "clientId" | "clientSecret">>;

export const oAuthProviders = {
google: {
Expand All @@ -13,8 +10,7 @@ export const oAuthProviders = {
},
microsoft: {
redirectUri: "http://localhost:3000/callback",
authReqUrl:
"https://login.microsoftonline.com/common/oauth2/v2.0/authorize",
authReqUrl: "https://login.microsoftonline.com/common/oauth2/v2.0/authorize",
tokenUrl: "http://needtofind",
},
github: {
Expand All @@ -24,10 +20,7 @@ export const oAuthProviders = {
},
} satisfies ProvidersConfigRecord;

export const initGoogleOAuth: OAuthProviderFn = (
{ clientId, clientSecret },
options
) => {
export const initGoogleOAuth: OAuthProviderFn = ({ clientId, clientSecret }, options) => {
const redirectUrl = options?.redirectUrl;
return {
...oAuthProviders.google,
Expand All @@ -37,10 +30,7 @@ export const initGoogleOAuth: OAuthProviderFn = (
};
};

export const initGithubOAuth: OAuthProviderFn = (
{ clientId, clientSecret },
options
) => {
export const initGithubOAuth: OAuthProviderFn = ({ clientId, clientSecret }, options) => {
const redirectUrl = options?.redirectUrl;
return {
...oAuthProviders.github,
Expand All @@ -49,4 +39,3 @@ export const initGithubOAuth: OAuthProviderFn = (
clientSecret,
};
};

5 changes: 1 addition & 4 deletions auth/oauth-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ export type OAuthProviderOptions = {
};

export type OAuthProviderCreds = Pick<OAuthConfig, "clientId" | "clientSecret">;
export type OAuthProviderFn = (
config: OAuthProviderCreds,
options?: OAuthProviderOptions
) => OAuthConfig;
export type OAuthProviderFn = (config: OAuthProviderCreds, options?: OAuthProviderOptions) => OAuthConfig;

export type OAuthProviderInitializer = (config: OAuthConfig) => OAuthHelpers;
15 changes: 3 additions & 12 deletions auth/oauth.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
OAuthConfig,
OAuthProviderInitializer,
OAuthToken,
} from "./oauth-types";
import { OAuthConfig, OAuthProviderInitializer, OAuthToken } from "./oauth-types";

type FetcherResponse<T> = T & {
error?: string;
Expand All @@ -14,7 +10,7 @@ export async function oAuthFetcher<T>(
options: {
params: Record<string, string | undefined>;
headers?: Record<string, string>;
}
},
): Promise<FetcherResponse<T>> {
const response = await fetch(url, {
method: "post",
Expand Down Expand Up @@ -47,12 +43,7 @@ export async function getOAuthToken<T extends OAuthToken>({
return oAuthFetcher<T>(tokenUrl, params);
}

export const initProvider: OAuthProviderInitializer = ({
clientId,
authReqUrl,
redirectUri,
headers,
}) => {
export const initProvider: OAuthProviderInitializer = ({ clientId, authReqUrl, redirectUri, headers }) => {
return {
// TODO add options to be able to change response_type/scope, etc
getAuthorizationUrl: () => {
Expand Down
11 changes: 2 additions & 9 deletions auth/security-token.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { describe, it } from "bun:test";
import { expect } from "bun:test";
import {
getTokenExpireEpoch,
createToken,
verifyToken,
createSecurityToken,
} from ".";
import { getTokenExpireEpoch, createToken, verifyToken, createSecurityToken } from ".";

describe("Token Utilities", () => {
describe("getTokenExpireEpoch", () => {
Expand Down Expand Up @@ -40,8 +35,6 @@ describe("Token Utilities", () => {

describe("createSecurityToken", () => {
it("should create a security token with default expiration time", async () => {


const result = await createSecurityToken(5000);
expect(result.securityToken).toBeTruthy();
expect(result.tokenId).toBeTruthy();
Expand All @@ -52,7 +45,7 @@ describe("Token Utilities", () => {
const currentTime = new Date();
const tokenValidTime = 60 * 15; // 15 minutes
const result = await createSecurityToken(tokenValidTime, currentTime);
const expectedExpiration = currentTime.getTime() + (tokenValidTime * 1000);
const expectedExpiration = currentTime.getTime() + tokenValidTime * 1000;
expect(result.tokenExpireEpoch).toBeCloseTo(expectedExpiration, -2); // -2 is for a precision of 10 milliseconds
});
});
Expand Down
11 changes: 2 additions & 9 deletions auth/security-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ export const getTokenExpireEpoch = (date: Date, tokenValidTimeSec: number) => {
return expireEpoch;
};

export async function verifyToken(
tokenString: string,
salt: string,
storedHash: string
) {
export async function verifyToken(tokenString: string, salt: string, storedHash: string) {
const fullPassword = tokenString + salt;
const isMatch = await Bun.password.verify(fullPassword, storedHash);

Expand All @@ -26,10 +22,7 @@ export async function createToken(string: string, salt: string) {
});
}

export const createSecurityToken = async (
tokenValidTime: number,
currentDate?: Date
) => {
export const createSecurityToken = async (tokenValidTime: number, currentDate?: Date) => {
const salt = uuid();
const [tokenId, timestamp] = uuid({
returnTimestamp: true,
Expand Down
17 changes: 17 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$schema": "https://biomejs.dev/schemas/1.4.0/schema.json",
"organizeImports": {
"enabled": false
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
},
"formatter": {
"enabled": true,
"lineWidth": 120,
"indentStyle": "space"
}
}
35 changes: 11 additions & 24 deletions cli/cli-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,12 @@ export function getArguments(): string[] {
export function getOptionValue(
arg: string,
nextArg: string,
optionDef: OptionDefinition
optionDef: OptionDefinition,
): string | boolean | undefined {
let value = optionDef.default;

if (nextArg && !nextArg.startsWith("--")) {
const type = optionDef.types.find(
(type) => type === typeof nextArg || type === typeof value
);
const type = optionDef.types.find((type) => type === typeof nextArg || type === typeof value);
cliLog("Type found: ", type); // Debug log
if (type === "boolean") {
if (nextArg.toLowerCase() === "true") {
Expand All @@ -60,7 +58,7 @@ export function getOptionValue(

export function parseArgument(
arg: string,
nextArg: string
nextArg: string,
): { key: string | undefined; value: string | boolean | undefined } {
let key: string | undefined = undefined;
let value: string | boolean | undefined;
Expand Down Expand Up @@ -111,9 +109,7 @@ export const getAdditionalPrompt = () =>
});
});

export const chooseActions = async (
actionsConfig: Record<string, any>
): Promise<Array<keyof typeof actionsConfig>> => {
export const chooseActions = async (actionsConfig: Record<string, any>): Promise<Array<keyof typeof actionsConfig>> => {
cliLog("\nChoose actions (separated by commas):");
const actions = Object.keys(actionsConfig);
actions.forEach((action, index) => {
Expand All @@ -126,27 +122,18 @@ export const chooseActions = async (
});

const actionIndexes = await new Promise<string>((resolve) => {
rl.question(
"Enter the numbers corresponding to the actions: ",
(actionIndexes) => {
rl.close();
resolve(actionIndexes);
}
);
rl.question("Enter the numbers corresponding to the actions: ", (actionIndexes) => {
rl.close();
resolve(actionIndexes);
});
});

const selectedIndexes = actionIndexes
.split(",")
.map((index) => parseInt(index.trim()) - 1);
const selectedIndexes = actionIndexes.split(",").map((index) => parseInt(index.trim()) - 1);

const validSelection = selectedIndexes.every(
(index) => index >= 0 && index < actions.length
);
const validSelection = selectedIndexes.every((index) => index >= 0 && index < actions.length);

if (validSelection) {
return selectedIndexes.map(
(index) => actions[index] as keyof typeof actionsConfig
);
return selectedIndexes.map((index) => actions[index] as keyof typeof actionsConfig);
} else {
cliLog("Invalid input, please try again.");
return chooseActions(actionsConfig);
Expand Down
2 changes: 1 addition & 1 deletion cli/create-cli-factory.test.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
import { describe, expect,test} from "bun:test";
import { describe, expect, test } from "bun:test";
7 changes: 1 addition & 6 deletions cli/create-cli-factory.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { fileFactory } from "../files-folders";
import { defaultLogger } from "../logger";
import { BaseError } from "../utils/base-error";
import {
chooseActions,
getAdditionalPrompt,
getUserInput,
parseCliArgs,
} from "./cli-utils";
import { chooseActions, getAdditionalPrompt, getUserInput, parseCliArgs } from "./cli-utils";

export type CLIOptions = {
inputPrompt?: string;
Expand Down
2 changes: 1 addition & 1 deletion client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ export type {
TypeMap,
} from "./fetcher/fetch-types.ts";

export {} from './auth/security-token.ts'
export {} from "./auth/security-token.ts";
11 changes: 2 additions & 9 deletions cookies/client-cookie-factory.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import { CookieOptions } from "./cookie-types";
import {
parseCookieData,
retrieveRawCookieValue,
setCookie,
} from "./cookie-utils";
import { parseCookieData, retrieveRawCookieValue, setCookie } from "./cookie-utils";

declare var document: {
cookie: any;
};

export function clientCookieFactory<T = string>(
cookieKey: string,
options?: CookieOptions
) {
export function clientCookieFactory<T = string>(cookieKey: string, options?: CookieOptions) {
const handleSetCookie = (value: T, cookieSetOptions: CookieOptions = {}) => {
setCookie(cookieKey, value, cookieSetOptions || options || {});
};
Expand Down
14 changes: 7 additions & 7 deletions cookies/cookie-types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export type CookieOptions = {
maxAge?: number;
path?: string;
domain?: string;
secure?: boolean;
httpOnly?: boolean;
sameSite?: "Strict" | "Lax" | "None";
};
maxAge?: number;
path?: string;
domain?: string;
secure?: boolean;
httpOnly?: boolean;
sameSite?: "Strict" | "Lax" | "None";
};
9 changes: 2 additions & 7 deletions cookies/cookie-utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { afterEach, describe, expect, it } from "bun:test";
import {
parseCookieData,
retrieveRawCookieValue,
stringifyCookieData
} from "./cookie-utils";
import { parseCookieData, retrieveRawCookieValue, stringifyCookieData } from "./cookie-utils";

declare var document: {
cookie: any;
Expand Down Expand Up @@ -59,8 +55,7 @@ describe("retrieveRawCookieValue", () => {
});

it("should decode URI encoded cookie names and values", () => {
document.cookie =
"encodedName%3D=encodedValue%3D; anotherCookie=anotherValue";
document.cookie = "encodedName%3D=encodedValue%3D; anotherCookie=anotherValue";
expect(retrieveRawCookieValue("encodedName=")).toBe("encodedValue=");
});

Expand Down
14 changes: 3 additions & 11 deletions cookies/cookie-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,9 @@ export const retrieveRawCookieValue = (name: string): string | null => {
return null;
};

export const encodeCookie = <T>(
cookieKey: string,
value: T,
options: CookieOptions
): string => {
export const encodeCookie = <T>(cookieKey: string, value: T, options: CookieOptions): string => {
let cookieString = `${encodeURIComponent(cookieKey)}=${encodeURIComponent(
typeof value === "string" ? value : JSON.stringify(value)
typeof value === "string" ? value : JSON.stringify(value),
)}`;

if (options.maxAge) {
Expand All @@ -69,11 +65,7 @@ export const encodeCookie = <T>(
return cookieString;
};

export const setCookie = <T>(
cookieKey: string,
value: T,
options: CookieOptions
) => {
export const setCookie = <T>(cookieKey: string, value: T, options: CookieOptions) => {
document.cookie = encodeCookie(cookieKey, value, options);
};

Expand Down
Loading

0 comments on commit 35c4333

Please sign in to comment.