Skip to content

Commit 0a700ea

Browse files
authored
Merge pull request #1523 from drwpow/chore/enforce-type
Chore: Enforce type imports
2 parents c87275a + 9fbcabf commit 0a700ea

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+75
-64
lines changed

Diff for: .eslintrc.cjs

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ module.exports = {
1818
],
1919
rules: {
2020
"@typescript-eslint/consistent-indexed-object-style": "off", // sometimes naming keys is more user-friendly
21+
"@typescript-eslint/consistent-type-imports": [
22+
"error",
23+
{ prefer: "type-imports", fixStyle: "inline-type-imports" },
24+
],
2125
"@typescript-eslint/no-dynamic-delete": "off", // delete is OK
2226
"@typescript-eslint/no-non-null-assertion": "off", // this is better than "as"
2327
"@typescript-eslint/no-shadow": "error",

Diff for: docs/6.x/advanced.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ And the magic that produces this would live in a `test/utils.ts` file that can b
8888
::: code-group [test/utils.ts]
8989

9090
```ts
91-
import { paths } from "./api/v1/my-schema"; // generated by openapi-typescript
91+
import type { paths } from "./api/v1"; // generated by openapi-typescript
9292

9393
// Settings
9494
// ⚠️ Important: change this! This prefixes all URLs

Diff for: docs/6.x/cli.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export interface paths {
7171
Which means your type lookups also have to match the exact URL:
7272

7373
```ts
74-
import { paths } from "./my-schema";
74+
import type { paths } from "./api/v1";
7575

7676
const url = `/user/${id}`;
7777
type UserResponses = paths["/user/{user_id}"]["responses"];
@@ -80,7 +80,7 @@ type UserResponses = paths["/user/{user_id}"]["responses"];
8080
But when `--path-params-as-types` is enabled, you can take advantage of dynamic lookups like so:
8181

8282
```ts
83-
import { paths } from "./my-schema";
83+
import type { paths } from "./api/v1";
8484

8585
const url = `/user/${id}`;
8686
type UserResponses = paths[url]["responses"]; // automatically matches `paths['/user/{user_id}']`

Diff for: docs/6.x/introduction.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Be sure to [validate your schemas](https://redocly.com/docs/cli/commands/lint/)!
5959
Then, import schemas from the generated file like so:
6060

6161
```ts
62-
import { paths, components } from "./path/to/my/schema"; // <- generated by openapi-typescript
62+
import type { paths, components } from "./api/v1"; // generated by openapi-typescript
6363

6464
// Schema Obj
6565
type MyType = components["schemas"]["MyType"];

Diff for: docs/cli.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export interface paths {
115115
Which means your type lookups also have to match the exact URL:
116116

117117
```ts
118-
import { paths } from "./my-schema";
118+
import type{ paths } from "./api/v1";
119119
120120
const url = `/user/${id}`;
121121
type UserResponses = paths["/user/{user_id}"]["responses"];
@@ -124,7 +124,7 @@ type UserResponses = paths["/user/{user_id}"]["responses"];
124124
But when `--path-params-as-types` is enabled, you can take advantage of dynamic lookups like so:
125125

126126
```ts
127-
import { paths } from "./my-schema";
127+
import type { paths } from "./api/v1";
128128

129129
const url = `/user/${id}`;
130130
type UserResponses = paths[url]["responses"]; // automatically matches `paths['/user/{user_id}']`

Diff for: docs/examples.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ And the magic that produces this would live in a `test/utils.ts` file that can b
136136
::: code-group [test/utils.ts]
137137

138138
```ts
139-
import { paths } from "./api/v1/my-schema"; // generated by openapi-typescript
139+
import type { paths } from "./api/v1"; // generated by openapi-typescript
140140

141141
// Settings
142142
// ⚠️ Important: change this! This prefixes all URLs

Diff for: docs/introduction.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ npx openapi-typescript https://myapi.dev/api/v1/openapi.yaml -o ./path/to/my/sch
5959
Then in your TypeScript project, import types where needed:
6060

6161
```ts
62-
import { paths, components } from "./path/to/my/schema"; // <- generated by openapi-typescript
62+
import type { paths, components } from "./api/v1"; // generated by openapi-typescript
6363

6464
// Schema Obj
6565
type MyType = components["schemas"]["MyType"];

Diff for: docs/openapi-fetch/examples.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Here’s how it can be handled using [Nano Stores](https://github.com/nanostores
1616
// src/lib/api/index.ts
1717
import { atom, computed } from "nanostores";
1818
import createClient from "openapi-fetch";
19-
import { paths } from "./v1";
19+
import type { paths } from "./api/v1";
2020

2121
export const authToken = atom<string | undefined>();
2222
someAuthMethod().then((newToken) => authToken.set(newToken));
@@ -47,7 +47,7 @@ You can also use [proxies](https://developer.mozilla.org/en-US/docs/Web/JavaScri
4747
```ts
4848
// src/lib/api/index.ts
4949
import createClient from "openapi-fetch";
50-
import { paths } from "./v1";
50+
import type { paths } from "./api/v1";
5151

5252
let authToken: string | undefined = undefined;
5353
someAuthMethod().then((newToken) => (authToken = newToken));
@@ -80,7 +80,7 @@ You can also use a [getter](https://developer.mozilla.org/en-US/docs/Web/JavaScr
8080
```ts
8181
// src/lib/api/index.ts
8282
import createClient from "openapi-fetch";
83-
import { paths } from "./v1";
83+
import type { paths } from "./api/v1";
8484

8585
let authToken: string | undefined = undefined;
8686
someAuthMethod().then((newToken) => (authToken = newToken));

Diff for: docs/openapi-fetch/index.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ The syntax is inspired by popular libraries like react-query or Apollo client, b
1818

1919
```ts
2020
import createClient from "openapi-fetch";
21-
import { paths } from "./v1"; // generated from openapi-typescript
21+
import type { paths } from "./api/v1"; // generated by openapi-typescript
2222

2323
const { GET, PUT } = createClient<paths>({ baseUrl: "https://myapi.dev/v1/" });
2424

@@ -102,7 +102,7 @@ The best part about using openapi-fetch over oldschool codegen is no documentati
102102

103103
```ts
104104
import createClient from "openapi-fetch";
105-
import { paths } from "./v1";
105+
import type { paths } from "./api/v1";
106106

107107
const { GET, PUT } = createClient<paths>({ baseUrl: "https://myapi.dev/v1/" });
108108

Diff for: packages/openapi-fetch/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ The syntax is inspired by popular libraries like react-query or Apollo client, b
1414

1515
```ts
1616
import createClient from "openapi-fetch";
17-
import { paths } from "./v1"; // generated from openapi-typescript
17+
import type { paths } from "./api/v1"; // generated by openapi-typescript
1818

1919
const { GET, PUT } = createClient<paths>({ baseUrl: "https://myapi.dev/v1/" });
2020

@@ -88,7 +88,7 @@ The best part about using openapi-fetch over oldschool codegen is no documentati
8888

8989
```ts
9090
import createClient from "openapi-fetch";
91-
import { paths } from "./v1";
91+
import type { paths } from "./api/v1";
9292

9393
const { GET, PUT } = createClient<paths>({ baseUrl: "https://myapi.dev/v1/" });
9494

Diff for: packages/openapi-fetch/examples/react-query/src/hooks/queries.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useQuery } from "@tanstack/react-query";
22
import type { ParamsOption, RequestBodyOption } from "openapi-fetch";
3-
import { paths } from "../lib/api/v1";
3+
import type { paths } from "../lib/api/v1";
44
import client from "../lib/api";
55

66
type UseQueryOptions<T> = ParamsOption<T> &
@@ -15,7 +15,11 @@ type UseQueryOptions<T> = ParamsOption<T> &
1515
// paths
1616
const GET_FACT = "/fact";
1717

18-
export function getFact({ params, body, reactQuery }: UseQueryOptions<paths[typeof GET_FACT]["get"]>) {
18+
export function getFact({
19+
params,
20+
body,
21+
reactQuery,
22+
}: UseQueryOptions<paths[typeof GET_FACT]["get"]>) {
1923
return useQuery({
2024
...reactQuery,
2125
queryKey: [

Diff for: packages/openapi-typescript/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ npx openapi-typescript https://myapi.dev/api/v1/openapi.yaml -o ./path/to/my/sch
4848
Then, import schemas from the generated file like so:
4949

5050
```ts
51-
import { paths, components } from "./path/to/my/schema"; // <- generated by openapi-typescript
51+
import type { paths, components } from "./api/v1"; // generated by openapi-typescript
5252

5353
// Schema Obj
5454
type MyType = components["schemas"]["MyType"];

Diff for: packages/openapi-typescript/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createConfig } from "@redocly/openapi-core";
2-
import { Readable } from "node:stream";
3-
import ts from "typescript";
2+
import type { Readable } from "node:stream";
3+
import type ts from "typescript";
44
import { validateAndBundle } from "./lib/redoc.js";
55
import { debug, resolveRef, scanDiscriminators } from "./lib/utils.js";
66
import transformSchema from "./transform/index.js";

Diff for: packages/openapi-typescript/src/lib/redoc.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from "@redocly/openapi-core";
1010
import { Readable } from "node:stream";
1111
import { fileURLToPath } from "node:url";
12-
import { OpenAPI3 } from "../types.js";
12+
import type { OpenAPI3 } from "../types.js";
1313
import { debug, error, warn } from "./utils.js";
1414

1515
export interface ValidateAndBundleOptions {

Diff for: packages/openapi-typescript/src/lib/ts.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { parseRef } from "@redocly/openapi-core/lib/ref-utils.js";
2-
import ts, { LiteralTypeNode, TypeLiteralNode } from "typescript";
2+
import ts, { type LiteralTypeNode, type TypeLiteralNode } from "typescript";
33

44
export const JS_PROPERTY_INDEX_RE = /^[A-Za-z_$][A-Za-z_$0-9]*$/;
55
export const JS_ENUM_INVALID_CHARS_RE = /[^A-Za-z_$0-9]+(.)?/g;

Diff for: packages/openapi-typescript/src/lib/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
import c from "ansi-colors";
66
import supportsColor from "supports-color";
77
import ts from "typescript";
8-
import { DiscriminatorObject, OpenAPI3 } from "../types.js";
8+
import type { DiscriminatorObject, OpenAPI3 } from "../types.js";
99
import { tsLiteral, tsModifiers, tsPropertyIndex } from "./ts.js";
1010

1111
if (!supportsColor.stdout || supportsColor.stdout.hasBasic === false) {

Diff for: packages/openapi-typescript/src/transform/components-object.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
tsPropertyIndex,
88
} from "../lib/ts.js";
99
import { createRef, debug, getEntries } from "../lib/utils.js";
10-
import {
10+
import type {
1111
ComponentsObject,
1212
GlobalContext,
1313
SchemaObject,

Diff for: packages/openapi-typescript/src/transform/header-object.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
UNKNOWN,
88
} from "../lib/ts.js";
99
import { getEntries } from "../lib/utils.js";
10-
import { HeaderObject, TransformNodeOptions } from "../types.js";
10+
import type { HeaderObject, TransformNodeOptions } from "../types.js";
1111
import transformMediaTypeObject from "./media-type-object.js";
1212
import transformSchemaObject from "./schema-object.js";
1313

Diff for: packages/openapi-typescript/src/transform/index.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import ts, { InterfaceDeclaration, TypeLiteralNode } from "typescript";
1+
import ts, {
2+
type InterfaceDeclaration,
3+
type TypeLiteralNode,
4+
} from "typescript";
25
import { NEVER, STRING, tsModifiers, tsRecord } from "../lib/ts.js";
36
import { createRef, debug } from "../lib/utils.js";
4-
import { GlobalContext, OpenAPI3 } from "../types.js";
7+
import type { GlobalContext, OpenAPI3 } from "../types.js";
58
import transformComponentsObject from "./components-object.js";
69
import transformPathsObject from "./paths-object.js";
710
import transformSchemaObject from "./schema-object.js";

Diff for: packages/openapi-typescript/src/transform/media-type-object.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import ts from "typescript";
1+
import type ts from "typescript";
22
import { UNKNOWN } from "../lib/ts.js";
3-
import { MediaTypeObject, TransformNodeOptions } from "../types.js";
3+
import type { MediaTypeObject, TransformNodeOptions } from "../types.js";
44
import transformSchemaObject from "./schema-object.js";
55

66
/**

Diff for: packages/openapi-typescript/src/transform/operation-object.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
tsPropertyIndex,
99
} from "../lib/ts.js";
1010
import { createRef } from "../lib/utils.js";
11-
import {
11+
import type {
1212
OperationObject,
1313
RequestBodyObject,
1414
TransformNodeOptions,

Diff for: packages/openapi-typescript/src/transform/parameter-object.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import ts from "typescript";
1+
import type ts from "typescript";
22
import { STRING } from "../lib/ts.js";
3-
import { ParameterObject, TransformNodeOptions } from "../types.js";
3+
import type { ParameterObject, TransformNodeOptions } from "../types.js";
44
import transformSchemaObject from "./schema-object.js";
55

66
/**

Diff for: packages/openapi-typescript/src/transform/parameters-array.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
tsPropertyIndex,
99
} from "../lib/ts.js";
1010
import { createRef } from "../lib/utils.js";
11-
import {
11+
import type {
1212
ParameterObject,
1313
ReferenceObject,
1414
TransformNodeOptions,

Diff for: packages/openapi-typescript/src/transform/path-item-object.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
tsPropertyIndex,
99
} from "../lib/ts.js";
1010
import { createRef } from "../lib/utils.js";
11-
import {
11+
import type {
1212
OperationObject,
1313
ParameterObject,
1414
PathItemObject,

Diff for: packages/openapi-typescript/src/transform/paths-object.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import {
77
tsPropertyIndex,
88
} from "../lib/ts.js";
99
import { createRef, debug, getEntries } from "../lib/utils.js";
10-
import {
10+
import type {
1111
GlobalContext,
1212
OperationObject,
1313
ParameterObject,
1414
PathItemObject,
1515
PathsObject,
1616
ReferenceObject,
1717
} from "../types.js";
18-
import transformPathItemObject, { Method } from "./path-item-object.js";
18+
import transformPathItemObject, { type Method } from "./path-item-object.js";
1919

2020
const PATH_PARAM_RE = /\{[^}]+\}/g;
2121

Diff for: packages/openapi-typescript/src/transform/request-body-object.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
tsPropertyIndex,
88
} from "../lib/ts.js";
99
import { createRef, getEntries } from "../lib/utils.js";
10-
import { RequestBodyObject, TransformNodeOptions } from "../types.js";
10+
import type { RequestBodyObject, TransformNodeOptions } from "../types.js";
1111
import transformMediaTypeObject from "./media-type-object.js";
1212
import transformSchemaObject from "./schema-object.js";
1313

Diff for: packages/openapi-typescript/src/transform/response-object.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
tsPropertyIndex,
1111
} from "../lib/ts.js";
1212
import { createRef, getEntries } from "../lib/utils.js";
13-
import { ResponseObject, TransformNodeOptions } from "../types.js";
13+
import type { ResponseObject, TransformNodeOptions } from "../types.js";
1414
import transformHeaderObject from "./header-object.js";
1515
import transformMediaTypeObject from "./media-type-object.js";
1616

Diff for: packages/openapi-typescript/src/transform/responses-object.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
tsPropertyIndex,
88
} from "../lib/ts.js";
99
import { createRef, getEntries } from "../lib/utils.js";
10-
import { ResponsesObject, TransformNodeOptions } from "../types.js";
10+
import type { ResponsesObject, TransformNodeOptions } from "../types.js";
1111
import transformResponseObject from "./response-object.js";
1212

1313
/**

Diff for: packages/openapi-typescript/src/transform/schema-object.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
createRef,
2929
getEntries,
3030
} from "../lib/utils.js";
31-
import {
31+
import type {
3232
ReferenceObject,
3333
SchemaObject,
3434
TransformNodeOptions,

Diff for: packages/openapi-typescript/src/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Config as RedoclyConfig } from "@redocly/openapi-core";
2-
import { PathLike } from "node:fs";
2+
import type { PathLike } from "node:fs";
33
import type ts from "typescript";
44

55
// Many types allow for true “any” for inheritance to work

Diff for: packages/openapi-typescript/test/cli.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { execa } from "execa";
22
import fs from "node:fs";
33
import os from "node:os";
44
import { fileURLToPath } from "node:url";
5-
import { TestCase } from "./test-helpers.js";
5+
import type { TestCase } from "./test-helpers.js";
66

77
const root = new URL("../", import.meta.url);
88
const cwd = os.platform() === "win32" ? fileURLToPath(root) : root; // execa bug: fileURLToPath required on Windows

Diff for: packages/openapi-typescript/test/discriminators.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { fileURLToPath } from "node:url";
2-
import openapiTS, { OpenAPITSOptions, astToString } from "../src/index.js";
3-
import { TestCase } from "./test-helpers.js";
2+
import openapiTS, { type OpenAPITSOptions, astToString } from "../src/index.js";
3+
import type { TestCase } from "./test-helpers.js";
44

55
describe("3.1 discriminators", () => {
66
const tests: TestCase<any, OpenAPITSOptions>[] = [

Diff for: packages/openapi-typescript/test/index.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { fileURLToPath } from "node:url";
22
import openapiTS, { astToString } from "../src/index.js";
33
import type { OpenAPI3, OpenAPITSOptions } from "../src/types.js";
4-
import { TestCase } from "./test-helpers.js";
4+
import type { TestCase } from "./test-helpers.js";
55

66
// prevent process.exit(1) from truly firing, as it will bypass Vitest’s error catching (throw new Error() will work as-expected)
77
beforeAll(() => {

Diff for: packages/openapi-typescript/test/node-api.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { fileURLToPath } from "node:url";
22
import ts from "typescript";
33
import openapiTS, { COMMENT_HEADER, astToString } from "../src/index.js";
4-
import { OpenAPITSOptions } from "../src/types.js";
5-
import { TestCase } from "./test-helpers.js";
4+
import type { OpenAPITSOptions } from "../src/types.js";
5+
import type { TestCase } from "./test-helpers.js";
66

77
const EXAMPLES_DIR = new URL("../examples/", import.meta.url);
88

Diff for: packages/openapi-typescript/test/test-helpers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createConfig } from "@redocly/openapi-core";
22
import { resolveRef } from "../src/lib/utils.js";
3-
import { GlobalContext, TransformNodeOptions } from "../src/types.js";
3+
import type { GlobalContext, TransformNodeOptions } from "../src/types.js";
44

55
/* eslint-disable @typescript-eslint/no-explicit-any */
66

0 commit comments

Comments
 (0)