Skip to content

Commit

Permalink
Added extension for ExpressJS
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienCastex committed Nov 25, 2017
1 parent 0664554 commit 9114666
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/extensions/Express.1.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { WebDAVServer } from '../server/v2/export';
export declare function express(root: string, server: WebDAVServer, options: any): (req: any, res: any, next: any) => any;
17 changes: 17 additions & 0 deletions lib/extensions/Express.1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Path_1 = require("../manager/v2/Path");
function express(root, server, options) {
var path = new Path_1.Path(root).toString(false);
var pathRegex = new RegExp('^(' + path + ')((\/[^\/]+)*)\/?$');
return function (req, res, next) {
var matches = pathRegex.exec(req.url);
if (!matches)
return next();
var root = matches[1];
var subUrl = matches[2];
req.url = new Path_1.Path(subUrl).toString(false);
server.executeRequest(req, res, root);
};
}
exports.express = express;
8 changes: 8 additions & 0 deletions lib/extensions/Express.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { WebDAVServer } from '../server/v2/export';
/**
* Mount a WebDAVServer instance on a ExpressJS server.
*
* @param root Root path of the mount
* @param server Server to mount
*/
export declare function express(root: string, server: WebDAVServer): (req: any, res: any, next: any) => any;
22 changes: 22 additions & 0 deletions lib/extensions/Express.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Path_1 = require("../manager/v2/Path");
/**
* Mount a WebDAVServer instance on a ExpressJS server.
*
* @param root Root path of the mount
* @param server Server to mount
*/
function express(root, server) {
var path = new Path_1.Path(root).toString(false);
var pathRegex = new RegExp('^' + path + '((\/[^\/]+)*)\/?$');
return function (req, res, next) {
var matches = pathRegex.exec(req.url);
if (!matches)
return next();
var subUrl = matches[1];
req.url = new Path_1.Path(subUrl).toString(false);
server.executeRequest(req, res, path);
};
}
exports.express = express;
1 change: 1 addition & 0 deletions lib/extensions/export.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Express';
6 changes: 6 additions & 0 deletions lib/extensions/export.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
__export(require("./Express"));
1 change: 1 addition & 0 deletions lib/extensions/exports.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Express';
6 changes: 6 additions & 0 deletions lib/extensions/exports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
__export(require("./Express"));
2 changes: 2 additions & 0 deletions lib/index.v2.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ export * from './user/v2/export';
export * from './helper/v2/export';
export * from './resource/export.v2';
export * from './Errors';
import * as extensions from './extensions/export';
export { extensions };
2 changes: 2 additions & 0 deletions lib/index.v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ __export(require("./user/v2/export"));
__export(require("./helper/v2/export"));
__export(require("./resource/export.v2"));
__export(require("./Errors"));
var extensions = require("./extensions/export");
exports.extensions = extensions;
28 changes: 28 additions & 0 deletions src/extensions/Express.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { WebDAVServer } from '../server/v2/export'
import { Path } from '../manager/v2/Path'

/**
* Mount a WebDAVServer instance on a ExpressJS server.
*
* @param root Root path of the mount
* @param server Server to mount
*/
export function express(root : string, server : WebDAVServer)
{
const path = new Path(root).toString(false);

const pathRegex = new RegExp('^' + path + '((\/[^\/]+)*)\/?$');

return function(req, res, next)
{
const matches = pathRegex.exec(req.url);
if(!matches)
return next();

const subUrl = matches[1];

req.url = new Path(subUrl).toString(false);

server.executeRequest(req, res, path);
};
}
2 changes: 2 additions & 0 deletions src/extensions/export.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

export * from './Express'
3 changes: 3 additions & 0 deletions src/index.v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ export * from './user/v2/export';
export * from './helper/v2/export';
export * from './resource/export.v2';
export * from './Errors';

import * as extensions from './extensions/export'
export { extensions }

0 comments on commit 9114666

Please sign in to comment.