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 = {
_id: string;
bio: string;
description: string;
link: string;
name: string;
slug: string;
quoteCount: string;
};
AuthorRequest
type AuthorRequest = Partial<{
slug: string;
sortBy: SortBy<Author>;
order: Order;
limit: number;
page: number;
}>;
AuthorResponse
type AuthorResponse = {
count: number;
totalCount: number;
page: number;
totalPages: number;
lastItemIndex: number | null;
results: Author[];
};
ListQuoteRequest
type ListQuoteRequest = RandomQuoteParams &
Partial<{
sortBy: Sort<Quote>;
order: Order;
page: number;
}>;
ListQuoteResponse
type ListQuoteResponse = {
count: number;
totalCount: number;
page: number;
totalPages: number;
lastItemIndex: number;
results: Quote[];
};
Order
type Order = 'asc' | 'desc' | 'default';
Quote
type Quote = {
_id: string;
content: string;
author: string;
authorSlug: string;
length: number;
tags: string[];
};
RandomQuoteRequest
type RandomQuoteRequest = Partial<{
limit: number;
maxLength: number;
minLength: number;
tags: string;
author: string;
}>;
RequestParams
type RequestParams =
| ListQuoteRequest
| RandomQuoteRequest
| AuthorRequest
| TagRequest;
Result
type Result<T> = {
success: boolean;
data?: T;
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