Skip to content

Commit b6c7fa0

Browse files
committed
chore: Update main typings
1 parent 8498906 commit b6c7fa0

File tree

7 files changed

+114
-230
lines changed

7 files changed

+114
-230
lines changed

src/includer/constants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type {OpenJSONSchema} from './models';
1+
import type {OpenAPIV3} from 'openapi-types';
22

33
export enum LeadingPageMode {
44
Section = 'section',
@@ -30,7 +30,7 @@ export const DEPRECATED_ANNOTATION = 'Deprecated';
3030
export const DEPRECATED_POPUP_TEXT =
3131
'No longer supported, please use an alternative and newer version.';
3232
export const SUPPORTED_ENUM_TYPES = ['string', 'number'] as const;
33-
export const PRIMITIVE_JSON6_SCHEMA_TYPES = new Set<OpenJSONSchema['type']>([
33+
export const PRIMITIVE_JSON6_SCHEMA_TYPES = new Set<OpenAPIV3.SchemaObject['type'] | 'null'>([
3434
'string',
3535
'boolean',
3636
'null',

src/includer/index.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1+
import type {OpenAPIV3} from 'openapi-types';
12
import type {
2-
OpenAPISpec,
3+
Dereference,
34
OpenApiIncluderParams,
4-
OpenJSONSchema,
55
Run,
66
V3Endpoint,
77
V3Info,
88
YfmPreset,
9-
YfmToc,
109
YfmTocItem,
1110
} from './models';
1211

@@ -111,7 +110,10 @@ function assertLeadingPageMode(mode: string) {
111110
);
112111
}
113112

114-
async function generateToc(data: OpenAPISpec, ctx: Context): Promise<YfmToc> {
113+
async function generateToc(
114+
data: Dereference<OpenAPIV3.Document>,
115+
ctx: Context,
116+
): Promise<YfmTocItem> {
115117
const {vars, params} = ctx;
116118
const {leadingPage, filter} = params;
117119
const leadingPageName = leadingPage?.name ?? LEADING_PAGE_NAME_DEFAULT;
@@ -166,7 +168,7 @@ async function generateToc(data: OpenAPISpec, ctx: Context): Promise<YfmToc> {
166168
addLeadingPage(toc, leadingPageMode, rootLadingPageName, 'index.md');
167169
}
168170

169-
return toc as YfmToc;
171+
return toc;
170172
}
171173

172174
function addLeadingPage(section: YfmTocItem, mode: LeadingPageMode, name: string, href: string) {
@@ -185,7 +187,10 @@ type EndpointRoute = {
185187
content: string;
186188
};
187189

188-
async function generateContent(data: OpenAPISpec, ctx: Context): Promise<EndpointRoute[]> {
190+
async function generateContent(
191+
data: Dereference<OpenAPIV3.Document>,
192+
ctx: Context,
193+
): Promise<EndpointRoute[]> {
189194
const {vars, params} = ctx;
190195
const {input, leadingPage, filter, noindex, hidden, sandbox} = params;
191196
const contentPath = ctx.relative(input);
@@ -274,17 +279,18 @@ function handleEndpointIncluder(
274279
return {path, content};
275280
}
276281

277-
function handleEndpointRender(endpoint: V3Endpoint, pathPrefix?: string): YfmToc {
282+
function handleEndpointRender(endpoint: V3Endpoint, pathPrefix?: string): YfmTocItem {
278283
let path = mdPath(endpoint);
279284
if (pathPrefix) {
280285
path = join(pathPrefix, path);
281286
}
287+
282288
return {
283289
href: path,
284290
name: sectionName(endpoint),
285291
hidden: endpoint.hidden,
286292
deprecated: endpoint.deprecated,
287-
} as YfmToc;
293+
};
288294
}
289295

290296
export function sectionName(e: V3Endpoint): string {

src/includer/models.ts

Lines changed: 33 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
1-
import type {JSONSchema6, JSONSchema6Definition} from 'json-schema';
2-
import type {
3-
LeadingPageMode,
4-
SPEC_RENDER_MODE_DEFAULT,
5-
SPEC_RENDER_MODE_HIDDEN,
6-
SUPPORTED_ENUM_TYPES,
7-
} from './constants';
1+
import type {OpenAPIV3} from 'openapi-types';
2+
import type {LeadingPageMode, SPEC_RENDER_MODE_DEFAULT, SPEC_RENDER_MODE_HIDDEN} from './constants';
83

9-
export type VarsPreset = 'internal' | 'external';
4+
export type Dereference<T> = T extends OpenAPIV3.ReferenceObject
5+
? never
6+
: T extends object
7+
? T extends OpenAPIV3.ReferenceObject
8+
? never
9+
: // eslint-disable-next-line
10+
T extends any[]
11+
? DereferenceArray<T[number]>
12+
: DereferenceObject<T>
13+
: T;
14+
15+
type DereferenceArray<T> = Array<Dereference<T>>;
16+
17+
type DereferenceObject<T extends object> = {
18+
[K in keyof T]: Dereference<T[K]>;
19+
};
20+
21+
export type TableRef = string;
1022

1123
export type YfmPreset = Record<string, string>;
1224
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1325
export type Metadata = Record<string, any>;
1426

15-
export enum IncludeMode {
16-
ROOT_MERGE = 'root_merge',
17-
MERGE = 'merge',
18-
LINK = 'link',
19-
}
20-
2127
export interface Filter {
2228
when?: boolean | string;
2329
[key: string]: unknown;
@@ -29,68 +35,14 @@ export interface TextItem extends Filter {
2935

3036
export type TextItems = string | (TextItem | string)[];
3137

32-
export interface YfmToc extends Filter {
33-
name: string;
34-
href: string;
35-
items: YfmToc[];
36-
stage?: Stage;
37-
base?: string;
38-
title?: TextItems;
39-
include?: YfmTocInclude;
40-
id?: string;
41-
singlePage?: boolean;
42-
hidden?: boolean;
43-
deprecated?: boolean;
44-
}
45-
4638
export interface YfmTocItem extends Filter {
4739
name: string;
4840
href?: string;
4941
items?: YfmTocItem[];
50-
include?: YfmTocInclude;
51-
id?: string;
52-
}
53-
54-
export interface YfmTocInclude {
55-
repo: string;
56-
path: string;
57-
mode?: IncludeMode;
58-
includers?: YfmTocIncluders;
42+
hidden?: boolean;
43+
deprecated?: boolean;
5944
}
6045

61-
export type YfmTocIncluders = YfmTocIncluder[];
62-
63-
export type YfmTocIncluder = {
64-
name: 'openapi';
65-
// arbitrary includer parameters
66-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
67-
} & Record<string, unknown>;
68-
69-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
70-
export type Includer<FnParams = any> = {
71-
name: 'openapi';
72-
includerFunction: IncluderFunction<FnParams>;
73-
};
74-
75-
export type IncluderFunction<PassedParams> = (
76-
args: IncluderFunctionParams<PassedParams>,
77-
) => Promise<void>;
78-
79-
export type IncluderFunctionParams<PassedParams> = {
80-
/** item that contains include that uses includer */
81-
item: YfmToc;
82-
/** base read directory path */
83-
readBasePath: string;
84-
/** base write directory path */
85-
writeBasePath: string;
86-
/** toc with includer path */
87-
tocPath: string;
88-
vars: YfmPreset;
89-
/** arbitrary includer parameters */
90-
passedParams: PassedParams;
91-
index: number;
92-
};
93-
9446
export const titleDepths = [1, 2, 3, 4, 5, 6] as const;
9547

9648
export type TitleDepth = (typeof titleDepths)[number];
@@ -99,22 +51,16 @@ export type SandboxProps = {
9951
path: string;
10052
host?: string;
10153
method: Method;
102-
pathParams?: V3Parameters;
103-
searchParams?: V3Parameters;
104-
headers?: V3Parameters;
54+
pathParams?: Dereference<OpenAPIV3.ParameterObject>[];
55+
searchParams?: Dereference<OpenAPIV3.ParameterObject>[];
56+
headers?: Dereference<OpenAPIV3.ParameterObject>[];
10557
body?: string;
10658
bodyType?: string;
107-
schema?: OpenJSONSchema;
59+
schema?: Dereference<OpenAPIV3.SchemaObject>;
10860
security?: V3Security[];
10961
projectName: string;
11062
};
11163

112-
export type OpenAPISpec = {
113-
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
114-
[key: string]: any;
115-
security?: Array<Record<string, V3Security>>;
116-
};
117-
11864
export type V3SecurityApiKey = {
11965
type: 'apiKey';
12066
description: string;
@@ -145,24 +91,6 @@ export type V3Security = V3SecurityApiKey | V3SecurityOAuth2 | {type: string; de
14591

14692
export type V3SecurityType = V3Security['type'];
14793

148-
export type OpenAPIOperation = {
149-
summary?: string;
150-
description?: string;
151-
operationId?: string;
152-
deprecated?: boolean;
153-
tags?: string[];
154-
servers?: V3Servers;
155-
parameters?: V3Parameters;
156-
responses?: {};
157-
requestBody?: {
158-
required?: boolean;
159-
description?: string;
160-
content: {[ContentType: string]: {schema: OpenJSONSchema}};
161-
};
162-
security?: Array<Record<string, V3Security>>;
163-
'x-navtitle': string[];
164-
};
165-
16694
export type V3Info = {
16795
name: string;
16896
version: string;
@@ -198,14 +126,14 @@ export type V3Endpoints = V3Endpoint[];
198126
export type V3Endpoint = {
199127
id: string;
200128
operationId?: string;
201-
method: Method;
129+
method: string;
202130
path: string;
203131
tags: string[];
204132
summary?: string;
205133
description?: string;
206134
servers: V3Servers;
207-
parameters?: V3Parameters;
208-
responses?: V3Responses;
135+
parameters: Dereference<OpenAPIV3.ParameterObject>[];
136+
responses: V3Responses;
209137
requestBody?: V3Schema;
210138
security: V3Security[];
211139
noindex?: boolean;
@@ -238,28 +166,10 @@ export type V3Server = {
238166
description?: string;
239167
};
240168

241-
// eslint-disable-next-line @typescript-eslint/no-redeclare
242-
export type V3Parameters = V3Parameter[];
243-
244169
export type In = 'path' | 'query' | 'header' | 'cookie';
245170

246171
export type Primitive = string | number | boolean;
247172

248-
export type V3Parameter = {
249-
name: string;
250-
in: In;
251-
required: boolean;
252-
description?: string;
253-
example?: Primitive;
254-
default?: Primitive;
255-
schema: OpenJSONSchema;
256-
257-
readOnly?: boolean;
258-
writeOnly?: boolean;
259-
// vendor extensions
260-
'x-hidden'?: boolean;
261-
};
262-
263173
export type V3Responses = V3Response[];
264174

265175
// eslint-disable-next-line @typescript-eslint/no-redeclare
@@ -268,41 +178,21 @@ export type V3Response = {
268178
code: string;
269179
statusText: string;
270180
description: string;
271-
schemas?: V3Schemas;
181+
schemas?: V3Schema[];
182+
deprecated?: boolean;
272183
};
273184

274-
export type V3Schemas = V3Schema[];
275-
276185
export type V3Schema = {
277186
type: string;
278-
schema: OpenJSONSchema;
187+
schema: OpenAPIV3.SchemaObject;
279188
};
280189

281-
export type Refs = {[typeName: string]: OpenJSONSchema};
282-
283-
export type JsType =
284-
| 'string'
285-
| 'number'
286-
| 'bigint'
287-
| 'boolean'
288-
| 'symbol'
289-
| 'undefined'
290-
| 'object'
291-
| 'function';
190+
export type Refs = {[typeName: string]: OpenAPIV3.SchemaObject};
292191

293192
export type LeadingPageSpecRenderMode =
294193
| typeof SPEC_RENDER_MODE_DEFAULT
295194
| typeof SPEC_RENDER_MODE_HIDDEN;
296195

297-
export type SupportedEnumType = (typeof SUPPORTED_ENUM_TYPES)[number];
298-
299-
export enum Stage {
300-
NEW = 'new',
301-
PREVIEW = 'preview',
302-
TECH_PREVIEW = 'tech-preview',
303-
SKIP = 'skip',
304-
}
305-
306196
export type LeadingPageParams = {
307197
name?: string;
308198
mode?: LeadingPageMode;
@@ -340,32 +230,6 @@ export type OpenApiIncluderParams = {
340230
};
341231
};
342232

343-
export type OpenJSONSchema = JSONSchema6 & {
344-
_runtime?: true;
345-
_emptyDescription?: true;
346-
example?: unknown;
347-
deprecated?: boolean;
348-
properties?: {
349-
[key: string]: JSONSchema6Definition & {
350-
readOnly?: boolean;
351-
writeOnly?: boolean;
352-
'x-hidden'?: boolean;
353-
};
354-
};
355-
};
356-
export type OpenJSONSchemaDefinition = OpenJSONSchema | boolean;
357-
358-
export type FoundRefType = {
359-
ref: string;
360-
};
361-
export type BaseJSONSchemaType = Exclude<OpenJSONSchema['type'], undefined>;
362-
export type JSONSchemaUnionType = {
363-
ref?: string;
364-
/* Not oneOf because of collision with OpenJSONSchema['oneOf'] */
365-
unionOf: JSONSchemaType[];
366-
};
367-
export type JSONSchemaType = BaseJSONSchemaType | JSONSchemaUnionType | FoundRefType;
368-
369233
export type Run = {
370234
input: string;
371235
vars: {

0 commit comments

Comments
 (0)