Archived
1
0
Fork 0

[v2.1.1] fix axios type bug

This commit is contained in:
sup39 2022-06-13 21:06:21 +09:00
parent 377e34deb5
commit 272269339e
3 changed files with 11 additions and 9 deletions

View file

@ -608,6 +608,8 @@ interface Task {
This tool only supports `application/json` type for request and response body. Any other type like `multipart/form` or `image/*` are **not supported** and will be ignored.
## Versions
#### 2.1.1
- Fix axios type bug (`AxiosPromise` vs `Promise<AxiosResponse>`)
#### 2.1.0
- Move `@sup39/api-ts-gen/utils` to `@sup39/api-ts-gen/dist/utils` since symlink is no longer allowed
- Simplify `apiRouter.ts` by using `router.use` to check for `BadValueError`

View file

@ -1,21 +1,21 @@
import { AxiosResponse } from 'axios';
import { AxiosPromise, AxiosResponse } from 'axios';
declare type ValueOf<T> = T[keyof T];
declare type RHandler<T> = ValueOf<{
[K in keyof T]: T[K] extends (data: any) => infer U ? U : never;
}>;
export declare class BadResponseError extends Error {
res: AxiosResponse<any>;
constructor(res: AxiosResponse<any>, label: string);
res: AxiosResponse;
constructor(res: AxiosResponse, label: string);
}
export declare class APIPromise<TRes, KRsv extends keyof TRes, THdl extends {
[K in KRsv]: (data: TRes[K]) => any;
}, KOn extends keyof TRes = keyof TRes> implements PromiseLike<RHandler<THdl>> {
private handlers;
private promise;
constructor(resPromise: Promise<AxiosResponse>, stps: {
constructor(resPromise: AxiosPromise, stps: {
[K in keyof TRes]: (data: any) => TRes[K];
}, handlers: THdl);
static init<TRes, KRsv extends keyof TRes>(res: Promise<AxiosResponse>, stps: {
static init<TRes, KRsv extends keyof TRes>(res: AxiosPromise, stps: {
[K in keyof TRes]: (data: any) => TRes[K];
}, kRsvs: KRsv[]): APIPromise<TRes, KRsv, {
[K in KRsv]: (data: TRes[K]) => TRes[K];

View file

@ -1,4 +1,4 @@
import {AxiosResponse} from 'axios';
import {AxiosPromise, AxiosResponse} from 'axios';
type ValueOf<T> = T[keyof T];
type RHandler<T> = ValueOf<{[K in keyof T]:
@ -11,7 +11,7 @@ function typeGuard<T extends U, U=any>(checker: (x: U) => boolean) {
}
export class BadResponseError extends Error {
constructor(public res: AxiosResponse<any>, label: string) {
constructor(public res: AxiosResponse, label: string) {
super(`${label} status code: ${res.status}\ndata: ${
typeof res.data === 'object' ? JSON.stringify(res.data) : res.data}`);
Object.setPrototypeOf(this, BadResponseError.prototype);
@ -27,7 +27,7 @@ export class APIPromise<
private promise: Promise<RHandler<THdl>>;
constructor(
resPromise: Promise<AxiosResponse>,
resPromise: AxiosPromise,
stps: {[K in keyof TRes]: (data: any) => TRes[K]},
private handlers: THdl,
) {
@ -48,7 +48,7 @@ export class APIPromise<
}
static init<TRes, KRsv extends keyof TRes>(
res: Promise<AxiosResponse>,
res: AxiosPromise,
stps: {[K in keyof TRes]: (data: any) => TRes[K]},
kRsvs: KRsv[],
): APIPromise<