Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new featueres #25

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
231 changes: 231 additions & 0 deletions src/__snapshots__/examples/array.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`openapi project with examples renders example field 1`] = `
"<div class="openapi">
# example.array
## Request
<div class="openapi__request__wrapper">
<div class="openapi__request" style="--method: var(--dc-openapi-methods-post)">
POST {.openapi__method}
\`\`\`
http://localhost:8080/test
\`\`\`
</div>
</div>
Generated server url{.openapi__request__description}
#### Body
{% cut "application/json" %}
\`\`\`json
[
{
"test": 1
},
{
"test": 2
}
]
\`\`\`
{% endcut %}
any[]
## Responses
<div class="openapi__response__code__200">
## 200 OK
Base 200 response
#### Body
{% cut "application/json" %}
\`\`\`json
{}
\`\`\`
{% endcut %}
</div>
<!-- markdownlint-disable-file -->
</div>"
`;

exports[`openapi project with examples renders infered example 1`] = `
"<div class="openapi">
# example.array
## Request
<div class="openapi__request__wrapper">
<div class="openapi__request" style="--method: var(--dc-openapi-methods-post)">
POST {.openapi__method}
\`\`\`
http://localhost:8080/test
\`\`\`
</div>
</div>
Generated server url{.openapi__request__description}
#### Body
{% cut "application/json" %}
\`\`\`json
[
{
"name": "string"
}
]
\`\`\`
{% endcut %}
[Cat](#cat)[]
### Cat
#||| **Name** | **Type** | **Description** ||
|| name | string | |||#
## Responses
<div class="openapi__response__code__200">
## 200 OK
Base 200 response
#### Body
{% cut "application/json" %}
\`\`\`json
{}
\`\`\`
{% endcut %}
</div>
<!-- markdownlint-disable-file -->
</div>"
`;

exports[`openapi project with examples renders nested arrays exmaple 1`] = `
"<div class="openapi">
# example.array
## Request
<div class="openapi__request__wrapper">
<div class="openapi__request" style="--method: var(--dc-openapi-methods-post)">
POST {.openapi__method}
\`\`\`
http://localhost:8080/test
\`\`\`
</div>
</div>
Generated server url{.openapi__request__description}
#### Body
{% cut "application/json" %}
\`\`\`json
[
[
{
"name": "string"
}
]
]
\`\`\`
{% endcut %}
[Cat](#cat)[][]
### Cat
#||| **Name** | **Type** | **Description** ||
|| name | string | |||#
## Responses
<div class="openapi__response__code__200">
## 200 OK
Base 200 response
#### Body
{% cut "application/json" %}
\`\`\`json
{}
\`\`\`
{% endcut %}
</div>
<!-- markdownlint-disable-file -->
</div>"
`;
100 changes: 100 additions & 0 deletions src/__tests__/examples/array.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import {DocumentBuilder, run} from '../__helpers__/run';

const name = 'example.array';
describe('openapi project with examples', () => {
it('renders example field', async () => {
const spec = new DocumentBuilder(name)
.request({
schema: {
example: [
{
test: 1,
},
{
test: 2,
},
],
type: 'array',
items: {},
},
})
.response(200, {
description: 'Base 200 response',
schema: {
type: 'object',
},
})
.build();

const fs = await run(spec);

const page = fs.match(name);

expect(page).toMatchSnapshot();
});

it('renders infered example', async () => {
const spec = new DocumentBuilder(name)
.request({
schema: {
type: 'array',
items: DocumentBuilder.ref('Cat'),
},
})
.response(200, {
description: 'Base 200 response',
schema: {
type: 'object',
},
})
.component('Cat', {
type: 'object',
properties: {
name: {
type: 'string',
},
},
})
.build();

const fs = await run(spec);

const page = fs.match(name);

expect(page).toMatchSnapshot();
});

it('renders nested arrays exmaple', async () => {
const spec = new DocumentBuilder(name)
.request({
schema: {
type: 'array',
items: {
type: 'array',
items: DocumentBuilder.ref('Cat'),
},
},
})
.response(200, {
description: 'Base 200 response',
schema: {
type: 'object',
},
})
.component('Cat', {
type: 'object',
properties: {
name: {
type: 'string',
},
},
})
.build();

const fs = await run(spec);

const page = fs.match(name);

expect(page).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {DocumentBuilder, run} from './__helpers__/run';
import {DocumentBuilder, run} from '../__helpers__/run';

const name = 'example';
describe('openapi project with examples', () => {
Expand Down
21 changes: 14 additions & 7 deletions src/includer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,20 @@
items: [],
};

section.items = endpointsOfTag.map((endpoint) => handleEndpointRender(endpoint, id));

const custom = ArgvService.tag(tag.name);
const customId = custom?.alias || id;

section.items = endpointsOfTag.map((endpoint) => handleEndpointRender(endpoint, customId));

const customLeadingPageName = custom?.name || leadingPageName;

if (!custom?.hidden) {
addLeadingPage(section, leadingPageMode, customLeadingPageName, join(id, 'index.md'));
addLeadingPage(
section,
leadingPageMode,
customLeadingPageName,
join(customId, 'index.md'),
);
}

toc.items.push(section);
Expand Down Expand Up @@ -199,7 +205,7 @@
href: href,
});
} else {
section.href = href;

Check warning on line 208 in src/includer/index.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Assignment to property of function parameter 'section'
}
}

Expand Down Expand Up @@ -229,11 +235,11 @@

const filterContent = filterUsefullContent(filter, vars);
const applyNoindex = matchFilter(noindex || {}, vars, (endpoint) => {
endpoint.noindex = true;

Check warning on line 238 in src/includer/index.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Assignment to property of function parameter 'endpoint'
});

const applyHidden = matchFilter(hidden || {}, vars, (endpoint) => {
endpoint.hidden = true;

Check warning on line 242 in src/includer/index.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Assignment to property of function parameter 'endpoint'
});

const leadingPageSpecRenderMode = leadingPage?.spec?.renderMode ?? SPEC_RENDER_MODE_DEFAULT;
Expand Down Expand Up @@ -270,12 +276,13 @@
spec.tags.forEach((tag, id) => {
const {endpoints} = tag;

const custom = ArgvService.tag(tag.name);
const customId = custom?.alias || id;

endpoints.forEach((endpoint) => {
results.push(handleEndpointIncluder(endpoint, join(writePath, id), sandbox));
results.push(handleEndpointIncluder(endpoint, join(writePath, customId), sandbox));
});

const custom = ArgvService.tag(tag.name);

if (custom?.hidden) {
return;
}
Expand All @@ -285,7 +292,7 @@
: generators.section(tag);

results.push({
path: join(writePath, id, 'index.md'),
path: join(writePath, customId, 'index.md'),
content,
});
});
Expand Down
1 change: 1 addition & 0 deletions src/includer/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ export type CustomTag = {
hidden?: boolean;
name?: string;
path?: string;
alias?: string;
};

export type OpenApiIncluderParams = {
Expand Down
Loading
Loading