Skip to content

Commit 73bd4fb

Browse files
refactor: exports extras and missed things (#635)
1 parent add289e commit 73bd4fb

File tree

8 files changed

+65
-14
lines changed

8 files changed

+65
-14
lines changed

src/from.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { readFile } from 'fs/promises';
1+
import { readFile } from 'fs';
2+
import { promisify } from 'util';
23

34
import type { RequestInit } from 'node-fetch';
45
import type { Parser } from './parser';
@@ -29,9 +30,9 @@ export function fromURL(parser: Parser, source: string, options?: RequestInit):
2930
};
3031
}
3132

32-
export function fromFile(parser: Parser, source: string, options?: Parameters<typeof readFile>[1]): FromResult {
33+
export function fromFile(parser: Parser, source: string, options?: Parameters<typeof readFile.__promisify__>[1]): FromResult {
3334
async function readFileFn(): Promise<Input> {
34-
return (await readFile(source, options)).toString();
35+
return (await promisify(readFile)(source, options)).toString();
3536
}
3637

3738
return {

src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1+
import { DiagnosticSeverity } from '@stoplight/types';
2+
13
import { Parser } from './parser';
24

35
export * from './models';
46

57
export { Parser };
68
export { stringify, unstringify } from './stringify';
79
export { fromURL, fromFile } from './from';
10+
export { createAsyncAPIDocument, toAsyncAPIDocument, isAsyncAPIDocument } from './document';
811

9-
export { AsyncAPIDocument as OldAsyncAPIDocument } from './old-api/asyncapi';
10-
export { convertToOldAPI } from './old-api/converter';
12+
export * from './old-api';
1113

14+
export { DiagnosticSeverity };
1215
export type { AsyncAPISemver, Input, Diagnostic, SchemaValidateResult } from './types';
1316
export type { ValidateOptions, ValidateOutput } from './validate';
1417
export type { ParseOptions, ParseOutput } from './parse';

src/old-api/index.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export { convertToOldAPI } from './converter';
2+
3+
export { AsyncAPIDocument as OldAsyncAPIDocument } from './asyncapi';
4+
export { Base as OldBase } from './base';
5+
export { ChannelParameter as OldChannelParameter } from './channel-parameter';
6+
export { Channel as OldChannel } from './channel';
7+
export { Components as OldComponents } from './components';
8+
export { Contact as OldContact } from './contact';
9+
export { CorrelationId as OldCorrelationId } from './correlation-id';
10+
export { ExternalDocs as OldExternalDocs } from './external-docs';
11+
export { License as OldLicense } from './license';
12+
export { MessageTrait as OldMessageTrait } from './message-trait';
13+
export { Message as OldMessage } from './message';
14+
export { OAuthFlow as OldOAuthFlow } from './oauth-flow';
15+
export { OperationTrait as OldOperationTrait } from './operation-trait';
16+
export { Operation as OldOperation } from './operation';
17+
export { Schema as OldSchema } from './schema';
18+
export { SecurityRequirement as OldSecurityRequirement } from './security-requirement';
19+
export { SecurityScheme as OldSecurityScheme } from './security-scheme';
20+
export { ServerVariable as OldServerVariable } from './server-variable';
21+
export { Server as OldServer } from './server';
22+
export { Tag as OldTag } from './tag';

src/parse.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@ import { createDetailedAsyncAPI, mergePatch, setExtension } from './utils';
88

99
import { xParserSpecParsed } from './constants';
1010

11-
import type { Spectral } from '@stoplight/spectral-core';
11+
import type { Spectral, Document } from '@stoplight/spectral-core';
1212
import type { Parser } from './parser';
1313
import type { ValidateOptions } from './validate';
1414
import type { Input, Diagnostic } from './types';
1515

1616
export interface ParseOutput {
1717
document: AsyncAPIDocumentInterface | undefined;
18-
diagnostics: Diagnostic[];
18+
diagnostics: Diagnostic[];
19+
extras?: {
20+
document: Document;
21+
}
1922
}
2023

2124
export interface ParseOptions {
@@ -33,11 +36,12 @@ const defaultOptions: ParseOptions = {
3336

3437
export async function parse(parser: Parser, spectral: Spectral, asyncapi: Input, options: ParseOptions = {}): Promise<ParseOutput> {
3538
options = mergePatch<ParseOptions>(defaultOptions, options);
36-
const { validated, diagnostics } = await validate(spectral, asyncapi, { ...options.validateOptions, source: options.source });
39+
const { validated, diagnostics, extras } = await validate(spectral, asyncapi, { ...options.validateOptions, source: options.source });
3740
if (validated === undefined) {
3841
return {
3942
document: undefined,
4043
diagnostics,
44+
extras: undefined
4145
};
4246
}
4347

@@ -52,5 +56,6 @@ export async function parse(parser: Parser, spectral: Spectral, asyncapi: Input,
5256
return {
5357
document,
5458
diagnostics,
59+
extras,
5560
};
5661
}

src/utils.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ export function normalizeInput(asyncapi: string | MaybeAsyncAPI): string {
3131
return JSON.stringify(asyncapi, undefined, 2);
3232
}
3333

34-
export function unfreezeObject(data: unknown) {
35-
return JSON.parse(JSON.stringify(data));
36-
}
37-
3834
export function hasErrorDiagnostic(diagnostics: ISpectralDiagnostic[]): boolean {
3935
return diagnostics.some(diagnostic => diagnostic.severity === DiagnosticSeverity.Error);
4036
}

src/validate.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { Input, Diagnostic } from './types';
99
export interface ValidateOptions extends IRunOpts {
1010
source?: string;
1111
allowedSeverity?: {
12+
error?: boolean;
1213
warning?: boolean;
1314
info?: boolean;
1415
hint?: boolean;
@@ -18,10 +19,14 @@ export interface ValidateOptions extends IRunOpts {
1819
export interface ValidateOutput {
1920
validated: unknown;
2021
diagnostics: Diagnostic[];
22+
extras: {
23+
document: Document,
24+
}
2125
}
2226

2327
const defaultOptions: ValidateOptions = {
2428
allowedSeverity: {
29+
error: false,
2530
warning: true,
2631
info: true,
2732
hint: true,
@@ -37,13 +42,13 @@ export async function validate(spectral: Spectral, asyncapi: Input, options: Val
3742
let { resolved: validated, results } = await spectral.runWithResolved(document);
3843

3944
if (
40-
hasErrorDiagnostic(results) ||
45+
(!allowedSeverity?.error && hasErrorDiagnostic(results)) ||
4146
(!allowedSeverity?.warning && hasWarningDiagnostic(results)) ||
4247
(!allowedSeverity?.info && hasInfoDiagnostic(results)) ||
4348
(!allowedSeverity?.hint && hasHintDiagnostic(results))
4449
) {
4550
validated = undefined;
4651
}
4752

48-
return { validated, diagnostics: results };
53+
return { validated, diagnostics: results, extras: { document: document as Document } };
4954
}

test/parse.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Document } from '@stoplight/spectral-core';
2+
13
import { AsyncAPIDocumentV2 } from '../src/models';
24
import { Parser } from '../src/parser';
35

@@ -33,6 +35,22 @@ describe('parse()', function() {
3335
expect(diagnostics.length > 0).toEqual(true);
3436
});
3537

38+
it('should return extras', async function() {
39+
const documentRaw = {
40+
asyncapi: '2.0.0',
41+
info: {
42+
title: 'Valid AsyncApi document',
43+
version: '1.0',
44+
},
45+
channels: {}
46+
};
47+
const { document, diagnostics, extras } = await parser.parse(documentRaw);
48+
49+
expect(document).toBeInstanceOf(AsyncAPIDocumentV2);
50+
expect(extras?.document).toBeInstanceOf(Document);
51+
expect(diagnostics.length > 0).toEqual(true);
52+
});
53+
3654
it('should preserve references', async function() {
3755
const documentRaw = {
3856
asyncapi: '2.0.0',

webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ module.exports = {
3535
fallback: {
3636
fs: false,
3737
path: false,
38+
util: false,
3839
buffer: require.resolve('buffer/'),
3940
}
4041
},

0 commit comments

Comments
 (0)