UNPKG

12.2 kBJavaScriptView Raw
1"use strict";
2var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
3 if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
4 return cooked;
5};
6var __assign = (this && this.__assign) || Object.assign || function(t) {
7 for (var s, i = 1, n = arguments.length; i < n; i++) {
8 s = arguments[i];
9 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
10 t[p] = s[p];
11 }
12 return t;
13};
14Object.defineProperty(exports, "__esModule", { value: true });
15var handlebars_1 = require("handlebars");
16var change_case_1 = require("change-case");
17var common_tags_1 = require("common-tags");
18var field_type_to_string_1 = require("./field-type-to-string");
19var sanitizie_filename_1 = require("./sanitizie-filename");
20var flatten_types_1 = require("./flatten-types");
21exports.initHelpers = function (config, schemaContext) {
22 var customHelpers = config.customHelpers || {};
23 Object.keys(customHelpers).forEach(function (helperName) {
24 handlebars_1.registerHelper(helperName, customHelpers[helperName]);
25 });
26 handlebars_1.registerHelper('toPrimitive', function (type) {
27 return config.primitives[type] || type || '';
28 });
29 handlebars_1.registerHelper('stringify', function (obj) {
30 return new handlebars_1.SafeString(JSON.stringify(obj));
31 });
32 handlebars_1.registerHelper('times', function (n, block) {
33 var accum = '';
34 for (var i = 0; i < n; ++i) {
35 accum += block.fn(i);
36 }
37 return accum;
38 });
39 handlebars_1.registerHelper('ifDirective', function (context, directiveName, options) {
40 if (context && context['directives'] && directiveName && typeof directiveName === 'string') {
41 var directives = context['directives'];
42 var directiveValue = directives[directiveName];
43 if (directiveValue) {
44 return options && options.fn ? options.fn(__assign({}, (directiveValue || {}), context)) : '';
45 }
46 else {
47 return options && options.inverse ? options.inverse(context) : '';
48 }
49 }
50 return options && options.inverse ? options.inverse(context) : '';
51 });
52 handlebars_1.registerHelper('unlessDirective', function (context, directiveName, options) {
53 if (context && context['directives'] && directiveName && typeof directiveName === 'string') {
54 var directives = context['directives'];
55 var directiveValue = directives[directiveName];
56 if (!directiveValue) {
57 return options && options.fn ? options.fn(__assign({}, (directiveValue || {}), context)) : '';
58 }
59 else {
60 return options && options.inverse ? options.inverse(context) : '';
61 }
62 }
63 return options && options.inverse ? options.inverse(context) : '';
64 });
65 handlebars_1.registerHelper('toComment', function (str) {
66 if (!str || str === '') {
67 return '';
68 }
69 return new handlebars_1.SafeString('/** ' + common_tags_1.oneLineTrim(templateObject_1 || (templateObject_1 = __makeTemplateObject(["", ""], ["", ""])), str || '') + ' */');
70 });
71 handlebars_1.registerHelper('eachImport', function (context, options) {
72 var ret = '';
73 var imports = [];
74 // Interface, input types, types
75 if (context.fields && !context.onType && !context.operationType) {
76 context.fields.forEach(function (field) {
77 if (!config.primitives[field.type]) {
78 if (field.type === context.name) {
79 return;
80 }
81 var fieldType = field_type_to_string_1.getFieldTypeAsString(field);
82 var file = sanitizie_filename_1.sanitizeFilename(field.type, fieldType);
83 if (!imports.find(function (t) { return t.name === field.type; })) {
84 imports.push({ name: field.type, file: file, type: fieldType });
85 }
86 }
87 // Fields arguments
88 if (field.arguments && field.hasArguments) {
89 field.arguments.forEach(function (arg) {
90 if (!config.primitives[arg.type]) {
91 var fieldType = field_type_to_string_1.getFieldTypeAsString(arg);
92 var file = sanitizie_filename_1.sanitizeFilename(arg.type, fieldType);
93 if (!imports.find(function (t) { return t.name === arg.type; })) {
94 imports.push({ name: arg.type, file: file, type: fieldType });
95 }
96 }
97 });
98 }
99 });
100 }
101 // Types that uses interfaces
102 if (context.interfaces) {
103 context.interfaces.forEach(function (infName) {
104 var file = sanitizie_filename_1.sanitizeFilename(infName, 'interface');
105 if (!imports.find(function (t) { return t.name === infName; })) {
106 imports.push({ name: infName, file: file, type: 'interface' });
107 }
108 });
109 }
110 // Unions
111 if (context.possibleTypes) {
112 context.possibleTypes.forEach(function (possibleType) {
113 var file = sanitizie_filename_1.sanitizeFilename(possibleType, 'type');
114 if (!imports.find(function (t) { return t.name === possibleType; })) {
115 imports.push({ name: possibleType, file: file, type: 'type' });
116 }
117 });
118 }
119 if (context.variables) {
120 context.variables.forEach(function (variable) {
121 if (!config.primitives[variable.type]) {
122 var fieldType = field_type_to_string_1.getFieldTypeAsString(variable);
123 var file = sanitizie_filename_1.sanitizeFilename(variable.type, fieldType);
124 if (!imports.find(function (t) { return t.name === variable.type; })) {
125 imports.push({ name: variable.type, file: file, type: fieldType });
126 }
127 }
128 });
129 }
130 // Operations and Fragments
131 if (context.selectionSet) {
132 var flattenDocument = context.isFlatten ? context : flatten_types_1.flattenSelectionSet(context);
133 flattenDocument.fragmentsSpread.forEach(function (fragmentSpread) {
134 var file = sanitizie_filename_1.sanitizeFilename(fragmentSpread.fragmentName, 'fragment');
135 if (!imports.find(function (t) { return t.name === fragmentSpread.fragmentName; })) {
136 imports.push({ name: fragmentSpread.fragmentName, file: file, type: 'fragment' });
137 }
138 });
139 flattenDocument.innerModels.forEach(function (innerModel) {
140 if (innerModel.fragmentsSpread && innerModel.fragmentsSpread.length > 0) {
141 innerModel.fragmentsSpread.forEach(function (fragmentSpread) {
142 var file = sanitizie_filename_1.sanitizeFilename(fragmentSpread.fragmentName, 'fragment');
143 if (!imports.find(function (t) { return t.name === fragmentSpread.fragmentName; })) {
144 imports.push({ name: fragmentSpread.fragmentName, file: file, type: 'fragment' });
145 }
146 });
147 }
148 innerModel.fields.forEach(function (field) {
149 if (!config.primitives[field.type]) {
150 var type = null;
151 if (field.isEnum) {
152 type = 'enum';
153 }
154 else if (field.isInputType) {
155 type = 'input-type';
156 }
157 else if (field.isScalar) {
158 type = 'scalar';
159 }
160 if (type !== null) {
161 var file = sanitizie_filename_1.sanitizeFilename(field.type, type);
162 if (!imports.find(function (t) { return t.name === field.type; })) {
163 imports.push({ name: field.type, file: file, type: type });
164 }
165 }
166 }
167 });
168 });
169 }
170 for (var i = 0, j = imports.length; i < j; i++) {
171 ret =
172 ret +
173 options.fn(imports[i], {
174 data: {
175 withExtension: imports[i] + '.' + config.filesExtension
176 }
177 });
178 }
179 return ret;
180 });
181 handlebars_1.registerHelper('toLowerCase', function (str) {
182 return (str || '').toLowerCase();
183 });
184 handlebars_1.registerHelper('toUpperCase', function (str) {
185 return (str || '').toUpperCase();
186 });
187 handlebars_1.registerHelper('toPascalCase', function (str) {
188 return change_case_1.pascalCase(str || '');
189 });
190 handlebars_1.registerHelper('toSnakeCase', function (str) {
191 return change_case_1.snakeCase(str || '');
192 });
193 handlebars_1.registerHelper('toTitleCase', function (str) {
194 return change_case_1.titleCase(str || '');
195 });
196 handlebars_1.registerHelper('toCamelCase', function (str) {
197 return change_case_1.camelCase(str || '');
198 });
199 handlebars_1.registerHelper('multilineString', function (str) {
200 if (!str) {
201 return '';
202 }
203 var lines = str.split('\n');
204 return lines
205 .map(function (line, index) {
206 var isLastLine = index !== lines.length - 1;
207 return "\"" + line.replace(/"/g, '\\"') + "\"" + (isLastLine ? ' +' : '');
208 })
209 .join('\r\n');
210 });
211 handlebars_1.registerHelper('for', function (from, to, incr, block) {
212 var accum = '';
213 for (var i = from; i < to; i += incr) {
214 accum += block.fn(i);
215 }
216 return accum;
217 });
218 handlebars_1.registerHelper('ifCond', function (v1, operator, v2, options) {
219 switch (operator) {
220 case '==':
221 return v1 === v2 ? options.fn(this) : options.inverse(this);
222 case '===':
223 return v1 === v2 ? options.fn(this) : options.inverse(this);
224 case '!=':
225 return v1 !== v2 ? options.fn(this) : options.inverse(this);
226 case '!==':
227 return v1 !== v2 ? options.fn(this) : options.inverse(this);
228 case '<':
229 return v1 < v2 ? options.fn(this) : options.inverse(this);
230 case '<=':
231 return v1 <= v2 ? options.fn(this) : options.inverse(this);
232 case '>':
233 return v1 > v2 ? options.fn(this) : options.inverse(this);
234 case '>=':
235 return v1 >= v2 ? options.fn(this) : options.inverse(this);
236 case '&&':
237 return v1 && v2 ? options.fn(this) : options.inverse(this);
238 case '||':
239 return v1 || v2 ? options.fn(this) : options.inverse(this);
240 default:
241 return options.inverse(this);
242 }
243 });
244 handlebars_1.registerHelper('withGql', function (type, name, options) {
245 if (!type || !name) {
246 throw new Error("Both type and name are required for withGql helper!");
247 }
248 type = change_case_1.camelCase(type);
249 var sourceArr = schemaContext[type] || schemaContext[type + 's'];
250 if (!sourceArr) {
251 throw new Error("Type " + type + " is not a valid SchemaTemplateContext field!");
252 }
253 var item = sourceArr.find(function (item) { return item.name === name; });
254 if (!item) {
255 throw new Error("GraphQL object with name " + name + " and type " + type + " cannot be found!");
256 }
257 return options.fn(item);
258 });
259};
260var templateObject_1;
261//# sourceMappingURL=handlebars-extensions.js.map
\No newline at end of file