@graphql-mesh/string-interpolation 0.1.0 → 0.2.0
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/CHANGELOG.md +11 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +80 -8
- package/dist/index.mjs +75 -9
- package/dist/package.json +6 -2
- package/dist/resolver-data-factory.d.ts +19 -0
- package/package.json +6 -2
- package/src/index.ts +27 -0
- package/src/interpolator.js +14 -12
- package/src/modifiers/index.js +14 -12
- package/src/resolver-data-factory.ts +66 -0
- package/src/statics/DefaultOptions.js +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
# @graphql-mesh/string-interpolation
|
|
2
2
|
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 974e703e2: No longer import entire lodash library but instead individual smaller packages
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- 974e703e2: Cleanup dependencies
|
|
12
|
+
- 974e703e2: Use deeper lodash imports to have better treeshaking and avoid using eval
|
|
13
|
+
|
|
3
14
|
## 0.1.0
|
|
4
15
|
|
|
5
16
|
### Minor Changes
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -4,8 +4,9 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
|
6
6
|
|
|
7
|
-
const
|
|
7
|
+
const lodashGet = _interopDefault(require('lodash.get'));
|
|
8
8
|
const JsonPointer = _interopDefault(require('json-pointer'));
|
|
9
|
+
const dayjs = _interopDefault(require('dayjs'));
|
|
9
10
|
|
|
10
11
|
const defaultOptions = {
|
|
11
12
|
delimiter: ['{', '}'],
|
|
@@ -17,7 +18,8 @@ const lowercase = value => value.toLowerCase();
|
|
|
17
18
|
|
|
18
19
|
const titlecase = value => value.replace(/\w\S*/g, s => s.charAt(0).toUpperCase() + s.substr(1).toLowerCase());
|
|
19
20
|
|
|
20
|
-
const defaultModifiers = [
|
|
21
|
+
const defaultModifiers = [
|
|
22
|
+
{
|
|
21
23
|
key: 'uppercase',
|
|
22
24
|
transform: uppercase,
|
|
23
25
|
},
|
|
@@ -28,7 +30,8 @@ const defaultModifiers = [{
|
|
|
28
30
|
{
|
|
29
31
|
key: 'title',
|
|
30
32
|
transform: titlecase,
|
|
31
|
-
}
|
|
33
|
+
},
|
|
34
|
+
];
|
|
32
35
|
|
|
33
36
|
class Interpolator {
|
|
34
37
|
constructor(options = defaultOptions) {
|
|
@@ -68,20 +71,20 @@ class Interpolator {
|
|
|
68
71
|
return matches ? this.extractRules(matches) : [];
|
|
69
72
|
}
|
|
70
73
|
extractRules(matches) {
|
|
71
|
-
return matches.map(
|
|
74
|
+
return matches.map(match => {
|
|
72
75
|
const alternativeText = this.getAlternativeText(match);
|
|
73
76
|
const modifiers = this.getModifiers(match);
|
|
74
77
|
return {
|
|
75
78
|
key: this.getKeyFromMatch(match),
|
|
76
79
|
replace: match,
|
|
77
80
|
modifiers,
|
|
78
|
-
alternativeText
|
|
81
|
+
alternativeText,
|
|
79
82
|
};
|
|
80
83
|
});
|
|
81
84
|
}
|
|
82
85
|
getKeyFromMatch(match) {
|
|
83
86
|
const removeReservedSymbols = [':', '|'];
|
|
84
|
-
return this.removeDelimiter(removeReservedSymbols.reduce((val, sym) => val.indexOf(sym) > 0 ? this.removeAfter(val, sym) : val, match));
|
|
87
|
+
return this.removeDelimiter(removeReservedSymbols.reduce((val, sym) => (val.indexOf(sym) > 0 ? this.removeAfter(val, sym) : val), match));
|
|
85
88
|
}
|
|
86
89
|
removeDelimiter(val) {
|
|
87
90
|
return val.replace(new RegExp(this.delimiterStart(), 'g'), '').replace(new RegExp(this.delimiterEnd(), 'g'), '');
|
|
@@ -138,7 +141,7 @@ class Interpolator {
|
|
|
138
141
|
}
|
|
139
142
|
applyData(key, data) {
|
|
140
143
|
const [prop, ptr] = key.split('#');
|
|
141
|
-
const propData =
|
|
144
|
+
const propData = lodashGet(data, prop);
|
|
142
145
|
if (ptr) {
|
|
143
146
|
try {
|
|
144
147
|
return JsonPointer.get(propData, ptr);
|
|
@@ -158,7 +161,7 @@ class Interpolator {
|
|
|
158
161
|
applyModifiers(modifiers, str, rawData) {
|
|
159
162
|
try {
|
|
160
163
|
const transformers = modifiers.map(modifier => modifier && modifier.transform);
|
|
161
|
-
return transformers.reduce((str, transform) => transform ? transform(str, rawData) : str, str);
|
|
164
|
+
return transformers.reduce((str, transform) => (transform ? transform(str, rawData) : str), str);
|
|
162
165
|
}
|
|
163
166
|
catch (e) {
|
|
164
167
|
console.error(`An error occurred while applying modifiers to ${str}`, modifiers, e);
|
|
@@ -180,4 +183,73 @@ class Interpolator {
|
|
|
180
183
|
}
|
|
181
184
|
}
|
|
182
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
|
+
if (interpolationKeyParts[0] === 'args') {
|
|
197
|
+
const argType = argTypeMap && varName in argTypeMap ? argTypeMap[varName] : 'ID';
|
|
198
|
+
args[varName] = {
|
|
199
|
+
type: argType,
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
else if (interpolationKeyParts[0] === 'context') {
|
|
203
|
+
contextVariables.push(varName);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return {
|
|
207
|
+
args,
|
|
208
|
+
contextVariables,
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
function getInterpolatedStringFactory(nonInterpolatedString) {
|
|
212
|
+
return resolverData => stringInterpolator.parse(nonInterpolatedString, resolverData);
|
|
213
|
+
}
|
|
214
|
+
function getInterpolatedHeadersFactory(nonInterpolatedHeaders = {}) {
|
|
215
|
+
return resolverData => {
|
|
216
|
+
const headers = {};
|
|
217
|
+
for (const headerName in nonInterpolatedHeaders) {
|
|
218
|
+
const headerValue = nonInterpolatedHeaders[headerName];
|
|
219
|
+
if (headerValue) {
|
|
220
|
+
headers[headerName.toLowerCase()] = stringInterpolator.parse(headerValue, resolverData);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return headers;
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const hashCode = (s) => s.split('').reduce((a, b) => ((a << 5) - a + b.charCodeAt(0)) | 0, 0);
|
|
228
|
+
function hashObject(value) {
|
|
229
|
+
return hashCode(JSON.stringify(value)).toString();
|
|
230
|
+
}
|
|
231
|
+
const stringInterpolator = new Interpolator({
|
|
232
|
+
delimiter: ['{', '}'],
|
|
233
|
+
});
|
|
234
|
+
stringInterpolator.addAlias('typeName', 'info.parentType.name');
|
|
235
|
+
stringInterpolator.addAlias('type', 'info.parentType.name');
|
|
236
|
+
stringInterpolator.addAlias('parentType', 'info.parentType.name');
|
|
237
|
+
stringInterpolator.addAlias('fieldName', 'info.fieldName');
|
|
238
|
+
stringInterpolator.registerModifier('date', (formatStr) => dayjs(new Date()).format(formatStr));
|
|
239
|
+
stringInterpolator.registerModifier('hash', (value) => hashObject(value));
|
|
240
|
+
stringInterpolator.registerModifier('base64', (value) => {
|
|
241
|
+
if (globalThis.Buffer.from) {
|
|
242
|
+
return globalThis.Buffer.from(value).toString('base64');
|
|
243
|
+
}
|
|
244
|
+
else {
|
|
245
|
+
return btoa(value);
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
|
|
183
249
|
exports.Interpolator = Interpolator;
|
|
250
|
+
exports.getInterpolatedHeadersFactory = getInterpolatedHeadersFactory;
|
|
251
|
+
exports.getInterpolatedStringFactory = getInterpolatedStringFactory;
|
|
252
|
+
exports.getInterpolationKeys = getInterpolationKeys;
|
|
253
|
+
exports.hashObject = hashObject;
|
|
254
|
+
exports.parseInterpolationStrings = parseInterpolationStrings;
|
|
255
|
+
exports.stringInterpolator = stringInterpolator;
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import lodashGet from 'lodash.get';
|
|
2
2
|
import JsonPointer from 'json-pointer';
|
|
3
|
+
import dayjs from 'dayjs';
|
|
3
4
|
|
|
4
5
|
const defaultOptions = {
|
|
5
6
|
delimiter: ['{', '}'],
|
|
@@ -11,7 +12,8 @@ const lowercase = value => value.toLowerCase();
|
|
|
11
12
|
|
|
12
13
|
const titlecase = value => value.replace(/\w\S*/g, s => s.charAt(0).toUpperCase() + s.substr(1).toLowerCase());
|
|
13
14
|
|
|
14
|
-
const defaultModifiers = [
|
|
15
|
+
const defaultModifiers = [
|
|
16
|
+
{
|
|
15
17
|
key: 'uppercase',
|
|
16
18
|
transform: uppercase,
|
|
17
19
|
},
|
|
@@ -22,7 +24,8 @@ const defaultModifiers = [{
|
|
|
22
24
|
{
|
|
23
25
|
key: 'title',
|
|
24
26
|
transform: titlecase,
|
|
25
|
-
}
|
|
27
|
+
},
|
|
28
|
+
];
|
|
26
29
|
|
|
27
30
|
class Interpolator {
|
|
28
31
|
constructor(options = defaultOptions) {
|
|
@@ -62,20 +65,20 @@ class Interpolator {
|
|
|
62
65
|
return matches ? this.extractRules(matches) : [];
|
|
63
66
|
}
|
|
64
67
|
extractRules(matches) {
|
|
65
|
-
return matches.map(
|
|
68
|
+
return matches.map(match => {
|
|
66
69
|
const alternativeText = this.getAlternativeText(match);
|
|
67
70
|
const modifiers = this.getModifiers(match);
|
|
68
71
|
return {
|
|
69
72
|
key: this.getKeyFromMatch(match),
|
|
70
73
|
replace: match,
|
|
71
74
|
modifiers,
|
|
72
|
-
alternativeText
|
|
75
|
+
alternativeText,
|
|
73
76
|
};
|
|
74
77
|
});
|
|
75
78
|
}
|
|
76
79
|
getKeyFromMatch(match) {
|
|
77
80
|
const removeReservedSymbols = [':', '|'];
|
|
78
|
-
return this.removeDelimiter(removeReservedSymbols.reduce((val, sym) => val.indexOf(sym) > 0 ? this.removeAfter(val, sym) : val, match));
|
|
81
|
+
return this.removeDelimiter(removeReservedSymbols.reduce((val, sym) => (val.indexOf(sym) > 0 ? this.removeAfter(val, sym) : val), match));
|
|
79
82
|
}
|
|
80
83
|
removeDelimiter(val) {
|
|
81
84
|
return val.replace(new RegExp(this.delimiterStart(), 'g'), '').replace(new RegExp(this.delimiterEnd(), 'g'), '');
|
|
@@ -132,7 +135,7 @@ class Interpolator {
|
|
|
132
135
|
}
|
|
133
136
|
applyData(key, data) {
|
|
134
137
|
const [prop, ptr] = key.split('#');
|
|
135
|
-
const propData =
|
|
138
|
+
const propData = lodashGet(data, prop);
|
|
136
139
|
if (ptr) {
|
|
137
140
|
try {
|
|
138
141
|
return JsonPointer.get(propData, ptr);
|
|
@@ -152,7 +155,7 @@ class Interpolator {
|
|
|
152
155
|
applyModifiers(modifiers, str, rawData) {
|
|
153
156
|
try {
|
|
154
157
|
const transformers = modifiers.map(modifier => modifier && modifier.transform);
|
|
155
|
-
return transformers.reduce((str, transform) => transform ? transform(str, rawData) : str, str);
|
|
158
|
+
return transformers.reduce((str, transform) => (transform ? transform(str, rawData) : str), str);
|
|
156
159
|
}
|
|
157
160
|
catch (e) {
|
|
158
161
|
console.error(`An error occurred while applying modifiers to ${str}`, modifiers, e);
|
|
@@ -174,4 +177,67 @@ class Interpolator {
|
|
|
174
177
|
}
|
|
175
178
|
}
|
|
176
179
|
|
|
177
|
-
|
|
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
|
+
if (interpolationKeyParts[0] === 'args') {
|
|
191
|
+
const argType = argTypeMap && varName in argTypeMap ? argTypeMap[varName] : 'ID';
|
|
192
|
+
args[varName] = {
|
|
193
|
+
type: argType,
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
else if (interpolationKeyParts[0] === 'context') {
|
|
197
|
+
contextVariables.push(varName);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return {
|
|
201
|
+
args,
|
|
202
|
+
contextVariables,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
function getInterpolatedStringFactory(nonInterpolatedString) {
|
|
206
|
+
return resolverData => stringInterpolator.parse(nonInterpolatedString, resolverData);
|
|
207
|
+
}
|
|
208
|
+
function getInterpolatedHeadersFactory(nonInterpolatedHeaders = {}) {
|
|
209
|
+
return resolverData => {
|
|
210
|
+
const headers = {};
|
|
211
|
+
for (const headerName in nonInterpolatedHeaders) {
|
|
212
|
+
const headerValue = nonInterpolatedHeaders[headerName];
|
|
213
|
+
if (headerValue) {
|
|
214
|
+
headers[headerName.toLowerCase()] = stringInterpolator.parse(headerValue, resolverData);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return headers;
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const hashCode = (s) => s.split('').reduce((a, b) => ((a << 5) - a + b.charCodeAt(0)) | 0, 0);
|
|
222
|
+
function hashObject(value) {
|
|
223
|
+
return hashCode(JSON.stringify(value)).toString();
|
|
224
|
+
}
|
|
225
|
+
const stringInterpolator = new Interpolator({
|
|
226
|
+
delimiter: ['{', '}'],
|
|
227
|
+
});
|
|
228
|
+
stringInterpolator.addAlias('typeName', 'info.parentType.name');
|
|
229
|
+
stringInterpolator.addAlias('type', 'info.parentType.name');
|
|
230
|
+
stringInterpolator.addAlias('parentType', 'info.parentType.name');
|
|
231
|
+
stringInterpolator.addAlias('fieldName', 'info.fieldName');
|
|
232
|
+
stringInterpolator.registerModifier('date', (formatStr) => dayjs(new Date()).format(formatStr));
|
|
233
|
+
stringInterpolator.registerModifier('hash', (value) => hashObject(value));
|
|
234
|
+
stringInterpolator.registerModifier('base64', (value) => {
|
|
235
|
+
if (globalThis.Buffer.from) {
|
|
236
|
+
return globalThis.Buffer.from(value).toString('base64');
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
return btoa(value);
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
export { Interpolator, getInterpolatedHeadersFactory, getInterpolatedStringFactory, getInterpolationKeys, hashObject, parseInterpolationStrings, stringInterpolator };
|
package/dist/package.json
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-mesh/string-interpolation",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Dynamic string manipulation",
|
|
5
5
|
"sideEffects": false,
|
|
6
|
+
"peerDependencies": {
|
|
7
|
+
"graphql": "*"
|
|
8
|
+
},
|
|
6
9
|
"dependencies": {
|
|
10
|
+
"dayjs": "1.11.2",
|
|
7
11
|
"json-pointer": "0.6.2",
|
|
8
|
-
"lodash": "
|
|
12
|
+
"lodash.get": "4.4.2"
|
|
9
13
|
},
|
|
10
14
|
"repository": {
|
|
11
15
|
"type": "git",
|
|
@@ -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: string[], argTypeMap?: Record<string, string | GraphQLInputType>): {
|
|
13
|
+
args: Record<string, {
|
|
14
|
+
type: string | GraphQLInputType;
|
|
15
|
+
}>;
|
|
16
|
+
contextVariables: string[];
|
|
17
|
+
};
|
|
18
|
+
export declare function getInterpolatedStringFactory(nonInterpolatedString: string): ResolverDataBasedFactory<string>;
|
|
19
|
+
export declare function getInterpolatedHeadersFactory(nonInterpolatedHeaders?: Record<string, string>): ResolverDataBasedFactory<Record<string, string>>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-mesh/string-interpolation",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Dynamic string manipulation",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -19,9 +19,13 @@
|
|
|
19
19
|
"import": "./dist/*.mjs"
|
|
20
20
|
}
|
|
21
21
|
},
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"graphql": "*"
|
|
24
|
+
},
|
|
22
25
|
"dependencies": {
|
|
23
26
|
"json-pointer": "0.6.2",
|
|
24
|
-
"
|
|
27
|
+
"dayjs": "1.11.2",
|
|
28
|
+
"lodash.get": "4.4.2"
|
|
25
29
|
},
|
|
26
30
|
"author": "Arda TANRIKULU <ardatanrikulu@gmail.com>",
|
|
27
31
|
"repository": {
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,30 @@
|
|
|
1
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
|
+
}
|
|
2
9
|
|
|
3
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
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { defaultOptions } from './statics/DefaultOptions';
|
|
2
|
-
import
|
|
2
|
+
import lodashGet from 'lodash.get';
|
|
3
3
|
import { defaultModifiers } from './modifiers';
|
|
4
4
|
import JsonPointer from 'json-pointer';
|
|
5
5
|
|
|
@@ -37,7 +37,7 @@ export class Interpolator {
|
|
|
37
37
|
return new Error('Modifiers must have a transformer. Transformers must be a function that returns a value.');
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
this.modifiers.push({key: key.toLowerCase(), transform});
|
|
40
|
+
this.modifiers.push({ key: key.toLowerCase(), transform });
|
|
41
41
|
return this;
|
|
42
42
|
}
|
|
43
43
|
|
|
@@ -51,21 +51,23 @@ export class Interpolator {
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
extractRules(matches) {
|
|
54
|
-
return matches.map(
|
|
54
|
+
return matches.map(match => {
|
|
55
55
|
const alternativeText = this.getAlternativeText(match);
|
|
56
56
|
const modifiers = this.getModifiers(match);
|
|
57
57
|
return {
|
|
58
58
|
key: this.getKeyFromMatch(match),
|
|
59
59
|
replace: match,
|
|
60
60
|
modifiers,
|
|
61
|
-
alternativeText
|
|
62
|
-
}
|
|
63
|
-
})
|
|
61
|
+
alternativeText,
|
|
62
|
+
};
|
|
63
|
+
});
|
|
64
64
|
}
|
|
65
65
|
|
|
66
66
|
getKeyFromMatch(match) {
|
|
67
67
|
const removeReservedSymbols = [':', '|'];
|
|
68
|
-
return this.removeDelimiter(
|
|
68
|
+
return this.removeDelimiter(
|
|
69
|
+
removeReservedSymbols.reduce((val, sym) => (val.indexOf(sym) > 0 ? this.removeAfter(val, sym) : val), match)
|
|
70
|
+
);
|
|
69
71
|
}
|
|
70
72
|
|
|
71
73
|
removeDelimiter(val) {
|
|
@@ -135,10 +137,10 @@ export class Interpolator {
|
|
|
135
137
|
|
|
136
138
|
applyData(key, data) {
|
|
137
139
|
const [prop, ptr] = key.split('#');
|
|
138
|
-
const propData =
|
|
140
|
+
const propData = lodashGet(data, prop);
|
|
139
141
|
if (ptr) {
|
|
140
142
|
try {
|
|
141
|
-
|
|
143
|
+
return JsonPointer.get(propData, ptr);
|
|
142
144
|
} catch (e) {
|
|
143
145
|
if (e.message.startsWith('Invalid reference')) {
|
|
144
146
|
return undefined;
|
|
@@ -156,7 +158,7 @@ export class Interpolator {
|
|
|
156
158
|
applyModifiers(modifiers, str, rawData) {
|
|
157
159
|
try {
|
|
158
160
|
const transformers = modifiers.map(modifier => modifier && modifier.transform);
|
|
159
|
-
return transformers.reduce((str, transform) => transform ? transform(str, rawData) : str, str);
|
|
161
|
+
return transformers.reduce((str, transform) => (transform ? transform(str, rawData) : str), str);
|
|
160
162
|
} catch (e) {
|
|
161
163
|
console.error(`An error occurred while applying modifiers to ${str}`, modifiers, e);
|
|
162
164
|
return str;
|
|
@@ -164,8 +166,8 @@ export class Interpolator {
|
|
|
164
166
|
}
|
|
165
167
|
|
|
166
168
|
addAlias(key, ref) {
|
|
167
|
-
if (typeof ref === 'function'){
|
|
168
|
-
this.aliases.push({key, ref: ref() });
|
|
169
|
+
if (typeof ref === 'function') {
|
|
170
|
+
this.aliases.push({ key, ref: ref() });
|
|
169
171
|
} else {
|
|
170
172
|
this.aliases.push({ key, ref });
|
|
171
173
|
}
|
package/src/modifiers/index.js
CHANGED
|
@@ -2,15 +2,17 @@ import { uppercase } from './uppercase';
|
|
|
2
2
|
import { lowercase } from './lowercase';
|
|
3
3
|
import { titlecase } from './title';
|
|
4
4
|
|
|
5
|
-
export const defaultModifiers = [
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
+
];
|
|
@@ -0,0 +1,66 @@
|
|
|
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: 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: string[] = [];
|
|
29
|
+
|
|
30
|
+
for (const interpolationKey of interpolationKeys) {
|
|
31
|
+
const interpolationKeyParts = interpolationKey.split('.');
|
|
32
|
+
const varName = interpolationKeyParts[interpolationKeyParts.length - 1];
|
|
33
|
+
if (interpolationKeyParts[0] === 'args') {
|
|
34
|
+
const argType = argTypeMap && varName in argTypeMap ? argTypeMap[varName] : 'ID';
|
|
35
|
+
args[varName] = {
|
|
36
|
+
type: argType,
|
|
37
|
+
};
|
|
38
|
+
} else if (interpolationKeyParts[0] === 'context') {
|
|
39
|
+
contextVariables.push(varName);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return {
|
|
44
|
+
args,
|
|
45
|
+
contextVariables,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function getInterpolatedStringFactory(nonInterpolatedString: string): ResolverDataBasedFactory<string> {
|
|
50
|
+
return resolverData => stringInterpolator.parse(nonInterpolatedString, resolverData);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function getInterpolatedHeadersFactory(
|
|
54
|
+
nonInterpolatedHeaders: Record<string, string> = {}
|
|
55
|
+
): ResolverDataBasedFactory<Record<string, string>> {
|
|
56
|
+
return resolverData => {
|
|
57
|
+
const headers: Record<string, string> = {};
|
|
58
|
+
for (const headerName in nonInterpolatedHeaders) {
|
|
59
|
+
const headerValue = nonInterpolatedHeaders[headerName];
|
|
60
|
+
if (headerValue) {
|
|
61
|
+
headers[headerName.toLowerCase()] = stringInterpolator.parse(headerValue, resolverData);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return headers;
|
|
65
|
+
};
|
|
66
|
+
}
|