Originally posted by @KATT in https://github.com/trpc/trpc/discussions/1936#discussioncomment-2818345
If we go down this route what we're really talking about is straying away from JSON as the defacto mode of transport. If we do this we should also have support for some sort of binary format when doing POST
s that could take the compression further and let us have something else than content-type: application/json
.
Furthermore, I think the server should be able to receive different types of request types and be informed on what to do. I have thought about this a bit before in the context of transformers, so outlining below.
Rough ideas below, I think best by writing code, but don't see any of this as a final design.
contentType
-paramGoals:
Response
with a body
Fetch.body
When creating a server:
import { initTRPC } from '@trpc/server';
import { jsonContentType, createContentType } from '@trpc/server/content-type';
// JSON crush could be a community plugin that looks something like this:
import jsoncrush from 'jsoncrush';
const crush = createContentType({
key: 'crush',
contentType: 'application/json',
// a smarter content type would use something that could be streamed, i.e ReadableStream from `Response`
// used for `POST`:
fromResponse: async (res) => jsoncrush.uncrush(await res.text()),
fromString: (str) => jsoncrush.uncrush(str),
toBody: data => jsoncrush.crush(data), // this could allow for the types that the `Fetch API` allows - ArrayBuffer, string, [...]
// used for `GET`
toString: data => jsoncrush.data(data),
})
const trpc = initTRPC({
contentTypes: [
jsonContentType,
crush,
}
});
HTTP level
// inform server that we are sending `crush` content
GET http://localhost?content=crush&input=[...]
GET http://localhost?content=json&input=[...]
POST http://localhost?content=crush
content-type: "application/json"
// [..]
transformer
-param could be usedimport { initTRPC } from '@trpc/server';
import { defaultTransform } from '@trpc/server/transform';
// JSON crush could be a community plugin
import { crush } from 'trpc-json-crush';
const trpc = initTRPC({
transforms: [
defaultTransform, // our "no-op" trnasform
{ key: "superjson", value: superjson },
],
});
Maybe only needs to allow for one content type.
import { crush } from "~/shared/contentType"
const client = createTRPCClient({
links: [ /* ... */ ],
contentType: crush,
});
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too