@pagamio/frontend-commons-lib
Version:
Pagamio library for Frontend reusable components like the form engine and table container
197 lines (196 loc) • 6.92 kB
TypeScript
import type { ApiResponseTransformer, AuthResponse, CommerceAppAuthConfig, EventsAppAuthConfig, VasAppAuthConfig } from '../types';
/**
* Transforms authentication responses from the Events App API format into a standardized AuthResponse.
* This transformer handles responses that include a single token and role-based user information.
*
* Expected API response format:
* ```typescript
* {
* token: string;
* firstName: string;
* lastName: string;
* username: string;
* roles: string[] | null;
* roleName: string;
* roleDesc: string;
* vendorId: number;
* promoterId: number | null;
* company: string;
* }
* ```
*
* @implements {ApiResponseTransformer<EventsAppAuthConfig>}
*
* @example
* ```typescript
* const transformer = new EventsAppResponseTransformer();
* const response = {
* token: "jwt.token.here",
* firstName: "John",
* lastName: "Doe",
* username: "john.doe",
* roleName: "ADMIN"
* // ... other fields
* };
*
* if (transformer.canHandle(response)) {
* const authResponse = transformer.transform(response);
* // Use transformed response
* }
* ```
*/
export declare class EventsAppResponseTransformer implements ApiResponseTransformer<EventsAppAuthConfig> {
/**
* Checks if the given response matches the Event App API format.
* Verifies the presence of required fields specific to Event App responses.
*
* @param response - The raw API response to check
* @returns True if the response contains 'token' and 'roleName' fields
*/
canHandle(response: any): boolean;
/**
* Transforms a Event App API response into the standardized AuthResponse format.
* Maps API-specific fields to the common auth response structure.
*
* @param response - The raw API response to transform
* @param remember
* @returns Standardized auth response with user and token information
* @throws Error if required fields are missing from the response
*/
transform(response: any, remember?: boolean): AuthResponse<EventsAppAuthConfig>;
}
/**
* Transforms authentication responses from the Vas App API format into a standardized AuthResponse.
* This transformer handles responses that include both access and refresh tokens.
*
* Expected API response format:
* ```typescript
* {
* accessToken: string;
* refreshToken: string;
* accessTokenExpiresIn: number;
* refreshTokenExpiresIn: number;
* username: string;
* roles: string[];
* userId: number;
* }
* ```
*
* @implements {ApiResponseTransformer<VasAppAuthConfig>}
*
* @example
* ```typescript
* const transformer = new VasAppResponseTransformer();
* const response = {
* accessToken: "access.token.here",
* refreshToken: "refresh.token.here",
* accessTokenExpiresIn: 3600,
* username: "vendor_user"
* // ... other fields
* };
*
* if (transformer.canHandle(response)) {
* const authResponse = transformer.transform(response);
* // Use transformed response
* }
* ```
*/
export declare class VasAppResponseTransformer implements ApiResponseTransformer<VasAppAuthConfig> {
/**
* Checks if the given response matches the Vas App API format.
* Verifies the presence of required fields specific to Vendor responses.
*
* @param response - The raw API response to check
* @returns True if the response contains both 'accessToken' and 'refreshToken' fields
*/
canHandle(response: any): boolean;
/**
* Transforms a Vas App API response into the standardized AuthResponse format.
* Maps API-specific fields to the common auth response structure.
*
* @param response - The raw API response to transform
* @param remember
* @returns Standardized auth response with user and token information
* @throws Error if required fields are missing from the response
*/
transform(response: any, remember?: boolean): AuthResponse<VasAppAuthConfig>;
}
/**
* Transforms authentication responses from the Commerce App API format into a standardized AuthResponse.
* This transformer handles responses wrapped in a success/data structure.
*
* Expected API response format:
* ```typescript
* {
* success: boolean;
* data: {
* user: {...};
* accessToken: string;
* refreshToken: string;
* };
* }
* ```
*
* @implements {ApiResponseTransformer<CommerceAppAuthConfig>}
*/
export declare class CommerceAppResponseTransformer implements ApiResponseTransformer<CommerceAppAuthConfig> {
/**
* Checks if the given response matches the Commerce App API format.
* Verifies the presence of required fields specific to Commerce App responses.
*
* @param response - The raw API response to check
* @returns True if the response contains 'success' and 'data' fields with user and tokens
*/
canHandle(response: any): boolean;
/**
* Transforms a Commerce App API response into the standardized AuthResponse format.
* Maps API-specific fields to the common auth response structure.
*
* @param response - The raw API response to transform
* @param remember - Whether to use extended token expiration
* @returns Standardized auth response with user and token information
* @throws Error if required fields are missing from the response
*/
transform(response: any, remember?: boolean): AuthResponse<CommerceAppAuthConfig>;
}
/**
* Factory class for creating and managing response transformers.
* Implements the Factory pattern to dynamically select the appropriate transformer
* based on the API response format.
*
* @example
* ```typescript
* const apiResponse = await fetchFromApi();
* const transformer = ResponseTransformerFactory.getTransformer(apiResponse);
* const authResponse = transformer.transform(apiResponse);
* ```
*/
export declare class ResponseTransformerFactory {
/**
* Collection of available transformers.
* Add new transformer instances here to support additional API response formats.
*
* @private
* @static
*/
private static readonly transformers;
/**
* Gets the appropriate transformer for the given API response.
* Iterates through available transformers and returns the first one that can handle the response.
*
* @param response - The raw API response to find a transformer for
* @returns The first transformer that can handle the response format
* @throws Error if no suitable transformer is found
*
* @example
* ```typescript
* try {
* const transformer = ResponseTransformerFactory.getTransformer(apiResponse);
* const authResponse = transformer.transform(apiResponse);
* } catch (error) {
* console.error('No suitable transformer found:', error);
* }
* ```
*/
static getTransformer(response: any): ApiResponseTransformer<any>;
}