Files
Send a file up, or return one as a download - mix it with regular data.
File and Blob are first-class. Mix them with objects. HTTP switches to multipart when needed; JSON stays JSON. A lone returned File is a download (Content-Disposition).
import { const tacho: <C extends Context = {}>() => Builder<C, undefined, unique symbol>tacho } from "tacho";
import { import zz } from "zod";
const const rpc: Builder<{}, undefined, unique symbol>rpc = tacho<{}>(): Builder<{}, undefined, unique symbol>tacho();
export const const router: {
upload: ProcedureDef<{}, {
file: File;
note: string;
}, string>;
download: ProcedureDef<{}, undefined, File>;
}
router = const rpc: <{
upload: ProcedureDef<{}, {
file: File;
note: string;
}, string>;
download: ProcedureDef<{}, undefined, File>;
}>(def: {
upload: ProcedureDef<{}, {
file: File;
note: string;
}, string>;
download: ProcedureDef<{}, undefined, File>;
}) => {
upload: ProcedureDef<{}, {
file: File;
note: string;
}, string>;
download: ProcedureDef<{}, undefined, File>;
}
rpc({
upload: ProcedureDef<{}, {
file: File;
note: string;
}, string>
upload: const rpc: Builder<{}, undefined, unique symbol>rpc
.input<{
file: File;
note: string;
}>(schema: Schema<{
file: File;
note: string;
}>): Builder<{}, {
file: File;
note: string;
}, unique symbol>
input(import zz.function object<{
file: z.ZodCustom<File, File>;
note: z.ZodString;
}>(shape?: {
file: z.ZodCustom<File, File>;
note: z.ZodString;
} | undefined, params?: string | {
error?: string | z.core.$ZodErrorMap<NonNullable<z.core.$ZodIssueInvalidType<unknown> | z.core.$ZodIssueUnrecognizedKeys>> | undefined;
message?: string | undefined | undefined;
} | undefined): z.ZodObject<{
file: z.ZodCustom<File, File>;
note: z.ZodString;
}, z.core.$strip>
object({ file: z.ZodCustom<File, File>file: import zz.instanceof<{
new (fileBits: BlobPart[], fileName: string, options?: FilePropertyBag): File;
prototype: File;
}>(cls: {
new (fileBits: BlobPart[], fileName: string, options?: FilePropertyBag): File;
prototype: File;
}, params?: {
when?: ((payload: z.core.ParsePayload) => boolean) | undefined | undefined;
error?: string | z.core.$ZodErrorMap<z.core.$ZodIssueCustom> | undefined;
message?: string | undefined | undefined;
}): z.ZodCustom<File, File>
export instanceof
instanceof(var File: {
new (fileBits: BlobPart[], fileName: string, options?: FilePropertyBag): File;
prototype: File;
}
The **`File`** interface provides information about files and allows JavaScript in a web page to access their content.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/File)File), note: z.ZodStringnote: import zz.function string(params?: string | z.core.$ZodStringParams): z.ZodString (+1 overload)string() }))
.run: <string>(fn: (opts: {
input: {
file: File;
note: string;
};
ctx: {};
}) => string | Promise<string>) => ProcedureDef<{}, {
file: File;
note: string;
}, string>
run(({ input: {
file: File;
note: string;
}
input }) => input: {
file: File;
note: string;
}
input.file: Filefile.File.name: stringThe **`name`** read-only property of the File interface returns the name of the file represented by a File object. For security reasons, the path is excluded from this property.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/File/name)name),
download: ProcedureDef<{}, undefined, File>download: const rpc: Builder<{}, undefined, unique symbol>rpc.run: <File>(fn: (opts: {
input: undefined;
ctx: {};
}) => File | Promise<File>) => ProcedureDef<{}, undefined, File>
run(
() => new var File: new (fileBits: BlobPart[], fileName: string, options?: FilePropertyBag) => FileThe **`File`** interface provides information about files and allows JavaScript in a web page to access their content.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/File)File(["hello"], "hello.txt", { BlobPropertyBag.type?: string | undefinedtype: "text/plain" }),
),
});
export type type Router = {
upload: ProcedureDef<{}, {
file: File;
note: string;
}, string>;
download: ProcedureDef<{}, undefined, File>;
}
Router = typeof const router: {
upload: ProcedureDef<{}, {
file: File;
note: string;
}, string>;
download: ProcedureDef<{}, undefined, File>;
}
router;
// @filename: client.ts
import { function createClient<R>(opts: ClientOptions): RPCClient<R>createClient } from "tacho/client/http";
import type { type Router = {
upload: ProcedureDef<{}, {
file: File;
note: string;
}, string>;
download: ProcedureDef<{}, undefined, File>;
}
Router } from "./server";
const const client: RPCClient<{
upload: ProcedureDef<{}, {
file: File;
note: string;
}, string>;
download: ProcedureDef<{}, undefined, File>;
}>
client = createClient<{
upload: ProcedureDef<{}, {
file: File;
note: string;
}, string>;
download: ProcedureDef<{}, undefined, File>;
}>(opts: ClientOptions): RPCClient<{
upload: ProcedureDef<{}, {
file: File;
note: string;
}, string>;
download: ProcedureDef<{}, undefined, File>;
}>
createClient<type Router = {
upload: ProcedureDef<{}, {
file: File;
note: string;
}, string>;
download: ProcedureDef<{}, undefined, File>;
}
Router>({
url: stringurl: "http://localhost:3000",
});
await const client: RPCClient<{
upload: ProcedureDef<{}, {
file: File;
note: string;
}, string>;
download: ProcedureDef<{}, undefined, File>;
}>
client.upload: (input: {
file: File;
note: string;
}, opts?: CallOptions | undefined) => Promise<string>
upload({
file: Filefile: new var File: new (fileBits: BlobPart[], fileName: string, options?: FilePropertyBag) => FileThe **`File`** interface provides information about files and allows JavaScript in a web page to access their content.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/File)File(["hi"], "hi.txt"),
note: stringnote: "ok",
});
const const file: Filefile = await const client: RPCClient<{
upload: ProcedureDef<{}, {
file: File;
note: string;
}, string>;
download: ProcedureDef<{}, undefined, File>;
}>
client.download: (input?: undefined, opts?: CallOptions | undefined) => Promise<File>download();
await const file: Filefile.Blob.text(): Promise<string>The **`text()`** method of the Blob interface returns a Promise that resolves with a string containing the contents of the blob, interpreted as UTF-8.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/text)text();