index.d.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { Options } from 'xml2js';
  2. import { RequestOptions } from 'https';
  3. declare namespace Parser {
  4. type CustomFieldItem<U> = keyof U | (string | { keepArray: boolean })[]
  5. export interface CustomFields<T, U> {
  6. readonly feed?: Array<keyof T>;
  7. readonly item?: CustomFieldItem<U>[] | CustomFieldItem<U>[][];
  8. }
  9. export interface ParserOptions<T, U> {
  10. readonly xml2js?: Options;
  11. readonly requestOptions?: RequestOptions;
  12. readonly headers?: Record<string, string>;
  13. readonly defaultRSS?: number;
  14. readonly maxRedirects?: number;
  15. readonly customFields?: CustomFields<T, U>;
  16. readonly timeout?: number;
  17. }
  18. export interface Enclosure {
  19. url: string;
  20. length?: number;
  21. type?: string;
  22. }
  23. export interface Item {
  24. link?: string;
  25. guid?: string;
  26. title?: string;
  27. pubDate?: string;
  28. creator?: string;
  29. summary?: string;
  30. content?: string;
  31. isoDate?: string;
  32. categories?: string[];
  33. contentSnippet?: string;
  34. enclosure?: Enclosure;
  35. }
  36. export interface PaginationLinks {
  37. self?: string;
  38. first?: string;
  39. next?: string;
  40. last?: string;
  41. prev?: string;
  42. }
  43. export interface Output<U> {
  44. image?: {
  45. link?: string;
  46. url: string;
  47. title?: string;
  48. },
  49. paginationLinks?: PaginationLinks;
  50. link?: string;
  51. title?: string;
  52. items: (U & Item)[];
  53. feedUrl?: string;
  54. description?: string;
  55. itunes?: {
  56. [key: string]: any;
  57. image?: string;
  58. owner?: {
  59. name?: string;
  60. email?: string;
  61. };
  62. author?: string;
  63. summary?: string;
  64. explicit?: string;
  65. categories?: string[];
  66. keywords?: string[];
  67. };
  68. }
  69. }
  70. /**
  71. * Class that handles all parsing or URL, or even XML, RSS feed to JSON.
  72. */
  73. declare class Parser<T = {[key: string]: any}, U = {[key: string]: any}> {
  74. /**
  75. * @param options - Parser options.
  76. */
  77. constructor(options?: Parser.ParserOptions<T, U>);
  78. /**
  79. * Parse XML content to JSON.
  80. *
  81. * @param xml - The xml to be parsed.
  82. * @param callback - Traditional callback.
  83. *
  84. * @returns Promise that has the same Output as the callback.
  85. */
  86. parseString(
  87. xml: string,
  88. callback?: (err: Error, feed: Parser.Output<U>) => void
  89. ): Promise<T & Parser.Output<U>>;
  90. /**
  91. * Parse URL content to JSON.
  92. *
  93. * @param feedUrl - The url that needs to be parsed to JSON.
  94. * @param callback - Traditional callback.
  95. * @param redirectCount - Max of redirects, default is set to five.
  96. *
  97. * @example
  98. * await parseURL('https://www.reddit.com/.rss');
  99. * parseURL('https://www.reddit.com/.rss', (err, feed) => { ... });
  100. *
  101. * @returns Promise that has the same Output as the callback.
  102. */
  103. parseURL(
  104. feedUrl: string,
  105. callback?: (err: Error, feed: Parser.Output<U>) => void,
  106. redirectCount?: number
  107. ): Promise<T & Parser.Output<U>>;
  108. }
  109. export = Parser;