Files
jackwener__opencli/clis/pixiv/utils.js
jakevin d2974a9ff6 refactor(adapters): convert adapter layer from TypeScript to JavaScript (#928)
* refactor(adapters): convert adapter layer from TypeScript to JavaScript

Core framework stays TypeScript; adapter layer moves to JS-first.
Adapters are essentially "executable config + browser scripts" that
barely use TS features — this simplifies the build/distribution pipeline
by removing the dist/clis/ intermediate compilation step.

Changes:
- Convert all 753 adapter files in clis/ from .ts to .js
- Update tsconfig to exclude clis/ from compilation
- Simplify build-manifest to scan clis/*.js directly (no dist/clis/)
- Update discovery, main, fetch-adapters to load JS adapters from clis/
- Update generate-verified to output .js artifacts
- Update package.json files field: dist/clis/ → clis/
- Fix all test files for the .ts → .js transition

* fix(main): use findPackageRoot for BUILTIN_CLIS path

The previous relative path (../../clis from __dirname) only worked for
dist/src/main.js but broke dev mode (tsx src/main.ts) where __dirname
is <repo>/src — resolving to /clis instead of <repo>/clis.

Use findPackageRoot() which works for both dev and prod paths.
2026-04-10 14:52:18 +08:00

50 lines
2.0 KiB
JavaScript

/**
* Pixiv shared helpers: authenticated Ajax fetch with standard error handling.
*
* All Pixiv Ajax APIs return `{ error: false, body: ... }` on success.
* On failure the HTTP status code is used to distinguish auth (401/403),
* not-found (404), and other errors.
*/
import { AuthRequiredError, CommandExecutionError } from '@jackwener/opencli/errors';
const PIXIV_DOMAIN = 'www.pixiv.net';
/**
* Navigate to Pixiv (to attach cookies) then fetch a Pixiv Ajax API endpoint.
*
* Handles the common navigate → evaluate(fetch) → error-check pattern used
* by every Pixiv TS adapter.
*
* @param page - Browser page instance
* @param path - API path, e.g. '/ajax/illust/12345'
* @param opts - Optional query params
* @returns - The parsed `body` from the JSON response
* @throws AuthRequiredError on 401/403
* @throws CommandExecutionError on 404 or other HTTP errors
*/
export async function pixivFetch(page, path, opts = {}) {
await page.goto(`https://${PIXIV_DOMAIN}`);
const qs = opts.params
? '?' + Object.entries(opts.params).map(([k, v]) => `${k}=${encodeURIComponent(v)}`).join('&')
: '';
const url = `https://${PIXIV_DOMAIN}${path}${qs}`;
const data = await page.evaluate(`
(async () => {
const res = await fetch(${JSON.stringify(url)}, { credentials: 'include' });
if (!res.ok) return { __httpError: res.status };
return await res.json();
})()
`);
if (data?.__httpError) {
const status = data.__httpError;
if (status === 401 || status === 403) {
throw new AuthRequiredError(PIXIV_DOMAIN, 'Authentication required — please log in to Pixiv in Chrome');
}
if (status === 404) {
throw new CommandExecutionError(opts.notFoundMsg || `Pixiv resource not found (HTTP 404)`);
}
throw new CommandExecutionError(`Pixiv request failed (HTTP ${status})`);
}
return data?.body;
}
/** Maximum number of illust IDs per batch detail request (Pixiv server limit). */
export const BATCH_SIZE = 48;