Skip to content

Commit

Permalink
Merge branch 'main' into fix/ProxyMiddlewareForConnect
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasqueck authored Aug 28, 2023
2 parents c1d41f9 + 0510437 commit 6e4134a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 14 deletions.
6 changes: 6 additions & 0 deletions packages/preview-middleware/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @sap-ux/preview-middleware

## 0.4.0

### Minor Changes

- 4b906238: Preview html is shown in CAP project using cds-plugin-ui5@0.4.0

## 0.3.9

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/preview-middleware/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"bugs": {
"url": "https://github.com/SAP/open-ux-tools/issues?q=is%3Aopen+is%3Aissue+label%3Abug+label%3Apreview-middleware"
},
"version": "0.3.9",
"version": "0.4.0",
"license": "Apache-2.0",
"author": "@SAP/ux-tools-team",
"main": "dist/index.js",
Expand Down
23 changes: 15 additions & 8 deletions packages/preview-middleware/src/base/flp.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ReaderCollection } from '@ui5/fs';
import { render } from 'ejs';
import type { Request, Response } from 'express';
import type { Request, RequestHandler, Response, Router } from 'express';
import { readFileSync } from 'fs';
import { dirname, join, relative } from 'path';
import type { App, FlpConfig } from '../types';
Expand All @@ -10,6 +10,13 @@ import { deleteChange, readChanges, writeChange } from './flex';
import type { MiddlewareUtils } from '@ui5/server';
import type { Manifest, UI5FlexLayer } from '@sap-ux/project-access';

/**
* Enhanced request handler that exposes a list of endpoints for the cds-plugin-ui5.
*/
export type EnhancedRouter = Router & {
getAppPages?: () => string[];
};

/**
* Default theme
*/
Expand Down Expand Up @@ -70,7 +77,7 @@ export interface TemplateConfig {
export class FlpSandbox {
protected templateConfig: TemplateConfig;
public readonly config: FlpConfig;
public readonly router: any;
public readonly router: EnhancedRouter;

/**
* Constructor setting defaults and keeping reference to workspace resources.
Expand Down Expand Up @@ -218,12 +225,12 @@ export class FlpSandbox {
});
const api = '/preview/api/changes';
this.router.use(api, json());
this.router.get(api, async (_req: Request, res: Response) => {
this.router.get(api, (async (_req: Request, res: Response) => {
res.status(200)
.contentType('application/json')
.send(await readChanges(this.project, this.logger));
});
this.router.post(api, async (req: Request, res: Response) => {
}) as RequestHandler);
this.router.post(api, (async (req: Request, res: Response) => {
try {
const { success, message } = writeChange(
req.body,
Expand All @@ -238,8 +245,8 @@ export class FlpSandbox {
} catch (error) {
res.status(500).send(error.message);
}
});
this.router.delete(api, async (req: Request, res: Response) => {
}) as RequestHandler);
this.router.delete(api, (async (req: Request, res: Response) => {
try {
const { success, message } = deleteChange(
req.body,
Expand All @@ -254,7 +261,7 @@ export class FlpSandbox {
} catch (error) {
res.status(500).send(error.message);
}
});
}) as RequestHandler);

return [
{
Expand Down
2 changes: 1 addition & 1 deletion packages/preview-middleware/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface App {
*/
export interface FlpConfig {
path: string;
intent?: Intent;
intent: Intent;
rta?: {
layer?: UI5FlexLayer;
};
Expand Down
4 changes: 3 additions & 1 deletion packages/preview-middleware/src/ui5/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async function initAdp(rootProject: ReaderCollection, config: AdpPreviewConfig,
const layer = await adp.init(JSON.parse(await files[0].getString()));
flp.config.rta = { layer };
await flp.init(adp.descriptor.manifest, adp.descriptor.name, adp.resources);
flp.router.use(adp.descriptor.url, adp.proxy.bind(adp));
flp.router.use(adp.descriptor.url, adp.proxy.bind(adp) as RequestHandler);
} else {
throw new Error('ADP configured but no manifest.appdescr_variant found.');
}
Expand Down Expand Up @@ -56,6 +56,8 @@ async function createRouter({ resources, options, middlewareUtil }: MiddlewarePa
throw new Error('No manifest.json found.');
}
}
// add exposed endpoints for cds-plugin-ui5
flp.router.getAppPages = () => [`${flp.config.path}#${flp.config.intent.object}-${flp.config.intent.action}`];
return flp.router;
}

Expand Down
17 changes: 14 additions & 3 deletions packages/preview-middleware/test/unit/ui5/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { Resource } from '@ui5/fs';
import { readFileSync } from 'fs';
import { join } from 'path';
import nock from 'nock';
import type { EnhancedRouter } from '../../../src/base/flp';

jest.mock('@sap-ux/store', () => {
return {
Expand All @@ -18,9 +19,8 @@ jest.mock('@sap-ux/store', () => {
};
});

// middleware function wrapper for testing to simplify tests
async function getTestServer(fixture?: string, configuration: Partial<Config> = {}): Promise<any> {
const router = await (previewMiddleware as any).default({
async function getRouter(fixture?: string, configuration: Partial<Config> = {}): Promise<EnhancedRouter> {
return await (previewMiddleware as any).default({
options: { configuration },
resources: {
rootProject: {
Expand Down Expand Up @@ -48,6 +48,11 @@ async function getTestServer(fixture?: string, configuration: Partial<Config> =
},
middlewareUtil: {}
});
}

// middleware function wrapper for testing to simplify tests
async function getTestServer(fixture?: string, configuration: Partial<Config> = {}): Promise<any> {
const router = await getRouter(fixture, configuration);
const app = express();
app.use(router);
return supertest(app);
Expand Down Expand Up @@ -113,4 +118,10 @@ describe('ui5/middleware', () => {
expect(error).toBeDefined();
}
});

test('exposed endpoints (for cds-plugin-ui5)', async () => {
const router = await getRouter('simple-app');
expect(router.getAppPages).toBeDefined();
expect(router.getAppPages?.()).toEqual(['/test/flp.html#app-preview']);
});
});

0 comments on commit 6e4134a

Please sign in to comment.