I'm trying to use an existing js library (validate.js) in both the client and server.
I installed it using npm, and everything compiles for both server and client.
When using it in the server it works great, but when I execute it in the browser it throws an error.
The same file is used in both cases:
import validate = require("validate.js");
export function RequestValidator(data: any): any {
return (validate as any)(data, constraints, { allowEmpty: true });
}
validate is asserted to any becuase otherwise I get:
TS2349: Cannot invoke an expression whose type lacks a call signature.
The .d.ts I'm using is:
declare module "validate.js" {
export interface ValidateJS {
(attributes: any, constraints: any, options?: any): any;
async(attributes: any, constraints: any, options?: any): Promise<any>;
single(value: any, constraints: any, options?: any): any;
}
export const validate: ValidateJS;
export default validate;
}
The module only exports a function, and that works well in the server, but in the client when invoking this function I get:
Uncaught TypeError: validate is not a function(…)
The code is compiled using target commonjs for the server:
"use strict";
const validate = require("validate.js");
...
And system for the client:
System.register(["validate.js"], function(exports_1, context_1) {
"use strict";
var __moduleName = context_1 && context_1.id;
var validate;
...
return {
setters:[
function (validate_1) {
validate = validate_1;
}],
...
When debugging it, validate indeed isn't a function it's:
validate: r
EMPTY_STRING_REGEXP: (...)
get EMPTY_STRING_REGEXP: function()
set EMPTY_STRING_REGEXP: function()
Promise: (...)
get Promise: function()
set Promise: function()
__useDefault: (...)
get __useDefault: function()
set __useDefault: function()
async: (...)
get async: function()
set async: function()
capitalize: (...)
get capitalize: function()
set capitalize: function()
cleanAttributes: (...)
get cleanAttributes: function()
set cleanAttributes: function()
...
Any idea what's going on and why it behaves this way in the browser?
@nodehowever I have no personal experience with this kind of black magic.jspmas well?