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,15 +6,17 @@ import { axiosConfig } from "./auth"
export type ElementWithId = unknown & { id: number } 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>( export default function getServiceAccessors<
elementName: string, // eslint-disable-next-line @typescript-eslint/ban-types
translation: ElementTranslation ElementNoId extends object,
): (id: number) => Promise<{ Element extends ElementNoId & ElementWithId
>(elementName: string, translation: { [k in keyof Element]: string }): any {
function get(): (id: number) => Promise<{
data?: Element data?: Element
error?: Error error?: Error
}> { }> {
interface ElementGetResponse { interface ElementGetResponse {
data?: Element data?: Element
error?: Error error?: Error
@ -37,22 +39,22 @@ export function get<Element>(
return { error: error as Error } return { error: error as Error }
} }
} }
} }
export function listGet<Element>( function listGet(): () => Promise<{
elementName: string,
translation: ElementTranslation
): () => Promise<{
data?: Element[] data?: Element[]
error?: Error error?: Error
}> { }> {
interface ElementListGetResponse { interface ElementListGetResponse {
data?: Element[] data?: Element[]
error?: Error error?: Error
} }
return async (): Promise<ElementListGetResponse> => { return async (): Promise<ElementListGetResponse> => {
try { 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) { if (!data) {
return { data } return { data }
} }
@ -69,16 +71,13 @@ export function listGet<Element>(
return { error: error as Error } return { error: error as Error }
} }
} }
} }
// eslint-disable-next-line @typescript-eslint/ban-types // eslint-disable-next-line @typescript-eslint/ban-types
export function add<ElementNoId extends object, Element extends ElementNoId & ElementWithId>( function add(): (volunteerWithoutId: ElementNoId) => Promise<{
elementName: string,
translation: ElementTranslation
): (volunteerWithoutId: ElementNoId) => Promise<{
data?: Element data?: Element
error?: Error error?: Error
}> { }> {
interface ElementGetResponse { interface ElementGetResponse {
data?: Element data?: Element
error?: Error error?: Error
@ -110,15 +109,12 @@ export function add<ElementNoId extends object, Element extends ElementNoId & El
return { error: error as Error } return { error: error as Error }
} }
} }
} }
export function set<Element>( function set(): (volunteer: Element) => Promise<{
elementName: string,
translation: ElementTranslation
): (volunteer: Element) => Promise<{
data?: Element data?: Element
error?: Error error?: Error
}> { }> {
interface ElementGetResponse { interface ElementGetResponse {
data?: Element data?: Element
error?: Error error?: Error
@ -149,4 +145,7 @@ export function set<Element>(
return { error: error as Error } return { error: error as Error }
} }
} }
}
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 { export class JavGame {
id = 0 id = 0
@ -54,10 +54,12 @@ const elementName = "JavGame"
export type JavGameWithoutId = Omit<JavGame, "id"> 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 javGameListGet = listGet()
export const javGameGet = get()
export const javGameAdd = add<JavGameWithoutId, JavGame>(elementName, translationJavGame) export const javGameAdd = add()
export const javGameSet = set()
export const javGameSet = set<JavGame>(elementName, translationJavGame)

View File

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

View File

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

View File

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