From d78459330b29bc28a8de5a60e89892c42f6c276b Mon Sep 17 00:00:00 2001 From: Stefan Probst Date: Mon, 1 Feb 2021 20:24:55 +0100 Subject: [PATCH 1/4] Add types --- package.json | 9 ++++++-- types/index.d.ts | 50 +++++++++++++++++++++++++++++++++++++++++++++ types/test.ts | 20 ++++++++++++++++++ types/tsconfig.json | 16 +++++++++++++++ types/tslint.json | 10 +++++++++ 5 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 types/index.d.ts create mode 100644 types/test.ts create mode 100644 types/tsconfig.json create mode 100644 types/tslint.json diff --git a/package.json b/package.json index 066d3fc..82041dd 100644 --- a/package.json +++ b/package.json @@ -25,10 +25,13 @@ "type": "module", "main": "./index.mjs", "module": "./index.mjs", + "types": "types", "files": [ - "index.mjs" + "index.mjs", + "types/index.d.ts" ], "dependencies": { + "@types/xast": "^1.0.0", "bcp-47-normalize": "^1.0.0", "unist-builder": "^2.0.0", "xastscript": "^2.0.0" @@ -36,6 +39,7 @@ "devDependencies": { "c8": "^7.0.0", "concat-stream": "^2.0.0", + "dtslint": "^4.0.0", "prettier": "^2.0.0", "regenerate": "^1.0.0", "remark-cli": "^9.0.0", @@ -48,7 +52,8 @@ "format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", "test-api": "node test.mjs", "test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --experimental-modules test.mjs", - "test": "npm run format && npm run test-coverage" + "test-types": "dtslint types", + "test": "npm run format && npm run test-coverage && npm run test-types" }, "prettier": { "tabWidth": 2, diff --git a/types/index.d.ts b/types/index.d.ts new file mode 100644 index 0000000..1581b04 --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,50 @@ +// Minimum TypeScript Version: 3.5 + +import {Root} from 'xast' + +/** + * Entries represent a single URL and describe them with metadata. + */ +export interface Entry { + /** + * Full URL. + * + * @see https://www.sitemaps.org/protocol.html#locdef + * + * @example 'https://example.org/' + */ + url: string + + /** + * Value indicating when the page last changed. + */ + modified?: Date | string | number + + /** + * BCP 47 tag indicating the language of the page. + * + * @see https://github.com/wooorm/bcp-47 + * + * @example 'en-GB' + */ + lang?: string + + /** + * Translations of the page, where each key is a BCP 47 tag and each value an entry. + * + * Alternate resources inherit fields from the entry they are described in. + * + * @see https://github.com/wooorm/bcp-47 + */ + alternate?: Record< + string, + string | Omit // eslint-disable-line @typescript-eslint/ban-types + > +} + +/** + * Build a sitemap. + * + * @param data URLs to build a sitemap for. + */ +export function sitemap(data: Array): Root diff --git a/types/test.ts b/types/test.ts new file mode 100644 index 0000000..c81a003 --- /dev/null +++ b/types/test.ts @@ -0,0 +1,20 @@ +import {sitemap} from 'xast-util-sitemap' + +sitemap([ + 'https://example.com', + {url: 'https://example.com'}, + {url: 'https://example.com', modified: new Date()}, + {url: 'https://example.com', modified: Date.now()}, + {url: 'https://example.com', modified: '01-02-2021'}, + {url: 'https://example.com', lang: 'en-GB'}, + { + url: 'https://example.com', + lang: 'en-GB', + alternate: {'de-AT': 'https://example.at'} + }, + { + url: 'https://example.com', + lang: 'en-GB', + alternate: {'de-AT': {url: 'https://example.at', modified: Date.now()}} + } +]) diff --git a/types/tsconfig.json b/types/tsconfig.json new file mode 100644 index 0000000..6f64483 --- /dev/null +++ b/types/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "module": "es2015", + "lib": ["es2015"], + "moduleResolution": "node", + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "noEmit": true, + "baseUrl": ".", + "paths": { + "xast-util-sitemap": ["."] + } + } +} diff --git a/types/tslint.json b/types/tslint.json new file mode 100644 index 0000000..cefffa1 --- /dev/null +++ b/types/tslint.json @@ -0,0 +1,10 @@ + +{ + "extends": "dtslint/dtslint.json", + "rules": { + "no-redundant-jsdoc": false, + "no-redundant-jsdoc-2": true, + "semicolon": false, + "whitespace": false + } +} From 2417e1063b4fc05dd786c136d9473951b60f636f Mon Sep 17 00:00:00 2001 From: stefanprobst Date: Tue, 2 Feb 2021 19:15:19 +0100 Subject: [PATCH 2/4] Update types/tsconfig.json Co-authored-by: Christian Murphy --- types/tsconfig.json | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/types/tsconfig.json b/types/tsconfig.json index 6f64483..4c0b962 100644 --- a/types/tsconfig.json +++ b/types/tsconfig.json @@ -3,10 +3,7 @@ "module": "es2015", "lib": ["es2015"], "moduleResolution": "node", - "noImplicitAny": true, - "noImplicitThis": true, - "strictNullChecks": true, - "strictFunctionTypes": true, + "strict": true, "noEmit": true, "baseUrl": ".", "paths": { From 8562c0ad4eea50cc7a3c94a81c13a1e20be3775a Mon Sep 17 00:00:00 2001 From: stefanprobst Date: Tue, 2 Feb 2021 19:15:30 +0100 Subject: [PATCH 3/4] Update types/test.ts Co-authored-by: Christian Murphy --- types/test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/types/test.ts b/types/test.ts index c81a003..b7c9b3a 100644 --- a/types/test.ts +++ b/types/test.ts @@ -1,5 +1,6 @@ import {sitemap} from 'xast-util-sitemap' +sitemap() // $ExpectError sitemap([ 'https://example.com', {url: 'https://example.com'}, From 2a1a85a33cbd5a565f4c7fef7f142160a596bd6d Mon Sep 17 00:00:00 2001 From: Stefan Probst Date: Tue, 2 Feb 2021 19:16:48 +0100 Subject: [PATCH 4/4] add types folder to xo ignores --- package.json | 5 ++++- types/index.d.ts | 5 +---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 82041dd..6e1e0a6 100644 --- a/package.json +++ b/package.json @@ -83,7 +83,10 @@ "no-self-compare": "off", "unicorn/prefer-number-properties": "off", "unicorn/prefer-type-error": "off" - } + }, + "ignores": [ + "types" + ] }, "remarkConfig": { "plugins": [ diff --git a/types/index.d.ts b/types/index.d.ts index 1581b04..e666485 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -36,10 +36,7 @@ export interface Entry { * * @see https://github.com/wooorm/bcp-47 */ - alternate?: Record< - string, - string | Omit // eslint-disable-line @typescript-eslint/ban-types - > + alternate?: Record> } /**