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 ## Advanced usage
Instead of adding `spec-url` attribute to the `<redoc>` element you can initialize ReDoc via globally exposed `Redoc` object: Instead of adding `spec-url` attribute to the `<redoc>` element you can initialize ReDoc via globally exposed `Redoc` object:
```js ```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.: `options` is javascript object with camel-cased version of `<redoc>` tag attribute names as the keys, e.g.:
```js ```js
Redoc.init('http://petstore.swagger.io/v2/swagger.json', { 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 * as detectScollParent from 'scrollparent';
import { SpecManager } from '../../utils/spec-manager'; 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'; import { LazyTasksService } from '../../shared/components/LazyFor/lazy-for';
@Component({ @Component({
@ -29,7 +29,7 @@ export class Redoc extends BaseComponent implements OnInit {
error: any; error: any;
specLoaded: boolean; specLoaded: boolean;
options: any; options: Options;
loadingProgress: number; loadingProgress: number;
@ -84,7 +84,8 @@ export class Redoc extends BaseComponent implements OnInit {
} }
load() { 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; throw err;
}); });

View File

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

View File

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

View File

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