@graphql-codegen/flutter-freezed 2.11.2 → 3.0.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.
Files changed (53) hide show
  1. package/cjs/config/config-value.js +132 -0
  2. package/cjs/config/pattern.js +731 -0
  3. package/cjs/config/plugin-config.js +140 -0
  4. package/cjs/freezed-declaration-blocks/class-block.js +87 -224
  5. package/cjs/freezed-declaration-blocks/enum-block.js +54 -0
  6. package/cjs/freezed-declaration-blocks/factory-block.js +81 -113
  7. package/cjs/freezed-declaration-blocks/index.js +91 -4
  8. package/cjs/freezed-declaration-blocks/node-repository.js +26 -0
  9. package/cjs/freezed-declaration-blocks/parameter-block.js +55 -143
  10. package/cjs/index.js +10 -14
  11. package/cjs/schema-visitor.js +8 -7
  12. package/cjs/utils.js +64 -219
  13. package/esm/config/config-value.js +128 -0
  14. package/esm/config/pattern.js +723 -0
  15. package/esm/config/plugin-config.js +137 -0
  16. package/esm/freezed-declaration-blocks/class-block.js +86 -223
  17. package/esm/freezed-declaration-blocks/enum-block.js +50 -0
  18. package/esm/freezed-declaration-blocks/factory-block.js +81 -113
  19. package/esm/freezed-declaration-blocks/index.js +89 -3
  20. package/esm/freezed-declaration-blocks/node-repository.js +22 -0
  21. package/esm/freezed-declaration-blocks/parameter-block.js +53 -141
  22. package/esm/index.js +11 -15
  23. package/esm/schema-visitor.js +8 -7
  24. package/esm/utils.js +56 -212
  25. package/package.json +5 -5
  26. package/typings/config/config-value.d.cts +24 -0
  27. package/typings/config/config-value.d.ts +24 -0
  28. package/typings/config/pattern.d.cts +252 -0
  29. package/typings/config/pattern.d.ts +252 -0
  30. package/typings/config/plugin-config.d.cts +683 -0
  31. package/typings/config/plugin-config.d.ts +683 -0
  32. package/typings/freezed-declaration-blocks/class-block.d.cts +11 -35
  33. package/typings/freezed-declaration-blocks/class-block.d.ts +11 -35
  34. package/typings/freezed-declaration-blocks/enum-block.d.cts +10 -0
  35. package/typings/freezed-declaration-blocks/enum-block.d.ts +10 -0
  36. package/typings/freezed-declaration-blocks/factory-block.d.cts +16 -37
  37. package/typings/freezed-declaration-blocks/factory-block.d.ts +16 -37
  38. package/typings/freezed-declaration-blocks/index.d.cts +27 -3
  39. package/typings/freezed-declaration-blocks/index.d.ts +27 -3
  40. package/typings/freezed-declaration-blocks/node-repository.d.cts +10 -0
  41. package/typings/freezed-declaration-blocks/node-repository.d.ts +10 -0
  42. package/typings/freezed-declaration-blocks/parameter-block.d.cts +11 -44
  43. package/typings/freezed-declaration-blocks/parameter-block.d.ts +11 -44
  44. package/typings/index.d.cts +1 -1
  45. package/typings/index.d.ts +1 -1
  46. package/typings/schema-visitor.d.cts +7 -7
  47. package/typings/schema-visitor.d.ts +7 -7
  48. package/typings/utils.d.cts +33 -72
  49. package/typings/utils.d.ts +33 -72
  50. package/cjs/config.js +0 -2
  51. package/esm/config.js +0 -1
  52. package/typings/config.d.cts +0 -479
  53. package/typings/config.d.ts +0 -479
@@ -0,0 +1,140 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defaultFreezedPluginConfig = exports.DART_KEYWORDS = exports.DART_SCALARS = exports.APPLIES_ON_PARAMETERS = exports.APPLIES_ON_MERGED_FACTORY_PARAMETERS = exports.APPLIES_ON_UNION_FACTORY_PARAMETERS = exports.APPLIES_ON_DEFAULT_FACTORY_PARAMETERS = exports.APPLIES_ON_FACTORY = exports.APPLIES_ON_MERGED_FACTORY = exports.APPLIES_ON_UNION_FACTORY = exports.APPLIES_ON_DEFAULT_FACTORY = exports.APPLIES_ON_CLASS = exports.APPLIES_ON_ENUM_VALUE = exports.APPLIES_ON_ENUM = void 0;
4
+ exports.APPLIES_ON_ENUM = ['enum'];
5
+ exports.APPLIES_ON_ENUM_VALUE = ['enum_value'];
6
+ exports.APPLIES_ON_CLASS = ['class'];
7
+ exports.APPLIES_ON_DEFAULT_FACTORY = ['factory', 'default_factory'];
8
+ exports.APPLIES_ON_UNION_FACTORY = ['factory', 'named_factory', 'union_factory'];
9
+ exports.APPLIES_ON_MERGED_FACTORY = ['factory', 'named_factory', 'merged_factory'];
10
+ exports.APPLIES_ON_FACTORY = ['factory', 'default_factory', 'named_factory', 'merged_factory', 'union_factory'];
11
+ exports.APPLIES_ON_DEFAULT_FACTORY_PARAMETERS = ['parameter', 'default_factory_parameter'];
12
+ exports.APPLIES_ON_UNION_FACTORY_PARAMETERS = [
13
+ 'parameter',
14
+ 'named_factory_parameter',
15
+ 'union_factory_parameter',
16
+ ];
17
+ exports.APPLIES_ON_MERGED_FACTORY_PARAMETERS = [
18
+ 'parameter',
19
+ 'named_factory_parameter',
20
+ 'merged_factory_parameter',
21
+ ];
22
+ exports.APPLIES_ON_PARAMETERS = [
23
+ 'parameter',
24
+ 'default_factory_parameter',
25
+ 'named_factory_parameter',
26
+ 'union_factory_parameter',
27
+ 'merged_factory_parameter',
28
+ ];
29
+ /**
30
+ * maps GraphQL scalar types to Dart's scalar types
31
+ */
32
+ exports.DART_SCALARS = {
33
+ ID: 'String',
34
+ String: 'String',
35
+ Boolean: 'bool',
36
+ Int: 'int',
37
+ Float: 'double',
38
+ DateTime: 'DateTime',
39
+ };
40
+ exports.DART_KEYWORDS = {
41
+ abstract: 'built-in',
42
+ else: 'reserved',
43
+ import: 'built-in',
44
+ show: 'context',
45
+ as: 'built-in',
46
+ enum: 'reserved',
47
+ in: 'reserved',
48
+ static: 'built-in',
49
+ assert: 'reserved',
50
+ export: 'built-in',
51
+ interface: 'built-in',
52
+ super: 'reserved',
53
+ async: 'context',
54
+ extends: 'reserved',
55
+ is: 'reserved',
56
+ switch: 'reserved',
57
+ await: 'async-reserved',
58
+ extension: 'built-in',
59
+ late: 'built-in',
60
+ sync: 'context',
61
+ break: 'reserved',
62
+ external: 'built-in',
63
+ library: 'built-in',
64
+ this: 'reserved',
65
+ case: 'reserved',
66
+ factory: 'built-in',
67
+ mixin: 'built-in',
68
+ throw: 'reserved',
69
+ catch: 'reserved',
70
+ false: 'reserved',
71
+ new: 'reserved',
72
+ true: 'reserved',
73
+ class: 'reserved',
74
+ final: 'reserved',
75
+ null: 'reserved',
76
+ try: 'reserved',
77
+ const: 'reserved',
78
+ finally: 'reserved',
79
+ on: 'context',
80
+ typedef: 'built-in',
81
+ continue: 'reserved',
82
+ for: 'reserved',
83
+ operator: 'built-in',
84
+ var: 'reserved',
85
+ covariant: 'built-in',
86
+ Function: 'built-in',
87
+ part: 'built-in',
88
+ void: 'reserved',
89
+ default: 'reserved',
90
+ get: 'built-in',
91
+ required: 'built-in',
92
+ while: 'reserved',
93
+ deferred: 'built-in',
94
+ hide: 'context',
95
+ rethrow: 'reserved',
96
+ with: 'reserved',
97
+ do: 'reserved',
98
+ if: 'reserved',
99
+ return: 'reserved',
100
+ yield: 'async-reserved',
101
+ dynamic: 'built-in',
102
+ implements: 'built-in',
103
+ set: 'built-in',
104
+ // built-in types
105
+ int: 'reserved',
106
+ double: 'reserved',
107
+ String: 'reserved',
108
+ bool: 'reserved',
109
+ List: 'reserved',
110
+ Set: 'reserved',
111
+ Map: 'reserved',
112
+ Runes: 'reserved',
113
+ Symbol: 'reserved',
114
+ Object: 'reserved',
115
+ Null: 'reserved',
116
+ Never: 'reserved',
117
+ Enum: 'reserved',
118
+ Future: 'reserved',
119
+ Iterable: 'reserved',
120
+ };
121
+ /** initializes a FreezedPluginConfig with the defaults values */
122
+ exports.defaultFreezedPluginConfig = {
123
+ camelCasedEnums: true,
124
+ copyWith: undefined,
125
+ customScalars: {},
126
+ defaultValues: undefined,
127
+ deprecated: undefined,
128
+ equal: undefined,
129
+ escapeDartKeywords: true,
130
+ final: undefined,
131
+ // fromJsonToJson: true, // TODO: @next-version
132
+ ignoreTypes: undefined,
133
+ immutable: true,
134
+ makeCollectionsUnmodifiable: undefined,
135
+ mergeTypes: undefined,
136
+ mutableInputs: true,
137
+ privateEmptyConstructor: true,
138
+ unionClass: undefined,
139
+ };
140
+ //#endregion
@@ -1,232 +1,95 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.FreezedDeclarationBlock = void 0;
3
+ exports.ClassBlock = void 0;
4
4
  const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
5
5
  const graphql_1 = require("graphql");
6
- const change_case_all_1 = require("change-case-all");
7
- const utils_js_1 = require("../utils.js");
6
+ const config_value_js_1 = require("../config/config-value.js");
7
+ const plugin_config_js_1 = require("../config/plugin-config.js");
8
8
  const factory_block_js_1 = require("./factory-block.js");
9
- class FreezedDeclarationBlock {
10
- constructor(_config, _freezedFactoryBlockRepository, _node) {
11
- this._config = _config;
12
- this._freezedFactoryBlockRepository = _freezedFactoryBlockRepository;
13
- this._node = _node;
14
- /** document the class */
15
- this._comment = '';
16
- /** a list of decorators to copy paste to the generator */
17
- this._decorators = [];
18
- /** a list of default constructor and named Constructors used create a Freezed union/sealed class */
19
- this._factoryBlocks = [];
20
- this._config = _config;
21
- this._freezedFactoryBlockRepository = _freezedFactoryBlockRepository;
22
- this._node = _node;
23
- this._freezedConfigValue = new utils_js_1.FreezedConfigValue(this._config, this._node.name.value);
24
- }
25
- init() {
26
- if (this._node.kind === graphql_1.Kind.ENUM_TYPE_DEFINITION) {
27
- this.setDecorators().setName().setShape().setBlock();
28
- return this;
29
- }
30
- this.setComment().setDecorators().setName().setFactoryBlocks().setShape().setBlock();
31
- return this;
32
- }
33
- setComment() {
34
- var _a;
35
- const comment = (_a = this._node.description) === null || _a === void 0 ? void 0 : _a.value;
36
- if (comment && comment !== null && comment !== '') {
37
- this._comment = `/// ${comment} \n`;
38
- }
39
- return this;
40
- }
41
- getEnumComment(value) {
42
- var _a;
43
- const comment = (_a = value.description) === null || _a === void 0 ? void 0 : _a.value;
44
- if (comment && comment !== null && comment !== '') {
45
- return `/// ${comment} \n`;
46
- }
47
- return '';
48
- }
49
- setDecorators() {
50
- var _a, _b;
51
- const name = this._node.name.value;
52
- // determine if should mark as deprecated
53
- const isDeprecated = (_b = (_a = this._config.typeSpecificFreezedConfig) === null || _a === void 0 ? void 0 : _a[name]) === null || _b === void 0 ? void 0 : _b.deprecated;
54
- this._decorators =
55
- this._node.kind === graphql_1.Kind.ENUM_TYPE_DEFINITION
56
- ? [...(0, utils_js_1.transformCustomDecorators)((0, utils_js_1.getCustomDecorators)(this._config, ['enum'], name), this._node)]
57
- : [
58
- this.getFreezedDecorator(),
59
- ...(0, utils_js_1.transformCustomDecorators)((0, utils_js_1.getCustomDecorators)(this._config, ['class'], name), this._node),
60
- ];
61
- // @deprecated
62
- // if this._decorators doesn't include an @deprecated decorator but the field is marked as @deprecated...
63
- if (!this._decorators.includes('@deprecated') && isDeprecated) {
64
- this._decorators = [...this._decorators, '@deprecated\n'];
65
- }
66
- return this;
67
- }
68
- getFreezedDecorator() {
69
- const use_unfreezed = () => {
70
- if (!this._freezedConfigValue.get('immutable') ||
71
- (this._node.kind === graphql_1.Kind.INPUT_OBJECT_TYPE_DEFINITION && this._freezedConfigValue.get('mutableInputs'))) {
72
- return '@unfreezed\n';
73
- }
74
- return use_Freezed_or_freezed();
75
- };
76
- const use_Freezed_or_freezed = () => {
77
- if (isCustomizedFreezed()) {
78
- const copyWith = this._freezedConfigValue.get('copyWith');
79
- const equal = this._freezedConfigValue.get('equal');
80
- const makeCollectionsUnmodifiable = this._freezedConfigValue.get('makeCollectionsUnmodifiable');
81
- const unionKey = this._freezedConfigValue.get('unionKey');
82
- const unionValueCase = this._freezedConfigValue.get('unionValueCase');
83
- let atFreezed = '@Freezed(\n';
84
- if (copyWith !== undefined) {
85
- atFreezed += (0, visitor_plugin_common_1.indent)(`copyWith: ${copyWith},\n`);
86
- }
87
- if (equal !== undefined) {
88
- atFreezed += (0, visitor_plugin_common_1.indent)(`equal: ${equal},\n`);
89
- }
90
- if (makeCollectionsUnmodifiable !== undefined) {
91
- atFreezed += (0, visitor_plugin_common_1.indent)(`makeCollectionsUnmodifiable: ${makeCollectionsUnmodifiable},\n`);
92
- }
93
- if (unionKey !== undefined) {
94
- atFreezed += (0, visitor_plugin_common_1.indent)(`unionKey: ${unionKey},\n`);
95
- }
96
- if (unionValueCase !== undefined) {
97
- atFreezed += (0, visitor_plugin_common_1.indent)(`unionValueCase: '${unionValueCase}',\n`);
98
- }
99
- atFreezed += ')\n';
100
- return atFreezed;
101
- }
102
- // else fallback to the normal @freezed decorator
103
- return '@freezed\n';
104
- };
105
- const isCustomizedFreezed = () => {
106
- return (this._freezedConfigValue.get('copyWith') !== undefined ||
107
- this._freezedConfigValue.get('equal') !== undefined ||
108
- this._freezedConfigValue.get('makeCollectionsUnmodifiable') !== undefined ||
109
- this._freezedConfigValue.get('unionKey') !== undefined ||
110
- this._freezedConfigValue.get('unionValueCase') !==
111
- undefined);
112
- };
113
- // this is the start of the pipeline of decisions to determine which Freezed decorator to use
114
- return use_unfreezed();
115
- }
116
- setName() {
117
- this._name = (0, change_case_all_1.pascalCase)(this._node.name.value);
118
- return this;
119
- }
120
- setFactoryBlocks() {
121
- var _a, _b, _c, _d;
122
- if (this._node.kind === graphql_1.Kind.UNION_TYPE_DEFINITION) {
123
- this._factoryBlocks =
124
- (_b = (_a = this._node.types) === null || _a === void 0 ? void 0 : _a.map((_type) => new factory_block_js_1.FreezedFactoryBlock(this._config, this._node).init())) !== null && _b !== void 0 ? _b : [];
125
- }
126
- else if (this._node.kind !== graphql_1.Kind.ENUM_TYPE_DEFINITION) {
127
- /*
128
- for `ObjectTypeDefinitionNode` and `InputObjectTypeDefinitionNode` nodes,
129
- we use the `ShapeRepository`
130
- to register the `FreezedFactoryBlock` so that we can use it later
131
- when we are merging inputs or generating freezed union/sealed classes
132
- for GraphQL union types
133
- */
134
- this._factoryBlocks =
135
- (_d = (_c = this._node.fields) === null || _c === void 0 ? void 0 : _c.map((_field) => this._freezedFactoryBlockRepository.register(this._node.name.value, new factory_block_js_1.FreezedFactoryBlock(this._config, this._node).init()))) !== null && _d !== void 0 ? _d : [];
136
- }
137
- return this;
138
- }
139
- setShape() {
140
- var _a, _b, _c;
141
- let shape = '';
142
- // some helper variables
143
- const name = this._node.name.value;
144
- let namedConstructor;
145
- let factoryBlockKey;
146
- // handle enums differently
147
- if (this._node.kind === graphql_1.Kind.ENUM_TYPE_DEFINITION) {
148
- this._shape = (_a = this._node.values) === null || _a === void 0 ? void 0 : _a.map((value) => {
149
- var _a;
150
- shape = (0, visitor_plugin_common_1.indent)(this.getEnumComment(value));
151
- if ((_a = this._config.camelCasedEnums) !== null && _a !== void 0 ? _a : true) {
152
- shape += `@JsonKey(name: ${value.name.value}) ${value.name.value.toLowerCase()}`;
153
- }
154
- else {
155
- shape += value.name.value;
156
- }
157
- return `${shape}\n`;
158
- }).join('');
159
- return this;
160
- }
161
- // append private empty constructor
162
- if (this._freezedConfigValue.get('privateEmptyConstructor')) {
163
- shape += (0, visitor_plugin_common_1.indent)(`const ${this._name}._();\n\n`);
164
- }
165
- // decide whether to append an empty Union constructor
166
- if (this._freezedConfigValue.get('defaultUnionConstructor') && this._node.kind === graphql_1.Kind.UNION_TYPE_DEFINITION) {
167
- shape += (0, visitor_plugin_common_1.indent)(`const factory ${this._name}() = _${this._name};\n\n`);
168
- }
169
- // append tokens which will be used to retrieve the factory blocks
170
- // from the FreezedFactoryBlockRepository
171
- if (this._node.kind === graphql_1.Kind.UNION_TYPE_DEFINITION) {
172
- (_c = (_b = this._node) === null || _b === void 0 ? void 0 : _b.types) === null || _c === void 0 ? void 0 : _c.forEach(type => {
173
- namedConstructor = type.name.value;
174
- factoryBlockKey = namedConstructor;
175
- shape += `==>factory==>${factoryBlockKey}==>${'union_factory'}==>${name}==>${namedConstructor}\n`;
176
- });
177
- }
178
- else {
179
- factoryBlockKey = name;
180
- // replace token for the ObjectType & InputType to be replaced with the default Freezed constructor
181
- shape += `==>factory==>${factoryBlockKey}==>${'class_factory'}==>${name}\n`;
182
- const mergeInputs = this._freezedConfigValue.get('mergeInputs');
183
- if (this._node.kind === graphql_1.Kind.OBJECT_TYPE_DEFINITION && mergeInputs) {
184
- // replace token for the InputTypes(a.k.a namedConstructors) as a union/sealed class
185
- mergeInputs.forEach(input => {
186
- const separator = input.includes('$') ? '$' : input.includes(name) ? name : '*';
187
- namedConstructor = (0, change_case_all_1.camelCase)(input.split(separator).join('_'));
188
- factoryBlockKey = input.replace('$', name);
189
- shape += `==>factory==>${factoryBlockKey}==>${'merged_input_factory'}==>${name}==>${namedConstructor}\n`;
190
- });
191
- }
192
- }
193
- this._shape = shape;
194
- return this;
195
- }
196
- /**
197
- * returns the string output of the block
198
- */
199
- setBlock() {
9
+ const pattern_js_1 = require("../config/pattern.js");
10
+ const index_js_1 = require("./index.js");
11
+ const utils_js_1 = require("../utils.js");
12
+ class ClassBlock {
13
+ static build(config, node) {
14
+ const typeName = pattern_js_1.TypeName.fromString(node.name.value);
15
+ const _className = index_js_1.Block.buildBlockName(config, plugin_config_js_1.APPLIES_ON_CLASS, typeName.value, typeName, undefined, 'PascalCase');
200
16
  let block = '';
201
- //append comment
202
- block += this._comment;
203
- // append the decorators
204
- block += this._decorators.join('');
205
- // handle enums differently
206
- if (this._node.kind === graphql_1.Kind.ENUM_TYPE_DEFINITION) {
207
- block += `enum ${this._name}{\n${this._shape}}\n\n`;
208
- this._block = block;
209
- return this;
210
- }
211
- // append start of class definition
212
- block += `class ${this._name} with _$${this._name} {\n`;
213
- // append the shape
214
- block += this._shape;
215
- // append fromJson
216
- if (this._freezedConfigValue.get('fromJsonToJson')) {
217
- block += (0, visitor_plugin_common_1.indent)(`factory ${this._name}.fromJson(Map<String, Object?> json) => _${this._name}FromJson(json);\n`);
218
- }
219
- //append end of class definition
220
- block += '}\n\n';
221
- this._block = block;
222
- return this;
223
- }
224
- /** returns the block */
225
- toString() {
226
- if (!this._block) {
227
- throw new Error('setShape must be called before calling toString()');
228
- }
229
- return this._block;
17
+ // the comments should be on the factory block instead
18
+ // block += Block.buildComment(node);
19
+ block += this.buildDecorators(config, node);
20
+ block += this.buildHeader(config, typeName, _className);
21
+ block += this.buildBody(config, node);
22
+ block += this.buildFooter(config, typeName, _className);
23
+ return block;
230
24
  }
231
25
  }
232
- exports.FreezedDeclarationBlock = FreezedDeclarationBlock;
26
+ exports.ClassBlock = ClassBlock;
27
+ ClassBlock.buildDecorators = (config, node) => {
28
+ const freezedDecorator = ClassBlock.buildFreezedDecorator(config, node);
29
+ // TODO: consider implementing custom decorators
30
+ return [freezedDecorator].join('');
31
+ };
32
+ ClassBlock.buildFreezedDecorator = (config, node) => {
33
+ // this is the start of the pipeline of decisions to determine which Freezed decorator to use
34
+ return ClassBlock.decorateAsFreezed(config, node);
35
+ };
36
+ ClassBlock.decorateAsFreezed = (config, node) => {
37
+ const typeName = pattern_js_1.TypeName.fromString(node.name.value);
38
+ const copyWith = config_value_js_1.Config.copyWith(config, typeName);
39
+ const equal = config_value_js_1.Config.equal(config, typeName);
40
+ const makeCollectionsUnmodifiable = config_value_js_1.Config.makeCollectionsUnmodifiable(config, typeName);
41
+ const unionKey = config_value_js_1.Config.unionKey();
42
+ const unionValueCase = config_value_js_1.Config.unionValueCase();
43
+ const body = [
44
+ copyWith !== undefined ? `copyWith: ${copyWith},\n` : undefined,
45
+ equal !== undefined ? `equal: ${equal},\n` : undefined,
46
+ makeCollectionsUnmodifiable !== undefined
47
+ ? `makeCollectionsUnmodifiable: ${makeCollectionsUnmodifiable},\n`
48
+ : undefined,
49
+ unionKey !== undefined ? `unionKey: ${unionKey},\n` : undefined,
50
+ unionValueCase !== undefined ? `unionValueCase: '${unionValueCase}',\n` : undefined,
51
+ ]
52
+ .filter(value => value !== undefined)
53
+ .map(value => (0, visitor_plugin_common_1.indent)(value))
54
+ .join('');
55
+ return (0, utils_js_1.stringIsNotEmpty)(body) ? `@Freezed(\n${body})\n` : ClassBlock.decorateAsUnfreezed(config, node);
56
+ };
57
+ ClassBlock.decorateAsUnfreezed = (config, node) => {
58
+ const typeName = pattern_js_1.TypeName.fromString(node.name.value);
59
+ const immutable = config_value_js_1.Config.immutable(config, typeName);
60
+ const mutableInputs = config_value_js_1.Config.mutableInputs(config, typeName);
61
+ const mutable = immutable !== true || (node.kind === graphql_1.Kind.INPUT_OBJECT_TYPE_DEFINITION && mutableInputs);
62
+ return mutable ? '@unfreezed\n' : '@freezed\n';
63
+ };
64
+ ClassBlock.buildHeader = (config, typeName, _className) => {
65
+ const privateEmptyConstructor = config_value_js_1.Config.privateEmptyConstructor(config, typeName)
66
+ ? (0, visitor_plugin_common_1.indent)(`const ${_className}._();\n\n`)
67
+ : '';
68
+ return `class ${_className} with _$${_className} {\n${privateEmptyConstructor}`;
69
+ };
70
+ ClassBlock.buildBody = (config, node) => {
71
+ var _a;
72
+ const className = pattern_js_1.TypeName.fromString(node.name.value);
73
+ let body = '';
74
+ if ((0, utils_js_1.nodeIsObjectType)(node)) {
75
+ body += factory_block_js_1.FactoryBlock.serializeDefaultFactory(className);
76
+ }
77
+ else if (node.kind === graphql_1.Kind.UNION_TYPE_DEFINITION) {
78
+ body += ((_a = node.types) !== null && _a !== void 0 ? _a : [])
79
+ .map(value => {
80
+ const factoryName = pattern_js_1.TypeName.fromString(value.name.value);
81
+ return factory_block_js_1.FactoryBlock.serializeUnionFactory(className, factoryName);
82
+ })
83
+ .join('');
84
+ }
85
+ body += config_value_js_1.Config.mergeTypes(config, className)
86
+ .map(value => {
87
+ const factoryName = pattern_js_1.TypeName.fromString(value);
88
+ return factory_block_js_1.FactoryBlock.serializeMergedFactory(className, factoryName);
89
+ })
90
+ .join('');
91
+ return body;
92
+ };
93
+ ClassBlock.buildFooter = (config, typeName, _className) => {
94
+ return (0, visitor_plugin_common_1.indent)(`factory ${_className}.fromJson(Map<String, dynamic> json) => _$${_className}FromJson(json);\n}\n\n`);
95
+ };
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EnumBlock = void 0;
4
+ const pattern_js_1 = require("../config/pattern.js");
5
+ const plugin_config_js_1 = require("../config/plugin-config.js");
6
+ const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
7
+ const index_js_1 = require("./index.js");
8
+ const config_value_js_1 = require("../config/config-value.js");
9
+ const utils_js_1 = require("../utils.js");
10
+ class EnumBlock {
11
+ // TODO: @next-version: Implement enhanced enums
12
+ static build(config, node) {
13
+ const typeName = pattern_js_1.TypeName.fromString(node.name.value);
14
+ let block = '';
15
+ block += index_js_1.Block.buildComment(node);
16
+ block += this.buildDecorators();
17
+ block += this.buildHeader(config, typeName);
18
+ block += this.buildBody(config, node);
19
+ block += this.buildFooter();
20
+ return block;
21
+ }
22
+ }
23
+ exports.EnumBlock = EnumBlock;
24
+ EnumBlock.buildDecorators = () => {
25
+ // TODO: @next-version: @JsonEnum(valueField: 'code', fieldRename: 'new-name')
26
+ return '';
27
+ };
28
+ EnumBlock.buildHeader = (config, typeName) => {
29
+ const enumTypeName = index_js_1.Block.buildBlockName(config, plugin_config_js_1.APPLIES_ON_ENUM, typeName.value, typeName, undefined, 'PascalCase');
30
+ return `enum ${enumTypeName} {\n`;
31
+ };
32
+ EnumBlock.buildBody = (config, node) => {
33
+ var _a, _b;
34
+ const typeName = pattern_js_1.TypeName.fromString(node.name.value);
35
+ return (_b = ((_a = node.values) !== null && _a !== void 0 ? _a : [])) === null || _b === void 0 ? void 0 : _b.map((enumValue) => {
36
+ const fieldName = pattern_js_1.FieldName.fromString(enumValue.name.value);
37
+ const enumValueName = index_js_1.Block.buildBlockName(config, plugin_config_js_1.APPLIES_ON_ENUM_VALUE, fieldName.value, typeName, fieldName, config_value_js_1.Config.camelCasedEnums(config));
38
+ const comment = index_js_1.Block.buildComment(enumValue);
39
+ const jsonKey = (0, utils_js_1.atJsonKeyDecorator)({
40
+ name: fieldName.value !== enumValueName ? fieldName.value : undefined,
41
+ });
42
+ //TODO: @next-version: const jsonValue = @JsonValue(String|int)
43
+ const decorators = [
44
+ jsonKey,
45
+ // jsonValue
46
+ ].join('');
47
+ return [comment, decorators, `${enumValueName}\n`]
48
+ .map(block => ((0, utils_js_1.stringIsNotEmpty)(block) ? (0, visitor_plugin_common_1.indent)(block) : block))
49
+ .join('');
50
+ }).join('');
51
+ };
52
+ EnumBlock.buildFooter = () => {
53
+ return '}\n\n';
54
+ };