Skip to content

Commit

Permalink
Replace image path/handler with generic file one
Browse files Browse the repository at this point in the history
  • Loading branch information
John Schulz committed Oct 18, 2019
1 parent 363d3bf commit 478f80c
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { InstallationAttributes } from '../../common/types';
import * as Registry from '../registry';
import { createInstallableFrom } from './index';

export { SearchParams } from '../registry';
export { SearchParams, fetchFile as getFile } from '../registry';

function nameAsTitle(name: string) {
return name.charAt(0).toUpperCase() + name.substr(1).toLowerCase();
Expand Down Expand Up @@ -68,9 +68,6 @@ export async function getPackageInfo(options: {
return createInstallableFrom(updated, savedObject);
}

export const getImage = async (options: Registry.ImageRequestParams) =>
Registry.fetchImage(options);

export async function getInstallationObject(options: {
savedObjectsClient: SavedObjectsClientContract;
pkgkey: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
*/

import { AssetType, Request, ResponseToolkit } from '../../common/types';
import { API_ROOT } from '../../common/routes';
import { PluginContext } from '../plugin';
import { getClient } from '../saved_objects';
import {
SearchParams,
getCategories,
getClusterAccessor,
getImage,
getFile,
getPackageInfo,
getPackages,
installPackage,
removeInstallation,
} from './index';
import { ImageRequestParams } from '../registry';

interface Extra extends ResponseToolkit {
context: PluginContext;
Expand All @@ -33,10 +33,6 @@ interface PackageRequest extends Request {
};
}

interface ImageRequest extends Request {
params: Request['params'] & ImageRequestParams;
}

interface InstallAssetRequest extends Request {
params: AssetRequestParams;
}
Expand Down Expand Up @@ -71,8 +67,10 @@ export async function handleGetInfo(req: PackageRequest, extra: Extra) {
return packageInfo;
}

export const handleGetImage = async (req: ImageRequest, extra: Extra) => {
const response = await getImage(req.params);
export const handleGetFile = async (req: Request, extra: Extra) => {
if (!req.url.path) throw new Error('path is required');
const filePath = req.url.path.replace(API_ROOT, '');
const response = await getFile(filePath);
const newResponse = extra.response(response.body);
// set the content type from the registry response
newResponse.header('Content-Type', response.headers.get('content-type') || '');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ export interface SearchParams {
category?: CategoryId;
}

export interface ImageRequestParams {
pkgkey: string;
imgPath: string;
}

export async function fetchList(params?: SearchParams): Promise<RegistryList> {
const { registryUrl } = epmConfigStore.getConfig();
const url = new URL(`${registryUrl}/search`);
Expand All @@ -46,10 +41,9 @@ export async function fetchInfo(key: string): Promise<RegistryPackage> {
return fetchUrl(`${registryUrl}/package/${key}`).then(JSON.parse);
}

export async function fetchImage(params: ImageRequestParams): Promise<Response> {
export async function fetchFile(filePath: string): Promise<Response> {
const { registryUrl } = epmConfigStore.getConfig();
const { pkgkey, imgPath } = params;
return getResponse(`${registryUrl}/package/${pkgkey}/img/${imgPath}`);
return getResponse(`${registryUrl}${filePath}`);
}

export async function fetchCategories(): Promise<CategorySummaryList> {
Expand Down
6 changes: 2 additions & 4 deletions x-pack/legacy/plugins/integrations_manager/server/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import { ServerRoute } from '../common/types';
import * as CommonRoutes from '../common/routes';
import * as Packages from './packages/handlers';

const API_IMG_PATTERN = `${CommonRoutes.API_ROOT}/package/{pkgkey}/img/{imgPath*}`;

// Manager public API paths
export const routes: ServerRoute[] = [
{
Expand All @@ -26,9 +24,9 @@ export const routes: ServerRoute[] = [
},
{
method: 'GET',
path: API_IMG_PATTERN,
path: `${CommonRoutes.API_INFO_PATTERN}/{filePath*}`,
options: { tags: [`access:${PLUGIN.ID}`], json: { space: 2 } },
handler: Packages.handleGetImage,
handler: Packages.handleGetFile,
},
{
method: 'GET',
Expand Down

0 comments on commit 478f80c

Please sign in to comment.