All files / src/shared response-helpers.ts

100% Statements 23/23
100% Branches 27/27
100% Functions 17/17
100% Lines 23/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180                            22x                           2x             1x             2x                     3x             2x             1x             2x             2x                   2x                   2x                   2x                     4x 4x                         2x                     2x                             4x 4x 2x   4x 2x   4x               1x    
/**
 * Response helper functions for route handlers
 * Provides convenient functions to create HTTP responses with status codes and headers
 */
 
import type { VitekResponse } from '../core/context/create-context.js';
 
/**
 * Creates a JSON response with status code and optional headers
 */
export function json(
  body: any,
  options: { status?: number; headers?: Record<string, string> } = {}
): VitekResponse {
  return {
    status: options.status || 200,
    headers: {
      'Content-Type': 'application/json',
      ...options.headers,
    },
    body,
  };
}
 
/**
 * Creates a 200 OK response
 */
export function ok(body: any, headers?: Record<string, string>): VitekResponse {
  return json(body, { status: 200, headers });
}
 
/**
 * Creates a 201 Created response
 */
export function created(body: any, headers?: Record<string, string>): VitekResponse {
  return json(body, { status: 201, headers });
}
 
/**
 * Creates a 204 No Content response
 */
export function noContent(headers?: Record<string, string>): VitekResponse {
  return {
    status: 204,
    headers: headers || {},
    body: undefined,
  };
}
 
/**
 * Creates a 400 Bad Request response
 */
export function badRequest(body: any = { error: 'Bad request' }, headers?: Record<string, string>): VitekResponse {
  return json(body, { status: 400, headers });
}
 
/**
 * Creates a 401 Unauthorized response
 */
export function unauthorized(body: any = { error: 'Unauthorized' }, headers?: Record<string, string>): VitekResponse {
  return json(body, { status: 401, headers });
}
 
/**
 * Creates a 403 Forbidden response
 */
export function forbidden(body: any = { error: 'Forbidden' }, headers?: Record<string, string>): VitekResponse {
  return json(body, { status: 403, headers });
}
 
/**
 * Creates a 404 Not Found response
 */
export function notFound(body: any = { error: 'Not found' }, headers?: Record<string, string>): VitekResponse {
  return json(body, { status: 404, headers });
}
 
/**
 * Creates a 409 Conflict response
 */
export function conflict(body: any = { error: 'Conflict' }, headers?: Record<string, string>): VitekResponse {
  return json(body, { status: 409, headers });
}
 
/**
 * Creates a 422 Unprocessable Entity response (validation errors)
 */
export function unprocessableEntity(
  body: any = { error: 'Validation error' },
  headers?: Record<string, string>
): VitekResponse {
  return json(body, { status: 422, headers });
}
 
/**
 * Creates a 429 Too Many Requests response
 */
export function tooManyRequests(
  body: any = { error: 'Too many requests' },
  headers?: Record<string, string>
): VitekResponse {
  return json(body, { status: 429, headers });
}
 
/**
 * Creates a 500 Internal Server Error response
 */
export function internalServerError(
  body: any = { error: 'Internal server error' },
  headers?: Record<string, string>
): VitekResponse {
  return json(body, { status: 500, headers });
}
 
/**
 * Creates a redirect response (301, 302, 307, 308)
 */
export function redirect(
  url: string,
  permanent: boolean = false,
  preserveMethod: boolean = false
): VitekResponse {
  const status = permanent ? (preserveMethod ? 308 : 301) : preserveMethod ? 307 : 302;
  return {
    status,
    headers: {
      Location: url,
    },
    body: undefined,
  };
}
 
/**
 * Creates a plain text response (Content-Type: text/plain).
 */
export function text(body: string, status: number = 200): VitekResponse {
  return {
    status,
    headers: { 'Content-Type': 'text/plain; charset=utf-8' },
    body,
  };
}
 
/**
 * Creates an HTML response (Content-Type: text/html).
 */
export function html(body: string, status: number = 200): VitekResponse {
  return {
    status,
    headers: { 'Content-Type': 'text/html; charset=utf-8' },
    body,
  };
}
 
/**
 * Cache-Control header helpers. Merge returned headers into your response, e.g.:
 * `{ ...ok(body), headers: { ...ok(body).headers, ...cacheControl(60) } }`
 */
export function cacheControl(
  maxAgeSeconds: number,
  options?: { staleWhileRevalidate?: number; private?: boolean }
): Record<string, string> {
  const parts = [`max-age=${maxAgeSeconds}`];
  if (options?.staleWhileRevalidate != null) {
    parts.push(`stale-while-revalidate=${options.staleWhileRevalidate}`);
  }
  if (options?.private === true) {
    parts.push('private');
  }
  return { 'Cache-Control': parts.join(', ') };
}
 
/**
 * Returns headers to disable caching. Merge into response headers, e.g.:
 * `{ ...ok(body), headers: { ...ok(body).headers, ...noStore() } }`
 */
export function noStore(): Record<string, string> {
  return { 'Cache-Control': 'no-store' };
}