@graphql-codegen/typescript-enum-array 2.3.0 → 2.4.0-alpha-20230130170123-43f773c64
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 +19 -5
- package/esm/index.js +19 -5
- package/package.json +2 -1
- package/typings/config.d.cts +6 -1
- package/typings/config.d.ts +6 -1
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,20 +13,33 @@ 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 =>
|
|
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
|
-
if (constArrays) {
|
|
38
|
+
if (config.constArrays) {
|
|
25
39
|
return `export const ${upperName} = [${values}] as const;`;
|
|
26
40
|
}
|
|
27
41
|
else {
|
|
28
|
-
return `export const ${upperName}: ${
|
|
42
|
+
return `export const ${upperName}: ${enumName}[] = [${values}];`;
|
|
29
43
|
}
|
|
30
44
|
}
|
|
31
45
|
function buildImportStatement(enums, importFrom) {
|
|
@@ -34,7 +48,7 @@ function buildImportStatement(enums, importFrom) {
|
|
|
34
48
|
}
|
|
35
49
|
const plugin = (schema, _documents, config) => {
|
|
36
50
|
const enums = getEnumTypeMap(schema);
|
|
37
|
-
const content = enums.map(e =>
|
|
51
|
+
const content = enums.map(e => buildArrayDefinition(e, config)).join('\n');
|
|
38
52
|
const result = { content };
|
|
39
53
|
if (config.importFrom) {
|
|
40
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,20 +10,33 @@ 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 =>
|
|
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
|
-
if (constArrays) {
|
|
35
|
+
if (config.constArrays) {
|
|
22
36
|
return `export const ${upperName} = [${values}] as const;`;
|
|
23
37
|
}
|
|
24
38
|
else {
|
|
25
|
-
return `export const ${upperName}: ${
|
|
39
|
+
return `export const ${upperName}: ${enumName}[] = [${values}];`;
|
|
26
40
|
}
|
|
27
41
|
}
|
|
28
42
|
function buildImportStatement(enums, importFrom) {
|
|
@@ -31,7 +45,7 @@ function buildImportStatement(enums, importFrom) {
|
|
|
31
45
|
}
|
|
32
46
|
export const plugin = (schema, _documents, config) => {
|
|
33
47
|
const enums = getEnumTypeMap(schema);
|
|
34
|
-
const content = enums.map(e =>
|
|
48
|
+
const content = enums.map(e => buildArrayDefinition(e, config)).join('\n');
|
|
35
49
|
const result = { content };
|
|
36
50
|
if (config.importFrom) {
|
|
37
51
|
result['prepend'] = buildImportStatement(enums, config.importFrom);
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-codegen/typescript-enum-array",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.0-alpha-20230130170123-43f773c64",
|
|
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
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": {
|
package/typings/config.d.cts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
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.
|
|
@@ -8,4 +9,8 @@ export interface EnumArrayPluginConfig {
|
|
|
8
9
|
* @description generate the arrays as const. Defaults to false
|
|
9
10
|
*/
|
|
10
11
|
constArrays?: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* @description use enum members instead of string literals. Defaults to false
|
|
14
|
+
*/
|
|
15
|
+
useMembers?: boolean;
|
|
11
16
|
}
|
package/typings/config.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
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.
|
|
@@ -8,4 +9,8 @@ export interface EnumArrayPluginConfig {
|
|
|
8
9
|
* @description generate the arrays as const. Defaults to false
|
|
9
10
|
*/
|
|
10
11
|
constArrays?: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* @description use enum members instead of string literals. Defaults to false
|
|
14
|
+
*/
|
|
15
|
+
useMembers?: boolean;
|
|
11
16
|
}
|