Skip to content

Commit d99f256

Browse files
committed
feat: support for OpenAPI object as a parameter for init
closes #224
1 parent 1e96f88 commit d99f256

File tree

5 files changed

+15
-9
lines changed

5 files changed

+15
-9
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ ReDoc makes use of the following [vendor extensions](http://swagger.io/specifica
144144
## Advanced usage
145145
Instead of adding `spec-url` attribute to the `<redoc>` element you can initialize ReDoc via globally exposed `Redoc` object:
146146
```js
147-
Redoc.init(specUrl, options)
147+
Redoc.init(specOrSpecUrl, options)
148148
```
149-
149+
`specOrSpecUrl` is either JSON object with specification or an URL to the spec in `JSON` or `YAML` format.
150150
`options` is javascript object with camel-cased version of `<redoc>` tag attribute names as the keys, e.g.:
151151
```js
152152
Redoc.init('http://petstore.swagger.io/v2/swagger.json', {

lib/components/Redoc/redoc.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { BaseComponent } from '../base';
1515
import * as detectScollParent from 'scrollparent';
1616

1717
import { SpecManager } from '../../utils/spec-manager';
18-
import { SearchService, OptionsService, Hash, AppStateService, SchemaHelper } from '../../services/';
18+
import { SearchService, OptionsService, Options, Hash, AppStateService, SchemaHelper } from '../../services/';
1919
import { LazyTasksService } from '../../shared/components/LazyFor/lazy-for';
2020

2121
@Component({
@@ -29,7 +29,7 @@ export class Redoc extends BaseComponent implements OnInit {
2929

3030
error: any;
3131
specLoaded: boolean;
32-
options: any;
32+
options: Options;
3333

3434
loadingProgress: number;
3535

@@ -84,7 +84,8 @@ export class Redoc extends BaseComponent implements OnInit {
8484
}
8585

8686
load() {
87-
this.specMgr.load(this.options.specUrl).catch(err => {
87+
// bunlde spec directly if passsed or load by URL
88+
this.specMgr.load(this.options.spec || this.options.specUrl).catch(err => {
8889
throw err;
8990
});
9091

lib/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { enableProdMode } from '@angular/core';
55
import { Redoc } from './components/index';
66
import { BrowserDomAdapter as DOM } from './utils/browser-adapter';
77
import { disableDebugTools } from '@angular/platform-browser';
8+
import { isString } from './utils/helpers';
89

910
var bootstrapRedoc;
1011
if (AOT) {
@@ -21,13 +22,16 @@ if (IS_PRODUCTION) {
2122
export const version = LIB_VERSION;
2223

2324
var moduleRef;
24-
export function init(specUrl:string, options:any = {}) {
25+
export function init(specUrlOrSpec:string|any, options:any = {}) {
2526
if (moduleRef) {
2627
destroy();
2728
}
2829

2930
Redoc._preOptions = options;
30-
options.specUrl = options.specUrl || specUrl;
31+
options.specUrl = options.specUrl || (isString(specUrlOrSpec) ? specUrlOrSpec : '');
32+
if (!isString(specUrlOrSpec)) {
33+
options.spec = specUrlOrSpec;
34+
}
3135
return bootstrapRedoc()
3236
.then(appRef => {
3337
moduleRef = appRef;

lib/services/options.service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const OPTION_NAMES = new Set([
1919
'requiredPropsFirst'
2020
]);
2121

22-
interface Options {
22+
export interface Options {
2323
scrollYOffset?: any;
2424
disableLazySchemas?: boolean;
2525
specUrl?: string;
@@ -29,6 +29,7 @@ interface Options {
2929
expandResponses?: Set<string> | 'all';
3030
$scrollParent?: HTMLElement | Window;
3131
requiredPropsFirst?: boolean;
32+
spec?: any;
3233
}
3334

3435
@Injectable()

lib/utils/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export function stringify(obj:any) {
88
return JSON.stringify(obj);
99
}
1010

11-
export function isString(str:any) {
11+
export function isString(str:any):str is String {
1212
return typeof str === 'string';
1313
}
1414

0 commit comments

Comments
 (0)