@graphql-mesh/string-interpolation
Advanced tools
Comparing version 0.1.0 to 0.1.1-alpha-77a22b738.0
# @graphql-mesh/string-interpolation | ||
## 0.1.1-alpha-77a22b738.0 | ||
### Patch Changes | ||
- 7ceb64fab: Cleanup dependencies | ||
## 0.1.0 | ||
@@ -4,0 +10,0 @@ |
import { Interpolator } from './interpolator'; | ||
export declare function hashObject(value: any): string; | ||
export { Interpolator }; | ||
export declare const stringInterpolator: Interpolator; | ||
export * from './resolver-data-factory'; |
@@ -9,2 +9,4 @@ 'use strict'; | ||
const JsonPointer = _interopDefault(require('json-pointer')); | ||
const dayjs = _interopDefault(require('dayjs')); | ||
const objectHash = _interopDefault(require('object-hash')); | ||
@@ -21,3 +23,4 @@ const defaultOptions = { | ||
const defaultModifiers = [{ | ||
const defaultModifiers = [ | ||
{ | ||
key: 'uppercase', | ||
@@ -33,3 +36,4 @@ transform: uppercase, | ||
transform: titlecase, | ||
}]; | ||
}, | ||
]; | ||
@@ -74,3 +78,3 @@ class Interpolator { | ||
extractRules(matches) { | ||
return matches.map((match) => { | ||
return matches.map(match => { | ||
const alternativeText = this.getAlternativeText(match); | ||
@@ -82,3 +86,3 @@ const modifiers = this.getModifiers(match); | ||
modifiers, | ||
alternativeText | ||
alternativeText, | ||
}; | ||
@@ -89,3 +93,3 @@ }); | ||
const removeReservedSymbols = [':', '|']; | ||
return this.removeDelimiter(removeReservedSymbols.reduce((val, sym) => val.indexOf(sym) > 0 ? this.removeAfter(val, sym) : val, match)); | ||
return this.removeDelimiter(removeReservedSymbols.reduce((val, sym) => (val.indexOf(sym) > 0 ? this.removeAfter(val, sym) : val), match)); | ||
} | ||
@@ -167,3 +171,3 @@ removeDelimiter(val) { | ||
const transformers = modifiers.map(modifier => modifier && modifier.transform); | ||
return transformers.reduce((str, transform) => transform ? transform(str, rawData) : str, str); | ||
return transformers.reduce((str, transform) => (transform ? transform(str, rawData) : str), str); | ||
} | ||
@@ -190,2 +194,70 @@ catch (e) { | ||
function getInterpolationKeys(...interpolationStrings) { | ||
return interpolationStrings.reduce((keys, str) => [...keys, ...(str ? stringInterpolator.parseRules(str).map((match) => match.key) : [])], []); | ||
} | ||
function parseInterpolationStrings(interpolationStrings, argTypeMap) { | ||
const interpolationKeys = getInterpolationKeys(...interpolationStrings); | ||
const args = {}; | ||
const contextVariables = []; | ||
for (const interpolationKey of interpolationKeys) { | ||
const interpolationKeyParts = interpolationKey.split('.'); | ||
const varName = interpolationKeyParts[interpolationKeyParts.length - 1]; | ||
if (interpolationKeyParts[0] === 'args') { | ||
const argType = argTypeMap && varName in argTypeMap ? argTypeMap[varName] : 'ID'; | ||
args[varName] = { | ||
type: argType, | ||
}; | ||
} | ||
else if (interpolationKeyParts[0] === 'context') { | ||
contextVariables.push(varName); | ||
} | ||
} | ||
return { | ||
args, | ||
contextVariables, | ||
}; | ||
} | ||
function getInterpolatedStringFactory(nonInterpolatedString) { | ||
return resolverData => stringInterpolator.parse(nonInterpolatedString, resolverData); | ||
} | ||
function getInterpolatedHeadersFactory(nonInterpolatedHeaders = {}) { | ||
return resolverData => { | ||
const headers = {}; | ||
for (const headerName in nonInterpolatedHeaders) { | ||
const headerValue = nonInterpolatedHeaders[headerName]; | ||
if (headerValue) { | ||
headers[headerName.toLowerCase()] = stringInterpolator.parse(headerValue, resolverData); | ||
} | ||
} | ||
return headers; | ||
}; | ||
} | ||
function hashObject(value) { | ||
return objectHash(value, { ignoreUnknown: true }).toString(); | ||
} | ||
const stringInterpolator = new Interpolator({ | ||
delimiter: ['{', '}'], | ||
}); | ||
stringInterpolator.addAlias('typeName', 'info.parentType.name'); | ||
stringInterpolator.addAlias('type', 'info.parentType.name'); | ||
stringInterpolator.addAlias('parentType', 'info.parentType.name'); | ||
stringInterpolator.addAlias('fieldName', 'info.fieldName'); | ||
stringInterpolator.registerModifier('date', (formatStr) => dayjs(new Date()).format(formatStr)); | ||
stringInterpolator.registerModifier('hash', (value) => hashObject(value)); | ||
stringInterpolator.registerModifier('base64', (value) => { | ||
if (globalThis.Buffer.from) { | ||
return globalThis.Buffer.from(value).toString('base64'); | ||
} | ||
else { | ||
return btoa(value); | ||
} | ||
}); | ||
exports.Interpolator = Interpolator; | ||
exports.getInterpolatedHeadersFactory = getInterpolatedHeadersFactory; | ||
exports.getInterpolatedStringFactory = getInterpolatedStringFactory; | ||
exports.getInterpolationKeys = getInterpolationKeys; | ||
exports.hashObject = hashObject; | ||
exports.parseInterpolationStrings = parseInterpolationStrings; | ||
exports.stringInterpolator = stringInterpolator; |
{ | ||
"name": "@graphql-mesh/string-interpolation", | ||
"version": "0.1.0", | ||
"version": "0.1.1-alpha-77a22b738.0", | ||
"description": "Dynamic string manipulation", | ||
"sideEffects": false, | ||
"peerDependencies": { | ||
"graphql": "*" | ||
}, | ||
"dependencies": { | ||
"dayjs": "1.11.2", | ||
"json-pointer": "0.6.2", | ||
"lodash": "^4.17.21" | ||
"lodash": "^4.17.21", | ||
"object-hash": "3.0.0" | ||
}, | ||
@@ -10,0 +15,0 @@ "repository": { |
{ | ||
"name": "@graphql-mesh/string-interpolation", | ||
"version": "0.1.0", | ||
"version": "0.1.1-alpha-77a22b738.0", | ||
"description": "Dynamic string manipulation", | ||
@@ -22,4 +22,9 @@ "sideEffects": false, | ||
}, | ||
"peerDependencies": { | ||
"graphql": "*" | ||
}, | ||
"dependencies": { | ||
"json-pointer": "0.6.2", | ||
"dayjs": "1.11.2", | ||
"object-hash": "3.0.0", | ||
"lodash": "^4.17.21" | ||
@@ -26,0 +31,0 @@ }, |
import { Interpolator } from './interpolator'; | ||
import dayjs from 'dayjs'; | ||
import objectHash from 'object-hash'; | ||
export function hashObject(value: any): string { | ||
return objectHash(value, { ignoreUnknown: true }).toString(); | ||
} | ||
export { Interpolator }; | ||
export const stringInterpolator = new Interpolator({ | ||
delimiter: ['{', '}'], | ||
}); | ||
stringInterpolator.addAlias('typeName', 'info.parentType.name'); | ||
stringInterpolator.addAlias('type', 'info.parentType.name'); | ||
stringInterpolator.addAlias('parentType', 'info.parentType.name'); | ||
stringInterpolator.addAlias('fieldName', 'info.fieldName'); | ||
stringInterpolator.registerModifier('date', (formatStr: string) => dayjs(new Date()).format(formatStr)); | ||
stringInterpolator.registerModifier('hash', (value: any) => hashObject(value)); | ||
stringInterpolator.registerModifier('base64', (value: any) => { | ||
if (globalThis.Buffer.from) { | ||
return globalThis.Buffer.from(value).toString('base64'); | ||
} else { | ||
return btoa(value); | ||
} | ||
}); | ||
export * from './resolver-data-factory'; |
@@ -40,3 +40,3 @@ import { defaultOptions } from './statics/DefaultOptions'; | ||
this.modifiers.push({key: key.toLowerCase(), transform}); | ||
this.modifiers.push({ key: key.toLowerCase(), transform }); | ||
return this; | ||
@@ -55,3 +55,3 @@ } | ||
extractRules(matches) { | ||
return matches.map((match) => { | ||
return matches.map(match => { | ||
const alternativeText = this.getAlternativeText(match); | ||
@@ -63,5 +63,5 @@ const modifiers = this.getModifiers(match); | ||
modifiers, | ||
alternativeText | ||
} | ||
}) | ||
alternativeText, | ||
}; | ||
}); | ||
} | ||
@@ -71,3 +71,5 @@ | ||
const removeReservedSymbols = [':', '|']; | ||
return this.removeDelimiter(removeReservedSymbols.reduce((val, sym) => val.indexOf(sym) > 0 ? this.removeAfter(val, sym) : val, match)); | ||
return this.removeDelimiter( | ||
removeReservedSymbols.reduce((val, sym) => (val.indexOf(sym) > 0 ? this.removeAfter(val, sym) : val), match) | ||
); | ||
} | ||
@@ -145,3 +147,3 @@ | ||
try { | ||
return JsonPointer.get(propData, ptr); | ||
return JsonPointer.get(propData, ptr); | ||
} catch (e) { | ||
@@ -164,3 +166,3 @@ if (e.message.startsWith('Invalid reference')) { | ||
const transformers = modifiers.map(modifier => modifier && modifier.transform); | ||
return transformers.reduce((str, transform) => transform ? transform(str, rawData) : str, str); | ||
return transformers.reduce((str, transform) => (transform ? transform(str, rawData) : str), str); | ||
} catch (e) { | ||
@@ -173,4 +175,4 @@ console.error(`An error occurred while applying modifiers to ${str}`, modifiers, e); | ||
addAlias(key, ref) { | ||
if (typeof ref === 'function'){ | ||
this.aliases.push({key, ref: ref() }); | ||
if (typeof ref === 'function') { | ||
this.aliases.push({ key, ref: ref() }); | ||
} else { | ||
@@ -177,0 +179,0 @@ this.aliases.push({ key, ref }); |
@@ -5,13 +5,15 @@ import { uppercase } from './uppercase'; | ||
export const defaultModifiers = [{ | ||
key: 'uppercase', | ||
transform: uppercase, | ||
}, | ||
{ | ||
key: 'lowercase', | ||
transform: lowercase, | ||
}, | ||
{ | ||
key: 'title', | ||
transform: titlecase, | ||
}]; | ||
export const defaultModifiers = [ | ||
{ | ||
key: 'uppercase', | ||
transform: uppercase, | ||
}, | ||
{ | ||
key: 'lowercase', | ||
transform: lowercase, | ||
}, | ||
{ | ||
key: 'title', | ||
transform: titlecase, | ||
}, | ||
]; |
export const defaultOptions = { | ||
delimiter: ['{', '}'], | ||
} | ||
}; |
Sorry, the diff of this file is not supported yet
31248
21
799
5
+ Addeddayjs@1.11.2
+ Addedobject-hash@3.0.0
+ Addeddayjs@1.11.2(transitive)
+ Addedgraphql@16.10.0(transitive)
+ Addedobject-hash@3.0.0(transitive)