Skip to content

Commit

Permalink
return result and arg types
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Jul 22, 2024
1 parent fc1b1b7 commit 07cea0e
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 144 deletions.
134 changes: 0 additions & 134 deletions src/api/endpoints.ts

This file was deleted.

30 changes: 30 additions & 0 deletions src/api/endpoints/authFactor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { type EndpointBuilder } from "@reduxjs/toolkit/query/react"

import {
buildUrl,
tagData,
type ListArg,
type ListResult,
} from "../../utils/api"
import type { AuthFactor } from "../models"
import { type TagTypes } from "../tagTypes"
import urls from "../urls"

export const AUTH_FACTOR_TAG: TagTypes = "AuthFactor"

export type ListAuthFactorsResult = ListResult<AuthFactor, "type">
export type ListAuthFactorsArg = ListArg

export default function getReadAuthFactorEndpoints(
build: EndpointBuilder<any, any, any>,
) {
return {
listAuthFactors: build.query<ListAuthFactorsResult, ListAuthFactorsArg>({
query: search => ({
url: buildUrl(urls.authFactor.list, { search }),
method: "GET",
}),
providesTags: tagData(AUTH_FACTOR_TAG),
}),
}
}
47 changes: 47 additions & 0 deletions src/api/endpoints/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import getReadAuthFactorEndpoints, {
type ListAuthFactorsArg,
type ListAuthFactorsResult,
AUTH_FACTOR_TAG,
} from "./authFactor"
import getReadClassEndpoints, {
type ListClassesArg,
type ListClassesResult,
type RetrieveClassArg,
type RetrieveClassResult,
CLASS_TAG,
} from "./klass"
import getReadSchoolEndpoints, {
type RetrieveSchoolArg,
type RetrieveSchoolResult,
SCHOOL_TAG,
} from "./school"
import getReadUserEndpoints, {
type ListUsersArg,
type ListUsersResult,
type RetrieveUserArg,
type RetrieveUserResult,
USER_TAG,
} from "./user"

export {
AUTH_FACTOR_TAG,
CLASS_TAG,
getReadAuthFactorEndpoints,
getReadClassEndpoints,
getReadSchoolEndpoints,
getReadUserEndpoints,
SCHOOL_TAG,
USER_TAG,
type ListAuthFactorsArg,
type ListAuthFactorsResult,
type ListClassesArg,
type ListClassesResult,
type ListUsersArg,
type ListUsersResult,
type RetrieveClassArg,
type RetrieveClassResult,
type RetrieveSchoolArg,
type RetrieveSchoolResult,
type RetrieveUserArg,
type RetrieveUserResult,
}
56 changes: 56 additions & 0 deletions src/api/endpoints/klass.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { type EndpointBuilder } from "@reduxjs/toolkit/query/react"

import {
buildUrl,
tagData,
type ListArg,
type ListResult,
type RetrieveArg,
type RetrieveResult,
} from "../../utils/api"
import type { Class } from "../models"
import { type TagTypes } from "../tagTypes"
import urls from "../urls"

export const CLASS_TAG: TagTypes = "Class"

export type RetrieveClassResult = RetrieveResult<
Class,
| "name"
| "read_classmates_data"
| "receive_requests_until"
| "school"
| "teacher"
>
export type RetrieveClassArg = RetrieveArg<Class>

export type ListClassesResult = ListResult<
Class,
| "name"
| "read_classmates_data"
| "receive_requests_until"
| "school"
| "teacher"
>
export type ListClassesArg = ListArg

export default function getReadClassEndpoints(
build: EndpointBuilder<any, any, any>,
) {
return {
retrieveClass: build.query<RetrieveClassResult, RetrieveClassArg>({
query: id => ({
url: buildUrl(urls.class.detail, { url: { id } }),
method: "GET",
}),
providesTags: tagData(CLASS_TAG),
}),
listClasses: build.query<ListClassesResult, ListClassesArg>({
query: search => ({
url: buildUrl(urls.class.list, { search }),
method: "GET",
}),
providesTags: tagData(CLASS_TAG),
}),
}
}
33 changes: 33 additions & 0 deletions src/api/endpoints/school.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { type EndpointBuilder } from "@reduxjs/toolkit/query/react"

import {
buildUrl,
tagData,
type RetrieveArg,
type RetrieveResult,
} from "../../utils/api"
import type { School } from "../models"
import { type TagTypes } from "../tagTypes"
import urls from "../urls"

export const SCHOOL_TAG: TagTypes = "School"

export type RetrieveSchoolResult = RetrieveResult<
School,
"name" | "country" | "uk_county"
>
export type RetrieveSchoolArg = RetrieveArg<School>

export default function getReadSchoolEndpoints(
build: EndpointBuilder<any, any, any>,
) {
return {
retrieveSchool: build.query<RetrieveSchoolResult, RetrieveSchoolArg>({
query: id => ({
url: buildUrl(urls.school.detail, { url: { id } }),
method: "GET",
}),
providesTags: tagData(SCHOOL_TAG),
}),
}
}
62 changes: 62 additions & 0 deletions src/api/endpoints/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { type EndpointBuilder } from "@reduxjs/toolkit/query/react"

import {
buildUrl,
tagData,
type ListArg,
type ListResult,
type RetrieveArg,
type RetrieveResult,
} from "../../utils/api"
import type { User } from "../models"
import { type TagTypes } from "../tagTypes"
import urls from "../urls"

export const USER_TAG: TagTypes = "User"

export type RetrieveUserResult = RetrieveResult<
User,
| "first_name"
| "last_name"
| "email"
| "is_active"
| "date_joined"
| "requesting_to_join_class"
| "student"
| "teacher"
>
export type RetrieveUserArg = RetrieveArg<User>

export type ListUsersResult = ListResult<
User,
| "first_name"
| "last_name"
| "email"
| "is_active"
| "date_joined"
| "requesting_to_join_class"
| "student"
| "teacher"
>
export type ListUsersArg = ListArg<{ students_in_class: string }>

export default function getReadUserEndpoints(
build: EndpointBuilder<any, any, any>,
) {
return {
retrieveUser: build.query<RetrieveUserResult, RetrieveUserArg>({
query: id => ({
url: buildUrl(urls.user.detail, { url: { id } }),
method: "GET",
}),
providesTags: tagData(USER_TAG),
}),
listUsers: build.query<ListUsersResult, ListUsersArg>({
query: search => ({
url: buildUrl(urls.user.list, { search }),
method: "GET",
}),
providesTags: tagData(USER_TAG),
}),
}
}
10 changes: 0 additions & 10 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import baseQuery from "./baseQuery"
import {
getReadAuthFactorEndpoints,
getReadClassEndpoints,
getReadSchoolEndpoints,
getReadUserEndpoints,
} from "./endpoints"
import type {
AdminSchoolTeacher,
AdminSchoolTeacherUser,
Expand All @@ -30,10 +24,6 @@ import urls from "./urls"

export {
baseQuery,
getReadAuthFactorEndpoints,
getReadClassEndpoints,
getReadSchoolEndpoints,
getReadUserEndpoints,
tagTypes,
urls,
type AdminSchoolTeacher,
Expand Down

0 comments on commit 07cea0e

Please sign in to comment.