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

refactor(ui): only initialize the api-client once #35

Merged
merged 2 commits into from
Dec 13, 2024
Merged
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
47 changes: 39 additions & 8 deletions ui/src/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,47 @@ import {
import { redirect } from "next/navigation";
import { auth } from "../app/api/auth/[...nextauth]/authOptions";

export async function getClient(tenantId: string) {
const session = await auth();
export class LHUserTasksClientSingleton {
private static instance: LittleHorseUserTasksApiClient | null = null;
private static currentConfig: {
baseUrl: string;
tenantId: string;
accessToken: string;
} | null = null;

private constructor() {}

Choose a reason for hiding this comment

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

usage syntax could be neater by calling getInstance inside the constructor and returning the instance, so you can use the singleton with the new ExampleClass() syntax.

i suppose it is just preference though

Copy link
Member Author

Choose a reason for hiding this comment

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

Thats a good point you brought up. Can you push those changes to the PR and ill merge.

Choose a reason for hiding this comment

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

@HazimAr Actually TypeScript class constructors can't return promises so nvm


public static async getInstance(
tenantId: string,
): Promise<LittleHorseUserTasksApiClient> {
const session = await auth();

if (!session) redirect(`/api/auth/signin?callbackUrl=/`);

const config = {
baseUrl: process.env.LHUT_API_URL!,
tenantId,
accessToken: session.access_token,
};

if (!session) redirect(`/api/auth/signin?callbackUrl=/`);
// Create new instance if config changes or doesn't exist
if (
!this.instance ||
!this.currentConfig ||
this.currentConfig.baseUrl !== config.baseUrl ||
this.currentConfig.tenantId !== config.tenantId ||
this.currentConfig.accessToken !== config.accessToken
) {
this.instance = new LittleHorseUserTasksApiClient(config);
this.currentConfig = config;
}

return new LittleHorseUserTasksApiClient({
baseUrl: process.env.LHUT_API_URL!,
tenantId,
accessToken: session.access_token,
});
return this.instance;
}
}

export async function getClient(tenantId: string) {
return LHUserTasksClientSingleton.getInstance(tenantId);
}

export async function clientWithErrorHandling<T>(
Expand Down
Loading