mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-17 04:46:05 +00:00
added format command and added trailing commas to reduce conflicts
This commit is contained in:
@@ -24,14 +24,14 @@ export type { TSchemaActions };
|
||||
enum Fetching {
|
||||
None = 0,
|
||||
Schema = 1,
|
||||
Secrets = 2
|
||||
Secrets = 2,
|
||||
}
|
||||
|
||||
export function BkndProvider({
|
||||
includeSecrets = false,
|
||||
adminOverride,
|
||||
children,
|
||||
fallback = null
|
||||
fallback = null,
|
||||
}: { includeSecrets?: boolean; children: any; fallback?: React.ReactNode } & Pick<
|
||||
BkndContext,
|
||||
"adminOverride"
|
||||
@@ -59,7 +59,7 @@ export function BkndProvider({
|
||||
|
||||
const res = await api.system.readSchema({
|
||||
config: true,
|
||||
secrets: _includeSecrets
|
||||
secrets: _includeSecrets,
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
@@ -80,13 +80,13 @@ export function BkndProvider({
|
||||
schema: getDefaultSchema(),
|
||||
config: getDefaultConfig(),
|
||||
permissions: [],
|
||||
fallback: true
|
||||
fallback: true,
|
||||
} as any);
|
||||
|
||||
if (adminOverride) {
|
||||
newSchema.config.server.admin = {
|
||||
...newSchema.config.server.admin,
|
||||
...adminOverride
|
||||
...adminOverride,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import type { AppTheme } from "modules/server/AppServer";
|
||||
import { createContext, useContext } from "react";
|
||||
|
||||
const ClientContext = createContext<{ baseUrl: string; api: Api }>({
|
||||
baseUrl: undefined
|
||||
baseUrl: undefined,
|
||||
} as any);
|
||||
|
||||
export type ClientProviderProps = {
|
||||
@@ -69,7 +69,7 @@ export function useBkndWindowContext(): BkndWindowContext {
|
||||
return window.__BKND__ as any;
|
||||
} else {
|
||||
return {
|
||||
logout_route: "/api/auth/logout"
|
||||
logout_route: "/api/auth/logout",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,10 +5,10 @@ import { useApi } from "ui/client";
|
||||
|
||||
export const useApiQuery = <
|
||||
Data,
|
||||
RefineFn extends (data: ResponseObject<Data>) => unknown = (data: ResponseObject<Data>) => Data
|
||||
RefineFn extends (data: ResponseObject<Data>) => unknown = (data: ResponseObject<Data>) => Data,
|
||||
>(
|
||||
fn: (api: Api) => FetchPromise<Data>,
|
||||
options?: SWRConfiguration & { enabled?: boolean; refine?: RefineFn }
|
||||
options?: SWRConfiguration & { enabled?: boolean; refine?: RefineFn },
|
||||
) => {
|
||||
const api = useApi();
|
||||
const promise = fn(api);
|
||||
@@ -23,7 +23,7 @@ export const useApiQuery = <
|
||||
...swr,
|
||||
promise,
|
||||
key,
|
||||
api
|
||||
api,
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ import { type Api, useApi } from "ui/client";
|
||||
export class UseEntityApiError<Payload = any> extends Error {
|
||||
constructor(
|
||||
public response: ResponseObject<Payload>,
|
||||
fallback?: string
|
||||
fallback?: string,
|
||||
) {
|
||||
let message = fallback;
|
||||
if ("error" in response) {
|
||||
@@ -26,10 +26,10 @@ export class UseEntityApiError<Payload = any> extends Error {
|
||||
export const useEntity = <
|
||||
Entity extends keyof DB | string,
|
||||
Id extends PrimaryFieldType | undefined = undefined,
|
||||
Data = Entity extends keyof DB ? DB[Entity] : EntityData
|
||||
Data = Entity extends keyof DB ? DB[Entity] : EntityData,
|
||||
>(
|
||||
entity: Entity,
|
||||
id?: Id
|
||||
id?: Id,
|
||||
) => {
|
||||
const api = useApi().data;
|
||||
|
||||
@@ -71,7 +71,7 @@ export const useEntity = <
|
||||
throw new UseEntityApiError(res, `Failed to delete entity "${entity}"`);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
@@ -80,7 +80,7 @@ export function makeKey(
|
||||
api: ModuleApi,
|
||||
entity: string,
|
||||
id?: PrimaryFieldType,
|
||||
query?: RepoQueryIn
|
||||
query?: RepoQueryIn,
|
||||
) {
|
||||
return (
|
||||
"/" +
|
||||
@@ -93,12 +93,12 @@ export function makeKey(
|
||||
|
||||
export const useEntityQuery = <
|
||||
Entity extends keyof DB | string,
|
||||
Id extends PrimaryFieldType | undefined = undefined
|
||||
Id extends PrimaryFieldType | undefined = undefined,
|
||||
>(
|
||||
entity: Entity,
|
||||
id?: Id,
|
||||
query?: RepoQueryIn,
|
||||
options?: SWRConfiguration & { enabled?: boolean; revalidateOnMutate?: boolean }
|
||||
options?: SWRConfiguration & { enabled?: boolean; revalidateOnMutate?: boolean },
|
||||
) => {
|
||||
const api = useApi().data;
|
||||
const key = makeKey(api, entity as string, id, query);
|
||||
@@ -109,13 +109,13 @@ export const useEntityQuery = <
|
||||
const swr = useSWR<T>(options?.enabled === false ? null : key, fetcher as any, {
|
||||
revalidateOnFocus: false,
|
||||
keepPreviousData: true,
|
||||
...options
|
||||
...options,
|
||||
});
|
||||
|
||||
const mutateAll = async () => {
|
||||
const entityKey = makeKey(api, entity as string);
|
||||
return mutate((key) => typeof key === "string" && key.startsWith(entityKey), undefined, {
|
||||
revalidate: true
|
||||
revalidate: true,
|
||||
});
|
||||
};
|
||||
|
||||
@@ -138,13 +138,13 @@ export const useEntityQuery = <
|
||||
mutate: mutateAll,
|
||||
mutateRaw: swr.mutate,
|
||||
api,
|
||||
key
|
||||
key,
|
||||
};
|
||||
};
|
||||
|
||||
export async function mutateEntityCache<
|
||||
Entity extends keyof DB | string,
|
||||
Data = Entity extends keyof DB ? Omit<DB[Entity], "id"> : EntityData
|
||||
Data = Entity extends keyof DB ? Omit<DB[Entity], "id"> : EntityData,
|
||||
>(api: Api["data"], entity: Entity, id: PrimaryFieldType, partialData: Partial<Data>) {
|
||||
function update(prev: any, partialNext: any) {
|
||||
if (
|
||||
@@ -171,23 +171,23 @@ export async function mutateEntityCache<
|
||||
return update(data, partialData);
|
||||
},
|
||||
{
|
||||
revalidate: false
|
||||
}
|
||||
revalidate: false,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
export const useEntityMutate = <
|
||||
Entity extends keyof DB | string,
|
||||
Id extends PrimaryFieldType | undefined = undefined,
|
||||
Data = Entity extends keyof DB ? Omit<DB[Entity], "id"> : EntityData
|
||||
Data = Entity extends keyof DB ? Omit<DB[Entity], "id"> : EntityData,
|
||||
>(
|
||||
entity: Entity,
|
||||
id?: Id,
|
||||
options?: SWRConfiguration
|
||||
options?: SWRConfiguration,
|
||||
) => {
|
||||
const { data, ...$q } = useEntityQuery<Entity, Id>(entity, id, undefined, {
|
||||
...options,
|
||||
enabled: false
|
||||
enabled: false,
|
||||
});
|
||||
|
||||
const _mutate = id
|
||||
@@ -198,6 +198,6 @@ export const useEntityMutate = <
|
||||
...$q,
|
||||
mutate: _mutate as unknown as Id extends undefined
|
||||
? (id: PrimaryFieldType, data: Partial<Data>) => Promise<void>
|
||||
: (data: Partial<Data>) => Promise<void>
|
||||
: (data: Partial<Data>) => Promise<void>,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -3,7 +3,7 @@ export {
|
||||
useBkndWindowContext,
|
||||
type ClientProviderProps,
|
||||
useApi,
|
||||
useBaseUrl
|
||||
useBaseUrl,
|
||||
} from "./ClientProvider";
|
||||
|
||||
export * from "./api/use-api";
|
||||
|
||||
@@ -18,12 +18,12 @@ export function getSchemaActions({ api, setSchema, reloadSchema }: SchemaActions
|
||||
action: string,
|
||||
module: Module,
|
||||
res: ResponseObject<ConfigUpdateResponse<Module>>,
|
||||
path?: string
|
||||
path?: string,
|
||||
): Promise<boolean> {
|
||||
const base: Partial<NotificationData> = {
|
||||
id: "schema-" + [action, module, path].join("-"),
|
||||
position: "top-right",
|
||||
autoClose: 3000
|
||||
autoClose: 3000,
|
||||
};
|
||||
|
||||
if (res.success === true) {
|
||||
@@ -35,8 +35,8 @@ export function getSchemaActions({ api, setSchema, reloadSchema }: SchemaActions
|
||||
...prev,
|
||||
config: {
|
||||
...prev.config,
|
||||
[module]: res.config
|
||||
}
|
||||
[module]: res.config,
|
||||
},
|
||||
};
|
||||
});
|
||||
}
|
||||
@@ -45,7 +45,7 @@ export function getSchemaActions({ api, setSchema, reloadSchema }: SchemaActions
|
||||
...base,
|
||||
title: `Config updated: ${ucFirst(module)}`,
|
||||
color: "blue",
|
||||
message: `Operation ${action.toUpperCase()} at ${module}${path ? "." + path : ""}`
|
||||
message: `Operation ${action.toUpperCase()} at ${module}${path ? "." + path : ""}`,
|
||||
});
|
||||
} else {
|
||||
notifications.show({
|
||||
@@ -54,7 +54,7 @@ export function getSchemaActions({ api, setSchema, reloadSchema }: SchemaActions
|
||||
color: "red",
|
||||
withCloseButton: true,
|
||||
autoClose: false,
|
||||
message: res.error ?? "Failed to complete config update"
|
||||
message: res.error ?? "Failed to complete config update",
|
||||
});
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ export function getSchemaActions({ api, setSchema, reloadSchema }: SchemaActions
|
||||
set: async <Module extends keyof ModuleConfigs>(
|
||||
module: keyof ModuleConfigs,
|
||||
value: ModuleConfigs[Module],
|
||||
force?: boolean
|
||||
force?: boolean,
|
||||
) => {
|
||||
const res = await api.system.setConfig(module, value, force);
|
||||
return await handleConfigUpdate("set", module, res);
|
||||
@@ -74,7 +74,7 @@ export function getSchemaActions({ api, setSchema, reloadSchema }: SchemaActions
|
||||
patch: async <Module extends keyof ModuleConfigs>(
|
||||
module: Module,
|
||||
path: string,
|
||||
value: any
|
||||
value: any,
|
||||
): Promise<boolean> => {
|
||||
const res = await api.system.patchConfig(module, path, value);
|
||||
return await handleConfigUpdate("patch", module, res, path);
|
||||
@@ -82,7 +82,7 @@ export function getSchemaActions({ api, setSchema, reloadSchema }: SchemaActions
|
||||
overwrite: async <Module extends keyof ModuleConfigs>(
|
||||
module: Module,
|
||||
path: string,
|
||||
value: any
|
||||
value: any,
|
||||
) => {
|
||||
const res = await api.system.overwriteConfig(module, path, value);
|
||||
return await handleConfigUpdate("overwrite", module, res, path);
|
||||
@@ -94,6 +94,6 @@ export function getSchemaActions({ api, setSchema, reloadSchema }: SchemaActions
|
||||
remove: async <Module extends keyof ModuleConfigs>(module: Module, path: string) => {
|
||||
const res = await api.system.removeConfig(module, path);
|
||||
return await handleConfigUpdate("remove", module, res, path);
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -69,6 +69,6 @@ export const useAuth = (options?: { baseUrl?: string }): UseAuth => {
|
||||
register,
|
||||
logout,
|
||||
setToken,
|
||||
verify
|
||||
verify,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -14,7 +14,7 @@ export function useBkndAuth() {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
},
|
||||
},
|
||||
roles: {
|
||||
add: async (name: string, data: any = {}) => {
|
||||
@@ -31,8 +31,8 @@ export function useBkndAuth() {
|
||||
return await bkndActions.remove("auth", `roles.${name}`);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const minimum_permissions = [
|
||||
@@ -40,7 +40,7 @@ export function useBkndAuth() {
|
||||
"system.access.api",
|
||||
"system.config.read",
|
||||
"system.config.read.secrets",
|
||||
"system.build"
|
||||
"system.build",
|
||||
];
|
||||
const $auth = {
|
||||
roles: {
|
||||
@@ -49,19 +49,21 @@ export function useBkndAuth() {
|
||||
has_admin: Object.entries(config.auth.roles ?? {}).some(
|
||||
([name, role]) =>
|
||||
role.implicit_allow ||
|
||||
minimum_permissions.every((p) => role.permissions?.includes(p))
|
||||
)
|
||||
minimum_permissions.every((p) => role.permissions?.includes(p)),
|
||||
),
|
||||
},
|
||||
routes: {
|
||||
settings: app.getSettingsPath(["auth"]),
|
||||
listUsers: app.getAbsolutePath("/data/" + routes.data.entity.list(config.auth.entity_name))
|
||||
}
|
||||
listUsers: app.getAbsolutePath(
|
||||
"/data/" + routes.data.entity.list(config.auth.entity_name),
|
||||
),
|
||||
},
|
||||
};
|
||||
|
||||
return {
|
||||
$auth,
|
||||
config: config.auth,
|
||||
schema: schema.auth,
|
||||
actions
|
||||
actions,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import {
|
||||
entitiesSchema,
|
||||
entityFields,
|
||||
fieldsSchema,
|
||||
relationsSchema
|
||||
relationsSchema,
|
||||
} from "data/data-schema";
|
||||
import { useBknd } from "ui/client/bknd";
|
||||
import type { TSchemaActions } from "ui/client/schema/actions";
|
||||
@@ -28,7 +28,7 @@ export function useBkndData() {
|
||||
console.log("create entity", { data });
|
||||
const validated = parse(entitiesSchema, data, {
|
||||
skipMark: true,
|
||||
forceParse: true
|
||||
forceParse: true,
|
||||
});
|
||||
console.log("validated", validated);
|
||||
// @todo: check for existing?
|
||||
@@ -46,12 +46,12 @@ export function useBkndData() {
|
||||
return await bkndActions.overwrite(
|
||||
"data",
|
||||
`entities.${entityName}.config`,
|
||||
partial
|
||||
partial,
|
||||
);
|
||||
},
|
||||
fields: entityFieldActions(bkndActions, entityName)
|
||||
fields: entityFieldActions(bkndActions, entityName),
|
||||
};
|
||||
}
|
||||
},
|
||||
},
|
||||
relations: {
|
||||
add: async (relation: TAppDataRelation) => {
|
||||
@@ -59,12 +59,12 @@ export function useBkndData() {
|
||||
const name = crypto.randomUUID();
|
||||
const validated = parse(Type.Union(relationsSchema), relation, {
|
||||
skipMark: true,
|
||||
forceParse: true
|
||||
forceParse: true,
|
||||
});
|
||||
console.log("validated", validated);
|
||||
return await bkndActions.add("data", `relations.${name}`, validated);
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
const $data = {
|
||||
entity: (name: string) => entities[name],
|
||||
@@ -72,8 +72,8 @@ export function useBkndData() {
|
||||
system: (name: string) => ({
|
||||
any: entities[name]?.type === "system",
|
||||
users: name === config.auth.entity_name,
|
||||
media: name === config.media.entity_name
|
||||
})
|
||||
media: name === config.media.entity_name,
|
||||
}),
|
||||
};
|
||||
|
||||
return {
|
||||
@@ -82,7 +82,7 @@ export function useBkndData() {
|
||||
relations: app.relations,
|
||||
config: config.data,
|
||||
schema: schema.data,
|
||||
actions
|
||||
actions,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ const modals = {
|
||||
createEntity: () =>
|
||||
bkndModals.open(bkndModals.ids.dataCreate, {
|
||||
initialPath: ["entities", "entity"],
|
||||
initialState: { action: "entity" }
|
||||
initialState: { action: "entity" },
|
||||
}),
|
||||
createRelation: (entity?: string) =>
|
||||
bkndModals.open(bkndModals.ids.dataCreate, {
|
||||
@@ -99,9 +99,9 @@ const modals = {
|
||||
initialState: {
|
||||
action: "relation",
|
||||
relations: {
|
||||
create: [{ source: entity, type: "n:1" } as any]
|
||||
}
|
||||
}
|
||||
create: [{ source: entity, type: "n:1" } as any],
|
||||
},
|
||||
},
|
||||
}),
|
||||
createMedia: (entity?: string) =>
|
||||
bkndModals.open(bkndModals.ids.dataCreate, {
|
||||
@@ -109,10 +109,10 @@ const modals = {
|
||||
initialState: {
|
||||
action: "template-media",
|
||||
initial: {
|
||||
entity
|
||||
}
|
||||
}
|
||||
})
|
||||
entity,
|
||||
},
|
||||
},
|
||||
}),
|
||||
};
|
||||
|
||||
function entityFieldActions(bkndActions: TSchemaActions, entityName: string) {
|
||||
@@ -121,7 +121,7 @@ function entityFieldActions(bkndActions: TSchemaActions, entityName: string) {
|
||||
console.log("create field", { name, field });
|
||||
const validated = parse(fieldsSchema, field, {
|
||||
skipMark: true,
|
||||
forceParse: true
|
||||
forceParse: true,
|
||||
});
|
||||
console.log("validated", validated);
|
||||
return await bkndActions.add("data", `entities.${entityName}.fields.${name}`, validated);
|
||||
@@ -132,12 +132,12 @@ function entityFieldActions(bkndActions: TSchemaActions, entityName: string) {
|
||||
try {
|
||||
const validated = parse(entityFields, fields, {
|
||||
skipMark: true,
|
||||
forceParse: true
|
||||
forceParse: true,
|
||||
});
|
||||
const res = await bkndActions.overwrite(
|
||||
"data",
|
||||
`entities.${entityName}.fields`,
|
||||
validated
|
||||
validated,
|
||||
);
|
||||
console.log("res", res);
|
||||
//bkndActions.set("data", "entities", fields);
|
||||
@@ -149,6 +149,6 @@ function entityFieldActions(bkndActions: TSchemaActions, entityName: string) {
|
||||
alert("An error occured, check console. There will be nice error handling soon.");
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -13,8 +13,8 @@ export function useFlows() {
|
||||
console.log("parsed", parsed);
|
||||
const res = await bkndActions.add("flows", `flows.${name}`, parsed);
|
||||
console.log("res", res);
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
return { flows: app.flows, config: config.flows, actions };
|
||||
|
||||
@@ -13,8 +13,8 @@ export function useBkndMedia() {
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
const $media = {};
|
||||
|
||||
@@ -22,6 +22,6 @@ export function useBkndMedia() {
|
||||
$media,
|
||||
config: config.media,
|
||||
schema: schema.media,
|
||||
actions
|
||||
actions,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -9,15 +9,15 @@ export function useBkndSystem() {
|
||||
theme: {
|
||||
set: async (scheme: "light" | "dark") => {
|
||||
return await bkndActions.patch("server", "admin", {
|
||||
color_scheme: scheme
|
||||
color_scheme: scheme,
|
||||
});
|
||||
},
|
||||
toggle: async () => {
|
||||
return await bkndActions.patch("server", "admin", {
|
||||
color_scheme: theme === "light" ? "dark" : "light"
|
||||
color_scheme: theme === "light" ? "dark" : "light",
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
const $system = {};
|
||||
|
||||
@@ -26,7 +26,7 @@ export function useBkndSystem() {
|
||||
config: config.server,
|
||||
schema: schema.server,
|
||||
theme,
|
||||
actions
|
||||
actions,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -36,6 +36,6 @@ export function useBkndSystemTheme() {
|
||||
return {
|
||||
theme: $sys.theme,
|
||||
set: $sys.actions.theme.set,
|
||||
toggle: () => $sys.actions.theme.toggle()
|
||||
toggle: () => $sys.actions.theme.toggle(),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -14,9 +14,9 @@ export function useSetTheme(initialTheme: AppTheme = "light") {
|
||||
fetch("/api/system/config/patch/server/admin", {
|
||||
method: "PATCH",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({ color_scheme: newTheme })
|
||||
body: JSON.stringify({ color_scheme: newTheme }),
|
||||
})
|
||||
.then((res) => res.json())
|
||||
.then((data) => {
|
||||
|
||||
Reference in New Issue
Block a user