Skip to content

Commit

Permalink
Merge pull request #203 from atomic-state/enhancements/useServerAction
Browse files Browse the repository at this point in the history
enh(useServerAction):
  • Loading branch information
danybeltran authored Dec 15, 2024
2 parents 86d7d93 + e809051 commit dd28544
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 44 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "http-react",
"version": "3.7.4",
"version": "3.7.5",
"description": "React hooks for data fetching",
"main": "dist/index.js",
"scripts": {
Expand Down
5 changes: 3 additions & 2 deletions src/hooks/others.ts
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ export function useServerAction<T extends (args: any) => any>(
> & {
onSubmit?:
| 'reset'
| ((form: HTMLFormElement, data: Parameters<T>[0]) => void)
| ((form: HTMLFormElement, formData: Parameters<T>[0]) => void)
} & (Parameters<T>[0] extends typeof undefined
? {}
: {
Expand All @@ -710,6 +710,7 @@ export function useServerAction<T extends (args: any) => any>(
return { data, error, status }
},
id: mockServerActionId,
auto: false,
...config,
onResolve(...c) {
if (config?.onResolve) config.onResolve(...c)
Expand Down Expand Up @@ -759,7 +760,7 @@ export function useServerMutation<T extends (args: any) => any>(
> & {
onSubmit?:
| 'reset'
| ((form: HTMLFormElement, data: Parameters<T>[0]) => void)
| ((form: HTMLFormElement, formData: Parameters<T>[0]) => void)
} & (Parameters<T>[0] extends typeof undefined
? {}
: {
Expand Down
26 changes: 13 additions & 13 deletions src/internal/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ const ATTEMPT_INTERVAL = 2
const REVALIDATE_ON_FOCUS = false
const RETRY_ON_RECONNECT = true
const REVALIDATE_ON_MOUNT = true
const DEFAULT_GRAPHQL_PATH = "/graphql"
const DEFAULT_GRAPHQL_PATH = '/graphql'
const DEFAULT_RESOLVER = (e: any) => e.json()
const DEFAULT_MIDDLEWARE = (incoming: any, previous: any) => incoming
const DEFAULT_TRANSFORM = (fetchData: any) => fetchData

const METHODS = {
GET: "GET",
DELETE: "DELETE",
HEAD: "HEAD",
OPTIONS: "OPTIONS",
POST: "POST",
PUT: "PUT",
PATCH: "PATCH",
PURGE: "PURGE",
LINK: "LINK",
UNLINK: "UNLINK",
GET: 'GET',
DELETE: 'DELETE',
HEAD: 'HEAD',
OPTIONS: 'OPTIONS',
POST: 'POST',
PUT: 'PUT',
PATCH: 'PATCH',
PURGE: 'PURGE',
LINK: 'LINK',
UNLINK: 'UNLINK'
}

const UNITS_MILISECONDS_EQUIVALENTS = {
Expand All @@ -35,7 +35,7 @@ const UNITS_MILISECONDS_EQUIVALENTS = {
d: 86400000,
we: 604800000,
mo: 2629800000,
y: 31536000000,
y: 31536000000
}

export {
Expand All @@ -55,5 +55,5 @@ export {
METHODS,
UNITS_MILISECONDS_EQUIVALENTS,
DEFAULT_MIDDLEWARE,
DEFAULT_TRANSFORM,
DEFAULT_TRANSFORM
}
18 changes: 9 additions & 9 deletions src/internal/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client"
import { createContext, useContext } from "react"
'use client'
import { createContext, useContext } from 'react'

import { CacheStoreType, FetchContextType } from "../types"
import { CacheStoreType, FetchContextType } from '../types'
import {
ATTEMPTS,
ATTEMPT_INTERVAL,
Expand All @@ -15,9 +15,9 @@ import {
QUERY,
RETRY_ON_RECONNECT,
REVALIDATE_ON_FOCUS,
REVALIDATE_ON_MOUNT,
} from "./constants"
import { $context } from "./shared"
REVALIDATE_ON_MOUNT
} from './constants'
import { $context } from './shared'

/**
* This marks which requests are running
Expand Down Expand Up @@ -135,7 +135,7 @@ export const defaultCache: CacheStoreType = {
},
remove(k) {
resolvedRequests.delete(k)
},
}
}

const requestsSubscribers = new Map()
Expand Down Expand Up @@ -163,7 +163,7 @@ export const requestsProvider = {
listener(payload)
})
}
},
}
}

const defaultContextVaue: FetchContextType = {
Expand All @@ -181,7 +181,7 @@ const defaultContextVaue: FetchContextType = {
cacheIfError: true,
middleware: DEFAULT_MIDDLEWARE,
transform: DEFAULT_TRANSFORM,
...$context.value,
...$context.value
}

export const FetchContext = createContext<FetchContextType>(defaultContextVaue)
Expand Down
38 changes: 19 additions & 19 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
export type HTTP_METHODS =
| "GET"
| "DELETE"
| "HEAD"
| "OPTIONS"
| "POST"
| "PUT"
| "PATCH"
| "PURGE"
| "LINK"
| "UNLINK"
| 'GET'
| 'DELETE'
| 'HEAD'
| 'OPTIONS'
| 'POST'
| 'PUT'
| 'PATCH'
| 'PURGE'
| 'LINK'
| 'UNLINK'

export type FetchContextType = {
clientOnly?: boolean
Expand Down Expand Up @@ -83,15 +83,15 @@ export type FetchContextType = {
ctx: FetchContextType
): void
maxCacheAge?: TimeSpan
} & Omit<RequestInit, "body">
} & Omit<RequestInit, 'body'>

export type CacheStoreType = {
get(k?: any): any
set(k?: any, v?: any): any
remove?(k?: any): any
}

export type CustomResponse<T> = Omit<Response, "json"> & {
export type CustomResponse<T> = Omit<Response, 'json'> & {
json(): Promise<T>
}

Expand All @@ -103,7 +103,7 @@ export type RequestWithBody = <R = any, BodyType = any>(
/**
* The request configuration
*/
reqConfig?: Omit<RequestInit & FetchConfigType<R, BodyType>, "suspense"> & {
reqConfig?: Omit<RequestInit & FetchConfigType<R, BodyType>, 'suspense'> & {
/**
* Default value
*/
Expand Down Expand Up @@ -144,7 +144,7 @@ export type RequestWithBody = <R = any, BodyType = any>(

export type TimeSpan =
| number
| `${string} ${"ms" | "sec" | "min" | "h" | "d" | "we" | "mo" | "y"}`
| `${string} ${'ms' | 'sec' | 'min' | 'h' | 'd' | 'we' | 'mo' | 'y'}`

/**
* An imperative version of the `useFetch` hook
Expand All @@ -165,7 +165,7 @@ export type ImperativeFetch = {

export type FetchConfigType<FetchDataType = any, BodyType = any> = Omit<
RequestInit,
"body" | "headers"
'body' | 'headers'
> & {
headers?: any
/**
Expand Down Expand Up @@ -219,7 +219,7 @@ export type FetchConfigType<FetchDataType = any, BodyType = any> = Omit<
* @default true
*/
memory?: boolean
onSubmit?: "reset" | ((form: HTMLFormElement, data: FormData) => void)
onSubmit?: 'reset' | ((form: HTMLFormElement, data: FormData) => void)
/**
* Function to run when request is resolved succesfuly
*/
Expand Down Expand Up @@ -337,11 +337,11 @@ export type FetchConfigType<FetchDataType = any, BodyType = any> = Omit<
/**
* Will run when the request is sent
*/
onFetchStart?: FetchContextType["onFetchStart"]
onFetchStart?: FetchContextType['onFetchStart']
/**
* Will run when the response is received
*/
onFetchEnd?: FetchContextType["onFetchEnd"]
onFetchEnd?: FetchContextType['onFetchEnd']
/**
* If `true`, the last resolved value be returned as `data` if the request fails. If `false`, the default value will be returned instead
*
Expand All @@ -357,7 +357,7 @@ export type FetchConfigType<FetchDataType = any, BodyType = any> = Omit<
// If first argument is a string
export type FetchConfigTypeNoUrl<FetchDataType = any, BodyType = any> = Omit<
FetchConfigType<FetchDataType, BodyType>,
"url"
'url'
>

/**
Expand Down

0 comments on commit dd28544

Please sign in to comment.