Skip to content

Commit

Permalink
Merge pull request #1346 from opencomponents/add-action-route-support
Browse files Browse the repository at this point in the history
Add support for Actions
  • Loading branch information
ricardo-devis-agullo authored Dec 23, 2023
2 parents 60b6b6f + 7e1b2d8 commit 524adde
Show file tree
Hide file tree
Showing 8 changed files with 10,465 additions and 7 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ npm-debug.log
node_modules
.yarn
# yarn.lock
package-lock.json

src/components/oc.json
src/components/base-component-handlebars/_package
Expand Down
10,296 changes: 10,296 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
"multer": "1.4.3",
"nice-cache": "0.0.5",
"oc-client": "4.0.1",
"oc-client-browser": "1.6.6",
"oc-client-browser": "1.6.8",
"oc-empty-response-handler": "1.0.2",
"oc-get-unix-utc-timestamp": "1.0.6",
"oc-s3-storage-adapter": "2.1.1",
Expand All @@ -128,4 +128,4 @@
"universalify": "^2.0.0",
"yargs": "17.7.2"
}
}
}
2 changes: 2 additions & 0 deletions src/registry/routes/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Config } from '../../types';
import { Request, RequestHandler, Response } from 'express';

type Component = {
action?: string;
name: string;
version: string;
parameters: Record<string, string>;
Expand Down Expand Up @@ -69,6 +70,7 @@ export default function components(
getComponent(
{
conf: res.conf,
action: component.action,
name: component.name,
headers: req.headers,
ip: req.ip!,
Expand Down
6 changes: 5 additions & 1 deletion src/registry/routes/helpers/get-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { IncomingHttpHeaders } from 'http';
import { fromPromise } from 'universalify';

export interface RendererOptions {
action?: string;
conf: Config;
headers: IncomingHttpHeaders;
ip: string;
Expand Down Expand Up @@ -191,7 +192,7 @@ export default function getComponent(conf: Config, repository: Repository) {
component.oc.parameters
);

if (!validationResult.isValid) {
if (!options.action && !validationResult.isValid) {
return callback({
status: 400,
response: {
Expand Down Expand Up @@ -323,13 +324,15 @@ export default function getComponent(conf: Config, repository: Repository) {
}

const response: {
baseUrl: string;
type: string;
version: string;
requestVersion: string;
name: string;
renderMode: string;
href?: string;
} = {
baseUrl: conf.baseUrl,
type: conf.local ? 'oc-component-local' : 'oc-component',
version: component.version,
requestVersion: requestedComponent.version,
Expand Down Expand Up @@ -461,6 +464,7 @@ export default function getComponent(conf: Config, repository: Repository) {
const setEmptyResponse =
emptyResponseHandler.contextDecorator(returnComponent);
const contextObj = {
action: options.action,
acceptLanguage: acceptLanguageParser.parse(acceptLanguage!),
baseUrl: conf.baseUrl,
env: { ...conf.env, ...env },
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/mocked-components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
'timeout-component': require('./timeout'),
'undefined-component': require('./undefined'),
'simple-component': require('./simple'),
'required-parameter-component': require('./parameters'),
'response-headers-component': require('./response-headers'),
'another-response-headers-component': require('./another-response-headers')
};
35 changes: 35 additions & 0 deletions test/fixtures/mocked-components/parameters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

module.exports = {
package: {
name: 'required-parameter-component',
version: '1.0.0',
oc: {
container: false,
renderInfo: false,
files: {
template: {
type: 'jade',
hashKey: '8c1fbd954f2b0d8cd5cf11c885fed4805225749f',
src: 'template.js'
},
dataProvider: {
type: 'node.js',
hashKey: '123457',
src: 'server.js'
}
},
parameters: {
userId: {
description: 'The id identifying the user',
mandatory: true,
type: 'string'
}
}
}
},
data: '"use strict";module.exports.data = function(ctx, cb){cb(null, {done:true});};',
view:
'var oc=oc||{};oc.components=oc.components||{},oc.components["8c1fbd954f2b0d8cd5cf11c885fed4805225749f"]' +
'=function(){var o=[];return o.push("<div>hello</div>"),o.join("")};'
};
127 changes: 124 additions & 3 deletions test/unit/registry-routes-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const expect = require('chai').expect;
const sinon = require('sinon');
const settings = require('../../dist/resources/settings').default;

describe('registry : routes : components', () => {
const ComponentsRoute =
Expand Down Expand Up @@ -41,17 +42,25 @@ describe('registry : routes : components', () => {
};
};

const makeRequest = function (body, cb) {
const makeRequest = function (body, headersOrCallback, cb) {
let callback = cb;
let headers = headersOrCallback;

if (typeof headersOrCallback === 'function') {
callback = headersOrCallback;
headers = {};
}

componentsRoute(
{ headers: {}, body: body },
{ headers, body },
{
conf: { baseUrl: 'http://components.com/' },
status: function (jsonCode) {
code = jsonCode;
return {
json: function (jsonResponse) {
response = jsonResponse;
cb();
callback();
}
};
}
Expand Down Expand Up @@ -319,4 +328,116 @@ describe('registry : routes : components', () => {
});
});
});

describe('when getting a component that has custom parameters', () => {
before(done => {
initialise(mockedComponents['required-parameter-component']);
componentsRoute = ComponentsRoute({}, mockedRepository);

makeRequest(
{
components: [
{
name: 'required-parameter-component',
version: '1.X.X',
parameters: { userId: 'USERID' }
}
]
},
done
);
});

const expectedResponse = [
{
status: 200,
response: {
name: 'required-parameter-component',
type: 'oc-component',
requestVersion: '1.X.X',
version: '1.0.0'
}
}
];

it('should return a 200 response', () => {
expect(response[0].response).to.include(expectedResponse[0].response);
});
});

describe('when getting a component that has missing required parameters', () => {
before(done => {
initialise(mockedComponents['required-parameter-component']);
componentsRoute = ComponentsRoute({}, mockedRepository);

makeRequest(
{
components: [
{
name: 'required-parameter-component',
version: '1.X.X'
}
]
},
done
);
});

const expectedResponse = [
{
status: 400,
response: {
name: 'required-parameter-component',
code: 'NOT_VALID_REQUEST',
error: 'Expected mandatory parameters are missing: userId',
requestVersion: '1.X.X'
}
}
];

it('should return 400 status code', () => {
expect(response).to.be.eql(expectedResponse);
});
});

describe('when making valid info request for a component action', () => {
before(done => {
initialise(mockedComponents['required-parameter-component']);
componentsRoute = ComponentsRoute({}, mockedRepository);

makeRequest(
{
components: [
{
action: 'someAction',
name: 'required-parameter-component',
version: '1.X.X'
}
]
},
{
accept: settings.registry.acceptUnrenderedHeader
},
done
);
});

const expectedResponse = [
{
status: 200,
response: {
href: 'http://components.com/required-parameter-component/1.X.X',
name: 'required-parameter-component',
type: 'oc-component',
requestVersion: '1.X.X',
version: '1.0.0',
renderMode: 'unrendered'
}
}
];

it('should return a 200 response', () => {
expect(response[0].response).to.include(expectedResponse[0].response);
});
});
});

0 comments on commit 524adde

Please sign in to comment.