@open-norantec/herbal 1.0.2-alpha.19 → 1.0.2-alpha.20

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,13 @@
1
1
  import 'reflect-metadata';
2
2
  import { Constructor } from 'type-fest';
3
- import { GroupsFactory } from '../decorators/client-groups.decorator';
4
3
  import { OpenAPIObject } from 'zod-openapi/dist/openapi3-ts/dist/model/openapi31';
5
4
  export interface CreateClientOptions {
6
5
  Module: Constructor<any>;
7
- allowedClientGroupsFactory?: GroupsFactory;
8
6
  }
9
7
  export declare abstract class Client {
10
8
  readonly options: CreateClientOptions;
11
9
  protected document: OpenAPIObject;
12
10
  constructor(options: CreateClientOptions);
13
- abstract generateClientSourceFile(currentGroup?: string): string;
11
+ abstract generateClientSourceFile(): string;
12
+ createSchema(group?: string): void;
14
13
  }
@@ -8,7 +8,6 @@ var controller_util_class_1 = require("../utilities/controller-util.class");
8
8
  var method_decorator_1 = require("../decorators/method.decorator");
9
9
  var Client = (function () {
10
10
  function Client(options) {
11
- var _this = this;
12
11
  this.options = options;
13
12
  this.document = {
14
13
  openapi: '3.1.0',
@@ -18,19 +17,24 @@ var Client = (function () {
18
17
  },
19
18
  paths: {},
20
19
  };
21
- nest_util_class_1.NestUtil.getControllerClasses(options.Module).forEach(function (Class) {
20
+ }
21
+ Client.prototype.createSchema = function (group) {
22
+ var _this = this;
23
+ if (string_util_class_1.StringUtil.isFalsyString(group) && typeof group !== 'undefined')
24
+ return;
25
+ nest_util_class_1.NestUtil.getControllerClasses(this.options.Module).forEach(function (Class) {
22
26
  if (string_util_class_1.StringUtil.isFalsyString(Class === null || Class === void 0 ? void 0 : Class.name) || !(0, controller_util_class_1.isHerbalController)(Class))
23
27
  return;
24
28
  var controllerName = (0, controller_util_class_1.getControllerName)(Class);
25
29
  var pool = method_decorator_1.Method.getPool(Class.prototype);
26
30
  if (string_util_class_1.StringUtil.isFalsyString(controllerName) || pool === null)
27
31
  return;
28
- Object.entries(pool.getOpenAPIPathsObject()).forEach(function (_a) {
32
+ Object.entries(pool.getOpenAPIPathsObject(group)).forEach(function (_a) {
29
33
  var pathname = _a[0], schemas = _a[1];
30
34
  _this.document.paths[["/".concat(controllerName), pathname].join('')] = schemas;
31
35
  });
32
36
  });
33
- }
37
+ };
34
38
  return Client;
35
39
  }());
36
40
  exports.Client = Client;
@@ -146,7 +146,7 @@ command
146
146
  }))
147
147
  .addCommand((0, forge_1.createCommand)('generate-client', function (_a) {
148
148
  var addOption = _a.addOption;
149
- addOption('--group', 'Client group name to generate');
149
+ addOption('--group <name>', 'Client group name to generate');
150
150
  return {
151
151
  onLog: log,
152
152
  hiddenOptions: [
@@ -172,11 +172,12 @@ command
172
172
  return [
173
173
  "const entry = require('".concat(buildEntryFilePath, "')"),
174
174
  "const { isClient } = require(\'@open-norantec/herbal\')",
175
- 'module.exports = () => {',
175
+ 'module.exports = (context) => {',
176
176
  ' let client = entry;',
177
177
  ' if (!isClient(client)) { client = entry?.default; }',
178
178
  " if (!isClient(client)) return '';",
179
- " try { return client.instance.generateClientSourceFile() ?? ''; } catch (error) { throw error; }",
179
+ ' client.instance.createSchema(context?.group);',
180
+ " return client.instance.generateClientSourceFile() ?? '';",
180
181
  '};',
181
182
  ].join('\n');
182
183
  },
@@ -16,7 +16,6 @@ declare namespace OpenApiToTypescript {
16
16
  }
17
17
  export declare function convertOpenApiToTypescript(schema: SchemaObject, name?: string, options?: OpenApiToTypescript.Options): OpenApiToTypescript.TypeResult;
18
18
  export declare class TypeScriptClient extends Client implements Client {
19
- readonly options: CreateClientOptions;
20
19
  constructor(options: CreateClientOptions);
21
20
  generateClientSourceFile(): string;
22
21
  }
@@ -227,11 +227,10 @@ exports.convertOpenApiToTypescript = convertOpenApiToTypescript;
227
227
  var TypeScriptClient = (function (_super) {
228
228
  __extends(TypeScriptClient, _super);
229
229
  function TypeScriptClient(options) {
230
- var _this = _super.call(this, options) || this;
231
- _this.options = options;
232
- return _this;
230
+ return _super.call(this, options) || this;
233
231
  }
234
232
  TypeScriptClient.prototype.generateClientSourceFile = function () {
233
+ var _this = this;
235
234
  var options = this.options;
236
235
  if (!(options === null || options === void 0 ? void 0 : options.Module))
237
236
  throw new Error("Parameter 'Module' must be specified");
@@ -246,13 +245,13 @@ var TypeScriptClient = (function (_super) {
246
245
  var RESULT_TYPE_ANNOTATION = "".concat(RESULT_TYPE_NAME, "<").concat(METHOD_TYPE_MAP_NAME, "[T]['response']>");
247
246
  var methodTypeMapCodeLines = nest_util_class_1.NestUtil.getControllerClasses(options.Module)
248
247
  .reduce(function (result, Class) {
248
+ var _a;
249
249
  if (string_util_class_1.StringUtil.isFalsyString(Class === null || Class === void 0 ? void 0 : Class.name) || !(0, controller_util_class_1.isHerbalController)(Class))
250
250
  return result;
251
- var controllerName = (0, controller_util_class_1.getControllerName)(Class);
252
251
  var pool = method_decorator_1.Method.getPool(Class.prototype);
253
- if (string_util_class_1.StringUtil.isFalsyString(controllerName) || pool === null)
252
+ if (pool === null)
254
253
  return result;
255
- return result.concat(Object.entries(pool.getOpenAPIPathsObject())
254
+ return result.concat(Object.entries((_a = _this.document.paths) !== null && _a !== void 0 ? _a : {})
256
255
  .map(function (_a) {
257
256
  var _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
258
257
  var pathname = _a[0], schema = _a[1];
@@ -261,7 +260,7 @@ var TypeScriptClient = (function (_super) {
261
260
  if (!requestSchema && !responseSchema)
262
261
  return null;
263
262
  return [
264
- "'/".concat(controllerName).concat(pathname, "': {"),
263
+ "'".concat(pathname, "': {"),
265
264
  " request: ".concat(((_l = convertOpenApiToTypescript(requestSchema)) === null || _l === void 0 ? void 0 : _l.code) || 'any', ";"),
266
265
  " response: ".concat(((_m = convertOpenApiToTypescript(responseSchema)) === null || _m === void 0 ? void 0 : _m.code) || 'any', ";"),
267
266
  ' };',
@@ -269,7 +268,7 @@ var TypeScriptClient = (function (_super) {
269
268
  })
270
269
  .filter(function (value) { return value !== null; }));
271
270
  }, [])
272
- .map(function (line) { return " ".concat(line); });
271
+ .map(function (line) { return " ".concat(line); });
273
272
  methodTypeMapCodeLines.unshift("export interface ".concat(METHOD_TYPE_MAP_NAME, " {"));
274
273
  methodTypeMapCodeLines.push('}');
275
274
  return [
@@ -1,17 +1,18 @@
1
1
  import 'reflect-metadata';
2
2
  import { z } from 'zod';
3
3
  import { AuthAdapter } from '../abstracts';
4
- import { GroupsFactory } from './client-groups.decorator';
5
4
  import { HeaderUtil } from '@open-norantec/utilities/dist/header-util.class';
6
5
  import { Constructor } from 'type-fest';
7
6
  import { Type } from '@nestjs/common';
8
7
  import { RequestContext } from '../types';
9
8
  import { PathsObject } from 'zod-openapi/dist/openapi3-ts/dist/model/openapi31';
9
+ type ClientGroups = Array<string> | null | undefined;
10
+ type ClienttGroupsFactory = (defaultGroupName: string) => ClientGroups;
10
11
  export interface MethodOptions<IS extends z.Schema<any>, OS extends z.Schema<any>> {
11
12
  inputSchema: IS;
12
13
  outputSchema: OS;
13
14
  authAdapters?: AuthAdapter[];
14
- clientGroups?: GroupsFactory | string[];
15
+ clientGroups?: ClientGroups | ClienttGroupsFactory;
15
16
  disableTransaction?: boolean;
16
17
  }
17
18
  export type DependencyGetter = <T>(dependency: Constructor<T>) => T;
@@ -34,7 +35,7 @@ declare class MethodPool {
34
35
  protected readonly methods: Map<string, MethodConfig<any, any>>;
35
36
  registerMethod<IS extends z.Schema<any>, OS extends z.Schema<any>>(name: string, options: MethodOptions<IS, OS>, callback: MethodCallback<IS, OS>): void;
36
37
  getCallFn(name: string): any;
37
- getOpenAPIPathsObject(): PathsObject;
38
+ getOpenAPIPathsObject(group?: string): PathsObject;
38
39
  }
39
40
  export declare function Method<IS extends z.Schema<any>, OS extends z.Schema<any>>(...parameters: Parameters<typeof MethodPool.prototype.registerMethod<IS, OS>>): ClassDecorator;
40
41
  export declare namespace Method {
@@ -121,10 +121,18 @@ var MethodPool = (function () {
121
121
  var callFn = (_a = this.methods.get(name)) === null || _a === void 0 ? void 0 : _a.call;
122
122
  return typeof callFn === 'function' ? callFn.bind(this) : null;
123
123
  };
124
- MethodPool.prototype.getOpenAPIPathsObject = function () {
124
+ MethodPool.prototype.getOpenAPIPathsObject = function (group) {
125
125
  var result = {};
126
126
  Array.from(this.methods.entries()).forEach(function (_a) {
127
+ var _b;
127
128
  var name = _a[0], config = _a[1];
129
+ var defaultGroupName = "".concat(Date.now(), "_").concat(Math.random().toString(16).slice(2));
130
+ var currentGroupName = utilities_1.StringUtil.isFalsyString(group) ? defaultGroupName : group;
131
+ var clientGroups = typeof config.options.clientGroups === 'function'
132
+ ? config.options.clientGroups(defaultGroupName)
133
+ : (_b = config === null || config === void 0 ? void 0 : config.options) === null || _b === void 0 ? void 0 : _b.clientGroups;
134
+ if (Array.isArray(clientGroups) && !clientGroups.includes(currentGroupName))
135
+ return;
128
136
  result["/".concat(name)] = {
129
137
  post: {
130
138
  requestBody: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-norantec/herbal",
3
- "version": "1.0.2-alpha.19",
3
+ "version": "1.0.2-alpha.20",
4
4
  "description": "Herbal is a builder and toolchain for Nest.js applications",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {