JSPM

telegraph.js

1.0.0
    • ESM via JSPM
    • ES Module Entrypoint
    • Export Map
    • Keywords
    • License
    • Repository URL
    • TypeScript Types
    • README
    • Created
    • Published
    • 0
    • Score
      100M100P100Q24164F
    • License ISC

    The Telegra.ph API wrapper made in Typescript.

    Package Exports

    • telegraph.js
    • telegraph.js/dist/index.js

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (telegraph.js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    Readme

    Telegraph.js

    Overview

    The Telegra.ph API wrapper made in Typescript. Simplifies creating and managing accounts, pages, and monitor the page stats on Telegraph. It uses schema validation using zod.

    Key Features:

    • Account Management: Create, edit, and retrieve account info.
    • Page Management: Create, edit, and retrieve pages.
    • Views: Track page views.

    Table of Contents

    1. Instalation
    2. Usage
    3. API Referenece

    Instalation

    npm install telegraph.js

    or

    yarn add telegraph.js

    Usage

    Client Setup

    Before using any of the API methods, you need to create an instance of the Telegraph class and set the access token.

    import { Telegraph } from 'telegraph.js';
    
    const telegraph = new Telegraph();

    Creating an Account

    const acount = await telegraph.createAccount({
      shortname: 'nugra',
      author_name: 'nugraizy',
      author_url: 'https://github.com/ngraizy',
    });

    Editing Account Info

    const updatedAccount = await telegraph.updateAccount({
      shortname: 'nugra',
      author_name: 'nugraizy',
      author_url: 'https://github.com/ngraizy',
    });

    Get Account Info

    const accountInfo = await telegraph.getAccountInfo({
      fields: ['author_name', 'author_url', 'page_count', 'short_name', 'auth_url'],
    });

    Revoke Access Token

    const account = await telegraph.revokeAccessToken();

    Creating a Page

    const page = await telegraph.createPage({
      title: 'My first Page',
      author_name: 'nugraizy',
      author_url: 'https://github.com/ngraizy',
      content: [
        {
          tag: 'p',
          children: ['This is a sample page.'],
          tag: 'p',
          attrs: { href: 'https://github.com/nugraizy' },
          children: ['Visit my profile!'],
        },
      ],
      return_content: false,
    });

    Creating a Page using Markdown

    import { Parser } from 'telegraph.js';
    
    const content = Parser.parse(`This is a sample page.
    
    [Visit my profile!](https://github.com/nugraizy)`);
    
    const page = await telegraph.createPage({
      title: 'My first Page',
      author_name: 'nugraizy',
      author_url: 'https://github.com/ngraizy',
      content: content,
      return_content: false,
    });

    Editing a Page

    const updatedPage = await telegraph.editPage({
      author_name: 'nugraizy',
      author_url: 'https://authorurl.com',
      path: 'my-first-page', // usually the path has date in it.
      title: 'Updated Title',
      content: [{ tag: 'p', children: ['Updated content.'] }],
      return_content: false,
    });

    Editing a Page using Markdown

    import { Parser } from 'telegraph.js';
    
    const content = Parser.parse(`Updated content.
    
    [Visit my profile!](https://github.com/nugraizy)`);
    
    const updatedPage = await telegraph.editPage({
      author_name: 'nugraizy',
      author_url: 'https://authorurl.com',
      path: 'my-first-page', // usually the path has date in it.
      title: 'Updated Title',
      content: content,
      return_content: false,
    });

    Retrieving a Page

    const page = await telegraph.getPage({
      path: 'my-first-page', // usually the path has date in it.
      return_content: true,
    });

    Tracking Page Views

    const page = await telegraph.getViews({
      path: 'my-first-page', // usually the path has date in it.
    });

    API Reference

    Constructor

    new Telegraph(token?: string);
    • token: (optional) — The access token required for making requests to the Telegra.ph API

    Methods

    createAccount

    createAccount({ author_name, short_name, author_url });

    Creates a new Telegraph account.

    • Parameters:
      • author_name: (string) — The author's name (0-128 characters).
      • short_name: (string) — The account's short name (1-32 characters).
      • author_url: (string) — The author's URL (0-512 characters).
    • Returns: Promise<Partial<Account> | undefined>

    editAccountInfo

    editAccountInfo({ short_name, author_name, author_url });

    Edits an existing account.

    getAccountInfo

    getAccountInfo({ fields });

    Retrieves account information.

    revokeAccessToken

    revokeAccessToken();

    Revoke Current Access Token.

    createPage

    createPage({ title, author_name, author_url, content, return_content });

    Creates a new page.

    • Parameters:
      • title: (string) — The title of the page (1-256 characters).
      • author_name: (string) — The author’s name.
      • author_url: (string) — The author’s URL.
      • content: (NodeElement[]) — The content of the page.
      • return_content: (boolean) — Whether to return the content in the response.
    • Returns: Promise<Partial<Page<T>> | undefined>

    editPage

    editPage({ path, title, content, author_name, author_url, return_content });

    Edits an existing page.

    • Parameters:
      • path: (string) — The path to the page.
      • title: (string) — The new title.
      • content: (NodeElement[]) — The new content.
      • author_name: (string) — The author’s name.
      • author_url: (string) — The author’s URL.
      • return_content: (boolean) — Whether to return the content in the response.
    • Returns: Promise<Partial<Page<T>> | undefined>

    getPage

    getPage({ path, return_content });

    Retrieves a specific page.

    getPageList

    getPageList({ offset, limit });

    Retrieves a list of pages.

    getViews

    getViews({ path, year, month, day, hour });

    Retrieves view statistics for a page.

    • Parameters:
      • path: (string) — The path to the page.
      • year: (number) — Year filter (optional).
      • month: (number) — Month filter (optional).
      • day: (number) — Day filter (optional).
      • hour: (number) — Hour filter (optional).
    • Returns: Promise<Partial<PageViews> | undefined>