@mintlify/prebuild 1.0.996 → 1.0.998

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,9 @@
1
+ import { CircularRefError, PathTraversalError, RefInvalidJsonError, RefNotFoundError, resolveRefs, } from '@mintlify/validation';
1
2
  import { mkdtemp, writeFile, mkdir, rm } from 'fs/promises';
2
3
  import { readFile } from 'fs/promises';
3
4
  import { tmpdir } from 'os';
4
5
  import path from 'path';
5
6
  import { describe, it, expect, beforeEach, afterEach } from 'vitest';
6
- import { resolveRefs, RefNotFoundError, CircularRefError, PathTraversalError, RefInvalidJsonError, } from '../resolveRefs.js';
7
7
  describe('resolveRefs', () => {
8
8
  let tempDir;
9
9
  let reader;
@@ -37,8 +37,8 @@ describe('resolveRefs', () => {
37
37
  const input = { tabs: { $ref: './nav/tabs.json' } };
38
38
  const { resolved, referencedFiles } = await resolveRefs(input, reader);
39
39
  expect(resolved).toEqual({ tabs: { items: ['page1', 'page2'] } });
40
- expect(referencedFiles).toContain(path.join('nav', 'tabs.json'));
41
- expect(referencedFiles).toContain(path.join('nav', 'items.json'));
40
+ expect(referencedFiles).toContain('nav/tabs.json');
41
+ expect(referencedFiles).toContain('nav/items.json');
42
42
  });
43
43
  it('resolves $ref inside array elements', async () => {
44
44
  await writeJson('group1.json', { group: 'API', pages: ['api/get', 'api/post'] });
@@ -1,18 +1,4 @@
1
- export declare class RefNotFoundError extends Error {
2
- constructor(refPath: string, referencedFrom: string);
3
- }
4
- export declare class CircularRefError extends Error {
5
- constructor(chain: string[]);
6
- }
7
- export declare class PathTraversalError extends Error {
8
- constructor(refPath: string);
9
- }
10
- export declare class RefInvalidJsonError extends Error {
11
- constructor(refPath: string, details: string);
12
- }
13
- export interface ResolveRefsResult {
14
- resolved: unknown;
15
- referencedFiles: string[];
16
- }
17
- export declare function resolveRefs(value: unknown, readFile: (relativePath: string) => Promise<string>): Promise<ResolveRefsResult>;
1
+ import { CircularRefError, PathTraversalError, RefInvalidJsonError, RefNotFoundError, resolveRefs, type ResolveRefsResult } from '@mintlify/validation';
2
+ export { CircularRefError, PathTraversalError, RefInvalidJsonError, RefNotFoundError, resolveRefs };
3
+ export type { ResolveRefsResult };
18
4
  export declare function resolveFileRefs(value: unknown, contentDirectory: string): Promise<ResolveRefsResult>;
@@ -1,95 +1,7 @@
1
+ import { CircularRefError, PathTraversalError, RefInvalidJsonError, RefNotFoundError, resolveRefs, } from '@mintlify/validation';
1
2
  import { readFile } from 'fs/promises';
2
3
  import path from 'path';
3
- export class RefNotFoundError extends Error {
4
- constructor(refPath, referencedFrom) {
5
- super(`Referenced config file '${refPath}' does not exist (referenced from ${referencedFrom})`);
6
- this.name = 'RefNotFoundError';
7
- }
8
- }
9
- export class CircularRefError extends Error {
10
- constructor(chain) {
11
- super(`Circular reference detected: ${chain.join(' → ')}`);
12
- this.name = 'CircularRefError';
13
- }
14
- }
15
- export class PathTraversalError extends Error {
16
- constructor(refPath) {
17
- super(`Referenced config file '${refPath}' is outside the project directory`);
18
- this.name = 'PathTraversalError';
19
- }
20
- }
21
- export class RefInvalidJsonError extends Error {
22
- constructor(refPath, details) {
23
- super(`Referenced config file '${refPath}' contains invalid JSON: ${details}`);
24
- this.name = 'RefInvalidJsonError';
25
- }
26
- }
27
- function isJsonObject(value) {
28
- return typeof value === 'object' && value !== null && !Array.isArray(value);
29
- }
30
- function getRefPath(value) {
31
- if (!isJsonObject(value) || typeof value.$ref !== 'string')
32
- return undefined;
33
- return value.$ref;
34
- }
35
- function resolveRefPath(refPath, fromFile) {
36
- if (fromFile)
37
- return path.normalize(path.join(path.dirname(fromFile), refPath));
38
- return path.normalize(refPath);
39
- }
40
- async function loadRefFile(refPath, fullPath, readFile, fromFile) {
41
- let content;
42
- try {
43
- content = await readFile(fullPath);
44
- }
45
- catch {
46
- throw new RefNotFoundError(refPath, fromFile ?? 'project root');
47
- }
48
- try {
49
- return JSON.parse(content);
50
- }
51
- catch (e) {
52
- throw new RefInvalidJsonError(refPath, e instanceof Error ? e.message : String(e));
53
- }
54
- }
55
- export async function resolveRefs(value, readFile) {
56
- const referencedFiles = [];
57
- async function resolve(value, visited, fromFile) {
58
- const refPath = getRefPath(value);
59
- if (refPath != null) {
60
- const fullPath = resolveRefPath(refPath, fromFile);
61
- if (fullPath.startsWith('..'))
62
- throw new PathTraversalError(refPath);
63
- if (visited.has(fullPath))
64
- throw new CircularRefError([...visited, fullPath]);
65
- const parsed = await loadRefFile(refPath, fullPath, readFile, fromFile);
66
- referencedFiles.push(fullPath);
67
- const resolvedRef = await resolve(parsed, new Set([...visited, fullPath]), fullPath);
68
- if (isJsonObject(resolvedRef)) {
69
- const siblings = Object.entries(value).filter(([key]) => key !== '$ref');
70
- const result = { ...resolvedRef };
71
- for (const [key, val] of siblings) {
72
- result[key] = await resolve(val, visited, fromFile);
73
- }
74
- return result;
75
- }
76
- return resolvedRef;
77
- }
78
- if (Array.isArray(value)) {
79
- return Promise.all(value.map((item) => resolve(item, visited, fromFile)));
80
- }
81
- if (isJsonObject(value)) {
82
- const result = {};
83
- for (const [key, val] of Object.entries(value)) {
84
- result[key] = await resolve(val, visited, fromFile);
85
- }
86
- return result;
87
- }
88
- return value;
89
- }
90
- const resolved = await resolve(value, new Set());
91
- return { resolved, referencedFiles };
92
- }
4
+ export { CircularRefError, PathTraversalError, RefInvalidJsonError, RefNotFoundError, resolveRefs };
93
5
  export async function resolveFileRefs(value, contentDirectory) {
94
6
  return resolveRefs(value, (relativePath) => readFile(path.join(contentDirectory, relativePath), 'utf-8'));
95
7
  }