@povio/openapi-codegen-cli 2.0.0-rc.3 → 2.0.1-rc.0

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/README.md CHANGED
@@ -1,9 +1,11 @@
1
1
  # OpenAPI code generation CLI
2
2
 
3
- **NOTE:** This CLI tool is primarily designed for use within our organization. The generated code output aligns with our internal template. If you are using this tool without our internal template, make sure to use it in **standalone** mode.
3
+ **NOTE:** This CLI tool is primarily designed for use within our organization. The generated code output aligns with our internal template.
4
4
 
5
5
  **NOTE:** Version 1+ requires zod v4 and is not compatible with zod v3.
6
6
 
7
+ **NOTE:** Version 2+ includes supporting classes/types/components for the generated code as well as auth, therefore it has peerDependencies for @casl/ability, @casl/react, @tanstack/react-query, axios, react and zod.
8
+
7
9
  Use this tool to generate code (Zod schemas, TypeScript types, API definitions, and React queries) from an OpenAPI v3 specification. API definitions are generated to use a REST client wrapper that utilizes Axios. React queries are generated in alignment with our code standards, without the need for explicit types.
8
10
 
9
11
  The tool partially leverages code from [openapi-zod-client](https://github.com/astahmer/openapi-zod-client) repository.
@@ -20,12 +22,6 @@ yarn add @povio/openapi-codegen-cli
20
22
  yarn openapi-codegen generate --input http://localhost:3001/docs-json
21
23
  ```
22
24
 
23
- #### Standalone mode
24
-
25
- ```bash
26
- yarn openapi-codegen generate --input http://localhost:3001/docs-json --standalone
27
- ```
28
-
29
25
  ## Configuration Files
30
26
 
31
27
  The CLI supports TypeScript configuration files to simplify command execution and provide consistent settings with full type safety. Configuration files are automatically discovered in your project root.
@@ -100,8 +96,7 @@ yarn openapi-codegen generate --config my-config.ts
100
96
 
101
97
  --builderConfigs Generate configs for builders (default: false)
102
98
 
103
- --standalone Generate any missing supporting classes/types, e.g., REST client class, React Query type extensions, etc. (default: false)
104
- --baseUrl (Requires `--standalone`) Base URL for the REST client; falls back to the OpenAPI spec if not provided
99
+ --baseUrl (Requires `--restClientImportPath` to NOT be set) Base URL for the generated REST client; falls back to the OpenAPI spec if not provided
105
100
  ```
106
101
 
107
102
  #### Check command (checks if OpenAPI spec is compliant)
@@ -145,6 +140,52 @@ yarn start:dist generate --input ./test/petstore.yaml --verbose
145
140
 
146
141
  ## Common Issues
147
142
 
143
+ ### App REST Client Interceptors
144
+
145
+ In order to add interceptors to the used REST client, you must create your own instance of a RestClient and pass your implemented interceptors into the constructor. Make sure to set `restClientImportPath` in your openapi generation configuration too.
146
+
147
+ ```ts
148
+ import { RestInterceptor } from "@povio/openapi-codegen-cli";
149
+
150
+ import { ACCESS_TOKEN_KEY } from "@/config/jwt.config";
151
+
152
+ export const AuthorizationHeaderInterceptor = new RestInterceptor((client) => {
153
+ return client.interceptors.request.use(async (config) => {
154
+ const accessToken = localStorage.getItem(ACCESS_TOKEN_KEY);
155
+ if (accessToken != null) {
156
+ config.headers.Authorization = `Bearer ${accessToken}`;
157
+ }
158
+
159
+ return config;
160
+ });
161
+ });
162
+ ```
163
+
164
+ ```ts
165
+ import { RestClient } from "@povio/openapi-codegen-cli";
166
+
167
+ import { AuthorizationHeaderInterceptor } from "@/clients/rest/interceptors/authorization-header.interceptor";
168
+ import { AppConfig } from "@/config/app.config";
169
+
170
+ export const AppRestClient = new RestClient({
171
+ config: {
172
+ baseURL: AppConfig.api.url,
173
+ },
174
+ interceptors: [AuthorizationHeaderInterceptor],
175
+ });
176
+ ```
177
+
178
+ ```ts
179
+ import type { OpenAPICodegenConfig } from "@povio/openapi-codegen-cli";
180
+
181
+ const config: OpenAPICodegenConfig = {
182
+ restClientImportPath: "@/clients/app-rest-client",
183
+ // ...
184
+ };
185
+
186
+ export default config;
187
+ ```
188
+
148
189
  ### Enums
149
190
 
150
191
  If you're using Enums in your backend DTOs with `@Expose()` and `@IsEnum`, they may still not appear correctly in the OpenAPI schema unless you also provide both `enum` **and** `enumName` to `@ApiProperty`.
package/dist/acl.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export { AbilityContext } from "./lib/acl/ability.context";
2
+ export { createAclGuard } from "./lib/acl/AclGuard";
3
+ export { Can } from "./lib/acl/Can";
4
+ export type { AppAbilities, AppAbility } from "./lib/acl/appAbility.types";
package/dist/acl.js ADDED
@@ -0,0 +1 @@
1
+ var h=Object.defineProperty;var t=(e,o)=>h(e,"name",{value:o,configurable:!0});import{AbilityBuilder as w,createMongoAbility as g}from"@casl/ability";import{unpackRules as W}from"@casl/ability/extra";import{useAbility as B}from"@casl/react";import{createContext as G,useEffect as E,useState as M}from"react";import{createContext as v,use as R,useMemo as U}from"react";import{jsx as k}from"react/jsx-runtime";var d;(r=>{let e=v({});r.Provider=t(({isAuthenticated:i,isInitializing:A,logout:s,updateTokens:u,accessToken:a,user:c,userPromise:l,routes:b,loadingState:y,children:m})=>{let f=U(()=>({isAuthenticated:i,isInitializing:A,logout:s,updateTokens:u,accessToken:a,user:c,userPromise:l,routes:b,loadingState:y}),[i,A,s,u,a,c,l,b,y]);return k(e.Provider,{value:f,children:m})},"Provider"),r.useAuth=t(()=>R(e),"useAuth")})(d||(d={}));import{jsx as O}from"react/jsx-runtime";var n;(s=>{let e=t(()=>new w(g),"createAppAbilityBuilder"),o=e().build(),p=G({});({Consumer:s.Consumer}=p),s.Provider=t(({children:u})=>{let[a,c]=M(o),{user:l}=d.useAuth();return E(()=>{if(!l||!("aclRules"in l))return;let{can:b,build:y}=e(),m=l.aclRules;W(m).forEach(({action:C,subject:T,conditions:x})=>{b(C,T,x)}),c(y())},[l]),O(p.Provider,{value:a,children:u})},"Provider"),s.useAbility=t(()=>B(p),"useAbility")})(n||(n={}));import{createContext as N,use as I,useMemo as S}from"react";import{jsx as q}from"react/jsx-runtime";var P;(r=>{let e=N(null);r.Provider=t(({children:i,replace:A})=>{let s=S(()=>({replace:A}),[A]);return q(e,{value:s,children:i})},"Provider"),r.useRouter=t(()=>{let i=I(e);if(!i)throw new Error("useRouter must be used within an OpenApiRouter.Provider");return i},"useRouter")})(P||(P={}));var z=t(()=>({canUse:e,redirectTo:o="/",children:p})=>{let r=n.useAbility(),{replace:i}=P.useRouter();return r.can(e[0],e[1])?p:(i(o),null)},"createAclGuard");import{createContextualCan as D}from"@casl/react";import{jsx as J}from"react/jsx-runtime";var F=D(n.Consumer),H=t(({use:e,...o})=>{let[p,r]=e;return J(F,{...o,do:p,on:r})},"Can");export{n as AbilityContext,H as Can,z as createAclGuard};