@vendure/core
Version:
A modern, headless ecommerce framework
74 lines • 2.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GraphQLMoney = void 0;
exports.isObjectLike = isObjectLike;
const graphql_1 = require("graphql");
const inspect_1 = require("graphql/jsutils/inspect");
function isObjectLike(value) {
// eslint-disable-next-line eqeqeq
return typeof value == 'object' && value !== null;
}
// Support serializing objects with custom valueOf() or toJSON() functions -
// a common way to represent a complex value which can be represented as
// a string (ex: MongoDB id objects).
function serializeObject(outputValue) {
if (isObjectLike(outputValue)) {
if (typeof outputValue.valueOf === 'function') {
const valueOfResult = outputValue.valueOf();
if (!isObjectLike(valueOfResult)) {
return valueOfResult;
}
}
if (typeof outputValue.toJSON === 'function') {
return outputValue.toJSON();
}
}
return outputValue;
}
/**
* @description
* The Money scalar is used to represent monetary values in the GraphQL API. It is based on the native `Float` scalar.
*/
exports.GraphQLMoney = new graphql_1.GraphQLScalarType({
name: 'Money',
description: 'The `Money` scalar type represents monetary values and supports signed double-precision ' +
'fractional values as specified by [IEEE 754](https://en.wikipedia.org/wiki/IEEE_floating_point).',
serialize(outputValue) {
const coercedValue = serializeObject(outputValue);
if (typeof coercedValue === 'boolean') {
return coercedValue ? 1 : 0;
}
let num = coercedValue;
if (typeof coercedValue === 'string' && coercedValue !== '') {
num = Number(coercedValue);
}
if (typeof num !== 'number' || !Number.isFinite(num)) {
throw new graphql_1.GraphQLError(`Money cannot represent non numeric value: ${(0, inspect_1.inspect)(coercedValue)}`);
}
return num;
},
parseValue(inputValue) {
if (typeof inputValue !== 'number' || !Number.isFinite(inputValue)) {
throw new graphql_1.GraphQLError(`Money cannot represent non numeric value: ${(0, inspect_1.inspect)(inputValue)}`);
}
return inputValue;
},
parseLiteral(valueNode) {
if (valueNode.kind !== graphql_1.Kind.FLOAT && valueNode.kind !== graphql_1.Kind.INT) {
throw new graphql_1.GraphQLError(`Money cannot represent non numeric value: ${(0, graphql_1.print)(valueNode)}`, {
nodes: valueNode,
});
}
return parseFloat(valueNode.value);
},
extensions: {
codegenScalarType: 'number',
jsonSchema: {
title: 'Money',
type: 'number',
minimum: Number.MIN_SAFE_INTEGER,
maximum: Number.MAX_SAFE_INTEGER,
},
},
});
//# sourceMappingURL=money-scalar.js.map