@ruiapp/rapid-core 0.5.1 → 0.5.2

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.js CHANGED
@@ -999,7 +999,6 @@ function mergeHeaders(target, source) {
999
999
  else if (lodash.isObject(source)) {
1000
1000
  Object.entries(source).forEach(([key, value]) => target.set(key, value));
1001
1001
  }
1002
- return target;
1003
1002
  }
1004
1003
  function newResponse(options) {
1005
1004
  return new Response(options.body, {
@@ -1008,6 +1007,7 @@ function newResponse(options) {
1008
1007
  });
1009
1008
  }
1010
1009
  class RapidResponse {
1010
+ // TODO: remove this field.
1011
1011
  #response;
1012
1012
  status;
1013
1013
  body;
@@ -1027,13 +1027,16 @@ class RapidResponse {
1027
1027
  if (headers) {
1028
1028
  mergeHeaders(responseHeaders, headers);
1029
1029
  }
1030
- this.#response = newResponse({ body, status: status || 200, headers: responseHeaders });
1030
+ this.status = status || 200;
1031
+ this.body = body;
1032
+ this.#response = newResponse({ body, status: this.status, headers: responseHeaders });
1031
1033
  }
1032
1034
  redirect(location, status) {
1033
1035
  this.headers.set("Location", location);
1036
+ this.status = status || 302;
1034
1037
  this.#response = newResponse({
1035
1038
  headers: this.headers,
1036
- status: status || 302,
1039
+ status: this.status,
1037
1040
  });
1038
1041
  }
1039
1042
  getResponse() {
@@ -4161,19 +4164,27 @@ class RapidServer {
4161
4164
  const rapidRequest = new RapidRequest(this, request);
4162
4165
  await rapidRequest.parseBody();
4163
4166
  const routeContext = new RouteContext(this, rapidRequest);
4167
+ const { response } = routeContext;
4164
4168
  try {
4165
4169
  await this.#pluginManager.onPrepareRouteContext(routeContext);
4166
4170
  await this.#buildedRoutes(routeContext, next);
4167
4171
  }
4168
4172
  catch (ex) {
4169
4173
  this.#logger.error("handle request error:", ex);
4170
- routeContext.response.json({
4174
+ response.json({
4171
4175
  error: {
4172
4176
  message: ex.message || ex,
4173
4177
  },
4174
4178
  }, 500);
4175
4179
  }
4176
- return routeContext.response.getResponse();
4180
+ if (!response.status && !response.body) {
4181
+ response.json({
4182
+ error: {
4183
+ message: "No route handler was found to handle this request.",
4184
+ },
4185
+ }, 404);
4186
+ }
4187
+ return response.getResponse();
4177
4188
  }
4178
4189
  async beforeRunRouteActions(handlerContext) {
4179
4190
  await this.#pluginManager.beforeRunRouteActions(handlerContext);
@@ -4989,7 +5000,13 @@ async function handler$q(plugin, ctx, options) {
4989
5000
  routeContext,
4990
5001
  });
4991
5002
  if (!entity) {
4992
- throw new Error(`${options.namespace}.${options.singularCode} with id "${id}" was not found.`);
5003
+ ctx.routerContext.response.json({
5004
+ error: {
5005
+ message: `${options.namespace}.${options.singularCode} with id "${id}" was not found.`,
5006
+ },
5007
+ }, 404);
5008
+ // routerContext.json() function will not be called if the ctx.output is null or undefined.
5009
+ return;
4993
5010
  }
4994
5011
  return entity;
4995
5012
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ruiapp/rapid-core",
3
- "version": "0.5.1",
3
+ "version": "0.5.2",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -15,7 +15,6 @@ function mergeHeaders(target: Headers, source: HeadersInit) {
15
15
  } else if (isObject(source)) {
16
16
  Object.entries(source).forEach(([key, value]) => target.set(key, value));
17
17
  }
18
- return target;
19
18
  }
20
19
 
21
20
  interface NewResponseOptions {
@@ -32,6 +31,7 @@ function newResponse(options: NewResponseOptions) {
32
31
  }
33
32
 
34
33
  export class RapidResponse {
34
+ // TODO: remove this field.
35
35
  #response: Response;
36
36
  status: number;
37
37
  body: BodyInit;
@@ -53,14 +53,17 @@ export class RapidResponse {
53
53
  if (headers) {
54
54
  mergeHeaders(responseHeaders, headers);
55
55
  }
56
- this.#response = newResponse({ body, status: status || 200, headers: responseHeaders });
56
+ this.status = status || 200;
57
+ this.body = body;
58
+ this.#response = newResponse({ body, status: this.status, headers: responseHeaders });
57
59
  }
58
60
 
59
61
  redirect(location: string, status?: HttpStatus) {
60
62
  this.headers.set("Location", location);
63
+ this.status = status || 302;
61
64
  this.#response = newResponse({
62
65
  headers: this.headers,
63
- status: status || 302,
66
+ status: this.status,
64
67
  });
65
68
  }
66
69
 
@@ -14,7 +14,16 @@ export async function handler(plugin: RapidPlugin, ctx: ActionHandlerContext, op
14
14
  routeContext,
15
15
  });
16
16
  if (!entity) {
17
- throw new Error(`${options.namespace}.${options.singularCode} with id "${id}" was not found.`);
17
+ ctx.routerContext.response.json(
18
+ {
19
+ error: {
20
+ message: `${options.namespace}.${options.singularCode} with id "${id}" was not found.`,
21
+ },
22
+ },
23
+ 404,
24
+ );
25
+ // routerContext.json() function will not be called if the ctx.output is null or undefined.
26
+ return;
18
27
  }
19
28
  return entity;
20
29
  });
package/src/server.ts CHANGED
@@ -400,13 +400,14 @@ export class RapidServer implements IRpdServer {
400
400
  const rapidRequest = new RapidRequest(this, request);
401
401
  await rapidRequest.parseBody();
402
402
  const routeContext: RouteContext = new RouteContext(this, rapidRequest);
403
+ const { response } = routeContext;
403
404
 
404
405
  try {
405
406
  await this.#pluginManager.onPrepareRouteContext(routeContext);
406
407
  await this.#buildedRoutes(routeContext, next);
407
408
  } catch (ex) {
408
409
  this.#logger.error("handle request error:", ex);
409
- routeContext.response.json(
410
+ response.json(
410
411
  {
411
412
  error: {
412
413
  message: ex.message || ex,
@@ -415,7 +416,18 @@ export class RapidServer implements IRpdServer {
415
416
  500,
416
417
  );
417
418
  }
418
- return routeContext.response.getResponse();
419
+
420
+ if (!response.status && !response.body) {
421
+ response.json(
422
+ {
423
+ error: {
424
+ message: "No route handler was found to handle this request.",
425
+ },
426
+ },
427
+ 404,
428
+ );
429
+ }
430
+ return response.getResponse();
419
431
  }
420
432
 
421
433
  async beforeRunRouteActions(handlerContext: ActionHandlerContext) {