File size: 1,145 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
import { stringify } from 'qs';
async function fetchDocsEndpoint( endpoint, params, callback ) {
const queryParams = stringify( params );
const query = queryParams === '' ? '' : `?${ queryParams }`;
const res = await fetch( `/devdocs/service/${ endpoint }${ query }` );
if ( ! res.ok ) {
callback( `Error invoking /devdocs/${ endpoint }: ${ await res.text() }`, null );
return;
}
const contentType = res.headers.get( 'content-type' );
if ( contentType && contentType.includes( 'application/json' ) ) {
callback( null, await res.json() );
} else {
callback( null, await res.text() );
}
}
/**
* This service allows you to retrieve a document list (with title, snippets and path) based on
* query term or filename(s), and also to separately retrieve the document body by path.
*/
export default {
search: function ( term, callback ) {
fetchDocsEndpoint( 'search', { q: term }, callback );
},
list: function ( filenames, callback ) {
fetchDocsEndpoint( 'list', { files: filenames.join( ',' ) }, callback );
},
fetch: function ( path, callback ) {
fetchDocsEndpoint( 'content', { path: path }, callback );
},
};
|