From b42b76e9b4dbda6e91168915f4b5017e11dd66da Mon Sep 17 00:00:00 2001 From: Jon Ursenbach Date: Thu, 14 Sep 2023 16:03:42 -0700 Subject: [PATCH 1/2] refactor(api): cleaning up the api directory now that core is split out --- packages/api/src/bin.ts | 2 +- packages/api/src/{cli => }/codegen/index.ts | 0 .../api/src/{cli => }/codegen/language.ts | 2 +- .../{cli => }/codegen/languages/typescript.ts | 0 .../codegen/languages/typescript/util.ts | 0 packages/api/src/{cli => }/commands/index.ts | 0 .../api/src/{cli => }/commands/install.ts | 0 packages/api/src/{cli => }/fetcher.ts | 0 packages/api/src/{cli => }/lib/prompt.ts | 0 packages/api/src/{cli => }/logger.ts | 0 packages/api/src/{cli => }/storage.ts | 3 +- packages/api/test/__fixtures__/oas.json | 51 ------------------- .../codegen/languages/typescript.test.ts | 20 ++++---- .../languages/typescript/smoketest.test.ts | 2 +- .../languages/typescript/utils.test.ts | 2 +- packages/api/test/{cli => }/fetcher.test.ts | 11 ++-- packages/api/test/{cli => }/storage.test.ts | 13 +++-- 17 files changed, 26 insertions(+), 80 deletions(-) rename packages/api/src/{cli => }/codegen/index.ts (100%) rename packages/api/src/{cli => }/codegen/language.ts (96%) rename packages/api/src/{cli => }/codegen/languages/typescript.ts (100%) rename packages/api/src/{cli => }/codegen/languages/typescript/util.ts (100%) rename packages/api/src/{cli => }/commands/index.ts (100%) rename packages/api/src/{cli => }/commands/install.ts (100%) rename packages/api/src/{cli => }/fetcher.ts (100%) rename packages/api/src/{cli => }/lib/prompt.ts (100%) rename packages/api/src/{cli => }/logger.ts (100%) rename packages/api/src/{cli => }/storage.ts (99%) delete mode 100644 packages/api/test/__fixtures__/oas.json rename packages/api/test/{cli => }/codegen/languages/typescript.test.ts (91%) rename packages/api/test/{cli => }/codegen/languages/typescript/smoketest.test.ts (97%) rename packages/api/test/{cli => }/codegen/languages/typescript/utils.test.ts (97%) rename packages/api/test/{cli => }/fetcher.test.ts (95%) rename packages/api/test/{cli => }/storage.test.ts (96%) diff --git a/packages/api/src/bin.ts b/packages/api/src/bin.ts index bb66f3df..e45a89d0 100644 --- a/packages/api/src/bin.ts +++ b/packages/api/src/bin.ts @@ -1,6 +1,6 @@ import { Command } from 'commander'; -import commands from './cli/commands'; +import commands from './commands'; import * as pkg from './packageInfo'; (async () => { diff --git a/packages/api/src/cli/codegen/index.ts b/packages/api/src/codegen/index.ts similarity index 100% rename from packages/api/src/cli/codegen/index.ts rename to packages/api/src/codegen/index.ts diff --git a/packages/api/src/cli/codegen/language.ts b/packages/api/src/codegen/language.ts similarity index 96% rename from packages/api/src/cli/codegen/language.ts rename to packages/api/src/codegen/language.ts index 314334ac..8e4061c1 100644 --- a/packages/api/src/cli/codegen/language.ts +++ b/packages/api/src/codegen/language.ts @@ -1,7 +1,7 @@ import type Storage from '../storage'; import type Oas from 'oas'; -import { PACKAGE_NAME, PACKAGE_VERSION } from '../../packageInfo'; +import { PACKAGE_NAME, PACKAGE_VERSION } from '../packageInfo'; export interface InstallerOptions { /** diff --git a/packages/api/src/cli/codegen/languages/typescript.ts b/packages/api/src/codegen/languages/typescript.ts similarity index 100% rename from packages/api/src/cli/codegen/languages/typescript.ts rename to packages/api/src/codegen/languages/typescript.ts diff --git a/packages/api/src/cli/codegen/languages/typescript/util.ts b/packages/api/src/codegen/languages/typescript/util.ts similarity index 100% rename from packages/api/src/cli/codegen/languages/typescript/util.ts rename to packages/api/src/codegen/languages/typescript/util.ts diff --git a/packages/api/src/cli/commands/index.ts b/packages/api/src/commands/index.ts similarity index 100% rename from packages/api/src/cli/commands/index.ts rename to packages/api/src/commands/index.ts diff --git a/packages/api/src/cli/commands/install.ts b/packages/api/src/commands/install.ts similarity index 100% rename from packages/api/src/cli/commands/install.ts rename to packages/api/src/commands/install.ts diff --git a/packages/api/src/cli/fetcher.ts b/packages/api/src/fetcher.ts similarity index 100% rename from packages/api/src/cli/fetcher.ts rename to packages/api/src/fetcher.ts diff --git a/packages/api/src/cli/lib/prompt.ts b/packages/api/src/lib/prompt.ts similarity index 100% rename from packages/api/src/cli/lib/prompt.ts rename to packages/api/src/lib/prompt.ts diff --git a/packages/api/src/cli/logger.ts b/packages/api/src/logger.ts similarity index 100% rename from packages/api/src/cli/logger.ts rename to packages/api/src/logger.ts diff --git a/packages/api/src/cli/storage.ts b/packages/api/src/storage.ts similarity index 99% rename from packages/api/src/cli/storage.ts rename to packages/api/src/storage.ts index d2b11b85..71239a26 100644 --- a/packages/api/src/cli/storage.ts +++ b/packages/api/src/storage.ts @@ -7,9 +7,8 @@ import makeDir from 'make-dir'; import ssri from 'ssri'; import validateNPMPackageName from 'validate-npm-package-name'; -import { PACKAGE_VERSION } from '../packageInfo'; - import Fetcher from './fetcher'; +import { PACKAGE_VERSION } from './packageInfo'; export default class Storage { static dir: string; diff --git a/packages/api/test/__fixtures__/oas.json b/packages/api/test/__fixtures__/oas.json deleted file mode 100644 index c6153ee5..00000000 --- a/packages/api/test/__fixtures__/oas.json +++ /dev/null @@ -1,51 +0,0 @@ -{ - "openapi": "3.0.0", - "info": { - "version": "1.0.0", - "title": "Single Path", - "description": "This is a slimmed down single path version of the Petstore definition." - }, - "components": { - "securitySchemes": { - "apiKey": { - "type": "http", - "scheme": "basic" - } - } - }, - "servers": [ - { - "url": "https://httpbin.org" - } - ], - "paths": { - "/pet/:id": { - "put": { - "tags": ["pet"], - "summary": "Update a pet", - "description": "This operation will update a pet in the database.", - "responses": { - "400": { - "description": "Invalid id value" - } - }, - "security": [ - { - "apiKey": [] - } - ] - }, - "get": { - "tags": ["pet"], - "summary": "Find a pet", - "description": "This operation will find a pet in the database.", - "responses": { - "400": { - "description": "Invalid status value" - } - }, - "security": [] - } - } - } -} diff --git a/packages/api/test/cli/codegen/languages/typescript.test.ts b/packages/api/test/codegen/languages/typescript.test.ts similarity index 91% rename from packages/api/test/cli/codegen/languages/typescript.test.ts rename to packages/api/test/codegen/languages/typescript.test.ts index d1c6889b..cebefed4 100644 --- a/packages/api/test/cli/codegen/languages/typescript.test.ts +++ b/packages/api/test/codegen/languages/typescript.test.ts @@ -1,4 +1,4 @@ -import type { TSGeneratorOptions } from '../../../../src/cli/codegen/languages/typescript'; +import type { TSGeneratorOptions } from '../../../src/codegen/languages/typescript'; import { promises as fs } from 'fs'; import path from 'path'; @@ -10,9 +10,9 @@ import Oas from 'oas'; import uniqueTempDir from 'unique-temp-dir'; import { describe, beforeEach, afterEach, it, expect, vi } from 'vitest'; -import TSGenerator from '../../../../src/cli/codegen/languages/typescript'; -import Storage from '../../../../src/cli/storage'; -import * as packageInfo from '../../../../src/packageInfo'; +import TSGenerator from '../../../src/codegen/languages/typescript'; +import * as packageInfo from '../../../src/packageInfo'; +import Storage from '../../../src/storage'; function assertSDKFixture(file: string, fixture: string, opts: TSGeneratorOptions = {}) { return async () => { @@ -23,7 +23,7 @@ function assertSDKFixture(file: string, fixture: string, opts: TSGeneratorOption const actualFiles = await ts.generator(); // Determine if the generated code matches what we've got in our fixture. - const dir = path.resolve(path.join(__dirname, '..', '..', '..', '__fixtures__', 'sdk', fixture)); + const dir = path.resolve(path.join(__dirname, '..', '..', '__fixtures__', 'sdk', fixture)); let expectedFiles: string[]; try { @@ -63,7 +63,7 @@ function assertSDKFixture(file: string, fixture: string, opts: TSGeneratorOption ); // Make sure that we can load the SDK without any TS compilation errors. - const sdk = await import(`../../../__fixtures__/sdk/${fixture}`).then(r => r.default); + const sdk = await import(`../../__fixtures__/sdk/${fixture}`).then(r => r.default); expect(sdk.constructor.name).toBe('SDK'); }; } @@ -172,7 +172,7 @@ describe('typescript', () => { }); it('should be able to make an API request (TS)', async () => { - const sdk = await import('../../../__fixtures__/sdk/simple-ts').then(r => r.default); + const sdk = await import('../../__fixtures__/sdk/simple-ts').then(r => r.default); fetchMock.get('http://petstore.swagger.io/v2/pet/findByStatus?status=available', mockResponse.searchParams); await sdk.findPetsByStatus({ status: ['available'] }).then(({ data, status, headers, res }) => { @@ -184,7 +184,7 @@ describe('typescript', () => { }); it('should be able to make an API request with an `accept` header`', async () => { - const sdk = await import('../../../__fixtures__/sdk/simple-ts').then(r => r.default); + const sdk = await import('../../__fixtures__/sdk/simple-ts').then(r => r.default); fetchMock.get('http://petstore.swagger.io/v2/pet/findByStatus?status=available', mockResponse.headers); await sdk @@ -198,7 +198,7 @@ describe('typescript', () => { }); it('should be able to make an API request (JS + CommonJS)', async () => { - const sdk = await import('../../../__fixtures__/sdk/simple-js-cjs').then(r => r.default); + const sdk = await import('../../__fixtures__/sdk/simple-js-cjs').then(r => r.default); fetchMock.get('http://petstore.swagger.io/v2/pet/findByStatus?status=available', mockResponse.searchParams); await sdk.findPetsByStatus({ status: ['available'] }).then(({ data, status, headers, res }) => { @@ -210,7 +210,7 @@ describe('typescript', () => { }); it('should be able to make an API request (JS + ESM)', async () => { - const sdk = await import('../../../__fixtures__/sdk/simple-js-esm').then(r => r.default); + const sdk = await import('../../__fixtures__/sdk/simple-js-esm').then(r => r.default); fetchMock.get('http://petstore.swagger.io/v2/pet/findByStatus?status=available', mockResponse.searchParams); await sdk.findPetsByStatus({ status: ['available'] }).then(({ data, status, headers, res }) => { diff --git a/packages/api/test/cli/codegen/languages/typescript/smoketest.test.ts b/packages/api/test/codegen/languages/typescript/smoketest.test.ts similarity index 97% rename from packages/api/test/cli/codegen/languages/typescript/smoketest.test.ts rename to packages/api/test/codegen/languages/typescript/smoketest.test.ts index ee9c65be..b06fa000 100644 --- a/packages/api/test/cli/codegen/languages/typescript/smoketest.test.ts +++ b/packages/api/test/codegen/languages/typescript/smoketest.test.ts @@ -20,7 +20,7 @@ import Oas from 'oas'; import OASNormalize from 'oas-normalize'; import { describe, it, expect } from 'vitest'; -import TSGenerator from '../../../../../src/cli/codegen/languages/typescript'; +import TSGenerator from '../../../../src/codegen/languages/typescript'; // These APIs don't have any schemas so they should only be generating an `index.ts`. const APIS_WITHOUT_SCHEMAS = ['poemist.com']; diff --git a/packages/api/test/cli/codegen/languages/typescript/utils.test.ts b/packages/api/test/codegen/languages/typescript/utils.test.ts similarity index 97% rename from packages/api/test/cli/codegen/languages/typescript/utils.test.ts rename to packages/api/test/codegen/languages/typescript/utils.test.ts index 7fe16498..7e1cc579 100644 --- a/packages/api/test/cli/codegen/languages/typescript/utils.test.ts +++ b/packages/api/test/codegen/languages/typescript/utils.test.ts @@ -1,6 +1,6 @@ import { describe, it, expect } from 'vitest'; -import { docblockEscape, generateTypeName, wordWrap } from '../../../../../src/cli/codegen/languages/typescript/util'; +import { docblockEscape, generateTypeName, wordWrap } from '../../../../src/codegen/languages/typescript/util'; describe('ts codegen utils', () => { describe('#docblockEscape', () => { diff --git a/packages/api/test/cli/fetcher.test.ts b/packages/api/test/fetcher.test.ts similarity index 95% rename from packages/api/test/cli/fetcher.test.ts rename to packages/api/test/fetcher.test.ts index 22976622..2317d29c 100644 --- a/packages/api/test/cli/fetcher.test.ts +++ b/packages/api/test/fetcher.test.ts @@ -5,7 +5,7 @@ import loadSpec from '@api/test-utils/load-spec'; import fetchMock from 'fetch-mock'; import { describe, beforeAll, it, expect } from 'vitest'; -import Fetcher from '../../src/cli/fetcher'; +import Fetcher from '../src/fetcher'; let readmeSpec; @@ -200,16 +200,15 @@ describe('fetcher', () => { const fetcher = new Fetcher(require.resolve('@readme/oas-examples/3.0/json/readme.json')); const res = await fetcher.load(); - expect(res.paths['/api-specification'].get.parameters).toBeDereferenced(); + expect(res.paths?.['/api-specification']?.get?.parameters).toBeDereferenced(); }); it('should be able to handle a relative path', async () => { - const fetcher = new Fetcher('../api/test/__fixtures__/oas.json'); + const fetcher = new Fetcher('../test-utils/definitions/simple.json'); await expect(fetcher.load()).resolves.toHaveProperty('info', { version: '1.0.0', - title: 'Single Path', - description: 'This is a slimmed down single path version of the Petstore definition.', + title: 'Swagger Petstore', }); }); @@ -218,7 +217,7 @@ describe('fetcher', () => { const fetcher = new Fetcher(file); const res = await fetcher.load(); - expect(res.paths['/api-specification'].get.parameters).toBeDereferenced(); + expect(res.paths?.['/api-specification']?.get?.parameters).toBeDereferenced(); }); }); }); diff --git a/packages/api/test/cli/storage.test.ts b/packages/api/test/storage.test.ts similarity index 96% rename from packages/api/test/cli/storage.test.ts rename to packages/api/test/storage.test.ts index 9dad3aca..21eb6d00 100644 --- a/packages/api/test/cli/storage.test.ts +++ b/packages/api/test/storage.test.ts @@ -9,8 +9,8 @@ import fetchMock from 'fetch-mock'; import uniqueTempDir from 'unique-temp-dir'; import { describe, beforeAll, beforeEach, afterEach, it, expect } from 'vitest'; -import Storage from '../../src/cli/storage'; -import { PACKAGE_VERSION } from '../../src/packageInfo'; +import { PACKAGE_VERSION } from '../src/packageInfo'; +import Storage from '../src/storage'; let petstoreSimple; @@ -287,7 +287,7 @@ describe('storage', () => { }); it('should be able to handle a relative path', async () => { - const file = '../api/test/__fixtures__/oas.json'; + const file = '../test-utils/definitions/simple.json'; const storage = new Storage(file, 'relative-path'); expect(storage.isInLockfile()).toBe(false); @@ -297,14 +297,13 @@ describe('storage', () => { expect(storage.isInLockfile()).toBe(true); expect(storage.getAPIDefinition().info).toStrictEqual({ version: '1.0.0', - title: 'Single Path', - description: 'This is a slimmed down single path version of the Petstore definition.', + title: 'Swagger Petstore', }); expect(storage.getFromLockfile()).toStrictEqual({ identifier: 'relative-path', source: file, - integrity: 'sha512-Qi5BB9mfzkRqHe0rMvjRmKunNJ21zILF0e4KzYKi2hMw+zLfi2idmmn0lAngdRwqYdGIKTXUWhJNn0i3iDqUUg==', + integrity: 'sha512-Ey83iRY4tY7JCCUI03eqfNb8YsxKlBdLILXcLDBbxZ1a2X/YfTspCTA8mLp6aaG9gRSyNMhI1hmtSlduWZw8RA==', installerVersion: PACKAGE_VERSION, }); }); @@ -337,7 +336,7 @@ describe('storage', () => { }); it('should error if definition is not a valid openapi file', async () => { - await expect(new Storage(require.resolve('../../package.json'), 'invalid').load()).rejects.toThrow( + await expect(new Storage(require.resolve('../package.json'), 'invalid').load()).rejects.toThrow( "Sorry, that doesn't look like a valid OpenAPI definition.", ); }); From 273f82b50751a6d31a7a4f9428e3aa2ded705b5e Mon Sep 17 00:00:00 2001 From: Jon Ursenbach Date: Thu, 14 Sep 2023 16:52:52 -0700 Subject: [PATCH 2/2] feat: moving node imports over to using the `node:` prefix --- .eslintrc | 3 +++ bin/build-markdown.js | 4 ++-- packages/api/.eslintrc | 3 --- .../api/src/codegen/languages/typescript.ts | 18 +++++++++--------- packages/api/src/fetcher.ts | 6 +++--- packages/api/src/storage.ts | 4 ++-- .../test/codegen/languages/typescript.test.ts | 4 ++-- packages/api/test/fetcher.test.ts | 4 ++-- packages/api/test/storage.test.ts | 6 +++--- packages/core/.eslintrc | 2 -- packages/core/src/index.ts | 3 ++- packages/core/src/lib/parseResponse.ts | 1 + packages/core/src/lib/prepareParams.ts | 18 +++++++++++------- packages/core/test/index.test.ts | 2 +- packages/core/test/lib/prepareParams.test.ts | 12 ++++++------ packages/httpsnippet-client-api/.eslintrc | 5 ----- packages/httpsnippet-client-api/src/index.ts | 2 ++ .../httpsnippet-client-api/test/index.test.ts | 6 +++--- 18 files changed, 52 insertions(+), 51 deletions(-) delete mode 100644 packages/httpsnippet-client-api/.eslintrc 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';