Factorize service accessors

This commit is contained in:
pikiou 2021-12-08 11:06:27 +01:00
parent 1844c6acad
commit c64bf376f8
5 changed files with 152 additions and 145 deletions

View File

@ -6,12 +6,14 @@ import { axiosConfig } from "./auth"
export type ElementWithId = unknown & { id: number }
export type ElementTranslation = { [englishProp: string]: string }
export type ElementTranslation<Element> = { [k in keyof Element]: string }
export function get<Element>(
elementName: string,
translation: ElementTranslation
): (id: number) => Promise<{
export default function getServiceAccessors<
// eslint-disable-next-line @typescript-eslint/ban-types
ElementNoId extends object,
Element extends ElementNoId & ElementWithId
>(elementName: string, translation: { [k in keyof Element]: string }): any {
function get(): (id: number) => Promise<{
data?: Element
error?: Error
}> {
@ -39,10 +41,7 @@ export function get<Element>(
}
}
export function listGet<Element>(
elementName: string,
translation: ElementTranslation
): () => Promise<{
function listGet(): () => Promise<{
data?: Element[]
error?: Error
}> {
@ -52,7 +51,10 @@ export function listGet<Element>(
}
return async (): Promise<ElementListGetResponse> => {
try {
const { data } = await axios.get(`${config.API_URL}/${elementName}ListGet`, axiosConfig)
const { data } = await axios.get(
`${config.API_URL}/${elementName}ListGet`,
axiosConfig
)
if (!data) {
return { data }
}
@ -72,10 +74,7 @@ export function listGet<Element>(
}
// eslint-disable-next-line @typescript-eslint/ban-types
export function add<ElementNoId extends object, Element extends ElementNoId & ElementWithId>(
elementName: string,
translation: ElementTranslation
): (volunteerWithoutId: ElementNoId) => Promise<{
function add(): (volunteerWithoutId: ElementNoId) => Promise<{
data?: Element
error?: Error
}> {
@ -112,10 +111,7 @@ export function add<ElementNoId extends object, Element extends ElementNoId & El
}
}
export function set<Element>(
elementName: string,
translation: ElementTranslation
): (volunteer: Element) => Promise<{
function set(): (volunteer: Element) => Promise<{
data?: Element
error?: Error
}> {
@ -150,3 +146,6 @@ export function set<Element>(
}
}
}
return { listGet, get, set, add }
}

View File

@ -1,4 +1,4 @@
import { get, listGet, add, set } from "./accessors"
import getServiceAccessors from "./accessors"
export class JavGame {
id = 0
@ -54,10 +54,12 @@ const elementName = "JavGame"
export type JavGameWithoutId = Omit<JavGame, "id">
export const javGameGet = get<JavGame>(elementName, translationJavGame)
const { listGet, get, set, add } = getServiceAccessors<JavGameWithoutId, JavGame>(
elementName,
translationJavGame
)
export const javGameListGet = listGet<JavGame>(elementName, translationJavGame)
export const javGameAdd = add<JavGameWithoutId, JavGame>(elementName, translationJavGame)
export const javGameSet = set<JavGame>(elementName, translationJavGame)
export const javGameListGet = listGet()
export const javGameGet = get()
export const javGameAdd = add()
export const javGameSet = set()

View File

@ -1,4 +1,4 @@
import { get, listGet, add, set } from "./accessors"
import getServiceAccessors from "./accessors"
export class PreMember {
id = 0
@ -30,10 +30,12 @@ const elementName = "PreMember"
export type PreMemberWithoutId = Omit<PreMember, "id">
export const preMemberGet = get<PreMember>(elementName, translationPreMember)
const { listGet, get, set, add } = getServiceAccessors<PreMemberWithoutId, PreMember>(
elementName,
translationPreMember
)
export const preMemberListGet = listGet<PreMember>(elementName, translationPreMember)
export const preMemberAdd = add<PreMemberWithoutId, PreMember>(elementName, translationPreMember)
export const preMemberSet = set<PreMember>(elementName, translationPreMember)
export const preMemberListGet = listGet()
export const preMemberGet = get()
export const preMemberAdd = add()
export const preMemberSet = set()

View File

@ -1,4 +1,4 @@
import { get, listGet, add, set } from "./accessors"
import getServiceAccessors from "./accessors"
export class Volunteer {
id = 0
@ -60,10 +60,12 @@ export interface MemberLogin {
export type VolunteerWithoutId = Omit<Volunteer, "id">
export const volunteerGet = get<Volunteer>(elementName, translationMember)
const { listGet, get, set, add } = getServiceAccessors<VolunteerWithoutId, Volunteer>(
elementName,
translationMember
)
export const volunteerListGet = listGet<Volunteer>(elementName, translationMember)
export const volunteerAdd = add<VolunteerWithoutId, Volunteer>(elementName, translationMember)
export const volunteerSet = set<Volunteer>(elementName, translationMember)
export const volunteerListGet = listGet()
export const volunteerGet = get()
export const volunteerAdd = add()
export const volunteerSet = set()

View File

@ -1,4 +1,4 @@
import { get, listGet, add, set } from "./accessors"
import getServiceAccessors from "./accessors"
export class Wish {
id = 0
@ -27,10 +27,12 @@ const elementName = "Wish"
export type WishWithoutId = Omit<Wish, "id">
export const wishGet = get<Wish>(elementName, translationWish)
const { listGet, get, set, add } = getServiceAccessors<WishWithoutId, Wish>(
elementName,
translationWish
)
export const wishListGet = listGet<Wish>(elementName, translationWish)
export const wishAdd = add<WishWithoutId, Wish>(elementName, translationWish)
export const wishSet = set<Wish>(elementName, translationWish)
export const wishListGet = listGet()
export const wishGet = get()
export const wishAdd = add()
export const wishSet = set()