Skip to content

Commit

Permalink
Merge pull request #1523 from drwpow/chore/enforce-type
Browse files Browse the repository at this point in the history
Chore: Enforce type imports
  • Loading branch information
drwpow authored Jan 28, 2024
2 parents c87275a + 9fbcabf commit 0a700ea
Show file tree
Hide file tree
Showing 50 changed files with 75 additions and 64 deletions.
4 changes: 4 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ module.exports = {
],
rules: {
"@typescript-eslint/consistent-indexed-object-style": "off", // sometimes naming keys is more user-friendly
"@typescript-eslint/consistent-type-imports": [
"error",
{ prefer: "type-imports", fixStyle: "inline-type-imports" },
],
"@typescript-eslint/no-dynamic-delete": "off", // delete is OK
"@typescript-eslint/no-non-null-assertion": "off", // this is better than "as"
"@typescript-eslint/no-shadow": "error",
Expand Down
2 changes: 1 addition & 1 deletion docs/6.x/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ And the magic that produces this would live in a `test/utils.ts` file that can b
::: code-group [test/utils.ts]

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

// Settings
// ⚠️ Important: change this! This prefixes all URLs
Expand Down
4 changes: 2 additions & 2 deletions docs/6.x/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export interface paths {
Which means your type lookups also have to match the exact URL:

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

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

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

const url = `/user/${id}`;
type UserResponses = paths[url]["responses"]; // automatically matches `paths['/user/{user_id}']`
Expand Down
2 changes: 1 addition & 1 deletion docs/6.x/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Be sure to [validate your schemas](https://redocly.com/docs/cli/commands/lint/)!
Then, import schemas from the generated file like so:

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

// Schema Obj
type MyType = components["schemas"]["MyType"];
Expand Down
4 changes: 2 additions & 2 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export interface paths {
Which means your type lookups also have to match the exact URL:

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

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

const url = `/user/${id}`;
type UserResponses = paths[url]["responses"]; // automatically matches `paths['/user/{user_id}']`
Expand Down
2 changes: 1 addition & 1 deletion docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ And the magic that produces this would live in a `test/utils.ts` file that can b
::: code-group [test/utils.ts]

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

// Settings
// ⚠️ Important: change this! This prefixes all URLs
Expand Down
2 changes: 1 addition & 1 deletion docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ npx openapi-typescript https://myapi.dev/api/v1/openapi.yaml -o ./path/to/my/sch
Then in your TypeScript project, import types where needed:

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

// Schema Obj
type MyType = components["schemas"]["MyType"];
Expand Down
6 changes: 3 additions & 3 deletions docs/openapi-fetch/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Here’s how it can be handled using [Nano Stores](https://github.com/nanostores
// src/lib/api/index.ts
import { atom, computed } from "nanostores";
import createClient from "openapi-fetch";
import { paths } from "./v1";
import type { paths } from "./api/v1";

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

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

let authToken: string | undefined = undefined;
someAuthMethod().then((newToken) => (authToken = newToken));
Expand Down
4 changes: 2 additions & 2 deletions docs/openapi-fetch/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ The syntax is inspired by popular libraries like react-query or Apollo client, b

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

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

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

```ts
import createClient from "openapi-fetch";
import { paths } from "./v1";
import type { paths } from "./api/v1";

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

Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-fetch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The syntax is inspired by popular libraries like react-query or Apollo client, b

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

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

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

```ts
import createClient from "openapi-fetch";
import { paths } from "./v1";
import type { paths } from "./api/v1";

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useQuery } from "@tanstack/react-query";
import type { ParamsOption, RequestBodyOption } from "openapi-fetch";
import { paths } from "../lib/api/v1";
import type { paths } from "../lib/api/v1";
import client from "../lib/api";

type UseQueryOptions<T> = ParamsOption<T> &
Expand All @@ -15,7 +15,11 @@ type UseQueryOptions<T> = ParamsOption<T> &
// paths
const GET_FACT = "/fact";

export function getFact({ params, body, reactQuery }: UseQueryOptions<paths[typeof GET_FACT]["get"]>) {
export function getFact({
params,
body,
reactQuery,
}: UseQueryOptions<paths[typeof GET_FACT]["get"]>) {
return useQuery({
...reactQuery,
queryKey: [
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ npx openapi-typescript https://myapi.dev/api/v1/openapi.yaml -o ./path/to/my/sch
Then, import schemas from the generated file like so:

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

// Schema Obj
type MyType = components["schemas"]["MyType"];
Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-typescript/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createConfig } from "@redocly/openapi-core";
import { Readable } from "node:stream";
import ts from "typescript";
import type { Readable } from "node:stream";
import type ts from "typescript";
import { validateAndBundle } from "./lib/redoc.js";
import { debug, resolveRef, scanDiscriminators } from "./lib/utils.js";
import transformSchema from "./transform/index.js";
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-typescript/src/lib/redoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "@redocly/openapi-core";
import { Readable } from "node:stream";
import { fileURLToPath } from "node:url";
import { OpenAPI3 } from "../types.js";
import type { OpenAPI3 } from "../types.js";
import { debug, error, warn } from "./utils.js";

export interface ValidateAndBundleOptions {
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-typescript/src/lib/ts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parseRef } from "@redocly/openapi-core/lib/ref-utils.js";
import ts, { LiteralTypeNode, TypeLiteralNode } from "typescript";
import ts, { type LiteralTypeNode, type TypeLiteralNode } from "typescript";

export const JS_PROPERTY_INDEX_RE = /^[A-Za-z_$][A-Za-z_$0-9]*$/;
export const JS_ENUM_INVALID_CHARS_RE = /[^A-Za-z_$0-9]+(.)?/g;
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-typescript/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
import c from "ansi-colors";
import supportsColor from "supports-color";
import ts from "typescript";
import { DiscriminatorObject, OpenAPI3 } from "../types.js";
import type { DiscriminatorObject, OpenAPI3 } from "../types.js";
import { tsLiteral, tsModifiers, tsPropertyIndex } from "./ts.js";

if (!supportsColor.stdout || supportsColor.stdout.hasBasic === false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
tsPropertyIndex,
} from "../lib/ts.js";
import { createRef, debug, getEntries } from "../lib/utils.js";
import {
import type {
ComponentsObject,
GlobalContext,
SchemaObject,
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-typescript/src/transform/header-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
UNKNOWN,
} from "../lib/ts.js";
import { getEntries } from "../lib/utils.js";
import { HeaderObject, TransformNodeOptions } from "../types.js";
import type { HeaderObject, TransformNodeOptions } from "../types.js";
import transformMediaTypeObject from "./media-type-object.js";
import transformSchemaObject from "./schema-object.js";

Expand Down
7 changes: 5 additions & 2 deletions packages/openapi-typescript/src/transform/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import ts, { InterfaceDeclaration, TypeLiteralNode } from "typescript";
import ts, {
type InterfaceDeclaration,
type TypeLiteralNode,
} from "typescript";
import { NEVER, STRING, tsModifiers, tsRecord } from "../lib/ts.js";
import { createRef, debug } from "../lib/utils.js";
import { GlobalContext, OpenAPI3 } from "../types.js";
import type { GlobalContext, OpenAPI3 } from "../types.js";
import transformComponentsObject from "./components-object.js";
import transformPathsObject from "./paths-object.js";
import transformSchemaObject from "./schema-object.js";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ts from "typescript";
import type ts from "typescript";
import { UNKNOWN } from "../lib/ts.js";
import { MediaTypeObject, TransformNodeOptions } from "../types.js";
import type { MediaTypeObject, TransformNodeOptions } from "../types.js";
import transformSchemaObject from "./schema-object.js";

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
tsPropertyIndex,
} from "../lib/ts.js";
import { createRef } from "../lib/utils.js";
import {
import type {
OperationObject,
RequestBodyObject,
TransformNodeOptions,
Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-typescript/src/transform/parameter-object.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ts from "typescript";
import type ts from "typescript";
import { STRING } from "../lib/ts.js";
import { ParameterObject, TransformNodeOptions } from "../types.js";
import type { ParameterObject, TransformNodeOptions } from "../types.js";
import transformSchemaObject from "./schema-object.js";

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
tsPropertyIndex,
} from "../lib/ts.js";
import { createRef } from "../lib/utils.js";
import {
import type {
ParameterObject,
ReferenceObject,
TransformNodeOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
tsPropertyIndex,
} from "../lib/ts.js";
import { createRef } from "../lib/utils.js";
import {
import type {
OperationObject,
ParameterObject,
PathItemObject,
Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-typescript/src/transform/paths-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import {
tsPropertyIndex,
} from "../lib/ts.js";
import { createRef, debug, getEntries } from "../lib/utils.js";
import {
import type {
GlobalContext,
OperationObject,
ParameterObject,
PathItemObject,
PathsObject,
ReferenceObject,
} from "../types.js";
import transformPathItemObject, { Method } from "./path-item-object.js";
import transformPathItemObject, { type Method } from "./path-item-object.js";

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
tsPropertyIndex,
} from "../lib/ts.js";
import { createRef, getEntries } from "../lib/utils.js";
import { RequestBodyObject, TransformNodeOptions } from "../types.js";
import type { RequestBodyObject, TransformNodeOptions } from "../types.js";
import transformMediaTypeObject from "./media-type-object.js";
import transformSchemaObject from "./schema-object.js";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
tsPropertyIndex,
} from "../lib/ts.js";
import { createRef, getEntries } from "../lib/utils.js";
import { ResponseObject, TransformNodeOptions } from "../types.js";
import type { ResponseObject, TransformNodeOptions } from "../types.js";
import transformHeaderObject from "./header-object.js";
import transformMediaTypeObject from "./media-type-object.js";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
tsPropertyIndex,
} from "../lib/ts.js";
import { createRef, getEntries } from "../lib/utils.js";
import { ResponsesObject, TransformNodeOptions } from "../types.js";
import type { ResponsesObject, TransformNodeOptions } from "../types.js";
import transformResponseObject from "./response-object.js";

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-typescript/src/transform/schema-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
createRef,
getEntries,
} from "../lib/utils.js";
import {
import type {
ReferenceObject,
SchemaObject,
TransformNodeOptions,
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-typescript/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Config as RedoclyConfig } from "@redocly/openapi-core";
import { PathLike } from "node:fs";
import type { PathLike } from "node:fs";
import type ts from "typescript";

// Many types allow for true “any” for inheritance to work
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-typescript/test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { execa } from "execa";
import fs from "node:fs";
import os from "node:os";
import { fileURLToPath } from "node:url";
import { TestCase } from "./test-helpers.js";
import type { TestCase } from "./test-helpers.js";

const root = new URL("../", import.meta.url);
const cwd = os.platform() === "win32" ? fileURLToPath(root) : root; // execa bug: fileURLToPath required on Windows
Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-typescript/test/discriminators.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fileURLToPath } from "node:url";
import openapiTS, { OpenAPITSOptions, astToString } from "../src/index.js";
import { TestCase } from "./test-helpers.js";
import openapiTS, { type OpenAPITSOptions, astToString } from "../src/index.js";
import type { TestCase } from "./test-helpers.js";

describe("3.1 discriminators", () => {
const tests: TestCase<any, OpenAPITSOptions>[] = [
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-typescript/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { fileURLToPath } from "node:url";
import openapiTS, { astToString } from "../src/index.js";
import type { OpenAPI3, OpenAPITSOptions } from "../src/types.js";
import { TestCase } from "./test-helpers.js";
import type { TestCase } from "./test-helpers.js";

// prevent process.exit(1) from truly firing, as it will bypass Vitest’s error catching (throw new Error() will work as-expected)
beforeAll(() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-typescript/test/node-api.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { fileURLToPath } from "node:url";
import ts from "typescript";
import openapiTS, { COMMENT_HEADER, astToString } from "../src/index.js";
import { OpenAPITSOptions } from "../src/types.js";
import { TestCase } from "./test-helpers.js";
import type { OpenAPITSOptions } from "../src/types.js";
import type { TestCase } from "./test-helpers.js";

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

Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-typescript/test/test-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createConfig } from "@redocly/openapi-core";
import { resolveRef } from "../src/lib/utils.js";
import { GlobalContext, TransformNodeOptions } from "../src/types.js";
import type { GlobalContext, TransformNodeOptions } from "../src/types.js";

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

Expand Down
Loading

0 comments on commit 0a700ea

Please sign in to comment.