Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
SamyPesse committed Mar 31, 2024
1 parent 1d24556 commit 8bc45f6
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"content-security-policy-merger": "^1.0.0",
"framer-motion": "^10.16.14",
"js-cookie": "^3.0.5",

"jsontoxml": "^1.0.1",
"katex": "^0.16.9",
"memoizee": "^0.4.15",
Expand Down
5 changes: 2 additions & 3 deletions packages/react-openapi/src/fetchOpenAPIOperation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ it('should parse Swagger 2.0', async () => {
expect(resolved).toMatchObject({
servers: [
{
url: "https://petstore.swagger.io/v2",
url: 'https://petstore.swagger.io/v2',
},
{
url: "http://petstore.swagger.io/v2",
url: 'http://petstore.swagger.io/v2',
},
],
operation: {
Expand All @@ -149,4 +149,3 @@ it('should parse Swagger 2.0', async () => {
},
});
});

15 changes: 12 additions & 3 deletions packages/react-openapi/src/fetchOpenAPIOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,10 @@ export async function parseOpenAPIV3(url: string, text: string): Promise<OpenAPI
data = yaml.load(text);
} catch (yamlError) {
if ((yamlError as Error).name === 'YAMLException') {
throw new OpenAPIFetchError('Failed to parse YAML: ' + (yamlError as Error).message, url);
throw new OpenAPIFetchError(
'Failed to parse YAML: ' + (yamlError as Error).message,
url,
);
} else {
throw yamlError;
}
Expand All @@ -202,7 +205,10 @@ export async function parseOpenAPIV3(url: string, text: string): Promise<OpenAPI
data = result.openapi;
} catch (error) {
if ((error as Error).name === 'S2OError') {
throw new OpenAPIFetchError('Failed to convert Swagger 2.0 to OpenAPI 3.0: ' + (error as Error).message, url);
throw new OpenAPIFetchError(
'Failed to convert Swagger 2.0 to OpenAPI 3.0: ' + (error as Error).message,
url,
);
} else {
throw error;
}
Expand All @@ -216,7 +222,10 @@ export async function parseOpenAPIV3(url: string, text: string): Promise<OpenAPI
export class OpenAPIFetchError extends Error {
public name = 'OpenAPIFetchError';

constructor(message: string, public readonly url: string) {
constructor(
message: string,
public readonly url: string,
) {
super(message);
}
}
8 changes: 5 additions & 3 deletions src/components/DocumentView/OpenAPI/OpenAPI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ export async function OpenAPI(props: BlockProps<DocumentBlockSwagger>) {

async function OpenAPIBody(props: BlockProps<DocumentBlockSwagger>) {
const { block, context } = props;
const { data, error } = await fetchOpenAPIBlock(block, context.resolveContentRef);
const { data, error } = await fetchOpenAPIBlock(block, context.resolveContentRef);

if (error) {
return (
<div className={tcls('hidden')}>
<p>Error with {error.url}: {error.message}</p>
<p>
Error with {error.url}: {error.message}
</p>
</div>
)
);
}

if (!data) {
Expand Down
7 changes: 5 additions & 2 deletions src/lib/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ import { ResolvedContentRef } from './references';
export async function fetchOpenAPIBlock(
block: DocumentBlockSwagger,
resolveContentRef: (ref: ContentRef) => Promise<ResolvedContentRef | null>,
): Promise<{ data: OpenAPIOperationData | null; error?: undefined } | { error: OpenAPIFetchError; data?: undefined; }> {
): Promise<
| { data: OpenAPIOperationData | null; error?: undefined }
| { error: OpenAPIFetchError; data?: undefined }
> {
const resolved = block.data.ref ? await resolveContentRef(block.data.ref) : null;
if (!resolved || !block.data.path || !block.data.method) {
return { data: null };
Expand All @@ -38,7 +41,7 @@ export async function fetchOpenAPIBlock(
return { data };
} catch (error) {
if (error instanceof OpenAPIFetchError) {
return { error }
return { error };
}

throw error;
Expand Down

0 comments on commit 8bc45f6

Please sign in to comment.