diff --git a/.eslintrc b/.eslintrc index 391abb7a..cac942a0 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,6 +1,9 @@ { "extends": ["@readme/eslint-config", "@readme/eslint-config/typescript"], "root": true, + "rules": { + "unicorn/prefer-node-protocol": "error" + }, "overrides": [ { "files": ["bin/**"], diff --git a/bin/build-markdown.js b/bin/build-markdown.js index 93f2ed90..2c7dcdc9 100644 --- a/bin/build-markdown.js +++ b/bin/build-markdown.js @@ -1,5 +1,5 @@ -const fs = require('fs'); -const path = require('path'); +const fs = require('node:fs'); +const path = require('node:path'); const readme = fs.readFileSync('README.md', 'utf-8'); diff --git a/packages/api/.eslintrc b/packages/api/.eslintrc index a0f07c94..9a22ff04 100644 --- a/packages/api/.eslintrc +++ b/packages/api/.eslintrc @@ -1,7 +1,4 @@ { - "rules": { - "@typescript-eslint/no-explicit-any": "off" // @todo fix these eventually - }, "overrides": [ { "files": ["bin/api"], diff --git a/packages/api/src/codegen/languages/typescript.ts b/packages/api/src/codegen/languages/typescript.ts index 194596f1..9f399c2c 100644 --- a/packages/api/src/codegen/languages/typescript.ts +++ b/packages/api/src/codegen/languages/typescript.ts @@ -13,8 +13,8 @@ import type { } from 'ts-morph'; import type { PackageJson } from 'type-fest'; -import fs from 'fs'; -import path from 'path'; +import fs from 'node:fs'; +import path from 'node:path'; import execa from 'execa'; import setWith from 'lodash.setwith'; @@ -62,12 +62,12 @@ export default class TSGenerator extends CodeGeneratorLanguage { string, // Operation-level type | { - body?: any; - metadata?: any; - response?: Record; + body?: unknown; + metadata?: unknown; + response?: Record; } // Wholesale collection of `$ref` pointer types - | Record + | Record >; usesHTTPMethodRangeInterface = false; @@ -807,7 +807,7 @@ sdk.server('https://eu.api.example.com/v14');`), .reduce((prev, next) => Object.assign(prev, next)); return Object.entries(res) - .map(([paramType, schema]: [string, string | unknown]) => { + .map(([paramType, schema]: [string, string | SchemaObject]) => { let typeName; if (typeof schema === 'string' && schema.startsWith('::convert::')) { @@ -816,7 +816,7 @@ sdk.server('https://eu.api.example.com/v14');`), typeName = schema.replace('::convert::', ''); } else { typeName = generateTypeName(operationId, paramType, 'param'); - this.addSchemaToExport(schema, typeName, `${generateTypeName(operationId)}.${paramType}`); + this.addSchemaToExport(schema as SchemaObject, typeName, `${generateTypeName(operationId)}.${paramType}`); } return { @@ -901,7 +901,7 @@ sdk.server('https://eu.api.example.com/v14');`), * Add a given schema into our schema dataset that we'll be be exporting as types. * */ - addSchemaToExport(schema: any, typeName: string, pointer: string) { + addSchemaToExport(schema: SchemaObject, typeName: string, pointer: string) { if (this.types.has(typeName)) { return; } diff --git a/packages/api/src/fetcher.ts b/packages/api/src/fetcher.ts index cd5d6040..f68171f4 100644 --- a/packages/api/src/fetcher.ts +++ b/packages/api/src/fetcher.ts @@ -1,7 +1,7 @@ import type { OASDocument } from 'oas/dist/rmoas.types'; -import fs from 'fs'; -import path from 'path'; +import fs from 'node:fs'; +import path from 'node:path'; import OpenAPIParser from '@readme/openapi-parser'; import yaml from 'js-yaml'; @@ -114,7 +114,7 @@ export default class Fetcher { }); } - static validate(json: any) { + static validate(json: OASDocument) { if (json.swagger) { throw new Error('Sorry, this module only supports OpenAPI definitions.'); } diff --git a/packages/api/src/storage.ts b/packages/api/src/storage.ts index 71239a26..b3896e41 100644 --- a/packages/api/src/storage.ts +++ b/packages/api/src/storage.ts @@ -1,7 +1,7 @@ import type { OASDocument } from 'oas/dist/rmoas.types'; -import fs from 'fs'; -import path from 'path'; +import fs from 'node:fs'; +import path from 'node:path'; import makeDir from 'make-dir'; import ssri from 'ssri'; diff --git a/packages/api/test/codegen/languages/typescript.test.ts b/packages/api/test/codegen/languages/typescript.test.ts index cebefed4..aadb1546 100644 --- a/packages/api/test/codegen/languages/typescript.test.ts +++ b/packages/api/test/codegen/languages/typescript.test.ts @@ -1,7 +1,7 @@ import type { TSGeneratorOptions } from '../../../src/codegen/languages/typescript'; -import { promises as fs } from 'fs'; -import path from 'path'; +import fs from 'node:fs/promises'; +import path from 'node:path'; import { responses as mockResponse } from '@api/test-utils/fetch-mock'; import loadSpec from '@api/test-utils/load-spec'; diff --git a/packages/api/test/fetcher.test.ts b/packages/api/test/fetcher.test.ts index 2317d29c..5acf7be9 100644 --- a/packages/api/test/fetcher.test.ts +++ b/packages/api/test/fetcher.test.ts @@ -1,5 +1,5 @@ -import assert from 'assert'; -import fs from 'fs/promises'; +import assert from 'node:assert'; +import fs from 'node:fs/promises'; import loadSpec from '@api/test-utils/load-spec'; import fetchMock from 'fetch-mock'; diff --git a/packages/api/test/storage.test.ts b/packages/api/test/storage.test.ts index 21eb6d00..a95472d8 100644 --- a/packages/api/test/storage.test.ts +++ b/packages/api/test/storage.test.ts @@ -1,8 +1,8 @@ import type { OASDocument } from 'oas/dist/rmoas.types'; -import assert from 'assert'; -import fs from 'fs/promises'; -import path from 'path'; +import assert from 'node:assert'; +import fs from 'node:fs/promises'; +import path from 'node:path'; import loadSpec from '@api/test-utils/load-spec'; import fetchMock from 'fetch-mock'; diff --git a/packages/core/.eslintrc b/packages/core/.eslintrc index 7ea05f4f..fe5cd651 100644 --- a/packages/core/.eslintrc +++ b/packages/core/.eslintrc @@ -1,7 +1,5 @@ { "rules": { - "@typescript-eslint/no-explicit-any": "off", // @todo fix these eventually - "no-restricted-imports": [ "error", { diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index e7dd1429..b453db37 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -1,3 +1,4 @@ +import type { Har } from 'har-format'; import type Oas from 'oas'; import type { Operation } from 'oas'; import type { HttpMethods } from 'oas/dist/rmoas.types'; @@ -121,7 +122,7 @@ export default class APICore { init.signal = controller.signal; } - return fetchHar(har as any, { + return fetchHar(har as Har, { files: data.files || {}, init, userAgent: this.userAgent, diff --git a/packages/core/src/lib/parseResponse.ts b/packages/core/src/lib/parseResponse.ts index 48095fff..5a7e6c69 100644 --- a/packages/core/src/lib/parseResponse.ts +++ b/packages/core/src/lib/parseResponse.ts @@ -8,6 +8,7 @@ export default async function parseResponse( const responseBody = await response.text(); + // eslint-disable-next-line @typescript-eslint/no-explicit-any let data: any = responseBody; if (isJSON) { try { diff --git a/packages/core/src/lib/prepareParams.ts b/packages/core/src/lib/prepareParams.ts index c4ef105b..856d6aee 100644 --- a/packages/core/src/lib/prepareParams.ts +++ b/packages/core/src/lib/prepareParams.ts @@ -1,10 +1,10 @@ -import type { ReadStream } from 'fs'; +import type { ReadStream } from 'node:fs'; import type { Operation } from 'oas'; import type { ParameterObject, SchemaObject } from 'oas/dist/rmoas.types'; -import fs from 'fs'; -import path from 'path'; -import stream from 'stream'; +import fs from 'node:fs'; +import path from 'node:path'; +import stream from 'node:stream'; import caseless from 'caseless'; import DatauriParser from 'datauri/parser'; @@ -41,11 +41,12 @@ function digestParameters(parameters: ParameterObject[]): Record; files?: Record; + // eslint-disable-next-line @typescript-eslint/no-explicit-any formData?: any; header?: Record; path?: Record; @@ -329,6 +332,7 @@ export default async function prepareParams(operation: Operation, body?: unknown if (!('query' in params)) params.query = {}; Object.entries(digestedParameters).forEach(([paramName, param]) => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any let value: any; let metadataHeaderParam; if (typeof metadata === 'object' && !isEmpty(metadata)) { diff --git a/packages/core/test/index.test.ts b/packages/core/test/index.test.ts index 57598803..e8ee0c11 100644 --- a/packages/core/test/index.test.ts +++ b/packages/core/test/index.test.ts @@ -1,4 +1,4 @@ -import assert from 'assert'; +import assert from 'node:assert'; import { responses as mockResponse } from '@api/test-utils/fetch-mock'; import loadSpec from '@api/test-utils/load-spec'; diff --git a/packages/core/test/lib/prepareParams.test.ts b/packages/core/test/lib/prepareParams.test.ts index 0838cd29..3b4b49f6 100644 --- a/packages/core/test/lib/prepareParams.test.ts +++ b/packages/core/test/lib/prepareParams.test.ts @@ -1,4 +1,4 @@ -import fs from 'fs'; +import fs from 'node:fs'; import payloadExamples from '@api/test-utils/definitions/payloads.json'; import loadSpec from '@api/test-utils/load-spec'; @@ -169,7 +169,7 @@ describe('#prepareParams', () => { const res = await prepareParams(operation, body); expect(res.body).toContain('data:image/png;name=owlbert.png;base64,'); - expect(res.files['owlbert.png']).toBeInstanceOf(Buffer); + expect(res.files?.['owlbert.png']).toBeInstanceOf(Buffer); }); it('should support a file stream payload', async () => { @@ -178,7 +178,7 @@ describe('#prepareParams', () => { const res = await prepareParams(operation, body); expect(res.body).toContain('data:image/png;name=owlbert.png;base64,'); - expect(res.files['owlbert.png']).toBeInstanceOf(Buffer); + expect(res.files?.['owlbert.png']).toBeInstanceOf(Buffer); }); }); @@ -191,7 +191,7 @@ describe('#prepareParams', () => { const res = await prepareParams(operation, body); expect(res.body.documentFile).toContain('data:application/json;name=readme.json;base64,'); - expect(res.files['readme.json']).toBeInstanceOf(Buffer); + expect(res.files?.['readme.json']).toBeInstanceOf(Buffer); }); it('should handle when the file path is relative', async () => { @@ -202,7 +202,7 @@ describe('#prepareParams', () => { const res = await prepareParams(operation, body); expect(res.body.documentFile).toContain('data:image/png;name=owlbert.png;base64,'); - expect(res.files['owlbert.png']).toBeInstanceOf(Buffer); + expect(res.files?.['owlbert.png']).toBeInstanceOf(Buffer); }); it('should handle a multipart body when a property is a file stream', async () => { @@ -213,7 +213,7 @@ describe('#prepareParams', () => { const res = await prepareParams(operation, body); expect(res.body.documentFile).toContain('data:image/png;name=owlbert.png;base64,'); - expect(res.files['owlbert.png']).toBeInstanceOf(Buffer); + expect(res.files?.['owlbert.png']).toBeInstanceOf(Buffer); }); }); }); diff --git a/packages/httpsnippet-client-api/.eslintrc b/packages/httpsnippet-client-api/.eslintrc deleted file mode 100644 index 8b8e4250..00000000 --- a/packages/httpsnippet-client-api/.eslintrc +++ /dev/null @@ -1,5 +0,0 @@ -{ - "rules": { - "@typescript-eslint/no-explicit-any": "off" - } -} diff --git a/packages/httpsnippet-client-api/src/index.ts b/packages/httpsnippet-client-api/src/index.ts index d42c35a3..b30eb53d 100644 --- a/packages/httpsnippet-client-api/src/index.ts +++ b/packages/httpsnippet-client-api/src/index.ts @@ -10,6 +10,7 @@ import stringifyObject from 'stringify-object'; const { matchesMimeType } = utils; +// eslint-disable-next-line @typescript-eslint/no-explicit-any function stringify(obj: any, opts = {}) { return stringifyObject(obj, { indent: ' ', ...opts }); } @@ -241,6 +242,7 @@ const client: Client = { } } + // eslint-disable-next-line @typescript-eslint/no-explicit-any let body: any; switch (postData.mimeType) { case 'application/x-www-form-urlencoded': diff --git a/packages/httpsnippet-client-api/test/index.test.ts b/packages/httpsnippet-client-api/test/index.test.ts index 6faa4d85..a0dd6f15 100644 --- a/packages/httpsnippet-client-api/test/index.test.ts +++ b/packages/httpsnippet-client-api/test/index.test.ts @@ -2,9 +2,9 @@ import type { HarRequest, Request } from '@readme/httpsnippet'; import type { Client } from '@readme/httpsnippet/dist/targets/targets'; import type { OASDocument } from 'oas/dist/rmoas.types'; -import { readdirSync } from 'fs'; -import fs from 'fs/promises'; -import path from 'path'; +import { readdirSync } from 'node:fs'; +import fs from 'node:fs/promises'; +import path from 'node:path'; import { HTTPSnippet, addTargetClient } from '@readme/httpsnippet'; import readme from '@readme/oas-examples/3.0/json/readme.json';