Skip to content

Reference Data

@granit/reference-data provides framework-agnostic types for reference data entities — mirroring Granit.ReferenceData on the .NET backend. Currently covers ISO 3166-1 countries with multilingual labels.

@granit/react-reference-data wraps these into TanStack Query hooks with separate read and admin endpoints for query/mutation operations.

Peer dependencies: axios, react ^19, @tanstack/react-query ^5

  • Directory@granit/reference-data/ Country types and list params (framework-agnostic)
    • @granit/react-reference-data Query/mutation hooks for country CRUD
PackageRoleDepends on
@granit/reference-dataCountry, CountriesListParams
@granit/react-reference-datauseCountries, useCountry, useCreateCountry, useUpdateCountry, useDeactivateCountry, useReactivateCountry@granit/reference-data, @tanstack/react-query, axios, react
import { useCountries, useCountry } from '@granit/react-reference-data';
import { api } from './api-client';
function CountrySelect() {
const { data: countries, isLoading } = useCountries({
client: api,
params: { isActive: true },
});
// Render country options...
}
interface Country {
readonly code: string; // ISO 3166-1 alpha-2
readonly alpha3: string; // ISO 3166-1 alpha-3
readonly numericCode: string; // ISO 3166-1 numeric
readonly labelEn: string;
readonly labelFr: string;
readonly labelNl: string;
readonly labelDe: string;
readonly officialName: string;
readonly nativeName: string;
readonly region: string;
readonly subRegion: string;
readonly phoneCode: string;
readonly sortOrder: number;
readonly isActive: boolean;
readonly validFrom: string | null;
readonly validTo: string | null;
readonly createdAt: string;
readonly updatedAt: string;
}
interface CountriesListParams {
readonly search?: string;
readonly page?: number;
readonly pageSize?: number;
readonly sortBy?: string;
readonly desc?: boolean;
readonly region?: string;
readonly isActive?: boolean;
}
interface ReferenceDataHookOptions {
client: AxiosInstance;
basePath?: string; // default: '/api/v1/reference-data/countries'
}
interface ReferenceDataMutationOptions {
client: AxiosInstance;
basePath?: string; // default: '/api/v1/admin/reference-data/countries'
}

Fetches countries with optional filtering, search, pagination, and sorting.

function useCountries(
options: ReferenceDataHookOptions & { params?: CountriesListParams; enabled?: boolean }
): UseQueryResult<Country[]>;

Fetches a single country by ISO 3166-1 alpha-2 code.

function useCountry(
code: string,
options: ReferenceDataHookOptions & { enabled?: boolean }
): UseQueryResult<Country>;

All mutations invalidate the country list and detail queries on success.

type CreateCountryPayload = Omit<Country, 'createdAt' | 'updatedAt'>;
function useCreateCountry(
options: ReferenceDataMutationOptions
): UseMutationResult<Country, Error, CreateCountryPayload>;
interface UpdateCountryPayload {
code: string;
data: Partial<Omit<Country, 'code' | 'createdAt' | 'updatedAt'>>;
}
function useUpdateCountry(
options: ReferenceDataMutationOptions
): UseMutationResult<Country, Error, UpdateCountryPayload>;

Soft-deletes a country by code.

function useDeactivateCountry(
options: ReferenceDataMutationOptions
): UseMutationResult<void, Error, string>;

Reactivates a previously deactivated country.

function useReactivateCountry(
options: ReferenceDataMutationOptions
): UseMutationResult<Country, Error, string>;
const countryKeys = {
all: ['reference-data', 'countries'] as const,
list: (params?: CountriesListParams) => [...countryKeys.all, 'list', params] as const,
detail: (code: string) => [...countryKeys.all, 'detail', code] as const,
};
CategoryKey exportsPackage
TypesCountry, CountriesListParams@granit/reference-data
Query hooksuseCountries(), useCountry()@granit/react-reference-data
Mutation hooksuseCreateCountry(), useUpdateCountry(), useDeactivateCountry(), useReactivateCountry()@granit/react-reference-data
Cache keyscountryKeys@granit/react-reference-data