Skip to content

Commit

Permalink
Add code
Browse files Browse the repository at this point in the history
  • Loading branch information
tiberiuichim committed Nov 3, 2020
1 parent d2ae331 commit db0404f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -14,3 +14,4 @@ dist
.env.development.local
.env.test.local
.env.production.local
*~
5 changes: 2 additions & 3 deletions README.md
Expand Up @@ -5,9 +5,8 @@

## Features

###

Demo GIF
This package offers an Express middleware for eea.rdfmarshaller's ``@@rdf``
view, piping the backend resource with a proper VH-aware request.

## Getting started

Expand Down
51 changes: 48 additions & 3 deletions src/index.js
@@ -1,5 +1,50 @@
const applyConfig = (config) => {
import superagent from 'superagent';
import cookie from 'react-cookie';
import { parse as parseUrl } from 'url';

import { settings } from '~/config';

export const getAPIResourceWithAuth = (req) =>
new Promise((resolve) => {
const internalApiUrl = parseUrl(
settings.internalApiPath || settings.apiPath,
);
const apiUrl = parseUrl(settings.apiPath);

const scheme = apiUrl.protocol.slice(0, apiUrl.protocol.length - 1);

const path = `/VirtualHostBase/${scheme}/${apiUrl.hostname}:${apiUrl.port}${apiUrl.path}/VirtualHostRoot${req.path}`;
const url = `${internalApiUrl.hostname}:${internalApiUrl.port}${path}`;
const request = superagent.get(url).responseType('blob');
const authToken = cookie.load('auth_token');
if (authToken) {
request.set('Authorization', `Bearer ${authToken}`);
}
request.end((error, res = {}) => {
if (error) {
resolve(res || error);
} else {
resolve(res);
}
});
});

export default (config) => {
if (__SERVER__) {
const express = require('express');
const middleware = express.Router();

middleware.all('/**/@@rdf', function (req, res, next) {
getAPIResourceWithAuth(req).then((resource) => {
res.send(resource.body);
});
});
middleware.id = 'rdf-proxy-middleware';

config.settings.expressMiddleware = [
...config.settings.expressMiddleware,
middleware,
];
}
return config;
};

export default applyConfig;

0 comments on commit db0404f

Please sign in to comment.