npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@__dirname/pixiv-web-api

v0.1.2

Published

A thin wrapper of pixiv web api

Downloads

32

Readme

pixiv-web-api

对 Pixiv 网页端 API 的简单封装
零依赖,基于 fetch(),因此支持多数 Javascript 运行时

例如:浏览器(需要自行处理 CORS),nodejs>=18,deno,bun,以及其它 serverless 环境

返回值

[!NOTE]
返回值暂时没有类型标注。

Pixiv 网页端 API 存在两种形式的返回值,大多数情况如下,数据包含在 body 属性下
此时 error 为 false 时只返回 body 属性的值,否则将 message 封装为 Error 抛出

type APIResponse<T> = {
    error: boolean;
    message: string;
    body: T;
};

另外一种则是直接返回数据,此时我们也直接返回数据

安装

node.js 安装

npm i @__dirname/pixiv-web-api --registry https://registry.npmjs.org/
# 确保使用的是官方 npm 源
import * as pixiv_web_api from "@__dirname/pixiv-web-api";
// 或按需引入
import { ranking } from "@__dirname/pixiv-web-api";

浏览器引入为全局变量

<script src="https://unpkg.com/@__dirname/pixiv-web-api/dist/index.umd.js"></script>
<script>
    pixiv_web_api.ranking().then((data) => {
        // ...
    });
</script>

浏览器以模块导入

<script type="module">
    import * as pixiv_web_api from "https://unpkg.com/@__dirname/pixiv-web-api/dist/index.js";
    pixiv_web_api.ranking().then((data) => {
        // ...
    });
</script>

示例

import * as api from "@__dirname/pixiv-web-api";

// 默认配置直接请求官方接口,使用默认配置则不需要调用下面的配置函数
api.setOptions({
    // 默认值 https://www.pixiv.net (国内网络需要代理)
    // 若实现了对 https://www.pixiv.net 的反向代理,则可以使用该代理调用 API,例如:
    baseURL: "https://md-cors.deno.dev/https://www.pixiv.net",
    // 设置语言
    // 浏览器默认为本机语言,其它环境默认值为 zh-CN(设置为空字符串可移除)
    acceptLanguage: "zh-CN",
    // 仅当访问需要登陆的接口才必须提供
    cookie: "xxx",
    // 默认值为 globalThis.fetch
    // 用于自定义发出请求的 fetch 函数
    fetch: globalThis.fetch,
});

const data = await api.ranking(); // 获取排行榜
console.log(data);

构建

git clone https://github.com/YieldRay/pixiv-web-api.git
cd pixiv-web-api
npm install
npm run build
npm pack --pack-destination out