-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform.ts
49 lines (42 loc) · 1.61 KB
/
transform.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import fs from 'node:fs';
let content = fs.readFileSync('./src/autogenerated.types.ts', 'utf-8');
Array.from({ length: 3 }).forEach(() => {
content = content.replaceAll(/$\s+[\w_]+\?:(:?[{/*\s\w]+)?.*;?$/gim, (v) => {
// console.log(v);
if (
v.includes('?: Parameters.') ||
v.includes('Parameters<') ||
v.includes(' data?: any,') ||
v.includes('config?: AxiosRequestConfig')
) {
return v;
}
return v.replace('?:', ':');
});
});
content = content.replace(
`\
import type {
OpenAPIClient,
Parameters,
UnknownParamsObject,
OperationResponse,
AxiosRequestConfig,
} from 'openapi-client-axios';`,
`\
import type { Parameters, UnknownParamsObject, UnknownOperationMethods, UnknownPathsDictionary, OpenAPIClientAxios } from 'openapi-client-axios';
import type { CacheAxiosResponse as AxiosResponse, CacheRequestConfig as AxiosRequestConfig, AxiosCacheInstance as AxiosInstance } from 'axios-cache-interceptor';
type OpenAPIClient<OperationMethods = UnknownOperationMethods, PathsDictionary = UnknownPathsDictionary> = AxiosInstance & OperationMethods & {
api: OpenAPIClientAxios;
paths: PathsDictionary;
};
type OperationResponse<Response> = Promise<AxiosResponse<Response>>;`
);
content += content
.match(/namespace Schemas \{([\w\s\n{}*|"-;[\]=<>`!?]+)^\s{4}}\n}\ndeclare/gm)![0]
.match(/export interface (\w+) \{/gm)!
.map((v) => v.replace(/export interface (\w+) \{/, '$1'))
.map((v) => `export type ${v} = Components.Schemas.${v};`)
.join('\n');
fs.writeFileSync('./src/autogenerated.types.ts', content);
// [\s\w/*{\n]+([\w_]+\?:(:?[{/*\s\w]+)?.*;)