From 0c79013e47900f775039578585ff59d5599d59d9 Mon Sep 17 00:00:00 2001 From: SKairinos Date: Wed, 14 Aug 2024 14:44:09 +0000 Subject: [PATCH] api --- src/api/fruit.ts | 108 +++++++++++++++++++++++++++++++++++++++++++++++ src/api/index.ts | 8 ++++ 2 files changed, 116 insertions(+) create mode 100644 src/api/fruit.ts create mode 100644 src/api/index.ts diff --git a/src/api/fruit.ts b/src/api/fruit.ts new file mode 100644 index 0000000..4f69268 --- /dev/null +++ b/src/api/fruit.ts @@ -0,0 +1,108 @@ +import { + type CreateArg, + type CreateResult, + type DestroyArg, + type DestroyResult, + type ListArg, + type ListResult, + type Model, + type RetrieveArg, + type RetrieveResult, + type UpdateArg, + type UpdateResult, + buildUrl, + modelUrls, + tagData, +} from "codeforlife/utils/api" + +import api from "." + +export type Fruit = Model< + number, + { + name: string + is_citrus: boolean + expires_on: Date + } +> + +const fruitUrls = modelUrls("fruits/", "fruits//") + +export type RetrieveFruitResult = RetrieveResult< + Fruit, + "name" | "is_citrus" | "expires_on" +> +export type RetrieveFruitArg = RetrieveArg + +export type ListFruitsResult = ListResult< + Fruit, + "name" | "is_citrus" | "expires_on" +> +export type ListFruitsArg = ListArg + +export type CreateFruitResult = CreateResult< + Fruit, + "name" | "is_citrus" | "expires_on" +> +export type CreateFruitArg = CreateArg + +export type UpdateFruitResult = UpdateResult< + Fruit, + "name" | "is_citrus" | "expires_on" +> +export type UpdateFruitArg = UpdateArg + +export type DestroyFruitResult = DestroyResult +export type DestroyFruitArg = DestroyArg + +const fruitApi = api.injectEndpoints({ + endpoints: build => ({ + retrieveFruit: build.query({ + query: id => ({ + url: buildUrl(fruitUrls.detail, { url: { id } }), + method: "GET", + }), + providesTags: tagData("Fruit"), + }), + listFruits: build.query({ + query: search => ({ + url: buildUrl(fruitUrls.list, { search }), + method: "GET", + }), + providesTags: tagData("Fruit"), + }), + createFruit: build.mutation({ + query: body => ({ + url: fruitUrls.list, + method: "POST", + body, + }), + }), + updateFruit: build.mutation({ + query: ({ id, ...body }) => ({ + url: buildUrl(fruitUrls.detail, { url: { id } }), + method: "PATCH", + body, + }), + invalidatesTags: tagData("Fruit"), + }), + destroyFruit: build.mutation({ + query: id => ({ + url: buildUrl(fruitUrls.detail, { url: { id } }), + method: "DELETE", + }), + invalidatesTags: tagData("Fruit"), + }), + }), +}) + +export default fruitApi +export const { + useRetrieveFruitQuery, + useLazyRetrieveFruitQuery, + useListFruitsQuery, + useLazyListFruitsQuery, + useCreateFruitMutation, + useUpdateFruitMutation, + useDestroyFruitMutation, +} = fruitApi diff --git a/src/api/index.ts b/src/api/index.ts new file mode 100644 index 0000000..47e17d8 --- /dev/null +++ b/src/api/index.ts @@ -0,0 +1,8 @@ +import { createApi } from "codeforlife/api" + +const api = createApi({ + tagTypes: ["Fruit"], +}) + +export default api +export const { useLogoutMutation } = api