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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 3x 3x 3x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 5x 6x 6x 6x 1x 1x | /**
* @file responseExtraction.ts
* @module api/inventory/utils/responseExtraction
*
* @summary
* Response parsing utilities for extracting data from various envelope formats.
* Handles plain arrays, Spring Page envelopes, and custom response shapes.
*
* @enterprise
* - Tolerant of multiple response envelope formats
* - Single source of truth for response parsing
* - Clear separation of parsing logic
* - Defensive programming: returns sensible defaults
*/
import { isRecord } from './typeGuards';
/**
* Extract response.data safely, or return an empty object.
* Handles both Axios responses and raw data objects.
*
* @param resp - Response object from http call
* @returns Response data or empty object if not found
*
* @example
* ```typescript
* const data = resDataOrEmpty(response); // Safe access to response.data
* ```
*/
export const resDataOrEmpty = (resp: unknown): unknown => {
if (isRecord(resp) && 'data' in resp) {
const r = resp as Record<string, unknown>;
return r.data ?? {};
}
return {};
};
/**
* From an unknown response object, try to pull an array from one of the keys.
* Useful for extracting arrays from envelope formats like { items: [...] } or { content: [...] }.
* Falls back to [] if nothing sane is found.
*
* @param obj - Response object to extract array from
* @param keys - Keys to try in order (e.g., ['items', 'content', 'data'])
* @returns Array if found in one of the keys, empty array otherwise
*
* @example
* ```typescript
* const items = extractArray(response, ['items', 'content']);
* ```
*/
export const extractArray = (obj: unknown, keys: string[]): unknown[] => {
if (!isRecord(obj)) return [];
for (const k of keys) {
const v = obj[k];
if (Array.isArray(v)) return v as unknown[];
}
return [];
};
|