Skip to content

Commit

Permalink
feat: add common headers to requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Snazzah committed Dec 9, 2024
1 parent a609015 commit a122a36
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/search/images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { decode } from 'html-entities';
import needle, { NeedleOptions } from 'needle';

import { DuckbarImageResult, DuckbarResponse } from '../types';
import { ensureJSON, getVQD, queryString, SafeSearchType } from '../util';
import { COMMON_HEADERS, ensureJSON, getVQD, queryString, SafeSearchType } from '../util';

/** The types of image sizes. */
export enum ImageSize {
Expand Down Expand Up @@ -172,7 +172,7 @@ export async function searchImages(query: string, options?: ImageSearchOptions,
s: String(options.offset || 0)
};

const response = await needle('get', `https://duckduckgo.com/i.js?${queryString(queryObject)}`, needleOptions);
const response = await needle('get', `https://duckduckgo.com/i.js?${queryString(queryObject)}`, needleOptions || { headers: COMMON_HEADERS });

if (response.statusCode === 403) throw new Error('A server error occurred!');

Expand Down
4 changes: 2 additions & 2 deletions src/search/news.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { decode } from 'html-entities';
import needle, { NeedleOptions } from 'needle';

import { DuckbarNewsResult, DuckbarResponse } from '../types';
import { ensureJSON, getVQD, queryString, SafeSearchType, SearchTimeType } from '../util';
import { COMMON_HEADERS, ensureJSON, getVQD, queryString, SafeSearchType, SearchTimeType } from '../util';

/** The options for {@link searchNews}. */
export interface NewsSearchOptions {
Expand Down Expand Up @@ -84,7 +84,7 @@ export async function searchNews(query: string, options?: NewsSearchOptions, nee
s: String(options.offset || 0)
};

const response = await needle('get', `https://duckduckgo.com/news.js?${queryString(queryObject)}`, needleOptions);
const response = await needle('get', `https://duckduckgo.com/news.js?${queryString(queryObject)}`, needleOptions || { headers: COMMON_HEADERS });

if (response.statusCode === 403) throw new Error('A server error occurred!');

Expand Down
4 changes: 2 additions & 2 deletions src/search/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
DuckbarRelatedSearch,
DuckbarVideoResult
} from '../types';
import { ensureJSON, getVQD, queryString, SafeSearchType, SearchTimeType } from '../util';
import { COMMON_HEADERS, ensureJSON, getVQD, queryString, SafeSearchType, SearchTimeType } from '../util';
import { NewsResult } from './news';
import { VideoResult } from './videos';

Expand Down Expand Up @@ -121,7 +121,7 @@ export async function search(query: string, options?: SearchOptions, needleOptio
else options = sanityCheck(options);

let vqd = options.vqd!;
if (!vqd) vqd = await getVQD(query, 'web', needleOptions);
if (!vqd) vqd = await getVQD(query, 'web', needleOptions || { headers: COMMON_HEADERS });

/* istanbul ignore next */
const queryObject: Record<string, string> = {
Expand Down
4 changes: 2 additions & 2 deletions src/search/videos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { decode } from 'html-entities';
import needle, { NeedleOptions } from 'needle';

import { DuckbarResponse, DuckbarVideoResult } from '../types';
import { ensureJSON, getVQD, queryString, SafeSearchType, SearchTimeType } from '../util';
import { COMMON_HEADERS, ensureJSON, getVQD, queryString, SafeSearchType, SearchTimeType } from '../util';

/** The types of video definition. */
export enum VideoDefinition {
Expand Down Expand Up @@ -127,7 +127,7 @@ export async function searchVideos(query: string, options?: VideoSearchOptions,
s: String(options.offset || 0)
};

const response = await needle('get', `https://duckduckgo.com/v.js?${queryString(queryObject)}`, needleOptions);
const response = await needle('get', `https://duckduckgo.com/v.js?${queryString(queryObject)}`, needleOptions || { headers: COMMON_HEADERS });

if (response.statusCode === 403) throw new Error('A server error occurred!');

Expand Down
13 changes: 13 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ import needle, { NeedleOptions } from 'needle';
export const SPICE_BASE = 'https://duckduckgo.com/js/spice';
/** @internal */
export const VQD_REGEX = /vqd=['"](\d+-\d+(?:-\d+)?)['"]/;
/** @internal */
export const COMMON_HEADERS: Record<string, string> = {
'sec-ch-ua': '"Not=A?Brand";v="8", "Chromium";v="129"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'document',
'sec-fetch-mode': 'navigate',
'sec-fetch-site': 'none',
'sec-fetch-user': '?1',
'sec-gpc': '1',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36'
};

/** The safe search values when searching DuckDuckGo. */
export enum SafeSearchType {
Expand Down

0 comments on commit a122a36

Please sign in to comment.