@nattyjs/core 0.0.1-beta.65 → 0.0.1-beta.66

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -211,8 +211,10 @@ function setupLegacyTypes(types) {
211
211
  function defaultResolver(path) {
212
212
  if (typeof path === "function")
213
213
  return path();
214
- if (typeof path === "string")
215
- return require(path);
214
+ if (typeof path === "string") {
215
+ const normalizedPath = path.startsWith("/") ? `.${path}` : path;
216
+ return require(normalizedPath);
217
+ }
216
218
  return {};
217
219
  }
218
220
  function initializeModule(config) {
@@ -562,6 +564,15 @@ class BaseResponse {
562
564
  notFound() {
563
565
  throw new HttpNotFoundException({});
564
566
  }
567
+ normalizeHttpResponse(result) {
568
+ if (result instanceof HttpResponse)
569
+ return result;
570
+ if (result instanceof HttpException)
571
+ return result.getResponse();
572
+ if (result && typeof result === "object" && ("status" in result || "body" in result || "headers" in result || "cookies" in result || "isBuffer" in result))
573
+ return new HttpResponse(result);
574
+ return result;
575
+ }
565
576
  }
566
577
 
567
578
  function getTypedErrorMessage(type, value) {
@@ -935,11 +946,19 @@ class RouteParser extends ParameterTypeConverter {
935
946
  return this.parsedRequestUrl;
936
947
  }
937
948
  getRequestPathname(url) {
938
- const apiRootPath = common.commonContainer.nattyConfig.api.rootPath;
939
- let splitUrl = url.pathname.split(`${apiRootPath}/`);
940
- if (splitUrl.length > 1)
941
- return splitUrl[1];
942
- return url.pathname;
949
+ const apiRootPath = String(common.commonContainer.nattyConfig.api.rootPath || "").replace(/^\/+|\/+$/g, "");
950
+ const normalizedPathname = url.pathname.replace(/^\/+/, "");
951
+ if (!apiRootPath) {
952
+ return normalizedPathname;
953
+ }
954
+ if (normalizedPathname === apiRootPath) {
955
+ return "";
956
+ }
957
+ const apiPrefix = `${apiRootPath}/`;
958
+ if (normalizedPathname.startsWith(apiPrefix)) {
959
+ return normalizedPathname.slice(apiPrefix.length);
960
+ }
961
+ return normalizedPathname;
943
962
  }
944
963
  getQueryParams(url) {
945
964
  const queryParams = {};
@@ -1573,11 +1592,11 @@ class Resolver extends RequestProcessor {
1573
1592
  if (onException) {
1574
1593
  const instance = this.resolveClass(onException);
1575
1594
  if (instance.onException)
1576
- result = instance.onException({
1595
+ result = this.normalizeHttpResponse(instance.onException({
1577
1596
  error: ex,
1578
1597
  request: this.httpContext.request,
1579
1598
  routeInfo: this.routeInfo
1580
- });
1599
+ }));
1581
1600
  } else
1582
1601
  result = new HttpException({
1583
1602
  body: { message: ex.message, stack: ex.stack },
@@ -1608,11 +1627,11 @@ class RequestHandler extends Resolver {
1608
1627
  if (onException) {
1609
1628
  const instance = this.resolveClass(onException);
1610
1629
  if (instance.onException)
1611
- return instance.onException({
1630
+ return this.normalizeHttpResponse(instance.onException({
1612
1631
  error: ex,
1613
1632
  request: this.httpContext.request,
1614
1633
  routeInfo: this.routeInfo
1615
- });
1634
+ }));
1616
1635
  } else
1617
1636
  return new HttpException({
1618
1637
  body: {
package/dist/index.d.ts CHANGED
@@ -402,6 +402,7 @@ declare abstract class BaseResponse {
402
402
  badRequest(): void;
403
403
  success(body: any): HttpResponse;
404
404
  notFound(): void;
405
+ protected normalizeHttpResponse(result: any): any;
405
406
  }
406
407
 
407
408
  declare abstract class ParameterTypeConverter extends BaseResponse {
package/dist/index.mjs CHANGED
@@ -205,8 +205,10 @@ function setupLegacyTypes(types) {
205
205
  function defaultResolver(path) {
206
206
  if (typeof path === "function")
207
207
  return path();
208
- if (typeof path === "string")
209
- return require(path);
208
+ if (typeof path === "string") {
209
+ const normalizedPath = path.startsWith("/") ? `.${path}` : path;
210
+ return require(normalizedPath);
211
+ }
210
212
  return {};
211
213
  }
212
214
  function initializeModule(config) {
@@ -556,6 +558,15 @@ class BaseResponse {
556
558
  notFound() {
557
559
  throw new HttpNotFoundException({});
558
560
  }
561
+ normalizeHttpResponse(result) {
562
+ if (result instanceof HttpResponse)
563
+ return result;
564
+ if (result instanceof HttpException)
565
+ return result.getResponse();
566
+ if (result && typeof result === "object" && ("status" in result || "body" in result || "headers" in result || "cookies" in result || "isBuffer" in result))
567
+ return new HttpResponse(result);
568
+ return result;
569
+ }
559
570
  }
560
571
 
561
572
  function getTypedErrorMessage(type, value) {
@@ -929,11 +940,19 @@ class RouteParser extends ParameterTypeConverter {
929
940
  return this.parsedRequestUrl;
930
941
  }
931
942
  getRequestPathname(url) {
932
- const apiRootPath = commonContainer.nattyConfig.api.rootPath;
933
- let splitUrl = url.pathname.split(`${apiRootPath}/`);
934
- if (splitUrl.length > 1)
935
- return splitUrl[1];
936
- return url.pathname;
943
+ const apiRootPath = String(commonContainer.nattyConfig.api.rootPath || "").replace(/^\/+|\/+$/g, "");
944
+ const normalizedPathname = url.pathname.replace(/^\/+/, "");
945
+ if (!apiRootPath) {
946
+ return normalizedPathname;
947
+ }
948
+ if (normalizedPathname === apiRootPath) {
949
+ return "";
950
+ }
951
+ const apiPrefix = `${apiRootPath}/`;
952
+ if (normalizedPathname.startsWith(apiPrefix)) {
953
+ return normalizedPathname.slice(apiPrefix.length);
954
+ }
955
+ return normalizedPathname;
937
956
  }
938
957
  getQueryParams(url) {
939
958
  const queryParams = {};
@@ -1567,11 +1586,11 @@ class Resolver extends RequestProcessor {
1567
1586
  if (onException) {
1568
1587
  const instance = this.resolveClass(onException);
1569
1588
  if (instance.onException)
1570
- result = instance.onException({
1589
+ result = this.normalizeHttpResponse(instance.onException({
1571
1590
  error: ex,
1572
1591
  request: this.httpContext.request,
1573
1592
  routeInfo: this.routeInfo
1574
- });
1593
+ }));
1575
1594
  } else
1576
1595
  result = new HttpException({
1577
1596
  body: { message: ex.message, stack: ex.stack },
@@ -1602,11 +1621,11 @@ class RequestHandler extends Resolver {
1602
1621
  if (onException) {
1603
1622
  const instance = this.resolveClass(onException);
1604
1623
  if (instance.onException)
1605
- return instance.onException({
1624
+ return this.normalizeHttpResponse(instance.onException({
1606
1625
  error: ex,
1607
1626
  request: this.httpContext.request,
1608
1627
  routeInfo: this.routeInfo
1609
- });
1628
+ }));
1610
1629
  } else
1611
1630
  return new HttpException({
1612
1631
  body: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nattyjs/core",
3
- "version": "0.0.1-beta.65",
3
+ "version": "0.0.1-beta.66",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "author": "ajayojha",
@@ -17,7 +17,7 @@
17
17
  "dependencies": {
18
18
  "reflect-metadata": "0.2.2",
19
19
  "path-to-regexp": "6.2.1",
20
- "@nattyjs/common": "0.0.1-beta.65"
20
+ "@nattyjs/common": "0.0.1-beta.66"
21
21
  },
22
22
  "devDependencies": {
23
23
  "unbuild": "1.2.1"