@graphql-mesh/string-interpolation 0.3.3 → 0.4.0-alpha-20221117085952-ec731e304
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 +30 -0
- package/{dist/index.js → cjs/interpolator.js} +12 -112
- package/cjs/modifiers/index.js +20 -0
- package/cjs/modifiers/lowercase.js +5 -0
- package/cjs/modifiers/title.js +5 -0
- package/cjs/modifiers/uppercase.js +5 -0
- package/cjs/package.json +1 -0
- package/cjs/resolver-data-factory.js +51 -0
- package/cjs/statics/DefaultOptions.js +6 -0
- package/esm/index.js +25 -0
- package/{dist/index.mjs → esm/interpolator.js} +3 -96
- package/esm/modifiers/index.js +17 -0
- package/{src → esm}/modifiers/lowercase.js +0 -0
- package/{src → esm}/modifiers/title.js +0 -0
- package/{src → esm}/modifiers/uppercase.js +0 -0
- package/esm/resolver-data-factory.js +44 -0
- package/{src → esm}/statics/DefaultOptions.js +1 -1
- package/package.json +27 -22
- package/typings/index.d.cts +5 -0
- package/{dist → typings}/index.d.ts +2 -2
- package/{dist/interpolator.d.ts → typings/interpolator.d.cts} +0 -0
- package/typings/interpolator.d.ts +32 -0
- package/{dist/modifiers/index.d.ts → typings/modifiers/index.d.cts} +0 -0
- package/typings/modifiers/index.d.ts +4 -0
- package/{dist/modifiers/lowercase.d.ts → typings/modifiers/lowercase.d.cts} +0 -0
- package/typings/modifiers/lowercase.d.ts +1 -0
- package/{dist/modifiers/title.d.ts → typings/modifiers/title.d.cts} +0 -0
- package/typings/modifiers/title.d.ts +1 -0
- package/{dist/modifiers/uppercase.d.ts → typings/modifiers/uppercase.d.cts} +0 -0
- package/typings/modifiers/uppercase.d.ts +1 -0
- package/{dist/resolver-data-factory.d.ts → typings/resolver-data-factory.d.cts} +0 -0
- package/typings/resolver-data-factory.d.ts +19 -0
- package/{dist/statics/DefaultOptions.d.ts → typings/statics/DefaultOptions.d.cts} +0 -0
- package/typings/statics/DefaultOptions.d.ts +3 -0
- package/CHANGELOG.md +0 -55
- package/dist/package.json +0 -38
- package/src/index.ts +0 -30
- package/src/interpolator.js +0 -181
- package/src/modifiers/index.js +0 -18
- package/src/resolver-data-factory.ts +0 -71
package/cjs/index.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.stringInterpolator = exports.Interpolator = exports.hashObject = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const interpolator_js_1 = require("./interpolator.js");
|
|
6
|
+
Object.defineProperty(exports, "Interpolator", { enumerable: true, get: function () { return interpolator_js_1.Interpolator; } });
|
|
7
|
+
const dayjs_1 = tslib_1.__importDefault(require("dayjs"));
|
|
8
|
+
const hashCode = (s) => s.split('').reduce((a, b) => ((a << 5) - a + b.charCodeAt(0)) | 0, 0);
|
|
9
|
+
function hashObject(value) {
|
|
10
|
+
return hashCode(JSON.stringify(value)).toString();
|
|
11
|
+
}
|
|
12
|
+
exports.hashObject = hashObject;
|
|
13
|
+
exports.stringInterpolator = new interpolator_js_1.Interpolator({
|
|
14
|
+
delimiter: ['{', '}'],
|
|
15
|
+
});
|
|
16
|
+
exports.stringInterpolator.addAlias('typeName', 'info.parentType.name');
|
|
17
|
+
exports.stringInterpolator.addAlias('type', 'info.parentType.name');
|
|
18
|
+
exports.stringInterpolator.addAlias('parentType', 'info.parentType.name');
|
|
19
|
+
exports.stringInterpolator.addAlias('fieldName', 'info.fieldName');
|
|
20
|
+
exports.stringInterpolator.registerModifier('date', (formatStr) => (0, dayjs_1.default)(new Date()).format(formatStr));
|
|
21
|
+
exports.stringInterpolator.registerModifier('hash', (value) => hashObject(value));
|
|
22
|
+
exports.stringInterpolator.registerModifier('base64', (value) => {
|
|
23
|
+
if (globalThis.Buffer.from) {
|
|
24
|
+
return globalThis.Buffer.from(value).toString('base64');
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
return btoa(value);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
tslib_1.__exportStar(require("./resolver-data-factory.js"), exports);
|
|
@@ -1,47 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const dayjs = _interopDefault(require('dayjs'));
|
|
10
|
-
|
|
11
|
-
const defaultOptions = {
|
|
12
|
-
delimiter: ['{', '}'],
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
const uppercase = value => value.toUpperCase();
|
|
16
|
-
|
|
17
|
-
const lowercase = value => value.toLowerCase();
|
|
18
|
-
|
|
19
|
-
const titlecase = value => value.replace(/\w\S*/g, s => s.charAt(0).toUpperCase() + s.substr(1).toLowerCase());
|
|
20
|
-
|
|
21
|
-
const defaultModifiers = [
|
|
22
|
-
{
|
|
23
|
-
key: 'uppercase',
|
|
24
|
-
transform: uppercase,
|
|
25
|
-
},
|
|
26
|
-
{
|
|
27
|
-
key: 'lowercase',
|
|
28
|
-
transform: lowercase,
|
|
29
|
-
},
|
|
30
|
-
{
|
|
31
|
-
key: 'title',
|
|
32
|
-
transform: titlecase,
|
|
33
|
-
},
|
|
34
|
-
];
|
|
35
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Interpolator = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const DefaultOptions_1 = require("./statics/DefaultOptions");
|
|
6
|
+
const lodash_get_1 = tslib_1.__importDefault(require("lodash.get"));
|
|
7
|
+
const modifiers_1 = require("./modifiers");
|
|
8
|
+
const json_pointer_1 = tslib_1.__importDefault(require("json-pointer"));
|
|
36
9
|
class Interpolator {
|
|
37
|
-
constructor(options = defaultOptions) {
|
|
10
|
+
constructor(options = DefaultOptions_1.defaultOptions) {
|
|
38
11
|
this.options = options;
|
|
39
12
|
this.modifiers = [];
|
|
40
13
|
this.aliases = [];
|
|
41
14
|
this.registerBuiltInModifiers();
|
|
42
15
|
}
|
|
43
16
|
registerBuiltInModifiers() {
|
|
44
|
-
defaultModifiers.forEach(modifier => this.registerModifier(modifier.key, modifier.transform));
|
|
17
|
+
modifiers_1.defaultModifiers.forEach(modifier => this.registerModifier(modifier.key, modifier.transform));
|
|
45
18
|
return this;
|
|
46
19
|
}
|
|
47
20
|
get delimiter() {
|
|
@@ -141,10 +114,10 @@ class Interpolator {
|
|
|
141
114
|
}
|
|
142
115
|
applyData(key, data) {
|
|
143
116
|
const [prop, ptr] = key.split('#');
|
|
144
|
-
const propData =
|
|
117
|
+
const propData = (0, lodash_get_1.default)(data, prop);
|
|
145
118
|
if (ptr) {
|
|
146
119
|
try {
|
|
147
|
-
return
|
|
120
|
+
return json_pointer_1.default.get(propData, ptr);
|
|
148
121
|
}
|
|
149
122
|
catch (e) {
|
|
150
123
|
if (e.message.startsWith('Invalid reference')) {
|
|
@@ -182,77 +155,4 @@ class Interpolator {
|
|
|
182
155
|
return this;
|
|
183
156
|
}
|
|
184
157
|
}
|
|
185
|
-
|
|
186
|
-
function getInterpolationKeys(...interpolationStrings) {
|
|
187
|
-
return interpolationStrings.reduce((keys, str) => [...keys, ...(str ? stringInterpolator.parseRules(str).map((match) => match.key) : [])], []);
|
|
188
|
-
}
|
|
189
|
-
function parseInterpolationStrings(interpolationStrings, argTypeMap) {
|
|
190
|
-
const interpolationKeys = getInterpolationKeys(...interpolationStrings);
|
|
191
|
-
const args = {};
|
|
192
|
-
const contextVariables = {};
|
|
193
|
-
for (const interpolationKey of interpolationKeys) {
|
|
194
|
-
const interpolationKeyParts = interpolationKey.split('.');
|
|
195
|
-
const varName = interpolationKeyParts[interpolationKeyParts.length - 1];
|
|
196
|
-
const initialObject = interpolationKeyParts[0];
|
|
197
|
-
const argType = argTypeMap && varName in argTypeMap ? argTypeMap[varName] : interpolationKeyParts.length > 2 ? 'JSON' : 'ID';
|
|
198
|
-
switch (initialObject) {
|
|
199
|
-
case 'args':
|
|
200
|
-
args[varName] = {
|
|
201
|
-
type: argType,
|
|
202
|
-
};
|
|
203
|
-
break;
|
|
204
|
-
case 'context':
|
|
205
|
-
contextVariables[varName] = `Scalars['${argType}']`;
|
|
206
|
-
break;
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
return {
|
|
210
|
-
args,
|
|
211
|
-
contextVariables,
|
|
212
|
-
};
|
|
213
|
-
}
|
|
214
|
-
function getInterpolatedStringFactory(nonInterpolatedString) {
|
|
215
|
-
return resolverData => stringInterpolator.parse(nonInterpolatedString, resolverData);
|
|
216
|
-
}
|
|
217
|
-
function getInterpolatedHeadersFactory(nonInterpolatedHeaders = {}) {
|
|
218
|
-
return resolverData => {
|
|
219
|
-
const headers = {};
|
|
220
|
-
for (const headerName in nonInterpolatedHeaders) {
|
|
221
|
-
const headerValue = nonInterpolatedHeaders[headerName];
|
|
222
|
-
if (headerValue) {
|
|
223
|
-
headers[headerName.toLowerCase()] = stringInterpolator.parse(headerValue, resolverData);
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
return headers;
|
|
227
|
-
};
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
const hashCode = (s) => s.split('').reduce((a, b) => ((a << 5) - a + b.charCodeAt(0)) | 0, 0);
|
|
231
|
-
function hashObject(value) {
|
|
232
|
-
return hashCode(JSON.stringify(value)).toString();
|
|
233
|
-
}
|
|
234
|
-
const stringInterpolator = new Interpolator({
|
|
235
|
-
delimiter: ['{', '}'],
|
|
236
|
-
});
|
|
237
|
-
stringInterpolator.addAlias('typeName', 'info.parentType.name');
|
|
238
|
-
stringInterpolator.addAlias('type', 'info.parentType.name');
|
|
239
|
-
stringInterpolator.addAlias('parentType', 'info.parentType.name');
|
|
240
|
-
stringInterpolator.addAlias('fieldName', 'info.fieldName');
|
|
241
|
-
stringInterpolator.registerModifier('date', (formatStr) => dayjs(new Date()).format(formatStr));
|
|
242
|
-
stringInterpolator.registerModifier('hash', (value) => hashObject(value));
|
|
243
|
-
stringInterpolator.registerModifier('base64', (value) => {
|
|
244
|
-
if (globalThis.Buffer.from) {
|
|
245
|
-
return globalThis.Buffer.from(value).toString('base64');
|
|
246
|
-
}
|
|
247
|
-
else {
|
|
248
|
-
return btoa(value);
|
|
249
|
-
}
|
|
250
|
-
});
|
|
251
|
-
|
|
252
158
|
exports.Interpolator = Interpolator;
|
|
253
|
-
exports.getInterpolatedHeadersFactory = getInterpolatedHeadersFactory;
|
|
254
|
-
exports.getInterpolatedStringFactory = getInterpolatedStringFactory;
|
|
255
|
-
exports.getInterpolationKeys = getInterpolationKeys;
|
|
256
|
-
exports.hashObject = hashObject;
|
|
257
|
-
exports.parseInterpolationStrings = parseInterpolationStrings;
|
|
258
|
-
exports.stringInterpolator = stringInterpolator;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defaultModifiers = void 0;
|
|
4
|
+
const uppercase_1 = require("./uppercase");
|
|
5
|
+
const lowercase_1 = require("./lowercase");
|
|
6
|
+
const title_1 = require("./title");
|
|
7
|
+
exports.defaultModifiers = [
|
|
8
|
+
{
|
|
9
|
+
key: 'uppercase',
|
|
10
|
+
transform: uppercase_1.uppercase,
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
key: 'lowercase',
|
|
14
|
+
transform: lowercase_1.lowercase,
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
key: 'title',
|
|
18
|
+
transform: title_1.titlecase,
|
|
19
|
+
},
|
|
20
|
+
];
|
package/cjs/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getInterpolatedHeadersFactory = exports.getInterpolatedStringFactory = exports.parseInterpolationStrings = exports.getInterpolationKeys = void 0;
|
|
4
|
+
const index_js_1 = require("./index.js");
|
|
5
|
+
function getInterpolationKeys(...interpolationStrings) {
|
|
6
|
+
return interpolationStrings.reduce((keys, str) => [...keys, ...(str ? index_js_1.stringInterpolator.parseRules(str).map((match) => match.key) : [])], []);
|
|
7
|
+
}
|
|
8
|
+
exports.getInterpolationKeys = getInterpolationKeys;
|
|
9
|
+
function parseInterpolationStrings(interpolationStrings, argTypeMap) {
|
|
10
|
+
const interpolationKeys = getInterpolationKeys(...interpolationStrings);
|
|
11
|
+
const args = {};
|
|
12
|
+
const contextVariables = {};
|
|
13
|
+
for (const interpolationKey of interpolationKeys) {
|
|
14
|
+
const interpolationKeyParts = interpolationKey.split('.');
|
|
15
|
+
const varName = interpolationKeyParts[interpolationKeyParts.length - 1];
|
|
16
|
+
const initialObject = interpolationKeyParts[0];
|
|
17
|
+
const argType = argTypeMap && varName in argTypeMap ? argTypeMap[varName] : interpolationKeyParts.length > 2 ? 'JSON' : 'ID';
|
|
18
|
+
switch (initialObject) {
|
|
19
|
+
case 'args':
|
|
20
|
+
args[varName] = {
|
|
21
|
+
type: argType,
|
|
22
|
+
};
|
|
23
|
+
break;
|
|
24
|
+
case 'context':
|
|
25
|
+
contextVariables[varName] = `Scalars['${argType}']`;
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
args,
|
|
31
|
+
contextVariables,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
exports.parseInterpolationStrings = parseInterpolationStrings;
|
|
35
|
+
function getInterpolatedStringFactory(nonInterpolatedString) {
|
|
36
|
+
return resolverData => index_js_1.stringInterpolator.parse(nonInterpolatedString, resolverData);
|
|
37
|
+
}
|
|
38
|
+
exports.getInterpolatedStringFactory = getInterpolatedStringFactory;
|
|
39
|
+
function getInterpolatedHeadersFactory(nonInterpolatedHeaders = {}) {
|
|
40
|
+
return resolverData => {
|
|
41
|
+
const headers = {};
|
|
42
|
+
for (const headerName in nonInterpolatedHeaders) {
|
|
43
|
+
const headerValue = nonInterpolatedHeaders[headerName];
|
|
44
|
+
if (headerValue) {
|
|
45
|
+
headers[headerName.toLowerCase()] = index_js_1.stringInterpolator.parse(headerValue, resolverData);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return headers;
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
exports.getInterpolatedHeadersFactory = getInterpolatedHeadersFactory;
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Interpolator } from './interpolator.js';
|
|
2
|
+
import dayjs from 'dayjs';
|
|
3
|
+
const hashCode = (s) => s.split('').reduce((a, b) => ((a << 5) - a + b.charCodeAt(0)) | 0, 0);
|
|
4
|
+
export function hashObject(value) {
|
|
5
|
+
return hashCode(JSON.stringify(value)).toString();
|
|
6
|
+
}
|
|
7
|
+
export { Interpolator };
|
|
8
|
+
export const stringInterpolator = new Interpolator({
|
|
9
|
+
delimiter: ['{', '}'],
|
|
10
|
+
});
|
|
11
|
+
stringInterpolator.addAlias('typeName', 'info.parentType.name');
|
|
12
|
+
stringInterpolator.addAlias('type', 'info.parentType.name');
|
|
13
|
+
stringInterpolator.addAlias('parentType', 'info.parentType.name');
|
|
14
|
+
stringInterpolator.addAlias('fieldName', 'info.fieldName');
|
|
15
|
+
stringInterpolator.registerModifier('date', (formatStr) => dayjs(new Date()).format(formatStr));
|
|
16
|
+
stringInterpolator.registerModifier('hash', (value) => hashObject(value));
|
|
17
|
+
stringInterpolator.registerModifier('base64', (value) => {
|
|
18
|
+
if (globalThis.Buffer.from) {
|
|
19
|
+
return globalThis.Buffer.from(value).toString('base64');
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
return btoa(value);
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
export * from './resolver-data-factory.js';
|
|
@@ -1,33 +1,8 @@
|
|
|
1
|
+
import { defaultOptions } from './statics/DefaultOptions';
|
|
1
2
|
import lodashGet from 'lodash.get';
|
|
3
|
+
import { defaultModifiers } from './modifiers';
|
|
2
4
|
import JsonPointer from 'json-pointer';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const defaultOptions = {
|
|
6
|
-
delimiter: ['{', '}'],
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
const uppercase = value => value.toUpperCase();
|
|
10
|
-
|
|
11
|
-
const lowercase = value => value.toLowerCase();
|
|
12
|
-
|
|
13
|
-
const titlecase = value => value.replace(/\w\S*/g, s => s.charAt(0).toUpperCase() + s.substr(1).toLowerCase());
|
|
14
|
-
|
|
15
|
-
const defaultModifiers = [
|
|
16
|
-
{
|
|
17
|
-
key: 'uppercase',
|
|
18
|
-
transform: uppercase,
|
|
19
|
-
},
|
|
20
|
-
{
|
|
21
|
-
key: 'lowercase',
|
|
22
|
-
transform: lowercase,
|
|
23
|
-
},
|
|
24
|
-
{
|
|
25
|
-
key: 'title',
|
|
26
|
-
transform: titlecase,
|
|
27
|
-
},
|
|
28
|
-
];
|
|
29
|
-
|
|
30
|
-
class Interpolator {
|
|
5
|
+
export class Interpolator {
|
|
31
6
|
constructor(options = defaultOptions) {
|
|
32
7
|
this.options = options;
|
|
33
8
|
this.modifiers = [];
|
|
@@ -176,71 +151,3 @@ class Interpolator {
|
|
|
176
151
|
return this;
|
|
177
152
|
}
|
|
178
153
|
}
|
|
179
|
-
|
|
180
|
-
function getInterpolationKeys(...interpolationStrings) {
|
|
181
|
-
return interpolationStrings.reduce((keys, str) => [...keys, ...(str ? stringInterpolator.parseRules(str).map((match) => match.key) : [])], []);
|
|
182
|
-
}
|
|
183
|
-
function parseInterpolationStrings(interpolationStrings, argTypeMap) {
|
|
184
|
-
const interpolationKeys = getInterpolationKeys(...interpolationStrings);
|
|
185
|
-
const args = {};
|
|
186
|
-
const contextVariables = {};
|
|
187
|
-
for (const interpolationKey of interpolationKeys) {
|
|
188
|
-
const interpolationKeyParts = interpolationKey.split('.');
|
|
189
|
-
const varName = interpolationKeyParts[interpolationKeyParts.length - 1];
|
|
190
|
-
const initialObject = interpolationKeyParts[0];
|
|
191
|
-
const argType = argTypeMap && varName in argTypeMap ? argTypeMap[varName] : interpolationKeyParts.length > 2 ? 'JSON' : 'ID';
|
|
192
|
-
switch (initialObject) {
|
|
193
|
-
case 'args':
|
|
194
|
-
args[varName] = {
|
|
195
|
-
type: argType,
|
|
196
|
-
};
|
|
197
|
-
break;
|
|
198
|
-
case 'context':
|
|
199
|
-
contextVariables[varName] = `Scalars['${argType}']`;
|
|
200
|
-
break;
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
return {
|
|
204
|
-
args,
|
|
205
|
-
contextVariables,
|
|
206
|
-
};
|
|
207
|
-
}
|
|
208
|
-
function getInterpolatedStringFactory(nonInterpolatedString) {
|
|
209
|
-
return resolverData => stringInterpolator.parse(nonInterpolatedString, resolverData);
|
|
210
|
-
}
|
|
211
|
-
function getInterpolatedHeadersFactory(nonInterpolatedHeaders = {}) {
|
|
212
|
-
return resolverData => {
|
|
213
|
-
const headers = {};
|
|
214
|
-
for (const headerName in nonInterpolatedHeaders) {
|
|
215
|
-
const headerValue = nonInterpolatedHeaders[headerName];
|
|
216
|
-
if (headerValue) {
|
|
217
|
-
headers[headerName.toLowerCase()] = stringInterpolator.parse(headerValue, resolverData);
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
return headers;
|
|
221
|
-
};
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
const hashCode = (s) => s.split('').reduce((a, b) => ((a << 5) - a + b.charCodeAt(0)) | 0, 0);
|
|
225
|
-
function hashObject(value) {
|
|
226
|
-
return hashCode(JSON.stringify(value)).toString();
|
|
227
|
-
}
|
|
228
|
-
const stringInterpolator = new Interpolator({
|
|
229
|
-
delimiter: ['{', '}'],
|
|
230
|
-
});
|
|
231
|
-
stringInterpolator.addAlias('typeName', 'info.parentType.name');
|
|
232
|
-
stringInterpolator.addAlias('type', 'info.parentType.name');
|
|
233
|
-
stringInterpolator.addAlias('parentType', 'info.parentType.name');
|
|
234
|
-
stringInterpolator.addAlias('fieldName', 'info.fieldName');
|
|
235
|
-
stringInterpolator.registerModifier('date', (formatStr) => dayjs(new Date()).format(formatStr));
|
|
236
|
-
stringInterpolator.registerModifier('hash', (value) => hashObject(value));
|
|
237
|
-
stringInterpolator.registerModifier('base64', (value) => {
|
|
238
|
-
if (globalThis.Buffer.from) {
|
|
239
|
-
return globalThis.Buffer.from(value).toString('base64');
|
|
240
|
-
}
|
|
241
|
-
else {
|
|
242
|
-
return btoa(value);
|
|
243
|
-
}
|
|
244
|
-
});
|
|
245
|
-
|
|
246
|
-
export { Interpolator, getInterpolatedHeadersFactory, getInterpolatedStringFactory, getInterpolationKeys, hashObject, parseInterpolationStrings, stringInterpolator };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { uppercase } from './uppercase';
|
|
2
|
+
import { lowercase } from './lowercase';
|
|
3
|
+
import { titlecase } from './title';
|
|
4
|
+
export const defaultModifiers = [
|
|
5
|
+
{
|
|
6
|
+
key: 'uppercase',
|
|
7
|
+
transform: uppercase,
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
key: 'lowercase',
|
|
11
|
+
transform: lowercase,
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
key: 'title',
|
|
15
|
+
transform: titlecase,
|
|
16
|
+
},
|
|
17
|
+
];
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { stringInterpolator } from './index.js';
|
|
2
|
+
export function getInterpolationKeys(...interpolationStrings) {
|
|
3
|
+
return interpolationStrings.reduce((keys, str) => [...keys, ...(str ? stringInterpolator.parseRules(str).map((match) => match.key) : [])], []);
|
|
4
|
+
}
|
|
5
|
+
export function parseInterpolationStrings(interpolationStrings, argTypeMap) {
|
|
6
|
+
const interpolationKeys = getInterpolationKeys(...interpolationStrings);
|
|
7
|
+
const args = {};
|
|
8
|
+
const contextVariables = {};
|
|
9
|
+
for (const interpolationKey of interpolationKeys) {
|
|
10
|
+
const interpolationKeyParts = interpolationKey.split('.');
|
|
11
|
+
const varName = interpolationKeyParts[interpolationKeyParts.length - 1];
|
|
12
|
+
const initialObject = interpolationKeyParts[0];
|
|
13
|
+
const argType = argTypeMap && varName in argTypeMap ? argTypeMap[varName] : interpolationKeyParts.length > 2 ? 'JSON' : 'ID';
|
|
14
|
+
switch (initialObject) {
|
|
15
|
+
case 'args':
|
|
16
|
+
args[varName] = {
|
|
17
|
+
type: argType,
|
|
18
|
+
};
|
|
19
|
+
break;
|
|
20
|
+
case 'context':
|
|
21
|
+
contextVariables[varName] = `Scalars['${argType}']`;
|
|
22
|
+
break;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return {
|
|
26
|
+
args,
|
|
27
|
+
contextVariables,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
export function getInterpolatedStringFactory(nonInterpolatedString) {
|
|
31
|
+
return resolverData => stringInterpolator.parse(nonInterpolatedString, resolverData);
|
|
32
|
+
}
|
|
33
|
+
export function getInterpolatedHeadersFactory(nonInterpolatedHeaders = {}) {
|
|
34
|
+
return resolverData => {
|
|
35
|
+
const headers = {};
|
|
36
|
+
for (const headerName in nonInterpolatedHeaders) {
|
|
37
|
+
const headerValue = nonInterpolatedHeaders[headerName];
|
|
38
|
+
if (headerValue) {
|
|
39
|
+
headers[headerName.toLowerCase()] = stringInterpolator.parse(headerValue, resolverData);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return headers;
|
|
43
|
+
};
|
|
44
|
+
}
|
package/package.json
CHANGED
|
@@ -1,25 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-mesh/string-interpolation",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"license": "MIT",
|
|
3
|
+
"version": "0.4.0-alpha-20221117085952-ec731e304",
|
|
5
4
|
"description": "Dynamic string manipulation",
|
|
6
5
|
"sideEffects": false,
|
|
7
|
-
"main": "dist/index.js",
|
|
8
|
-
"module": "dist/index.mjs",
|
|
9
|
-
"typings": "dist/index.d.ts",
|
|
10
|
-
"typescript": {
|
|
11
|
-
"definition": "dist/index.d.ts"
|
|
12
|
-
},
|
|
13
|
-
"exports": {
|
|
14
|
-
".": {
|
|
15
|
-
"require": "./dist/index.js",
|
|
16
|
-
"import": "./dist/index.mjs"
|
|
17
|
-
},
|
|
18
|
-
"./*": {
|
|
19
|
-
"require": "./dist/*.js",
|
|
20
|
-
"import": "./dist/*.mjs"
|
|
21
|
-
}
|
|
22
|
-
},
|
|
23
6
|
"peerDependencies": {
|
|
24
7
|
"graphql": "*"
|
|
25
8
|
},
|
|
@@ -28,13 +11,35 @@
|
|
|
28
11
|
"dayjs": "1.11.6",
|
|
29
12
|
"lodash.get": "4.4.2"
|
|
30
13
|
},
|
|
31
|
-
"author": "Arda TANRIKULU <ardatanrikulu@gmail.com>",
|
|
32
14
|
"repository": {
|
|
33
15
|
"type": "git",
|
|
34
16
|
"url": "Urigo/graphql-mesh",
|
|
35
17
|
"directory": "packages/string-interpolation"
|
|
36
18
|
},
|
|
37
|
-
"
|
|
38
|
-
|
|
19
|
+
"author": "Arda TANRIKULU <ardatanrikulu@gmail.com>",
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"main": "cjs/index.js",
|
|
22
|
+
"module": "esm/index.js",
|
|
23
|
+
"typings": "typings/index.d.ts",
|
|
24
|
+
"typescript": {
|
|
25
|
+
"definition": "typings/index.d.ts"
|
|
26
|
+
},
|
|
27
|
+
"type": "module",
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"require": {
|
|
31
|
+
"types": "./typings/index.d.cts",
|
|
32
|
+
"default": "./cjs/index.js"
|
|
33
|
+
},
|
|
34
|
+
"import": {
|
|
35
|
+
"types": "./typings/index.d.ts",
|
|
36
|
+
"default": "./esm/index.js"
|
|
37
|
+
},
|
|
38
|
+
"default": {
|
|
39
|
+
"types": "./typings/index.d.ts",
|
|
40
|
+
"default": "./esm/index.js"
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
"./package.json": "./package.json"
|
|
39
44
|
}
|
|
40
|
-
}
|
|
45
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Interpolator } from './interpolator';
|
|
1
|
+
import { Interpolator } from './interpolator.js';
|
|
2
2
|
export declare function hashObject(value: any): string;
|
|
3
3
|
export { Interpolator };
|
|
4
4
|
export declare const stringInterpolator: Interpolator;
|
|
5
|
-
export * from './resolver-data-factory';
|
|
5
|
+
export * from './resolver-data-factory.js';
|
|
File without changes
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export class Interpolator {
|
|
2
|
+
constructor(options?: {
|
|
3
|
+
delimiter: string[];
|
|
4
|
+
});
|
|
5
|
+
options: {
|
|
6
|
+
delimiter: string[];
|
|
7
|
+
};
|
|
8
|
+
modifiers: any[];
|
|
9
|
+
aliases: any[];
|
|
10
|
+
registerBuiltInModifiers(): Interpolator;
|
|
11
|
+
get delimiter(): string[];
|
|
12
|
+
delimiterStart(): string;
|
|
13
|
+
delimiterEnd(): string;
|
|
14
|
+
registerModifier(key: any, transform: any): Error | Interpolator;
|
|
15
|
+
parseRules(str: any): any;
|
|
16
|
+
extractRules(matches: any): any;
|
|
17
|
+
getKeyFromMatch(match: any): any;
|
|
18
|
+
removeDelimiter(val: any): any;
|
|
19
|
+
removeAfter(str: any, val: any): any;
|
|
20
|
+
extractAfter(str: any, val: any): any;
|
|
21
|
+
getAlternativeText(str: any): any;
|
|
22
|
+
getModifiers(str: any): any;
|
|
23
|
+
parse(str?: string, data?: {}): any;
|
|
24
|
+
parseFromRules(str: any, data: any, rules: any): any;
|
|
25
|
+
applyRule(str: any, rule: any, data?: {}): any;
|
|
26
|
+
getFromAlias(key: any): any;
|
|
27
|
+
applyData(key: any, data: any): any;
|
|
28
|
+
getModifier(key: any): any;
|
|
29
|
+
applyModifiers(modifiers: any, str: any, rawData: any): any;
|
|
30
|
+
addAlias(key: any, ref: any): Interpolator;
|
|
31
|
+
removeAlias(key: any): Interpolator;
|
|
32
|
+
}
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export function lowercase(value: any): any;
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export function titlecase(value: any): any;
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export function uppercase(value: any): any;
|
|
File without changes
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { GraphQLInputType, GraphQLResolveInfo } from 'graphql';
|
|
2
|
+
export declare type ResolverData<TParent = any, TArgs = any, TContext = any, TResult = any> = {
|
|
3
|
+
root?: TParent;
|
|
4
|
+
args?: TArgs;
|
|
5
|
+
context?: TContext;
|
|
6
|
+
info?: GraphQLResolveInfo;
|
|
7
|
+
result?: TResult;
|
|
8
|
+
env: Record<string, string>;
|
|
9
|
+
};
|
|
10
|
+
export declare type ResolverDataBasedFactory<T> = (data: ResolverData) => T;
|
|
11
|
+
export declare function getInterpolationKeys(...interpolationStrings: string[]): any[];
|
|
12
|
+
export declare function parseInterpolationStrings(interpolationStrings: Iterable<string>, argTypeMap?: Record<string, string | GraphQLInputType>): {
|
|
13
|
+
args: Record<string, {
|
|
14
|
+
type: string | GraphQLInputType;
|
|
15
|
+
}>;
|
|
16
|
+
contextVariables: Record<string, string>;
|
|
17
|
+
};
|
|
18
|
+
export declare function getInterpolatedStringFactory(nonInterpolatedString: string): ResolverDataBasedFactory<string>;
|
|
19
|
+
export declare function getInterpolatedHeadersFactory(nonInterpolatedHeaders?: Record<string, string>): ResolverDataBasedFactory<Record<string, string>>;
|
|
File without changes
|
package/CHANGELOG.md
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
# @graphql-mesh/string-interpolation
|
|
2
|
-
|
|
3
|
-
## 0.3.3
|
|
4
|
-
|
|
5
|
-
### Patch Changes
|
|
6
|
-
|
|
7
|
-
- [#4719](https://github.com/Urigo/graphql-mesh/pull/4719) [`5c87cfc60`](https://github.com/Urigo/graphql-mesh/commit/5c87cfc60501213e8701482b093490ec1a5fce23) Thanks [@renovate](https://github.com/apps/renovate)! - dependencies updates:
|
|
8
|
-
- Updated dependency [`dayjs@1.11.6` ↗︎](https://www.npmjs.com/package/dayjs/v/1.11.6) (from `1.11.5`, in `dependencies`)
|
|
9
|
-
|
|
10
|
-
## 0.3.2
|
|
11
|
-
|
|
12
|
-
### Patch Changes
|
|
13
|
-
|
|
14
|
-
- [#4310](https://github.com/Urigo/graphql-mesh/pull/4310) [`2ad667d96`](https://github.com/Urigo/graphql-mesh/commit/2ad667d964545ed47170cfa4f9393282edc2073b) Thanks [@belgattitude](https://github.com/belgattitude)! - Add MIT licence field in package.json
|
|
15
|
-
|
|
16
|
-
## 0.3.1
|
|
17
|
-
|
|
18
|
-
### Patch Changes
|
|
19
|
-
|
|
20
|
-
- [#4298](https://github.com/Urigo/graphql-mesh/pull/4298) [`991373717`](https://github.com/Urigo/graphql-mesh/commit/99137371708b7fe12b32dfcfe93d535507a7f968) Thanks [@renovate](https://github.com/apps/renovate)! - dependencies updates:
|
|
21
|
-
|
|
22
|
-
- Updated dependency [`dayjs@1.11.5` ↗︎](https://www.npmjs.com/package/dayjs/v/1.11.5) (was `1.11.4`, in `dependencies`)
|
|
23
|
-
|
|
24
|
-
## 0.3.0
|
|
25
|
-
|
|
26
|
-
### Minor Changes
|
|
27
|
-
|
|
28
|
-
- 13b9b30f7: Add interpolation strings to the generated MeshContext type
|
|
29
|
-
|
|
30
|
-
## 0.2.0
|
|
31
|
-
|
|
32
|
-
### Minor Changes
|
|
33
|
-
|
|
34
|
-
- 974e703e2: No longer import entire lodash library but instead individual smaller packages
|
|
35
|
-
|
|
36
|
-
### Patch Changes
|
|
37
|
-
|
|
38
|
-
- 974e703e2: Cleanup dependencies
|
|
39
|
-
- 974e703e2: Use deeper lodash imports to have better treeshaking and avoid using eval
|
|
40
|
-
|
|
41
|
-
## 0.1.0
|
|
42
|
-
|
|
43
|
-
### Minor Changes
|
|
44
|
-
|
|
45
|
-
- 2e9addd80: Use platform agnostic json-pointer instead of json-ptr
|
|
46
|
-
|
|
47
|
-
### Patch Changes
|
|
48
|
-
|
|
49
|
-
- 2e9addd80: Bump cross-undici-fetch for Node 18 compat
|
|
50
|
-
|
|
51
|
-
## 0.0.1
|
|
52
|
-
|
|
53
|
-
### Patch Changes
|
|
54
|
-
|
|
55
|
-
- 7856f92d3: Bump all packages
|
package/dist/package.json
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@graphql-mesh/string-interpolation",
|
|
3
|
-
"version": "0.3.3",
|
|
4
|
-
"description": "Dynamic string manipulation",
|
|
5
|
-
"sideEffects": false,
|
|
6
|
-
"peerDependencies": {
|
|
7
|
-
"graphql": "*"
|
|
8
|
-
},
|
|
9
|
-
"dependencies": {
|
|
10
|
-
"dayjs": "1.11.6",
|
|
11
|
-
"json-pointer": "0.6.2",
|
|
12
|
-
"lodash.get": "4.4.2"
|
|
13
|
-
},
|
|
14
|
-
"repository": {
|
|
15
|
-
"type": "git",
|
|
16
|
-
"url": "Urigo/graphql-mesh",
|
|
17
|
-
"directory": "packages/string-interpolation"
|
|
18
|
-
},
|
|
19
|
-
"author": "Arda TANRIKULU <ardatanrikulu@gmail.com>",
|
|
20
|
-
"license": "MIT",
|
|
21
|
-
"main": "index.js",
|
|
22
|
-
"module": "index.mjs",
|
|
23
|
-
"typings": "index.d.ts",
|
|
24
|
-
"typescript": {
|
|
25
|
-
"definition": "index.d.ts"
|
|
26
|
-
},
|
|
27
|
-
"exports": {
|
|
28
|
-
".": {
|
|
29
|
-
"require": "./index.js",
|
|
30
|
-
"import": "./index.mjs"
|
|
31
|
-
},
|
|
32
|
-
"./*": {
|
|
33
|
-
"require": "./*.js",
|
|
34
|
-
"import": "./*.mjs"
|
|
35
|
-
},
|
|
36
|
-
"./package.json": "./package.json"
|
|
37
|
-
}
|
|
38
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { Interpolator } from './interpolator';
|
|
2
|
-
import dayjs from 'dayjs';
|
|
3
|
-
|
|
4
|
-
const hashCode = (s: string) => s.split('').reduce((a, b) => ((a << 5) - a + b.charCodeAt(0)) | 0, 0);
|
|
5
|
-
|
|
6
|
-
export function hashObject(value: any): string {
|
|
7
|
-
return hashCode(JSON.stringify(value)).toString();
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export { Interpolator };
|
|
11
|
-
|
|
12
|
-
export const stringInterpolator = new Interpolator({
|
|
13
|
-
delimiter: ['{', '}'],
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
stringInterpolator.addAlias('typeName', 'info.parentType.name');
|
|
17
|
-
stringInterpolator.addAlias('type', 'info.parentType.name');
|
|
18
|
-
stringInterpolator.addAlias('parentType', 'info.parentType.name');
|
|
19
|
-
stringInterpolator.addAlias('fieldName', 'info.fieldName');
|
|
20
|
-
stringInterpolator.registerModifier('date', (formatStr: string) => dayjs(new Date()).format(formatStr));
|
|
21
|
-
stringInterpolator.registerModifier('hash', (value: any) => hashObject(value));
|
|
22
|
-
stringInterpolator.registerModifier('base64', (value: any) => {
|
|
23
|
-
if (globalThis.Buffer.from) {
|
|
24
|
-
return globalThis.Buffer.from(value).toString('base64');
|
|
25
|
-
} else {
|
|
26
|
-
return btoa(value);
|
|
27
|
-
}
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
export * from './resolver-data-factory';
|
package/src/interpolator.js
DELETED
|
@@ -1,181 +0,0 @@
|
|
|
1
|
-
import { defaultOptions } from './statics/DefaultOptions';
|
|
2
|
-
import lodashGet from 'lodash.get';
|
|
3
|
-
import { defaultModifiers } from './modifiers';
|
|
4
|
-
import JsonPointer from 'json-pointer';
|
|
5
|
-
|
|
6
|
-
export class Interpolator {
|
|
7
|
-
constructor(options = defaultOptions) {
|
|
8
|
-
this.options = options;
|
|
9
|
-
this.modifiers = [];
|
|
10
|
-
this.aliases = [];
|
|
11
|
-
this.registerBuiltInModifiers();
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
registerBuiltInModifiers() {
|
|
15
|
-
defaultModifiers.forEach(modifier => this.registerModifier(modifier.key, modifier.transform));
|
|
16
|
-
return this;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
get delimiter() {
|
|
20
|
-
return this.options.delimiter;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
delimiterStart() {
|
|
24
|
-
return this.options.delimiter[0];
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
delimiterEnd() {
|
|
28
|
-
return this.options.delimiter[1];
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
registerModifier(key, transform) {
|
|
32
|
-
if (!key) {
|
|
33
|
-
return new Error('Modifiers must have a key');
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
if (typeof transform !== 'function') {
|
|
37
|
-
return new Error('Modifiers must have a transformer. Transformers must be a function that returns a value.');
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
this.modifiers.push({ key: key.toLowerCase(), transform });
|
|
41
|
-
return this;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
parseRules(str) {
|
|
45
|
-
const regex = `${this.delimiterStart()}([^}]+)${this.delimiterEnd()}`;
|
|
46
|
-
const execRegex = new RegExp(regex, 'gi');
|
|
47
|
-
const matches = str.match(execRegex);
|
|
48
|
-
|
|
49
|
-
// const parsableMatches = matches.map((match) => ({ key: removeDelimiter(match), replaceWith: match }));
|
|
50
|
-
return matches ? this.extractRules(matches) : [];
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
extractRules(matches) {
|
|
54
|
-
return matches.map(match => {
|
|
55
|
-
const alternativeText = this.getAlternativeText(match);
|
|
56
|
-
const modifiers = this.getModifiers(match);
|
|
57
|
-
return {
|
|
58
|
-
key: this.getKeyFromMatch(match),
|
|
59
|
-
replace: match,
|
|
60
|
-
modifiers,
|
|
61
|
-
alternativeText,
|
|
62
|
-
};
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
getKeyFromMatch(match) {
|
|
67
|
-
const removeReservedSymbols = [':', '|'];
|
|
68
|
-
return this.removeDelimiter(
|
|
69
|
-
removeReservedSymbols.reduce((val, sym) => (val.indexOf(sym) > 0 ? this.removeAfter(val, sym) : val), match)
|
|
70
|
-
);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
removeDelimiter(val) {
|
|
74
|
-
return val.replace(new RegExp(this.delimiterStart(), 'g'), '').replace(new RegExp(this.delimiterEnd(), 'g'), '');
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
removeAfter(str, val) {
|
|
78
|
-
return str.substring(0, str.indexOf(val));
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
extractAfter(str, val) {
|
|
82
|
-
return str.substring(str.indexOf(val) + 1);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
getAlternativeText(str) {
|
|
86
|
-
if (str.indexOf(':') > 0) {
|
|
87
|
-
const altText = this.removeDelimiter(this.extractAfter(str, ':'));
|
|
88
|
-
if (altText.indexOf('|') > 0) {
|
|
89
|
-
return this.removeAfter(altText, '|');
|
|
90
|
-
}
|
|
91
|
-
return altText;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return '';
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
getModifiers(str) {
|
|
98
|
-
if (str.indexOf('|') > 0) {
|
|
99
|
-
const strModifiers = this.removeDelimiter(this.extractAfter(str, '|')).split(',');
|
|
100
|
-
return strModifiers.map(modifier => this.getModifier(modifier.toLowerCase()));
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return [];
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
parse(str = '', data = {}) {
|
|
107
|
-
const rules = this.parseRules(str);
|
|
108
|
-
if (rules && rules.length > 0) {
|
|
109
|
-
return this.parseFromRules(str, data, rules);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
return str;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
parseFromRules(str, data, rules) {
|
|
116
|
-
return rules.reduce((reducedStr, rule) => this.applyRule(reducedStr, rule, data), str);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
applyRule(str, rule, data = {}) {
|
|
120
|
-
const dataToReplace = this.applyData(rule.key, data);
|
|
121
|
-
if (dataToReplace) {
|
|
122
|
-
return str.replace(rule.replace, this.applyModifiers(rule.modifiers, dataToReplace, data));
|
|
123
|
-
} else if (rule.alternativeText) {
|
|
124
|
-
return str.replace(rule.replace, this.applyModifiers(rule.modifiers, rule.alternativeText, data));
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
const defaultModifier = this.applyModifiers(rule.modifiers, rule.key, data);
|
|
128
|
-
if (defaultModifier === rule.key) {
|
|
129
|
-
return str.replace(rule.replace, '');
|
|
130
|
-
}
|
|
131
|
-
return str.replace(rule.replace, defaultModifier);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
getFromAlias(key) {
|
|
135
|
-
return this.aliases.find(alias => alias.key.toLowerCase() === key.toLowerCase());
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
applyData(key, data) {
|
|
139
|
-
const [prop, ptr] = key.split('#');
|
|
140
|
-
const propData = lodashGet(data, prop);
|
|
141
|
-
if (ptr) {
|
|
142
|
-
try {
|
|
143
|
-
return JsonPointer.get(propData, ptr);
|
|
144
|
-
} catch (e) {
|
|
145
|
-
if (e.message.startsWith('Invalid reference')) {
|
|
146
|
-
return undefined;
|
|
147
|
-
}
|
|
148
|
-
throw e;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
return propData;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
getModifier(key) {
|
|
155
|
-
return this.modifiers.find(modifier => modifier.key === key);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
applyModifiers(modifiers, str, rawData) {
|
|
159
|
-
try {
|
|
160
|
-
const transformers = modifiers.map(modifier => modifier && modifier.transform);
|
|
161
|
-
return transformers.reduce((str, transform) => (transform ? transform(str, rawData) : str), str);
|
|
162
|
-
} catch (e) {
|
|
163
|
-
console.error(`An error occurred while applying modifiers to ${str}`, modifiers, e);
|
|
164
|
-
return str;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
addAlias(key, ref) {
|
|
169
|
-
if (typeof ref === 'function') {
|
|
170
|
-
this.aliases.push({ key, ref: ref() });
|
|
171
|
-
} else {
|
|
172
|
-
this.aliases.push({ key, ref });
|
|
173
|
-
}
|
|
174
|
-
return this;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
removeAlias(key) {
|
|
178
|
-
this.aliases = this.aliases.filter(alias => alias.key !== key);
|
|
179
|
-
return this;
|
|
180
|
-
}
|
|
181
|
-
}
|
package/src/modifiers/index.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { uppercase } from './uppercase';
|
|
2
|
-
import { lowercase } from './lowercase';
|
|
3
|
-
import { titlecase } from './title';
|
|
4
|
-
|
|
5
|
-
export const defaultModifiers = [
|
|
6
|
-
{
|
|
7
|
-
key: 'uppercase',
|
|
8
|
-
transform: uppercase,
|
|
9
|
-
},
|
|
10
|
-
{
|
|
11
|
-
key: 'lowercase',
|
|
12
|
-
transform: lowercase,
|
|
13
|
-
},
|
|
14
|
-
{
|
|
15
|
-
key: 'title',
|
|
16
|
-
transform: titlecase,
|
|
17
|
-
},
|
|
18
|
-
];
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import { stringInterpolator } from './index';
|
|
2
|
-
import { GraphQLInputType, GraphQLResolveInfo } from 'graphql';
|
|
3
|
-
|
|
4
|
-
export type ResolverData<TParent = any, TArgs = any, TContext = any, TResult = any> = {
|
|
5
|
-
root?: TParent;
|
|
6
|
-
args?: TArgs;
|
|
7
|
-
context?: TContext;
|
|
8
|
-
info?: GraphQLResolveInfo;
|
|
9
|
-
result?: TResult;
|
|
10
|
-
env: Record<string, string>;
|
|
11
|
-
};
|
|
12
|
-
export type ResolverDataBasedFactory<T> = (data: ResolverData) => T;
|
|
13
|
-
|
|
14
|
-
export function getInterpolationKeys(...interpolationStrings: string[]) {
|
|
15
|
-
return interpolationStrings.reduce(
|
|
16
|
-
(keys, str) => [...keys, ...(str ? stringInterpolator.parseRules(str).map((match: any) => match.key) : [])],
|
|
17
|
-
[] as string[]
|
|
18
|
-
);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function parseInterpolationStrings(
|
|
22
|
-
interpolationStrings: Iterable<string>,
|
|
23
|
-
argTypeMap?: Record<string, string | GraphQLInputType>
|
|
24
|
-
) {
|
|
25
|
-
const interpolationKeys = getInterpolationKeys(...interpolationStrings);
|
|
26
|
-
|
|
27
|
-
const args: Record<string, { type: string | GraphQLInputType }> = {};
|
|
28
|
-
const contextVariables: Record<string, string> = {};
|
|
29
|
-
|
|
30
|
-
for (const interpolationKey of interpolationKeys) {
|
|
31
|
-
const interpolationKeyParts = interpolationKey.split('.');
|
|
32
|
-
const varName = interpolationKeyParts[interpolationKeyParts.length - 1];
|
|
33
|
-
const initialObject = interpolationKeyParts[0];
|
|
34
|
-
const argType =
|
|
35
|
-
argTypeMap && varName in argTypeMap ? argTypeMap[varName] : interpolationKeyParts.length > 2 ? 'JSON' : 'ID';
|
|
36
|
-
switch (initialObject) {
|
|
37
|
-
case 'args':
|
|
38
|
-
args[varName] = {
|
|
39
|
-
type: argType,
|
|
40
|
-
};
|
|
41
|
-
break;
|
|
42
|
-
case 'context':
|
|
43
|
-
contextVariables[varName] = `Scalars['${argType}']`;
|
|
44
|
-
break;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return {
|
|
49
|
-
args,
|
|
50
|
-
contextVariables,
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export function getInterpolatedStringFactory(nonInterpolatedString: string): ResolverDataBasedFactory<string> {
|
|
55
|
-
return resolverData => stringInterpolator.parse(nonInterpolatedString, resolverData);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export function getInterpolatedHeadersFactory(
|
|
59
|
-
nonInterpolatedHeaders: Record<string, string> = {}
|
|
60
|
-
): ResolverDataBasedFactory<Record<string, string>> {
|
|
61
|
-
return resolverData => {
|
|
62
|
-
const headers: Record<string, string> = {};
|
|
63
|
-
for (const headerName in nonInterpolatedHeaders) {
|
|
64
|
-
const headerValue = nonInterpolatedHeaders[headerName];
|
|
65
|
-
if (headerValue) {
|
|
66
|
-
headers[headerName.toLowerCase()] = stringInterpolator.parse(headerValue, resolverData);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
return headers;
|
|
70
|
-
};
|
|
71
|
-
}
|