npm package: @garycraft/fanbox
Description: A simple wrapper for the Fanbox API
Author: garycraft
Latest version: 1.3.2

# Fanbox
Get infos from [Pixiv Fanbox](https://www.fanbox.cc)

## Getting started
`npm i @garycraft/fanbox --save`

## Usage

### Import the lib
```js
const Fanbox = require('@garycraft/fanbox').default
```

### Init the fanbox
```js
const Fanbox = require('@garycraft/fanbox').default

const fanbox = new Fanbox();
```
### Login to your account
```js
const fanbox = new Fanbox("your cookies");
```
You can find how to get your cookies [here](https://www.clym.io/how-to-find-out-what-cookies-are-set-by-your-website/).
Why you want to login ? To get all subscriptions infos and account related actions (likes, follows)

### Get User
```js
const fanbox = new Fanbox();

const options = {
  withPosts: true,
  withPlans: true,
  withShop: true
};

fanbox.getUser("creatorID", options).then(user => {
    
});
```
Some of the fields way be null or undefined if they are not fetched
All of the options are optionnals and the `options` agrument is optionnal too.

### Get Self User
```js
const fanbox = new Fanbox();

const options = {
  withPosts: true,
  withPlans: true,
  withShop: true
};

fanbox.getSelfUser(options).then(selfUser => {
    
});
```
Some of the fields way be null or undefined if they are not fetched
All of the options are optionnals and the `options` agrument is optionnal too.

### Get Post
```js
const fanbox = new Fanbox();

fanbox.getPost("postID").then(post => {
    
})
```
Return a `ImagePost` or `EntryPost` or `ArticlePost`, if none of the type post is found it will return a `Post` with all the data from the request.
If the creator from the post already exists it will add it to the stored user.

## Classes / Interfaces
### Fanbox
```
{
    public users: Array<User> = [];
    login: (cookies: string) => void;
    getUser: (creatorId: string, options?: {
        withPosts?: boolean,
        withPlans?: boolean,
        withShop?: boolean
    }) => Promise<User>;
    getPost: (id: string) => Promise<Post>
}
```

### User
```
{
    name: string;
    followed: boolean;
    avatarURL: string;
    backgroundURL: string;
    references: Array<string>;
    plans: Array<Plan>;
    posts: {[k: string]: Post};
    shop: Shop;
    pixivId: string;
    creatorId: string;
    getShop: () => Promise<void>;
    getPlans: () => Promise<void>;
    getPosts: () => Promise<void>;
}
```
### SelfUser
```
{
    twitter: {
        enabled: true,
        id: string,
        name: string,
        profileImageUrl: string,
        screenName: string,
        url: string
    } | {
        enabled: false
    };
    name: string;
    id: string;
    avatarUrl: string;
    followedCreatorIds: Array<string>;
    supportedCreatorIds: Array<string>;
    r18: boolean;
    unreadNewsletterCount: number;
    unreadMessageCount: number;
    unreadBellCount: number;
    creatorId: string;
    creator: User
}
```
### Plan
```
{
    name: string;
    description: string;
    price: number;
    iconURL: string;
}
```
### Shop
```
{
    name: string;
    url: string;
    items: Array<{
        adult: boolean,
        description: string,
        id: number,
        image: string,
        market_url: string,
        name: string,
        price: number,
        url: string,
        published_at: Date
    }>;
    total: number;
}
```
### Post
```
{
    name: string;
    date: Date;
    price: number;
    iconURL: string;
    description: string;
    tags: Array<string>;
    type: string;
    id: string;
    liked: boolean;
    likeCount: number;
}
```
### ImagePost (extends Post)
```
{
    text: string;
    images: Array<{
        id: string,
        extention: string,
        width: number,
        height: number,
        originalUrl: string,
        thumbnailUrl: string
    }>;
}
```

### EntryPost (extends Post)
```
{
    html: string;
}
```

### ArticlePost (extends Post)
```
blocks: Array<{
        type: 'p',
        text: string,
        styles?: Array<{
            type: string,
            offset: number,
            length: number
        }>,
        links?: Array<{
            offset: number,
            length: number,
            url: string
        }>
    } | {
        type: 'image',
        imageId: string
    } | {
        type: "header",
        text: string
    }>;
    imageMap: {
        [k: string]: {
            id: string,
            extension: "png" | "jpeg",
            width: number,
            height: number,
            originalUrl: string,
            thumbnailUrl: string
        }
    };
    fileMap: {
        [k: string]: {
            id: string,
            name: string,
            extension: string,
            size: number,
            url: string
        }
    };
    embedMap: {
        [k: string]: {
            id: string,
            serviceProvider: string,
            contentId: string
        }
    };
    urlEmbedMap: {
        [k: string]: {
            id: string,
            type: "fanbox.post",
            postInfo: {
                id: string,
                title: string,
                feeRequired: string,
                hasAdultContent: boolean,
                creatorId: string,
                coverImageUrl: string,
                excerpt: string,
                publishedDatetime: string
            }
        }
    };
```

## Example

```js
const Fanbox = require('@garycraft/fanbox').default
const fanbox = new Fanbox();

// MAKE A ASYNC FUNCTION AT START
(async () => {
   const user = await fanbox.getUser("qtonagi", {
       withPlans: false,
       withShop: false
   });
   console.log(user);
   console.log("Total posts from " + user.name + " : " + Object.keys(user.posts).length);
})()
```


## ENVIRONMENT VARIABLES
- `FANBOX_LOG`: Logging level, by default it is silent, you can set it to `debug` to see more logs.