From c64bf376f882a2d5fd05afb808ab57e8686bc42c Mon Sep 17 00:00:00 2001 From: pikiou Date: Wed, 8 Dec 2021 11:06:27 +0100 Subject: [PATCH] Factorize service accessors --- src/services/accessors.ts | 233 +++++++++++++++++----------------- src/services/javGames.ts | 16 ++- src/services/preVolunteers.ts | 16 ++- src/services/volunteers.ts | 16 ++- src/services/wishes.ts | 16 ++- 5 files changed, 152 insertions(+), 145 deletions(-) diff --git a/src/services/accessors.ts b/src/services/accessors.ts index dc0f701..d5e8a6c 100644 --- a/src/services/accessors.ts +++ b/src/services/accessors.ts @@ -6,147 +6,146 @@ import { axiosConfig } from "./auth" export type ElementWithId = unknown & { id: number } -export type ElementTranslation = { [englishProp: string]: string } +export type ElementTranslation = { [k in keyof Element]: string } -export function get( - elementName: string, - translation: ElementTranslation -): (id: number) => Promise<{ - data?: Element - error?: Error -}> { - interface ElementGetResponse { +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 - } - return async (id: number): Promise => { - try { - const { data } = await axios.get(`${config.API_URL}/${elementName}Get`, { - ...axiosConfig, - params: { id }, - }) - if (!data) { - return { data } + }> { + interface ElementGetResponse { + data?: Element + error?: Error + } + return async (id: number): Promise => { + try { + const { data } = await axios.get(`${config.API_URL}/${elementName}Get`, { + ...axiosConfig, + params: { id }, + }) + if (!data) { + return { data } + } + const englishData = _.mapValues( + translation, + (frenchProp: string) => data[frenchProp] + ) as Element + return { data: englishData } + } catch (error) { + return { error: error as Error } } - const englishData = _.mapValues( - translation, - (frenchProp: string) => data[frenchProp] - ) as Element - return { data: englishData } - } catch (error) { - return { error: error as Error } } } -} -export function listGet( - elementName: string, - translation: ElementTranslation -): () => Promise<{ - data?: Element[] - error?: Error -}> { - interface ElementListGetResponse { + function listGet(): () => Promise<{ data?: Element[] error?: Error - } - return async (): Promise => { - try { - const { data } = await axios.get(`${config.API_URL}/${elementName}ListGet`, axiosConfig) - if (!data) { - return { data } - } + }> { + interface ElementListGetResponse { + data?: Element[] + error?: Error + } + return async (): Promise => { + try { + const { data } = await axios.get( + `${config.API_URL}/${elementName}ListGet`, + axiosConfig + ) + if (!data) { + return { data } + } - const englishDataList = data.map( - (frenchData: any) => - _.mapValues( - translation, - (frenchProp: string) => frenchData[frenchProp] - ) as Element - ) - return { data: englishDataList } - } catch (error) { - return { error: error as Error } + const englishDataList = data.map( + (frenchData: any) => + _.mapValues( + translation, + (frenchProp: string) => frenchData[frenchProp] + ) as Element + ) + return { data: englishDataList } + } catch (error) { + return { error: error as Error } + } } } -} -// eslint-disable-next-line @typescript-eslint/ban-types -export function add( - elementName: string, - translation: ElementTranslation -): (volunteerWithoutId: ElementNoId) => Promise<{ - data?: Element - error?: Error -}> { - interface ElementGetResponse { + // eslint-disable-next-line @typescript-eslint/ban-types + function add(): (volunteerWithoutId: ElementNoId) => Promise<{ data?: Element error?: Error - } - return async (volunteerWithoutId: ElementNoId): Promise => { - try { - const invertedTranslationWithoutId = _.invert(_.omit(translation, "id")) - const frenchDataWithoutId = _.mapValues( - invertedTranslationWithoutId, - (englishProp: string, _frenchProp: string) => - (volunteerWithoutId as any)[englishProp] - ) + }> { + interface ElementGetResponse { + data?: Element + error?: Error + } + return async (volunteerWithoutId: ElementNoId): Promise => { + try { + const invertedTranslationWithoutId = _.invert(_.omit(translation, "id")) + const frenchDataWithoutId = _.mapValues( + invertedTranslationWithoutId, + (englishProp: string, _frenchProp: string) => + (volunteerWithoutId as any)[englishProp] + ) - const { data } = await axios.post( - `${config.API_URL}/${elementName}Add`, - frenchDataWithoutId, - axiosConfig - ) - if (!data) { - return { data } + const { data } = await axios.post( + `${config.API_URL}/${elementName}Add`, + frenchDataWithoutId, + axiosConfig + ) + if (!data) { + return { data } + } + + const englishData = _.mapValues( + translation, + (frenchProp: string) => data[frenchProp] + ) as Element + return { data: englishData } + } catch (error) { + return { error: error as Error } } - - const englishData = _.mapValues( - translation, - (frenchProp: string) => data[frenchProp] - ) as Element - return { data: englishData } - } catch (error) { - return { error: error as Error } } } -} -export function set( - elementName: string, - translation: ElementTranslation -): (volunteer: Element) => Promise<{ - data?: Element - error?: Error -}> { - interface ElementGetResponse { + function set(): (volunteer: Element) => Promise<{ data?: Element error?: Error - } - return async (volunteer: Element): Promise => { - try { - const invertedTranslation = _.invert(translation) - const frenchData = _.mapValues( - invertedTranslation, - (englishProp: string) => (volunteer as any)[englishProp] - ) + }> { + interface ElementGetResponse { + data?: Element + error?: Error + } + return async (volunteer: Element): Promise => { + try { + const invertedTranslation = _.invert(translation) + const frenchData = _.mapValues( + invertedTranslation, + (englishProp: string) => (volunteer as any)[englishProp] + ) - const { data } = await axios.post( - `${config.API_URL}/${elementName}Set`, - frenchData, - axiosConfig - ) - if (!data) { - return { data } + const { data } = await axios.post( + `${config.API_URL}/${elementName}Set`, + frenchData, + axiosConfig + ) + if (!data) { + return { data } + } + + const englishData = _.mapValues( + translation, + (frenchProp: string) => data[frenchProp] + ) as Element + return { data: englishData } + } catch (error) { + return { error: error as Error } } - - const englishData = _.mapValues( - translation, - (frenchProp: string) => data[frenchProp] - ) as Element - return { data: englishData } - } catch (error) { - return { error: error as Error } } } + + return { listGet, get, set, add } } diff --git a/src/services/javGames.ts b/src/services/javGames.ts index 0fc1c99..af1f2c3 100644 --- a/src/services/javGames.ts +++ b/src/services/javGames.ts @@ -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 -export const javGameGet = get(elementName, translationJavGame) +const { listGet, get, set, add } = getServiceAccessors( + elementName, + translationJavGame +) -export const javGameListGet = listGet(elementName, translationJavGame) - -export const javGameAdd = add(elementName, translationJavGame) - -export const javGameSet = set(elementName, translationJavGame) +export const javGameListGet = listGet() +export const javGameGet = get() +export const javGameAdd = add() +export const javGameSet = set() diff --git a/src/services/preVolunteers.ts b/src/services/preVolunteers.ts index 38944af..e3c61e5 100644 --- a/src/services/preVolunteers.ts +++ b/src/services/preVolunteers.ts @@ -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 -export const preMemberGet = get(elementName, translationPreMember) +const { listGet, get, set, add } = getServiceAccessors( + elementName, + translationPreMember +) -export const preMemberListGet = listGet(elementName, translationPreMember) - -export const preMemberAdd = add(elementName, translationPreMember) - -export const preMemberSet = set(elementName, translationPreMember) +export const preMemberListGet = listGet() +export const preMemberGet = get() +export const preMemberAdd = add() +export const preMemberSet = set() diff --git a/src/services/volunteers.ts b/src/services/volunteers.ts index 20fc15f..d84c029 100644 --- a/src/services/volunteers.ts +++ b/src/services/volunteers.ts @@ -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 -export const volunteerGet = get(elementName, translationMember) +const { listGet, get, set, add } = getServiceAccessors( + elementName, + translationMember +) -export const volunteerListGet = listGet(elementName, translationMember) - -export const volunteerAdd = add(elementName, translationMember) - -export const volunteerSet = set(elementName, translationMember) +export const volunteerListGet = listGet() +export const volunteerGet = get() +export const volunteerAdd = add() +export const volunteerSet = set() diff --git a/src/services/wishes.ts b/src/services/wishes.ts index 3a8aebd..3e44404 100644 --- a/src/services/wishes.ts +++ b/src/services/wishes.ts @@ -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 -export const wishGet = get(elementName, translationWish) +const { listGet, get, set, add } = getServiceAccessors( + elementName, + translationWish +) -export const wishListGet = listGet(elementName, translationWish) - -export const wishAdd = add(elementName, translationWish) - -export const wishSet = set(elementName, translationWish) +export const wishListGet = listGet() +export const wishGet = get() +export const wishAdd = add() +export const wishSet = set()