Skip to content

Commit

Permalink
rename some types
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusmarminge committed Nov 5, 2024
1 parent 7714ef3 commit 4004ea8
Show file tree
Hide file tree
Showing 19 changed files with 74 additions and 82 deletions.
2 changes: 1 addition & 1 deletion packages/expo/src/document-picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const GENERATE_useDocumentUploader = <
}) => {
const useDocumentUploader = <TEndpoint extends keyof TRouter>(
endpoint: TEndpoint,
opts?: UseUploadthingProps<TRouter, TEndpoint>,
opts?: UseUploadthingProps<TRouter[TEndpoint]>,
) => {
const { routeConfig, startUpload, isUploading } = __useUploadThingInternal(
initOpts.url,
Expand Down
2 changes: 1 addition & 1 deletion packages/expo/src/image-picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const GENERATE_useImageUploader = <
}) => {
const useImageUploader = <TEndpoint extends keyof TRouter>(
endpoint: TEndpoint,
opts?: UseUploadthingProps<TRouter, TEndpoint>,
opts?: UseUploadthingProps<TRouter[TEndpoint]>,
) => {
const { routeConfig, startUpload, isUploading } = __useUploadThingInternal(
initOpts.url,
Expand Down
10 changes: 5 additions & 5 deletions packages/react/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
UploadThingError,
} from "@uploadthing/shared";
import type {
AnyFileRoute,
ClientUploadedFileData,
EndpointArg,
FileRouter,
Expand All @@ -28,9 +29,8 @@ export interface GenerateTypedHelpersOptions {
}

export type UseUploadthingProps<
TRouter extends FileRouter,
TEndpoint extends keyof TRouter,
TServerOutput = inferEndpointOutput<TRouter[TEndpoint]>,
TFileRoute extends AnyFileRoute,
TServerOutput = inferEndpointOutput<TFileRoute>,
> = {
/**
* Called when the upload is submitted and the server is about to be queried for presigned URLs
Expand Down Expand Up @@ -74,7 +74,7 @@ export type UseUploadthingProps<
* Called if the upload fails
*/
onUploadError?:
| ((e: UploadThingError<inferErrorShape<TRouter>>) => MaybePromise<void>)
| ((e: UploadThingError<inferErrorShape<TFileRoute>>) => MaybePromise<void>)
| undefined;
/**
* Set custom headers that'll get sent with requests
Expand All @@ -94,7 +94,7 @@ export type UploadthingComponentProps<
TRouter extends FileRouter,
TEndpoint extends keyof TRouter,
> = Omit<
UseUploadthingProps<TRouter, TEndpoint>,
UseUploadthingProps<TRouter[TEndpoint]>,
/**
* Signal is omitted, component has its own AbortController
* If you need to control the interruption with more granularity,
Expand Down
8 changes: 4 additions & 4 deletions packages/react/src/useUploadThing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function __useUploadThingInternal<
>(
url: URL,
endpoint: EndpointArg<TRouter, TEndpoint>,
opts?: UseUploadthingProps<TRouter, TEndpoint>,
opts?: UseUploadthingProps<TRouter[TEndpoint]>,
) {
const { uploadFiles, routeRegistry } = genUploader<TRouter>({
url,
Expand Down Expand Up @@ -115,9 +115,9 @@ export function __useUploadThingInternal<
*/
if (e instanceof UploadAbortedError) throw e;

let error: UploadThingError<inferErrorShape<TRouter>>;
let error: UploadThingError<inferErrorShape<TRouter[TEndpoint]>>;
if (e instanceof UploadThingError) {
error = e as UploadThingError<inferErrorShape<TRouter>>;
error = e as UploadThingError<inferErrorShape<TRouter[TEndpoint]>>;
} else {
error = INTERNAL_DO_NOT_USE__fatalClientError(e as Error);
console.error(
Expand Down Expand Up @@ -161,7 +161,7 @@ export const generateReactHelpers = <TRouter extends FileRouter>(

function useUploadThing<TEndpoint extends keyof TRouter>(
endpoint: EndpointArg<TRouter, TEndpoint>,
opts?: UseUploadthingProps<TRouter, TEndpoint>,
opts?: UseUploadthingProps<TRouter[TEndpoint]>,
) {
return __useUploadThingInternal(url, endpoint, opts);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/solid/src/create-uploadthing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const INTERNAL_createUploadThingGen = <

const createUploadThing = <TEndpoint extends keyof TRouter>(
endpoint: EndpointArg<TRouter, TEndpoint>,
opts?: CreateUploadthingProps<TRouter, TEndpoint>,
opts?: CreateUploadthingProps<TRouter[TEndpoint]>,
) => {
const [isUploading, setUploading] = createSignal(false);
let uploadProgress = 0;
Expand Down Expand Up @@ -99,9 +99,9 @@ export const INTERNAL_createUploadThingGen = <
*/
if (e instanceof UploadAbortedError) throw e;

let error: UploadThingError<inferErrorShape<TRouter>>;
let error: UploadThingError<inferErrorShape<TRouter[TEndpoint]>>;
if (e instanceof UploadThingError) {
error = e as UploadThingError<inferErrorShape<TRouter>>;
error = e as UploadThingError<inferErrorShape<TRouter[TEndpoint]>>;
} else {
error = INTERNAL_DO_NOT_USE__fatalClientError(e as Error);
console.error(
Expand Down
17 changes: 8 additions & 9 deletions packages/solid/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
UploadThingError,
} from "@uploadthing/shared";
import type {
AnyFileRoute,
ClientUploadedFileData,
EndpointArg,
FileRouter,
Expand All @@ -28,9 +29,8 @@ export interface GenerateTypedHelpersOptions {
}

export type CreateUploadthingProps<
TRouter extends FileRouter,
TEndpoint extends keyof TRouter,
TServerOutput = inferEndpointOutput<TRouter[TEndpoint]>,
TFileRoute extends AnyFileRoute,
TServerOutput = inferEndpointOutput<TFileRoute>,
> = {
/**
* Called when the upload is submitted and the server is about to be queried for presigned URLs
Expand Down Expand Up @@ -74,7 +74,7 @@ export type CreateUploadthingProps<
* Called if the upload fails
*/
onUploadError?:
| ((e: UploadThingError<inferErrorShape<TRouter>>) => MaybePromise<void>)
| ((e: UploadThingError<inferErrorShape<TFileRoute>>) => MaybePromise<void>)
| undefined;
/**
* Set custom headers that'll get sent with requests
Expand All @@ -94,16 +94,15 @@ export type CreateUploadthingProps<
* @deprecated Use `CreateUploadthingProps` instead
*/
export type UseUploadThingProps<
TRouter extends FileRouter,
TEndpoint extends keyof TRouter,
TServerOutput = inferEndpointOutput<TRouter[TEndpoint]>,
> = CreateUploadthingProps<TRouter, TEndpoint, TServerOutput>;
TFileRoute extends AnyFileRoute,
TServerOutput = inferEndpointOutput<TFileRoute>,
> = CreateUploadthingProps<TFileRoute, TServerOutput>;

export type UploadthingComponentProps<
TRouter extends FileRouter,
TEndpoint extends keyof TRouter,
> = Omit<
CreateUploadthingProps<TRouter, TEndpoint>,
CreateUploadthingProps<TRouter[TEndpoint]>,
/**
* Signal is omitted, component has its own AbortController
* If you need to control the interruption with more granularity,
Expand Down
6 changes: 3 additions & 3 deletions packages/svelte/src/lib/create-uploadthing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const INTERNAL_createUploadThingGen = <

const useUploadThing = <TEndpoint extends keyof TRouter>(
endpoint: EndpointArg<TRouter, TEndpoint>,
opts?: UseUploadthingProps<TRouter, TEndpoint>,
opts?: UseUploadthingProps<TRouter[TEndpoint]>,
) => {
const isUploading = writable(false);
let uploadProgress = 0;
Expand Down Expand Up @@ -103,9 +103,9 @@ export const INTERNAL_createUploadThingGen = <
*/
if (e instanceof UploadAbortedError) throw e;

let error: UploadThingError<inferErrorShape<TRouter>>;
let error: UploadThingError<inferErrorShape<TRouter[TEndpoint]>>;
if (e instanceof UploadThingError) {
error = e as UploadThingError<inferErrorShape<TRouter>>;
error = e as UploadThingError<inferErrorShape<TRouter[TEndpoint]>>;
} else {
error = INTERNAL_DO_NOT_USE__fatalClientError(e as Error);
console.error(
Expand Down
10 changes: 5 additions & 5 deletions packages/svelte/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
UploadThingError,
} from "@uploadthing/shared";
import type {
AnyFileRoute,
ClientUploadedFileData,
EndpointArg,
FileRouter,
Expand All @@ -28,9 +29,8 @@ export interface GenerateTypedHelpersOptions {
}

export type UseUploadthingProps<
TRouter extends FileRouter,
TEndpoint extends keyof TRouter,
TServerOutput = inferEndpointOutput<TRouter[TEndpoint]>,
TFileRoute extends AnyFileRoute,
TServerOutput = inferEndpointOutput<TFileRoute>,
> = {
/**
* Called when the upload is submitted and the server is about to be queried for presigned URLs
Expand Down Expand Up @@ -74,7 +74,7 @@ export type UseUploadthingProps<
* Called if the upload fails
*/
onUploadError?:
| ((e: UploadThingError<inferErrorShape<TRouter>>) => MaybePromise<void>)
| ((e: UploadThingError<inferErrorShape<TFileRoute>>) => MaybePromise<void>)
| undefined;
/**
* Set custom headers that'll get sent with requests
Expand All @@ -94,7 +94,7 @@ export type UploadthingComponentProps<
TRouter extends FileRouter,
TEndpoint extends keyof TRouter,
> = Omit<
UseUploadthingProps<TRouter, TEndpoint>,
UseUploadthingProps<TRouter[TEndpoint]>,
/**
* Signal is omitted, component has its own AbortController
* If you need to control the interruption with more granularity,
Expand Down
4 changes: 2 additions & 2 deletions packages/uploadthing/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const genUploader = <TRouter extends FileRouter>(
>(
slug: EndpointArg<TRouter, TEndpoint>,
opts: Omit<
CreateUploadOptions<TRouter, TEndpoint>,
CreateUploadOptions<TRouter[TEndpoint]>,
keyof GenerateUploaderOptions
>,
) => {
Expand Down Expand Up @@ -249,7 +249,7 @@ export const genUploader = <TRouter extends FileRouter>(
const typedUploadFiles = <TEndpoint extends keyof TRouter>(
slug: EndpointArg<TRouter, TEndpoint>,
opts: Omit<
UploadFilesOptions<TRouter, TEndpoint>,
UploadFilesOptions<TRouter[TEndpoint]>,
keyof GenerateUploaderOptions
>,
) => {
Expand Down
7 changes: 3 additions & 4 deletions packages/uploadthing/src/internal/error-formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ export function defaultErrorFormatter(error: UploadThingError) {
};
}

export function formatError<TRouter extends FileRouter>(
export function formatError(
error: UploadThingError,
router: TRouter,
): inferErrorShape<TRouter> {
router: FileRouter,
): inferErrorShape<FileRouter[string]> {
const errorFormatter =
router[Object.keys(router)[0]]?.errorFormatter ?? defaultErrorFormatter;

// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return errorFormatter(error);
}
10 changes: 5 additions & 5 deletions packages/uploadthing/src/internal/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import {
UploadThingHook,
} from "./shared-schemas";
import { UTFiles } from "./types";
import type { AnyUploader, MiddlewareFnArgs, UTEvents } from "./types";
import type { AnyFileRoute, MiddlewareFnArgs, UTEvents } from "./types";

export class MiddlewareArguments extends Context.Tag(
"uploadthing/MiddlewareArguments",
Expand Down Expand Up @@ -228,7 +228,7 @@ export const createRequestHandler = <TRouter extends FileRouter>(
);
}).pipe(Effect.withLogSpan("createRequestHandler"));

const handleErrorRequest = (opts: { uploadable: AnyUploader }) =>
const handleErrorRequest = (opts: { uploadable: AnyFileRoute }) =>
Effect.gen(function* () {
const { uploadable } = opts;
const request = yield* HttpServerRequest.HttpServerRequest;
Expand Down Expand Up @@ -289,7 +289,7 @@ const handleErrorRequest = (opts: { uploadable: AnyUploader }) =>
}).pipe(Effect.withLogSpan("handleErrorRequest"));

const handleCallbackRequest = (opts: {
uploadable: AnyUploader;
uploadable: AnyFileRoute;
fePackage: string;
beAdapter: string;
}) =>
Expand Down Expand Up @@ -381,7 +381,7 @@ const handleCallbackRequest = (opts: {

const runRouteMiddleware = (opts: {
json: typeof UploadActionPayload.Type;
uploadable: AnyUploader;
uploadable: AnyFileRoute;
}) =>
Effect.gen(function* () {
const middlewareArgs = yield* MiddlewareArguments;
Expand Down Expand Up @@ -441,7 +441,7 @@ const runRouteMiddleware = (opts: {
}).pipe(Effect.withLogSpan("runRouteMiddleware"));

const handleUploadAction = (opts: {
uploadable: AnyUploader;
uploadable: AnyFileRoute;
fePackage: string;
beAdapter: string;
slug: string;
Expand Down
6 changes: 3 additions & 3 deletions packages/uploadthing/src/internal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export interface UploadBuilder<TParams extends AnyParams> {
>,
TOutput
>,
) => Uploader<{
) => FileRoute<{
input: TParams["_input"]["in"] extends UnsetMarker
? void
: TParams["_input"]["in"];
Expand All @@ -158,7 +158,7 @@ export type AnyBuiltUploaderTypes = {
errorShape: any;
};

export interface Uploader<TTypes extends AnyBuiltUploaderTypes> {
export interface FileRoute<TTypes extends AnyBuiltUploaderTypes> {
$types: TTypes;
routerConfig: FileRouterInputConfig;
routeOptions: RouteOptions;
Expand All @@ -168,7 +168,7 @@ export interface Uploader<TTypes extends AnyBuiltUploaderTypes> {
errorFormatter: (err: UploadThingError) => any;
onUploadComplete: UploadCompleteFn<any, any>;
}
export type AnyUploader = Uploader<AnyBuiltUploaderTypes>;
export type AnyFileRoute = FileRoute<AnyBuiltUploaderTypes>;

/**
* Map actionType to the required payload for that action
Expand Down
9 changes: 4 additions & 5 deletions packages/uploadthing/src/internal/upload-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,18 @@ import type {
import { defaultErrorFormatter } from "./error-formatter";
import type {
AnyBuiltUploaderTypes,
AnyUploader,
AnyFileRoute,
MiddlewareFnArgs,
UnsetMarker,
UploadBuilder,
Uploader,
} from "./types";

function internalCreateBuilder<
TMiddlewareArgs extends MiddlewareFnArgs<any, any, any>,
TRouteOptions extends RouteOptions,
TErrorShape extends Json = { message: string },
>(
initDef: Partial<AnyUploader> = {},
initDef: Partial<AnyFileRoute> = {},
): UploadBuilder<{
_routeOptions: TRouteOptions;
_input: { in: UnsetMarker; out: UnsetMarker };
Expand All @@ -30,7 +29,7 @@ function internalCreateBuilder<
_errorFn: UnsetMarker;
_output: UnsetMarker;
}> {
const _def: AnyUploader = {
const _def: AnyFileRoute = {
$types: {} as AnyBuiltUploaderTypes,
// Default router config
routerConfig: {
Expand Down Expand Up @@ -77,7 +76,7 @@ function internalCreateBuilder<
return {
..._def,
onUploadComplete: userUploadComplete,
} as Uploader<any>;
} as AnyFileRoute;
},
onUploadError(userOnUploadError) {
return internalCreateBuilder({
Expand Down
2 changes: 1 addition & 1 deletion packages/uploadthing/src/internal/upload.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export const uploadFilesInternal = <
TServerOutput = inferEndpointOutput<TRouter[TEndpoint]>,
>(
endpoint: TEndpoint,
opts: UploadFilesOptions<TRouter, TEndpoint>,
opts: UploadFilesOptions<TRouter[TEndpoint]>,
): Micro.Micro<
ClientUploadedFileData<TServerOutput>[],
UploadThingError | FetchError
Expand Down
Loading

0 comments on commit 4004ea8

Please sign in to comment.