Skip to content

Commit

Permalink
chore(includers/openapi): linter warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
moki committed Sep 1, 2022
1 parent f3bb43b commit eae4f11
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 16 deletions.
26 changes: 24 additions & 2 deletions src/services/includers/batteries/openapi/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,28 @@ const REQUEST_SECTION_NAME = 'Request';
const PARAMETERS_SECTION_NAME = 'Parameters';
const RESPONSES_SECTION_NAME = 'Responses';

export {TAG_NAMES_FIELD, BLOCK, DESCRIPTION_SECTION_NAME, CONTACTS_SECTION_NAME, TAGS_SECTION_NAME, ENDPOINTS_SECTION_NAME, REQUEST_SECTION_NAME, PARAMETERS_SECTION_NAME, RESPONSES_SECTION_NAME, EOL};
export {
TAG_NAMES_FIELD,
BLOCK,
DESCRIPTION_SECTION_NAME,
CONTACTS_SECTION_NAME,
TAGS_SECTION_NAME,
ENDPOINTS_SECTION_NAME,
REQUEST_SECTION_NAME,
PARAMETERS_SECTION_NAME,
RESPONSES_SECTION_NAME,
EOL,
};

export default {TAG_NAMES_FIELD, BLOCK, DESCRIPTION_SECTION_NAME, CONTACTS_SECTION_NAME, TAGS_SECTION_NAME, ENDPOINTS_SECTION_NAME, REQUEST_SECTION_NAME, PARAMETERS_SECTION_NAME, RESPONSES_SECTION_NAME, EOL};
export default {
TAG_NAMES_FIELD,
BLOCK,
DESCRIPTION_SECTION_NAME,
CONTACTS_SECTION_NAME,
TAGS_SECTION_NAME,
ENDPOINTS_SECTION_NAME,
REQUEST_SECTION_NAME,
PARAMETERS_SECTION_NAME,
RESPONSES_SECTION_NAME,
EOL,
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import {EOL, BLOCK} from '../constants';

import {TitleDepth} from '../types';
Expand Down Expand Up @@ -27,8 +26,11 @@ function code(text: string) {
return EOL + ['```', text, '```'].join(EOL) + EOL;
}

/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
function table(data: any[][]) {
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
const colgen = (col: any) => (Array.isArray(col) ? `${EOL}${table(col)}${EOL}` : ` ${col} `);
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
const rowgen = (row: any) => `||${row.map(colgen).join('|')}||`;

return `#|${block(data.map(rowgen))}|#`;
Expand All @@ -38,6 +40,7 @@ function cut(text: string, heading = '') {
return block([`{% cut "${heading}" %}`, text, '{% endcut %}']) + EOL;
}

/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
function block(elements: any[]) {
return elements.filter(Boolean).join(BLOCK);
}
Expand Down
15 changes: 8 additions & 7 deletions src/services/includers/batteries/openapi/generators/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,23 @@ function parameters(params?: Parameters) {
]);
}

function responses(responses?: Responses) {
return responses?.length && block([
function responses(resps?: Responses) {
return resps?.length && block([
title(2)(RESPONSES_SECTION_NAME),
block(responses.map(response)),
block(resps.map(response)),
]);
}

function response(response: Response) {
function response(resp: Response) {
return block([
title(3)(response.code),
title(3)(resp.code),
title(4)(DESCRIPTION_SECTION_NAME),
body(response.description),
response.schemas?.length && block(response.schemas.map(schema)),
body(resp.description),
resp.schemas?.length && block(resp.schemas.map(schema)),
]);
}

/* eslint-disable-next-line no-shadow */
function schema({type, schema}: Schema) {
return cut(code(JSON.stringify(schema, null, 4)), type);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable-next-line no-shadow */
import {block, title, body, link, list} from './common';
import {DESCRIPTION_SECTION_NAME, ENDPOINTS_SECTION_NAME} from '../constants';

Expand All @@ -18,10 +19,9 @@ function description(text?: string) {
}

function endpoints(data?: Endpoints) {
const links = (endpoints: Endpoints) =>
endpoints.map(({id, summary}: Endpoint) => link(summary ?? id, id + '.md'));
const linkMap = ({id, summary}: Endpoint) => link(summary ?? id, id + '.md');

return data?.length && block([title(2)(ENDPOINTS_SECTION_NAME), list(links(data))]);
return data?.length && block([title(2)(ENDPOINTS_SECTION_NAME), list(data.map(linkMap))]);
}

export {section};
Expand Down
7 changes: 5 additions & 2 deletions src/services/includers/batteries/openapi/parsers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint-disable no-shadow, @typescript-eslint/no-explicit-any */
/* eslint-disable no-shadow */
import slugify from 'slugify';

import {TAG_NAMES_FIELD} from './constants';
Expand Down Expand Up @@ -46,7 +46,6 @@ function info(spec: OpenapiSpec): Info {
name: contact.name,
sources: [],
};

if (contact.url) {
parsed.contact.sources.push({type: 'web', url: new URL(contact.url).href});
}
Expand Down Expand Up @@ -116,17 +115,20 @@ function paths(spec: OpenapiSpec, tagsByID: Map<string, Tag>): Map<string, Tag>
const serverURLs = (endpoint.servers || servers || [{url: '/'}])
.map(({url}: Server) => trimSlash(url));

/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
const parseResponse = ([code, response]: [string, {[key: string]: any}]) => {
const parsed: Response = {code, description: response.description};

if (response.content) {
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
parsed.schemas = Object.entries<{[key: string]: any}>(response.content)
.map(([type, schema]) => ({type, schema}));
}

return parsed;
};

/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
const parsedResponses: Responses = Object.entries<{[key: string]: any}>(responses ?? {})
.map(parseResponse);

Expand Down Expand Up @@ -163,6 +165,7 @@ function trimSlash(str: string) {

type VisiterParams = {path: string; method: Method; endpoint: OpenapiOperation};

/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
function visitPaths<T>(paths: {[key: string]: any}, visiter: (params: VisiterParams) => T): T[] {
const results: T[] = [];

Expand Down
3 changes: 2 additions & 1 deletion src/services/includers/batteries/openapi/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
export const titleDepths = [1, 2, 3, 4, 5, 6] as const;

export type TitleDepth = typeof titleDepths[number];

/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
export type OpenapiSpec = {[key: string]: any};

export type OpenapiOperation = {
Expand Down Expand Up @@ -101,5 +101,6 @@ export type Schemas = Schema[];

export type Schema = {
type: string;
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
schema: {[key: string]: any};
};

0 comments on commit eae4f11

Please sign in to comment.