Skip to content

Commit

Permalink
Fetching schema by url in library (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatatjahu authored Feb 7, 2019
1 parent f36a5f6 commit f625e3a
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 18 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ Check out this simple sandbox application that uses the asyncapi-react component

The list of props for the AsyncApi React component includes:

- **schema: string | AsyncApiInterface**
- **schema: string | AsyncApiInterface | FetchingSchemaInterface**

The `schema` property is required and contains AsyncAPI specification. It should be one of the `string` or [`AsyncApiInterface`](./library/src/types.ts#L13) type. For more information on what it contains and what it should look like, read [AsyncAPI Specification](https://github.com/asyncapi/asyncapi#asyncapi-specification).
The `schema` property is required and contains AsyncAPI specification. Use the `string` type, the [`AsyncApiInterface`](./library/src/types.ts#L13) type, or the [`FetchingSchemaInterface`](./library/src/helpers/fetchSchema.ts#L1) object to fetch the schema from an external resource. For more information on what it contains and what it should look like, read [AsyncAPI Specification](https://github.com/asyncapi/asyncapi#asyncapi-specification).

- **theme?: Partial<ThemeInterface\>**

Expand Down
41 changes: 29 additions & 12 deletions library/src/containers/AsyncApi/AsyncApi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { ThemeProvider } from 'styled-components';
import { AsyncApi, SecurityScheme } from '../../types';
import { ThemeInterface, defaultTheme } from '../../theme';
import { ConfigInterface, defaultConfig } from '../../config';
import { parser, beautifier } from '../../helpers';
import {
parser,
beautifier,
FetchingSchemaInterface,
isFetchingSchemaInterface,
fetchSchema,
} from '../../helpers';

import InfoComponent from '../Info/Info';
import Security from '../Security/Security';
Expand All @@ -16,7 +22,7 @@ import ErrorComponent from '../Error/Error';
import { AsyncApiWrapper } from './styled';

export interface AsyncApiProps {
schema: string | Object;
schema: string | Object | FetchingSchemaInterface;
theme?: Partial<ThemeInterface>;
config?: Partial<ConfigInterface>;
}
Expand All @@ -42,6 +48,27 @@ class AsyncApiComponent extends Component<AsyncApiProps, AsyncApiState> {
error: undefined,
};

async componentWillMount() {
this.updateSchema(this.props.schema);
}

async componentWillReceiveProps(nextProps: AsyncApiProps) {
const { schema } = nextProps;

if (schema !== this.props.schema) {
this.updateSchema(schema);
}
}

private async updateSchema(
schema: string | Object | FetchingSchemaInterface,
) {
if (isFetchingSchemaInterface(schema)) {
schema = await fetchSchema(schema as FetchingSchemaInterface);
}
this.prepareSchema(schema);
}

private async prepareSchema(schema: string | Object) {
try {
let validatedSchema = await this.validateSchema(schema);
Expand All @@ -52,16 +79,6 @@ class AsyncApiComponent extends Component<AsyncApiProps, AsyncApiState> {
}
}

async componentWillMount() {
this.prepareSchema(this.props.schema);
}

async componentWillReceiveProps(nextProps: AsyncApiProps) {
if (nextProps.schema !== this.props.schema) {
this.prepareSchema(nextProps.schema);
}
}

private async validateSchema(schema: string | any) {
if (typeof schema !== 'string') {
schema = JSON.stringify(schema);
Expand Down
23 changes: 23 additions & 0 deletions library/src/helpers/fetchSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export interface FetchingSchemaInterface {
url: string;
requestOptions?: RequestInit;
}

export function isFetchingSchemaInterface(schema: any) {
return (<FetchingSchemaInterface>schema).url !== undefined;
}

const defaultRequestOptions: RequestInit = {
method: 'GET',
};

export const fetchSchema = async ({
url,
requestOptions = defaultRequestOptions,
}: FetchingSchemaInterface): Promise<any> => {
return fetch(url, requestOptions).then(handleResponse);
};

function handleResponse(response: any) {
return response.text().then((data: string) => data);
}
4 changes: 3 additions & 1 deletion library/src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './parser';
export * from './beautifier';
export * from './beautifier';
export * from './fetchSchema';
export * from './json-parser';
19 changes: 19 additions & 0 deletions library/src/helpers/json-parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const parse = <T extends {}>(str?: string): T => {
if (!str) return {} as T;

try {
return JSON.parse(str) as T;
} catch (e) {
return {} as T;
}
};

export const stringify = <T extends {}>(content?: T): string => {
if (!content) return '';

try {
return JSON.stringify(content);
} catch (e) {
return '';
}
};
3 changes: 2 additions & 1 deletion library/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import AsyncApi from './containers/AsyncApi/AsyncApi';
export { AsyncApiProps } from './containers/AsyncApi/AsyncApi';
export { ThemeInterface } from './theme/theme';
export { ConfigInterface } from './config/config';
export { FetchingSchemaInterface } from './helpers';
export { AsyncApi as AsyncApiInterface } from './types';

export default AsyncApi
export default AsyncApi;
8 changes: 6 additions & 2 deletions playground/src/common/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
export const parse = <T extends {}>(str: string): T => {
export const parse = <T extends {}>(str?: string): T => {
if (!str) return {} as T;

try {
return JSON.parse(str) as T;
} catch (e) {
return {} as T;
}
};

export const stringify = <T extends {}>(content: T): string => {
export const stringify = <T extends {}>(content?: T): string => {
if (!content) return '';

try {
return JSON.stringify(content);
} catch (e) {
Expand Down

0 comments on commit f625e3a

Please sign in to comment.