2023-12-02 12:00:05 +00:00
|
|
|
import { mkdir, writeFile } from 'fs/promises';
|
2024-01-13 07:54:25 +00:00
|
|
|
import { OpenAPIV3_1 } from 'openapi-types';
|
2023-12-02 12:00:05 +00:00
|
|
|
import { toPascal } from 'ts-case-convert';
|
2024-01-13 07:54:25 +00:00
|
|
|
import OpenAPIParser from '@readme/openapi-parser';
|
2023-12-02 12:00:05 +00:00
|
|
|
import openapiTS from 'openapi-typescript';
|
|
|
|
|
|
|
|
async function generateBaseTypes(
|
2024-01-13 07:54:25 +00:00
|
|
|
openApiDocs: OpenAPIV3_1.Document,
|
2023-12-02 12:00:05 +00:00
|
|
|
openApiJsonPath: string,
|
|
|
|
typeFileName: string,
|
|
|
|
) {
|
|
|
|
const disabledLints = [
|
|
|
|
'@typescript-eslint/naming-convention',
|
|
|
|
'@typescript-eslint/no-explicit-any',
|
|
|
|
];
|
|
|
|
|
|
|
|
const lines: string[] = [];
|
|
|
|
for (const lint of disabledLints) {
|
|
|
|
lines.push(`/* eslint ${lint}: 0 */`);
|
|
|
|
}
|
|
|
|
lines.push('');
|
|
|
|
|
|
|
|
const generatedTypes = await openapiTS(openApiJsonPath, { exportType: true });
|
|
|
|
lines.push(generatedTypes);
|
|
|
|
lines.push('');
|
|
|
|
|
|
|
|
await writeFile(typeFileName, lines.join('\n'));
|
|
|
|
}
|
|
|
|
|
|
|
|
async function generateSchemaEntities(
|
2024-01-13 07:54:25 +00:00
|
|
|
openApiDocs: OpenAPIV3_1.Document,
|
2023-12-02 12:00:05 +00:00
|
|
|
typeFileName: string,
|
|
|
|
outputPath: string,
|
|
|
|
) {
|
|
|
|
if (!openApiDocs.components?.schemas) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const schemas = openApiDocs.components.schemas;
|
|
|
|
const schemaNames = Object.keys(schemas);
|
|
|
|
const typeAliasLines: string[] = [];
|
|
|
|
|
|
|
|
typeAliasLines.push(`import { components } from '${toImportPath(typeFileName)}';`);
|
|
|
|
typeAliasLines.push(
|
|
|
|
...schemaNames.map(it => `export type ${it} = components['schemas']['${it}'];`),
|
|
|
|
);
|
|
|
|
typeAliasLines.push('');
|
|
|
|
|
|
|
|
await writeFile(outputPath, typeAliasLines.join('\n'));
|
|
|
|
}
|
|
|
|
|
|
|
|
async function generateEndpoints(
|
2024-01-13 07:54:25 +00:00
|
|
|
openApiDocs: OpenAPIV3_1.Document,
|
2023-12-02 12:00:05 +00:00
|
|
|
typeFileName: string,
|
|
|
|
entitiesOutputPath: string,
|
|
|
|
endpointOutputPath: string,
|
|
|
|
) {
|
|
|
|
const endpoints: Endpoint[] = [];
|
|
|
|
|
|
|
|
// misskey-jsはPOST固定で送っているので、こちらも決め打ちする。別メソッドに対応することがあればこちらも直す必要あり
|
2024-01-13 07:54:25 +00:00
|
|
|
const paths = openApiDocs.paths ?? {};
|
2023-12-02 12:00:05 +00:00
|
|
|
const postPathItems = Object.keys(paths)
|
2024-03-25 03:11:10 +00:00
|
|
|
.map(it => ({
|
|
|
|
_path_: it.replace(/^\//, ''),
|
|
|
|
...paths[it]?.post,
|
|
|
|
}))
|
2023-12-02 12:00:05 +00:00
|
|
|
.filter(filterUndefined);
|
|
|
|
|
|
|
|
for (const operation of postPathItems) {
|
2024-03-25 03:11:10 +00:00
|
|
|
const path = operation._path_;
|
2023-12-02 12:00:05 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
const operationId = operation.operationId!;
|
2024-03-25 03:11:10 +00:00
|
|
|
const endpoint = new Endpoint(path);
|
2023-12-02 12:00:05 +00:00
|
|
|
endpoints.push(endpoint);
|
|
|
|
|
|
|
|
if (isRequestBodyObject(operation.requestBody)) {
|
|
|
|
const reqContent = operation.requestBody.content;
|
|
|
|
const supportMediaTypes = Object.keys(reqContent);
|
|
|
|
if (supportMediaTypes.length > 0) {
|
|
|
|
// いまのところ複数のメディアタイプをとるエンドポイントは無いので決め打ちする
|
|
|
|
endpoint.request = new OperationTypeAlias(
|
|
|
|
operationId,
|
2024-03-25 03:11:10 +00:00
|
|
|
path,
|
2023-12-02 12:00:05 +00:00
|
|
|
supportMediaTypes[0],
|
|
|
|
OperationsAliasType.REQUEST,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-25 03:11:10 +00:00
|
|
|
if (operation.responses && isResponseObject(operation.responses['200']) && operation.responses['200'].content) {
|
2023-12-02 12:00:05 +00:00
|
|
|
const resContent = operation.responses['200'].content;
|
|
|
|
const supportMediaTypes = Object.keys(resContent);
|
|
|
|
if (supportMediaTypes.length > 0) {
|
|
|
|
// いまのところ複数のメディアタイプを返すエンドポイントは無いので決め打ちする
|
|
|
|
endpoint.response = new OperationTypeAlias(
|
|
|
|
operationId,
|
2024-03-25 03:11:10 +00:00
|
|
|
path,
|
2023-12-02 12:00:05 +00:00
|
|
|
supportMediaTypes[0],
|
|
|
|
OperationsAliasType.RESPONSE,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const entitiesOutputLine: string[] = [];
|
|
|
|
|
2024-03-24 07:46:15 +00:00
|
|
|
entitiesOutputLine.push('/* eslint @typescript-eslint/naming-convention: 0 */');
|
|
|
|
|
2023-12-02 12:00:05 +00:00
|
|
|
entitiesOutputLine.push(`import { operations } from '${toImportPath(typeFileName)}';`);
|
|
|
|
entitiesOutputLine.push('');
|
|
|
|
|
|
|
|
entitiesOutputLine.push(new EmptyTypeAlias(OperationsAliasType.REQUEST).toLine());
|
|
|
|
entitiesOutputLine.push(new EmptyTypeAlias(OperationsAliasType.RESPONSE).toLine());
|
|
|
|
entitiesOutputLine.push('');
|
|
|
|
|
|
|
|
const entities = endpoints
|
|
|
|
.flatMap(it => [it.request, it.response].filter(i => i))
|
|
|
|
.filter(filterUndefined);
|
|
|
|
entitiesOutputLine.push(...entities.map(it => it.toLine()));
|
|
|
|
entitiesOutputLine.push('');
|
|
|
|
|
|
|
|
await writeFile(entitiesOutputPath, entitiesOutputLine.join('\n'));
|
|
|
|
|
|
|
|
const endpointOutputLine: string[] = [];
|
|
|
|
|
|
|
|
endpointOutputLine.push('import type {');
|
|
|
|
endpointOutputLine.push(
|
|
|
|
...[emptyRequest, emptyResponse, ...entities].map(it => '\t' + it.generateName() + ','),
|
|
|
|
);
|
|
|
|
endpointOutputLine.push(`} from '${toImportPath(entitiesOutputPath)}';`);
|
|
|
|
endpointOutputLine.push('');
|
|
|
|
|
|
|
|
endpointOutputLine.push('export type Endpoints = {');
|
|
|
|
endpointOutputLine.push(
|
|
|
|
...endpoints.map(it => '\t' + it.toLine()),
|
|
|
|
);
|
|
|
|
endpointOutputLine.push('}');
|
|
|
|
endpointOutputLine.push('');
|
|
|
|
|
|
|
|
await writeFile(endpointOutputPath, endpointOutputLine.join('\n'));
|
|
|
|
}
|
|
|
|
|
2023-12-08 06:15:17 +00:00
|
|
|
async function generateApiClientJSDoc(
|
2024-01-13 07:54:25 +00:00
|
|
|
openApiDocs: OpenAPIV3_1.Document,
|
2023-12-08 06:15:17 +00:00
|
|
|
apiClientFileName: string,
|
|
|
|
endpointsFileName: string,
|
|
|
|
warningsOutputPath: string,
|
|
|
|
) {
|
2024-03-25 03:11:10 +00:00
|
|
|
const endpoints: {
|
|
|
|
operationId: string;
|
|
|
|
path: string;
|
|
|
|
description: string;
|
|
|
|
}[] = [];
|
2023-12-08 06:15:17 +00:00
|
|
|
|
|
|
|
// misskey-jsはPOST固定で送っているので、こちらも決め打ちする。別メソッドに対応することがあればこちらも直す必要あり
|
2024-01-13 07:54:25 +00:00
|
|
|
const paths = openApiDocs.paths ?? {};
|
2023-12-08 06:15:17 +00:00
|
|
|
const postPathItems = Object.keys(paths)
|
2024-03-25 03:11:10 +00:00
|
|
|
.map(it => ({
|
|
|
|
_path_: it.replace(/^\//, ''),
|
|
|
|
...paths[it]?.post,
|
|
|
|
}))
|
2023-12-08 06:15:17 +00:00
|
|
|
.filter(filterUndefined);
|
|
|
|
|
|
|
|
for (const operation of postPathItems) {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
const operationId = operation.operationId!;
|
|
|
|
|
|
|
|
if (operation.description) {
|
|
|
|
endpoints.push({
|
|
|
|
operationId: operationId,
|
2024-03-25 03:11:10 +00:00
|
|
|
path: operation._path_,
|
2023-12-08 06:15:17 +00:00
|
|
|
description: operation.description,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const endpointOutputLine: string[] = [];
|
|
|
|
|
|
|
|
endpointOutputLine.push(`import type { SwitchCaseResponseType } from '${toImportPath(apiClientFileName)}';`);
|
|
|
|
endpointOutputLine.push(`import type { Endpoints } from '${toImportPath(endpointsFileName)}';`);
|
|
|
|
endpointOutputLine.push('');
|
|
|
|
|
|
|
|
endpointOutputLine.push(`declare module '${toImportPath(apiClientFileName)}' {`);
|
|
|
|
endpointOutputLine.push(' export interface APIClient {');
|
|
|
|
for (let i = 0; i < endpoints.length; i++) {
|
|
|
|
const endpoint = endpoints[i];
|
|
|
|
|
|
|
|
endpointOutputLine.push(
|
|
|
|
' /**',
|
|
|
|
` * ${endpoint.description.split('\n').join('\n * ')}`,
|
|
|
|
' */',
|
2024-03-25 03:11:10 +00:00
|
|
|
` request<E extends '${endpoint.path}', P extends Endpoints[E][\'req\']>(`,
|
2023-12-08 06:15:17 +00:00
|
|
|
' endpoint: E,',
|
|
|
|
' params: P,',
|
|
|
|
' credential?: string | null,',
|
|
|
|
' ): Promise<SwitchCaseResponseType<E, P>>;',
|
|
|
|
);
|
|
|
|
|
|
|
|
if (i < endpoints.length - 1) {
|
|
|
|
endpointOutputLine.push('\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
endpointOutputLine.push(' }');
|
|
|
|
endpointOutputLine.push('}');
|
|
|
|
endpointOutputLine.push('');
|
|
|
|
|
|
|
|
await writeFile(warningsOutputPath, endpointOutputLine.join('\n'));
|
|
|
|
}
|
|
|
|
|
2024-01-13 07:54:25 +00:00
|
|
|
function isRequestBodyObject(value: unknown): value is OpenAPIV3_1.RequestBodyObject {
|
2023-12-02 12:00:05 +00:00
|
|
|
if (!value) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-01-13 07:54:25 +00:00
|
|
|
const { content } = value as Record<keyof OpenAPIV3_1.RequestBodyObject, unknown>;
|
2023-12-02 12:00:05 +00:00
|
|
|
return content !== undefined;
|
|
|
|
}
|
|
|
|
|
2024-01-13 07:54:25 +00:00
|
|
|
function isResponseObject(value: unknown): value is OpenAPIV3_1.ResponseObject {
|
2023-12-02 12:00:05 +00:00
|
|
|
if (!value) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-01-13 07:54:25 +00:00
|
|
|
const { description } = value as Record<keyof OpenAPIV3_1.ResponseObject, unknown>;
|
2023-12-02 12:00:05 +00:00
|
|
|
return description !== undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
function filterUndefined<T>(item: T): item is Exclude<T, undefined> {
|
|
|
|
return item !== undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
function toImportPath(fileName: string, fromPath = '/built/autogen', toPath = ''): string {
|
|
|
|
return fileName.replace(fromPath, toPath).replace('.ts', '.js');
|
|
|
|
}
|
|
|
|
|
|
|
|
enum OperationsAliasType {
|
|
|
|
REQUEST = 'Request',
|
|
|
|
RESPONSE = 'Response'
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IOperationTypeAlias {
|
|
|
|
readonly type: OperationsAliasType
|
|
|
|
|
|
|
|
generateName(): string
|
|
|
|
|
|
|
|
toLine(): string
|
|
|
|
}
|
|
|
|
|
|
|
|
class OperationTypeAlias implements IOperationTypeAlias {
|
|
|
|
public readonly operationId: string;
|
2024-03-25 03:11:10 +00:00
|
|
|
public readonly path: string;
|
2023-12-02 12:00:05 +00:00
|
|
|
public readonly mediaType: string;
|
|
|
|
public readonly type: OperationsAliasType;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
operationId: string,
|
2024-03-25 03:11:10 +00:00
|
|
|
path: string,
|
2023-12-02 12:00:05 +00:00
|
|
|
mediaType: string,
|
|
|
|
type: OperationsAliasType,
|
|
|
|
) {
|
|
|
|
this.operationId = operationId;
|
2024-03-25 03:11:10 +00:00
|
|
|
this.path = path;
|
2023-12-02 12:00:05 +00:00
|
|
|
this.mediaType = mediaType;
|
|
|
|
this.type = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
generateName(): string {
|
2024-03-25 03:11:10 +00:00
|
|
|
const nameBase = this.path.replace(/\//g, '-');
|
2023-12-02 12:00:05 +00:00
|
|
|
return toPascal(nameBase + this.type);
|
|
|
|
}
|
|
|
|
|
|
|
|
toLine(): string {
|
|
|
|
const name = this.generateName();
|
|
|
|
return (this.type === OperationsAliasType.REQUEST)
|
|
|
|
? `export type ${name} = operations['${this.operationId}']['requestBody']['content']['${this.mediaType}'];`
|
|
|
|
: `export type ${name} = operations['${this.operationId}']['responses']['200']['content']['${this.mediaType}'];`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class EmptyTypeAlias implements IOperationTypeAlias {
|
|
|
|
readonly type: OperationsAliasType;
|
|
|
|
|
|
|
|
constructor(type: OperationsAliasType) {
|
|
|
|
this.type = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
generateName(): string {
|
|
|
|
return 'Empty' + this.type;
|
|
|
|
}
|
|
|
|
|
|
|
|
toLine(): string {
|
|
|
|
const name = this.generateName();
|
|
|
|
return `export type ${name} = Record<string, unknown> | undefined;`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const emptyRequest = new EmptyTypeAlias(OperationsAliasType.REQUEST);
|
|
|
|
const emptyResponse = new EmptyTypeAlias(OperationsAliasType.RESPONSE);
|
|
|
|
|
|
|
|
class Endpoint {
|
2024-03-25 03:11:10 +00:00
|
|
|
public readonly path: string;
|
2023-12-02 12:00:05 +00:00
|
|
|
public request?: IOperationTypeAlias;
|
|
|
|
public response?: IOperationTypeAlias;
|
|
|
|
|
2024-03-25 03:11:10 +00:00
|
|
|
constructor(path: string) {
|
|
|
|
this.path = path;
|
2023-12-02 12:00:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
toLine(): string {
|
|
|
|
const reqName = this.request?.generateName() ?? emptyRequest.generateName();
|
|
|
|
const resName = this.response?.generateName() ?? emptyResponse.generateName();
|
|
|
|
|
2024-03-25 03:11:10 +00:00
|
|
|
return `'${this.path}': { req: ${reqName}; res: ${resName} };`;
|
2023-12-02 12:00:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
const generatePath = './built/autogen';
|
|
|
|
await mkdir(generatePath, { recursive: true });
|
|
|
|
|
|
|
|
const openApiJsonPath = './api.json';
|
2024-01-13 07:54:25 +00:00
|
|
|
const openApiDocs = await OpenAPIParser.parse(openApiJsonPath) as OpenAPIV3_1.Document;
|
2023-12-02 12:00:05 +00:00
|
|
|
|
|
|
|
const typeFileName = './built/autogen/types.ts';
|
|
|
|
await generateBaseTypes(openApiDocs, openApiJsonPath, typeFileName);
|
|
|
|
|
|
|
|
const modelFileName = `${generatePath}/models.ts`;
|
|
|
|
await generateSchemaEntities(openApiDocs, typeFileName, modelFileName);
|
|
|
|
|
|
|
|
const entitiesFileName = `${generatePath}/entities.ts`;
|
|
|
|
const endpointFileName = `${generatePath}/endpoint.ts`;
|
|
|
|
await generateEndpoints(openApiDocs, typeFileName, entitiesFileName, endpointFileName);
|
2023-12-08 06:15:17 +00:00
|
|
|
|
|
|
|
const apiClientWarningFileName = `${generatePath}/apiClientJSDoc.ts`;
|
|
|
|
await generateApiClientJSDoc(openApiDocs, '../api.ts', endpointFileName, apiClientWarningFileName);
|
2023-12-02 12:00:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
main();
|