fork of hey-api/openapi-ts because I need some additional things
1import { parse } from 'yaml';
2
3import type { FileInfo, JSONSchema, Plugin } from '../types';
4import { ParserError } from '../util/errors';
5
6export const yamlParser: Plugin = {
7 // JSON is valid YAML
8 canHandle: (file: FileInfo) => ['.yaml', '.yml', '.json'].includes(file.extension),
9 handler: async (file: FileInfo): Promise<JSONSchema> => {
10 const data = Buffer.isBuffer(file.data) ? file.data.toString() : file.data;
11
12 if (typeof data !== 'string') {
13 // data is already a JavaScript value (object, array, number, null, NaN, etc.)
14 return data;
15 }
16
17 try {
18 return parse(data) as JSONSchema;
19 } catch (error: any) {
20 throw new ParserError(error?.message || 'Parser Error', file.url);
21 }
22 },
23 name: 'yaml',
24};