Skip to content

Commit

Permalink
Change config in Dapper and ApiForms/ApiActions to be a callable
Browse files Browse the repository at this point in the history
This is done in efforts to keep the config out of memory, so that it can
be refreshed and reliably used from the localStorage
  • Loading branch information
Oksamies committed Jan 19, 2025
1 parent 9c5c131 commit 914e125
Show file tree
Hide file tree
Showing 53 changed files with 126 additions and 104 deletions.
10 changes: 6 additions & 4 deletions apps/cyberstorm-remix/app/c/community.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ export const meta: MetaFunction<typeof loader> = ({ data }) => {
export async function loader({ request, params }: LoaderFunctionArgs) {
if (params.communityId) {
try {
const dapper = new DapperTs({
apiHost: process.env.PUBLIC_API_URL,
sessionId: undefined,
csrfToken: undefined,
const dapper = new DapperTs(() => {
return {
apiHost: process.env.PUBLIC_API_URL,
sessionId: undefined,
csrfToken: undefined,
};
});
const searchParams = new URL(request.url).searchParams;
const ordering =
Expand Down
10 changes: 6 additions & 4 deletions apps/cyberstorm-remix/app/communities/communities.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,12 @@ export async function loader({ request }: LoaderFunctionArgs) {
const order = searchParams.get("order") ?? SortOptions.Popular;
const search = searchParams.get("search");
const page = undefined;
const dapper = new DapperTs({
apiHost: process.env.PUBLIC_API_URL,
sessionId: undefined,
csrfToken: undefined,
const dapper = new DapperTs(() => {
return {
apiHost: process.env.PUBLIC_API_URL,
sessionId: undefined,
csrfToken: undefined,
};
});
return await dapper.getCommunities(page, order ?? "", search ?? "");
}
Expand Down
12 changes: 6 additions & 6 deletions apps/cyberstorm-remix/app/p/packageListing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ export const meta: MetaFunction<typeof loader> = ({ data }) => {
export async function loader({ params }: LoaderFunctionArgs) {
if (params.communityId && params.namespaceId && params.packageId) {
try {
const dapper = new DapperTs({
apiHost: process.env.PUBLIC_API_URL,
sessionId: undefined,
csrfToken: undefined,
const dapper = new DapperTs(() => {
return {
apiHost: process.env.PUBLIC_API_URL,
sessionId: undefined,
csrfToken: undefined,
};
});
return {
community: await dapper.getCommunity(params.communityId),
Expand Down Expand Up @@ -129,8 +131,6 @@ export default function Community() {

const [isLiked, setIsLiked] = useState(false);

// TODO: VERY CRITICAL TO FIGURE OUT !!! Figure out if this is stupid or not? I'm not sure if Remix will let fetch part of the client loader.
// Ideally we could just tell Remix to use the clientLoader to fetch all related data, but it seems like it just doesnt do it, so we have to do this.
const fetchAndSetRatedPackages = async () => {
const dapper = window.Dapper;
if (currentUser?.username) {
Expand Down
10 changes: 6 additions & 4 deletions apps/cyberstorm-remix/app/p/tabs/Changelog/Changelog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import { DapperTs } from "@thunderstore/dapper-ts";
export async function loader({ params }: LoaderFunctionArgs) {
if (params.namespaceId && params.packageId) {
try {
const dapper = new DapperTs({
apiHost: process.env.PUBLIC_API_URL,
sessionId: undefined,
csrfToken: undefined,
const dapper = new DapperTs(() => {
return {
apiHost: process.env.PUBLIC_API_URL,
sessionId: undefined,
csrfToken: undefined,
};
});
return {
status: "ok",
Expand Down
10 changes: 6 additions & 4 deletions apps/cyberstorm-remix/app/p/tabs/Readme/Readme.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import { DapperTs } from "@thunderstore/dapper-ts";
export async function loader({ params }: LoaderFunctionArgs) {
if (params.namespaceId && params.packageId) {
try {
const dapper = new DapperTs({
apiHost: process.env.PUBLIC_API_URL,
sessionId: undefined,
csrfToken: undefined,
const dapper = new DapperTs(() => {
return {
apiHost: process.env.PUBLIC_API_URL,
sessionId: undefined,
csrfToken: undefined,
};
});
return {
status: "ok",
Expand Down
10 changes: 6 additions & 4 deletions apps/cyberstorm-remix/app/p/tabs/Required/Required.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import { DapperTs } from "@thunderstore/dapper-ts";
export async function loader({ params }: LoaderFunctionArgs) {
if (params.communityId && params.namespaceId && params.packageId) {
try {
const dapper = new DapperTs({
apiHost: process.env.PUBLIC_API_URL,
sessionId: undefined,
csrfToken: undefined,
const dapper = new DapperTs(() => {
return {
apiHost: process.env.PUBLIC_API_URL,
sessionId: undefined,
csrfToken: undefined,
};
});
return {
listing: await dapper.getPackageListingDetails(
Expand Down
10 changes: 6 additions & 4 deletions apps/cyberstorm-remix/app/p/tabs/Versions/Versions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import { DapperTs } from "@thunderstore/dapper-ts";
export async function loader({ params }: LoaderFunctionArgs) {
if (params.namespaceId && params.packageId) {
try {
const dapper = new DapperTs({
apiHost: process.env.PUBLIC_API_URL,
sessionId: undefined,
csrfToken: undefined,
const dapper = new DapperTs(() => {
return {
apiHost: process.env.PUBLIC_API_URL,
sessionId: undefined,
csrfToken: undefined,
};
});
return {
status: "ok",
Expand Down
13 changes: 6 additions & 7 deletions apps/cyberstorm-remix/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ declare global {

export type OutletContextShape = {
currentUser: CurrentUser | undefined;
requestConfig: RequestConfig;
requestConfig: () => RequestConfig;
};

export const meta: MetaFunction = () => {
Expand Down Expand Up @@ -193,23 +193,22 @@ function App() {
const [currentUser, setCurrentUser] = useState<CurrentUser | undefined>(
undefined
);
const [requestConfig, setRequestConfig] = useState<RequestConfig>();
const [rcCallable, setRcCallable] = useState<() => RequestConfig>();

const session = useSession();
useEffect(() => {
// INIT DAPPER
window.Dapper = new DapperTs(session.getConfig(), () => {
window.Dapper = new DapperTs(session.getConfig, () => {
session.clearSession();
redirect("/communities");
});
// eslint-disable-next-line @typescript-eslint/no-unused-vars
setRcCallable((prevRcCallable) => session.getConfig);

Check failure

Code scanning / ESLint

Disallow unused variables Error

'prevRcCallable' is defined but never used.
setCurrentUser(session.getSessionCurrentUser(true));
setRequestConfig(session.getConfig());
}, []);

return (
<Outlet
context={{ currentUser: currentUser, requestConfig: requestConfig }}
/>
<Outlet context={{ currentUser: currentUser, requestConfig: rcCallable }} />
);
}

Expand Down
12 changes: 7 additions & 5 deletions apps/cyberstorm-remix/cyberstorm/dapper/sessionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ export function getDapper(isClient = false) {
if (isClient) {
const session = useSession();
// TODO: Add "ValidateDapper" and stop always returning a new dapper.
return new DapperTs(session.getConfig(), () => {
return new DapperTs(session.getConfig, () => {
session.clearSession();
redirect("/communities");
});
} else {
return new DapperTs({
apiHost: process.env.PUBLIC_API_URL,
sessionId: undefined,
csrfToken: undefined,
return new DapperTs(() => {
return {
apiHost: process.env.PUBLIC_API_URL,
sessionId: undefined,
csrfToken: undefined,
};
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function PackageDeprecateAction(props: {
namespace: string;
isDeprecated: boolean;
dataUpdateTrigger: () => Promise<void>;
config: RequestConfig;
config: () => RequestConfig;
}) {
const { onSubmitSuccess, onSubmitError } = useFormToaster({
successMessage: `${
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function PackageLikeAction(props: {
namespace: string;
isLiked: boolean;
dataUpdateTrigger: () => Promise<void>;
config: RequestConfig;
config: () => RequestConfig;
}) {
const { onSubmitSuccess, onSubmitError } = useFormToaster({
successMessage: `${props.isLiked ? "Unliked" : "Liked"} package ${
Expand Down
2 changes: 1 addition & 1 deletion packages/cyberstorm-forms/src/forms/PackageEditForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function PackageEditForm(props: {
isDeprecated: boolean;
deprecationButton: ReactNode;
dataUpdateTrigger: () => Promise<void>;
config: RequestConfig;
config: () => RequestConfig;
}) {
const { onSubmitSuccess, onSubmitError } = useFormToaster({
successMessage: "Changes saved!",
Expand Down
4 changes: 3 additions & 1 deletion packages/dapper-ts/src/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ const packageName = "Testitute";
let dapper: DapperTs;

beforeAll(() => {
dapper = new DapperTs({ apiHost: "https://thunderstore.dev" });
dapper = new DapperTs(() => {
return { apiHost: "https://thunderstore.dev" };
});
});

it("executes getCommunities without errors", async () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/dapper-ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ import {
} from "./methods/team";

export interface DapperTsInterface extends DapperInterface {
config: RequestConfig;
config: () => RequestConfig;
removeSessionHook?: () => void;
}

export class DapperTs implements DapperTsInterface {
config: RequestConfig;
config: () => RequestConfig;
removeSessionHook?: () => void;

constructor(config: RequestConfig, removeSessionHook?: () => void) {
constructor(config: () => RequestConfig, removeSessionHook?: () => void) {
this.config = config;
this.removeSessionHook = removeSessionHook;
this.getDynamicHTML = this.getDynamicHTML.bind(this);
Expand Down
2 changes: 1 addition & 1 deletion packages/dapper-ts/src/methods/currentUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const emptyUser = {
};

export async function getCurrentUser(this: DapperTsInterface) {
if (typeof this.config.sessionId !== "string") {
if (typeof this.config().sessionId !== "string") {
return emptyUser;
}

Expand Down
8 changes: 4 additions & 4 deletions packages/thunderstore-api/src/apiFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function sleep(delay: number) {
}

export type apiFetchArgs = {
config: RequestConfig;
config: () => RequestConfig;
path: string;
query?: string;
request?: Omit<RequestInit, "headers">;
Expand All @@ -45,9 +45,9 @@ export type apiFetchArgs = {
export async function apiFetch2(args: apiFetchArgs) {
const { config, path, request, query, useSession = false } = args;
const usedConfig: RequestConfig = useSession
? config
? config()
: {
apiHost: config.apiHost,
apiHost: config().apiHost,
csrfToken: undefined,
sessionId: undefined,
};
Expand All @@ -69,7 +69,7 @@ export async function apiFetch2(args: apiFetchArgs) {
}

export function apiFetch(
config: RequestConfig,
config: () => RequestConfig,
path: string,
query?: string,
request?: Omit<RequestInit, "headers">,
Expand Down
2 changes: 1 addition & 1 deletion packages/thunderstore-api/src/fetch/community.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { RequestConfig } from "../index";
import { apiFetch } from "../apiFetch";

export async function fetchCommunity(
config: RequestConfig,
config: () => RequestConfig,
communityId: string
) {
const path = `api/cyberstorm/community/${communityId}/`;
Expand Down
2 changes: 1 addition & 1 deletion packages/thunderstore-api/src/fetch/communityFilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { RequestConfig } from "../index";
import { apiFetch } from "../apiFetch";

export async function fetchCommunityFilters(
config: RequestConfig,
config: () => RequestConfig,
communityId: string
) {
const path = `api/cyberstorm/community/${communityId}/filters/`;
Expand Down
2 changes: 1 addition & 1 deletion packages/thunderstore-api/src/fetch/communityList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { apiFetch } from "../apiFetch";
import { serializeQueryString } from "../queryString";

export async function fetchCommunityList(
config: RequestConfig,
config: () => RequestConfig,
page = 1,
ordering = "name",
search?: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { serializeQueryString } from "../queryString";
import { PackageListingQueryParams } from "../types";

export async function fetchCommunityPackageListings(
config: RequestConfig,
config: () => RequestConfig,
communityId: string,
options?: PackageListingQueryParams
) {
Expand Down
2 changes: 1 addition & 1 deletion packages/thunderstore-api/src/fetch/currentUser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { RequestConfig } from "../index";
import { apiFetch } from "../apiFetch";

export async function fetchCurrentUser(config: RequestConfig) {
export async function fetchCurrentUser(config: () => RequestConfig) {
const path = "api/experimental/current-user/";
const request = { cache: "no-store" as RequestCache };

Expand Down
2 changes: 1 addition & 1 deletion packages/thunderstore-api/src/fetch/dynamicHTML.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { RequestConfig } from "../index";
import { apiFetch } from "../apiFetch";

export async function fetchDynamicHTML(
config: RequestConfig,
config: () => RequestConfig,
placement: string
) {
const path = `api/cyberstorm/dynamichtml/${placement}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { serializeQueryString } from "../queryString";
import { PackageListingQueryParams } from "../types";

export async function fetchNamespacePackageListings(
config: RequestConfig,
config: () => RequestConfig,
communityId: string,
namespaceId: string,
options?: PackageListingQueryParams
Expand Down
2 changes: 1 addition & 1 deletion packages/thunderstore-api/src/fetch/packageChangelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { RequestConfig } from "../index";
import { apiFetch } from "../apiFetch";

export async function fetchPackageChangelog(
config: RequestConfig,
config: () => RequestConfig,
namespaceId: string,
packageName: string,
versionNumber?: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { serializeQueryString } from "../queryString";
import { PackageListingQueryParams } from "../types";

export async function fetchPackageDependantsListings(
config: RequestConfig,
config: () => RequestConfig,
communityId: string,
namespaceId: string,
packageName: string,
Expand Down
2 changes: 1 addition & 1 deletion packages/thunderstore-api/src/fetch/packageDeprecate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type packageDeprecateApiArgs = {
};

export function packageDeprecate(
config: RequestConfig,
config: () => RequestConfig,
data: packageDeprecateApiArgs,
meta: packageDeprecateMetaArgs
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type PackageEditApiArgs = {
};

export function packageEditCategories(
config: RequestConfig,
config: () => RequestConfig,
data: PackageEditApiArgs,
meta: PackageEditMetaArgs
) {
Expand Down
2 changes: 1 addition & 1 deletion packages/thunderstore-api/src/fetch/packageLike.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type packageLikeApiArgs = {
};

export function packageLike(
config: RequestConfig,
config: () => RequestConfig,
data: packageLikeApiArgs,
meta: packageLikeMetaArgs
) {
Expand Down
Loading

0 comments on commit 914e125

Please sign in to comment.