@povio/openapi-codegen-cli 2.0.8-rc.4 → 2.0.8-rc.7
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 +63 -0
- package/dist/acl.d.mts +48 -0
- package/dist/acl.mjs +62 -7
- package/dist/auth.context-DKjzWiaA.mjs +59 -0
- package/dist/config-Cu_GYfai.d.mts +6 -0
- package/dist/generate.runner-DNldmk0f.mjs +98 -0
- package/dist/generateCodeFromOpenAPIDoc-B5lnFNGB.mjs +4522 -0
- package/dist/generator.d.mts +65 -0
- package/dist/generator.mjs +144 -0
- package/dist/index.d.mts +246 -0
- package/dist/index.mjs +365 -22
- package/dist/options-DBz5YE3s.d.mts +90 -0
- package/dist/sh.d.mts +1 -0
- package/dist/sh.mjs +439 -0
- package/dist/vite.d.mts +8 -0
- package/dist/vite.mjs +50 -0
- package/package.json +50 -45
- package/src/assets/useMutationEffects.ts +30 -25
- package/src/generators/templates/endpoints.hbs +0 -1
- package/dist/acl.d.ts +0 -4
- package/dist/commands/check.command.d.ts +0 -2
- package/dist/commands/check.d.ts +0 -7
- package/dist/commands/generate.command.d.ts +0 -2
- package/dist/commands/generate.d.ts +0 -8
- package/dist/generator.d.ts +0 -3
- package/dist/generator.js +0 -78
- package/dist/generators/checkOpenAPIDoc.d.ts +0 -3
- package/dist/generators/const/acl.const.d.ts +0 -13
- package/dist/generators/const/buildConfigs.const.d.ts +0 -1
- package/dist/generators/const/deps.const.d.ts +0 -38
- package/dist/generators/const/endpoints.const.d.ts +0 -11
- package/dist/generators/const/js.const.d.ts +0 -1
- package/dist/generators/const/openapi.const.d.ts +0 -7
- package/dist/generators/const/options.const.d.ts +0 -2
- package/dist/generators/const/package.const.d.ts +0 -2
- package/dist/generators/const/queries.const.d.ts +0 -8
- package/dist/generators/const/validation.const.d.ts +0 -53
- package/dist/generators/const/zod.const.d.ts +0 -19
- package/dist/generators/core/SchemaResolver.class.d.ts +0 -71
- package/dist/generators/core/endpoints/getEndpointAcl.d.ts +0 -8
- package/dist/generators/core/endpoints/getEndpointBody.d.ts +0 -13
- package/dist/generators/core/endpoints/getEndpointParameter.d.ts +0 -11
- package/dist/generators/core/endpoints/getEndpointsFromOpenAPIDoc.d.ts +0 -3
- package/dist/generators/core/endpoints/getEndpointsFromOpenAPIDoc.test.d.ts +0 -1
- package/dist/generators/core/getDataFromOpenAPIDoc.d.ts +0 -8
- package/dist/generators/core/getMetadataFromOpenAPIDoc.d.ts +0 -4
- package/dist/generators/core/getMetadataFromOpenAPIDoc.test.d.ts +0 -1
- package/dist/generators/core/openapi/getOpenAPISchemaComplexity.d.ts +0 -2
- package/dist/generators/core/openapi/getOpenAPISchemaComplexity.test.d.ts +0 -1
- package/dist/generators/core/openapi/getOpenAPISchemaDependencyGraph.d.ts +0 -6
- package/dist/generators/core/openapi/getOpenAPISchemaDependencyGraph.test.d.ts +0 -1
- package/dist/generators/core/openapi/getSchemaRefObjs.d.ts +0 -4
- package/dist/generators/core/openapi/iterateSchema.d.ts +0 -22
- package/dist/generators/core/resolveConfig.d.ts +0 -7
- package/dist/generators/core/zod/ZodSchema.class.d.ts +0 -26
- package/dist/generators/core/zod/enumExtraction/resolveExtractedEnumZodSchemaNames.d.ts +0 -2
- package/dist/generators/core/zod/enumExtraction/resolveExtractedEnumZodSchemaTags.d.ts +0 -2
- package/dist/generators/core/zod/enumExtraction/updateExtractedEnumZodSchemaData.d.ts +0 -17
- package/dist/generators/core/zod/getZodChain.d.ts +0 -8
- package/dist/generators/core/zod/getZodSchema.d.ts +0 -17
- package/dist/generators/core/zod/getZodSchema.test.d.ts +0 -1
- package/dist/generators/core/zod/getZodSchemaRefs.d.ts +0 -6
- package/dist/generators/core/zod/getZodSchemasFromOpenAPIDoc.d.ts +0 -6
- package/dist/generators/core/zod/resolveZodSchemaName.d.ts +0 -10
- package/dist/generators/core/zod/sortZodSchemasByTopology.d.ts +0 -4
- package/dist/generators/generate/generateAcl.d.ts +0 -3
- package/dist/generators/generate/generateAclCheck.d.ts +0 -2
- package/dist/generators/generate/generateAppRestClient.d.ts +0 -2
- package/dist/generators/generate/generateConfigs.d.ts +0 -2
- package/dist/generators/generate/generateEndpoints.d.ts +0 -2
- package/dist/generators/generate/generateModels.d.ts +0 -2
- package/dist/generators/generate/generateQueries.d.ts +0 -2
- package/dist/generators/generate/generateQueryModules.d.ts +0 -2
- package/dist/generators/generate/generateZodExtended.d.ts +0 -2
- package/dist/generators/generateCodeFromOpenAPIDoc.d.ts +0 -4
- package/dist/generators/types/builder-config.d.ts +0 -48
- package/dist/generators/types/common.d.ts +0 -27
- package/dist/generators/types/config.d.ts +0 -2
- package/dist/generators/types/endpoint.d.ts +0 -50
- package/dist/generators/types/generate.d.ts +0 -39
- package/dist/generators/types/metadata.d.ts +0 -51
- package/dist/generators/types/openapi.d.ts +0 -22
- package/dist/generators/types/options.d.ts +0 -66
- package/dist/generators/types/validation.d.ts +0 -5
- package/dist/generators/utils/array.utils.d.ts +0 -1
- package/dist/generators/utils/endpoint.utils.d.ts +0 -12
- package/dist/generators/utils/endpoint.utils.test.d.ts +0 -1
- package/dist/generators/utils/file.utils.d.ts +0 -8
- package/dist/generators/utils/generate/generate.acl.utils.d.ts +0 -23
- package/dist/generators/utils/generate/generate.configs.utils.d.ts +0 -15
- package/dist/generators/utils/generate/generate.endpoints.utils.d.ts +0 -39
- package/dist/generators/utils/generate/generate.imports.utils.d.ts +0 -39
- package/dist/generators/utils/generate/generate.imports.utils.test.d.ts +0 -1
- package/dist/generators/utils/generate/generate.openapi.utils.d.ts +0 -2
- package/dist/generators/utils/generate/generate.query.utils.d.ts +0 -6
- package/dist/generators/utils/generate/generate.utils.d.ts +0 -18
- package/dist/generators/utils/generate/generate.zod.utils.d.ts +0 -13
- package/dist/generators/utils/generate-files.utils.d.ts +0 -6
- package/dist/generators/utils/hbs/hbs-template.utils.d.ts +0 -3
- package/dist/generators/utils/hbs/hbs.acl.utils.d.ts +0 -2
- package/dist/generators/utils/hbs/hbs.common.utils.d.ts +0 -1
- package/dist/generators/utils/hbs/hbs.endpoints.utils.d.ts +0 -2
- package/dist/generators/utils/hbs/hbs.imports.utils.d.ts +0 -1
- package/dist/generators/utils/hbs/hbs.partials.utils.d.ts +0 -2
- package/dist/generators/utils/hbs/hbs.query.utils.d.ts +0 -2
- package/dist/generators/utils/hbs/hbs.zod.utils.d.ts +0 -2
- package/dist/generators/utils/js.utils.d.ts +0 -2
- package/dist/generators/utils/js.utils.test.d.ts +0 -1
- package/dist/generators/utils/math.utils.d.ts +0 -1
- package/dist/generators/utils/namespace.utils.d.ts +0 -7
- package/dist/generators/utils/object.utils.d.ts +0 -13
- package/dist/generators/utils/object.utils.test.d.ts +0 -1
- package/dist/generators/utils/openapi-schema.utils.d.ts +0 -15
- package/dist/generators/utils/openapi.utils.d.ts +0 -23
- package/dist/generators/utils/openapi.utils.test.d.ts +0 -1
- package/dist/generators/utils/operation.utils.d.ts +0 -22
- package/dist/generators/utils/operation.utils.test.d.ts +0 -1
- package/dist/generators/utils/query.utils.d.ts +0 -7
- package/dist/generators/utils/sort.utils.d.ts +0 -7
- package/dist/generators/utils/string.utils.d.ts +0 -14
- package/dist/generators/utils/string.utils.test.d.ts +0 -1
- package/dist/generators/utils/tag.utils.d.ts +0 -7
- package/dist/generators/utils/ts.utils.d.ts +0 -16
- package/dist/generators/utils/validation.utils.d.ts +0 -17
- package/dist/generators/utils/zod-schema.utils.d.ts +0 -15
- package/dist/helpers/cli.helper.d.ts +0 -22
- package/dist/helpers/config.helper.d.ts +0 -3
- package/dist/helpers/version.helper.d.ts +0 -4
- package/dist/helpers/yargs.helper.d.ts +0 -10
- package/dist/index.d.ts +0 -15
- package/dist/lib/acl/AclGuard.d.ts +0 -8
- package/dist/lib/acl/AclGuard.mjs +0 -14
- package/dist/lib/acl/Can.d.ts +0 -9
- package/dist/lib/acl/Can.mjs +0 -11
- package/dist/lib/acl/ability.context.d.ts +0 -15
- package/dist/lib/acl/ability.context.mjs +0 -37
- package/dist/lib/acl/appAbility.types.d.ts +0 -3
- package/dist/lib/assets/locales/en/translation.json.mjs +0 -8
- package/dist/lib/assets/locales/sl/translation.json.mjs +0 -8
- package/dist/lib/auth/AuthGuard.d.ts +0 -6
- package/dist/lib/auth/AuthGuard.mjs +0 -26
- package/dist/lib/auth/auth.context.d.ts +0 -22
- package/dist/lib/auth/auth.context.mjs +0 -41
- package/dist/lib/config/i18n.d.ts +0 -32
- package/dist/lib/config/i18n.mjs +0 -31
- package/dist/lib/config/queryConfig.context.d.ts +0 -17
- package/dist/lib/config/queryConfig.context.mjs +0 -26
- package/dist/lib/config/router.context.d.ts +0 -9
- package/dist/lib/config/router.context.mjs +0 -20
- package/dist/lib/react-query.types.d.ts +0 -10
- package/dist/lib/rest/error-handling.d.ts +0 -30
- package/dist/lib/rest/error-handling.mjs +0 -132
- package/dist/lib/rest/rest-client.d.ts +0 -22
- package/dist/lib/rest/rest-client.mjs +0 -62
- package/dist/lib/rest/rest-client.types.d.ts +0 -23
- package/dist/lib/rest/rest-interceptor.d.ts +0 -8
- package/dist/lib/rest/rest-interceptor.mjs +0 -21
- package/dist/lib/rest/rest.utils.d.ts +0 -7
- package/dist/lib/rest/rest.utils.mjs +0 -51
- package/dist/sh.d.ts +0 -2
- package/dist/sh.js +0 -635
package/README.md
CHANGED
|
@@ -69,6 +69,7 @@ yarn openapi-codegen generate --config my-config.ts
|
|
|
69
69
|
--config Path to TS config file (default: 'openapi-codegen.config.ts')
|
|
70
70
|
--input Path/URL to OpenAPI JSON/YAML document
|
|
71
71
|
--output Output directory path (default: 'output')
|
|
72
|
+
--incremental Skip generation when OpenAPI and config are unchanged (default: true)
|
|
72
73
|
--format Format the generated code using Oxfmt (default: true)
|
|
73
74
|
--verbose Display detailed log messages during execution (default: false)
|
|
74
75
|
|
|
@@ -84,11 +85,16 @@ yarn openapi-codegen generate --config my-config.ts
|
|
|
84
85
|
--tsPath (Requires `--importPath` to be 'ts') Typescript import path (default: '@/data')
|
|
85
86
|
--removeOperationPrefixEndingWith Remove operation name prefixes that end with the specified string (default: 'Controller_')
|
|
86
87
|
--extractEnums Extract enums into separate Zod schemas (default: true)
|
|
88
|
+
--modelsInCommon Keep all schema declarations in defaultTag models and emit per-module proxy exports (default: false)
|
|
87
89
|
--replaceOptionalWithNullish Replace `.optional()` chains with `.nullish()` in generated Zod schemas (default: false)
|
|
88
90
|
|
|
89
91
|
--axiosRequestConfig Include Axios request config parameters in query hooks (default: false)
|
|
90
92
|
--infiniteQueries Generate infinite queries for paginated API endpoints (default: false)
|
|
91
93
|
--mutationEffects Add mutation effects options to mutation hooks (default: true)
|
|
94
|
+
--workspaceContext Allow generated hooks to resolve path/ACL params from OpenApiWorkspaceContext (default: false)
|
|
95
|
+
--inlineEndpoints Inline endpoint implementations into generated query files (default: false)
|
|
96
|
+
--inlineEndpointsExcludeModules Comma-separated modules/tags to keep as separate API files while inlineEndpoints=true
|
|
97
|
+
--modelsOnly Generate only model files (default: false)
|
|
92
98
|
--parseRequestParams Add Zod parsing to API endpoints (default: true)
|
|
93
99
|
|
|
94
100
|
--acl Generate ACL related files (default: true)
|
|
@@ -186,6 +192,63 @@ const config: OpenAPICodegenConfig = {
|
|
|
186
192
|
export default config;
|
|
187
193
|
```
|
|
188
194
|
|
|
195
|
+
### OpenApiWorkspaceContext (Path + ACL defaults)
|
|
196
|
+
|
|
197
|
+
Enable `workspaceContext: true` in codegen config (or pass `--workspaceContext`) and wrap your app subtree with `OpenApiWorkspaceContext.Provider` if generated hooks frequently repeat workspace-scoped params (for example `officeId`).
|
|
198
|
+
|
|
199
|
+
```tsx
|
|
200
|
+
import { OpenApiWorkspaceContext } from "@povio/openapi-codegen-cli";
|
|
201
|
+
// openapi-codegen.config.ts -> { workspaceContext: true }
|
|
202
|
+
|
|
203
|
+
<OpenApiWorkspaceContext.Provider values={{ officeId: "office_123" }}>
|
|
204
|
+
<MyWorkspacePages />
|
|
205
|
+
</OpenApiWorkspaceContext.Provider>;
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
Generated query/mutation hooks can then omit matching path/ACL params and resolve them from `OpenApiWorkspaceContext`.
|
|
209
|
+
|
|
210
|
+
### Generation Modes
|
|
211
|
+
|
|
212
|
+
You can control whether API endpoint files are emitted, inlined into query files, or skipped entirely.
|
|
213
|
+
|
|
214
|
+
```ts
|
|
215
|
+
import type { OpenAPICodegenConfig } from "@povio/openapi-codegen-cli";
|
|
216
|
+
|
|
217
|
+
const config: OpenAPICodegenConfig = {
|
|
218
|
+
// 1) Default mode: separate *.api.ts files are generated
|
|
219
|
+
// 2) Inline mode: endpoint logic is generated inside *.queries.ts
|
|
220
|
+
// and can be used without separate api files:
|
|
221
|
+
// inlineEndpoints: true,
|
|
222
|
+
// inlineEndpointsExcludeModules: ["Users", "Billing"],
|
|
223
|
+
// 3) Models-only mode: generate only *.models.ts files
|
|
224
|
+
// modelsOnly: true,
|
|
225
|
+
// 4) Keep all model declarations in common.models and generate per-module model proxies
|
|
226
|
+
// modelsInCommon: true,
|
|
227
|
+
};
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
### Vite Plugin
|
|
231
|
+
|
|
232
|
+
You can run codegen directly from Vite config (without CLI config file):
|
|
233
|
+
|
|
234
|
+
```ts
|
|
235
|
+
import { defineConfig } from "vite";
|
|
236
|
+
import { openApiCodegen } from "@povio/openapi-codegen-cli/vite";
|
|
237
|
+
|
|
238
|
+
export default defineConfig({
|
|
239
|
+
plugins: [
|
|
240
|
+
openApiCodegen({
|
|
241
|
+
input: "./openapi.yaml",
|
|
242
|
+
output: "./src/data",
|
|
243
|
+
inlineEndpoints: true,
|
|
244
|
+
incremental: true,
|
|
245
|
+
}),
|
|
246
|
+
],
|
|
247
|
+
});
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
The plugin runs on both `vite serve` and `vite build`, and watches local OpenAPI files in dev mode.
|
|
251
|
+
|
|
189
252
|
### Enums
|
|
190
253
|
|
|
191
254
|
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.mts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import * as react from "react";
|
|
2
|
+
import { PropsWithChildren } from "react";
|
|
3
|
+
import * as react_jsx_runtime0 from "react/jsx-runtime";
|
|
4
|
+
import { AbilityTuple, PureAbility, RawRuleOf, Subject } from "@casl/ability";
|
|
5
|
+
import { PackRule } from "@casl/ability/extra";
|
|
6
|
+
import { BoundCanProps } from "@casl/react";
|
|
7
|
+
|
|
8
|
+
//#region src/lib/acl/appAbility.types.d.ts
|
|
9
|
+
type AppAbilities = AbilityTuple<string, Subject>;
|
|
10
|
+
type AppAbility = PureAbility<AppAbilities>;
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/lib/acl/ability.context.d.ts
|
|
13
|
+
declare namespace AbilityContext {
|
|
14
|
+
export const Consumer: react.Consumer<AppAbility>;
|
|
15
|
+
interface ProviderProps {
|
|
16
|
+
user?: {
|
|
17
|
+
aclRules?: PackRule<RawRuleOf<AppAbility>>[];
|
|
18
|
+
} | null;
|
|
19
|
+
}
|
|
20
|
+
export const Provider: ({
|
|
21
|
+
children
|
|
22
|
+
}: PropsWithChildren<ProviderProps>) => react_jsx_runtime0.JSX.Element;
|
|
23
|
+
export const useAbility: <TAppAbilities extends AppAbilities = AppAbilities>() => PureAbility<TAppAbilities>;
|
|
24
|
+
export {};
|
|
25
|
+
}
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region src/lib/acl/AclGuard.d.ts
|
|
28
|
+
interface AclGuardProps<TAppAbilities extends AppAbilities = AppAbilities> {
|
|
29
|
+
canUse: TAppAbilities;
|
|
30
|
+
redirectTo?: string;
|
|
31
|
+
}
|
|
32
|
+
declare const createAclGuard: <TAppAbilities extends AppAbilities = AppAbilities>() => ({
|
|
33
|
+
canUse,
|
|
34
|
+
redirectTo,
|
|
35
|
+
children
|
|
36
|
+
}: PropsWithChildren<AclGuardProps<TAppAbilities>>) => react.ReactNode;
|
|
37
|
+
//#endregion
|
|
38
|
+
//#region src/lib/acl/Can.d.ts
|
|
39
|
+
type CanAbility = PureAbility<AbilityTuple<AppAbilities[0], AppAbilities[1]>>;
|
|
40
|
+
type CanProps<TAppAbilities extends AppAbilities = AppAbilities> = {
|
|
41
|
+
use: TAppAbilities;
|
|
42
|
+
} & Omit<BoundCanProps<CanAbility>, "do" | "I" | "on" | "a" | "an" | "this">;
|
|
43
|
+
declare const Can: <TAppAbilities extends AppAbilities = AppAbilities>({
|
|
44
|
+
use,
|
|
45
|
+
...props
|
|
46
|
+
}: CanProps<TAppAbilities>) => react_jsx_runtime0.JSX.Element;
|
|
47
|
+
//#endregion
|
|
48
|
+
export { AbilityContext, type AppAbilities, type AppAbility, Can, createAclGuard };
|
package/dist/acl.mjs
CHANGED
|
@@ -1,8 +1,63 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import { n as OpenApiRouter, t as AuthContext } from "./auth.context-DKjzWiaA.mjs";
|
|
2
|
+
import { createContext, useEffect, useState } from "react";
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
import { AbilityBuilder, createMongoAbility } from "@casl/ability";
|
|
5
|
+
import { unpackRules } from "@casl/ability/extra";
|
|
6
|
+
import { createContextualCan, useAbility } from "@casl/react";
|
|
7
|
+
|
|
8
|
+
//#region src/lib/acl/ability.context.tsx
|
|
9
|
+
let AbilityContext;
|
|
10
|
+
(function(_AbilityContext) {
|
|
11
|
+
const createAppAbilityBuilder = () => new AbilityBuilder(createMongoAbility);
|
|
12
|
+
const initialAppAbility = createAppAbilityBuilder().build();
|
|
13
|
+
const Context = createContext({});
|
|
14
|
+
const { Consumer } = Context;
|
|
15
|
+
_AbilityContext.Consumer = Consumer;
|
|
16
|
+
_AbilityContext.Provider = ({ children }) => {
|
|
17
|
+
const [ability, setAbility] = useState(initialAppAbility);
|
|
18
|
+
const { user } = AuthContext.useAuth();
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
if (!user || !("aclRules" in user)) return;
|
|
21
|
+
const { can, build } = createAppAbilityBuilder();
|
|
22
|
+
const packedRules = user.aclRules;
|
|
23
|
+
unpackRules(packedRules).forEach(({ action, subject, conditions }) => {
|
|
24
|
+
can(action, subject, conditions);
|
|
25
|
+
});
|
|
26
|
+
setAbility(build());
|
|
27
|
+
}, [user]);
|
|
28
|
+
return /* @__PURE__ */ jsx(Context.Provider, {
|
|
29
|
+
value: ability,
|
|
30
|
+
children
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
_AbilityContext.useAbility = () => {
|
|
34
|
+
return useAbility(Context);
|
|
35
|
+
};
|
|
36
|
+
})(AbilityContext || (AbilityContext = {}));
|
|
37
|
+
|
|
38
|
+
//#endregion
|
|
39
|
+
//#region src/lib/acl/AclGuard.tsx
|
|
40
|
+
const createAclGuard = () => ({ canUse, redirectTo = "/", children }) => {
|
|
41
|
+
const ability = AbilityContext.useAbility();
|
|
42
|
+
const { replace } = OpenApiRouter.useRouter();
|
|
43
|
+
if (!ability.can(canUse[0], canUse[1])) {
|
|
44
|
+
replace(redirectTo);
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
return children;
|
|
8
48
|
};
|
|
49
|
+
|
|
50
|
+
//#endregion
|
|
51
|
+
//#region src/lib/acl/Can.tsx
|
|
52
|
+
const ContextualCan = createContextualCan(AbilityContext.Consumer);
|
|
53
|
+
const Can = ({ use, ...props }) => {
|
|
54
|
+
const [action, subject] = use;
|
|
55
|
+
return /* @__PURE__ */ jsx(ContextualCan, {
|
|
56
|
+
...props,
|
|
57
|
+
do: action,
|
|
58
|
+
on: subject
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
//#endregion
|
|
63
|
+
export { AbilityContext, Can, createAclGuard };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { createContext, use, useMemo } from "react";
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
|
|
4
|
+
//#region src/lib/config/router.context.tsx
|
|
5
|
+
let OpenApiRouter;
|
|
6
|
+
(function(_OpenApiRouter) {
|
|
7
|
+
const Context = createContext(null);
|
|
8
|
+
_OpenApiRouter.Provider = ({ children, replace }) => {
|
|
9
|
+
return /* @__PURE__ */ jsx(Context, {
|
|
10
|
+
value: useMemo(() => ({ replace }), [replace]),
|
|
11
|
+
children
|
|
12
|
+
});
|
|
13
|
+
};
|
|
14
|
+
_OpenApiRouter.useRouter = () => {
|
|
15
|
+
const context = use(Context);
|
|
16
|
+
if (!context) throw new Error("useRouter must be used within an OpenApiRouter.Provider");
|
|
17
|
+
return context;
|
|
18
|
+
};
|
|
19
|
+
})(OpenApiRouter || (OpenApiRouter = {}));
|
|
20
|
+
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region src/lib/auth/auth.context.tsx
|
|
23
|
+
let AuthContext;
|
|
24
|
+
(function(_AuthContext) {
|
|
25
|
+
const Context = createContext({});
|
|
26
|
+
_AuthContext.Provider = ({ isAuthenticated, isInitializing, logout, updateTokens, accessToken, user, userPromise, routes, loadingState, children }) => {
|
|
27
|
+
const value = useMemo(() => ({
|
|
28
|
+
isAuthenticated,
|
|
29
|
+
isInitializing,
|
|
30
|
+
logout,
|
|
31
|
+
updateTokens,
|
|
32
|
+
accessToken,
|
|
33
|
+
user,
|
|
34
|
+
userPromise,
|
|
35
|
+
routes,
|
|
36
|
+
loadingState
|
|
37
|
+
}), [
|
|
38
|
+
isAuthenticated,
|
|
39
|
+
isInitializing,
|
|
40
|
+
logout,
|
|
41
|
+
updateTokens,
|
|
42
|
+
accessToken,
|
|
43
|
+
user,
|
|
44
|
+
userPromise,
|
|
45
|
+
routes,
|
|
46
|
+
loadingState
|
|
47
|
+
]);
|
|
48
|
+
return /* @__PURE__ */ jsx(Context.Provider, {
|
|
49
|
+
value,
|
|
50
|
+
children
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
_AuthContext.useAuth = () => {
|
|
54
|
+
return use(Context);
|
|
55
|
+
};
|
|
56
|
+
})(AuthContext || (AuthContext = {}));
|
|
57
|
+
|
|
58
|
+
//#endregion
|
|
59
|
+
export { OpenApiRouter as n, AuthContext as t };
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { a as deepMerge, p as DEFAULT_GENERATE_OPTIONS, r as writeGenerateFileData, t as generateCodeFromOpenAPIDoc, x as Profiler } from "./generateCodeFromOpenAPIDoc-B5lnFNGB.mjs";
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import SwaggerParser from "@apidevtools/swagger-parser";
|
|
5
|
+
|
|
6
|
+
//#region src/generators/core/resolveConfig.ts
|
|
7
|
+
function resolveConfig({ fileConfig = {}, params: { excludeTags, inlineEndpointsExcludeModules, ...options } }) {
|
|
8
|
+
const resolvedConfig = deepMerge(DEFAULT_GENERATE_OPTIONS, fileConfig ?? {}, {
|
|
9
|
+
...options,
|
|
10
|
+
excludeTags: excludeTags?.split(","),
|
|
11
|
+
inlineEndpointsExcludeModules: inlineEndpointsExcludeModules?.split(",")
|
|
12
|
+
});
|
|
13
|
+
resolvedConfig.checkAcl = resolvedConfig.acl && resolvedConfig.checkAcl;
|
|
14
|
+
return resolvedConfig;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/generators/run/generate.runner.ts
|
|
19
|
+
const CACHE_FILE_NAME = ".openapi-codegen-cache.json";
|
|
20
|
+
async function runGenerate({ fileConfig, params, profiler = new Profiler(process.env.OPENAPI_CODEGEN_PROFILE === "1") }) {
|
|
21
|
+
const config = profiler.runSync("config.resolve", () => resolveConfig({
|
|
22
|
+
fileConfig,
|
|
23
|
+
params: params ?? {}
|
|
24
|
+
}));
|
|
25
|
+
const openApiDoc = await getOpenApiDoc(config.input, profiler);
|
|
26
|
+
const openApiHash = hashString(stableStringify(openApiDoc));
|
|
27
|
+
const optionsHash = hashString(stableStringify(getCacheableConfig(config)));
|
|
28
|
+
const cacheFilePath = path.resolve(config.output, CACHE_FILE_NAME);
|
|
29
|
+
if (config.incremental) {
|
|
30
|
+
const cached = readCache(cacheFilePath);
|
|
31
|
+
if (cached && cached.openApiHash === openApiHash && cached.optionsHash === optionsHash) return {
|
|
32
|
+
skipped: true,
|
|
33
|
+
config
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
const filesData = profiler.runSync("generate.total", () => generateCodeFromOpenAPIDoc(openApiDoc, config, profiler));
|
|
37
|
+
profiler.runSync("files.write", () => writeGenerateFileData(filesData));
|
|
38
|
+
if (config.incremental) writeCache(cacheFilePath, {
|
|
39
|
+
openApiHash,
|
|
40
|
+
optionsHash
|
|
41
|
+
});
|
|
42
|
+
return {
|
|
43
|
+
skipped: false,
|
|
44
|
+
config
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
async function getOpenApiDoc(input, profiler) {
|
|
48
|
+
const parsedDoc = await profiler.runAsync("openapi.parse", async () => await SwaggerParser.parse(input));
|
|
49
|
+
if (!profiler.runSync("openapi.detectExternalRefs", () => hasExternalRef(parsedDoc))) return parsedDoc;
|
|
50
|
+
return await profiler.runAsync("openapi.bundle", async () => await SwaggerParser.bundle(input));
|
|
51
|
+
}
|
|
52
|
+
function hasExternalRef(value) {
|
|
53
|
+
const stack = [value];
|
|
54
|
+
const visited = /* @__PURE__ */ new Set();
|
|
55
|
+
while (stack.length > 0) {
|
|
56
|
+
const current = stack.pop();
|
|
57
|
+
if (!current || typeof current !== "object") continue;
|
|
58
|
+
if (visited.has(current)) continue;
|
|
59
|
+
visited.add(current);
|
|
60
|
+
if ("$ref" in current && typeof current.$ref === "string" && !current.$ref.startsWith("#/")) return true;
|
|
61
|
+
for (const nested of Object.values(current)) if (nested && typeof nested === "object") stack.push(nested);
|
|
62
|
+
}
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
function getCacheableConfig(config) {
|
|
66
|
+
const { output, incremental, ...cacheableConfig } = config;
|
|
67
|
+
return cacheableConfig;
|
|
68
|
+
}
|
|
69
|
+
function readCache(filePath) {
|
|
70
|
+
if (!fs.existsSync(filePath)) return null;
|
|
71
|
+
try {
|
|
72
|
+
return JSON.parse(fs.readFileSync(filePath, "utf-8"));
|
|
73
|
+
} catch {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function writeCache(filePath, data) {
|
|
78
|
+
const dir = path.dirname(filePath);
|
|
79
|
+
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
|
|
80
|
+
fs.writeFileSync(filePath, JSON.stringify(data), "utf-8");
|
|
81
|
+
}
|
|
82
|
+
function hashString(input) {
|
|
83
|
+
let hash = 2166136261;
|
|
84
|
+
for (let i = 0; i < input.length; i += 1) {
|
|
85
|
+
hash ^= input.charCodeAt(i);
|
|
86
|
+
hash = Math.imul(hash, 16777619);
|
|
87
|
+
}
|
|
88
|
+
return (hash >>> 0).toString(16);
|
|
89
|
+
}
|
|
90
|
+
function stableStringify(input) {
|
|
91
|
+
if (input === null || typeof input !== "object") return JSON.stringify(input);
|
|
92
|
+
if (Array.isArray(input)) return `[${input.map((item) => stableStringify(item)).join(",")}]`;
|
|
93
|
+
const obj = input;
|
|
94
|
+
return `{${Object.keys(obj).sort((a, b) => a.localeCompare(b)).map((key) => `${JSON.stringify(key)}:${stableStringify(obj[key])}`).join(",")}}`;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
//#endregion
|
|
98
|
+
export { resolveConfig as n, runGenerate as t };
|