UNPKG

6.58 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.buildMapperImport = exports.transformDirectiveArgumentAndInputFieldMappings = exports.transformMappers = exports.isExternalMapper = exports.parseMapper = exports.isExternalMapperType = void 0;
4function isExternalMapperType(m) {
5 return !!m.import;
6}
7exports.isExternalMapperType = isExternalMapperType;
8var MapperKind;
9(function (MapperKind) {
10 MapperKind[MapperKind["Namespace"] = 0] = "Namespace";
11 MapperKind[MapperKind["Default"] = 1] = "Default";
12 MapperKind[MapperKind["Regular"] = 2] = "Regular";
13})(MapperKind || (MapperKind = {}));
14function prepareLegacy(mapper) {
15 const items = mapper.split('#');
16 const isNamespace = items.length === 3;
17 const isDefault = items[1].trim() === 'default' || items[1].startsWith('default ');
18 const hasAlias = items[1].includes(' as ');
19 return {
20 items,
21 isDefault,
22 isNamespace,
23 hasAlias,
24 };
25}
26function prepare(mapper) {
27 const [source, path] = mapper.split('#');
28 const isNamespace = path.includes('.');
29 const isDefault = path.trim() === 'default' || path.startsWith('default ');
30 const hasAlias = path.includes(' as ');
31 return {
32 items: isNamespace ? [source, ...path.split('.')] : [source, path],
33 isDefault,
34 isNamespace,
35 hasAlias,
36 };
37}
38function isLegacyMode(mapper) {
39 return mapper.split('#').length === 3;
40}
41function parseMapper(mapper, gqlTypeName = null, suffix) {
42 if (isExternalMapper(mapper)) {
43 const { isNamespace, isDefault, hasAlias, items } = isLegacyMode(mapper) ? prepareLegacy(mapper) : prepare(mapper);
44 const mapperKind = isNamespace
45 ? MapperKind.Namespace
46 : isDefault
47 ? MapperKind.Default
48 : MapperKind.Regular;
49 function handleAlias(isDefault = false) {
50 const [importedType, aliasType] = items[1].split(/\s+as\s+/);
51 const type = maybeSuffix(aliasType);
52 return {
53 importElement: isDefault ? type : `${importedType} as ${type}`,
54 type,
55 };
56 }
57 function maybeSuffix(type) {
58 if (suffix) {
59 return addSuffix(type, suffix);
60 }
61 return type;
62 }
63 function handle() {
64 switch (mapperKind) {
65 // ./my/module#Namespace#Identifier
66 case MapperKind.Namespace: {
67 const [, ns, identifier] = items;
68 return {
69 type: `${ns}.${identifier}`,
70 importElement: ns,
71 };
72 }
73 case MapperKind.Default: {
74 // ./my/module#default as alias
75 if (hasAlias) {
76 return handleAlias(true);
77 }
78 const type = maybeSuffix(`${gqlTypeName}`);
79 // ./my/module#default
80 return {
81 importElement: type,
82 type,
83 };
84 }
85 case MapperKind.Regular: {
86 // ./my/module#Identifier as alias
87 if (hasAlias) {
88 return handleAlias();
89 }
90 const identifier = items[1];
91 const type = maybeSuffix(identifier);
92 // ./my/module#Identifier
93 return {
94 type,
95 importElement: suffix ? `${identifier} as ${type}` : type,
96 };
97 }
98 }
99 }
100 const { type, importElement } = handle();
101 return {
102 default: isDefault,
103 isExternal: true,
104 source: items[0],
105 type,
106 import: importElement.replace(/<(.*?)>/g, ''),
107 };
108 }
109 return {
110 isExternal: false,
111 type: mapper,
112 };
113}
114exports.parseMapper = parseMapper;
115function addSuffix(element, suffix) {
116 const generic = element.indexOf('<');
117 if (generic === -1) {
118 return `${element}${suffix}`;
119 }
120 return `${element.slice(0, generic)}${suffix}${element.slice(generic)}`;
121}
122function isExternalMapper(value) {
123 return value.includes('#');
124}
125exports.isExternalMapper = isExternalMapper;
126function transformMappers(rawMappers, mapperTypeSuffix) {
127 const result = {};
128 Object.keys(rawMappers).forEach(gqlTypeName => {
129 const mapperDef = rawMappers[gqlTypeName];
130 const parsedMapper = parseMapper(mapperDef, gqlTypeName, mapperTypeSuffix);
131 result[gqlTypeName] = parsedMapper;
132 });
133 return result;
134}
135exports.transformMappers = transformMappers;
136function transformDirectiveArgumentAndInputFieldMappings(rawDirectiveArgumentAndInputFieldMappings, directiveArgumentAndInputFieldMappingTypeSuffix) {
137 const result = {};
138 Object.keys(rawDirectiveArgumentAndInputFieldMappings).forEach(directive => {
139 const mapperDef = rawDirectiveArgumentAndInputFieldMappings[directive];
140 const parsedMapper = parseMapper(mapperDef, directive, directiveArgumentAndInputFieldMappingTypeSuffix);
141 result[directive] = parsedMapper;
142 });
143 return result;
144}
145exports.transformDirectiveArgumentAndInputFieldMappings = transformDirectiveArgumentAndInputFieldMappings;
146function buildMapperImport(source, types, useTypeImports) {
147 if (!types || types.length === 0) {
148 return null;
149 }
150 const defaultType = types.find(t => t.asDefault === true);
151 let namedTypes = types.filter(t => !t.asDefault);
152 if (useTypeImports) {
153 if (defaultType) {
154 // default as Baz
155 namedTypes = [{ identifier: `default as ${defaultType.identifier}` }, ...namedTypes];
156 }
157 // { Foo, Bar as BarModel }
158 const namedImports = namedTypes.length ? `{ ${namedTypes.map(t => t.identifier).join(', ')} }` : '';
159 // { default as Baz, Foo, Bar as BarModel }
160 return `import type ${[namedImports].filter(Boolean).join(', ')} from '${source}';`;
161 }
162 // { Foo, Bar as BarModel }
163 const namedImports = namedTypes.length ? `{ ${namedTypes.map(t => t.identifier).join(', ')} }` : '';
164 // Baz
165 const defaultImport = defaultType ? defaultType.identifier : '';
166 // Baz, { Foo, Bar as BarModel }
167 return `import ${[defaultImport, namedImports].filter(Boolean).join(', ')} from '${source}';`;
168}
169exports.buildMapperImport = buildMapperImport;