@graphql-mesh/string-interpolation 0.1.0 → 0.1.1-alpha-77a22b738.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 CHANGED
@@ -1,5 +1,11 @@
1
1
  # @graphql-mesh/string-interpolation
2
2
 
3
+ ## 0.1.1-alpha-77a22b738.0
4
+
5
+ ### Patch Changes
6
+
7
+ - 7ceb64fab: Cleanup dependencies
8
+
3
9
  ## 0.1.0
4
10
 
5
11
  ### Minor Changes
package/dist/index.d.ts CHANGED
@@ -1,2 +1,5 @@
1
1
  import { Interpolator } from './interpolator';
2
+ export declare function hashObject(value: any): string;
2
3
  export { Interpolator };
4
+ export declare const stringInterpolator: Interpolator;
5
+ export * from './resolver-data-factory';
package/dist/index.js CHANGED
@@ -6,6 +6,8 @@ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'defau
6
6
 
7
7
  const _ = _interopDefault(require('lodash'));
8
8
  const JsonPointer = _interopDefault(require('json-pointer'));
9
+ const dayjs = _interopDefault(require('dayjs'));
10
+ const objectHash = _interopDefault(require('object-hash'));
9
11
 
10
12
  const defaultOptions = {
11
13
  delimiter: ['{', '}'],
@@ -17,7 +19,8 @@ const lowercase = value => value.toLowerCase();
17
19
 
18
20
  const titlecase = value => value.replace(/\w\S*/g, s => s.charAt(0).toUpperCase() + s.substr(1).toLowerCase());
19
21
 
20
- const defaultModifiers = [{
22
+ const defaultModifiers = [
23
+ {
21
24
  key: 'uppercase',
22
25
  transform: uppercase,
23
26
  },
@@ -28,7 +31,8 @@ const defaultModifiers = [{
28
31
  {
29
32
  key: 'title',
30
33
  transform: titlecase,
31
- }];
34
+ },
35
+ ];
32
36
 
33
37
  class Interpolator {
34
38
  constructor(options = defaultOptions) {
@@ -68,20 +72,20 @@ class Interpolator {
68
72
  return matches ? this.extractRules(matches) : [];
69
73
  }
70
74
  extractRules(matches) {
71
- return matches.map((match) => {
75
+ return matches.map(match => {
72
76
  const alternativeText = this.getAlternativeText(match);
73
77
  const modifiers = this.getModifiers(match);
74
78
  return {
75
79
  key: this.getKeyFromMatch(match),
76
80
  replace: match,
77
81
  modifiers,
78
- alternativeText
82
+ alternativeText,
79
83
  };
80
84
  });
81
85
  }
82
86
  getKeyFromMatch(match) {
83
87
  const removeReservedSymbols = [':', '|'];
84
- return this.removeDelimiter(removeReservedSymbols.reduce((val, sym) => val.indexOf(sym) > 0 ? this.removeAfter(val, sym) : val, match));
88
+ return this.removeDelimiter(removeReservedSymbols.reduce((val, sym) => (val.indexOf(sym) > 0 ? this.removeAfter(val, sym) : val), match));
85
89
  }
86
90
  removeDelimiter(val) {
87
91
  return val.replace(new RegExp(this.delimiterStart(), 'g'), '').replace(new RegExp(this.delimiterEnd(), 'g'), '');
@@ -158,7 +162,7 @@ class Interpolator {
158
162
  applyModifiers(modifiers, str, rawData) {
159
163
  try {
160
164
  const transformers = modifiers.map(modifier => modifier && modifier.transform);
161
- return transformers.reduce((str, transform) => transform ? transform(str, rawData) : str, str);
165
+ return transformers.reduce((str, transform) => (transform ? transform(str, rawData) : str), str);
162
166
  }
163
167
  catch (e) {
164
168
  console.error(`An error occurred while applying modifiers to ${str}`, modifiers, e);
@@ -180,4 +184,72 @@ class Interpolator {
180
184
  }
181
185
  }
182
186
 
187
+ function getInterpolationKeys(...interpolationStrings) {
188
+ return interpolationStrings.reduce((keys, str) => [...keys, ...(str ? stringInterpolator.parseRules(str).map((match) => match.key) : [])], []);
189
+ }
190
+ function parseInterpolationStrings(interpolationStrings, argTypeMap) {
191
+ const interpolationKeys = getInterpolationKeys(...interpolationStrings);
192
+ const args = {};
193
+ const contextVariables = [];
194
+ for (const interpolationKey of interpolationKeys) {
195
+ const interpolationKeyParts = interpolationKey.split('.');
196
+ const varName = interpolationKeyParts[interpolationKeyParts.length - 1];
197
+ if (interpolationKeyParts[0] === 'args') {
198
+ const argType = argTypeMap && varName in argTypeMap ? argTypeMap[varName] : 'ID';
199
+ args[varName] = {
200
+ type: argType,
201
+ };
202
+ }
203
+ else if (interpolationKeyParts[0] === 'context') {
204
+ contextVariables.push(varName);
205
+ }
206
+ }
207
+ return {
208
+ args,
209
+ contextVariables,
210
+ };
211
+ }
212
+ function getInterpolatedStringFactory(nonInterpolatedString) {
213
+ return resolverData => stringInterpolator.parse(nonInterpolatedString, resolverData);
214
+ }
215
+ function getInterpolatedHeadersFactory(nonInterpolatedHeaders = {}) {
216
+ return resolverData => {
217
+ const headers = {};
218
+ for (const headerName in nonInterpolatedHeaders) {
219
+ const headerValue = nonInterpolatedHeaders[headerName];
220
+ if (headerValue) {
221
+ headers[headerName.toLowerCase()] = stringInterpolator.parse(headerValue, resolverData);
222
+ }
223
+ }
224
+ return headers;
225
+ };
226
+ }
227
+
228
+ function hashObject(value) {
229
+ return objectHash(value, { ignoreUnknown: true }).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,7 @@
1
1
  import _ from 'lodash';
2
2
  import JsonPointer from 'json-pointer';
3
+ import dayjs from 'dayjs';
4
+ import objectHash from 'object-hash';
3
5
 
4
6
  const defaultOptions = {
5
7
  delimiter: ['{', '}'],
@@ -11,7 +13,8 @@ const lowercase = value => value.toLowerCase();
11
13
 
12
14
  const titlecase = value => value.replace(/\w\S*/g, s => s.charAt(0).toUpperCase() + s.substr(1).toLowerCase());
13
15
 
14
- const defaultModifiers = [{
16
+ const defaultModifiers = [
17
+ {
15
18
  key: 'uppercase',
16
19
  transform: uppercase,
17
20
  },
@@ -22,7 +25,8 @@ const defaultModifiers = [{
22
25
  {
23
26
  key: 'title',
24
27
  transform: titlecase,
25
- }];
28
+ },
29
+ ];
26
30
 
27
31
  class Interpolator {
28
32
  constructor(options = defaultOptions) {
@@ -62,20 +66,20 @@ class Interpolator {
62
66
  return matches ? this.extractRules(matches) : [];
63
67
  }
64
68
  extractRules(matches) {
65
- return matches.map((match) => {
69
+ return matches.map(match => {
66
70
  const alternativeText = this.getAlternativeText(match);
67
71
  const modifiers = this.getModifiers(match);
68
72
  return {
69
73
  key: this.getKeyFromMatch(match),
70
74
  replace: match,
71
75
  modifiers,
72
- alternativeText
76
+ alternativeText,
73
77
  };
74
78
  });
75
79
  }
76
80
  getKeyFromMatch(match) {
77
81
  const removeReservedSymbols = [':', '|'];
78
- return this.removeDelimiter(removeReservedSymbols.reduce((val, sym) => val.indexOf(sym) > 0 ? this.removeAfter(val, sym) : val, match));
82
+ return this.removeDelimiter(removeReservedSymbols.reduce((val, sym) => (val.indexOf(sym) > 0 ? this.removeAfter(val, sym) : val), match));
79
83
  }
80
84
  removeDelimiter(val) {
81
85
  return val.replace(new RegExp(this.delimiterStart(), 'g'), '').replace(new RegExp(this.delimiterEnd(), 'g'), '');
@@ -152,7 +156,7 @@ class Interpolator {
152
156
  applyModifiers(modifiers, str, rawData) {
153
157
  try {
154
158
  const transformers = modifiers.map(modifier => modifier && modifier.transform);
155
- return transformers.reduce((str, transform) => transform ? transform(str, rawData) : str, str);
159
+ return transformers.reduce((str, transform) => (transform ? transform(str, rawData) : str), str);
156
160
  }
157
161
  catch (e) {
158
162
  console.error(`An error occurred while applying modifiers to ${str}`, modifiers, e);
@@ -174,4 +178,66 @@ class Interpolator {
174
178
  }
175
179
  }
176
180
 
177
- export { Interpolator };
181
+ function getInterpolationKeys(...interpolationStrings) {
182
+ return interpolationStrings.reduce((keys, str) => [...keys, ...(str ? stringInterpolator.parseRules(str).map((match) => match.key) : [])], []);
183
+ }
184
+ function parseInterpolationStrings(interpolationStrings, argTypeMap) {
185
+ const interpolationKeys = getInterpolationKeys(...interpolationStrings);
186
+ const args = {};
187
+ const contextVariables = [];
188
+ for (const interpolationKey of interpolationKeys) {
189
+ const interpolationKeyParts = interpolationKey.split('.');
190
+ const varName = interpolationKeyParts[interpolationKeyParts.length - 1];
191
+ if (interpolationKeyParts[0] === 'args') {
192
+ const argType = argTypeMap && varName in argTypeMap ? argTypeMap[varName] : 'ID';
193
+ args[varName] = {
194
+ type: argType,
195
+ };
196
+ }
197
+ else if (interpolationKeyParts[0] === 'context') {
198
+ contextVariables.push(varName);
199
+ }
200
+ }
201
+ return {
202
+ args,
203
+ contextVariables,
204
+ };
205
+ }
206
+ function getInterpolatedStringFactory(nonInterpolatedString) {
207
+ return resolverData => stringInterpolator.parse(nonInterpolatedString, resolverData);
208
+ }
209
+ function getInterpolatedHeadersFactory(nonInterpolatedHeaders = {}) {
210
+ return resolverData => {
211
+ const headers = {};
212
+ for (const headerName in nonInterpolatedHeaders) {
213
+ const headerValue = nonInterpolatedHeaders[headerName];
214
+ if (headerValue) {
215
+ headers[headerName.toLowerCase()] = stringInterpolator.parse(headerValue, resolverData);
216
+ }
217
+ }
218
+ return headers;
219
+ };
220
+ }
221
+
222
+ function hashObject(value) {
223
+ return objectHash(value, { ignoreUnknown: true }).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,16 @@
1
1
  {
2
2
  "name": "@graphql-mesh/string-interpolation",
3
- "version": "0.1.0",
3
+ "version": "0.1.1-alpha-77a22b738.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": "^4.17.21"
12
+ "lodash": "^4.17.21",
13
+ "object-hash": "3.0.0"
9
14
  },
10
15
  "repository": {
11
16
  "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.1.0",
3
+ "version": "0.1.1-alpha-77a22b738.0",
4
4
  "description": "Dynamic string manipulation",
5
5
  "sideEffects": false,
6
6
  "main": "dist/index.js",
@@ -19,8 +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",
27
+ "dayjs": "1.11.2",
28
+ "object-hash": "3.0.0",
24
29
  "lodash": "^4.17.21"
25
30
  },
26
31
  "author": "Arda TANRIKULU <ardatanrikulu@gmail.com>",
package/src/index.ts CHANGED
@@ -1,3 +1,29 @@
1
1
  import { Interpolator } from './interpolator';
2
+ import dayjs from 'dayjs';
3
+ import objectHash from 'object-hash';
4
+
5
+ export function hashObject(value: any): string {
6
+ return objectHash(value, { ignoreUnknown: true }).toString();
7
+ }
2
8
 
3
9
  export { Interpolator };
10
+
11
+ export const stringInterpolator = new Interpolator({
12
+ delimiter: ['{', '}'],
13
+ });
14
+
15
+ stringInterpolator.addAlias('typeName', 'info.parentType.name');
16
+ stringInterpolator.addAlias('type', 'info.parentType.name');
17
+ stringInterpolator.addAlias('parentType', 'info.parentType.name');
18
+ stringInterpolator.addAlias('fieldName', 'info.fieldName');
19
+ stringInterpolator.registerModifier('date', (formatStr: string) => dayjs(new Date()).format(formatStr));
20
+ stringInterpolator.registerModifier('hash', (value: any) => hashObject(value));
21
+ stringInterpolator.registerModifier('base64', (value: any) => {
22
+ if (globalThis.Buffer.from) {
23
+ return globalThis.Buffer.from(value).toString('base64');
24
+ } else {
25
+ return btoa(value);
26
+ }
27
+ });
28
+
29
+ export * from './resolver-data-factory';
@@ -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((match) => {
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(removeReservedSymbols.reduce((val, sym) => val.indexOf(sym) > 0 ? this.removeAfter(val, sym) : val, match));
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) {
@@ -138,7 +140,7 @@ export class Interpolator {
138
140
  const propData = _.get(data, prop);
139
141
  if (ptr) {
140
142
  try {
141
- return JsonPointer.get(propData, ptr);
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
  }
@@ -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
- key: 'uppercase',
7
- transform: uppercase,
8
- },
9
- {
10
- key: 'lowercase',
11
- transform: lowercase,
12
- },
13
- {
14
- key: 'title',
15
- transform: titlecase,
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
+ }
@@ -1,3 +1,3 @@
1
1
  export const defaultOptions = {
2
2
  delimiter: ['{', '}'],
3
- }
3
+ };