Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
directiveList.forEach((value: DirectiveNode) => {
const url = getDirectiveArgument(value, 'url');
// require a protocol in the url
const protocolMatcher = /^http(s)?:\/\//;
if (!protocolMatcher.test(url)) {
throw new TransformerContractError(
`@http directive at location ${value.loc.start} ` + `requires a url parameter that begins with http:// or https://.`
);
}
// extract just the base url with protocol
const baseURL = url.replace(HttpTransformer.urlRegex, '$1');
const dataSourceID = HttpResourceIDs.HttpDataSourceID(baseURL);
// only create one DataSource per base URL
if (!ctx.getResource(dataSourceID)) {
ctx.mapResourceToStack(HTTP_STACK_NAME, dataSourceID);
ctx.setResource(dataSourceID, this.resources.makeHttpDataSource(baseURL));
}
});
};
let updatedFields = type.fields;
const versionFieldImpl = type.fields.find(f => f.name.value === versionField);
let updatedField = versionFieldImpl;
if (versionFieldImpl) {
const baseType = getBaseType(versionFieldImpl.type);
if (baseType === 'Int' || baseType === 'BigInt') {
// ok.
if (versionFieldImpl.type.kind !== Kind.NON_NULL_TYPE) {
updatedField = {
...updatedField,
type: makeNonNullType(versionFieldImpl.type),
};
updatedFields = updatedFields.map(f => (f.name.value === versionField ? updatedField : f));
}
} else {
throw new TransformerContractError(`The versionField "${versionField}" is required to be of type "Int" or "BigInt".`);
}
} else {
updatedField = makeField(versionField, [], makeNonNullType(makeNamedType('Int')));
updatedFields = [...updatedFields, updatedField];
}
const updatedType = {
...type,
fields: updatedFields,
};
ctx.putType(updatedType);
}
}
}