import { ServerResponse } from 'http' export default class Sitemap { private static allowedChangefreq = ['always', 'hourly', 'daily', 'weekly', 'monthly', 'yearly', 'never'] private datas = '' public constructor( private domain: string, private options?: { response?: ServerResponse } ) { if (this.options?.response) { this.options.response.setHeader('Content-Type', 'text/xml') this.options.response.write(this.datas) } } public addEntry(path: string, options?: { changefreq?: 'always' | 'hourly' | 'daily' | 'weekly' | 'monthly' | 'yearly' | 'never' lastmod?: Date priority?: 1 | 0.9 | 0.8 | 0.7 | 0.6 | 0.5 | 0.4 | 0.3 | 0.2 | 0.1 | 0 }) { let entryString = '' const url = `${this.domain}${path}` .replace(/&/g, '&') .replace(/'/g, ''') .replace(/"/g, '"') .replace(/>/g, '>') .replace(/${url}` if (options) { if (options.changefreq) { if (!Sitemap.allowedChangefreq.includes(options.changefreq)) { console.warn(`changefreq is not one of the allowed words (${options.changefreq})\nChangeFreq won't be added`) } else { entryString += `${options.changefreq}` } } if (options.lastmod) { entryString += `${options.lastmod.toISOString()}` } if (options.priority) { if (options.priority <= 1 && options.priority >= 0 && options.priority.toString().length <= 3) { entryString += `${options.priority}` } else { console.warn(`Priority is not between 0 and 1 and only containing one decimal (${options.priority})\nPriority won't be added`) } } } entryString += '' if (this.options?.response) { this.options.response.write(entryString) } else { this.datas += entryString } } public build(): string { if (this.options?.response) { this.options.response.write('') this.options.response.end() return '' } return this.datas + '' } }