@graphql-codegen/typescript-enum-array 2.2.1 → 2.4.0-alpha-20230130170038-34cf72572

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/cjs/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.plugin = void 0;
4
+ const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
4
5
  function getEnumTypeMap(schema) {
5
6
  var _a;
6
7
  const typeMap = schema.getTypeMap();
@@ -12,16 +13,34 @@ function getEnumTypeMap(schema) {
12
13
  }
13
14
  return result;
14
15
  }
15
- function buildArrayDefinition(e) {
16
+ function buildArrayDefinition(e, config) {
17
+ var _a;
18
+ const convert = (0, visitor_plugin_common_1.convertFactory)(config);
19
+ const enumName = convert(e.astNode, {
20
+ prefix: ((_a = config.enumPrefix) !== null && _a !== void 0 ? _a : true) ? config.typesPrefix : undefined,
21
+ suffix: config.typesSuffix,
22
+ });
16
23
  const upperName = e.name
17
24
  .replace(/[A-Z]/g, letter => `_${letter}`)
18
25
  .slice(1)
19
26
  .toUpperCase();
20
27
  const values = e
21
28
  .getValues()
22
- .map(v => `'${v.value}'`)
29
+ .map(v => {
30
+ if (config.useMembers) {
31
+ return `${enumName}.${convert(v.astNode, { transformUnderscore: true })}`;
32
+ }
33
+ else {
34
+ return `'${v.value}'`;
35
+ }
36
+ })
23
37
  .join(', ');
24
- return `export const ${upperName}: ${e.name}[] = [${values}];`;
38
+ if (config.constArrays) {
39
+ return `export const ${upperName} = [${values}] as const;`;
40
+ }
41
+ else {
42
+ return `export const ${upperName}: ${enumName}[] = [${values}];`;
43
+ }
25
44
  }
26
45
  function buildImportStatement(enums, importFrom) {
27
46
  const names = Object.values(enums).map(e => e.name);
@@ -29,7 +48,7 @@ function buildImportStatement(enums, importFrom) {
29
48
  }
30
49
  const plugin = (schema, _documents, config) => {
31
50
  const enums = getEnumTypeMap(schema);
32
- const content = enums.map(buildArrayDefinition).join('\n');
51
+ const content = enums.map(e => buildArrayDefinition(e, config)).join('\n');
33
52
  const result = { content };
34
53
  if (config.importFrom) {
35
54
  result['prepend'] = buildImportStatement(enums, config.importFrom);
package/esm/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { convertFactory } from '@graphql-codegen/visitor-plugin-common';
1
2
  function getEnumTypeMap(schema) {
2
3
  var _a;
3
4
  const typeMap = schema.getTypeMap();
@@ -9,16 +10,34 @@ function getEnumTypeMap(schema) {
9
10
  }
10
11
  return result;
11
12
  }
12
- function buildArrayDefinition(e) {
13
+ function buildArrayDefinition(e, config) {
14
+ var _a;
15
+ const convert = convertFactory(config);
16
+ const enumName = convert(e.astNode, {
17
+ prefix: ((_a = config.enumPrefix) !== null && _a !== void 0 ? _a : true) ? config.typesPrefix : undefined,
18
+ suffix: config.typesSuffix,
19
+ });
13
20
  const upperName = e.name
14
21
  .replace(/[A-Z]/g, letter => `_${letter}`)
15
22
  .slice(1)
16
23
  .toUpperCase();
17
24
  const values = e
18
25
  .getValues()
19
- .map(v => `'${v.value}'`)
26
+ .map(v => {
27
+ if (config.useMembers) {
28
+ return `${enumName}.${convert(v.astNode, { transformUnderscore: true })}`;
29
+ }
30
+ else {
31
+ return `'${v.value}'`;
32
+ }
33
+ })
20
34
  .join(', ');
21
- return `export const ${upperName}: ${e.name}[] = [${values}];`;
35
+ if (config.constArrays) {
36
+ return `export const ${upperName} = [${values}] as const;`;
37
+ }
38
+ else {
39
+ return `export const ${upperName}: ${enumName}[] = [${values}];`;
40
+ }
22
41
  }
23
42
  function buildImportStatement(enums, importFrom) {
24
43
  const names = Object.values(enums).map(e => e.name);
@@ -26,7 +45,7 @@ function buildImportStatement(enums, importFrom) {
26
45
  }
27
46
  export const plugin = (schema, _documents, config) => {
28
47
  const enums = getEnumTypeMap(schema);
29
- const content = enums.map(buildArrayDefinition).join('\n');
48
+ const content = enums.map(e => buildArrayDefinition(e, config)).join('\n');
30
49
  const result = { content };
31
50
  if (config.importFrom) {
32
51
  result['prepend'] = buildImportStatement(enums, config.importFrom);
package/package.json CHANGED
@@ -1,17 +1,18 @@
1
1
  {
2
2
  "name": "@graphql-codegen/typescript-enum-array",
3
- "version": "2.2.1",
3
+ "version": "2.4.0-alpha-20230130170038-34cf72572",
4
4
  "description": "GraphQL Code Generator plugin for adding const array",
5
5
  "peerDependencies": {
6
6
  "graphql": "^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0"
7
7
  },
8
8
  "dependencies": {
9
- "@graphql-codegen/plugin-helpers": "^2.6.2",
9
+ "@graphql-codegen/plugin-helpers": "^3.0.0",
10
+ "@graphql-codegen/visitor-plugin-common": "2.13.7",
10
11
  "tslib": "~2.4.0"
11
12
  },
12
13
  "repository": {
13
14
  "type": "git",
14
- "url": "https://github.com/dotansimha/graphql-code-generator.git",
15
+ "url": "https://github.com/dotansimha/graphql-code-generator-community.git",
15
16
  "directory": "packages/plugins/typescript/enum-array"
16
17
  },
17
18
  "license": "MIT",
@@ -1,7 +1,16 @@
1
- export interface EnumArrayPluginConfig {
1
+ import { RawTypesConfig } from '@graphql-codegen/visitor-plugin-common';
2
+ export interface EnumArrayPluginConfig extends RawTypesConfig {
2
3
  /**
3
4
  * @description import enum types from generated type path
4
5
  * if not given, omit import statement.
5
6
  */
6
7
  importFrom?: string;
8
+ /**
9
+ * @description generate the arrays as const. Defaults to false
10
+ */
11
+ constArrays?: boolean;
12
+ /**
13
+ * @description use enum members instead of string literals. Defaults to false
14
+ */
15
+ useMembers?: boolean;
7
16
  }
@@ -1,7 +1,16 @@
1
- export interface EnumArrayPluginConfig {
1
+ import { RawTypesConfig } from '@graphql-codegen/visitor-plugin-common';
2
+ export interface EnumArrayPluginConfig extends RawTypesConfig {
2
3
  /**
3
4
  * @description import enum types from generated type path
4
5
  * if not given, omit import statement.
5
6
  */
6
7
  importFrom?: string;
8
+ /**
9
+ * @description generate the arrays as const. Defaults to false
10
+ */
11
+ constArrays?: boolean;
12
+ /**
13
+ * @description use enum members instead of string literals. Defaults to false
14
+ */
15
+ useMembers?: boolean;
7
16
  }