feat: support for OpenAPI object as a parameter for init

closes #224
This commit is contained in:
Roman Hotsiy 2017-03-09 20:58:10 +02:00
parent 1e96f88ea8
commit d99f2562ac
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
5 changed files with 15 additions and 9 deletions

View File

@ -144,9 +144,9 @@ ReDoc makes use of the following [vendor extensions](http://swagger.io/specifica
## Advanced usage
Instead of adding `spec-url` attribute to the `<redoc>` element you can initialize ReDoc via globally exposed `Redoc` object:
```js
Redoc.init(specUrl, options)
Redoc.init(specOrSpecUrl, options)
```
`specOrSpecUrl` is either JSON object with specification or an URL to the spec in `JSON` or `YAML` format.
`options` is javascript object with camel-cased version of `<redoc>` tag attribute names as the keys, e.g.:
```js
Redoc.init('http://petstore.swagger.io/v2/swagger.json', {

View File

@ -15,7 +15,7 @@ import { BaseComponent } from '../base';
import * as detectScollParent from 'scrollparent';
import { SpecManager } from '../../utils/spec-manager';
import { SearchService, OptionsService, Hash, AppStateService, SchemaHelper } from '../../services/';
import { SearchService, OptionsService, Options, Hash, AppStateService, SchemaHelper } from '../../services/';
import { LazyTasksService } from '../../shared/components/LazyFor/lazy-for';
@Component({
@ -29,7 +29,7 @@ export class Redoc extends BaseComponent implements OnInit {
error: any;
specLoaded: boolean;
options: any;
options: Options;
loadingProgress: number;
@ -84,7 +84,8 @@ export class Redoc extends BaseComponent implements OnInit {
}
load() {
this.specMgr.load(this.options.specUrl).catch(err => {
// bunlde spec directly if passsed or load by URL
this.specMgr.load(this.options.spec || this.options.specUrl).catch(err => {
throw err;
});

View File

@ -5,6 +5,7 @@ import { enableProdMode } from '@angular/core';
import { Redoc } from './components/index';
import { BrowserDomAdapter as DOM } from './utils/browser-adapter';
import { disableDebugTools } from '@angular/platform-browser';
import { isString } from './utils/helpers';
var bootstrapRedoc;
if (AOT) {
@ -21,13 +22,16 @@ if (IS_PRODUCTION) {
export const version = LIB_VERSION;
var moduleRef;
export function init(specUrl:string, options:any = {}) {
export function init(specUrlOrSpec:string|any, options:any = {}) {
if (moduleRef) {
destroy();
}
Redoc._preOptions = options;
options.specUrl = options.specUrl || specUrl;
options.specUrl = options.specUrl || (isString(specUrlOrSpec) ? specUrlOrSpec : '');
if (!isString(specUrlOrSpec)) {
options.spec = specUrlOrSpec;
}
return bootstrapRedoc()
.then(appRef => {
moduleRef = appRef;

View File

@ -19,7 +19,7 @@ const OPTION_NAMES = new Set([
'requiredPropsFirst'
]);
interface Options {
export interface Options {
scrollYOffset?: any;
disableLazySchemas?: boolean;
specUrl?: string;
@ -29,6 +29,7 @@ interface Options {
expandResponses?: Set<string> | 'all';
$scrollParent?: HTMLElement | Window;
requiredPropsFirst?: boolean;
spec?: any;
}
@Injectable()

View File

@ -8,7 +8,7 @@ export function stringify(obj:any) {
return JSON.stringify(obj);
}
export function isString(str:any) {
export function isString(str:any):str is String {
return typeof str === 'string';
}