Skip to content

Commit

Permalink
api
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Aug 14, 2024
1 parent 779473e commit 0c79013
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
108 changes: 108 additions & 0 deletions src/api/fruit.ts
Original file line number Diff line number Diff line change
@@ -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/<id>/")

export type RetrieveFruitResult = RetrieveResult<
Fruit,
"name" | "is_citrus" | "expires_on"
>
export type RetrieveFruitArg = RetrieveArg<Fruit>

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<Fruit, "name" | "is_citrus">

export type UpdateFruitResult = UpdateResult<
Fruit,
"name" | "is_citrus" | "expires_on"
>
export type UpdateFruitArg = UpdateArg<Fruit, never, "name" | "is_citrus">

export type DestroyFruitResult = DestroyResult
export type DestroyFruitArg = DestroyArg<Fruit>

const fruitApi = api.injectEndpoints({
endpoints: build => ({
retrieveFruit: build.query<RetrieveFruitResult, RetrieveFruitArg>({
query: id => ({
url: buildUrl(fruitUrls.detail, { url: { id } }),
method: "GET",
}),
providesTags: tagData("Fruit"),
}),
listFruits: build.query<ListFruitsResult, ListFruitsArg>({
query: search => ({
url: buildUrl(fruitUrls.list, { search }),
method: "GET",
}),
providesTags: tagData("Fruit"),
}),
createFruit: build.mutation<CreateFruitResult, CreateFruitArg>({
query: body => ({
url: fruitUrls.list,
method: "POST",
body,
}),
}),
updateFruit: build.mutation<UpdateFruitResult, UpdateFruitArg>({
query: ({ id, ...body }) => ({
url: buildUrl(fruitUrls.detail, { url: { id } }),
method: "PATCH",
body,
}),
invalidatesTags: tagData("Fruit"),
}),
destroyFruit: build.mutation<DestroyFruitResult, DestroyFruitArg>({
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
8 changes: 8 additions & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createApi } from "codeforlife/api"

const api = createApi({
tagTypes: ["Fruit"],
})

export default api
export const { useLogoutMutation } = api

0 comments on commit 0c79013

Please sign in to comment.