1.6 KiB
1.6 KiB
Reference Data Layer
App-wide API-backed lookup datasets reused across modules — countries, languages, currencies, time zones, common option dictionaries. The layer fetches them, exposes reusable query hooks, and owns their cache keys.
Small dataset → one file; larger → a folder:
refdata/
├── countries.ts
├── languages.ts
└── currencies/
├── api.ts
├── dto.ts
├── mapper.ts
├── queries.ts
└── keys.ts
Rules
- App-wide datasets only — a dataset owned by one feature belongs in that module's
data/layer. - May use
#infrastructure/axiosand#infrastructure/reactQuery; must not import fromview/,pages/, or feature modules. - Expose hooks or small service functions — consumers should not know endpoints or cache keys.
- Add mappers when backend DTOs differ from app-facing data, tested like module data mappers.
Example
import { keepPreviousData, useQuery } from "@tanstack/react-query";
import { axiosClient } from "#infrastructure/axios";
import { createKeyBuilder } from "#infrastructure/reactQuery";
export const countriesCacheKey = createKeyBuilder(
"REF_DATA",
"COUNTRIES",
"LIST",
);
interface CountryData {
code: string;
name: string;
}
export const useCountries = () => {
return useQuery({
queryKey: countriesCacheKey.store(),
placeholderData: keepPreviousData,
queryFn: async () => {
return axiosClient
.get<Array<CountryData>>("/datasets/countries")
.then((data) => data.data);
},
});
};