Client & RPC

Client

Client-side utilities and patterns in Manic.

Client

Patterns for browser-only code, plus the typed RPC helper.


API reference


Fetch pattern

The recommended way to call APIs without RPC typing:

// Basic fetch pattern
async function () {
  const  = await ('/api/users');
  const  = (await .()) as { : <{ : number; : string }> };
  return .;
}

// Usage in component
const  = await ();

Environment Variables (Client-Side)

Access public environment variables in the browser via manicjs/env:

// Access public environment variables
declare const : { ?: string };

const  = . || 'https://api.example.com';
.('API:', );

See Environment Variables for full documentation.

Client-Side Only Code

Use environment checks for client-specific logic:

// Client-side only code
if (typeof  !== 'undefined' && 'localStorage' in ) {
  // This code only runs in the browser
  .('key', 'value');
  const  = .('key');
  .();
}

Type-Safe API Calls

Share types with your API routes for full type-safety. First, export the type from your API:

// API route type export
interface UserResponse {
  : <{ : number; : string }>;
}

// This would be exported from app/api/users/index.ts
type  = {
  : {
    : () => <UserResponse>;
  };
};

Then use in client code:

// Client-side API call
interface User {
  : number;
  : string;
}

interface UserResponse {
  : User[];
}

async function (): <User[]> {
  const  = await ('/api/users');
  const  = (await .()) as UserResponse;
  return .;
}

// Usage
const  = await ();
.( => .(.));

Prefetching Routes

Manic's <Link> component automatically prefetches route chunks on hover/focus:

// Link component automatically prefetches
interface LinkProps {
  : string;
  : string;
  ?: () => void;
}

// Usage in your app:
// <Link href="/about">About</Link> 
// Automatically prefetches /about route on hover
declare function (: LinkProps): any;

Making API Requests

Common patterns for making API requests from the client:

// POST request
async function (: string, : string) {
  const  = await ('/api/users', {
    : 'POST',
    : { 'Content-Type': 'application/json' },
    : .({ ,  }),
  });
  
  if (!.) throw new ('Failed to create user');
  return .();
}

// GET with query parameters
async function (: string) {
  const  = new ({ :  });
  const  = await (`/api/users/search?${}`);
  return .();
}

// DELETE request
async function (: number) {
  const  = await (`/api/users/${}`, { : 'DELETE' });
  return .;
}

Error Handling

Properly handle API errors in client code:

// Error handling pattern
interface ApiError {
  : string;
  ?: string;
}

async function (: string) {
  try {
    const  = await ();
    
    if (!.) {
      const  = (await .()) as ApiError;
      throw new (. || 'Request failed');
    }
    
    return .();
  } catch () {
    .('Fetch error:', );
    throw ;
  }
}

See Also

On this page