2017-01-19 00:48:55 +03:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { AppStateService } from './app-state.service';
|
2017-01-26 17:46:28 +03:00
|
|
|
import { SchemaNormalizer } from './schema-normalizer.service';
|
|
|
|
import { JsonPointer, groupBy, SpecManager, StringMap } from '../utils/';
|
2017-01-19 00:48:55 +03:00
|
|
|
|
2017-01-24 00:29:52 +03:00
|
|
|
import * as lunr from 'lunr';
|
|
|
|
|
|
|
|
interface IndexElement {
|
2017-01-26 17:46:28 +03:00
|
|
|
menuId: string;
|
2017-01-24 00:29:52 +03:00
|
|
|
title: string;
|
|
|
|
body: string;
|
|
|
|
pointer: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const index = lunr(function () {
|
2017-01-26 17:46:28 +03:00
|
|
|
this.field('menuId', {boost: 0});
|
2017-01-24 00:29:52 +03:00
|
|
|
this.field('title', {boost: 1.5});
|
|
|
|
this.field('body');
|
|
|
|
this.ref('pointer');
|
|
|
|
})
|
|
|
|
|
|
|
|
const store:StringMap<IndexElement> = {};
|
2017-01-19 00:48:55 +03:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class SearchService {
|
2017-01-26 17:46:28 +03:00
|
|
|
normalizer: SchemaNormalizer;
|
2017-01-24 00:29:52 +03:00
|
|
|
constructor(private app: AppStateService, private spec: SpecManager) {
|
2017-01-26 17:46:28 +03:00
|
|
|
this.normalizer = new SchemaNormalizer(spec);
|
2017-01-19 00:48:55 +03:00
|
|
|
}
|
2017-01-24 00:29:52 +03:00
|
|
|
|
2017-01-19 00:48:55 +03:00
|
|
|
ensureSearchVisible(containingPointers: string[]) {
|
|
|
|
this.app.searchContainingPointers.next(containingPointers);
|
|
|
|
}
|
2017-01-24 00:29:52 +03:00
|
|
|
|
|
|
|
indexAll() {
|
|
|
|
const swagger = this.spec.schema;
|
|
|
|
|
|
|
|
this.indexPaths(swagger);
|
|
|
|
}
|
|
|
|
|
|
|
|
search(q):StringMap<IndexElement[]> {
|
|
|
|
const res:IndexElement[] = index.search(q).map(res => {
|
|
|
|
return store[res.ref];
|
|
|
|
});
|
2017-01-26 17:46:28 +03:00
|
|
|
const grouped = groupBy(res, 'menuId');
|
2017-01-24 00:29:52 +03:00
|
|
|
return grouped;
|
|
|
|
}
|
|
|
|
|
|
|
|
index(element: IndexElement) {
|
|
|
|
index.add(element);
|
|
|
|
store[element.pointer] = element;
|
|
|
|
}
|
|
|
|
|
|
|
|
indexPaths(swagger:any) {
|
|
|
|
const paths = swagger.paths
|
|
|
|
const basePtr = '#/paths';
|
|
|
|
Object.keys(paths).forEach(path => {
|
|
|
|
let opearations = paths[path];
|
|
|
|
Object.keys(opearations).forEach(verb => {
|
|
|
|
const opearation = opearations[verb];
|
|
|
|
const ptr = JsonPointer.join(basePtr, [path, verb]);
|
|
|
|
this.indexOperation(opearation, ptr);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-01-26 17:46:28 +03:00
|
|
|
indexOperation(operation:any, operationPointer:string) {
|
2017-01-24 00:29:52 +03:00
|
|
|
this.index({
|
2017-01-26 17:46:28 +03:00
|
|
|
pointer: operationPointer,
|
|
|
|
menuId: operationPointer,
|
2017-01-24 00:29:52 +03:00
|
|
|
title: operation.summary,
|
|
|
|
body: operation.description
|
|
|
|
});
|
2017-01-26 17:46:28 +03:00
|
|
|
this.indexOperationResponses(operation, operationPointer);
|
|
|
|
this.indexOperationParameters(operation, operationPointer)
|
|
|
|
}
|
|
|
|
|
|
|
|
indexOperationParameters(operation: any, operationPointer: string) {
|
|
|
|
const parameters = operation.parameters;
|
|
|
|
for (let i=0; i<parameters.length; ++i) {
|
|
|
|
const param = parameters[i];
|
|
|
|
const paramPointer = JsonPointer.join(operationPointer, ['parameters', i]);
|
|
|
|
this.index({
|
|
|
|
pointer: paramPointer,
|
|
|
|
menuId: operationPointer,
|
|
|
|
title: param.in === 'body' ? '' : param.name,
|
|
|
|
body: param.description
|
|
|
|
});
|
|
|
|
|
|
|
|
if (param.in === 'body') {
|
|
|
|
this.indexSchema(param.schema, '', JsonPointer.join(paramPointer, ['schema']), operationPointer);
|
|
|
|
}
|
|
|
|
}
|
2017-01-24 00:29:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
indexOperationResponses(operation:any, operationPtr:string) {
|
|
|
|
const responses = operation.responses;
|
|
|
|
if (!responses) return;
|
|
|
|
Object.keys(responses).forEach(code => {
|
|
|
|
const resp = responses[code];
|
|
|
|
const respPtr = JsonPointer.join(operationPtr, ['responses', code]);
|
|
|
|
this.index({
|
|
|
|
pointer: respPtr,
|
2017-01-26 17:46:28 +03:00
|
|
|
menuId: operationPtr,
|
2017-01-24 00:29:52 +03:00
|
|
|
title: code,
|
|
|
|
body: resp.description
|
|
|
|
});
|
|
|
|
|
|
|
|
if (resp.schema) {
|
|
|
|
this.indexSchema(resp.schema, '', JsonPointer.join(respPtr, 'schema'), operationPtr);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
indexSchema(_schema:any, name: string, absolutePointer: string, menuPointer: string) {
|
|
|
|
if (!_schema) return;
|
|
|
|
let schema = _schema;
|
|
|
|
let title = name;
|
2017-01-26 17:46:28 +03:00
|
|
|
|
|
|
|
schema = this.normalizer.normalize(schema, absolutePointer);
|
2017-01-24 00:29:52 +03:00
|
|
|
|
|
|
|
let body = schema.description; // TODO: defaults, examples, etc...
|
|
|
|
|
|
|
|
if (schema.type === 'array') {
|
|
|
|
this.indexSchema(schema.items, title, JsonPointer.join(absolutePointer, ['items']), menuPointer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.index({
|
|
|
|
pointer: absolutePointer,
|
2017-01-26 17:46:28 +03:00
|
|
|
menuId: menuPointer,
|
2017-01-24 00:29:52 +03:00
|
|
|
title,
|
|
|
|
body
|
|
|
|
})
|
|
|
|
|
|
|
|
if (schema.properties) {
|
|
|
|
Object.keys(schema.properties).forEach(propName => {
|
|
|
|
let propPtr = JsonPointer.join(absolutePointer, ['properties', propName]);
|
|
|
|
this.indexSchema(schema.properties[propName], propName, propPtr, menuPointer);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2017-01-19 00:48:55 +03:00
|
|
|
}
|