@mintlify/scraping 4.0.155 → 4.0.157

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.
@@ -0,0 +1,155 @@
1
+ import { AsyncAPISchema } from '@mintlify/common/asyncapi';
2
+ import fs from 'fs/promises';
3
+ import yaml from 'js-yaml';
4
+ import { describe, it, expect, beforeEach, vi } from 'vitest';
5
+
6
+ import { getAsyncApiDefinition } from '../src/asyncapi/getAsyncApiDefinition.js';
7
+
8
+ vi.mock('fs/promises');
9
+ vi.mock('node-fetch');
10
+
11
+ const mockAsyncApiDoc: AsyncAPISchema = {
12
+ asyncapi: '3.0.0',
13
+ info: {
14
+ title: 'Test API',
15
+ version: '1.0.0',
16
+ },
17
+ channels: {},
18
+ components: {},
19
+ };
20
+
21
+ describe('getAsyncApiDefinition', () => {
22
+ beforeEach(() => {
23
+ vi.resetAllMocks();
24
+ });
25
+
26
+ it('should load AsyncAPI doc from a local file path', async () => {
27
+ const mockYaml = yaml.dump(mockAsyncApiDoc);
28
+ vi.mocked(fs.readFile).mockResolvedValue(mockYaml);
29
+
30
+ const result = await getAsyncApiDefinition('test.yaml');
31
+
32
+ expect(fs.readFile).toHaveBeenCalledWith(expect.stringContaining('test.yaml'), 'utf-8');
33
+ expect(result).toEqual({ document: mockAsyncApiDoc, isUrl: false });
34
+ });
35
+
36
+ it('should accept AsyncAPI document directly', async () => {
37
+ const result = await getAsyncApiDefinition(mockAsyncApiDoc);
38
+ expect(result).toEqual({ document: mockAsyncApiDoc, isUrl: false });
39
+ });
40
+
41
+ it('should fetch yaml AsyncAPI doc from URL', async () => {
42
+ const mockYaml = yaml.dump(mockAsyncApiDoc);
43
+ global.fetch = vi.fn().mockResolvedValue({
44
+ ok: true,
45
+ text: () => Promise.resolve(mockYaml),
46
+ });
47
+
48
+ const url = new URL('https://example.com/asyncapi.yaml');
49
+ const result = await getAsyncApiDefinition(url);
50
+
51
+ expect(fetch).toHaveBeenCalledWith(url);
52
+ expect(result).toEqual({ document: mockAsyncApiDoc, isUrl: true });
53
+ });
54
+
55
+ it('should fetch and parse valid JSON AsyncAPI doc from URL', async () => {
56
+ const mockJson = JSON.stringify(mockAsyncApiDoc);
57
+ global.fetch = vi.fn().mockResolvedValue({
58
+ ok: true,
59
+ status: 200,
60
+ text: () => Promise.resolve(mockJson),
61
+ });
62
+
63
+ const url = new URL('https://example.com/asyncapi.yaml');
64
+ const result = await getAsyncApiDefinition(url);
65
+
66
+ expect(fetch).toHaveBeenCalledWith(url);
67
+ expect(result).toEqual({ document: mockAsyncApiDoc, isUrl: true });
68
+ });
69
+
70
+ it('should fetch AsyncAPI doc from URL string', async () => {
71
+ const mockYaml = yaml.dump(mockAsyncApiDoc);
72
+ global.fetch = vi.fn().mockResolvedValue({
73
+ ok: true,
74
+ status: 200,
75
+ text: () => Promise.resolve(mockYaml),
76
+ });
77
+
78
+ const urlString = 'https://example.com/asyncapi.yaml';
79
+ const result = await getAsyncApiDefinition(urlString);
80
+
81
+ expect(fetch).toHaveBeenCalledWith(new URL(urlString));
82
+ expect(result).toEqual({ document: mockAsyncApiDoc, isUrl: true });
83
+ });
84
+
85
+ it('should throw error when local file read fails', async () => {
86
+ vi.mocked(fs.readFile).mockRejectedValue(new Error('File read error'));
87
+
88
+ await expect(getAsyncApiDefinition('test.yaml')).rejects.toThrow('File read error');
89
+ });
90
+
91
+ it('should throw error with URL and status code when fetch fails', async () => {
92
+ global.fetch = vi.fn().mockResolvedValue({
93
+ ok: false,
94
+ status: 404,
95
+ statusText: 'Not Found',
96
+ });
97
+
98
+ const urlString = 'https://mycoolwebsocketschema/asyncapi.doesnotexist';
99
+
100
+ await expect(getAsyncApiDefinition(urlString)).rejects.toThrow(
101
+ `${urlString} - failed to retrieve AsyncAPI file from source - : 404 Not Found`
102
+ );
103
+
104
+ expect(fetch).toHaveBeenCalledWith(new URL(urlString));
105
+ });
106
+
107
+ it('should throw error when HTTP URL is provided', async () => {
108
+ const httpUrl = new URL('http://example.com/asyncapi.yaml');
109
+ await expect(getAsyncApiDefinition(httpUrl)).rejects.toThrow(
110
+ 'Only HTTPS URLs are supported. Please provide an HTTPS URL'
111
+ );
112
+ });
113
+
114
+ it('should throw error when HTTP URL string is provided', async () => {
115
+ const httpUrlString = 'http://example.com/asyncapi.yaml';
116
+ await expect(getAsyncApiDefinition(httpUrlString)).rejects.toThrow(
117
+ 'Only HTTPS URLs are supported. Please provide an HTTPS URL'
118
+ );
119
+ });
120
+
121
+ it('should throw error when non-HTTPS URL string is provided', async () => {
122
+ const httpUrlString = 'ftp://example.com/asyncapi.yaml';
123
+ await expect(getAsyncApiDefinition(httpUrlString)).rejects.toThrow(
124
+ 'Only HTTPS URLs are supported. Please provide an HTTPS URL'
125
+ );
126
+ });
127
+
128
+ it('should throw error when URL response is invalid YAML', async () => {
129
+ global.fetch = vi.fn().mockResolvedValue({
130
+ text: () => Promise.resolve('invalid: yaml: content'),
131
+ });
132
+
133
+ await expect(getAsyncApiDefinition('https://example.com/asyncapi.yaml')).rejects.toThrow();
134
+ });
135
+
136
+ it('should throw error when URL response is invalid JSON', async () => {
137
+ global.fetch = vi.fn().mockResolvedValue({
138
+ text: () => Promise.resolve('{"invalid": "yaml", "invalid": "content"}'),
139
+ });
140
+
141
+ await expect(getAsyncApiDefinition('https://example.com/asyncapi.json')).rejects.toThrow();
142
+ });
143
+
144
+ it('should throw error when local file contains invalid YAML', async () => {
145
+ vi.mocked(fs.readFile).mockResolvedValue('invalid: yaml: content');
146
+
147
+ await expect(getAsyncApiDefinition('test.yaml')).rejects.toThrow();
148
+ });
149
+
150
+ it('should throw error when local file contains invalid JSON', async () => {
151
+ vi.mocked(fs.readFile).mockResolvedValue('{"invalid": "yaml", "invalid": "content"}');
152
+
153
+ await expect(getAsyncApiDefinition('test.json')).rejects.toThrow();
154
+ });
155
+ });
@@ -0,0 +1,5 @@
1
+ import { AsyncAPISchema } from '@mintlify/common/asyncapi';
2
+ export declare const getAsyncApiDefinition: (pathOrDocumentOrUrl: string | AsyncAPISchema | URL) => Promise<{
3
+ document: AsyncAPISchema;
4
+ isUrl: boolean;
5
+ }>;
@@ -0,0 +1,32 @@
1
+ import * as fs from 'fs/promises';
2
+ import yaml from 'js-yaml';
3
+ import * as path from 'path';
4
+ import { fetchAsyncApi } from '../utils/network.js';
5
+ export const getAsyncApiDefinition = async (pathOrDocumentOrUrl) => {
6
+ if (typeof pathOrDocumentOrUrl === 'string') {
7
+ if (pathOrDocumentOrUrl.startsWith('http://')) {
8
+ // This is an invalid location either for a file or a URL
9
+ throw new Error('Only HTTPS URLs are supported. Please provide an HTTPS URL');
10
+ }
11
+ else {
12
+ try {
13
+ const url = new URL(pathOrDocumentOrUrl);
14
+ pathOrDocumentOrUrl = url;
15
+ }
16
+ catch {
17
+ const pathname = path.join(process.cwd(), pathOrDocumentOrUrl.toString());
18
+ const file = await fs.readFile(pathname, 'utf-8');
19
+ pathOrDocumentOrUrl = yaml.load(file);
20
+ }
21
+ }
22
+ }
23
+ const isUrl = pathOrDocumentOrUrl instanceof URL;
24
+ if (pathOrDocumentOrUrl instanceof URL) {
25
+ if (pathOrDocumentOrUrl.protocol !== 'https:') {
26
+ throw new Error('Only HTTPS URLs are supported. Please provide an HTTPS URL');
27
+ }
28
+ pathOrDocumentOrUrl = await fetchAsyncApi(pathOrDocumentOrUrl);
29
+ }
30
+ return { document: pathOrDocumentOrUrl, isUrl };
31
+ };
32
+ //# sourceMappingURL=getAsyncApiDefinition.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getAsyncApiDefinition.js","sourceRoot":"","sources":["../../src/asyncapi/getAsyncApiDefinition.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,aAAa,CAAC;AAClC,OAAO,IAAI,MAAM,SAAS,CAAC;AAC3B,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAE7B,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD,MAAM,CAAC,MAAM,qBAAqB,GAAG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