Skip to content

Commit

Permalink
feat: 接口定义优化
Browse files Browse the repository at this point in the history
  • Loading branch information
wangj committed May 13, 2022
1 parent b266113 commit 96902f0
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 19 deletions.
8 changes: 3 additions & 5 deletions src/load.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import fs from "fs-extra";
import path from "path";
import { Definition } from "types/definitions.js";
import Swagger from "./types/index";
import { URL } from "url";
import undici from "undici";
import ora from "ora";

export function load(
input: string
): Promise<{ definitions: { [k: string]: Definition } }> {
export function load(input: string): Promise<Swagger> {
if (isUrl(input)) {
return fetch(input);
} else {
Expand Down Expand Up @@ -56,6 +54,6 @@ function fetch(url: string) {
});
})
.finally(() => {
spinner.clear();
spinner.stop();
});
}
3 changes: 3 additions & 0 deletions src/transform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Swagger from "types";

export function convert(swagger: Swagger) {}
14 changes: 0 additions & 14 deletions src/types/definitions.ts

This file was deleted.

73 changes: 73 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
type VALUE_TYPE_PRIMITIVE = "string" | "integer";
type VALUE_TYPE =
| "array"
| "boolean"
| "integer"
| "number"
| "object"
| "string"
| "double"
| "float"
| "long";
type VALUE_FORMAT = "int64" | "int32";

export interface ObjectScheme {
title?: string;
description?: string;
type?: VALUE_TYPE;
enum?: string[];
format?: VALUE_FORMAT;
properties?: {
[key: string]: ObjectScheme;
};
items?: ObjectScheme;
$ref?: any;
}
export type Parameter = {
name: string;
in: "path" | "body" | "query" | "formData" | "header";
required?: boolean;
description?: string;
type?: VALUE_TYPE_PRIMITIVE;
format?: VALUE_FORMAT;
schema: ObjectScheme;
};

interface Response {
description: string;
schema: ObjectScheme;
}

export interface SwaggerRequest {
summary: string;
description?: string;
operationId: string;
responses: {
200: Response;
default: Response;
};
parameters: Array<Parameter>;
}

export interface SwaggerPath {
get?: SwaggerRequest;
post?: SwaggerRequest;
put?: SwaggerRequest;
delete?: SwaggerRequest;
}

export default interface Swagger {
swagger: "2.0" | "3.0";
basePath?: string;
info: {
title: string;
version: string;
description?: string;
};
paths: {
[path: string]: SwaggerPath;
};
definitions: {
[definition: string]: ObjectScheme;
};
}

0 comments on commit 96902f0

Please sign in to comment.