mirror of
https://github.com/Redocly/redoc.git
synced 2025-08-07 21:54:53 +03:00
hooked with server
This commit is contained in:
parent
270d490905
commit
5b219b48d1
|
@ -32,7 +32,7 @@ const options: RedocRawOptions = { nativeScrollbars: false };
|
|||
async function init() {
|
||||
const spec = await loadAndBundleSpec(specUrl);
|
||||
store = new AppStore(spec, specUrl, options);
|
||||
|
||||
/*
|
||||
const ajv = new Ajv({ allErrors: true, unknownFormats: ['int32', 'UUID', 'int64'] });
|
||||
ajv.addSchema(spec, 'specs.json')
|
||||
|
||||
|
@ -40,7 +40,7 @@ async function init() {
|
|||
|
||||
window.ajv = ajv;
|
||||
window.ajvError = ajvError;
|
||||
|
||||
*/
|
||||
renderRoot({ store });
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
import { observer } from 'mobx-react';
|
||||
import * as React from 'react';
|
||||
import qs = require('qs');
|
||||
import { OperationModel } from '../../services/models';
|
||||
import { PayloadSamples } from '../PayloadSamples/PayloadSamples';
|
||||
import { SourceCodeWithCopy } from '../SourceCode/SourceCode';
|
||||
|
@ -8,16 +7,29 @@ import { SendButton } from '../../common-elements/buttons';
|
|||
import { ConsoleActionsRow } from '../../common-elements/panels';
|
||||
import { ConsoleEditor } from './ConsoleEditor';
|
||||
|
||||
const qs = require('qs');
|
||||
|
||||
|
||||
export interface ConsoleViewerProps {
|
||||
operation: OperationModel;
|
||||
}
|
||||
|
||||
export interface ConsoleViewerState {
|
||||
result: any;
|
||||
}
|
||||
|
||||
@observer
|
||||
export class ConsoleViewer extends React.Component<ConsoleViewerProps> {
|
||||
export class ConsoleViewer extends React.Component<ConsoleViewerProps, ConsoleViewerState> {
|
||||
private consoleEditor: ConsoleEditor;
|
||||
operation: OperationModel;
|
||||
visited = new Set();
|
||||
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = {
|
||||
result: null
|
||||
};
|
||||
}
|
||||
onClickSend = async () => {
|
||||
const ace = this.consoleEditor && this.consoleEditor.editor;
|
||||
//const value = ace && ace.editor &&
|
||||
|
@ -25,44 +37,52 @@ export class ConsoleViewer extends React.Component<ConsoleViewerProps> {
|
|||
const { operation } = this.props;
|
||||
|
||||
//console.log('Schema: ' + JSON.stringify(schema, null, 2));
|
||||
const value = ace && ace.editor.getValue();
|
||||
let value = ace && ace.editor.getValue();
|
||||
|
||||
const ref = schema && schema['_$ref'];
|
||||
|
||||
var valid = window && window.ajv.validate({ $ref: `specs.json${ref}` }, value);
|
||||
console.log(JSON.stringify(window.ajv.errors));
|
||||
if (!valid) {
|
||||
console.warn('INVALID REQUEST!');
|
||||
}
|
||||
//var valid = window && window.ajv.validate({ $ref: `specs.json${ref}` }, value);
|
||||
//console.log(JSON.stringify(window.ajv.errors));
|
||||
//if (!valid) {
|
||||
// console.warn('INVALID REQUEST!');
|
||||
//}
|
||||
|
||||
const endpoint = {
|
||||
method: operation.httpVerb,
|
||||
path: operation.servers[0].url + operation.path
|
||||
}
|
||||
|
||||
console.log('Value: ' + value);
|
||||
const result = await this.invoke(endpoint, { 'Content-Type': 'application/*' }, JSON.parse(value));
|
||||
if (value) {
|
||||
value = JSON.parse(value);
|
||||
}
|
||||
const result = await this.invoke(endpoint, { 'Content-Type': 'application/*' }, value);
|
||||
console.log('Result: ' + JSON.stringify(result));
|
||||
this.setState({
|
||||
result
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
async invoke(endpoint, headers, body) {
|
||||
|
||||
const fetchArgs = {
|
||||
method: endpoint.method,
|
||||
headers,
|
||||
redirect: 'manual',
|
||||
credentials: 'include'
|
||||
}
|
||||
|
||||
try {
|
||||
let url = endpoint.path;
|
||||
if (endpoint.method.toLocaleLowerCase() === 'get') {
|
||||
url = url + '?' + qs.stringify(body);
|
||||
} else {
|
||||
fetchArgs['body'] = (body) ? JSON.stringify(body) : null;
|
||||
url = url + '?' + qs.stringify(body || '');
|
||||
}
|
||||
|
||||
const result = await fetch(url, fetchArgs);
|
||||
var myHeaders = new Headers();
|
||||
myHeaders.append('Content-Type', 'application/*');
|
||||
|
||||
const request = new Request(url, {
|
||||
method: endpoint.method,
|
||||
credentials: 'include',
|
||||
redirect: 'manual',
|
||||
headers: myHeaders,
|
||||
body: (body) ? JSON.stringify(body) : null
|
||||
});
|
||||
|
||||
const result = await fetch(request);
|
||||
|
||||
const contentType = result.headers.get("content-type");
|
||||
if (contentType && contentType.indexOf("application/json") !== -1) {
|
||||
|
@ -96,6 +116,10 @@ export class ConsoleViewer extends React.Component<ConsoleViewerProps> {
|
|||
}
|
||||
};
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -105,7 +129,7 @@ export class ConsoleViewer extends React.Component<ConsoleViewerProps> {
|
|||
const hasBodySample = requestBodyContent && requestBodyContent.hasSample;
|
||||
const samples = operation.codeSamples;
|
||||
const mediaTypes = (requestBodyContent && requestBodyContent.mediaTypes) ? requestBodyContent.mediaTypes : [];
|
||||
|
||||
const { result } = this.state;
|
||||
return (
|
||||
<div>
|
||||
<h3> Console </h3>
|
||||
|
@ -120,6 +144,9 @@ export class ConsoleViewer extends React.Component<ConsoleViewerProps> {
|
|||
<ConsoleActionsRow>
|
||||
<SendButton onClick={this.onClickSend} >Send Request</SendButton>
|
||||
</ConsoleActionsRow>
|
||||
{result &&
|
||||
<SourceCodeWithCopy lang='json' source={JSON.stringify(result, null, 2)} />
|
||||
}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -7,3 +7,5 @@ import 'core-js/fn/array/find';
|
|||
import 'core-js/es6/map';
|
||||
import 'core-js/es6/set';
|
||||
import 'core-js/es6/symbol';
|
||||
|
||||
import 'whatwg-fetch';
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
import { Promise } from "core-js";
|
||||
|
||||
export class Fetch {
|
||||
|
||||
|
||||
execute(): Promise<Array<any>> {
|
||||
return fetch('https://api.github.com/orgs/lemoncode/members')
|
||||
.then((response) => this.checkStatus(response))
|
||||
.then((response) => this.parseJSON(response)
|
||||
.then((response) => { return Promise.resolve(this._parse(response)) })
|
||||
.catch((error) => this.throwError(error))
|
||||
);
|
||||
}
|
||||
|
||||
private checkStatus(response: Response): Promise<Response> {
|
||||
if (response.status >= 200 && response.status < 300) {
|
||||
return Promise.resolve(response);
|
||||
} else {
|
||||
let error = new Error(response.statusText);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
private parseJSON(response: Response): Promise<Response> {
|
||||
return response.json();
|
||||
}
|
||||
|
||||
private _parse(data) {
|
||||
return data;
|
||||
}
|
||||
|
||||
private throwError(error) {
|
||||
document.write("<p>Ops! something wrong! We are so embarrased..</p>");
|
||||
console.log(error);
|
||||
return Promise.reject(error);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user