From bf4bc7550e5364f133b84e01e1a562aed35fed06 Mon Sep 17 00:00:00 2001 From: William Killerud Date: Tue, 14 May 2024 16:21:52 +0200 Subject: [PATCH] fix: update dependency typings --- lib/podlet.js | 8 +---- package.json | 7 +++-- tests/podlet.test.js | 73 +++++++++++++++++++++++++++----------------- 3 files changed, 50 insertions(+), 38 deletions(-) diff --git a/lib/podlet.js b/lib/podlet.js index 87d8509a..6c49fd72 100644 --- a/lib/podlet.js +++ b/lib/podlet.js @@ -5,20 +5,15 @@ import { HttpIncoming, - // @ts-ignore pathnameBuilder, AssetCss, AssetJs, } from '@podium/utils'; -// @ts-ignore import * as schema from '@podium/schemas'; import Metrics from '@metrics/client'; -// @ts-ignore import abslog from 'abslog'; -// @ts-ignore import objobj from 'objobj'; import * as utils from '@podium/utils'; -// @ts-ignore import Proxy from '@podium/proxy'; import { join, dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; @@ -31,7 +26,6 @@ const pkgJson = fs.readFileSync( ); const pkg = JSON.parse(pkgJson); -// @ts-ignore const { template } = utils; /** @@ -47,7 +41,7 @@ const { template } = utils; * @property {string} [fallback] - path where the podlet fallback HTML markup is served from (default '/fallback') * @property {boolean} [development] - a boolean flag that, when true, enables additional development setup (default false) * @property {Console | AbsLogger} [logger] - a logger to use when provided. Can be the console object if console logging is desired but can also be any Log4j compatible logging object as well. Nothing is logged if no logger is provided. (default null) - * @property {import("@podium/proxy").default.PodiumProxyOptions} [proxy] - options that can be provided to configure the @podium/proxy instance used by the podlet. See that module for details. + * @property {import("@podium/proxy").PodiumProxyOptions} [proxy] - options that can be provided to configure the @podium/proxy instance used by the podlet. See that module for details. * * @typedef {{ debug: 'true' | 'false', locale: string, deviceType: string, requestedBy: string, mountOrigin: string, mountPathname: string, publicPathname: string }} PodletContext * @typedef {{ as?: string | false | null, crossorigin?: string | null | boolean, disabled?: boolean | '' | null, hreflang?: string | false | null, title?: string | false | null, media?: string | false | null, rel?: string | false | null, type?: string | false | null, value: string | false | null, data?: Array<{ key: string; value: string }>, strategy?: "beforeInteractive" | "afterInteractive" | "lazy", scope?: "content" | "fallback" | "all", [key: string]: any }} AssetCssLike diff --git a/package.json b/package.json index 3665affb..b94b1141 100644 --- a/package.json +++ b/package.json @@ -26,13 +26,13 @@ "scripts": { "lint": "eslint .", "lint:fix": "eslint --fix .", - "test:snapshots": "tap --snapshot --disable-coverage --allow-empty-coverage && tsc --project tsconfig.test.json", + "test": "tap --disable-coverage --allow-empty-coverage && tsc --project tsconfig.test.json", "types": "tsc --declaration --emitDeclarationOnly && ./fixup.sh" }, "dependencies": { "@metrics/client": "2.5.2", - "@podium/proxy": "5.0.14", - "@podium/schemas": "5.0.1", + "@podium/proxy": "5.0.16", + "@podium/schemas": "5.0.2", "@podium/utils": "5.0.6", "abslog": "2.4.4", "ajv": "8.13.0", @@ -48,6 +48,7 @@ "@semantic-release/npm": "11.0.3", "@semantic-release/release-notes-generator": "12.1.0", "@types/node": "^20.10.3", + "@types/readable-stream": "4.0.14", "eslint": "8.57.0", "eslint-config-airbnb-base": "15.0.0", "eslint-config-prettier": "9.1.0", diff --git a/tests/podlet.test.js b/tests/podlet.test.js index 30de53e6..e600d4bd 100644 --- a/tests/podlet.test.js +++ b/tests/podlet.test.js @@ -42,7 +42,11 @@ const SIMPLE_RES = { */ class FakeHttpServer { - constructor({ podlet, process } = {}, onRequest) { + /** + * @param {{ podlet: Podlet, process?: { proxy: boolean } }} options + * @param {(incoming: import('@podium/utils').HttpIncoming) => void} [onRequest] + */ + constructor({ podlet, process }, onRequest) { this.app = http.createServer(async (req, res) => { const incoming = new HttpIncoming(req, res); const reslt = await podlet.process(incoming, process); @@ -68,15 +72,17 @@ class FakeHttpServer { } close() { - return new Promise((resolve, reject) => { - this.server.close((err) => { - if (err) { - reject(err); - } else { - resolve(); - } - }); - }); + return /** @type {Promise} */ ( + new Promise((resolve, reject) => { + this.server.close((err) => { + if (err) { + reject(err); + } else { + resolve(); + } + }); + }) + ); } get(options = {}) { @@ -109,14 +115,20 @@ class FakeHttpServer { } class FakeExpressServer { + /** + * @param {Podlet} podlet + * @param {(incoming: import('@podium/utils').HttpIncoming) => void} [onRequest] + * @param {{ path: string; handler: import('express').Handler } | import('express').Handler} [onContentRoute] + * @param {import('express').Handler} [onFallbackRoute] + */ constructor(podlet, onRequest, onContentRoute, onFallbackRoute) { this.app = express(); this.app.use(podlet.middleware()); if (onContentRoute) { - if (onContentRoute.path && onContentRoute.handler) { - this.app.get(onContentRoute.path, onContentRoute.handler); - } else { + if (typeof onContentRoute === 'function') { this.app.get(podlet.content({ prefix: true }), onContentRoute); + } else { + this.app.get(onContentRoute.path, onContentRoute.handler); } } if (onFallbackRoute) @@ -143,15 +155,17 @@ class FakeExpressServer { } close() { - return new Promise((resolve, reject) => { - this.server.close((err) => { - if (err) { - reject(err); - } else { - resolve(); - } - }); - }); + return /** @type {Promise} */ ( + new Promise((resolve, reject) => { + this.server.close((err) => { + if (err) { + reject(err); + } else { + resolve(); + } + }); + }) + ); } get(options = {}) { @@ -167,6 +181,7 @@ class FakeExpressServer { res.on('end', () => { resolve({ headers: res.headers, + // @ts-ignore response: opts.raw ? chunks.join('') : JSON.parse(chunks.join('')), @@ -203,6 +218,7 @@ tap.test('Podlet() - object tag - should be PodiumPodlet', (t) => { tap.test('Podlet() - no value given to "name" argument - should throw', (t) => { t.throws(() => { + // @ts-expect-error Testing bad input const podlet = new Podlet({ version: 'v1.0.0', pathname: '/' }); // eslint-disable-line no-unused-vars }, 'The value, "", for the required argument "name" on the Podlet constructor is not defined or not valid.'); t.end(); @@ -212,7 +228,6 @@ tap.test( 'Podlet() - invalid value given to "name" argument - should throw', (t) => { t.throws(() => { - // Yeah; silly formatting, but only way to please ESLint const options = { name: 'foo bar', version: 'v1.0.0', @@ -228,6 +243,7 @@ tap.test( 'Podlet() - no value given to "version" argument - should throw', (t) => { t.throws(() => { + // @ts-expect-error Testing bad input const podlet = new Podlet({ name: 'foo', pathname: '/' }); // eslint-disable-line no-unused-vars }, 'The value, "", for the required argument "version" on the Podlet constructor is not defined or not valid.'); t.end(); @@ -238,12 +254,12 @@ tap.test( 'Podlet() - invalid value given to "version" argument - should throw', (t) => { t.throws(() => { - // Yeah; silly formatting, but only way to please ESLint const options = { name: 'foo', version: true, pathname: '/', }; + // @ts-expect-error Testing bad input const podlet = new Podlet(options); // eslint-disable-line no-unused-vars }, 'The value, "true", for the required argument "version" on the Podlet constructor is not defined or not valid.'); t.end(); @@ -254,6 +270,7 @@ tap.test( 'Podlet() - no value given to "pathname" argument - should throw', (t) => { t.throws(() => { + // @ts-expect-error Testing bad input const podlet = new Podlet({ name: 'foo', version: 'v1.0.0' }); // eslint-disable-line no-unused-vars }, 'The value, "", for the required argument "pathname" on the Podlet constructor is not defined or not valid.'); t.end(); @@ -264,7 +281,6 @@ tap.test( 'Podlet() - invalid value given to "pathname" argument - should throw', (t) => { t.throws(() => { - // Yeah; silly formatting, but only way to please ESLint const options = { name: 'foo', version: 'v1.0.0', @@ -280,7 +296,6 @@ tap.test( 'Podlet() - invalid value given to "manifest" argument - should throw', (t) => { t.throws(() => { - // Yeah; silly formatting, but only way to please ESLint const options = { manifest: 'æ / ø', ...DEFAULT_OPTIONS }; const podlet = new Podlet(options); // eslint-disable-line no-unused-vars }, 'The value, "æ / ø", for the optional argument "manifest" on the Podlet constructor is not valid.'); @@ -292,7 +307,6 @@ tap.test( 'Podlet() - invalid value given to "content" argument - should throw', (t) => { t.throws(() => { - // Yeah; silly formatting, but only way to please ESLint const options = { content: 'æ / ø', ...DEFAULT_OPTIONS }; const podlet = new Podlet(options); // eslint-disable-line no-unused-vars }, 'The value, "æ / ø", for the optional argument "content" on the Podlet constructor is not valid.'); @@ -304,7 +318,6 @@ tap.test( 'Podlet() - invalid value given to "fallback" argument - should throw', (t) => { t.throws(() => { - // Yeah; silly formatting, but only way to please ESLint const options = { fallback: 'æ / ø', ...DEFAULT_OPTIONS }; const podlet = new Podlet(options); // eslint-disable-line no-unused-vars }, 'The value, "æ / ø", for the optional argument "fallback" on the Podlet constructor is not valid.'); @@ -677,6 +690,7 @@ tap.test( tap.test('.css() - call method with no arguments - should throw', (t) => { const podlet = new Podlet(DEFAULT_OPTIONS); t.throws(() => { + // @ts-expect-error Testing bad input podlet.css(); }, 'Value for argument variable "value", "undefined", is not valid'); t.end(); @@ -937,6 +951,7 @@ tap.test( { value: '/foo/bar', data: { + // @ts-expect-error Testing older input bar: 'a', foo: 'b', }, @@ -1435,6 +1450,7 @@ tap.test( tap.test('.proxy() - no arguments - should throw', (t) => { const podlet = new Podlet(DEFAULT_OPTIONS); t.throws(() => { + // @ts-expect-error Testing bad input podlet.proxy(); }, 'Value on argument variable "target", "null", is not valid'); t.end(); @@ -1491,6 +1507,7 @@ tap.test( tap.test('.view() - set a non valid argument value - should throw', (t) => { const podlet = new Podlet(DEFAULT_OPTIONS); t.throws(() => { + // @ts-expect-error Testing bad input podlet.view('test'); }, 'Value on argument variable "template" must be a function'); t.end();