@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.
@@ -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
- {{~#each IR.securitySchemes~}}
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
- {{~#each IR.securitySchemes~}}
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{{~#if (hasBearerScheme IR.securitySchemes)}},
4
- type BearerAuthStrategy{{~/if~}}{{~#if (hasApiKeyScheme IR.securitySchemes)}},
5
- type ApiKeyAuthStrategy{{~/if~}}
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
- {{~#each IR.services~}}
8
+ {{#each IR.services}}
9
9
  import { {{serviceName tag}} } from './services/{{fileBase tag}}';
10
- {{~/each~}}
10
+ {{/each}}
11
11
 
12
12
  export type ClientOption = FetchClientConfig & {
13
- {{~#each IR.securitySchemes~}}
14
- {{~#if (eq this.type "http")~}}
15
- {{~#if (eq this.scheme "bearer")~}}
13
+ {{#each IR.securitySchemes}}
14
+ {{#if (eq this.type "http")}}
15
+ {{#if (eq this.scheme "bearer")}}
16
16
  {{camel this.key}}?: BearerAuthStrategy['token'];
17
- {{~else if (eq this.scheme "basic")~}}
17
+ {{else if (eq this.scheme "basic")}}
18
18
  {{camel this.key}}?: { username: string; password: string };
19
- {{~/if~}}
20
- {{~else if (eq this.type "apiKey")~}}
19
+ {{/if}}
20
+ {{else if (eq this.type "apiKey")}}
21
21
  {{camel this.key}}?: ApiKeyAuthStrategy['key'];
22
- {{~/if~}}
23
- {{~/each~}}
22
+ {{/if}}
23
+ {{/each}}
24
24
  };
25
-
26
- {{~setVar "grouped" (groupByNamespace IR.services)~}}
27
-
28
- {{~! Generate namespace classes for non-root services ~}}
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
- {{~#each services~}}
30
+ {{#each services}}
33
31
  readonly {{serviceProp (getServiceName tag)}}: {{serviceName tag}};
34
- {{~/each~}}
32
+ {{/each}}
35
33
 
36
34
  constructor(core: FetchClient) {
37
- {{~#each services~}}
35
+ {{#each services}}
38
36
  this.{{serviceProp (getServiceName tag)}} = new {{serviceName tag}}(core);
39
- {{~/each~}}
37
+ {{/each}}
40
38
  }
41
39
  }
42
-
43
- {{~/if~}}
44
- {{~/each~}}
45
-
40
+ {{/if}}
41
+ {{/each}}
46
42
  export class {{Client.name}} {
47
- {{~! Root level services (no namespace) ~}}
48
- {{~#each (getRootServices IR.services)~}}
43
+ {{! Root level services (no namespace) }}
44
+ {{#each (getRootServices IR.services)}}
49
45
  readonly {{serviceProp tag}}: {{serviceName tag}};
50
- {{~/each~}}
51
- {{~! Namespace properties ~}}
52
- {{~#each (groupByNamespace IR.services) as |services namespace|~}}
53
- {{~#if (ne namespace "")~}}
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
- {{~/if~}}
56
- {{~/each~}}
51
+ {{/if}}
52
+ {{/each}}
57
53
 
58
54
  constructor(options?: ClientOption) {
59
- const {
60
- {{~#each IR.securitySchemes~}}
61
- {{camel this.key}},{{~/each~}}
62
- ...restCfg
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
- {{~! Initialize root services ~}}
74
- {{~#each (getRootServices IR.services)~}}
68
+ {{! Initialize root services }}
69
+ {{#each (getRootServices IR.services)}}
75
70
  this.{{serviceProp tag}} = new {{serviceName tag}}(core);
76
- {{~/each~}}
77
- {{~! Initialize namespaces ~}}
78
- {{~#each (groupByNamespace IR.services) as |services namespace|~}}
79
- {{~#if (ne namespace "")~}}
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
- {{~/if~}}
82
- {{~/each~}}
76
+ {{/if}}
77
+ {{/each}}
83
78
  }
84
79
  }
85
80
 
@@ -5,7 +5,7 @@
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
7
7
  "types": "dist/index.d.ts",
8
- "files": ["dist/**", "src/**"],
8
+ "files": ["dist/**", "{{Client.srcDir}}/**"],
9
9
  "exports": {
10
10
  ".": {
11
11
  "types": "./dist/index.d.ts",
@@ -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
- {{~#each IR.modelDefs}}
22
- {{~#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"))}}
23
- {{~#if (eq this.schema.kind "enum")~}}
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
- {{~#if this.annotations.description~}}
27
+ {{#if this.annotations.description}}
27
28
  * {{decodeHtml (replace this.annotations.description "*/" "*\\/")}}
28
- {{~/if~}}
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
- {{~else if (eq this.schema.kind "object")~}}
37
+ {{else if (eq this.schema.kind "object")}}
37
38
  /**
38
39
  * Zod schema for {{this.name}}
39
- {{~#if this.annotations.description~}}
40
+ {{#if this.annotations.description}}
40
41
  * {{decodeHtml (replace this.annotations.description "*/" "*\\/")}}
41
- {{~/if~}}
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
- {{~else if (eq this.schema.kind "ref")~}}
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
- {{~else if (eq this.schema.kind "oneOf")~}}
59
+ {{else if (eq this.schema.kind "oneOf")}}
59
60
  /**
60
61
  * Zod schema for {{this.name}}
61
- {{~#if this.annotations.description~}}
62
+ {{#if this.annotations.description}}
62
63
  * {{decodeHtml (replace this.annotations.description "*/" "*\\/")}}
63
- {{~/if~}}
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
- {{~else if (eq this.schema.kind "anyOf")~}}
72
+ {{else if (eq this.schema.kind "anyOf")}}
72
73
  /**
73
74
  * Zod schema for {{this.name}}
74
- {{~#if this.annotations.description~}}
75
+ {{#if this.annotations.description}}
75
76
  * {{decodeHtml (replace this.annotations.description "*/" "*\\/")}}
76
- {{~/if~}}
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
- {{~else if (eq this.schema.kind "allOf")~}}
85
+ {{else if (eq this.schema.kind "allOf")}}
85
86
  /**
86
87
  * Zod schema for {{this.name}}
87
- {{~#if this.annotations.description~}}
88
+ {{#if this.annotations.description}}
88
89
  * {{decodeHtml (replace this.annotations.description "*/" "*\\/")}}
89
- {{~/if~}}
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
- {{~else if (eq this.schema.kind "array")~}}
94
+ {{else if (eq this.schema.kind "array")}}
94
95
  /**
95
96
  * Zod schema for {{this.name}}
96
- {{~#if this.annotations.description~}}
97
+ {{#if this.annotations.description}}
97
98
  * {{decodeHtml (replace this.annotations.description "*/" "*\\/")}}
98
- {{~/if~}}
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
- {{~else~}}
103
+ {{else}}
103
104
  /**
104
105
  * Zod schema for {{this.name}}
105
- {{~#if this.annotations.description~}}
106
+ {{#if this.annotations.description}}
106
107
  * {{decodeHtml (replace this.annotations.description "*/" "*\\/")}}
107
- {{~/if~}}
108
+ {{/if}}
108
109
  */
109
110
  export const {{this.name}}Schema = {{zodSchema this.schema}};
110
111
 
111
- {{~/if~}}
112
- {{~/unless~}}
113
- {{~/each~}}
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
- {{~#if this.description~}}
127
+ {{#if this.description}}
126
128
  * {{decodeHtml (replace this.description "*/" "*\\/")}}
127
- {{~/if~}}
129
+ {{/if}}
128
130
  */
129
131
  export const {{pascal ../tag}}{{pascal (methodName this)}}QuerySchema = z.object({
130
- {{~#each this.queryParams~}}
131
- {{~#if this.description~}}
132
+ {{#each this.queryParams}}
133
+ {{#if this.description}}
132
134
  /** {{decodeHtml (replace this.description "*/" "*\\/")}} */
133
- {{~/if~}}
135
+ {{/if}}
134
136
  {{quotePropName this.name}}: {{zodSchema this.schema}}{{#unless this.required}}.optional(){{/unless}},
135
- {{~/each~}}
137
+ {{/each}}
136
138
  });
137
139
 
138
140
  {{/if}}
@@ -1,5 +1,7 @@
1
1
  import { FetchClient } from '@blimu/fetch';
2
+ {{#if (serviceUsesSchema Service)}}
2
3
  import * as Schema from "../schema";
4
+ {{/if}}
3
5
  {{#if Client.includeQueryKeys}}
4
6
  import { isNotUndefined } from "../utils";
5
7
  {{/if}}
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "compilerOptions": {
3
- "rootDir": "./src",
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": "./src",
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": ["src/**/*"],
26
+ "include": ["{{Client.srcDir}}/**/*"],
27
27
  "exclude": ["node_modules", "dist"]
28
28
  }
@@ -1,7 +1,7 @@
1
1
  import { defineConfig } from "tsup";
2
2
 
3
3
  export default defineConfig({
4
- entry: ["src/**/*.ts"],
4
+ entry: ["{{Client.srcDir}}/**/*.ts"],
5
5
  format: ["cjs", "esm"],
6
6
  dts: true,
7
7
  splitting: false,
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;