index.d.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /// <reference types="node" />
  2. import type { IncomingMessage } from 'http';
  3. import type { BaseNextRequest } from '../base-http';
  4. import { NextApiRequest, NextApiResponse } from '../../shared/lib/utils';
  5. export declare type NextApiRequestCookies = Partial<{
  6. [key: string]: string;
  7. }>;
  8. export declare type NextApiRequestQuery = Partial<{
  9. [key: string]: string | string[];
  10. }>;
  11. export declare type __ApiPreviewProps = {
  12. previewModeId: string;
  13. previewModeEncryptionKey: string;
  14. previewModeSigningKey: string;
  15. };
  16. /**
  17. * Parse cookies from the `headers` of request
  18. * @param req request object
  19. */
  20. export declare function getCookieParser(headers: {
  21. [key: string]: undefined | string | string[];
  22. }): () => NextApiRequestCookies;
  23. /**
  24. *
  25. * @param res response object
  26. * @param statusCode `HTTP` status code of response
  27. */
  28. export declare function sendStatusCode(res: NextApiResponse, statusCode: number): NextApiResponse<any>;
  29. /**
  30. *
  31. * @param res response object
  32. * @param [statusOrUrl] `HTTP` status code of redirect
  33. * @param url URL of redirect
  34. */
  35. export declare function redirect(res: NextApiResponse, statusOrUrl: string | number, url?: string): NextApiResponse<any>;
  36. export declare const PRERENDER_REVALIDATE_HEADER = "x-prerender-revalidate";
  37. export declare const PRERENDER_REVALIDATE_ONLY_GENERATED_HEADER = "x-prerender-revalidate-if-generated";
  38. export declare function checkIsManualRevalidate(req: IncomingMessage | BaseNextRequest, previewProps: __ApiPreviewProps): {
  39. isManualRevalidate: boolean;
  40. revalidateOnlyGenerated: boolean;
  41. };
  42. export declare const COOKIE_NAME_PRERENDER_BYPASS = "__prerender_bypass";
  43. export declare const COOKIE_NAME_PRERENDER_DATA = "__next_preview_data";
  44. export declare const RESPONSE_LIMIT_DEFAULT: number;
  45. export declare const SYMBOL_PREVIEW_DATA: unique symbol;
  46. export declare const SYMBOL_CLEARED_COOKIES: unique symbol;
  47. export declare function clearPreviewData<T>(res: NextApiResponse<T>, options?: {
  48. path?: string;
  49. }): NextApiResponse<T>;
  50. /**
  51. * Custom error class
  52. */
  53. export declare class ApiError extends Error {
  54. readonly statusCode: number;
  55. constructor(statusCode: number, message: string);
  56. }
  57. /**
  58. * Sends error in `response`
  59. * @param res response object
  60. * @param statusCode of response
  61. * @param message of response
  62. */
  63. export declare function sendError(res: NextApiResponse, statusCode: number, message: string): void;
  64. interface LazyProps {
  65. req: NextApiRequest;
  66. }
  67. /**
  68. * Execute getter function only if its needed
  69. * @param LazyProps `req` and `params` for lazyProp
  70. * @param prop name of property
  71. * @param getter function to get data
  72. */
  73. export declare function setLazyProp<T>({ req }: LazyProps, prop: string, getter: () => T): void;
  74. export {};