Created
July 16, 2022 15:19
-
-
Save kiliman/06016bea814cc4c13c269e1fd4bc12c4 to your computer and use it in GitHub Desktop.
Remix re-export with superjson
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useActionData, useLoaderData } from "@remix-run/react"; | |
import * as _superjson from "superjson"; | |
export { | |
AbortController, | |
createCookie, | |
createCookieSessionStorage, | |
createFileSessionStorage, | |
createMemorySessionStorage, | |
createReadableStreamFromReadable, | |
createRequestHandler, | |
createSession, | |
createSessionStorage, | |
fetch, | |
FormData, | |
Headers, | |
installGlobals, | |
isCookie, | |
isSession, | |
json, | |
MaxPartSizeExceededError, | |
NodeOnDiskFile, | |
readableStreamToString, | |
Request, | |
Response, | |
unstable_composeUploadHandlers, | |
unstable_createFileUploadHandler, | |
unstable_createMemoryUploadHandler, | |
unstable_parseMultipartFormData, | |
writeAsyncIterableToWritable, | |
writeReadableStreamToWritable, | |
} from "@remix-run/node"; | |
export type { | |
ActionArgs, | |
ActionFunction, | |
AppData, | |
AppLoadContext, | |
Cookie, | |
CookieOptions, | |
CookieParseOptions, | |
CookieSerializeOptions, | |
CookieSignatureOptions, | |
CreateRequestHandlerFunction, | |
DataFunctionArgs, | |
EntryContext, | |
ErrorBoundaryComponent, | |
HandleDataRequestFunction, | |
HandleDocumentRequestFunction, | |
HeadersFunction, | |
HeadersInit, | |
HtmlLinkDescriptor, | |
HtmlMetaDescriptor, | |
LinkDescriptor, | |
LinksFunction, | |
LoaderArgs, | |
LoaderFunction, | |
MemoryUploadHandlerFilterArgs, | |
MemoryUploadHandlerOptions, | |
MetaDescriptor, | |
MetaFunction, | |
PageLinkDescriptor, | |
RequestHandler, | |
RequestInfo, | |
RequestInit, | |
ResponseInit, | |
RouteComponent, | |
RouteHandle, | |
ServerBuild, | |
ServerEntryModule, | |
Session, | |
SessionData, | |
SessionIdStorageStrategy, | |
SessionStorage, | |
UploadHandler, | |
UploadHandlerPart, | |
} from "@remix-run/node"; | |
export { | |
Form, | |
Link, | |
Links, | |
LiveReload, | |
Meta, | |
NavLink, | |
Outlet, | |
PrefetchPageLinks, | |
RemixBrowser, | |
RemixServer, | |
Scripts, | |
ScrollRestoration, | |
useActionData, | |
useBeforeUnload, | |
useCatch, | |
useFetcher, | |
useFetchers, | |
useFormAction, | |
useHref, | |
useLoaderData, | |
useLocation, | |
useMatches, | |
useNavigate, | |
useNavigationType, | |
useOutlet, | |
useOutletContext, | |
useParams, | |
useResolvedPath, | |
useSearchParams, | |
useSubmit, | |
useTransition, | |
} from "@remix-run/react"; | |
export type SuperJsonFunction = <Data extends unknown>( | |
data: Data, | |
init?: number | ResponseInit | |
) => SuperTypedResponse<Data>; | |
export declare type SuperTypedResponse<T extends unknown = unknown> = | |
Response & { | |
superjson(): Promise<T>; | |
}; | |
type AppData = any; | |
type DataFunction = (...args: any[]) => unknown; // matches any function | |
type DataOrFunction = AppData | DataFunction; | |
export type UseDataFunctionReturn<T extends DataOrFunction> = T extends ( | |
...args: any[] | |
) => infer Output | |
? Awaited<Output> extends SuperTypedResponse<infer U> | |
? U | |
: Awaited<ReturnType<T>> | |
: Awaited<T>; | |
export const superjson: SuperJsonFunction = (data, init = {}) => { | |
let responseInit = typeof init === "number" ? { status: init } : init; | |
let headers = new Headers(responseInit.headers); | |
if (!headers.has("Content-Type")) { | |
headers.set("Content-Type", "application/json; charset=utf-8"); | |
} | |
return new Response(_superjson.stringify(data), { | |
...responseInit, | |
headers, | |
}) as SuperTypedResponse<typeof data>; | |
}; | |
export function useSuperLoaderData<T = AppData>(): UseDataFunctionReturn<T> { | |
const data = useLoaderData(); | |
return _superjson.deserialize(data); | |
} | |
export function useSuperActionData< | |
T = AppData | |
>(): UseDataFunctionReturn<T> | null { | |
const data = useActionData(); | |
return data ? _superjson.deserialize(data) : null; | |
} | |
export type RedirectFunction = ( | |
url: string, | |
init?: number | ResponseInit | |
) => SuperTypedResponse<never>; | |
/** | |
* A redirect response. Sets the status code and the `Location` header. | |
* Defaults to "302 Found". | |
* | |
* @see https://remix.run/api/remix#redirect | |
*/ | |
export const redirect: RedirectFunction = (url, init = 302) => { | |
let responseInit = init; | |
if (typeof responseInit === "number") { | |
responseInit = { status: responseInit }; | |
} else if (typeof responseInit.status === "undefined") { | |
responseInit.status = 302; | |
} | |
let headers = new Headers(responseInit.headers); | |
headers.set("Location", url); | |
return new Response(null, { | |
...responseInit, | |
headers, | |
}) as SuperTypedResponse<never>; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment