aboutsummaryrefslogtreecommitdiff
path: root/scripts/apidoc/utils.ts
blob: 8a002f49335d5bcf57a50eb81a9eb62b3a50e092 (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { createHash } from 'node:crypto';
import { resolve } from 'node:path';
import type { Method } from '../../docs/.vitepress/components/api-docs/method';

// Types

export type Page = { text: string; link: string };

export type ModuleSummary = Page & {
  methods: Method[];
  diff: DocsApiDiff;
};

export interface DocsApiDiffIndex {
  /**
   * The methods in the module by name.
   */
  [module: string]: DocsApiDiff;
}

export interface DocsApiDiff {
  /**
   * The checksum of the entire module.
   */
  moduleHash: string;
  /**
   * The checksum of the method by name.
   */
  [method: string]: string;
}

// Paths

const pathRoot = resolve(__dirname, '..', '..');
export const pathDocsDir = resolve(pathRoot, 'docs');
const pathPublicDir = resolve(pathDocsDir, 'public');
export const nameDocsDiffIndexFile = 'api-diff-index.json';
export const pathDocsDiffIndexFile = resolve(
  pathPublicDir,
  nameDocsDiffIndexFile
);
export const pathOutputDir = resolve(pathDocsDir, 'api');

// Functions

export function adjustUrls(description: string): string {
  return description.replace(/https:\/\/(next.)?fakerjs.dev\//g, '/');
}

export function mapByName<T extends { name: string }, V>(
  input: T[],
  valueExtractor: (item: T) => V
): Record<string, V> {
  return input.reduce(
    (acc, item) => ({ ...acc, [item.name]: valueExtractor(item) }),
    {}
  );
}

/**
 * Creates a diff hash for the given method by removing the line number from the source path.
 *
 * @param method The method to create a hash for.
 */
export function methodDiffHash(method: Method): string {
  return diffHash({
    ...method,
    sourcePath: method.sourcePath.replace(/#.*/g, ''),
  });
}

/**
 * Creates a diff hash for the given object.
 *
 * @param object The object to create a hash for.
 */
export function diffHash(object: unknown): string {
  return createHash('md5').update(JSON.stringify(object)).digest('hex');
}