Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: support for formdata in transformersΒ #7028

@darklight9811

Description

@darklight9811

Describe the feature you'd like to request

Basically I automated the client to parse every object with a File into FormData and did the splitLink, but sadly there's no way to automate this on the server, so I always need to verify and parse on the input (something that I can end up forgeting, specially in larger projects).

Example of client code:

export const client = createTRPCClient<AppRouter>({
	links: [
		function formDataLink() {
			return ({ op, next }) => {
				return observable((observer) => {
					if (!containsFile(op.input as Record<string, unknown>)) {
						op.input = objectToFormData(op.input);
					}
					return next(op).subscribe({
						next(value) {
							observer.next(value);
						},
						error(err) {
							observer.error(err);
						},
						complete() {
							observer.complete();
						},
					});
				});
			};
		},
		loggerLink(),
		splitLink({
			condition: (op) => isNonJsonSerializable(op.input),
			true: httpLink({
				transformer: transformer,
				url: `${getBaseUrl()}/api/trpc`,
			}),
			false: httpBatchLink({
				transformer: transformer,
				url: `${getBaseUrl()}/api/trpc`,
			}),
		}),
	],
});

Describe the solution you'd like to see

Currently, transformer is not called when you pass a FormData and goes directly into the input validation. I would suggest a way to handle non string/json in the server. Maybe transformFormData?

Describe alternate solutions

  • Always remember to wrap input into zfd.formData
  • Hack a way to pass the correct body to trpc in the server before it reaches it.

Additional information

No response

πŸ‘¨β€πŸ‘§β€πŸ‘¦ Contributing

  • πŸ™‹β€β™‚οΈ Yes, I'd be down to file a PR implementing this feature!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions