Skip to Content
Calls

Upload files

Upload files with a typed multipart body.

Upload files with a body that's typed and checked by a schema, as for JSON, then sent as multipart/form-data.

Send a multipart body

To upload files, wrap the input schema in Fetch.multipart, and use Fetch.UploadFile for a file field:

import * as  from "effect/Effect";
import * as  from "effect/Schema";

import {  } from "@monarcode/effetch";

const  = .({ : . });

export const  = .("POST /documents", {
  : .(
    .({
      : .,
      : .(["PASSPORT", "DRIVERS_LICENSE"]),
      : .(.),
    }),
  ),
  : ,
});

class  extends .<>()("Api", {
  : "https://api.example.com",
}) {}

class  extends .<>()("DocumentApi", , {
  : ,
}) {}

// In a browser, `file` comes from an <input type="file">.
export const  = (: ) =>
  .(function* () {
    const  = yield* ;
    return yield* .({ : { , : "PASSPORT" } });
  });

Files on each platform

Fetch.UploadFile takes the files each platform can send:

  • in browsers, Node, Bun and Deno, a Blob or File;
  • in React Native, the { uri, name, type } object its pickers give, typed Fetch.NativeFile. uri and name are required: a server only treats a part as a file when it has a file name. type is optional, and a file without one is sent as application/octet-stream.

Use Fetch.UploadFile rather than Schema.File. Schema.File only takes a File, not the { uri, name, type } objects React Native's pickers give, and FetchTest can't read it back from a form.

Send a file from React Native

In React Native, send a NativeFile, not a Blob: React Native's fetch reads a form's files from disk by uri, and can't send a Blob in a form.

The following example builds a NativeFile from an image picker's asset:

import type {  } from "@monarcode/effetch";

// From expo-image-picker, for example
declare const : {
  readonly : string;
  readonly ?: string | null;
  readonly ?: string;
};

export const : .NativeFile = {
  : .,
  : . ?? "photo.jpg",
  : .,
};

How fields become form parts

Effetch turns the schema's fields into form parts as follows:

  • strings as they are; numbers, booleans and bigints as strings; a Date as its ISO string;
  • a file as a file part;
  • a list as one part per item, under the same name;
  • null and undefined left out.

A form can't hold a nested object or a list of lists, so TypeScript rejects most schemas that have one, and a value that has one makes the call die. To send one, encode it to a string in the schema, such as meta: Schema.fromJsonString(Meta).

Check a file's size

To check a file's size, add a check to Fetch.UploadFile. A NativeFile has no size until it's read, so the check only looks at a Blob:

import * as  from "effect/Schema";

import {  } from "@monarcode/effetch";

export const  = ..(
  .(
    () => !( instanceof ) || . < 10_000_000,
    { : "a file under 10 MB" },
  ),
);

Headers, errors and where it works

The following details cover the request's headers, how a bad body fails, and where multipart bodies work.

What's next

On this page