Skip to content

Commit

Permalink
fix: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
enkot committed Mar 27, 2024
1 parent a050325 commit f233ded
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 23 deletions.
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 @@ -393,4 +393,4 @@ export function renameDuplicates(arr: string[]): string[] {
}

return res;
}
}
29 changes: 20 additions & 9 deletions packages/openapi-typescript/src/transform/components-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ import {
addJSDocComment,
tsModifiers,
tsPropertyIndex,
oapiRef
oapiRef,
} from "../lib/ts.js";
import { createRef, debug, getEntries, renameDuplicates } from "../lib/utils.js";
import {
createRef,
debug,
getEntries,
renameDuplicates,
} from "../lib/utils.js";
import type {
ComponentsObject,
GlobalContext,
Expand Down Expand Up @@ -90,14 +95,20 @@ export default function transformComponentsObject(

if (ctx.rootTypes) {
const keySingular = key.slice(0, -1);
const pascalNames = renameDuplicates(entries.map(([name]) => pascalCase(`${keySingular}-${name}`)));
const pascalNames = renameDuplicates(
entries.map(([name]) => pascalCase(`${keySingular}-${name}`)),
);
for (let i = 0; i < entries.length; i++) {
refs.push(ts.factory.createTypeAliasDeclaration(
/* modifiers */ tsModifiers({ export: true }),
/* name */ pascalNames[i],
/* typeParameters */ undefined,
/* type */ oapiRef(createRef(["components", key, entries[i][0]])),
));
refs.push(
ts.factory.createTypeAliasDeclaration(
/* modifiers */ tsModifiers({ export: true }),
/* name */ pascalNames[i],
/* typeParameters */ undefined,
/* type */ oapiRef(
createRef(["components", key, entries[i][0]]),
),
),
);
}
}
}
Expand Down
11 changes: 8 additions & 3 deletions packages/openapi-typescript/src/transform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@ type SchemaTransforms = keyof Pick<

const transformers: Record<
SchemaTransforms,
(node: any, options: GlobalContext) => [ts.TypeNode, ts.TypeAliasDeclaration[]] // eslint-disable-line @typescript-eslint/no-explicit-any
(
node: any, // eslint-disable-line @typescript-eslint/no-explicit-any
options: GlobalContext,
) => [ts.TypeNode, ts.TypeAliasDeclaration[]]
> = {
paths: transformPathsObject,
webhooks: transformWebhooksObject,
components: transformComponentsObject,
$defs: (node, options) =>
[transformSchemaObject(node, { path: createRef(["$defs"]), ctx: options }), []],
$defs: (node, options) => [
transformSchemaObject(node, { path: createRef(["$defs"]), ctx: options }),
[],
],
};

export default function transformSchema(schema: OpenAPI3, ctx: GlobalContext) {
Expand Down
27 changes: 17 additions & 10 deletions packages/openapi-typescript/src/transform/paths-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ export default function transformPathsObject(

// pathParamsAsTypes
if (ctx.pathParamsAsTypes && url.includes("{")) {
const { parameters: pathParams = {} } = extractParams(pathItemObject, ctx);
const { parameters: pathParams = {} } = extractParams(
pathItemObject,
ctx,
);
const matches = url.match(PATH_PARAM_RE);
let rawPath = `\`${url}\``;
if (matches) {
Expand Down Expand Up @@ -112,12 +115,16 @@ export default function transformPathsObject(
if (ctx.rootTypes) {
const { operations } = extractParams(pathItemObject, ctx);
for (const name in operations) {
refs.push(ts.factory.createTypeAliasDeclaration(
/* modifiers */ tsModifiers({ export: true }),
/* name */ pascalCase(`request-${operations[name]}`),
/* typeParameters */ undefined,
/* type */ oapiRef(createRef(["paths", url, name, 'parameters'])),
));
refs.push(
ts.factory.createTypeAliasDeclaration(
/* modifiers */ tsModifiers({ export: true }),
/* name */ pascalCase(`request-${operations[name]}`),
/* typeParameters */ undefined,
/* type */ oapiRef(
createRef(["paths", url, name, "parameters"]),
),
),
);
}
}
}
Expand All @@ -142,7 +149,7 @@ function extractParams(pathItemObject: PathItemObject, ctx: GlobalContext) {
params.parameters = {
...params.parameters,
[resolved.name]: resolved,
}
};
}
}
for (const method of [
Expand Down Expand Up @@ -173,12 +180,12 @@ function extractParams(pathItemObject: PathItemObject, ctx: GlobalContext) {
params.parameters = {
...params.parameters,
[resolvedParam.name]: resolvedParam,
}
};
if (resolvedMethod.operationId) {
params.operations = {
...params.operations,
[method]: resolvedMethod.operationId,
}
};
}
}
}
Expand Down

0 comments on commit f233ded

Please sign in to comment.