@gracile/engine
Advanced tools
Comparing version 0.2.2-next.1 to 0.2.2-next.2
@@ -12,2 +12,3 @@ import { type ServerRenderedTemplate } from '@lit-labs/ssr'; | ||
export declare function isLitServerTemplate(input: unknown): input is ServerRenderedTemplate; | ||
export declare function isResponseOrPatchedResponse(input: unknown): input is Response; | ||
//# sourceMappingURL=assertions.d.ts.map |
@@ -28,1 +28,19 @@ import {} from '@lit-labs/ssr'; | ||
} | ||
export function isResponseOrPatchedResponse(input) { | ||
if (input instanceof Response) | ||
return true; | ||
// NOTE: We have to use that because Hono breaks with `Response.json` | ||
// (IDK why, maybe some global patching somewhere, | ||
// not found in the Hono codebase). | ||
if (input && | ||
typeof input === 'object' && | ||
input.constructor.name === 'Response' && | ||
'url' in input && | ||
'body' in input && | ||
'bodyUsed' in input && | ||
'headers' in input && | ||
'status' in input && | ||
'statusText' in input) | ||
return true; | ||
return false; | ||
} |
@@ -6,3 +6,3 @@ /* eslint-disable @typescript-eslint/no-floating-promises */ | ||
import { html } from 'lit'; | ||
import { isLitNormalTemplate, isLitServerTemplate, isLitTemplate, isUnknownObject, } from './assertions.js'; | ||
import { isLitNormalTemplate, isLitServerTemplate, isLitTemplate, isResponseOrPatchedResponse, isUnknownObject, } from './assertions.js'; | ||
describe('should assert lit templates, unknown objects', () => { | ||
@@ -26,2 +26,4 @@ const lit = html ` <div>Hello</div> `; | ||
}); | ||
}); | ||
describe('should assert unknown objects', () => { | ||
test('unknown object', () => { | ||
@@ -32,1 +34,18 @@ assert.equal(isUnknownObject({ something: 'something' }), true); | ||
}); | ||
describe('should assert responses', () => { | ||
test('assert standard response', () => { | ||
assert.equal(isResponseOrPatchedResponse(new Response('hey', { headers: { foo: 'bar' } })), true); | ||
}); | ||
test('assert patched standard response', () => { | ||
assert.equal(isResponseOrPatchedResponse( | ||
// eslint-disable-next-line @typescript-eslint/no-extraneous-class | ||
new (class Response { | ||
url = null; | ||
body = null; | ||
bodyUsed = null; | ||
headers = null; | ||
status = null; | ||
statusText = null; | ||
})()), true); | ||
}); | ||
}); |
@@ -8,4 +8,7 @@ // import { logger } from '@gracile/internal-utils/logger.build'; | ||
const result = await handler(context.req.raw, context.var); | ||
if (result?.body) | ||
return new Response(Readable.toWeb(result.body), result.init); | ||
if (result?.body) { | ||
// NOTE: Typings mismatches | ||
const body = Readable.toWeb(result.body); | ||
return new Response(body, result.init); | ||
} | ||
if (result?.response) | ||
@@ -12,0 +15,0 @@ return result.response; |
import { Readable } from 'node:stream'; | ||
import { logger } from '@gracile/internal-utils/logger'; | ||
import c from 'picocolors'; | ||
import { isUnknownObject } from '../assertions.js'; | ||
import * as assert from '../assertions.js'; | ||
import { errorPage } from '../errors/templates.js'; | ||
@@ -16,16 +16,8 @@ import { renderRouteTemplate } from '../render/route-template.js'; | ||
errorPageHtml = await vite.transformIndexHtml(urlPath, errorPageHtml); | ||
console.log({ errorPageHtml }); | ||
return { errorPageHtml, headers: { ...CONTENT_TYPE_HTML } }; | ||
} | ||
const middleware = async (request, locals) => { | ||
// HACK: Typing workaround | ||
if (!request.url) | ||
throw Error('Incorrect url'); | ||
if (!request.method) | ||
throw Error('Incorrect method'); | ||
const { url: urlPath, method } = request; | ||
// if (urlPath === '/favicon.ico') return next(); | ||
try { | ||
// NOTE: Maybe it should be constructed from `req` | ||
const fullUrl = request.url; | ||
const { url: fullUrl, method } = request; | ||
// MARK: Get route infos | ||
@@ -48,4 +40,4 @@ const routeOptions = { | ||
// MARK: Default, fallback 404 | ||
const message = `404 not found!\n\n---\n\nCreate a /src/routes/404.{js,ts} to get a custom page.\n${method} - ${urlPath}`; | ||
const { errorPageHtml, headers } = await createErrorPage(urlPath, new Error(message)); | ||
const message = `404 not found!\n\n---\n\nCreate a /src/routes/404.{js,ts} to get a custom page.\n${method} - ${fullUrl}`; | ||
const { errorPageHtml, headers } = await createErrorPage(fullUrl, new Error(message)); | ||
return { | ||
@@ -68,3 +60,3 @@ response: new Response(errorPageHtml, { | ||
}; | ||
logger.info(`[${c.yellow(method)}] ${c.yellow(urlPath)}`, { | ||
logger.info(`[${c.yellow(method)}] ${c.yellow(fullUrl)}`, { | ||
timestamp: true, | ||
@@ -74,3 +66,3 @@ }); | ||
let providedLocals = {}; | ||
if (locals && isUnknownObject(locals)) | ||
if (locals && assert.isUnknownObject(locals)) | ||
providedLocals = locals; | ||
@@ -116,3 +108,3 @@ // MARK: Server handler | ||
const handlerOutput = (await Promise.resolve(handler(routeContext))); | ||
if (handlerOutput instanceof Response) | ||
if (assert.isResponseOrPatchedResponse(handlerOutput)) | ||
output = handlerOutput; | ||
@@ -128,3 +120,3 @@ else | ||
const handlerOutput = await Promise.resolve(handlerWithMethod(routeContext)); | ||
if (handlerOutput instanceof Response) | ||
if (assert.isResponseOrPatchedResponse(handlerOutput)) | ||
output = handlerOutput; | ||
@@ -156,3 +148,3 @@ else { | ||
// NOTE: try directly with the requestPonyfill. This might not be necessary | ||
if (output instanceof Response) { | ||
if (assert.isResponseOrPatchedResponse(output)) { | ||
if (output.status >= 300 && output.status <= 303) { | ||
@@ -159,0 +151,0 @@ const location = output.headers.get('location'); |
{ | ||
"name": "@gracile/engine", | ||
"version": "0.2.2-next.1", | ||
"version": "0.2.2-next.2", | ||
"description": "A thin, full-stack, web framework", | ||
@@ -78,3 +78,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "8bcbeaa8c82b22003a1f227133a5a2f04589fe8a" | ||
"gitHead": "e11a9ac9b4c1441c89698f1ede7e639295842bd9" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
140206
1859