API tidak resmi untuk royalroad.com.
npm i -s @fsoc/royalroadl-api
This is an attempt to write a predictable and consistent wrapper around the mess that is RR. Karena tidak ada API publik resmi yang terpapar, modul ini mengikis semua data langsung dari HTML, yang membuatnya sangat rentan terhadap kematian spontan dan mengerikan.
Barebones documentation generated from TypeDoc can be found on fs-c.github.io/royalroad-api/. Anda juga dapat membangun dokumen ini sendiri dengan menjalankan npm run docs di akar proyek.
For more examples check out the /examples directory, see also the tests in /test .
import { RoyalRoadAPI } from '@fsoc/royalroadl-api' ;
const api = new RoyalRoadAPI ( ) ;
const { data } = await api . fictions . getPopular ( ) ;
const titles = data . slice ( 10 ) . map ( ( fic ) => fic . title ) ;
console . log ( `The top 10 popular fictions are: ${ titles . join ( ', ' ) } ` ) ; The module itself exports only a RoyalRoadAPI class which, by itself, has no methods. All functionality is delegated to service classes which are properties of the RoyalRoadAPI instance.
All responses and errors are either an instance of a RoyalResponse or a RoyalError , which extends RoyalResponse . This is done to easily allow for meta information to be tacked onto responses, and to have a consistent interface between user and module. Note that the RoyalError acts similarly to the NodeJS Error object, in that it captures and returns a short stack trace.
For example, a call to RoyalRoadAPI#fiction.getFiction() might yield the following response on success:
RoyalResponse {
data :
{ type : 'Original' ,
tags : [ 'Action' , 'Adventure' , 'Sci-fi' , ... 3 more items ] ,
stats :
{ pages : 766 ,
ratings : 719 ,
followers : 2991 ,
favorites : 690 ,
views : [ Object ] ,
score : [ Object ] } ,
title : 'Paladin' ,
image :
'https://www.royalroadcdn.com/(...)' ,
status : 'HIATUS' ,
author :
{ name : 'Komikhan' ,
title : '' ,
avatar :
'https://www.royalroadcdn.com/(...)' ,
id : 66486 } ,
warnings : [ 'Gore' , 'Profanity' ] ,
chapters : [ [ Object ] , [ Object ] , [ Object ] , ... 71 more items ] ,
description :
'When the first derelict alien spacecraft fell to Earth, (...)' } ,
success : true ,
timestamp : 1528119296799 }...or on error:
RoyalError {
data :
{ message : 'Page Not Found' ,
stack :
[ 'Error' ,
' at new RoyalError' , ... 8 more items ] } ,
success : false ,
timestamp : 1528119381034 } All service classes use the same instance of the Requester , the class responsible for making HTTP requests and returning their responses. By default, it will throw a RoyalError if it encounters a status code other than 200 (this can be disabled with the ignoreStatus option).
Karena RR suka mengembalikan 200 bahkan ketika respons aktual harus 404 atau 304, Requester akan mengurai HTML yang didapatnya (jika ada), dan mencoba membaca kesalahan darinya. If it finds signs that the request has failed, it will throw - this can be disabled with the ignoreParser option.
Tujuan utama Requester adalah untuk melacak cookie dan secara otomatis mengambil __ResponseVerificationToken yang sering dibutuhkan untuk permintaan pasca sebagai bagian dari tindakan anti CSRF. Pengambilan token ini dinonaktifkan secara default dan dapat diaktifkan dengan opsi fetchToken .
Semua layanan disusun dengan cara yang sangat mirip: dengan <Type>Service yang memperlihatkan semua metode yang relevan, dan <Type>Parser , yang biasanya memaparkan sejumlah metode statis yang digunakan untuk mengurai respons HTML.
Pembungkus cepat dari semua layanan yang ada adalah:
ChapterService , berisi metode untuk mengambil dan menerbitkan bab dan komentar bab.FictionService , mengambil data dan ulasan fiksi.FictionsService , Metode untuk mengambil semua jenis daftar fiksi yang ditawarkan RRL, dengan tingkat per detail per fiksi masing -masing.ProfileService , Profil Penanganan, Mengembalikan Profil Pengguna Parsed.UserService , tindakan yang terkait dengan pengguna yang masuk seperti logon, mendapatkan fiksi, bookmark, atau pemberitahuan pengguna.This uses cheerio to parse HTML, which is a very forgiving parser. Ini berarti bahwa bahkan jika RRL membuat perubahan kecil pada tata letak halaman mereka, sebagian besar API (bahkan bagian -bagian yang bertanggung jawab untuk area yang diubah) masih akan tetap fungsional.
Therefore, expect properties to be empty or null , and know that an error will not be thrown just because some values could not be parsed.