# @deox/gumroad

API client for Gumroad. All the methods in API v2 are available.

## Installation

Install the package by running the following command in terminal:

```shell
npm install @deox/gumroad
```

## Usage

> **Warning:** The library uses Fetch standard (Fetch API including fetch, Response, etc) which means that the environment must support it to make the library work!

```ts
// index.ts
import { Gumroad } from "@deox/gumroad";

const gumroad = new Gumroad(process.env.GUMROAD_ACCESS_TOKEN);
```

## API

Currently, all the methods in Gumroad API v2 are available.

Some examples are:

```ts
// index.ts
import { Gumroad } from "@deox/gumroad";

const gumroad = new Gumroad(process.env.GUMROAD_ACCESS_TOKEN);

const products = await gumroad.products.list(); // type: Product[]

const sales = await gumroad.sales.list(); // type: Sale[]

// ...
```

There are more methods. You can explore it by installing the package!

## Webhook (Resource Subscriptions)

You can handle webhook requests with ease by using this library. An example is shown below which is written for Cloudflare Workers:

```ts
// src/index.ts
import { env } from "cloudflare:workers";
import { Gumroad } from "@deox/gumroad";

const gumroad = new Gumroad(env.GUMROAD_ACCESS_TOKEN);

gumroad.on(
  "ping",
  async (ctx, next) => {
    console.log(
      `Good news! A new sale was made using an email address ${ctx.data.email}`,
    );

    const sale = await ctx.api.sales.get(ctx.data.sale_id);

    console.log(sale);

    // ...

    if (ctx.data.test) {
      await next();
    }
  },
  () => {
    // This will be called only on test pings
    console.log("A test ping!");
  },
);

export default {
  async fetch(request, env) {
    const url = new URL(request.url);

    // Check for pathname and request method
    if (request.method === "POST" && url.pathname === "/ping") {
      // Second parameter is type of resource subscription
      // which is going to be posted on this endpoint
      // Make sure you specify the correct type
      return gumroad.handle(request, "ping");
    }

    return new Response(null, { status: 404 });
  },
} satisfies ExportedHandler<Env>;
```
