UNPKG

@rest-api/react-models

Version:

[![npm version](https://img.shields.io/npm/v/@rest-api/react-models)](https://www.npmjs.com/package/@rest-api/react-models) [![codecov](https://codecov.io/gh/hector7/rest-api-react-models/branch/master/graph/badge.svg)](https://codecov.io/gh/hector7/rest-

144 lines (143 loc) 7.86 kB
import { RestModel, RouteOpts } from "../index"; import { Schema, StringOrNumberKeys, DeleteFieldSchema } from "../../Schema"; import BasicSearchRestModel from './BasicSearchRestModel'; import BasicIdRestModel from "./BasicIdRestModel"; import ComplexIdRestModel from "../ComplexIdRestModel"; import ComplexSearchRestModel from "../ComplexSearchRestModel"; declare type DynamicUrl = () => string; interface DeleteFieldsType<S extends Schema<any>, IdKey extends string, GetItem, MetaData> { <K extends DeleteFieldSchema<Exclude<keyof S["RealType"], IdKey>>>(...keys: K): BasicRestModel<Schema<{ [K2 in Exclude<keyof S['RealType'], K[number]>]: S['RealType'][K2]; }, { [K2 in Exclude<keyof S['PopulatedType'], K[number]>]: S['PopulatedType'][K2]; }, { [K2 in Exclude<keyof S['FullPopulatedType'], K[number]>]: S['FullPopulatedType'][K2]; }>, any, GetItem, MetaData>; } export default class BasicRestModel<S extends Schema<any> = any, IdKey extends StringOrNumberKeys<S['RealType']> & StringOrNumberKeys<S['PopulatedType']> & string = any, GetItem = any, MetaData = any> extends RestModel<{}, S, IdKey, GetItem, MetaData> { protected basicIdRestModel: BasicIdRestModel<S, IdKey>; private basicSearchRestModel; constructor(schema: S, id: IdKey, url: string | DynamicUrl, routeOpts?: RouteOpts); constructor(schema: S, id: IdKey, url: string | DynamicUrl, itemStructure: Schema<GetItem, any, any>, getItems: (el: GetItem) => S['RealType'][], routeOpts?: RouteOpts); constructor(schema: S, id: IdKey, url: string | DynamicUrl, itemStructure: Schema<GetItem, any, any>, getItems: (el: GetItem) => S['RealType'][], getMetaData: (el: GetItem) => MetaData); constructor(schema: S, id: IdKey, url: string | DynamicUrl, itemStructure: Schema<GetItem, any, any>, getItems: (el: GetItem) => S['RealType'][], getMetaData: (el: GetItem) => MetaData, opts: RouteOpts); hiddeFields: DeleteFieldsType<S, IdKey, GetItem, MetaData>; getSubModelWithKey<k extends StringOrNumberKeys<S['RealType']> & StringOrNumberKeys<S['PopulatedType']> & string>(key: k, url?: string): BasicIdRestModel<S, k>; getSubModelWithKey<R, k extends StringOrNumberKeys<S['RealType']> & StringOrNumberKeys<S['PopulatedType']> & string>(optSchema: Schema<R>, key: k, url: (opts: R) => string): ComplexIdRestModel<R, S, k>; getSearchSubModel(url: string): BasicSearchRestModel<S, IdKey, GetItem, MetaData>; getSearchSubModel<R>(optSchema: Schema<R>, url: (opt: R) => string): ComplexSearchRestModel<R, S, IdKey, GetItem, MetaData>; /** * Hook used to fetch if needed to "/" path of model */ useFetchIfNeeded(): (queryString?: string | URLSearchParams | undefined) => void; /** * Hook used to get data (without fetch if needed). Not optimal. Refresh on every change of model. */ useSelectorGet(): (queryString?: string | undefined) => NonNullable<S["RealType"]>[]; /** * Hook used to fetch and fetch submodels with idOnly if needed to "/" path of model */ useFetchPopulatedIfNeeded(): (queryString?: string | URLSearchParams | undefined) => void; /** * Hook used to get populated data (without fetch if needed). Not optimal. Refresh on every change of model. */ useSelectorGetPopulated(): (queryString?: string | undefined) => NonNullable<S["PopulatedType"]>[]; /** * Hook used to get id data (without fetch if needed). Not optimal. Refresh on every change of model. */ useSelectorGetById(): (id: S["RealType"][IdKey]) => S["PopulatedType"] | null; /** * Hook used to fetch if needed to "/:id" path of model */ useFetchByIdIfNeeded(): (id: S["RealType"][IdKey]) => void; /** * Hook used to get populated id data (without fetch if needed). Not optimal. Refresh on every change of model. */ useSelectorGetByIdPopulated(): (id: S["RealType"][IdKey]) => S["PopulatedType"] | null; /** * Hook used to fetch and fetch submodels with idOnly if needed to "/:id" path of model */ useFetchByIdPopulatedIfNeeded(): (id: S["PopulatedType"][IdKey]) => void; /** * Hook used to get the result if there are from path "/". * Internally use the hook useFetchIfNeeded with the querystring provided. */ get useGet(): (queryString?: string | URLSearchParams | undefined) => { reload: () => void; error: import("../../../..").HttpError | null; initialized: boolean; invalidated: boolean; loading: boolean; items: NonNullable<S["RealType"]>[]; metadata: MetaData | null; }; /** * Uset to invalidate all requests */ get useInvalidateAll(): () => () => void; /** * Hook used to get the result populated (populating models with idOnly if there are) if there are from path "/". * Internally use the hook useFetchPopulatedIfNeeded with the querystring provided. */ get useGetPopulated(): (queryString?: string | URLSearchParams | undefined) => { reload: () => void; invalidated: boolean; error: import("../../../..").HttpError | null; initialized: boolean; loading: boolean; metadata: MetaData | null; populated: true; items: S["FullPopulatedType"][]; }; /** * Hook used to get the result if there are from path "/:id". * Internally use the hook useFetchByIdIfNeeded with the id provided. */ get useGetById(): (id: S["RealType"][IdKey]) => { redirect: () => void; item: S["RealType"] | null; initialized: boolean; loading: boolean; invalidated: boolean; error: import("../../../..").HttpError | null; }; /** * Hook used to get the result if there are from path "/". * Internally use the hook useFetchByIdPopulatedIfNeeded with the id provided. */ get useGetByIdPopulated(): (id: S["PopulatedType"][IdKey]) => { redirect: () => void; populated: true; invalidated: boolean; initialized: boolean; error: import("../../../..").HttpError | null; loading: boolean; item: S["FullPopulatedType"]; }; /** * Used to change model: post, put, patch and delete */ useModificators(): { post: (item: FormData | Pick<S["RealType"], Exclude<keyof S["RealType"], IdKey>>, callback: import("../../../..").Callback<S["RealType"], import("../../../..").HttpError>) => any; put: (id: S["RealType"][IdKey], item: FormData | S["RealType"], callback: import("../../../..").Callback<S["RealType"], import("../../../..").HttpError>) => any; patch: (id: S["RealType"][IdKey], item: FormData | Partial<S["RealType"]>, callback: import("../../../..").Callback<S["RealType"], import("../../../..").HttpError>) => any; remove: (item: S["RealType"], callback: import("../../../..").Callback<undefined, import("../../../..").HttpError>) => any; }; /** * Used to post a entry model */ usePost(): (item: FormData | Pick<S["RealType"], Exclude<keyof S["RealType"], IdKey>>, callback: import("../../../..").Callback<S["RealType"], import("../../../..").HttpError>) => any; /** * Used to put a entry model */ usePut(): (id: S["RealType"][IdKey], item: FormData | S["RealType"], callback: import("../../../..").Callback<S["RealType"], import("../../../..").HttpError>) => any; /** * Used to patch a entry model */ usePatch(): (id: S["RealType"][IdKey], item: FormData | Partial<S["RealType"]>, callback: import("../../../..").Callback<S["RealType"], import("../../../..").HttpError>) => any; /** * Used to delete a entry model */ useDelete(): (item: S["RealType"], callback: import("../../../..").Callback<undefined, import("../../../..").HttpError>) => any; } export {};