Glanceway Glanceway
すべてのソース

Product Hunt

ニュース v1.0.0

Today's top products from Product Hunt

@codytseng #producthunt #products #startups

設定項目

名前 キー タイプ 必須 デフォルト 説明
API Token API_TOKEN secret はい Product Hunt Developer Token (get one at https://api.producthunt.com/v2/oauth/applications)

ソースコード

version: 1.0.0
name: Product Hunt
description: Today's top products from Product Hunt
author: codytseng
author_url: https://github.com/codytseng
category: News
tags:
  - producthunt
  - products
  - startups

config:
  - key: API_TOKEN
    name: API Token
    type: secret
    required: true
    description: Product Hunt Developer Token (get one at https://api.producthunt.com/v2/oauth/applications)
import type { GlancewayAPI, SourceMethods } from "../../types";

export default (api: GlancewayAPI): SourceMethods => {
  return {
    async refresh() {
      const token = api.config.get("API_TOKEN");

      const query = `{
        posts(order: RANKING) {
          edges {
            node {
              id
              name
              tagline
              url
              votesCount
              createdAt
            }
          }
        }
      }`;

      const response = await api.fetch<{
        data: {
          posts: {
            edges: Array<{
              node: {
                id: string;
                name: string;
                tagline: string;
                url: string;
                votesCount: number;
                createdAt: string;
              };
            }>;
          };
        };
      }>("https://api.producthunt.com/v2/api/graphql", {
        method: "POST",
        headers: {
          Authorization: `Bearer ${token}`,
          "Content-Type": "application/json",
        },
        body: JSON.stringify({ query }),
      });

      if (!response.ok || !response.json) {
        return;
      }

      api.emit(
        response.json.data.posts.edges.map((edge) => ({
          id: edge.node.id,
          title: edge.node.name,
          subtitle: edge.node.tagline,
          url: edge.node.url,
          timestamp: edge.node.createdAt,
        })),
      );
    },
  };
};