refactor: major refactor of the providers and resources to be more dynamic and flexible

This commit is contained in:
Derrick Hammer 2024-03-19 07:20:11 -04:00
parent 2e918e7802
commit ff5e5dec14
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
8 changed files with 177 additions and 148 deletions

View File

@ -0,0 +1,11 @@
import type { DataProvider } from "@refinedev/core";
import {SdkProvider} from "~/data/sdk-provider.js";
export const accountProvider: SdkProvider = {
getList: () => { throw Error("Not Implemented") },
getOne: () => { throw Error("Not Implemented") },
update: () => { throw Error("Not Implemented") },
create: () => { throw Error("Not Implemented") },
deleteOne: () => { throw Error("Not Implemented") },
getApiUrl: () => "",
}

View File

@ -31,42 +31,20 @@ export type Identity = {
email: string; email: string;
} }
export class PortalAuthProvider implements RequiredAuthProvider { export const createPortalAuthProvider = (sdk: Sdk): AuthProvider => {
constructor(apiUrl: string) { const maybeSetupAuth = (): void => {
this._sdk = Sdk.create(apiUrl); const jwt = sdk.account().jwtToken;
if (jwt) {
const methods: Array<keyof AuthProvider> = [ sdk.setAuthToken(jwt);
'login',
'logout',
'check',
'onError',
'register',
'forgotPassword',
'updatePassword',
'getPermissions',
'getIdentity',
];
methods.forEach((method) => {
this[method] = this[method]?.bind(this) as any;
});
}
private _sdk: Sdk;
get sdk(): Sdk {
return this._sdk;
}
public static create(apiUrl: string): AuthProvider {
return new PortalAuthProvider(apiUrl);
} }
};
return {
async login(params: AuthFormRequest): Promise<AuthActionResponse> { async login(params: AuthFormRequest): Promise<AuthActionResponse> {
const ret = await this._sdk.account().login({ const ret = await sdk.account().login({
email: params.email, email: params.email,
password: params.password, password: params.password,
}) });
let redirectTo: string | undefined; let redirectTo: string | undefined;
@ -75,59 +53,59 @@ export class PortalAuthProvider implements RequiredAuthProvider {
if (!redirectTo) { if (!redirectTo) {
redirectTo = ret ? "/dashboard" : "/login"; redirectTo = ret ? "/dashboard" : "/login";
} }
this._sdk.setAuthToken(this._sdk.account().jwtToken); sdk.setAuthToken(sdk.account().jwtToken);
} }
return { return {
success: ret, success: ret,
redirectTo, redirectTo,
}; };
} },
async logout(params: any): Promise<AuthActionResponse> { async logout(params: any): Promise<AuthActionResponse> {
let ret = await this._sdk.account().logout(); let ret = await sdk.account().logout();
return { success: ret, redirectTo: "/login" }; return { success: ret, redirectTo: "/login" };
} },
async check(params?: any): Promise<CheckResponse> { async check(params?: any): Promise<CheckResponse> {
const ret = await this._sdk.account().ping(); const ret = await sdk.account().ping();
if (ret) { if (ret) {
this.maybeSetupAuth(); maybeSetupAuth();
} }
return { authenticated: ret, redirectTo: ret ? undefined : "/login" }; return { authenticated: ret, redirectTo: ret ? undefined : "/login" };
} },
async onError(error: any): Promise<OnErrorResponse> { async onError(error: any): Promise<OnErrorResponse> {
return { logout: true }; return { logout: true };
} },
async register(params: RegisterFormRequest): Promise<AuthActionResponse> { async register(params: RegisterFormRequest): Promise<AuthActionResponse> {
const ret = await this._sdk.account().register({ const ret = await sdk.account().register({
email: params.email, email: params.email,
password: params.password, password: params.password,
first_name: params.firstName, first_name: params.firstName,
last_name: params.lastName, last_name: params.lastName,
}); });
return { success: ret, redirectTo: ret ? "/dashboard" : undefined }; return { success: ret, redirectTo: ret ? "/dashboard" : undefined };
} },
async forgotPassword(params: any): Promise<AuthActionResponse> { async forgotPassword(params: any): Promise<AuthActionResponse> {
return { success: true }; return { success: true };
} },
async updatePassword(params: any): Promise<AuthActionResponse> { async updatePassword(params: any): Promise<AuthActionResponse> {
return { success: true }; return { success: true };
} },
async getPermissions(params?: Record<string, any>): Promise<AuthActionResponse> { async getPermissions(params?: Record<string, any>): Promise<AuthActionResponse> {
return { success: true }; return { success: true };
} },
async getIdentity(params?: Identity): Promise<IdentityResponse> { async getIdentity(params?: Identity): Promise<IdentityResponse> {
this.maybeSetupAuth(); maybeSetupAuth();
const ret = await this._sdk.account().info(); const ret = await sdk.account().info();
if (!ret) { if (!ret) {
return { identity: null }; return { identity: null };
@ -141,24 +119,6 @@ export class PortalAuthProvider implements RequiredAuthProvider {
lastName: acct.last_name, lastName: acct.last_name,
email: acct.email, email: acct.email,
}; };
} },
};
maybeSetupAuth(): void { };
const jwt = this._sdk.account().jwtToken
if (jwt) {
this._sdk.setAuthToken(jwt);
}
}
}
export interface RequiredAuthProvider extends AuthProvider {
login: AuthProvider['login'];
logout: AuthProvider['logout'];
check: AuthProvider['check'];
onError: AuthProvider['onError'];
register: AuthProvider['register'];
forgotPassword: AuthProvider['forgotPassword'];
updatePassword: AuthProvider['updatePassword'];
getPermissions: AuthProvider['getPermissions'];
getIdentity: AuthProvider['getIdentity'];
}

View File

@ -1,6 +1,7 @@
import type { DataProvider } from "@refinedev/core"; import type { DataProvider } from "@refinedev/core";
import {SdkProvider} from "~/data/sdk-provider.js";
export const defaultProvider: DataProvider = { export const fileProvider: SdkProvider = {
getList: () => { throw Error("Not Implemented") }, getList: () => { throw Error("Not Implemented") },
getOne: () => { throw Error("Not Implemented") }, getOne: () => { throw Error("Not Implemented") },
update: () => { throw Error("Not Implemented") }, update: () => { throw Error("Not Implemented") },

31
app/data/providers.ts Normal file
View File

@ -0,0 +1,31 @@
import type {AuthProvider, DataProvider} from "@refinedev/core";
import {fileProvider} from "~/data/file-provider.js";
import {Sdk} from "@lumeweb/portal-sdk";
import {accountProvider} from "~/data/account-provider.js";
import type {SdkProvider} from "~/data/sdk-provider.js";
import {createPortalAuthProvider} from "~/data/auth-provider.js";
interface DataProviders {
default: SdkProvider;
auth: AuthProvider;
[key: string]: SdkProvider | AuthProvider;
}
let providers: DataProviders;
export function getProviders(sdk: Sdk) {
if (providers) {
return providers;
}
accountProvider.sdk = sdk;
fileProvider.sdk = sdk;
providers = {
default: accountProvider,
auth: createPortalAuthProvider(sdk),
files: fileProvider,
};
return providers;
}

18
app/data/resources.ts Normal file
View File

@ -0,0 +1,18 @@
import type {ResourceProps} from "@refinedev/core";
const resources: ResourceProps[] = [
{
name: 'account',
meta: {
dataProviderName: 'default',
}
},
{
name: 'file',
meta: {
dataProviderName: 'files',
}
}
];
export default resources;

6
app/data/sdk-provider.ts Normal file
View File

@ -0,0 +1,6 @@
import {DataProvider} from "@refinedev/core";
import {Sdk} from "@lumeweb/portal-sdk";
export interface SdkProvider extends DataProvider {
sdk?: Sdk;
}

View File

@ -1,10 +1,4 @@
import { import {Links, Meta, Outlet, Scripts, ScrollRestoration,} from "@remix-run/react";
Links,
Meta,
Outlet,
Scripts,
ScrollRestoration,
} from "@remix-run/react";
import stylesheet from "./tailwind.css?url"; import stylesheet from "./tailwind.css?url";
import type {LinksFunction} from "@remix-run/node"; import type {LinksFunction} from "@remix-run/node";
@ -12,10 +6,11 @@ import type { LinksFunction } from "@remix-run/node";
// Supports weights 200-800 // Supports weights 200-800
import '@fontsource-variable/manrope'; import '@fontsource-variable/manrope';
import {Refine} from "@refinedev/core"; import {Refine} from "@refinedev/core";
import {PortalAuthProvider} from "~/data/auth-provider.js";
import routerProvider from "@refinedev/remix-router"; import routerProvider from "@refinedev/remix-router";
import { defaultProvider } from "./data/file-provider";
import {SdkContextProvider} from "~/components/lib/sdk-context.js"; import {SdkContextProvider} from "~/components/lib/sdk-context.js";
import {getProviders} from "~/data/providers.js";
import {Sdk} from "@lumeweb/portal-sdk";
import resources from "~/data/resources.js";
export const links: LinksFunction = () => [ export const links: LinksFunction = () => [
{ rel: "stylesheet", href: stylesheet }, { rel: "stylesheet", href: stylesheet },
@ -40,19 +35,17 @@ export function Layout({children}: { children: React.ReactNode }) {
} }
export default function App() { export default function App() {
const auth = PortalAuthProvider.create("https://alpha.pinner.xyz") const sdk = Sdk.create(import.meta.env.VITE_PORTAL_URL)
const providers = getProviders(sdk);
return ( return (
<Refine <Refine
authProvider={auth} authProvider={providers.auth}
routerProvider={routerProvider} routerProvider={routerProvider}
dataProvider={defaultProvider} dataProvider={providers.default}
resources={[ resources={resources}
{ name: 'files' },
{ name: 'users' }
]}
options={{disableTelemetry: true}} options={{disableTelemetry: true}}
> >
<SdkContextProvider sdk={(auth as PortalAuthProvider).sdk}> <SdkContextProvider sdk={sdk}>
<Outlet/> <Outlet/>
</SdkContextProvider> </SdkContextProvider>
</Refine> </Refine>

9
vite-env.d.ts vendored Normal file
View File

@ -0,0 +1,9 @@
/// <reference types="vite/client" />
interface ImportMetaEnv {
readonly VITE_PORTAL_API_URL: string
}
interface ImportMeta {
readonly env: ImportMetaEnv
}