From d99f2562acaa7219399fa0ae9806efeaebebb58c Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Thu, 9 Mar 2017 20:58:10 +0200 Subject: [PATCH] feat: support for OpenAPI object as a parameter for `init` closes #224 --- README.md | 4 ++-- lib/components/Redoc/redoc.ts | 7 ++++--- lib/index.ts | 8 ++++++-- lib/services/options.service.ts | 3 ++- lib/utils/helpers.ts | 2 +- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 350f7b1d1b..aefd18c5e2 100644 --- a/README.md +++ b/README.md @@ -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 `` 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 `` tag attribute names as the keys, e.g.: ```js Redoc.init('http://petstore.swagger.io/v2/swagger.json', { diff --git a/lib/components/Redoc/redoc.ts b/lib/components/Redoc/redoc.ts index 6e89080f67..db5e3cb555 100644 --- a/lib/components/Redoc/redoc.ts +++ b/lib/components/Redoc/redoc.ts @@ -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; }); diff --git a/lib/index.ts b/lib/index.ts index 163de55ad2..b1a27a1e36 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -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; diff --git a/lib/services/options.service.ts b/lib/services/options.service.ts index 268a43f538..1747efeff4 100644 --- a/lib/services/options.service.ts +++ b/lib/services/options.service.ts @@ -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 | 'all'; $scrollParent?: HTMLElement | Window; requiredPropsFirst?: boolean; + spec?: any; } @Injectable() diff --git a/lib/utils/helpers.ts b/lib/utils/helpers.ts index 86fcc83912..ae9a367488 100644 --- a/lib/utils/helpers.ts +++ b/lib/utils/helpers.ts @@ -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'; }