From 812b5ea7bc03839cb19c59c58c003ca72cdd9c91 Mon Sep 17 00:00:00 2001 From: Matthew Herod Date: Sat, 4 Jan 2025 18:56:24 +0000 Subject: [PATCH] refactor: remove dynamic imports in favor of direct exports - Remove dynamicImports.ts and use direct exports in barrel files - Update example files to use direct import pattern - Update JSDoc examples to show correct import paths - Remove index.d.ts as it's no longer needed - Clean up tsconfig.json includes --- examples/advanced-usage.ts | 60 +++++------- examples/basic-usage.ts | 16 +--- src/core/cookies/dynamicImports.ts | 92 ------------------ src/core/cookies/getChromeCookie.ts | 4 +- src/core/cookies/getCookie.ts | 4 +- src/core/cookies/getFirefoxCookie.ts | 4 +- src/core/cookies/index.d.ts | 137 --------------------------- src/core/cookies/index.ts | 34 +++++++ src/index.ts | 23 ++++- tsconfig.json | 8 +- 10 files changed, 93 insertions(+), 289 deletions(-) delete mode 100644 src/core/cookies/dynamicImports.ts delete mode 100644 src/core/cookies/index.d.ts create mode 100644 src/core/cookies/index.ts diff --git a/examples/advanced-usage.ts b/examples/advanced-usage.ts index 2e8e198..ec3216b 100644 --- a/examples/advanced-usage.ts +++ b/examples/advanced-usage.ts @@ -5,24 +5,24 @@ /** * @description - * This example demonstrates advanced usage of the getCookie function. - * It shows how to retrieve cookies using browser-specific strategies - * and URL-based cookie retrieval. + * This example demonstrates advanced usage of the cookie retrieval functions. + * It shows how to retrieve cookies using different patterns and filters. * @internal * @example * ```typescript - * import { getCookie } from "../src"; + * import { getCookie } from "@mherod/get-cookie"; * - * // Get the cookie retrieval function - * const getCookieFn = await getCookie(); - * - * // Get a cookie by name and domain - * const cookie = await getCookieFn({ - * name: "session", + * // Get all cookies for a domain + * const cookies = await getCookie({ + * name: "*", * domain: "github.com" * }); * - * console.log(cookie); + * // Get specific cookies + * const authCookie = await getCookie({ + * name: "auth", + * domain: "api.github.com" + * }); * ``` */ @@ -30,43 +30,27 @@ import { getCookie } from "../src"; /** * @description - * Demonstrates browser-specific cookie retrieval examples. + * Run advanced examples of cookie retrieval. * @internal * @returns A promise that resolves when all examples have completed. */ -export async function browserSpecificExamples(): Promise { - // Get the cookie retrieval function - const getCookieFn = await getCookie(); - - // Get a cookie by name and domain - const cookie = await getCookieFn({ - name: "user_session", +export async function runAdvancedExamples(): Promise { + // Example 1: Get a specific cookie by name and domain + const cookie = await getCookie({ + name: "session", domain: "github.com", }); - console.log("Cookie:", cookie); -} - -/** - * @description - * Demonstrates URL-based cookie retrieval examples. - * @internal - * @returns A promise that resolves when all examples have completed. - */ -export async function urlBasedExamples(): Promise { - // Get the cookie retrieval function - const getCookieFn = await getCookie(); + console.log("Example 1 - Specific cookie:", cookie); - // Get a cookie by name and domain - const cookie = await getCookieFn({ - name: "user_session", + // Example 2: Get all cookies for a domain using wildcard + const cookies = await getCookie({ + name: "*", domain: "github.com", }); - console.log("Cookie:", cookie); + console.log("Example 2 - All domain cookies:", cookies); } // Execute the examples -Promise.all([browserSpecificExamples(), urlBasedExamples()]).catch( - console.error, -); +runAdvancedExamples().catch(console.error); diff --git a/examples/basic-usage.ts b/examples/basic-usage.ts index 2fe6f7f..761d93a 100644 --- a/examples/basic-usage.ts +++ b/examples/basic-usage.ts @@ -6,17 +6,14 @@ /** * @description * This example demonstrates basic usage of the getCookie function. - * It shows how to retrieve cookies from Chrome and Firefox browsers. + * It shows how to retrieve cookies from all supported browsers using a unified interface. * @internal * @example * ```typescript - * import { getCookie } from "../src"; - * - * // Get the cookie retrieval function - * const getCookieFn = await getCookie(); + * import { getCookie } from "@mherod/get-cookie"; * * // Get a cookie by name and domain - * const cookie = await getCookieFn({ + * const cookie = await getCookie({ * name: "session", * domain: "github.com" * }); @@ -34,12 +31,9 @@ import { getCookie } from "../src"; * @returns A promise that resolves when all examples have completed. */ export async function runBasicExamples(): Promise { - // Get the cookie retrieval function - const getCookieFn = await getCookie(); - // Get a cookie by name and domain - const cookie = await getCookieFn({ - name: "user_session", + const cookie = await getCookie({ + name: "session", domain: "github.com", }); diff --git a/src/core/cookies/dynamicImports.ts b/src/core/cookies/dynamicImports.ts deleted file mode 100644 index 0240e6a..0000000 --- a/src/core/cookies/dynamicImports.ts +++ /dev/null @@ -1,92 +0,0 @@ -import type { - RenderOptions, - CookieSpec, - ExportedCookie, -} from "../../types/schemas"; - -/** - * Dynamic import for the getCookie function. - * @internal - * @returns Promise resolving to the getCookie function - * @example - * ```typescript - * const getCookieFn = await getCookie(); - * const cookies = await getCookieFn({ domain: 'example.com' }); - * // Returns: [{ name: 'sessionId', value: 'abc123', domain: 'example.com' }, ...] - * ``` - */ -export const getCookie = (): Promise< - (cookieSpec: CookieSpec) => Promise -> => import("./getCookie").then((module) => module.getCookie); - -/** - * Dynamic import for Chrome-specific cookie retrieval. - * @internal - * @returns Promise resolving to the getChromeCookie function - * @example - * ```typescript - * const chromeCookieFn = await getChromeCookie(); - * const cookies = await chromeCookieFn({ domain: 'example.com', secure: true }); - * // Returns Chrome-format cookies: [{ name: 'auth', value: 'xyz789', secure: true }, ...] - * ``` - */ -export const getChromeCookie = (): Promise< - (cookieSpec: CookieSpec) => Promise -> => import("./getChromeCookie").then((module) => module.getChromeCookie); - -/** - * Dynamic import for Firefox-specific cookie retrieval. - * @internal - * @returns Promise resolving to the getFirefoxCookie function - * @example - * ```typescript - * const firefoxCookieFn = await getFirefoxCookie(); - * const cookies = await firefoxCookieFn({ path: '/api' }); - * // Returns Firefox-format cookies: [{ name: 'token', value: 'def456', path: '/api' }, ...] - * ``` - */ -export const getFirefoxCookie = (): Promise< - (cookieSpec: CookieSpec) => Promise -> => import("./getFirefoxCookie").then((module) => module.getFirefoxCookie); - -/** - * Dynamic import for retrieving grouped and rendered cookies. - * @internal - * @returns Promise resolving to the getGroupedRenderedCookies function - * @example - * ```typescript - * const groupedCookiesFn = await getGroupedRenderedCookies(); - * const cookieStrings = await groupedCookiesFn({ domain: 'example.com' }); - * // Returns: ['sessionId=abc123; Domain=example.com', 'auth=xyz789; Domain=example.com'] - * ``` - */ -export const getGroupedRenderedCookies = (): Promise< - (cookieSpec: CookieSpec) => Promise -> => - import("./getGroupedRenderedCookies").then( - (module) => module.getGroupedRenderedCookies, - ); - -/** - * Dynamic import for retrieving merged and rendered cookies. - * @internal - * @returns Promise resolving to the getMergedRenderedCookies function - * @example - * ```typescript - * const mergedCookiesFn = await getMergedRenderedCookies(); - * const cookieString = await mergedCookiesFn( - * { domain: 'example.com' }, - * { separator: '; ' } - * ); - * // Returns: "sessionId=abc123; auth=xyz789" - * ``` - */ -export const getMergedRenderedCookies = (): Promise< - ( - cookieSpec: CookieSpec, - options?: Omit, - ) => Promise -> => - import("./getMergedRenderedCookies").then( - (module) => module.getMergedRenderedCookies, - ); diff --git a/src/core/cookies/getChromeCookie.ts b/src/core/cookies/getChromeCookie.ts index 7b75af6..6c2ad6b 100644 --- a/src/core/cookies/getChromeCookie.ts +++ b/src/core/cookies/getChromeCookie.ts @@ -12,6 +12,8 @@ import { ChromeCookieQueryStrategy } from "../browsers/chrome/ChromeCookieQueryS * to the console without throwing to the caller * @example * ```typescript + * import { getChromeCookie } from "@mherod/get-cookie"; + * * // Get all cookies named "sessionId" from Chrome * const cookies = await getChromeCookie({ name: "sessionId" }); * @@ -42,7 +44,7 @@ export async function getChromeCookie( * Default export of the getChromeCookie function. * @example * ```typescript - * import getChromeCookie from './getChromeCookie'; + * import { getChromeCookie } from "@mherod/get-cookie"; * const cookies = await getChromeCookie({ * name: "sessionId", * domain: "example.com" diff --git a/src/core/cookies/getCookie.ts b/src/core/cookies/getCookie.ts index 28dceb4..5cbf465 100644 --- a/src/core/cookies/getCookie.ts +++ b/src/core/cookies/getCookie.ts @@ -14,6 +14,8 @@ import { queryCookies } from "./queryCookies"; * to the console without throwing to the caller * @example * ```typescript + * import { getCookie } from "@mherod/get-cookie"; + * * // Get all cookies named "sessionId" * const cookies = await getCookie({ name: "sessionId" }); * // Returns: [{ name: "sessionId", value: "abc123", domain: ".example.com", ... }] @@ -45,7 +47,7 @@ export async function getCookie( * Default export of the getCookie function. * @example * ```typescript - * import getCookie from './getCookie'; + * import { getCookie } from "@mherod/get-cookie"; * const authCookies = await getCookie({ * name: "auth-token", * domain: "api.example.com" diff --git a/src/core/cookies/getFirefoxCookie.ts b/src/core/cookies/getFirefoxCookie.ts index 131e669..0f4cdd5 100644 --- a/src/core/cookies/getFirefoxCookie.ts +++ b/src/core/cookies/getFirefoxCookie.ts @@ -12,6 +12,8 @@ import { FirefoxCookieQueryStrategy } from "../browsers/firefox/FirefoxCookieQue * to the console without throwing to the caller * @example * ```typescript + * import { getFirefoxCookie } from "@mherod/get-cookie"; + * * // Get all cookies named "sessionId" from Firefox * const cookies = await getFirefoxCookie({ name: "sessionId" }); * @@ -45,7 +47,7 @@ export async function getFirefoxCookie( * Default export of the getFirefoxCookie function. * @example * ```typescript - * import getFirefoxCookie from './getFirefoxCookie'; + * import { getFirefoxCookie } from "@mherod/get-cookie"; * const sessionCookies = await getFirefoxCookie({ * name: "PHPSESSID", * domain: "example.com" diff --git a/src/core/cookies/index.d.ts b/src/core/cookies/index.d.ts deleted file mode 100644 index 16cec6c..0000000 --- a/src/core/cookies/index.d.ts +++ /dev/null @@ -1,137 +0,0 @@ -import type { CookieSpec } from "../../types/CookieSpec"; -import type { ExportedCookie } from "../../types/ExportedCookie"; - -declare module "./getCookie" { - /** - * Retrieves browser cookies that match the specified cookie name and domain criteria - * @param cookieSpec - The cookie specification containing search criteria - * @returns A promise that resolves to an array of exported cookies - * @example - * ```typescript - * import { getCookie } from 'get-cookie'; - * - * // Get all cookies for a domain - * const cookies = await getCookie({ - * name: '*', - * domain: 'example.com' - * }); - * - * // Get specific cookie - * const sessionCookie = await getCookie({ - * name: 'sessionId', - * domain: 'api.example.com' - * }); - * ``` - */ - export function getCookie(cookieSpec: CookieSpec): Promise; -} - -declare module "./getChromeCookie" { - /** - * Retrieves Chrome browser cookies that match the specified cookie name and domain criteria - * @param cookieSpec - The cookie specification containing search criteria - * @returns A promise that resolves to an array of exported cookies - * @example - * ```typescript - * import { getChromeCookie } from 'get-cookie'; - * - * // Get all Chrome cookies for a domain - * const cookies = await getChromeCookie({ - * name: '*', - * domain: 'example.com' - * }); - * - * // Get specific Chrome cookie - * const authCookie = await getChromeCookie({ - * name: 'auth', - * domain: 'api.example.com' - * }); - * ``` - */ - export function getChromeCookie( - cookieSpec: CookieSpec, - ): Promise; -} - -declare module "./getFirefoxCookie" { - /** - * Retrieves Firefox browser cookies that match the specified cookie name and domain criteria - * @param cookieSpec - The cookie specification containing search criteria - * @returns A promise that resolves to an array of exported cookies - * @example - * ```typescript - * import { getFirefoxCookie } from 'get-cookie'; - * - * // Get all Firefox cookies for a domain - * const cookies = await getFirefoxCookie({ - * name: '*', - * domain: 'example.com' - * }); - * - * // Get specific Firefox cookie - * const preferenceCookie = await getFirefoxCookie({ - * name: 'prefs', - * domain: 'app.example.com' - * }); - * ``` - */ - export function getFirefoxCookie( - cookieSpec: CookieSpec, - ): Promise; -} - -declare module "./getGroupedRenderedCookies" { - /** - * Retrieves and renders cookies in a grouped format - * @param cookieSpec - The cookie specification containing search criteria - * @returns A promise that resolves to an array of rendered cookie strings - * @example - * ```typescript - * import { getGroupedRenderedCookies } from 'get-cookie'; - * - * // Get cookies grouped by source file - * const cookieStrings = await getGroupedRenderedCookies({ - * name: '*', - * domain: 'example.com' - * }); - * // Returns: [ - * 'Cookies.binarycookies: sessionId=abc123; theme=dark', - * 'Cookies.sqlite: auth=xyz789' - * ] - * ``` - */ - export function getGroupedRenderedCookies( - cookieSpec: CookieSpec, - ): Promise; -} - -declare module "./getMergedRenderedCookies" { - /** - * Retrieves and renders cookies in a merged format - * @param cookieSpec - The cookie specification containing search criteria - * @param options - Optional rendering options - * @returns A promise that resolves to a single rendered cookie string - * @example - * ```typescript - * import { getMergedRenderedCookies } from 'get-cookie'; - * - * // Get all cookies as a single string - * const cookieString = await getMergedRenderedCookies({ - * name: '*', - * domain: 'example.com' - * }); - * // Returns: "sessionId=abc123; theme=dark; auth=xyz789" - * - * // With custom separator - * const customString = await getMergedRenderedCookies( - * { name: '*', domain: 'example.com' }, - * { separator: ' && ' } - * ); - * // Returns: "sessionId=abc123 && theme=dark && auth=xyz789" - * ``` - */ - export function getMergedRenderedCookies( - cookieSpec: CookieSpec, - options?: Omit, - ): Promise; -} diff --git a/src/core/cookies/index.ts b/src/core/cookies/index.ts new file mode 100644 index 0000000..a71f4d4 --- /dev/null +++ b/src/core/cookies/index.ts @@ -0,0 +1,34 @@ +/** + * Main function to retrieve cookies from all supported browsers + */ +export { getCookie } from "./getCookie"; + +/** + * Function to retrieve cookies specifically from Chrome browser + */ +export { getChromeCookie } from "./getChromeCookie"; + +/** + * Function to retrieve cookies specifically from Firefox browser + */ +export { getFirefoxCookie } from "./getFirefoxCookie"; + +/** + * Function to retrieve and render cookies grouped by their source files + */ +export { getGroupedRenderedCookies } from "./getGroupedRenderedCookies"; + +/** + * Function to retrieve and render cookies in a merged format + */ +export { getMergedRenderedCookies } from "./getMergedRenderedCookies"; + +/** + * Utility function to render cookies in various formats + */ +export { renderCookies } from "./renderCookies"; + +/** + * Core function to query cookies using various browser strategies + */ +export { queryCookies } from "./queryCookies"; diff --git a/src/index.ts b/src/index.ts index 2d11a44..d55a491 100755 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1,22 @@ -export * from "./core/cookies/dynamicImports"; +/** + * Core cookie retrieval and manipulation functions + */ +export { + getCookie, + getChromeCookie, + getFirefoxCookie, + getGroupedRenderedCookies, + getMergedRenderedCookies, +} from "./core/cookies"; + +/** + * Type definitions for cookie-related interfaces and configurations + */ +export type { + CookieSpec, + ExportedCookie, + RenderOptions, + BrowserName, + CookieQueryStrategy, + MultiCookieSpec, +} from "./types/schemas"; diff --git a/tsconfig.json b/tsconfig.json index 15e72e7..265908c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,11 +1,5 @@ { - "include": [ - "src/**/*", - "scripts/**/*", - "*.ts", - "src/core/cookies/dynamicImports.ts", - "examples/**/*" - ], + "include": ["src/**/*", "scripts/**/*", "*.ts", "examples/**/*"], "compilerOptions": { "moduleResolution": "node", "target": "es2020",