@preprio/prepr-nextjs
Version:
Next.js package for Prepr CMS preview functionality with advanced debugging and visual editing capabilities
84 lines (81 loc) • 3.38 kB
TypeScript
import { GetServerSidePropsContext, GetStaticPropsContext } from 'next';
import { PreprErrorCode, PreprSegment, PreprToolbarProps } from '../../types/index.js';
export { default as createPreprMiddleware } from '../../middleware/index.js';
import 'next/server';
/**
* Custom error class for Prepr-related errors
*/
declare class PreprError extends Error {
readonly code: PreprErrorCode;
readonly context?: string | undefined;
readonly originalError?: Error | undefined;
constructor(message: string, code: PreprErrorCode, context?: string | undefined, originalError?: Error | undefined);
}
/**
* Returns the Prepr Customer ID from the context headers
* @param context - The Next.js context object
* @returns Prepr Customer ID
*/
declare function getPreprUUID(context: GetServerSidePropsContext | GetStaticPropsContext): string | null;
/**
* Returns the active segment from the context headers
* @param context - The Next.js context object
* @returns Active segment
*/
declare function getActiveSegment(context: GetServerSidePropsContext | GetStaticPropsContext): string | null;
/**
* Returns the active variant from the context headers
* @param context - The Next.js context object
* @returns Active variant
*/
declare function getActiveVariant(context: GetServerSidePropsContext | GetStaticPropsContext): string | null;
/**
* Helper function to retrieve all Prepr headers from context
* @param context - The Next.js context object
* @returns Object with Prepr headers
*/
declare function getPreprHeaders(context: GetServerSidePropsContext | GetStaticPropsContext): Record<string, string>;
/**
* Validates a Prepr GraphQL token
* @param token - The token to validate
* @returns Validation result with error details if invalid
*/
declare function validatePreprToken(token: string): {
valid: boolean;
error?: string;
};
/**
* Checks if the current environment is in preview mode
* @returns True if in preview mode
*/
declare function isPreviewMode(): boolean;
/**
* Extracts the access token from a Prepr GraphQL URL
* @param graphqlUrl - The full Prepr GraphQL URL
* @returns The access token or null if invalid
* @example
* ```typescript
* const token = extractAccessToken('https://graphql.prepr.io/abc123')
* // Returns: 'abc123'
* ```
*/
declare function extractAccessToken(graphqlUrl: string): string | null;
/**
* Fetches the segments from the Prepr API
* @param token Prepr GraphQL URL with scope 'segments'
* @returns Array of PreprSegment
* @throws PreprError if the request fails
*/
declare function getPreprEnvironmentSegments(token: string): Promise<PreprSegment[]>;
/**
* Fetches all the necessary toolbar props for Pages directory
* @param token Prepr GraphQL URL with scope 'segments'
* @param context The Next.js context object from getServerSideProps
* @returns Object with activeSegment, activeVariant and data
*/
declare function getToolbarProps(token: string, context: GetServerSidePropsContext | GetStaticPropsContext): Promise<PreprToolbarProps>;
/**
* Alias for getToolbarProps for consistency with App Router API
*/
declare const getPreviewBarProps: typeof getToolbarProps;
export { PreprError, extractAccessToken, getActiveSegment, getActiveVariant, getPreprEnvironmentSegments, getPreprHeaders, getPreprUUID, getPreviewBarProps, getToolbarProps, isPreviewMode, validatePreprToken };