mirror of
https://github.com/intergalacticalvariable/reader.git
synced 2024-11-16 11:42:32 +08:00
fix: Improve header handling in CrawlerOptions.from()
This commit is contained in:
parent
c77135490b
commit
f6ee7ca6e5
|
@ -190,21 +190,29 @@ export class CrawlerOptions extends AutoCastable {
|
|||
|
||||
static override from(input: any) {
|
||||
const instance = super.from(input) as CrawlerOptions;
|
||||
const ctx = Reflect.get(input, RPC_CALL_ENVIRONMENT) as {
|
||||
req: Request,
|
||||
res: Response,
|
||||
} | undefined;
|
||||
const ctx = Reflect.get(input, RPC_CALL_ENVIRONMENT);
|
||||
console.log('RPC_CALL_ENVIRONMENT:', ctx);
|
||||
|
||||
console.log(`ctx: ${JSON.stringify(ctx)}`);
|
||||
const customMode = ctx?.req.get('x-respond-with') || ctx?.req.get('x-return-format');
|
||||
console.log(`customMode: ${customMode}`);
|
||||
if (customMode !== undefined) {
|
||||
if (ctx && typeof ctx === 'object' && 'req' in ctx && 'res' in ctx) {
|
||||
const typedCtx = ctx as { req: Request, res: Response };
|
||||
console.log('Request headers:', typedCtx.req.headers);
|
||||
|
||||
const getHeader = (name: string): string | undefined => {
|
||||
const value = typedCtx.req.headers[name.toLowerCase()];
|
||||
return Array.isArray(value) ? value[0] : value;
|
||||
};
|
||||
|
||||
const customMode = getHeader('X-Respond-With') || getHeader('X-Return-Format');
|
||||
if (customMode) {
|
||||
instance.respondWith = customMode;
|
||||
}
|
||||
|
||||
const withGeneratedAlt = ctx?.req.get('x-with-generated-alt');
|
||||
const withGeneratedAlt = getHeader('X-With-Generated-Alt');
|
||||
if (withGeneratedAlt !== undefined) {
|
||||
instance.withGeneratedAlt = Boolean(withGeneratedAlt);
|
||||
instance.withGeneratedAlt = withGeneratedAlt.toLowerCase() === 'true';
|
||||
}
|
||||
} else {
|
||||
console.warn('Invalid or missing RPC_CALL_ENVIRONMENT');
|
||||
}
|
||||
const withLinksSummary = ctx?.req.get('x-with-links-summary');
|
||||
if (withLinksSummary !== undefined) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user