@blimu/codegen 0.3.0 → 0.4.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/dist/generator/typescript/templates/auth-strategies.ts.hbs +5 -9
- package/dist/generator/typescript/templates/client.ts.hbs +43 -48
- package/dist/generator/typescript/templates/package.json.hbs +1 -1
- package/dist/generator/typescript/templates/schema.zod.ts.hbs +35 -33
- package/dist/generator/typescript/templates/service.ts.hbs +2 -0
- package/dist/generator/typescript/templates/tsconfig.json.hbs +3 -3
- package/dist/generator/typescript/templates/tsup.config.ts.hbs +1 -1
- package/dist/index.d.mts +3 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +5 -5
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +5 -5
- package/dist/index.mjs.map +1 -1
- package/dist/main.js +5 -5
- package/dist/main.js.map +1 -1
- package/package.json +3 -2
|
@@ -1,14 +1,10 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
AuthStrategy{{~#if (hasBearerScheme IR.securitySchemes)}},
|
|
3
|
-
BearerAuthStrategy{{~/if~}}{{~#if (hasApiKeyScheme IR.securitySchemes)}},
|
|
4
|
-
ApiKeyAuthStrategy{{~/if~}}
|
|
5
|
-
} from '@blimu/fetch';
|
|
1
|
+
import type { AuthStrategy } from '@blimu/fetch';
|
|
6
2
|
import type { ClientOption } from './client';
|
|
7
3
|
|
|
8
4
|
export function buildAuthStrategies(cfg: ClientOption): AuthStrategy[] {
|
|
9
5
|
const authStrategies: AuthStrategy[] = [...(cfg?.authStrategies || [])];
|
|
10
|
-
|
|
11
|
-
{{
|
|
6
|
+
|
|
7
|
+
{{#each IR.securitySchemes~}}
|
|
12
8
|
{{~#if (eq this.type "http")~}}
|
|
13
9
|
{{~#if (eq this.scheme "bearer")~}}
|
|
14
10
|
if (cfg.{{camel this.key}}) {
|
|
@@ -20,8 +16,8 @@ export function buildAuthStrategies(cfg: ClientOption): AuthStrategy[] {
|
|
|
20
16
|
{{~/if~}}
|
|
21
17
|
{{~/if~}}
|
|
22
18
|
{{~/each~}}
|
|
23
|
-
|
|
24
|
-
{{
|
|
19
|
+
|
|
20
|
+
{{#each IR.securitySchemes~}}
|
|
25
21
|
{{~#if (eq this.type "http")~}}
|
|
26
22
|
{{~#if (eq this.scheme "basic")~}}
|
|
27
23
|
if (cfg.{{camel this.key}}) {
|
|
@@ -1,66 +1,61 @@
|
|
|
1
1
|
import { FetchClient, FetchError } from '@blimu/fetch';
|
|
2
2
|
import {
|
|
3
|
-
type FetchClientConfig{{
|
|
4
|
-
type BearerAuthStrategy{{
|
|
5
|
-
type ApiKeyAuthStrategy{{
|
|
3
|
+
type FetchClientConfig{{#if (hasBearerScheme IR.securitySchemes)}},
|
|
4
|
+
type BearerAuthStrategy{{/if}}{{#if (hasApiKeyScheme IR.securitySchemes)}},
|
|
5
|
+
type ApiKeyAuthStrategy{{/if}}
|
|
6
6
|
} from '@blimu/fetch';
|
|
7
7
|
import { buildAuthStrategies } from './auth-strategies';
|
|
8
|
-
{{
|
|
8
|
+
{{#each IR.services}}
|
|
9
9
|
import { {{serviceName tag}} } from './services/{{fileBase tag}}';
|
|
10
|
-
{{
|
|
10
|
+
{{/each}}
|
|
11
11
|
|
|
12
12
|
export type ClientOption = FetchClientConfig & {
|
|
13
|
-
{{
|
|
14
|
-
{{
|
|
15
|
-
|
|
13
|
+
{{#each IR.securitySchemes}}
|
|
14
|
+
{{#if (eq this.type "http")}}
|
|
15
|
+
{{#if (eq this.scheme "bearer")}}
|
|
16
16
|
{{camel this.key}}?: BearerAuthStrategy['token'];
|
|
17
|
-
|
|
17
|
+
{{else if (eq this.scheme "basic")}}
|
|
18
18
|
{{camel this.key}}?: { username: string; password: string };
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
{{/if}}
|
|
20
|
+
{{else if (eq this.type "apiKey")}}
|
|
21
21
|
{{camel this.key}}?: ApiKeyAuthStrategy['key'];
|
|
22
|
-
{{
|
|
23
|
-
{{
|
|
22
|
+
{{/if}}
|
|
23
|
+
{{/each}}
|
|
24
24
|
};
|
|
25
|
-
|
|
26
|
-
{{
|
|
27
|
-
|
|
28
|
-
{{
|
|
29
|
-
{{~#each (groupByNamespace IR.services) as |services namespace|~}}
|
|
30
|
-
{{~#if (ne namespace "")~}}
|
|
25
|
+
{{setVar "grouped" (groupByNamespace IR.services)}}
|
|
26
|
+
{{! Generate namespace classes for non-root services }}
|
|
27
|
+
{{#each (groupByNamespace IR.services) as |services namespace|}}
|
|
28
|
+
{{#if (ne namespace "")}}
|
|
31
29
|
class {{pascal namespace}}Namespace {
|
|
32
|
-
{{
|
|
30
|
+
{{#each services}}
|
|
33
31
|
readonly {{serviceProp (getServiceName tag)}}: {{serviceName tag}};
|
|
34
|
-
{{
|
|
32
|
+
{{/each}}
|
|
35
33
|
|
|
36
34
|
constructor(core: FetchClient) {
|
|
37
|
-
{{
|
|
35
|
+
{{#each services}}
|
|
38
36
|
this.{{serviceProp (getServiceName tag)}} = new {{serviceName tag}}(core);
|
|
39
|
-
{{
|
|
37
|
+
{{/each}}
|
|
40
38
|
}
|
|
41
39
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
{{~/each~}}
|
|
45
|
-
|
|
40
|
+
{{/if}}
|
|
41
|
+
{{/each}}
|
|
46
42
|
export class {{Client.name}} {
|
|
47
|
-
{{
|
|
48
|
-
{{
|
|
43
|
+
{{! Root level services (no namespace) }}
|
|
44
|
+
{{#each (getRootServices IR.services)}}
|
|
49
45
|
readonly {{serviceProp tag}}: {{serviceName tag}};
|
|
50
|
-
{{
|
|
51
|
-
{{
|
|
52
|
-
{{
|
|
53
|
-
{{
|
|
46
|
+
{{/each}}
|
|
47
|
+
{{! Namespace properties }}
|
|
48
|
+
{{#each (groupByNamespace IR.services) as |services namespace|}}
|
|
49
|
+
{{#if (ne namespace "")}}
|
|
54
50
|
readonly {{serviceProp namespace}}: {{pascal namespace}}Namespace;
|
|
55
|
-
{{
|
|
56
|
-
{{
|
|
51
|
+
{{/if}}
|
|
52
|
+
{{/each}}
|
|
57
53
|
|
|
58
54
|
constructor(options?: ClientOption) {
|
|
59
|
-
const {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
} = options || {};
|
|
55
|
+
const restCfg = { ...(options || {}) };
|
|
56
|
+
{{#each IR.securitySchemes}}
|
|
57
|
+
delete restCfg.{{camel this.key}};
|
|
58
|
+
{{/each}}
|
|
64
59
|
|
|
65
60
|
const authStrategies = buildAuthStrategies(options || {});
|
|
66
61
|
|
|
@@ -70,16 +65,16 @@ export class {{Client.name}} {
|
|
|
70
65
|
...(authStrategies.length > 0 ? { authStrategies } : {}),
|
|
71
66
|
});
|
|
72
67
|
|
|
73
|
-
{{
|
|
74
|
-
{{
|
|
68
|
+
{{! Initialize root services }}
|
|
69
|
+
{{#each (getRootServices IR.services)}}
|
|
75
70
|
this.{{serviceProp tag}} = new {{serviceName tag}}(core);
|
|
76
|
-
{{
|
|
77
|
-
{{
|
|
78
|
-
{{
|
|
79
|
-
{{
|
|
71
|
+
{{/each}}
|
|
72
|
+
{{! Initialize namespaces }}
|
|
73
|
+
{{#each (groupByNamespace IR.services) as |services namespace|}}
|
|
74
|
+
{{#if (ne namespace "")}}
|
|
80
75
|
this.{{serviceProp namespace}} = new {{pascal namespace}}Namespace(core);
|
|
81
|
-
{{
|
|
82
|
-
{{
|
|
76
|
+
{{/if}}
|
|
77
|
+
{{/each}}
|
|
83
78
|
}
|
|
84
79
|
}
|
|
85
80
|
|
|
@@ -13,19 +13,20 @@ import { z } from 'zod';
|
|
|
13
13
|
{{/if}}
|
|
14
14
|
*/
|
|
15
15
|
export const {{this.name}}Schema = {{zodSchema this.schema}};
|
|
16
|
+
|
|
16
17
|
{{/if}}
|
|
17
18
|
{{/each}}
|
|
18
19
|
|
|
19
20
|
{{! Objects and other named models: render Zod schemas from structured IR }}
|
|
20
21
|
{{#if (gt (len IR.modelDefs) 0)}}
|
|
21
|
-
{{
|
|
22
|
-
{{
|
|
23
|
-
{{
|
|
22
|
+
{{#each IR.modelDefs}}
|
|
23
|
+
{{#unless (or (eq this.schema.kind "string") (eq this.schema.kind "number") (eq this.schema.kind "integer") (eq this.schema.kind "boolean") (eq this.schema.kind "null"))}}
|
|
24
|
+
{{#if (eq this.schema.kind "enum")}}
|
|
24
25
|
/**
|
|
25
26
|
* Zod schema for {{this.name}}
|
|
26
|
-
{{
|
|
27
|
+
{{#if this.annotations.description}}
|
|
27
28
|
* {{decodeHtml (replace this.annotations.description "*/" "*\\/")}}
|
|
28
|
-
{{
|
|
29
|
+
{{/if}}
|
|
29
30
|
*/
|
|
30
31
|
export const {{this.name}}Schema = z.enum([
|
|
31
32
|
{{~#each this.schema.enumValues as |v|~}}
|
|
@@ -33,12 +34,12 @@ export const {{this.name}}Schema = z.enum([
|
|
|
33
34
|
{{~/each~}}
|
|
34
35
|
]);
|
|
35
36
|
|
|
36
|
-
|
|
37
|
+
{{else if (eq this.schema.kind "object")}}
|
|
37
38
|
/**
|
|
38
39
|
* Zod schema for {{this.name}}
|
|
39
|
-
{{
|
|
40
|
+
{{#if this.annotations.description}}
|
|
40
41
|
* {{decodeHtml (replace this.annotations.description "*/" "*\\/")}}
|
|
41
|
-
{{
|
|
42
|
+
{{/if}}
|
|
42
43
|
*/
|
|
43
44
|
export const {{this.name}}Schema = z.object({
|
|
44
45
|
{{~#each this.schema.properties~}}
|
|
@@ -49,18 +50,18 @@ export const {{this.name}}Schema = z.object({
|
|
|
49
50
|
{{~/each~}}
|
|
50
51
|
});
|
|
51
52
|
|
|
52
|
-
|
|
53
|
+
{{else if (eq this.schema.kind "ref")}}
|
|
53
54
|
/**
|
|
54
55
|
* Zod schema for {{this.name}}
|
|
55
56
|
*/
|
|
56
57
|
export const {{this.name}}Schema = {{zodSchema this.schema}};
|
|
57
58
|
|
|
58
|
-
|
|
59
|
+
{{else if (eq this.schema.kind "oneOf")}}
|
|
59
60
|
/**
|
|
60
61
|
* Zod schema for {{this.name}}
|
|
61
|
-
{{
|
|
62
|
+
{{#if this.annotations.description}}
|
|
62
63
|
* {{decodeHtml (replace this.annotations.description "*/" "*\\/")}}
|
|
63
|
-
{{
|
|
64
|
+
{{/if}}
|
|
64
65
|
*/
|
|
65
66
|
export const {{this.name}}Schema = z.union([
|
|
66
67
|
{{~#each this.schema.oneOf as |opt|~}}
|
|
@@ -68,12 +69,12 @@ export const {{this.name}}Schema = z.union([
|
|
|
68
69
|
{{~/each~}}
|
|
69
70
|
]);
|
|
70
71
|
|
|
71
|
-
|
|
72
|
+
{{else if (eq this.schema.kind "anyOf")}}
|
|
72
73
|
/**
|
|
73
74
|
* Zod schema for {{this.name}}
|
|
74
|
-
{{
|
|
75
|
+
{{#if this.annotations.description}}
|
|
75
76
|
* {{decodeHtml (replace this.annotations.description "*/" "*\\/")}}
|
|
76
|
-
{{
|
|
77
|
+
{{/if}}
|
|
77
78
|
*/
|
|
78
79
|
export const {{this.name}}Schema = z.union([
|
|
79
80
|
{{~#each this.schema.anyOf as |opt|~}}
|
|
@@ -81,36 +82,37 @@ export const {{this.name}}Schema = z.union([
|
|
|
81
82
|
{{~/each~}}
|
|
82
83
|
]);
|
|
83
84
|
|
|
84
|
-
|
|
85
|
+
{{else if (eq this.schema.kind "allOf")}}
|
|
85
86
|
/**
|
|
86
87
|
* Zod schema for {{this.name}}
|
|
87
|
-
{{
|
|
88
|
+
{{#if this.annotations.description}}
|
|
88
89
|
* {{decodeHtml (replace this.annotations.description "*/" "*\\/")}}
|
|
89
|
-
{{
|
|
90
|
+
{{/if}}
|
|
90
91
|
*/
|
|
91
92
|
export const {{this.name}}Schema = {{#each this.schema.allOf}}{{#if @first}}{{zodSchema this}}{{else}}.extend({{zodSchema this}}.shape){{/if}}{{/each}};
|
|
92
93
|
|
|
93
|
-
|
|
94
|
+
{{else if (eq this.schema.kind "array")}}
|
|
94
95
|
/**
|
|
95
96
|
* Zod schema for {{this.name}}
|
|
96
|
-
{{
|
|
97
|
+
{{#if this.annotations.description}}
|
|
97
98
|
* {{decodeHtml (replace this.annotations.description "*/" "*\\/")}}
|
|
98
|
-
{{
|
|
99
|
+
{{/if}}
|
|
99
100
|
*/
|
|
100
101
|
export const {{this.name}}Schema = z.array({{#if this.schema.items}}{{zodSchema this.schema.items}}{{else}}z.unknown(){{/if}});
|
|
101
102
|
|
|
102
|
-
|
|
103
|
+
{{else}}
|
|
103
104
|
/**
|
|
104
105
|
* Zod schema for {{this.name}}
|
|
105
|
-
{{
|
|
106
|
+
{{#if this.annotations.description}}
|
|
106
107
|
* {{decodeHtml (replace this.annotations.description "*/" "*\\/")}}
|
|
107
|
-
{{
|
|
108
|
+
{{/if}}
|
|
108
109
|
*/
|
|
109
110
|
export const {{this.name}}Schema = {{zodSchema this.schema}};
|
|
110
111
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
112
|
+
{{/if}}
|
|
113
|
+
{{/unless}}
|
|
114
|
+
|
|
115
|
+
{{/each}}
|
|
114
116
|
{{~/if~}}
|
|
115
117
|
|
|
116
118
|
{{#if IR.services}}
|
|
@@ -122,17 +124,17 @@ export const {{this.name}}Schema = {{zodSchema this.schema}};
|
|
|
122
124
|
{{#if (gt (len this.queryParams) 0)}}
|
|
123
125
|
/**
|
|
124
126
|
* Schema for query params of {{../tag}}.{{pascal (methodName this)}}
|
|
125
|
-
{{
|
|
127
|
+
{{#if this.description}}
|
|
126
128
|
* {{decodeHtml (replace this.description "*/" "*\\/")}}
|
|
127
|
-
{{
|
|
129
|
+
{{/if}}
|
|
128
130
|
*/
|
|
129
131
|
export const {{pascal ../tag}}{{pascal (methodName this)}}QuerySchema = z.object({
|
|
130
|
-
{{
|
|
131
|
-
{{
|
|
132
|
+
{{#each this.queryParams}}
|
|
133
|
+
{{#if this.description}}
|
|
132
134
|
/** {{decodeHtml (replace this.description "*/" "*\\/")}} */
|
|
133
|
-
{{
|
|
135
|
+
{{/if}}
|
|
134
136
|
{{quotePropName this.name}}: {{zodSchema this.schema}}{{#unless this.required}}.optional(){{/unless}},
|
|
135
|
-
{{
|
|
137
|
+
{{/each}}
|
|
136
138
|
});
|
|
137
139
|
|
|
138
140
|
{{/if}}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"compilerOptions": {
|
|
3
|
-
"rootDir": "./
|
|
3
|
+
"rootDir": "./{{Client.srcDir}}",
|
|
4
4
|
"module": "ES2022",
|
|
5
5
|
"moduleResolution": "Bundler",
|
|
6
6
|
"target": "ES2022",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"allowSyntheticDefaultImports": true,
|
|
13
13
|
"sourceMap": true,
|
|
14
14
|
"outDir": "./dist",
|
|
15
|
-
"baseUrl": "./
|
|
15
|
+
"baseUrl": "./{{Client.srcDir}}",
|
|
16
16
|
"incremental": true,
|
|
17
17
|
"tsBuildInfoFile": "./dist/tsconfig.tsbuildinfo",
|
|
18
18
|
"skipLibCheck": true,
|
|
@@ -23,6 +23,6 @@
|
|
|
23
23
|
"noUnusedParameters": true,
|
|
24
24
|
"useDefineForClassFields": true
|
|
25
25
|
},
|
|
26
|
-
"include": ["
|
|
26
|
+
"include": ["{{Client.srcDir}}/**/*"],
|
|
27
27
|
"exclude": ["node_modules", "dist"]
|
|
28
28
|
}
|
package/dist/index.d.mts
CHANGED
|
@@ -23,6 +23,7 @@ declare const TypeScriptClientSchema: z.ZodObject<{
|
|
|
23
23
|
type: z.ZodLiteral<"typescript">;
|
|
24
24
|
packageName: z.ZodString;
|
|
25
25
|
moduleName: z.ZodOptional<z.ZodString>;
|
|
26
|
+
srcDir: z.ZodOptional<z.ZodString>;
|
|
26
27
|
includeQueryKeys: z.ZodOptional<z.ZodBoolean>;
|
|
27
28
|
predefinedTypes: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
28
29
|
type: z.ZodString;
|
|
@@ -47,6 +48,7 @@ declare const ClientSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
|
47
48
|
type: z.ZodLiteral<"typescript">;
|
|
48
49
|
packageName: z.ZodString;
|
|
49
50
|
moduleName: z.ZodOptional<z.ZodString>;
|
|
51
|
+
srcDir: z.ZodOptional<z.ZodString>;
|
|
50
52
|
includeQueryKeys: z.ZodOptional<z.ZodBoolean>;
|
|
51
53
|
predefinedTypes: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
52
54
|
type: z.ZodString;
|
|
@@ -74,6 +76,7 @@ declare const ConfigSchema: z.ZodObject<{
|
|
|
74
76
|
type: z.ZodLiteral<"typescript">;
|
|
75
77
|
packageName: z.ZodString;
|
|
76
78
|
moduleName: z.ZodOptional<z.ZodString>;
|
|
79
|
+
srcDir: z.ZodOptional<z.ZodString>;
|
|
77
80
|
includeQueryKeys: z.ZodOptional<z.ZodBoolean>;
|
|
78
81
|
predefinedTypes: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
79
82
|
type: z.ZodString;
|
package/dist/index.d.ts
CHANGED
|
@@ -23,6 +23,7 @@ declare const TypeScriptClientSchema: z.ZodObject<{
|
|
|
23
23
|
type: z.ZodLiteral<"typescript">;
|
|
24
24
|
packageName: z.ZodString;
|
|
25
25
|
moduleName: z.ZodOptional<z.ZodString>;
|
|
26
|
+
srcDir: z.ZodOptional<z.ZodString>;
|
|
26
27
|
includeQueryKeys: z.ZodOptional<z.ZodBoolean>;
|
|
27
28
|
predefinedTypes: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
28
29
|
type: z.ZodString;
|
|
@@ -47,6 +48,7 @@ declare const ClientSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
|
47
48
|
type: z.ZodLiteral<"typescript">;
|
|
48
49
|
packageName: z.ZodString;
|
|
49
50
|
moduleName: z.ZodOptional<z.ZodString>;
|
|
51
|
+
srcDir: z.ZodOptional<z.ZodString>;
|
|
50
52
|
includeQueryKeys: z.ZodOptional<z.ZodBoolean>;
|
|
51
53
|
predefinedTypes: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
52
54
|
type: z.ZodString;
|
|
@@ -74,6 +76,7 @@ declare const ConfigSchema: z.ZodObject<{
|
|
|
74
76
|
type: z.ZodLiteral<"typescript">;
|
|
75
77
|
packageName: z.ZodString;
|
|
76
78
|
moduleName: z.ZodOptional<z.ZodString>;
|
|
79
|
+
srcDir: z.ZodOptional<z.ZodString>;
|
|
77
80
|
includeQueryKeys: z.ZodOptional<z.ZodBoolean>;
|
|
78
81
|
predefinedTypes: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
79
82
|
type: z.ZodString;
|