Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: moving node imports over to using the node: prefix #729

Merged
merged 3 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"extends": ["@readme/eslint-config", "@readme/eslint-config/typescript"],
"root": true,
"rules": {
"unicorn/prefer-node-protocol": "error"
},
"overrides": [
{
"files": ["bin/**"],
Expand Down
4 changes: 2 additions & 2 deletions bin/build-markdown.js
Original file line number Diff line number Diff line change
@@ -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');

Expand Down
3 changes: 0 additions & 3 deletions packages/api/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{
"rules": {
"@typescript-eslint/no-explicit-any": "off" // @todo fix these eventually
},
"overrides": [
{
"files": ["bin/api"],
Expand Down
18 changes: 9 additions & 9 deletions packages/api/src/codegen/languages/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -62,12 +62,12 @@ export default class TSGenerator extends CodeGeneratorLanguage {
string,
// Operation-level type
| {
body?: any;
metadata?: any;
response?: Record<string, any>;
body?: unknown;
metadata?: unknown;
response?: Record<string, unknown>;
}
// Wholesale collection of `$ref` pointer types
| Record<string, any>
| Record<string, unknown>
>;

usesHTTPMethodRangeInterface = false;
Expand Down Expand Up @@ -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::')) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/api/src/fetcher.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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.');
}
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/storage.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
4 changes: 2 additions & 2 deletions packages/api/test/codegen/languages/typescript.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { TSGeneratorOptions } from '../../../src/codegen/languages/typescript';

import { promises as fs } from 'fs';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no idea why i loaded fs/promises like this. the things you find in small refactors...

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';
Expand Down
4 changes: 2 additions & 2 deletions packages/api/test/fetcher.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
6 changes: 3 additions & 3 deletions packages/api/test/storage.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 0 additions & 2 deletions packages/core/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"rules": {
"@typescript-eslint/no-explicit-any": "off", // @todo fix these eventually

"no-restricted-imports": [
"error",
{
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -121,7 +122,7 @@ export default class APICore {
init.signal = controller.signal;
}

return fetchHar(har as any, {
return fetchHar(har as Har, {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The HAR coming out of @readme/oas-to-har is a HAR but not a full one so we need to cast this.

files: data.files || {},
init,
userAgent: this.userAgent,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/lib/parseResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default async function parseResponse<HTTPStatus extends number = number>(

const responseBody = await response.text();

// eslint-disable-next-line @typescript-eslint/no-explicit-any
let data: any = responseBody;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm guessing unknown won't fly here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfort not

if (isJSON) {
try {
Expand Down
18 changes: 11 additions & 7 deletions packages/core/src/lib/prepareParams.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -41,23 +41,24 @@ function digestParameters(parameters: ParameterObject[]): Record<string, Paramet
}

// https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore#_isempty
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isEmpty(obj: any) {
return [Object, Array].includes((obj || {}).constructor) && !Object.entries(obj || {}).length;
}

function isObject(thing: any) {
function isObject(thing: unknown) {
if (thing instanceof stream.Readable) {
return false;
}

return typeof thing === 'object' && thing !== null && !Array.isArray(thing);
}

function isPrimitive(obj: any) {
function isPrimitive(obj: unknown) {
return obj === null || typeof obj === 'number' || typeof obj === 'string';
}

function merge(src: any, target: any) {
function merge(src: unknown, target: unknown) {
if (Array.isArray(target)) {
// @todo we need to add support for merging array defaults with array body/metadata arguments
return target;
Expand Down Expand Up @@ -187,9 +188,11 @@ export default async function prepareParams(operation: Operation, body?: unknown
const jsonSchemaDefaults = jsonSchema ? getJSONSchemaDefaults(jsonSchema) : {};

const params: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
body?: any;
cookie?: Record<string, string | number | boolean>;
files?: Record<string, Buffer>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
formData?: any;
header?: Record<string, string | number | boolean>;
path?: Record<string, string | number | boolean>;
Expand Down Expand Up @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
12 changes: 6 additions & 6 deletions packages/core/test/lib/prepareParams.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 () => {
Expand All @@ -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);
});
});

Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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);
});
});
});
Expand Down
5 changes: 0 additions & 5 deletions packages/httpsnippet-client-api/.eslintrc

This file was deleted.

2 changes: 2 additions & 0 deletions packages/httpsnippet-client-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
Expand Down Expand Up @@ -241,6 +242,7 @@ const client: Client<APIOptions> = {
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
let body: any;
switch (postData.mimeType) {
case 'application/x-www-form-urlencoded':
Expand Down
6 changes: 3 additions & 3 deletions packages/httpsnippet-client-api/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down