mirror of
https://github.com/m1ngsama/FUJI.git
synced 2025-12-24 10:51:27 +00:00
add openapi-ts
This commit is contained in:
parent
f6c10df351
commit
adca70b4a1
18 changed files with 8813 additions and 6437 deletions
|
|
@ -54,6 +54,12 @@ export default [
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
ignores: ["dist/**/*", "public/**/*", "node_modules/**/*"],
|
ignores: [
|
||||||
|
"dist/**/*",
|
||||||
|
"public/**/*",
|
||||||
|
"node_modules/**/*",
|
||||||
|
"src/utils/active/**/*",
|
||||||
|
"openapi-ts.active.config.ts",
|
||||||
|
],
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
|
||||||
21
openapi-ts.active.config.ts
Normal file
21
openapi-ts.active.config.ts
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
import { defineConfig } from '@hey-api/openapi-ts'
|
||||||
|
export default defineConfig({
|
||||||
|
input: 'https://active.nbtca.space/openapi.json',
|
||||||
|
output: {
|
||||||
|
path: './src/utils/active',
|
||||||
|
format: 'prettier',
|
||||||
|
lint: 'eslint',
|
||||||
|
},
|
||||||
|
client: 'legacy/fetch',
|
||||||
|
types: {
|
||||||
|
dates: true,
|
||||||
|
enums: 'typescript+namespace',
|
||||||
|
name: 'PascalCase',
|
||||||
|
tree: false,
|
||||||
|
},
|
||||||
|
services: {
|
||||||
|
response: 'body',
|
||||||
|
},
|
||||||
|
schemas: false,
|
||||||
|
name: 'ApiClient',
|
||||||
|
})
|
||||||
|
|
@ -11,7 +11,7 @@
|
||||||
"astro": "astro",
|
"astro": "astro",
|
||||||
"prepare": "husky",
|
"prepare": "husky",
|
||||||
"gen-type": "openapi-typescript http://localhost:4000/openapi-3.0.json -o ./src/types/saturday.d.ts",
|
"gen-type": "openapi-typescript http://localhost:4000/openapi-3.0.json -o ./src/types/saturday.d.ts",
|
||||||
"active": "openapi-typescript https://active.nbtca.space/openapi.json -o ./src/types/active.d.ts"
|
"active": "openapi-ts -f openapi-ts.active.config.ts"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@astrojs/react": "^3.3.1",
|
"@astrojs/react": "^3.3.1",
|
||||||
|
|
@ -46,6 +46,7 @@
|
||||||
"@astrojs/markdown-remark": "^5.2.0",
|
"@astrojs/markdown-remark": "^5.2.0",
|
||||||
"@cspell/eslint-plugin": "^8.8.1",
|
"@cspell/eslint-plugin": "^8.8.1",
|
||||||
"@eslint/js": "^9.11.0",
|
"@eslint/js": "^9.11.0",
|
||||||
|
"@hey-api/openapi-ts": "^0.53.3",
|
||||||
"@types/eslint__js": "^8.42.3",
|
"@types/eslint__js": "^8.42.3",
|
||||||
"@types/md5": "^2.3.5",
|
"@types/md5": "^2.3.5",
|
||||||
"@types/qrcode": "^1.5.5",
|
"@types/qrcode": "^1.5.5",
|
||||||
|
|
|
||||||
14019
pnpm-lock.yaml
14019
pnpm-lock.yaml
File diff suppressed because it is too large
Load diff
133
src/types/active.d.ts
vendored
133
src/types/active.d.ts
vendored
|
|
@ -1,133 +0,0 @@
|
||||||
/**
|
|
||||||
* This file was auto-generated by openapi-typescript.
|
|
||||||
* Do not make direct changes to the file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
export interface paths {
|
|
||||||
"/api/freshman": {
|
|
||||||
parameters: {
|
|
||||||
query?: never
|
|
||||||
header?: never
|
|
||||||
path?: never
|
|
||||||
cookie?: never
|
|
||||||
}
|
|
||||||
/** 获取新人列表 */
|
|
||||||
get: operations["get_FreshmanList"]
|
|
||||||
put?: never
|
|
||||||
/** 添加新人 */
|
|
||||||
post: operations["post_FreshmanAdd"]
|
|
||||||
delete?: never
|
|
||||||
options?: never
|
|
||||||
head?: never
|
|
||||||
patch?: never
|
|
||||||
trace?: never
|
|
||||||
}
|
|
||||||
}
|
|
||||||
export type webhooks = Record<string, never>
|
|
||||||
export interface components {
|
|
||||||
schemas: never
|
|
||||||
responses: never
|
|
||||||
parameters: never
|
|
||||||
requestBodies: never
|
|
||||||
headers: never
|
|
||||||
pathItems: never
|
|
||||||
}
|
|
||||||
export type $defs = Record<string, never>
|
|
||||||
export interface operations {
|
|
||||||
get_FreshmanList: {
|
|
||||||
parameters: {
|
|
||||||
query?: never
|
|
||||||
header?: never
|
|
||||||
path?: never
|
|
||||||
cookie?: never
|
|
||||||
}
|
|
||||||
requestBody?: never
|
|
||||||
responses: {
|
|
||||||
/** @description Returns a list of tasks */
|
|
||||||
200: {
|
|
||||||
headers: {
|
|
||||||
[name: string]: unknown
|
|
||||||
}
|
|
||||||
content: {
|
|
||||||
"application/json": {
|
|
||||||
success: boolean
|
|
||||||
list: {
|
|
||||||
/** @example lorem */
|
|
||||||
name: string
|
|
||||||
/** @example 3240000000 */
|
|
||||||
number: string
|
|
||||||
/** @example 计算机科学与技术 */
|
|
||||||
major: string
|
|
||||||
/** @example 计科2001 */
|
|
||||||
class: string
|
|
||||||
/** @example xxx@qq.com */
|
|
||||||
email: string
|
|
||||||
/** @example 12345678901 */
|
|
||||||
phone: string
|
|
||||||
/** @example 123456789 */
|
|
||||||
qq: string
|
|
||||||
}[]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
post_FreshmanAdd: {
|
|
||||||
parameters: {
|
|
||||||
query?: never
|
|
||||||
header?: never
|
|
||||||
path?: never
|
|
||||||
cookie?: never
|
|
||||||
}
|
|
||||||
requestBody?: {
|
|
||||||
content: {
|
|
||||||
"application/json": {
|
|
||||||
/** @example lorem */
|
|
||||||
name: string
|
|
||||||
/** @example 3240000000 */
|
|
||||||
number: string
|
|
||||||
/** @example 计算机科学与技术 */
|
|
||||||
major: string
|
|
||||||
/** @example 计科2001 */
|
|
||||||
class: string
|
|
||||||
/** @example xxx@qq.com */
|
|
||||||
email: string
|
|
||||||
/** @example 12345678901 */
|
|
||||||
phone: string
|
|
||||||
/** @example 123456789 */
|
|
||||||
qq: string
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
responses: {
|
|
||||||
/** @description Returns the created task */
|
|
||||||
200: {
|
|
||||||
headers: {
|
|
||||||
[name: string]: unknown
|
|
||||||
}
|
|
||||||
content: {
|
|
||||||
"application/json": {
|
|
||||||
success: boolean
|
|
||||||
result?: {
|
|
||||||
/** @example lorem */
|
|
||||||
name: string
|
|
||||||
/** @example 3240000000 */
|
|
||||||
number: string
|
|
||||||
/** @example 计算机科学与技术 */
|
|
||||||
major: string
|
|
||||||
/** @example 计科2001 */
|
|
||||||
class: string
|
|
||||||
/** @example xxx@qq.com */
|
|
||||||
email: string
|
|
||||||
/** @example 12345678901 */
|
|
||||||
phone: string
|
|
||||||
/** @example 123456789 */
|
|
||||||
qq: string
|
|
||||||
}
|
|
||||||
error?: string
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
37
src/utils/active/ApiClient.ts
Normal file
37
src/utils/active/ApiClient.ts
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
import type { BaseHttpRequest } from "./core/BaseHttpRequest";
|
||||||
|
import type { OpenAPIConfig } from "./core/OpenAPI";
|
||||||
|
import { Interceptors } from "./core/OpenAPI";
|
||||||
|
import { FetchHttpRequest } from "./core/FetchHttpRequest";
|
||||||
|
|
||||||
|
import { FreshmanService } from "./services.gen";
|
||||||
|
|
||||||
|
type HttpRequestConstructor = new (config: OpenAPIConfig) => BaseHttpRequest;
|
||||||
|
|
||||||
|
export class ApiClient {
|
||||||
|
public readonly freshman: FreshmanService;
|
||||||
|
|
||||||
|
public readonly request: BaseHttpRequest;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
config?: Partial<OpenAPIConfig>,
|
||||||
|
HttpRequest: HttpRequestConstructor = FetchHttpRequest,
|
||||||
|
) {
|
||||||
|
this.request = new HttpRequest({
|
||||||
|
BASE: config?.BASE ?? "",
|
||||||
|
VERSION: config?.VERSION ?? "1.0.0",
|
||||||
|
WITH_CREDENTIALS: config?.WITH_CREDENTIALS ?? false,
|
||||||
|
CREDENTIALS: config?.CREDENTIALS ?? "include",
|
||||||
|
TOKEN: config?.TOKEN,
|
||||||
|
USERNAME: config?.USERNAME,
|
||||||
|
PASSWORD: config?.PASSWORD,
|
||||||
|
HEADERS: config?.HEADERS,
|
||||||
|
ENCODE_PATH: config?.ENCODE_PATH,
|
||||||
|
interceptors: {
|
||||||
|
request: config?.interceptors?.request ?? new Interceptors(),
|
||||||
|
response: config?.interceptors?.response ?? new Interceptors(),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
this.freshman = new FreshmanService(this.request);
|
||||||
|
}
|
||||||
|
}
|
||||||
25
src/utils/active/core/ApiError.ts
Normal file
25
src/utils/active/core/ApiError.ts
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
import type { ApiRequestOptions } from "./ApiRequestOptions";
|
||||||
|
import type { ApiResult } from "./ApiResult";
|
||||||
|
|
||||||
|
export class ApiError extends Error {
|
||||||
|
public readonly url: string;
|
||||||
|
public readonly status: number;
|
||||||
|
public readonly statusText: string;
|
||||||
|
public readonly body: unknown;
|
||||||
|
public readonly request: ApiRequestOptions;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
request: ApiRequestOptions,
|
||||||
|
response: ApiResult,
|
||||||
|
message: string,
|
||||||
|
) {
|
||||||
|
super(message);
|
||||||
|
|
||||||
|
this.name = "ApiError";
|
||||||
|
this.url = response.url;
|
||||||
|
this.status = response.status;
|
||||||
|
this.statusText = response.statusText;
|
||||||
|
this.body = response.body;
|
||||||
|
this.request = request;
|
||||||
|
}
|
||||||
|
}
|
||||||
21
src/utils/active/core/ApiRequestOptions.ts
Normal file
21
src/utils/active/core/ApiRequestOptions.ts
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
export type ApiRequestOptions<T = unknown> = {
|
||||||
|
readonly body?: any;
|
||||||
|
readonly cookies?: Record<string, unknown>;
|
||||||
|
readonly errors?: Record<number | string, string>;
|
||||||
|
readonly formData?: Record<string, unknown> | any[] | Blob | File;
|
||||||
|
readonly headers?: Record<string, unknown>;
|
||||||
|
readonly mediaType?: string;
|
||||||
|
readonly method:
|
||||||
|
| "DELETE"
|
||||||
|
| "GET"
|
||||||
|
| "HEAD"
|
||||||
|
| "OPTIONS"
|
||||||
|
| "PATCH"
|
||||||
|
| "POST"
|
||||||
|
| "PUT";
|
||||||
|
readonly path?: Record<string, unknown>;
|
||||||
|
readonly query?: Record<string, unknown>;
|
||||||
|
readonly responseHeader?: string;
|
||||||
|
readonly responseTransformer?: (data: unknown) => Promise<T>;
|
||||||
|
readonly url: string;
|
||||||
|
};
|
||||||
7
src/utils/active/core/ApiResult.ts
Normal file
7
src/utils/active/core/ApiResult.ts
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
export type ApiResult<TData = any> = {
|
||||||
|
readonly body: TData;
|
||||||
|
readonly ok: boolean;
|
||||||
|
readonly status: number;
|
||||||
|
readonly statusText: string;
|
||||||
|
readonly url: string;
|
||||||
|
};
|
||||||
11
src/utils/active/core/BaseHttpRequest.ts
Normal file
11
src/utils/active/core/BaseHttpRequest.ts
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
import type { ApiRequestOptions } from "./ApiRequestOptions";
|
||||||
|
import type { CancelablePromise } from "./CancelablePromise";
|
||||||
|
import type { OpenAPIConfig } from "./OpenAPI";
|
||||||
|
|
||||||
|
export abstract class BaseHttpRequest {
|
||||||
|
constructor(public readonly config: OpenAPIConfig) {}
|
||||||
|
|
||||||
|
public abstract request<T>(
|
||||||
|
options: ApiRequestOptions<T>,
|
||||||
|
): CancelablePromise<T>;
|
||||||
|
}
|
||||||
126
src/utils/active/core/CancelablePromise.ts
Normal file
126
src/utils/active/core/CancelablePromise.ts
Normal file
|
|
@ -0,0 +1,126 @@
|
||||||
|
export class CancelError extends Error {
|
||||||
|
constructor(message: string) {
|
||||||
|
super(message);
|
||||||
|
this.name = "CancelError";
|
||||||
|
}
|
||||||
|
|
||||||
|
public get isCancelled(): boolean {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface OnCancel {
|
||||||
|
readonly isResolved: boolean;
|
||||||
|
readonly isRejected: boolean;
|
||||||
|
readonly isCancelled: boolean;
|
||||||
|
|
||||||
|
(cancelHandler: () => void): void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class CancelablePromise<T> implements Promise<T> {
|
||||||
|
private _isResolved: boolean;
|
||||||
|
private _isRejected: boolean;
|
||||||
|
private _isCancelled: boolean;
|
||||||
|
readonly cancelHandlers: (() => void)[];
|
||||||
|
readonly promise: Promise<T>;
|
||||||
|
private _resolve?: (value: T | PromiseLike<T>) => void;
|
||||||
|
private _reject?: (reason?: unknown) => void;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
executor: (
|
||||||
|
resolve: (value: T | PromiseLike<T>) => void,
|
||||||
|
reject: (reason?: unknown) => void,
|
||||||
|
onCancel: OnCancel,
|
||||||
|
) => void,
|
||||||
|
) {
|
||||||
|
this._isResolved = false;
|
||||||
|
this._isRejected = false;
|
||||||
|
this._isCancelled = false;
|
||||||
|
this.cancelHandlers = [];
|
||||||
|
this.promise = new Promise<T>((resolve, reject) => {
|
||||||
|
this._resolve = resolve;
|
||||||
|
this._reject = reject;
|
||||||
|
|
||||||
|
const onResolve = (value: T | PromiseLike<T>): void => {
|
||||||
|
if (this._isResolved || this._isRejected || this._isCancelled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this._isResolved = true;
|
||||||
|
if (this._resolve) this._resolve(value);
|
||||||
|
};
|
||||||
|
|
||||||
|
const onReject = (reason?: unknown): void => {
|
||||||
|
if (this._isResolved || this._isRejected || this._isCancelled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this._isRejected = true;
|
||||||
|
if (this._reject) this._reject(reason);
|
||||||
|
};
|
||||||
|
|
||||||
|
const onCancel = (cancelHandler: () => void): void => {
|
||||||
|
if (this._isResolved || this._isRejected || this._isCancelled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.cancelHandlers.push(cancelHandler);
|
||||||
|
};
|
||||||
|
|
||||||
|
Object.defineProperty(onCancel, "isResolved", {
|
||||||
|
get: (): boolean => this._isResolved,
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.defineProperty(onCancel, "isRejected", {
|
||||||
|
get: (): boolean => this._isRejected,
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.defineProperty(onCancel, "isCancelled", {
|
||||||
|
get: (): boolean => this._isCancelled,
|
||||||
|
});
|
||||||
|
|
||||||
|
return executor(onResolve, onReject, onCancel as OnCancel);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
get [Symbol.toStringTag]() {
|
||||||
|
return "Cancellable Promise";
|
||||||
|
}
|
||||||
|
|
||||||
|
public then<TResult1 = T, TResult2 = never>(
|
||||||
|
onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null,
|
||||||
|
onRejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null,
|
||||||
|
): Promise<TResult1 | TResult2> {
|
||||||
|
return this.promise.then(onFulfilled, onRejected);
|
||||||
|
}
|
||||||
|
|
||||||
|
public catch<TResult = never>(
|
||||||
|
onRejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | null,
|
||||||
|
): Promise<T | TResult> {
|
||||||
|
return this.promise.catch(onRejected);
|
||||||
|
}
|
||||||
|
|
||||||
|
public finally(onFinally?: (() => void) | null): Promise<T> {
|
||||||
|
return this.promise.finally(onFinally);
|
||||||
|
}
|
||||||
|
|
||||||
|
public cancel(): void {
|
||||||
|
if (this._isResolved || this._isRejected || this._isCancelled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this._isCancelled = true;
|
||||||
|
if (this.cancelHandlers.length) {
|
||||||
|
try {
|
||||||
|
for (const cancelHandler of this.cancelHandlers) {
|
||||||
|
cancelHandler();
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.warn("Cancellation threw an error", error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.cancelHandlers.length = 0;
|
||||||
|
if (this._reject) this._reject(new CancelError("Request aborted"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public get isCancelled(): boolean {
|
||||||
|
return this._isCancelled;
|
||||||
|
}
|
||||||
|
}
|
||||||
23
src/utils/active/core/FetchHttpRequest.ts
Normal file
23
src/utils/active/core/FetchHttpRequest.ts
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
import type { ApiRequestOptions } from "./ApiRequestOptions";
|
||||||
|
import { BaseHttpRequest } from "./BaseHttpRequest";
|
||||||
|
import type { CancelablePromise } from "./CancelablePromise";
|
||||||
|
import type { OpenAPIConfig } from "./OpenAPI";
|
||||||
|
import { request as __request } from "./request";
|
||||||
|
|
||||||
|
export class FetchHttpRequest extends BaseHttpRequest {
|
||||||
|
constructor(config: OpenAPIConfig) {
|
||||||
|
super(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Request method
|
||||||
|
* @param options The request options from the service
|
||||||
|
* @returns CancelablePromise<T>
|
||||||
|
* @throws ApiError
|
||||||
|
*/
|
||||||
|
public override request<T>(
|
||||||
|
options: ApiRequestOptions<T>,
|
||||||
|
): CancelablePromise<T> {
|
||||||
|
return __request(this.config, options);
|
||||||
|
}
|
||||||
|
}
|
||||||
56
src/utils/active/core/OpenAPI.ts
Normal file
56
src/utils/active/core/OpenAPI.ts
Normal file
|
|
@ -0,0 +1,56 @@
|
||||||
|
import type { ApiRequestOptions } from "./ApiRequestOptions";
|
||||||
|
|
||||||
|
type Headers = Record<string, string>;
|
||||||
|
type Middleware<T> = (value: T) => T | Promise<T>;
|
||||||
|
type Resolver<T> = (options: ApiRequestOptions<T>) => Promise<T>;
|
||||||
|
|
||||||
|
export class Interceptors<T> {
|
||||||
|
_fns: Middleware<T>[];
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this._fns = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
eject(fn: Middleware<T>): void {
|
||||||
|
const index = this._fns.indexOf(fn);
|
||||||
|
if (index !== -1) {
|
||||||
|
this._fns = [...this._fns.slice(0, index), ...this._fns.slice(index + 1)];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
use(fn: Middleware<T>): void {
|
||||||
|
this._fns = [...this._fns, fn];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export type OpenAPIConfig = {
|
||||||
|
BASE: string;
|
||||||
|
CREDENTIALS: "include" | "omit" | "same-origin";
|
||||||
|
ENCODE_PATH?: ((path: string) => string) | undefined;
|
||||||
|
HEADERS?: Headers | Resolver<Headers> | undefined;
|
||||||
|
PASSWORD?: string | Resolver<string> | undefined;
|
||||||
|
TOKEN?: string | Resolver<string> | undefined;
|
||||||
|
USERNAME?: string | Resolver<string> | undefined;
|
||||||
|
VERSION: string;
|
||||||
|
WITH_CREDENTIALS: boolean;
|
||||||
|
interceptors: {
|
||||||
|
request: Interceptors<RequestInit>;
|
||||||
|
response: Interceptors<Response>;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export const OpenAPI: OpenAPIConfig = {
|
||||||
|
BASE: "",
|
||||||
|
CREDENTIALS: "include",
|
||||||
|
ENCODE_PATH: undefined,
|
||||||
|
HEADERS: undefined,
|
||||||
|
PASSWORD: undefined,
|
||||||
|
TOKEN: undefined,
|
||||||
|
USERNAME: undefined,
|
||||||
|
VERSION: "1.0.0",
|
||||||
|
WITH_CREDENTIALS: false,
|
||||||
|
interceptors: {
|
||||||
|
request: new Interceptors(),
|
||||||
|
response: new Interceptors(),
|
||||||
|
},
|
||||||
|
};
|
||||||
400
src/utils/active/core/request.ts
Normal file
400
src/utils/active/core/request.ts
Normal file
|
|
@ -0,0 +1,400 @@
|
||||||
|
import { ApiError } from "./ApiError";
|
||||||
|
import type { ApiRequestOptions } from "./ApiRequestOptions";
|
||||||
|
import type { ApiResult } from "./ApiResult";
|
||||||
|
import { CancelablePromise } from "./CancelablePromise";
|
||||||
|
import type { OnCancel } from "./CancelablePromise";
|
||||||
|
import type { OpenAPIConfig } from "./OpenAPI";
|
||||||
|
|
||||||
|
export const isString = (value: unknown): value is string => {
|
||||||
|
return typeof value === "string";
|
||||||
|
};
|
||||||
|
|
||||||
|
export const isStringWithValue = (value: unknown): value is string => {
|
||||||
|
return isString(value) && value !== "";
|
||||||
|
};
|
||||||
|
|
||||||
|
export const isBlob = (value: any): value is Blob => {
|
||||||
|
return value instanceof Blob;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const isFormData = (value: unknown): value is FormData => {
|
||||||
|
return value instanceof FormData;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const base64 = (str: string): string => {
|
||||||
|
try {
|
||||||
|
return btoa(str);
|
||||||
|
} catch (err) {
|
||||||
|
// @ts-ignore
|
||||||
|
return Buffer.from(str).toString("base64");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getQueryString = (params: Record<string, unknown>): string => {
|
||||||
|
const qs: string[] = [];
|
||||||
|
|
||||||
|
const append = (key: string, value: unknown) => {
|
||||||
|
qs.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`);
|
||||||
|
};
|
||||||
|
|
||||||
|
const encodePair = (key: string, value: unknown) => {
|
||||||
|
if (value === undefined || value === null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value instanceof Date) {
|
||||||
|
append(key, value.toISOString());
|
||||||
|
} else if (Array.isArray(value)) {
|
||||||
|
value.forEach((v) => encodePair(key, v));
|
||||||
|
} else if (typeof value === "object") {
|
||||||
|
Object.entries(value).forEach(([k, v]) => encodePair(`${key}[${k}]`, v));
|
||||||
|
} else {
|
||||||
|
append(key, value);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Object.entries(params).forEach(([key, value]) => encodePair(key, value));
|
||||||
|
|
||||||
|
return qs.length ? `?${qs.join("&")}` : "";
|
||||||
|
};
|
||||||
|
|
||||||
|
const getUrl = (config: OpenAPIConfig, options: ApiRequestOptions): string => {
|
||||||
|
const encoder = config.ENCODE_PATH || encodeURI;
|
||||||
|
|
||||||
|
const path = options.url
|
||||||
|
.replace("{api-version}", config.VERSION)
|
||||||
|
.replace(/{(.*?)}/g, (substring: string, group: string) => {
|
||||||
|
if (options.path?.hasOwnProperty(group)) {
|
||||||
|
return encoder(String(options.path[group]));
|
||||||
|
}
|
||||||
|
return substring;
|
||||||
|
});
|
||||||
|
|
||||||
|
const url = config.BASE + path;
|
||||||
|
return options.query ? url + getQueryString(options.query) : url;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getFormData = (
|
||||||
|
options: ApiRequestOptions,
|
||||||
|
): FormData | undefined => {
|
||||||
|
if (options.formData) {
|
||||||
|
const formData = new FormData();
|
||||||
|
|
||||||
|
const process = (key: string, value: unknown) => {
|
||||||
|
if (isString(value) || isBlob(value)) {
|
||||||
|
formData.append(key, value);
|
||||||
|
} else {
|
||||||
|
formData.append(key, JSON.stringify(value));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Object.entries(options.formData)
|
||||||
|
.filter(([, value]) => value !== undefined && value !== null)
|
||||||
|
.forEach(([key, value]) => {
|
||||||
|
if (Array.isArray(value)) {
|
||||||
|
value.forEach((v) => process(key, v));
|
||||||
|
} else {
|
||||||
|
process(key, value);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return formData;
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
type Resolver<T> = (options: ApiRequestOptions<T>) => Promise<T>;
|
||||||
|
|
||||||
|
export const resolve = async <T>(
|
||||||
|
options: ApiRequestOptions<T>,
|
||||||
|
resolver?: T | Resolver<T>,
|
||||||
|
): Promise<T | undefined> => {
|
||||||
|
if (typeof resolver === "function") {
|
||||||
|
return (resolver as Resolver<T>)(options);
|
||||||
|
}
|
||||||
|
return resolver;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getHeaders = async <T>(
|
||||||
|
config: OpenAPIConfig,
|
||||||
|
options: ApiRequestOptions<T>,
|
||||||
|
): Promise<Headers> => {
|
||||||
|
const [token, username, password, additionalHeaders] = await Promise.all([
|
||||||
|
// @ts-ignore
|
||||||
|
resolve(options, config.TOKEN),
|
||||||
|
// @ts-ignore
|
||||||
|
resolve(options, config.USERNAME),
|
||||||
|
// @ts-ignore
|
||||||
|
resolve(options, config.PASSWORD),
|
||||||
|
// @ts-ignore
|
||||||
|
resolve(options, config.HEADERS),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const headers = Object.entries({
|
||||||
|
Accept: "application/json",
|
||||||
|
...additionalHeaders,
|
||||||
|
...options.headers,
|
||||||
|
})
|
||||||
|
.filter(([, value]) => value !== undefined && value !== null)
|
||||||
|
.reduce(
|
||||||
|
(headers, [key, value]) => ({
|
||||||
|
...headers,
|
||||||
|
[key]: String(value),
|
||||||
|
}),
|
||||||
|
{} as Record<string, string>,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (isStringWithValue(token)) {
|
||||||
|
headers["Authorization"] = `Bearer ${token}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isStringWithValue(username) && isStringWithValue(password)) {
|
||||||
|
const credentials = base64(`${username}:${password}`);
|
||||||
|
headers["Authorization"] = `Basic ${credentials}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.body !== undefined) {
|
||||||
|
if (options.mediaType) {
|
||||||
|
headers["Content-Type"] = options.mediaType;
|
||||||
|
} else if (isBlob(options.body)) {
|
||||||
|
headers["Content-Type"] = options.body.type || "application/octet-stream";
|
||||||
|
} else if (isString(options.body)) {
|
||||||
|
headers["Content-Type"] = "text/plain";
|
||||||
|
} else if (!isFormData(options.body)) {
|
||||||
|
headers["Content-Type"] = "application/json";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Headers(headers);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getRequestBody = (options: ApiRequestOptions): unknown => {
|
||||||
|
if (options.body !== undefined) {
|
||||||
|
if (
|
||||||
|
options.mediaType?.includes("application/json") ||
|
||||||
|
options.mediaType?.includes("+json")
|
||||||
|
) {
|
||||||
|
return JSON.stringify(options.body);
|
||||||
|
} else if (
|
||||||
|
isString(options.body) ||
|
||||||
|
isBlob(options.body) ||
|
||||||
|
isFormData(options.body)
|
||||||
|
) {
|
||||||
|
return options.body;
|
||||||
|
} else {
|
||||||
|
return JSON.stringify(options.body);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const sendRequest = async (
|
||||||
|
config: OpenAPIConfig,
|
||||||
|
options: ApiRequestOptions,
|
||||||
|
url: string,
|
||||||
|
body: any,
|
||||||
|
formData: FormData | undefined,
|
||||||
|
headers: Headers,
|
||||||
|
onCancel: OnCancel,
|
||||||
|
): Promise<Response> => {
|
||||||
|
const controller = new AbortController();
|
||||||
|
|
||||||
|
let request: RequestInit = {
|
||||||
|
headers,
|
||||||
|
body: body ?? formData,
|
||||||
|
method: options.method,
|
||||||
|
signal: controller.signal,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (config.WITH_CREDENTIALS) {
|
||||||
|
request.credentials = config.CREDENTIALS;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const fn of config.interceptors.request._fns) {
|
||||||
|
request = await fn(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
onCancel(() => controller.abort());
|
||||||
|
|
||||||
|
return await fetch(url, request);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getResponseHeader = (
|
||||||
|
response: Response,
|
||||||
|
responseHeader?: string,
|
||||||
|
): string | undefined => {
|
||||||
|
if (responseHeader) {
|
||||||
|
const content = response.headers.get(responseHeader);
|
||||||
|
if (isString(content)) {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getResponseBody = async (response: Response): Promise<unknown> => {
|
||||||
|
if (response.status !== 204) {
|
||||||
|
try {
|
||||||
|
const contentType = response.headers.get("Content-Type");
|
||||||
|
if (contentType) {
|
||||||
|
const binaryTypes = [
|
||||||
|
"application/octet-stream",
|
||||||
|
"application/pdf",
|
||||||
|
"application/zip",
|
||||||
|
"audio/",
|
||||||
|
"image/",
|
||||||
|
"video/",
|
||||||
|
];
|
||||||
|
if (
|
||||||
|
contentType.includes("application/json") ||
|
||||||
|
contentType.includes("+json")
|
||||||
|
) {
|
||||||
|
return await response.json();
|
||||||
|
} else if (binaryTypes.some((type) => contentType.includes(type))) {
|
||||||
|
return await response.blob();
|
||||||
|
} else if (contentType.includes("multipart/form-data")) {
|
||||||
|
return await response.formData();
|
||||||
|
} else if (contentType.includes("text/")) {
|
||||||
|
return await response.text();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const catchErrorCodes = (
|
||||||
|
options: ApiRequestOptions,
|
||||||
|
result: ApiResult,
|
||||||
|
): void => {
|
||||||
|
const errors: Record<number, string> = {
|
||||||
|
400: "Bad Request",
|
||||||
|
401: "Unauthorized",
|
||||||
|
402: "Payment Required",
|
||||||
|
403: "Forbidden",
|
||||||
|
404: "Not Found",
|
||||||
|
405: "Method Not Allowed",
|
||||||
|
406: "Not Acceptable",
|
||||||
|
407: "Proxy Authentication Required",
|
||||||
|
408: "Request Timeout",
|
||||||
|
409: "Conflict",
|
||||||
|
410: "Gone",
|
||||||
|
411: "Length Required",
|
||||||
|
412: "Precondition Failed",
|
||||||
|
413: "Payload Too Large",
|
||||||
|
414: "URI Too Long",
|
||||||
|
415: "Unsupported Media Type",
|
||||||
|
416: "Range Not Satisfiable",
|
||||||
|
417: "Expectation Failed",
|
||||||
|
418: "Im a teapot",
|
||||||
|
421: "Misdirected Request",
|
||||||
|
422: "Unprocessable Content",
|
||||||
|
423: "Locked",
|
||||||
|
424: "Failed Dependency",
|
||||||
|
425: "Too Early",
|
||||||
|
426: "Upgrade Required",
|
||||||
|
428: "Precondition Required",
|
||||||
|
429: "Too Many Requests",
|
||||||
|
431: "Request Header Fields Too Large",
|
||||||
|
451: "Unavailable For Legal Reasons",
|
||||||
|
500: "Internal Server Error",
|
||||||
|
501: "Not Implemented",
|
||||||
|
502: "Bad Gateway",
|
||||||
|
503: "Service Unavailable",
|
||||||
|
504: "Gateway Timeout",
|
||||||
|
505: "HTTP Version Not Supported",
|
||||||
|
506: "Variant Also Negotiates",
|
||||||
|
507: "Insufficient Storage",
|
||||||
|
508: "Loop Detected",
|
||||||
|
510: "Not Extended",
|
||||||
|
511: "Network Authentication Required",
|
||||||
|
...options.errors,
|
||||||
|
};
|
||||||
|
|
||||||
|
const error = errors[result.status];
|
||||||
|
if (error) {
|
||||||
|
throw new ApiError(options, result, error);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!result.ok) {
|
||||||
|
const errorStatus = result.status ?? "unknown";
|
||||||
|
const errorStatusText = result.statusText ?? "unknown";
|
||||||
|
const errorBody = (() => {
|
||||||
|
try {
|
||||||
|
return JSON.stringify(result.body, null, 2);
|
||||||
|
} catch (e) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
||||||
|
throw new ApiError(
|
||||||
|
options,
|
||||||
|
result,
|
||||||
|
`Generic Error: status: ${errorStatus}; status text: ${errorStatusText}; body: ${errorBody}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Request method
|
||||||
|
* @param config The OpenAPI configuration object
|
||||||
|
* @param options The request options from the service
|
||||||
|
* @returns CancelablePromise<T>
|
||||||
|
* @throws ApiError
|
||||||
|
*/
|
||||||
|
export const request = <T>(
|
||||||
|
config: OpenAPIConfig,
|
||||||
|
options: ApiRequestOptions<T>,
|
||||||
|
): CancelablePromise<T> => {
|
||||||
|
return new CancelablePromise(async (resolve, reject, onCancel) => {
|
||||||
|
try {
|
||||||
|
const url = getUrl(config, options);
|
||||||
|
const formData = getFormData(options);
|
||||||
|
const body = getRequestBody(options);
|
||||||
|
const headers = await getHeaders(config, options);
|
||||||
|
|
||||||
|
if (!onCancel.isCancelled) {
|
||||||
|
let response = await sendRequest(
|
||||||
|
config,
|
||||||
|
options,
|
||||||
|
url,
|
||||||
|
body,
|
||||||
|
formData,
|
||||||
|
headers,
|
||||||
|
onCancel,
|
||||||
|
);
|
||||||
|
|
||||||
|
for (const fn of config.interceptors.response._fns) {
|
||||||
|
response = await fn(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
const responseBody = await getResponseBody(response);
|
||||||
|
const responseHeader = getResponseHeader(
|
||||||
|
response,
|
||||||
|
options.responseHeader,
|
||||||
|
);
|
||||||
|
|
||||||
|
let transformedBody = responseBody;
|
||||||
|
if (options.responseTransformer && response.ok) {
|
||||||
|
transformedBody = await options.responseTransformer(responseBody);
|
||||||
|
}
|
||||||
|
|
||||||
|
const result: ApiResult = {
|
||||||
|
url,
|
||||||
|
ok: response.ok,
|
||||||
|
status: response.status,
|
||||||
|
statusText: response.statusText,
|
||||||
|
body: responseHeader ?? transformedBody,
|
||||||
|
};
|
||||||
|
|
||||||
|
catchErrorCodes(options, result);
|
||||||
|
|
||||||
|
resolve(result.body);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
reject(error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
8
src/utils/active/index.ts
Normal file
8
src/utils/active/index.ts
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
// This file is auto-generated by @hey-api/openapi-ts
|
||||||
|
export { ApiClient } from "./ApiClient";
|
||||||
|
export { ApiError } from "./core/ApiError";
|
||||||
|
export { BaseHttpRequest } from "./core/BaseHttpRequest";
|
||||||
|
export { CancelablePromise, CancelError } from "./core/CancelablePromise";
|
||||||
|
export { OpenAPI, type OpenAPIConfig } from "./core/OpenAPI";
|
||||||
|
export * from "./services.gen";
|
||||||
|
export * from "./types.gen";
|
||||||
43
src/utils/active/services.gen.ts
Normal file
43
src/utils/active/services.gen.ts
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
// This file is auto-generated by @hey-api/openapi-ts
|
||||||
|
|
||||||
|
import type { CancelablePromise } from "./core/CancelablePromise";
|
||||||
|
import type { BaseHttpRequest } from "./core/BaseHttpRequest";
|
||||||
|
import type {
|
||||||
|
PostFreshmanAddData,
|
||||||
|
PostFreshmanAddResponse,
|
||||||
|
GetFreshmanListResponse,
|
||||||
|
} from "./types.gen";
|
||||||
|
|
||||||
|
export class FreshmanService {
|
||||||
|
constructor(public readonly httpRequest: BaseHttpRequest) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加新人
|
||||||
|
* @param data The data for the request.
|
||||||
|
* @param data.requestBody
|
||||||
|
* @returns unknown Returns the created task
|
||||||
|
* @throws ApiError
|
||||||
|
*/
|
||||||
|
public postFreshmanAdd(
|
||||||
|
data: PostFreshmanAddData = {},
|
||||||
|
): CancelablePromise<PostFreshmanAddResponse> {
|
||||||
|
return this.httpRequest.request({
|
||||||
|
method: "POST",
|
||||||
|
url: "/api/freshman",
|
||||||
|
body: data.requestBody,
|
||||||
|
mediaType: "application/json",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取新人列表
|
||||||
|
* @returns unknown Returns a list of tasks
|
||||||
|
* @throws ApiError
|
||||||
|
*/
|
||||||
|
public getFreshmanList(): CancelablePromise<GetFreshmanListResponse> {
|
||||||
|
return this.httpRequest.request({
|
||||||
|
method: "GET",
|
||||||
|
url: "/api/freshman",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
40
src/utils/active/types.gen.ts
Normal file
40
src/utils/active/types.gen.ts
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
// This file is auto-generated by @hey-api/openapi-ts
|
||||||
|
|
||||||
|
export type PostFreshmanAddData = {
|
||||||
|
requestBody?: {
|
||||||
|
name: string;
|
||||||
|
number: string;
|
||||||
|
major: string;
|
||||||
|
class: string;
|
||||||
|
email: string;
|
||||||
|
phone: string;
|
||||||
|
qq: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export type PostFreshmanAddResponse = {
|
||||||
|
success: boolean;
|
||||||
|
result?: {
|
||||||
|
name: string;
|
||||||
|
number: string;
|
||||||
|
major: string;
|
||||||
|
class: string;
|
||||||
|
email: string;
|
||||||
|
phone: string;
|
||||||
|
qq: string;
|
||||||
|
};
|
||||||
|
error?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type GetFreshmanListResponse = {
|
||||||
|
success: boolean;
|
||||||
|
list: Array<{
|
||||||
|
name: string;
|
||||||
|
number: string;
|
||||||
|
major: string;
|
||||||
|
class: string;
|
||||||
|
email: string;
|
||||||
|
phone: string;
|
||||||
|
qq: string;
|
||||||
|
}>;
|
||||||
|
};
|
||||||
|
|
@ -1,10 +1,13 @@
|
||||||
import createClient from "openapi-fetch"
|
import createClient from "openapi-fetch"
|
||||||
import type { paths as saturdayPaths } from "../types/saturday"
|
import type { paths as saturdayPaths } from "../types/saturday"
|
||||||
import type { paths as activePaths } from "../types/active"
|
// import type { paths as activePaths } from "../types/active"
|
||||||
|
import { ApiClient } from "./active"
|
||||||
export const saturdayClient = createClient<saturdayPaths>({
|
export const saturdayClient = createClient<saturdayPaths>({
|
||||||
baseUrl: "https://api.nbtca.space/v2/",
|
baseUrl: "https://api.nbtca.space/v2/",
|
||||||
})
|
})
|
||||||
export const activeClient = createClient<activePaths>({
|
// export const activeClient = createClient<activePaths>({
|
||||||
baseUrl: "https://active.nbtca.space/",
|
// baseUrl: "https://active.nbtca.space/",
|
||||||
|
// })
|
||||||
|
export const activeClient = new ApiClient({
|
||||||
|
BASE: "https://active.nbtca.space/",
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue