Skip to main content

useGetLocalResourceSchema

Fetches a resource's full schema and answers what that resource can do.

Use it when a custom component has to branch on the backend's capabilities — whether a resource has an edit view, whether the delete endpoint exists. Inside the generated views the schema is already in context.

Signature​

import {useGetLocalResourceSchema} from 'habarico';

const result = useGetLocalResourceSchema({
resource, // string — "serviceName/resourceName"; defaults to the resource context
schema, // LocalResourceSchema — when given, no request is made
actionName, // string — fetches the schema of one action
recordId, // string | number — fetches the per-record schema
params, // Record<string, string | number | boolean> — extra query params
});

Parameters​

NameTypeDefaultDescription
resourcestringresource from contextResource in "service/resource" form
schemaLocalResourceSchema—Skips the request and uses this schema
actionNamestring—Requests the schema of a specific action
recordIdstring | number—Requests the schema for one record — this is how per-record actions and permissions arrive
paramsRecord<string, string | number | boolean>—Appended to the schema request

Returns​

NameTypeDescription
schemaLocalResourceSchema | undefinedThe full schema
isLoading / isErrorbooleanRequest state
errorunknownThe failure, if any
hasListView, hasCreateView, hasEditView, hasShowViewbooleanWhether schema.views contains that view
hasListAction, hasCreateAction, hasEditAction, hasShowAction, hasDeleteActionbooleanWhether schema.actions exposes that action
hasGetEndpoint, hasPostEndpoint, hasPutEndpoint, hasPatchEndpoint, hasDeleteEndpointbooleanWhether the backend allows that HTTP method
getViewSchema(name: string) => ViewSchema | undefinedOne view by name
getMethodSchema(method: RequestMethodType) => MethodSchema | undefinedThe field set of one method
getFieldSchema(method: RequestMethodType, fieldName: string) => FieldSchema | undefinedOne field of one method

Example​

const {hasEditView, hasDeleteAction, isLoading} = useGetLocalResourceSchema({
resource: 'eshop/product',
});

if (isLoading) return <Loading />;

return (
<>
{hasEditView && <EditButton />}
{hasDeleteAction && <DeleteButton />}
</>
);
caution

The has*View / has*Action / has*Endpoint flags are derived from the fetched schema only. Pass a schema prop and they all stay false — the flags describe what the request returned, not what you handed in.

caution

The request is cached per resource, record, locale, action and identity — a schema fetched for one user is never reused for another, because permissions can change what the schema contains. The query stays disabled until the identity is known, so isLoading is true for a moment on every hard reload.

A resource that resolves to no entry in the service schemas — a typo, or a resource the backend does not expose — surfaces as isError once the service schemas are loaded, rather than silently returning an empty schema.

See also​