Skip to content

Commit

Permalink
refactor: react-core hooks key format
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Aug 19, 2024
1 parent 186e17a commit 0ec998b
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 74 deletions.
31 changes: 9 additions & 22 deletions libs/react-core/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ export type HookArgsSwr<
> = Params extends void
? {
api?: string
standalone?: string
config?: RequestConfig<void, Result>
disabled?: boolean
}
: {
params: Params
api?: string
standalone?: string
config?: RequestConfig<void, Result>
disabled?: boolean
}
Expand All @@ -43,13 +41,11 @@ export type HookArgsWithPayloadSwr<
? Payload extends void
? {
api?: string
standalone?: string
config?: RequestConfig<void, Result>
disabled?: boolean
}
: {
api?: string
standalone?: string
payload: Payload
config?: RequestConfig<void, Result>
disabled?: boolean
Expand All @@ -58,15 +54,13 @@ export type HookArgsWithPayloadSwr<
? {
params: Params
api?: string
standalone?: string
config?: RequestConfig<void, Result>
disabled?: boolean
}
: {
params: Params
payload: Payload
api?: string
standalone?: string
config?: RequestConfig<void, Result>
disabled?: boolean
}
Expand Down Expand Up @@ -190,14 +184,14 @@ export type Response<T> = {
error?: string
}

function getApi<Params extends RequestParams, Payload, Result>(
function getApi(
settings: AppSettings,
hookArgs:
| {
api?: string
}
| undefined,
callArgs: InternalCallbackArgs<Params, Payload, Result> | undefined
callArgs: { api?: string } | undefined
): string {
return callArgs?.api || hookArgs?.api || settings.api
}
Expand Down Expand Up @@ -240,18 +234,15 @@ export function buildRouteWithParams<
Result
>(
settings: AppSettings,
route: string | null,
route: string,
hookArgs:
| {
api?: string
params?: Params
}
| undefined,
callArgs: InternalCallbackArgs<Params, Payload, Result> | undefined
): string | null {
if (!route) {
return null
}
): string {
let params = hookArgs?.params || {}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if (callArgs && (callArgs as any).params) {
Expand All @@ -262,32 +253,28 @@ export function buildRouteWithParams<
}
}
route = parameterizeRoute(route, params)
if (!route) {
return null
}
const api = getApi(settings, hookArgs, callArgs)
if (api === settings.api) {
return `${api}/api${route}`
}
return `${api}${route}`
}

export function getPathFromKey<Params extends RequestParams, Payload, Result>(
export function getPathFromKey(
settings: AppSettings,
route: string,
key: [string, string],
hookArgs:
| {
api?: string
params?: Params
}
| undefined,
callArgs: InternalCallbackArgs<Params, Payload, Result> | undefined
callArgs: { api?: string } | undefined
): string {
const api = getApi(settings, hookArgs, callArgs)
if (api === settings.api) {
return route.replace(`${api}/api`, '')
return key[1].replace(`${api}/api`, '')
}
return route.replace(api, '')
return key[1].replace(api, '')
}

export type After<Params extends RequestParams, Payload, Result> = (
Expand Down
13 changes: 8 additions & 5 deletions libs/react-core/src/useDelete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from './request'
import { useAppSettings } from './useAppSettings'
import { RequestParams } from '@siafoundation/request'
import { getKey } from './usePatch'

type DeleteFunc<Params extends RequestParams, Result> = {
delete: (
Expand Down Expand Up @@ -45,14 +46,16 @@ export function useDeleteFunc<Params extends RequestParams, Result>(
if (!reqRoute) {
throw Error('No route')
}

const key = getKey('delete', reqRoute)
const path = getPathFromKey(
settings,
reqRoute,
key,
args,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
callArgs as any
)
setWorkflow(reqRoute, {
setWorkflow(key, {
path,
})
const response = await axios.delete<Result>(reqRoute, reqConfig)
Expand All @@ -61,12 +64,12 @@ export function useDeleteFunc<Params extends RequestParams, Result>(
(matcher, data = (d) => d, opts) =>
mutate(
(key) => {
if (typeof key !== 'string') {
if (!key || typeof key === 'string' || key.length !== 2) {
return false
}
const route = getPathFromKey(
settings,
key,
key as [string, string],
args,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
callArgs as any
Expand All @@ -80,7 +83,7 @@ export function useDeleteFunc<Params extends RequestParams, Result>(
response
)
}
removeWorkflow(reqRoute)
removeWorkflow(key)
return {
status: response.status,
data: response.data,
Expand Down
3 changes: 2 additions & 1 deletion libs/react-core/src/useGet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { SWRError } from './types'
import { useAppSettings } from './useAppSettings'
import { keyOrNull } from './utils'
import { RequestParams } from '@siafoundation/request'
import { getKey } from './usePatch'

export function useGetSwr<Params extends RequestParams, Result>(
args: InternalHookArgsSwr<Params, Result>
Expand All @@ -32,7 +33,7 @@ export function useGetSwr<Params extends RequestParams, Result>(
)
return useSWR<Result, SWRError>(
keyOrNull(
args.standalone ? `${args.standalone}/${reqRoute}` : reqRoute,
getKey('get', reqRoute),
hookArgs.disabled || (passwordProtectRequestHooks && !settings.password)
),
async () => {
Expand Down
32 changes: 19 additions & 13 deletions libs/react-core/src/usePatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,7 @@ export function usePatchSwr<Params extends RequestParams, Payload, Result>(
const key = useMemo(
() =>
keyOrNull(
reqRoute
? `${reqRoute}${JSON.stringify(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(args as any).payload !== undefined ? (args as any).payload : ''
)}`
: null,
getKey('patch', reqRoute, args as { payload: Payload }),
hookArgs.disabled || (passwordProtectRequestHooks && !settings.password)
),
[reqRoute, args, hookArgs, passwordProtectRequestHooks, settings]
Expand Down Expand Up @@ -114,14 +109,11 @@ export function usePatchFunc<Params extends RequestParams, Payload, Result>(
payload = callArgs.payload
}

const key = `${reqRoute}${JSON.stringify(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(args as any).payload !== undefined ? (args as any).payload : ''
)}`
const key = getKey('patch', reqRoute, args as { payload: Payload })

const path = getPathFromKey(
settings,
reqRoute,
key,
args,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
callArgs as any
Expand All @@ -136,12 +128,12 @@ export function usePatchFunc<Params extends RequestParams, Payload, Result>(
(matcher, data = (d) => d, opts) =>
mutate(
(key) => {
if (typeof key !== 'string') {
if (!key || typeof key === 'string' || key.length !== 2) {
return false
}
const route = getPathFromKey(
settings,
key,
key as [string, string],
args,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
callArgs as any
Expand Down Expand Up @@ -173,3 +165,17 @@ export function usePatchFunc<Params extends RequestParams, Payload, Result>(
},
}
}

type Method = 'get' | 'post' | 'put' | 'patch' | 'delete'
export function getKey<Payload>(
method: Method,
route: string,
args?: {
payload?: Payload
}
): [Method, string] {
return [
method,
`${route}${args?.payload ? JSON.stringify(args.payload) : ''}`,
]
}
19 changes: 6 additions & 13 deletions libs/react-core/src/usePost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { useAppSettings } from './useAppSettings'
import { keyOrNull } from './utils'
import { useWorkflows } from './workflows'
import { RequestParams } from '@siafoundation/request'
import { getKey } from './usePatch'

export function usePostSwr<Params extends RequestParams, Payload, Result>(
args: InternalHookArgsWithPayloadSwr<Params, Payload, Result>
Expand All @@ -41,12 +42,7 @@ export function usePostSwr<Params extends RequestParams, Payload, Result>(
const key = useMemo(
() =>
keyOrNull(
reqRoute
? `${reqRoute}${JSON.stringify(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(args as any).payload !== undefined ? (args as any).payload : ''
)}`
: null,
getKey('post', reqRoute, args as { payload: Payload }),
hookArgs.disabled || (passwordProtectRequestHooks && !settings.password)
),
[reqRoute, args, hookArgs, passwordProtectRequestHooks, settings]
Expand Down Expand Up @@ -114,14 +110,11 @@ export function usePostFunc<Params extends RequestParams, Payload, Result>(
payload = callArgs.payload
}

const key = `${reqRoute}${JSON.stringify(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(args as any).payload !== undefined ? (args as any).payload : ''
)}`
const key = getKey('post', reqRoute, args as { payload: Payload })

const path = getPathFromKey(
settings,
reqRoute,
key,
args,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
callArgs as any
Expand All @@ -137,12 +130,12 @@ export function usePostFunc<Params extends RequestParams, Payload, Result>(
(matcher, data = (d) => d, opts) =>
mutate(
(key) => {
if (typeof key !== 'string') {
if (!key || typeof key === 'string' || key.length !== 2) {
return false
}
const route = getPathFromKey(
settings,
key,
key as [string, string],
args,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
callArgs as any
Expand Down
19 changes: 6 additions & 13 deletions libs/react-core/src/usePut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { useMemo } from 'react'
import { keyOrNull } from './utils'
import { SWRError } from './types'
import { RequestParams } from '@siafoundation/request'
import { getKey } from './usePatch'

export function usePutSwr<Params extends RequestParams, Payload, Result>(
args: InternalHookArgsWithPayloadSwr<Params, Payload, Result>
Expand All @@ -41,12 +42,7 @@ export function usePutSwr<Params extends RequestParams, Payload, Result>(
const key = useMemo(
() =>
keyOrNull(
reqRoute
? `${reqRoute}${JSON.stringify(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(args as any).payload !== undefined ? (args as any).payload : ''
)}`
: null,
getKey('put', reqRoute, args as { payload: Payload }),
hookArgs.disabled || (passwordProtectRequestHooks && !settings.password)
),
[reqRoute, args, hookArgs, passwordProtectRequestHooks, settings]
Expand Down Expand Up @@ -114,14 +110,11 @@ export function usePutFunc<Params extends RequestParams, Payload, Result>(
payload = callArgs.payload
}

const key = `${reqRoute}${JSON.stringify(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(args as any).payload !== undefined ? (args as any).payload : ''
)}`
const key = getKey('put', reqRoute, args as { payload: Payload })

const path = getPathFromKey(
settings,
reqRoute,
key,
args,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
callArgs as any
Expand All @@ -136,12 +129,12 @@ export function usePutFunc<Params extends RequestParams, Payload, Result>(
(matcher, data = (d) => d, opts) =>
mutate(
(key) => {
if (typeof key !== 'string') {
if (!key || typeof key === 'string' || key.length !== 2) {
return false
}
const route = getPathFromKey(
settings,
key,
key as [string, string],
args,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
callArgs as any
Expand Down
2 changes: 1 addition & 1 deletion libs/react-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { mutate } from 'swr'

export function keyOrNull(name: string | null, disabled?: boolean) {
export function keyOrNull(name: string[] | null, disabled?: boolean) {
if (!name || disabled) {
return null
}
Expand Down
8 changes: 4 additions & 4 deletions libs/react-core/src/workflows.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ function useWorkflowsMain() {
const [workflowsMap, setWorkflowsMap] = useState<PendingMap>({})

const setWorkflow = useCallback(
(key: string, item?: WorkflowPayload) => {
(key: [string, string], item?: WorkflowPayload) => {
setWorkflowsMap((workflows) => {
return {
...workflows,
[key]: {
[key.join('/')]: {
key,
...item,
},
Expand All @@ -30,9 +30,9 @@ function useWorkflowsMain() {
)

const removeWorkflow = useCallback(
(key: string) => {
(key: [string, string]) => {
setWorkflowsMap((workflows) => {
delete workflows[key]
delete workflows[key.join('/')]
return {
...workflows,
}
Expand Down
Loading

0 comments on commit 0ec998b

Please sign in to comment.