added useApi and useApiQuery to work seamlessly with SWR (tbc)

This commit is contained in:
dswbx
2024-12-12 10:37:52 +01:00
parent 43ec075a32
commit 9d9aa7b7a5
10 changed files with 72 additions and 24 deletions

View File

@@ -15,7 +15,7 @@ export class DataApi extends ModuleApi<DataApiOptions> {
};
}
async readOne(
readOne(
entity: string,
id: PrimaryFieldType,
query: Partial<Omit<RepoQuery, "where" | "limit" | "offset">> = {}
@@ -23,14 +23,14 @@ export class DataApi extends ModuleApi<DataApiOptions> {
return this.get<RepositoryResponse<EntityData>>([entity, id], query);
}
async readMany(entity: string, query: Partial<RepoQuery> = {}) {
readMany(entity: string, query: Partial<RepoQuery> = {}) {
return this.get<Pick<RepositoryResponse, "meta" | "data">>(
[entity],
query ?? this.options.defaultQuery
);
}
async readManyByReference(
readManyByReference(
entity: string,
id: PrimaryFieldType,
reference: string,
@@ -42,19 +42,19 @@ export class DataApi extends ModuleApi<DataApiOptions> {
);
}
async createOne(entity: string, input: EntityData) {
createOne(entity: string, input: EntityData) {
return this.post<RepositoryResponse<EntityData>>([entity], input);
}
async updateOne(entity: string, id: PrimaryFieldType, input: EntityData) {
updateOne(entity: string, id: PrimaryFieldType, input: EntityData) {
return this.patch<RepositoryResponse<EntityData>>([entity, id], input);
}
async deleteOne(entity: string, id: PrimaryFieldType) {
deleteOne(entity: string, id: PrimaryFieldType) {
return this.delete<RepositoryResponse<EntityData>>([entity, id]);
}
async count(entity: string, where: RepoQuery["where"] = {}) {
count(entity: string, where: RepoQuery["where"] = {}) {
return this.post<RepositoryResponse<{ entity: string; count: number }>>(
[entity, "fn", "count"],
where