Skip to content

Commit

Permalink
fix(json-mapper): make package compatible with browser context
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Dec 27, 2022
1 parent 00af1ad commit 975eb10
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 54 deletions.
1 change: 0 additions & 1 deletion packages/di/src/domain/ContextLogger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {Hooks} from "@tsed/core";
import {levels, LogLevel} from "@tsed/logger";
import {DILogger} from "../interfaces/DILogger";
import type {DIContext} from "./DIContext";

export interface ContextLoggerOptions extends Record<string, any> {
id: string;
Expand Down
72 changes: 36 additions & 36 deletions packages/platform/platform-express/test/platform-express.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,42 @@ const utils = PlatformTestUtils.create({
});

describe("PlatformExpress", () => {
// describe("Handlers", () => {
// utils.test("handlers");
// });
// describe("Children controllers", () => {
// utils.test("childrenControllers");
// });
// describe("Inheritance controllers", () => {
// utils.test("inheritanceController");
// });
// describe("Middlewares", () => {
// utils.test("middlewares");
// });
// describe("Scope Request", () => {
// utils.test("scopeRequest");
// });
// describe("Headers", () => {
// utils.test("headers");
// });
// describe("Statics files", () => {
// utils.test("statics");
// });
// describe("Cookies", () => {
// utils.test("cookies");
// });
// describe("Session", () => {
// utils.test("session");
// });
// describe("AcceptMime", () => {
// utils.test("acceptMime");
// });
// describe("HeaderParams", () => {
// utils.test("headerParams");
// });
// describe("PathParams", () => {
// utils.test("pathParams");
// });
describe("Handlers", () => {
utils.test("handlers");
});
describe("Children controllers", () => {
utils.test("childrenControllers");
});
describe("Inheritance controllers", () => {
utils.test("inheritanceController");
});
describe("Middlewares", () => {
utils.test("middlewares");
});
describe("Scope Request", () => {
utils.test("scopeRequest");
});
describe("Headers", () => {
utils.test("headers");
});
describe("Statics files", () => {
utils.test("statics");
});
describe("Cookies", () => {
utils.test("cookies");
});
describe("Session", () => {
utils.test("session");
});
describe("AcceptMime", () => {
utils.test("acceptMime");
});
describe("HeaderParams", () => {
utils.test("headerParams");
});
describe("PathParams", () => {
utils.test("pathParams");
});
describe("QueryParams", () => {
utils.test("queryParams");
});
Expand Down
11 changes: 7 additions & 4 deletions packages/specs/json-mapper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@
"main": "./lib/cjs/index.js",
"module": "./lib/esm/index.js",
"typings": "./lib/types/index.d.ts",
"browser": "./lib/browser/json-mapper.umd.min.js",
"exports": {
"types": "./lib/types/index.d.ts",
"browser": "./lib/browser/json-mapper.umd.min.js",
"import": "./lib/esm/index.js",
"require": "./lib/cjs/index.js",
"default": "./lib/esm/index.js"
},
"scripts": {
"build": "yarn barrels && yarn run build:esm && yarn run build:cjs",
"clean": "rm -rf lib",
"build": "yarn clean && yarn barrels && yarn run build:esm && yarn run build:cjs && yarn run build:browser",
"build:cjs": "tsc --build tsconfig.compile.json",
"build:esm": "tsc --build tsconfig.compile.esm.json",
"build:browser": "webpack",
"barrels": "yarn barrelsby --delete -d ./src -e \"\\.spec\\.ts\" -e \"__mock__\" -e \".benchmark.ts\"",
"test": "cross-env NODE_ENV=test yarn jest --max-workers=2"
},
Expand All @@ -37,12 +41,10 @@
},
"devDependencies": {
"@tsed/core": "7.10.0-rc.6",
"@tsed/exceptions": "7.10.0-rc.6",
"@tsed/schema": "7.10.0-rc.6"
},
"peerDependencies": {
"@tsed/core": "^7.10.0-rc.6",
"@tsed/exceptions": "^7.10.0-rc.6",
"@tsed/schema": "^7.10.0-rc.6"
},
"peerDependenciesMeta": {
Expand All @@ -56,4 +58,5 @@
"optional": false
}
}
}
}

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {catchError} from "@tsed/core";
import Sinon from "sinon";
import {PrimitiveMapper} from "./PrimitiveMapper";

Expand Down Expand Up @@ -90,12 +91,7 @@ describe("PrimitiveMapper", () => {
next: Sinon.stub()
};

let actualError: any;
try {
mapper.deserialize(data, ctx);
} catch (er) {
actualError = er;
}
let actualError: any = catchError(() => mapper.deserialize(data, ctx));

expect(actualError.message).toEqual("Cast error. Expression value is not a number.");
});
Expand Down
11 changes: 9 additions & 2 deletions packages/specs/json-mapper/src/components/PrimitiveMapper.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import {BadRequest} from "@tsed/exceptions";
import {JsonMapper} from "../decorators/jsonMapper";
import {JsonMapperCtx, JsonMapperMethods} from "../interfaces/JsonMapperMethods";

function isNullish(data: any) {
return [null, "null"].includes(data);
}

export class CastError extends Error {
name = "CAST_ERROR";

constructor(message: string) {
super(`Cast error. ${message}`);
}
}

/**
* Mapper for the `String`, `Number`, `BigInt` and `Boolean` types.
* @jsonmapper
Expand All @@ -29,7 +36,7 @@ export class PrimitiveMapper implements JsonMapperMethods {
const n = +data;

if (isNaN(n)) {
throw new BadRequest("Cast error. Expression value is not a number.");
throw new CastError("Expression value is not a number.");
}

return n;
Expand Down
14 changes: 13 additions & 1 deletion packages/specs/json-mapper/tsconfig.compile.esm.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,17 @@
"declaration": true,
"declarationDir": "./lib/types"
},
"exclude": ["node_modules", "test", "lib", "benchmark", "coverage", "spec", "**/*.benchmark.ts", "**/*.spec.ts", "keys", "jest.config.js"]
"exclude": [
"node_modules",
"test",
"lib",
"benchmark",
"coverage",
"spec",
"**/*.benchmark.ts",
"**/*.spec.ts",
"keys",
"jest.config.js",
"webpack.config.js"
]
}
14 changes: 13 additions & 1 deletion packages/specs/json-mapper/tsconfig.compile.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,17 @@
"outDir": "lib/cjs",
"declaration": false
},
"exclude": ["node_modules", "test", "lib", "benchmark", "coverage", "spec", "**/*.benchmark.ts", "**/*.spec.ts", "keys", "jest.config.js"]
"exclude": [
"node_modules",
"test",
"lib",
"benchmark",
"coverage",
"spec",
"**/*.benchmark.ts",
"**/*.spec.ts",
"keys",
"jest.config.js",
"webpack.config.js"
]
}
15 changes: 15 additions & 0 deletions packages/specs/json-mapper/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const webpack = require("webpack");

module.exports = require("@tsed/webpack-config").create({
root: __dirname,
name: "json-mapper",
externals: {
"@tsed/core": "@tsed/core",
"@tsed/schema": "@tsed/schema"
},
resolve: {
alias: {
picomatch: require.resolve("picomatch-browser/posix")
}
}
});
5 changes: 2 additions & 3 deletions packages/specs/schema/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const webpack = require("webpack");

module.exports = require("@tsed/webpack-config").create({
root: __dirname,
name: "schema",
externals: {
"@tsed/core": "@tsed/core"
"@tsed/core": "@tsed/core",
"change-case": "change-case"
},
resolve: {
alias: {
Expand Down

0 comments on commit 975eb10

Please sign in to comment.