Skip to content

Commit

Permalink
feat: support for OpenAPI object as a parameter for init
Browse files Browse the repository at this point in the history
closes #224
  • Loading branch information
RomanHotsiy committed Mar 9, 2017
1 parent 1e96f88 commit d99f256
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down
7 changes: 4 additions & 3 deletions lib/components/Redoc/redoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -29,7 +29,7 @@ export class Redoc extends BaseComponent implements OnInit {

error: any;
specLoaded: boolean;
options: any;
options: Options;

loadingProgress: number;

Expand Down Expand Up @@ -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;
});

Expand Down
8 changes: 6 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion lib/services/options.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const OPTION_NAMES = new Set([
'requiredPropsFirst'
]);

interface Options {
export interface Options {
scrollYOffset?: any;
disableLazySchemas?: boolean;
specUrl?: string;
Expand All @@ -29,6 +29,7 @@ interface Options {
expandResponses?: Set<string> | 'all';
$scrollParent?: HTMLElement | Window;
requiredPropsFirst?: boolean;
spec?: any;
}

@Injectable()
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

Expand Down

0 comments on commit d99f256

Please sign in to comment.