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

@mariolazzari/quotable

v0.0.5

Published

Quotable REST APIs client TypeScript based

Downloads

77

Readme

Quotable REST API client

This package is a TypeScript based wrapper around the public Quotable REST APIs

Prerequisites

This package requires NodeJS (version 18 or later) and a node package manager (Npm, Yarn, Pnpm or Bun).

To make sure you have them available on your machine, try running the following command.

$ npm -v && node -v
v10.1.0
v18.18.0

Gettting started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.


Installation

BEFORE YOU INSTALL: please read the prerequisites.

Start with cloning this repo on your local machine:

$ git clone https://github.com/mariolazzari/quotable
$ cd quotable

To install and set up the library, run:

npm install @mariolazzari/quotable

Usage

Import package

import { Quotable } from "@mariolazzari/quotable"

Watch mode

npm test

Unit testing

npm test

Bulding new version

npm build

This task will create a distribution version of the project inside your local dist/ folder.


Quotable class

Quotable class content handles all the requests and the responses to Quotable REST APIs.

Constructor

In order to initialize Quotable client:

const quotable = new Quotable();

Methods

Quotable client includes the following methods:

getAuthors

Description

This asynchronous method handles GET /api/authors REST API.

Prototype

async getAuthors(params:AuthorRequest): Promise<Result<AuthorResponse>> 

Sample code

const sortBy: SortBy<Author> = 'quoteCount';
const order: Order = 'desc';
const { success, data, error } = await quotable.getAuthors({ 
    sortBy,
    order,
});

getQuote

Description

This asynchronous method handles GET /api/quotes/:id REST API.

Prototype

async getQuote(id:string): Promise<Result<Quote>> 

Sample code

const id = '2qpi1ZKL9Ko';
const { data, error, success } = await quotable.getQuote(id);

getQuotes

Description

This asynchronous method handles GET /api/quotes REST API.

Prototype

async getQuotes(params:ListQuoteRequest): Promise<Result<ListQuoteResponse>> 

Sample code

const tags = 'love | happiness';
const { data, error, success } = await quotable.getQuotes({ tags });

getRandomQuotes

Description

This asynchronous method handles GET /api/quotes/random REST API.

Prototype

async getQuotes(params:RandomQuoteRequest): Promise<Result<Quote[]>> 

Sample code

const tags = 'technology,famous-quotes';
const { success, data, error } = await quotable.getRandomQuotes({ tags });

getTags

Description

This asynchronous method handles GET /api/tags REST API.

Prototype

async getTags(params:TagRequest): Promise<Result<Tag[]>> 

Sample code

const sortBy: SortBy<Tag> = 'name';
const { success, data, error } = await quotable.getTags({ sortBy });

Types

Author

type Author = {
  // A unique id for this author
  _id: string;
  // A brief, one paragraph bio of the author. Source: wiki API
  bio: string;
  // A one-line description of the author. Typically it is the person's primary
  // occupation or what they are know for.
  description: string;
  // The link to the author's wikipedia page or official website
  link: string;
  // The authors full name
  name: string;
  // A slug is a URL-friendly ID derived from the authors name. It can be used as
  slug: string;
  // The number of quotes by this author
  quoteCount: string;
};

AuthorRequest

type AuthorRequest = Partial<{
  slug: string;
  sortBy: SortBy<Author>;
  order: Order;
  limit: number;
  page: number;
}>;

AuthorResponse

type AuthorResponse = {
  // The number of results included in this response.
  count: number;
  // The total number of results matching this request.
  totalCount: number;
  // The current page number
  page: number;
  // The total number of pages matching this request
  totalPages: number;
  // The 1-based index of the last result included in this response. This shows the
  // current pagination offset.
  lastItemIndex: number | null;
  // The array of authors
  results: Author[];
};

ListQuoteRequest

type ListQuoteRequest = RandomQuoteParams &
  Partial<{
    sortBy: Sort<Quote>;
    order: Order;
    page: number;
  }>;

ListQuoteResponse

type ListQuoteResponse = {
  // The number of quotes returned in this response
  count: number;
  // The total number of quotes matching this query
  totalCount: number;
  // The current page number
  page: number;
  // The total number of pages matching this request
  totalPages: number;
  // The 1-based index of the last result included in the current response.
  lastItemIndex: number;
  // The array of quotes
  results: Quote[];
};

Order

type Order = 'asc' | 'desc' | 'default';

Quote

type Quote = {
  _id: string;
  // The quotation text
  content: string;
  // The full name of the author
  author: string;
  // The `slug` of the quote author
  authorSlug: string;
  // The length of quote (number of characters)
  length: number;
  // An array of tag names for this quote
  tags: string[];
};

RandomQuoteRequest

type RandomQuoteRequest = Partial<{
  limit: number;
  maxLength: number;
  minLength: number;
  tags: string;
  author: string;
}>;

RequestParams

type RequestParams =
  | ListQuoteRequest
  | RandomQuoteRequest
  | AuthorRequest
  | TagRequest;

Result

Discriminated union containig api response.

type Result<T> =
  | {
      success: true;
      data: T;
    }
  | {
      success: false;
      error: string;
    };

SortBy

type Sort<T> = keyof T;

Tag

type Tag = {
  _id: string;
  name: string;
  dateAdded: string;
  dateModified: string;
  __v: number;
  quoteCount: number;
};

TagRequest

type TagRequest = Partial<{
  sortBy: SortBy<Tag>;
  order: Order;
}>;

Authors

  • Mario Lazzari - Initial work

Links