# core/loaders/payload.loader.ts

> 80 lines of code and 15 definitions.

Tree: Bane's Lab Content
Language: typescript
Layer: infrastructure
Canonical: https://banes-lab.com/anatomy/content#file-content-core-loaders-payload-loader-ts
Source text: https://banes-lab.com/source/content/core/loaders/payload.loader.ts.txt

Listed in [core/loaders](https://banes-lab.com/api/source/content/core/loaders.md), after [core/loaders/panel.loader.ts](https://banes-lab.com/source/content/core/loaders/panel.loader.ts.md) and before [core/loaders/readme.loader.ts](https://banes-lab.com/source/content/core/loaders/readme.loader.ts.md).

## Definitions

- `collectStrings` (lexical_declaration, line 22)
- `tabsOf` (lexical_declaration, line 44)
- `isCodeSample` (lexical_declaration, line 18)
- `payloadFileOf` (lexical_declaration, line 58, exported)
- `readPagePayload` (lexical_declaration, line 67, exported)
- `idsOf` (lexical_declaration, line 35)
- `readPageContent` (lexical_declaration, line 62, exported)
- `id` (lexical_declaration, line 52)
- `file` (lexical_declaration, line 68, exported)
- `parsed` (lexical_declaration, line 69, exported)
- `content` (lexical_declaration, line 70, exported)
- `tabs` (lexical_declaration, line 74, exported)
- `sections` (lexical_declaration, line 75, exported)
- `title` (lexical_declaration, line 76, exported)
- `label` (lexical_declaration, line 77, exported)

## Contained in

- [core/loaders](https://banes-lab.com/anatomy/content/folder-content-core-loaders.md)

## Uses

- [core/selectors/payload.selector.ts](https://banes-lab.com/source/content/core/selectors/payload.selector.ts.md)

## Used by

- [core/loaders/chapter.loader.ts](https://banes-lab.com/source/content/core/loaders/chapter.loader.ts.md)

## Linked from

- [core/loaders](https://banes-lab.com/anatomy/content/folder-content-core-loaders.md)
- [core/selectors](https://banes-lab.com/anatomy/content/folder-content-core-selectors.md)

## Source

```typescript
import {
    CODE_FIELD,
    CODE_KIND,
    JSON_EXTENSION,
    JSON_FOLDER,
    PAYLOAD_CONTENT_KEY,
    PAYLOAD_ID_KEY,
    PAYLOAD_KIND_KEY,
    PAYLOAD_LABEL_KEY,
    PAYLOAD_SECTIONS_KEY,
    PAYLOAD_TITLE_KEY,
} from "#configuration/constants/leak.constants";
import type { PagePayload, PayloadTab } from "#types/leak.types";
import { contentOf, isRecord, tabsIn } from "#core/selectors/payload.selector";
import { absolutePath } from "@ssot/paths";
import { readJsonOrNull } from "#core/persistence/report.persistence";

const isCodeSample = function isCodeSample(record: Record<string, unknown>, key: string): boolean {
    return record[PAYLOAD_KIND_KEY] === CODE_KIND && key === CODE_FIELD;
};

const collectStrings = function collectStrings(value: unknown): string[] {
    if (typeof value === "string") {
        return [value];
    }
    if (Array.isArray(value)) {
        return value.flatMap((entry: unknown) => collectStrings(entry));
    }
    if (!isRecord(value)) {
        return [];
    }
    return Object.entries(value).flatMap(([key, entry]) => (isCodeSample(value, key) ? [] : collectStrings(entry)));
};

const idsOf = function idsOf(list: unknown): string[] {
    if (!Array.isArray(list)) {
        return [];
    }
    return list.flatMap((entry: unknown) =>
        isRecord(entry) && typeof entry[PAYLOAD_ID_KEY] === "string" ? [entry[PAYLOAD_ID_KEY]] : [],
    );
};

const tabsOf = function tabsOf(list: unknown): PayloadTab[] {
    if (!Array.isArray(list)) {
        return [];
    }
    return list.flatMap((entry: unknown) => {
        if (!isRecord(entry)) {
            return [];
        }
        const id = entry[PAYLOAD_ID_KEY];
        const label = entry[PAYLOAD_LABEL_KEY];
        return typeof id === "string" && typeof label === "string" ? [{ id, label }] : [];
    });
};

export const payloadFileOf = function payloadFileOf(page: string): string {
    return absolutePath("builds.web", JSON_FOLDER, page + JSON_EXTENSION);
};

export const readPageContent = function readPageContent(page: string): unknown {
    const parsed = readJsonOrNull(payloadFileOf(page));
    return isRecord(parsed) ? (parsed[PAYLOAD_CONTENT_KEY] ?? null) : null;
};

export const readPagePayload = function readPagePayload(page: string): PagePayload | null {
    const file = payloadFileOf(page);
    const parsed = readJsonOrNull(file);
    const content = contentOf(parsed);
    if (!isRecord(parsed) || content === null) {
        return null;
    }
    const tabs = tabsIn(content);
    const sections = tabs.flatMap((tab) => idsOf(tab[PAYLOAD_SECTIONS_KEY]));
    const title = parsed[PAYLOAD_TITLE_KEY];
    const label = parsed[PAYLOAD_LABEL_KEY];
    return {
        file,
        label: typeof label === "string" ? label : "",
        page,
        sections,
        strings: collectStrings(content),
        tabs: tabsOf(tabs),
        title: typeof title === "string" ? title : "",
    };
};
```
