Skip to content

Commit

Permalink
feat: Implement includer export compatible with next cli
Browse files Browse the repository at this point in the history
  • Loading branch information
3y3 committed Jan 2, 2025
1 parent dcde7bd commit 03e50ec
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 35 deletions.
88 changes: 55 additions & 33 deletions src/includer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {readFileSync} from 'fs';
import {dump} from 'js-yaml';
import SwaggerParser from '@apidevtools/swagger-parser';

import {matchFilter} from './utils';
import {filterUsefullContent, matchFilter} from './utils';
import parsers from './parsers';
import generators from './ui';
import {RefsService} from './services/refs';
Expand All @@ -21,6 +21,7 @@ import {
OpenAPISpec,
OpenApiIncluderParams,
OpenJSONSchema,
Run,
V3Endpoint,
V3Info,
YfmPreset,
Expand All @@ -46,6 +47,59 @@ export type Context = {
refs: RefsService;
};

export async function includer(run: Run, params: OpenApiIncluderParams, tocPath: string) {
const {input, leadingPage = {}, filter, noindex, hidden, sandbox, tags = {}} = params;

const ctx: Context = {
tag(id: string) {
return tags[id];
},
refs: new RefsService(),
};
const vars = await run.vars.load(tocPath);

const contentPath = join(run.input, input);

const parser = new SwaggerParser();

try {
const data = (await parser.validate(contentPath, {validate: {spec: true}})) as OpenAPISpec;

for (const file of Object.values(parser.$refs.values())) {
const schemas = Object.entries(file.components?.schemas || {}).concat(
Object.entries(file),
);
for (const [refName, schema] of schemas) {
ctx.refs.add(refName, schema as OpenJSONSchema);
}
}

const toc = await generateToc({data, leadingPage, filter, vars}, ctx);
const files = await generateContent(
{
data,
leadingPage,
contentPath,
filter,
noindex,
vars,
hidden,
sandbox,
},
ctx,
);

return {toc, files};
} catch (error) {
if (error && !(error instanceof OpenApiIncluderError)) {
// eslint-disable-next-line no-ex-assign
error = new OpenApiIncluderError(error.toString(), tocPath);
}

throw error;
}
}

async function includerFunction(params: IncluderFunctionParams<OpenApiIncluderParams>) {
const {
readBasePath,
Expand Down Expand Up @@ -333,38 +387,6 @@ function handleEndpointRender(endpoint: V3Endpoint, pathPrefix?: string): YfmToc
} as YfmToc;
}

function filterUsefullContent(
filter: OpenApiIncluderParams['filter'] | undefined,
vars: YfmPreset,
) {
if (!filter) {
return (spec: Specification) => spec;
}

return (spec: Specification): Specification => {
const endpointsByTag = new Map();
const tags = new Map();

matchFilter(filter, vars, (endpoint, tag) => {
const tagId = tag?.id ?? null;
const collection = endpointsByTag.get(tagId) || [];

collection.push(endpoint);
endpointsByTag.set(tagId, collection);

if (tagId !== null) {
tags.set(tagId, {...tag, endpoints: collection});
}
})(spec);

return {
...spec,
tags,
endpoints: endpointsByTag.get(null) || [],
};
};
}

export function sectionName(e: V3Endpoint): string {
return e.summary ?? e.operationId ?? `${e.method} ${e.path}`;
}
Expand Down
8 changes: 7 additions & 1 deletion src/includer/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ export type CustomTag = {
};

export type OpenApiIncluderParams = {
allowAnonymousObjects?: boolean;
input: string;
leadingPage?: LeadingPageParams;
filter?: OpenApiFilter;
Expand Down Expand Up @@ -335,3 +334,10 @@ export type JSONSchemaUnionType = {
unionOf: JSONSchemaType[];
};
export type JSONSchemaType = BaseJSONSchemaType | JSONSchemaUnionType | FoundRefType;

export type Run = {
input: string;
vars: {
load(path: string): Promise<YfmPreset>;
};
};
34 changes: 33 additions & 1 deletion src/includer/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {evalExp} from '@diplodoc/transform/lib/liquid/evaluation';

import {OpenApiIncluderParams, Specification, V3Endpoint, V3Tag} from './models';
import {OpenApiIncluderParams, Specification, V3Endpoint, V3Tag, YfmPreset} from './models';

export function concatNewLine(prefix: string, suffix: string) {
return prefix.trim().length ? `${prefix}<br>${suffix}` : suffix;
Expand Down Expand Up @@ -46,3 +46,35 @@ export function matchFilter(
}
};
}

export function filterUsefullContent(
filter: OpenApiIncluderParams['filter'] | undefined,
vars: YfmPreset,
) {
if (!filter) {
return (spec: Specification) => spec;
}

return (spec: Specification): Specification => {
const endpointsByTag = new Map();
const tags = new Map();

matchFilter(filter, vars, (endpoint, tag) => {
const tagId = tag?.id ?? null;
const collection = endpointsByTag.get(tagId) || [];

collection.push(endpoint);
endpointsByTag.set(tagId, collection);

if (tagId !== null) {
tags.set(tagId, {...tag, endpoints: collection});
}
})(spec);

return {
...spec,
tags,
endpoints: endpointsByTag.get(null) || [],
};
};
}

0 comments on commit 03e50ec

Please sign in to comment.