Skip to content

Commit

Permalink
api interface (#1)
Browse files Browse the repository at this point in the history
* add api foundation

* install cfl package

* fix configs

* quick save

* add remaining api endpoints

* use new package

* remove pointless include

* fix: tag types and update arg

* install new package version

* ignore vscode files

* fix urls
  • Loading branch information
SKairinos authored Jun 4, 2024
1 parent 9677678 commit 6ea918c
Show file tree
Hide file tree
Showing 15 changed files with 1,945 additions and 763 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VITE_API_BASE_URL=REPLACE_ME
27 changes: 20 additions & 7 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@
"project": true,
"tsconfigRootDir": "./"
},
"plugins": ["@typescript-eslint"],
"plugins": [
"@typescript-eslint"
],
"root": true,
"ignorePatterns": ["dist"],
"ignorePatterns": [
"dist"
],
"rules": {
"@typescript-eslint/consistent-type-imports": [
2,
{
"fixStyle": "separate-type-imports"
"fixStyle": "inline-type-imports"
}
],
"@typescript-eslint/no-restricted-imports": [
Expand All @@ -26,7 +30,11 @@
"paths": [
{
"name": "react-redux",
"importNames": ["useSelector", "useStore", "useDispatch"],
"importNames": [
"useSelector",
"useStore",
"useDispatch"
],
"message": "Please use pre-typed versions from `src/app/hooks.ts` instead."
}
]
Expand All @@ -35,13 +43,18 @@
},
"overrides": [
{
"files": ["*.{c,m,}{t,j}s", "*.{t,j}sx"]
"files": [
"*.{c,m,}{t,j}s",
"*.{t,j}sx"
]
},
{
"files": ["*{test,spec}.{t,j}s?(x)"],
"files": [
"*{test,spec}.{t,j}s?(x)"
],
"env": {
"jest": true
}
}
]
}
}
4 changes: 2 additions & 2 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
**/*.json
**/*.yaml
**/*.yml
**/*.code-workspace
**/*.code-snippets
**/*.code-*
**/*.md
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
},
"dependencies": {
"@reduxjs/toolkit": "^2.0.1",
"codeforlife": "github:ocadotechnology/codeforlife-package-javascript#v1.27.7",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-redux": "^9.1.0",
"react-router-dom": "^6.23.1",
"serve": "^14.2.3"
},
"devDependencies": {
Expand Down
50 changes: 50 additions & 0 deletions src/api/authFactor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { urls, type AuthFactor } from "codeforlife/lib/esm/api"
import {
buildUrl,
tagData,
type CreateArg,
type CreateResult,
type DestroyArg,
type DestroyResult,
type ListArg,
type ListResult,
} from "codeforlife/lib/esm/helpers/rtkQuery"

import api from "."

const authFactorApi = api.injectEndpoints({
endpoints: build => ({
createAuthFactor: build.mutation<
CreateResult<AuthFactor>,
CreateArg<AuthFactor, "type">
>({
query: body => ({
url: urls.authFactor.list,
method: "POST",
body,
}),
}),
destroyAuthFactor: build.mutation<DestroyResult, DestroyArg<AuthFactor>>({
query: id => ({
url: buildUrl(urls.authFactor.detail, { url: { id } }),
method: "DELETE",
}),
invalidatesTags: tagData("AuthFactor"),
}),
listAuthFactors: build.query<ListResult<AuthFactor, "type">, ListArg>({
query: search => ({
url: buildUrl(urls.authFactor.list, { search }),
method: "GET",
}),
providesTags: tagData("AuthFactor"),
}),
}),
})

export default authFactorApi
export const {
useCreateAuthFactorMutation,
useDestroyAuthFactorMutation,
useListAuthFactorsQuery,
useLazyListAuthFactorsQuery,
} = authFactorApi
12 changes: 12 additions & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react"
import { tagTypes } from "codeforlife/lib/esm/api"

const api = createApi({
baseQuery: fetchBaseQuery({
baseUrl: import.meta.env.VITE_API_BASE_URL,
}),
tagTypes: [...tagTypes, "SchoolTeacherInvitation"],
endpoints: () => ({}),
})

export default api
103 changes: 103 additions & 0 deletions src/api/klass.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { urls, type Class } from "codeforlife/lib/esm/api"
import {
buildUrl,
tagData,
type CreateArg,
type CreateResult,
type DestroyArg,
type DestroyResult,
type ListArg,
type ListResult,
type RetrieveArg,
type RetrieveResult,
type UpdateArg,
type UpdateResult,
} from "codeforlife/lib/esm/helpers/rtkQuery"

import api from "."

const classApi = api.injectEndpoints({
endpoints: build => ({
createClass: build.mutation<
CreateResult<Class>,
CreateArg<
Class,
"name" | "read_classmates_data",
"teacher" | "receive_requests_until"
>
>({
query: body => ({
url: urls.class.list,
method: "POST",
body,
}),
}),
destroyClass: build.mutation<DestroyResult, DestroyArg<Class>>({
query: id => ({
url: buildUrl(urls.class.detail, { url: { id } }),
method: "DELETE",
}),
invalidatesTags: tagData("Class"),
}),
updateClass: build.mutation<
UpdateResult<Class>,
UpdateArg<
Class,
never,
"name" | "read_classmates_data" | "receive_requests_until" | "teacher"
>
>({
query: ([id, body]) => ({
url: buildUrl(urls.class.detail, { url: { id } }),
method: "PATCH",
body,
}),
invalidatesTags: tagData("Class"),
}),
retrieveClass: build.query<
RetrieveResult<
Class,
| "name"
| "read_classmates_data"
| "receive_requests_until"
| "school"
| "teacher"
>,
RetrieveArg<Class>
>({
query: id => ({
url: buildUrl(urls.class.detail, { url: { id } }),
method: "GET",
}),
providesTags: tagData("Class"),
}),
listClasses: build.query<
ListResult<
Class,
| "name"
| "read_classmates_data"
| "receive_requests_until"
| "school"
| "teacher"
>,
ListArg
>({
query: search => ({
url: buildUrl(urls.class.list, { search }),
method: "GET",
}),
providesTags: tagData("Class"),
}),
}),
})

export default classApi
export const {
useCreateClassMutation,
useDestroyClassMutation,
useUpdateClassMutation,
useRetrieveClassQuery,
useLazyRetrieveClassQuery,
useListClassesQuery,
useLazyListClassesQuery,
} = classApi
17 changes: 17 additions & 0 deletions src/api/otpBypassToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { urls } from "codeforlife/lib/esm/api"

import api from "."

const otpBypassTokenApi = api.injectEndpoints({
endpoints: build => ({
generateOtpBypassTokens: build.mutation<string[], null>({
query: () => ({
url: urls.otpBypassToken.list,
method: "POST",
}),
}),
}),
})

export default otpBypassTokenApi
export const { useGenerateOtpBypassTokensMutation } = otpBypassTokenApi
57 changes: 57 additions & 0 deletions src/api/school.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { urls, type School } from "codeforlife/lib/esm/api"
import {
buildUrl,
tagData,
type CreateArg,
type CreateResult,
type RetrieveArg,
type RetrieveResult,
type UpdateArg,
type UpdateResult,
} from "codeforlife/lib/esm/helpers/rtkQuery"

import api from "."

const schoolApi = api.injectEndpoints({
endpoints: build => ({
retrieveSchool: build.query<
RetrieveResult<School, "name" | "country" | "uk_county">,
RetrieveArg<School>
>({
query: id => ({
url: buildUrl(urls.school.detail, { url: { id } }),
method: "GET",
}),
providesTags: tagData("School"),
}),
createSchool: build.mutation<
CreateResult<School>,
CreateArg<School, "name", "country" | "uk_county">
>({
query: body => ({
url: urls.school.list,
method: "POST",
body,
}),
}),
updateSchool: build.mutation<
UpdateResult<School>,
UpdateArg<School, never, "name" | "country" | "uk_county">
>({
query: ([id, body]) => ({
url: buildUrl(urls.school.detail, { url: { id } }),
method: "PATCH",
body,
}),
invalidatesTags: tagData("School"),
}),
}),
})

export default schoolApi
export const {
useRetrieveSchoolQuery,
useLazyRetrieveSchoolQuery,
useCreateSchoolMutation,
useUpdateSchoolMutation,
} = schoolApi
Loading

0 comments on commit 6ea918c

Please sign in to comment.