@povio/openapi-codegen-cli 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@povio/openapi-codegen-cli",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "main": "./dist/index.js",
5
5
  "bin": {
6
6
  "openapi-codegen": "./dist/sh.js"
@@ -14,6 +14,7 @@
14
14
  "build": "yarn build:clean && yarn build:types && node ./esbuild.mjs && chmod +x ./dist/sh.js",
15
15
  "build:check": "yarn tsc --project . --noEmit",
16
16
  "start:dist": "node ./dist/sh.js",
17
+ "typecheck": "tsc --noEmit",
17
18
  "lint": "eslint --fix",
18
19
  "format:check": "prettier --check .",
19
20
  "format:fix": "prettier --write .",
@@ -21,6 +22,7 @@
21
22
  },
22
23
  "files": [
23
24
  "dist/*",
25
+ "src/assets/**",
24
26
  "src/generators/templates/**",
25
27
  "README.md"
26
28
  ],
@@ -0,0 +1,24 @@
1
+ import { UseInfiniteQueryOptions, UseMutationOptions, UseQueryOptions } from "@tanstack/react-query";
2
+
3
+ type Function = (...args: any) => any;
4
+ type IfAny<T, Y, N> = 0 extends 1 & T ? Y : N;
5
+ type IsAny<T> = IfAny<T, true, never>;
6
+ type IsUnknown<T, Y, N = T> = IsAny<T> extends never ? (unknown extends T ? Y : N) : N;
7
+
8
+ export type AppQueryOptions<TFunction extends Function, TData = Awaited<ReturnType<TFunction>>> = Omit<
9
+ UseQueryOptions<Awaited<ReturnType<TFunction>>, Error, IsUnknown<TData, Awaited<ReturnType<TFunction>>>>,
10
+ "queryKey" | "queryFn"
11
+ >;
12
+
13
+ export type AppMutationOptions<
14
+ TFunction extends Function,
15
+ TVariables = void,
16
+ TData = Awaited<ReturnType<TFunction>>,
17
+ > = Omit<UseMutationOptions<TData, Error, TVariables>, "mutationKey" | "mutationFn"> & {
18
+ enableInvalidateAll?: boolean;
19
+ };
20
+
21
+ export type AppInfiniteQueryOptions<TFunction extends Function, TData = Awaited<ReturnType<TFunction>>> = Omit<
22
+ UseInfiniteQueryOptions<Awaited<ReturnType<TFunction>>, Error, IsUnknown<TData, Awaited<ReturnType<TFunction>>>>,
23
+ "queryKey" | "queryFn" | "initialPageParam" | "getNextPageParam"
24
+ >;
@@ -0,0 +1,107 @@
1
+ import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, CreateAxiosDefaults } from "axios";
2
+ import { z } from "zod";
3
+ import { RestInterceptor } from "./rest-interceptor";
4
+
5
+ interface RequestInfo<ZResDto extends z.ZodRawShape, ResDto, Res> {
6
+ resSchema: z.ZodEffects<z.ZodObject<ZResDto, "strip", z.ZodTypeAny, ResDto>, Res> | z.ZodSchema<Res>;
7
+ }
8
+
9
+ type Method = "get" | "post" | "patch" | "put" | "delete";
10
+
11
+ const MethodHasBody: Record<Method, boolean> = {
12
+ get: false,
13
+ post: true,
14
+ patch: true,
15
+ put: true,
16
+ delete: true,
17
+ };
18
+
19
+ export class RestClient {
20
+ private client: AxiosInstance;
21
+
22
+ constructor({
23
+ config,
24
+ interceptors,
25
+ }: {
26
+ config?: CreateAxiosDefaults;
27
+ interceptors?: RestInterceptor<any[]>[];
28
+ } = {}) {
29
+ this.client = axios.create(config);
30
+ this.attachInterceptors(interceptors);
31
+ }
32
+
33
+ public attachInterceptors<T extends any[]>(interceptors?: RestInterceptor<T>[], ...args: T) {
34
+ if (interceptors != null) {
35
+ interceptors.forEach((interceptor) => this.attachInterceptor(interceptor, ...args));
36
+ }
37
+ }
38
+
39
+ public attachInterceptor<T extends any[]>(interceptor: RestInterceptor<T>, ...args: T) {
40
+ interceptor.addInterceptor(this.client, ...args);
41
+ }
42
+
43
+ public ejectInterceptor<T extends any[]>(interceptor: RestInterceptor<T>) {
44
+ interceptor.removeInterceptor(this.client);
45
+ }
46
+
47
+ public async get<ZResDto extends z.ZodRawShape, ResDto, Res>(
48
+ requestInfo: RequestInfo<ZResDto, ResDto, Res>,
49
+ url: string,
50
+ config?: AxiosRequestConfig,
51
+ ): Promise<Res> {
52
+ return this.makeRequest(requestInfo, "get", url, undefined, config);
53
+ }
54
+
55
+ public async post<ZResDto extends z.ZodRawShape, ResDto, Res>(
56
+ requestInfo: RequestInfo<ZResDto, ResDto, Res>,
57
+ url: string,
58
+ data?: any,
59
+ config?: AxiosRequestConfig,
60
+ ): Promise<Res> {
61
+ return this.makeRequest(requestInfo, "post", url, data, config);
62
+ }
63
+
64
+ public async patch<ZResDto extends z.ZodRawShape, ResDto, Res>(
65
+ requestInfo: RequestInfo<ZResDto, ResDto, Res>,
66
+ url: string,
67
+ data?: any,
68
+ config?: AxiosRequestConfig,
69
+ ): Promise<Res> {
70
+ return this.makeRequest(requestInfo, "patch", url, data, config);
71
+ }
72
+
73
+ public async put<ZResDto extends z.ZodRawShape, ResDto, Res>(
74
+ requestInfo: RequestInfo<ZResDto, ResDto, Res>,
75
+ url: string,
76
+ data?: any,
77
+ config?: AxiosRequestConfig,
78
+ ): Promise<Res> {
79
+ return this.makeRequest(requestInfo, "put", url, data, config);
80
+ }
81
+
82
+ public async delete<ZResDto extends z.ZodRawShape, ResDto, Res>(
83
+ requestInfo: RequestInfo<ZResDto, ResDto, Res>,
84
+ url: string,
85
+ data?: any,
86
+ config?: AxiosRequestConfig,
87
+ ): Promise<Res> {
88
+ return this.makeRequest(requestInfo, "delete", url, data, config);
89
+ }
90
+
91
+ private async makeRequest<ZResDto extends z.ZodRawShape, ResDto, Res>(
92
+ requestInfo: RequestInfo<ZResDto, ResDto, Res>,
93
+ method: Method,
94
+ url: string,
95
+ data?: any,
96
+ config?: AxiosRequestConfig,
97
+ ): Promise<Res> {
98
+ let res: AxiosResponse;
99
+ if (MethodHasBody[method]) {
100
+ res = await this.client[method](url, data, config);
101
+ } else {
102
+ res = await this.client[method](url, config);
103
+ }
104
+
105
+ return requestInfo.resSchema.parse(res);
106
+ }
107
+ }
@@ -0,0 +1,22 @@
1
+ import { AxiosInstance } from "axios";
2
+
3
+ export class RestInterceptor<T extends any[]> {
4
+ private interceptorIdMap: { client: AxiosInstance; interceptorId: number }[] = [];
5
+
6
+ constructor(private applyInterceptor: (client: AxiosInstance, ...args: T) => number) {}
7
+
8
+ public addInterceptor(client: AxiosInstance, ...args: T) {
9
+ this.removeInterceptor(client);
10
+ const interceptorId = this.applyInterceptor(client, ...args);
11
+ this.interceptorIdMap.push({ client, interceptorId });
12
+ }
13
+
14
+ public removeInterceptor(client: AxiosInstance) {
15
+ const interceptorId = this.interceptorIdMap.find((i) => i.client === client)?.interceptorId;
16
+
17
+ if (interceptorId != null) {
18
+ client.interceptors.request.eject(interceptorId);
19
+ this.interceptorIdMap = this.interceptorIdMap.filter((i) => i.client !== client);
20
+ }
21
+ }
22
+ }
@@ -0,0 +1,7 @@
1
+ import { RestClient } from "./rest-client";
2
+
3
+ export const {{appRestClientName}} = new RestClient({
4
+ config: {
5
+ baseURL: "{{baseUrl}}"
6
+ },
7
+ });
@@ -1,5 +1,5 @@
1
- {{! Rest client import}}
2
- {{{genImport restClientImport}}}
1
+ {{! App rest client import}}
2
+ {{{genImport appRestClientImport}}}
3
3
  {{! Zod import }}
4
4
  {{#if hasZodImport}}
5
5
  {{{genImport zodImport}}}
@@ -1,10 +0,0 @@
1
- import { Import } from "../types/generate";
2
- export declare const DATA_FILE_PATH = "src/data";
3
- export declare const DATA_TS_PATH = "@/data";
4
- export declare const REST_CLIENT_NAME = "AppRestClient";
5
- export declare const REST_CLIENT_IMPORT: Import;
6
- export declare const QUERY_OPTIONS_TYPES: {
7
- query: string;
8
- mutation: string;
9
- };
10
- export declare const QUERY_TYPES_IMPORT: Import;