fusion-framework 1.2.1 → 1.2.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.
Binary file
Binary file
Binary file
Binary file
package/index.js CHANGED
@@ -134,7 +134,7 @@ header.fingerprint = () =>
134
134
  : {
135
135
  'X-Powered-By': 'Fusion Framework',
136
136
  'X-Framework': 'Fusion',
137
- 'X-Fusion-Version': '1.2.1',
137
+ ['X-Fusion-Version']: '1.2.2',
138
138
  }
139
139
 
140
140
  function isThenable(value) {
@@ -170,7 +170,7 @@ function frameworkHeaders() {
170
170
 
171
171
  class FusionBaseApi {
172
172
  constructor(request) {
173
- this.request = request
173
+ this.request = request && typeof request === 'object' ? request : emptyRequest()
174
174
  }
175
175
 
176
176
  get method() {
@@ -219,7 +219,39 @@ function resolveRoutePath(routePath, ApiClass) {
219
219
  return native.resolveRoutePathJs(routePath, ApiClass.name)
220
220
  }
221
221
 
222
+ function emptyRequest() {
223
+ return {
224
+ method: '',
225
+ path: '',
226
+ body: '',
227
+ headers: {},
228
+ params: {},
229
+ query: {},
230
+ state: {},
231
+ }
232
+ }
233
+
234
+ /**
235
+ * N-API threadsafe callbacks may be `(request)` (Fatal) or `(err, request)` (CalleeHandled).
236
+ * Always return a real request object so middleware never sees `null`.
237
+ */
238
+ function nativeRequestArg(errOrRequest, maybeRequest) {
239
+ if (maybeRequest != null && typeof maybeRequest === 'object') {
240
+ return maybeRequest
241
+ }
242
+ if (errOrRequest instanceof Error) {
243
+ throw errOrRequest
244
+ }
245
+ if (errOrRequest != null && typeof errOrRequest === 'object') {
246
+ return errOrRequest
247
+ }
248
+ return emptyRequest()
249
+ }
250
+
222
251
  function ensureState(request) {
252
+ if (!request || typeof request !== 'object') {
253
+ return {}
254
+ }
223
255
  if (!request.state || typeof request.state !== 'object') {
224
256
  request.state = {}
225
257
  }
@@ -604,11 +636,12 @@ class FusionApp {
604
636
  for (const { path: routePath, ApiClass, middleware: routeMiddleware = [] } of registry) {
605
637
  for (const methodName of HTTP_METHODS) {
606
638
  if (!definesMethod(ApiClass, methodName)) continue
607
- this.engine.route(methodName.toUpperCase(), routePath, async (request) => {
639
+ this.engine.route(methodName.toUpperCase(), routePath, (errOrRequest, maybeRequest) => {
640
+ const request = nativeRequestArg(errOrRequest, maybeRequest)
608
641
  const chain = [...activeGlobalMiddleware, ...routeMiddleware]
609
642
  const handler = async (req) => {
610
643
  try {
611
- const instance = new ApiClass(req)
644
+ const instance = new ApiClass(req || emptyRequest())
612
645
  const fn = instance[methodName]
613
646
  return await Promise.resolve(fn.call(instance))
614
647
  } catch (err) {
@@ -673,12 +706,16 @@ class FusionApp {
673
706
  }
674
707
  }
675
708
 
676
- this.engine.route('GET', `${prefix}/openapi.json`, async () => openapi)
677
- this.engine.route('GET', prefix, async () => ({
709
+ const uiEnvelope = () => ({
678
710
  status: 200,
679
711
  body: swaggerUiHtml(swagger, `${prefix}/openapi.json`),
680
712
  headers: { 'content-type': 'text/html' },
681
- }))
713
+ })
714
+ this.engine.route('GET', `${prefix}/openapi.json`, () => openapi)
715
+ this.engine.route('GET', prefix, uiEnvelope)
716
+ if (prefix !== '/') {
717
+ this.engine.route('GET', `${prefix}/`, uiEnvelope)
718
+ }
682
719
  }
683
720
 
684
721
  this.mounted = true
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fusion-framework",
3
- "version": "1.2.1",
3
+ "version": "1.2.2",
4
4
  "description": "Class-based HTTP framework for Node.js, powered by a shared Rust core via N-API",
5
5
  "keywords": [
6
6
  "http",