Skip to content

Commit

Permalink
TypeScript Working
Browse files Browse the repository at this point in the history
  • Loading branch information
seantomburke committed Nov 6, 2021
1 parent d368fc6 commit 4ae7124
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 18 deletions.
201 changes: 201 additions & 0 deletions lib/assets/sitemapper.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
/**
* Sitemap Parser
*
* Copyright (c) 2020 Sean Thomas Burke
* Licensed under the MIT license.
* @author Sean Burke <@seantomburke>
*/
/// <reference types="node" />
import { SitemapperOptions } from '../../sitemapper';
/**
* @typedef {Object} Sitemapper
*/
export default class Sitemapper {
url: string;
timeout: number;
timeoutTable: Object;
requestHeaders: any;
debug: boolean;
/**
* Construct the Sitemapper class
*
* @params {Object} options to set
* @params {string} [options.url] - the Sitemap url (e.g https://wp.seantburke.com/sitemap.xml)
* @params {Timeout} [options.timeout] - @see {timeout}
*
* @example let sitemap = new Sitemapper({
* url: 'https://wp.seantburke.com/sitemap.xml',
* timeout: 15000
* });
*/
constructor(options?: SitemapperOptions);
/**
* Gets the sites from a sitemap.xml with a given URL
*
* @public
* @param {string} [url] - the Sitemaps url (e.g https://wp.seantburke.com/sitemap.xml)
* @returns {Promise<SitesData>}
* @example sitemapper.fetch('example.xml')
* .then((sites) => console.log(sites));
*/
fetch(url?: string): Promise<{
url: string;
sites: string[];
}>;
/**
* Get the timeout
*
* @example console.log(sitemapper.timeout);
* @returns {Timeout}
*/
static get timeout(): Number;
/**
* Set the timeout
*
* @public
* @param {Timeout} duration
* @example sitemapper.timeout = 15000; // 15 seconds
*/
static set timeout(duration: Number);
/**
*
* @param {string} url - url for making requests. Should be a link to a sitemaps.xml
* @example sitemapper.url = 'https://wp.seantburke.com/sitemap.xml'
*/
static set url(url: string);
/**
* Get the url to parse
* @returns {string}
* @example console.log(sitemapper.url)
*/
static get url(): string;
/**
* Setter for the debug state
* @param {Boolean} option - set whether to show debug logs in output.
* @example sitemapper.debug = true;
*/
static set debug(option: boolean);
/**
* Getter for the debug state
* @returns {Boolean}
* @example console.log(sitemapper.debug)
*/
static get debug(): boolean;
/**
* Requests the URL and uses parsestringPromise to parse through and find the data
*
* @private
* @param {string} [url] - the Sitemaps url (e.g https://wp.seantburke.com/sitemap.xml)
* @returns {Promise<ParseData>}
*/
parse(url?: string): Promise<{
error: any;
data: any;
}>;
/**
* Timeouts are necessary for large xml trees. This will cancel the call if the request is taking
* too long, but will still allow the promises to resolve.
*
* @private
* @param {string} url - url to use as a hash in the timeoutTable
* @param {Promise} requester - the promise that creates the web request to the url
*/
initializeTimeout(url: string, requester: {
cancel: Function;
}): void;
/**
* Recursive function that will go through a sitemaps tree and get all the sites
*
* @private
* @recursive
* @param {string} url - the Sitemaps url (e.g https://wp.seantburke.com/sitemap.xml)
* @returns {Promise<SitesArray> | Promise<ParseData>}
*/
crawl(url: string): Promise<Array<string>>;
/**
* Gets the sites from a sitemap.xml with a given URL
*
* @deprecated
* @param {string} url - url to query
* @param {getSitesCallback} callback - callback for sites and error
* @callback
*/
getSites(url: string, callback: any): Promise<any>;
/**
* Check to see if the url is a gzipped url
*
* @param {string} url - url to query
* @returns {Boolean}
*/
isGzip(url: string): boolean;
/**
* Decompress the gzipped response body using zlib.gunzip
*
* @param {Buffer} body - body of the gzipped file
* @returns {Boolean}
*/
decompressResponseBody(body: Buffer): Promise<Buffer>;
}
/**
* Callback for the getSites method
*
* @callback getSitesCallback
* @param {Object} error - error from callback
* @param {Array} sites - an Array of sitemaps
*/
/**
* Timeout in milliseconds
*
* @typedef {Number} Timeout
* the number of milliseconds before all requests timeout. The promises will still resolve so
* you'll still receive parts of the request, but maybe not all urls
* default is 15000 which is 15 seconds
*/
/**
* Resolve handler type for the promise in this.parse()
*
* @typedef {Object} ParseData
*
* @property {Error} error that either comes from `parsestringPromise` or `got` or custom error
* @property {Object} data
* @property {string} data.url - URL of sitemap
* @property {Array} data.urlset - Array of returned URLs
* @property {string} data.urlset.url - single Url
* @property {Object} data.sitemapindex - index of sitemap
* @property {string} data.sitemapindex.sitemap - Sitemap
* @example {
* error: "There was an error!"
* data: {
* url: 'https://linkedin.com',
* urlset: [{
* url: 'https://www.linkedin.com/project1'
* },[{
* url: 'https://www.linkedin.com/project2'
* }]
* }
* }
*/
/**
* Resolve handler type for the promise in this.parse()
*
* @typedef {Object} SitesData
*
* @property {string} url - the original url used to query the data
* @property {SitesArray} sites
* @example {
* url: 'https://linkedin.com/sitemap.xml',
* sites: [
* 'https://linkedin.com/project1',
* 'https://linkedin.com/project2'
* ]
* }
*/
/**
* An array of urls
*
* @typedef {string[]} SitesArray
* @example [
* 'https://www.google.com',
* 'https://www.linkedin.com'
* ]
*/
2 changes: 1 addition & 1 deletion lib/assets/sitemapper.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions lib/examples/google.js

This file was deleted.

2 changes: 0 additions & 2 deletions lib/examples/index.js

This file was deleted.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
},
"license": "MIT",
"files": [
"lib",
"sitemapper.d.ts"
"lib"
],
"main": "./lib/assets/sitemapper.js",
"types": "./sitemapper.d.ts",
Expand All @@ -33,12 +32,13 @@
"url": "http://www.seantburke.com"
},
"scripts": {
"compile": "babel src -d lib -s && tsc --project ./src/tests/",
"compile": "tsc -d --sourceMap --outDir lib -t esnext ./src/**/*.ts && babel lib -d lib -s",
"build": "npm run clean && npm run compile",
"start": "npm run build && node lib/examples/index.js",
"test": "npm run build && mocha ./lib/tests/*.js && npm run lint",
"lint": "eslint src",
"clean": "rm -rf lib",
"tsc": "tsc",
"docs": "documentation build ./src/assets/sitemapper.js -f md > docs.md"
},
"maintainers": [
Expand Down
12 changes: 7 additions & 5 deletions src/assets/sitemapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
*/

import { parseStringPromise } from 'xml2js';
import got, { Headers, OptionsOfTextResponseBody } from 'got';
// @ts-ignore
import got from 'got';
// @ts-ignore
import zlib from 'zlib';
// @ts-ignore
import Url from 'url';
// @ts-ignore
import path from 'path';
import { SitemapperOptions, SitemapperResponse} from '../../sitemapper';
import { ErrorCallback } from 'typescript';
import { Response } from 'got';
import { Buffer } from 'buffer';

/**
Expand All @@ -38,7 +40,7 @@ export default class Sitemapper {
* timeout: 15000
* });
*/
constructor(options: SitemapperOptions) {
constructor(options?: SitemapperOptions) {
const settings: SitemapperOptions = options || { requestHeaders: {}};
this.url = settings.url;
this.timeout = settings.timeout || 15000;
Expand Down Expand Up @@ -294,7 +296,7 @@ export default class Sitemapper {
* @param {string} url - url to query
* @returns {Boolean}
*/
isGzip(url) {
isGzip(url: string) {
const parsed = Url.parse(url);
const ext = path.extname(parsed.path);
return ext === '.gz';
Expand Down
4 changes: 2 additions & 2 deletions src/tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ describe('Sitemapper', function () {
});

it('should set url', () => {
sitemapper.url = 1000;
sitemapper.url.should.equal(1000);
sitemapper.url = 'https://wp.seantburke.com/sitemap.xml';
sitemapper.url.should.equal('https://wp.seantburke.com/sitemap.xml');
});
});

Expand Down
6 changes: 3 additions & 3 deletions src/tests/test.ts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'async';
import 'assert';
import 'should';
import isUrl = require('is-url');
const isUrl = require('is-url');

// @ts-ignore
import Sitemapper from '../../lib/assets/sitemapper.js';
Expand Down Expand Up @@ -52,8 +52,8 @@ describe('Sitemapper', function () {
});

it('should set url', () => {
sitemapper.url = 1000;
sitemapper.url.should.equal(1000);
sitemapper.url = 'https://wp.seantburke.com/sitemap.xml';
sitemapper.url.should.equal('https://wp.seantburke.com/sitemap.xml');
});
});

Expand Down

0 comments on commit 4ae7124

Please sign in to comment.