Skip to content

Commit

Permalink
Setup Bob v4
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Oct 10, 2022
1 parent 8ba194c commit 5bf309c
Show file tree
Hide file tree
Showing 88 changed files with 1,927 additions and 3,540 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ dist/
*.log
package-lock.json
.next/
.bob
36 changes: 22 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,28 @@
"url": "https://github.com/Urigo/graphql-scalars.git"
},
"sideEffects": false,
"main": "dist/index.js",
"module": "dist/index.mjs",
"typings": "dist/index.d.ts",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"typings": "dist/typings/index.d.ts",
"typescript": {
"definition": "dist/index.d.ts"
"definition": "dist/typings/index.d.ts"
},
"exports": {
".": {
"require": "./dist/index.js",
"import": "./dist/index.mjs"
"require": {
"types": "./dist/typings/index.d.cts",
"default": "./dist/cjs/index.js"
},
"import": {
"types": "./dist/typings/index.d.ts",
"default": "./dist/esm/index.js"
},
"default": {
"types": "./dist/typings/index.d.ts",
"default": "./dist/esm/index.js"
}
},
"./*": {
"require": "./dist/*.js",
"import": "./dist/*.mjs"
}
"./package.json": "./package.json"
},
"license": "MIT",
"scripts": {
Expand Down Expand Up @@ -51,7 +58,7 @@
"@types/node": "16.11.64",
"@typescript-eslint/eslint-plugin": "5.36.2",
"@typescript-eslint/parser": "5.36.2",
"bob-the-bundler": "1.7.3",
"bob-the-bundler": "4.0.0",
"eslint": "8.25.0",
"eslint-config-prettier": "8.5.0",
"eslint-config-standard": "17.0.0",
Expand Down Expand Up @@ -91,7 +98,8 @@
"node": ">=10"
},
"publishConfig": {
"access": "public",
"directory": "dist"
}
"directory": "dist",
"access": "public"
},
"type": "module"
}
14 changes: 7 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GraphQLScalarType } from 'graphql';
import * as mocks from './mocks';
import * as mocks from './mocks.js';

import {
GraphQLDate,
Expand Down Expand Up @@ -62,8 +62,8 @@ import {
GraphQLAccountNumber,
GraphQLCuid,
GraphQLSemVer,
} from './scalars';
import { GraphQLDuration } from './scalars/iso-date/Duration';
} from './scalars/index.js';
import { GraphQLDuration } from './scalars/iso-date/Duration.js';

export {
Date as DateTypeDefinition,
Expand Down Expand Up @@ -125,9 +125,9 @@ export {
AccountNumber as AccountNumberDefinition,
Cuid as CuidDefinition,
SemVer as SemVerDefinition,
} from './typeDefs';
} from './typeDefs.js';

export { typeDefs } from './typeDefs';
export { typeDefs } from './typeDefs.js';

export {
GraphQLDate as DateResolver,
Expand Down Expand Up @@ -319,11 +319,11 @@ export {
AccountNumber as AccountNumberMock,
Cuid as CuidMock,
SemVer as SemVerMock,
} from './mocks';
} from './mocks.js';

export { mocks };

export { RegularExpression, RegularExpressionOptions, RegularExpressionErrorMessageFn } from './RegularExpression';
export { RegularExpression, RegularExpressionOptions, RegularExpressionErrorMessageFn } from './RegularExpression.js';

export {
GraphQLDate,
Expand Down
55 changes: 13 additions & 42 deletions src/scalars/BigInt.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,18 @@
import {
Kind,
GraphQLScalarType,
GraphQLScalarTypeConfig,
GraphQLError,
print,
} from 'graphql';
import { serializeObject } from './utilities';
import { Kind, GraphQLScalarType, GraphQLScalarTypeConfig, GraphQLError, print } from 'graphql';
import { serializeObject } from './utilities.js';

export const GraphQLBigIntConfig: GraphQLScalarTypeConfig<
bigint,
bigint | BigInt | string | number
> = /*#__PURE__*/ {
export const GraphQLBigIntConfig: GraphQLScalarTypeConfig<bigint, bigint | bigint | string | number> = /*#__PURE__*/ {
name: 'BigInt',
description:
'The `BigInt` scalar type represents non-fractional signed whole numeric values.',
description: 'The `BigInt` scalar type represents non-fractional signed whole numeric values.',
serialize(outputValue) {
const coercedValue = serializeObject(outputValue);

let num = coercedValue;

if (
typeof coercedValue === 'object' &&
coercedValue != null &&
'toString' in coercedValue
) {
if (typeof coercedValue === 'object' && coercedValue != null && 'toString' in coercedValue) {
num = BigInt(coercedValue.toString());
if (num.toString() !== coercedValue.toString()) {
throw new GraphQLError(
`BigInt cannot represent non-integer value: ${coercedValue}`,
);
throw new GraphQLError(`BigInt cannot represent non-integer value: ${coercedValue}`);
}
}

Expand All @@ -39,32 +23,26 @@ export const GraphQLBigIntConfig: GraphQLScalarTypeConfig<
if (typeof coercedValue === 'string' && coercedValue !== '') {
num = BigInt(coercedValue);
if (num.toString() !== coercedValue) {
throw new GraphQLError(
`BigInt cannot represent non-integer value: ${coercedValue}`,
);
throw new GraphQLError(`BigInt cannot represent non-integer value: ${coercedValue}`);
}
}

if (typeof coercedValue === 'number') {
if (!Number.isInteger(coercedValue)) {
throw new GraphQLError(
`BigInt cannot represent non-integer value: ${coercedValue}`,
);
throw new GraphQLError(`BigInt cannot represent non-integer value: ${coercedValue}`);
}
num = BigInt(coercedValue);
}

if (typeof num !== 'bigint') {
throw new GraphQLError(
`BigInt cannot represent non-integer value: ${coercedValue}`,
);
throw new GraphQLError(`BigInt cannot represent non-integer value: ${coercedValue}`);
}

if ('toJSON' in BigInt.prototype) {
return num;
}

return new Proxy({} as BigInt, {
return new Proxy({} as bigint, {
has(_, prop) {
if (prop === 'toJSON') {
return true;
Expand Down Expand Up @@ -99,17 +77,11 @@ export const GraphQLBigIntConfig: GraphQLScalarTypeConfig<
},
parseLiteral(valueNode) {
if (valueNode.kind !== Kind.INT) {
throw new GraphQLError(
`BigInt cannot represent non-integer value: ${print(valueNode)}`,
valueNode,
);
throw new GraphQLError(`BigInt cannot represent non-integer value: ${print(valueNode)}`, valueNode);
}
const num = BigInt(valueNode.value);
if (num.toString() !== valueNode.value) {
throw new GraphQLError(
`BigInt cannot represent value: ${valueNode.value}`,
valueNode,
);
throw new GraphQLError(`BigInt cannot represent value: ${valueNode.value}`, valueNode);
}
return num;
},
Expand All @@ -118,5 +90,4 @@ export const GraphQLBigIntConfig: GraphQLScalarTypeConfig<
},
};

export const GraphQLBigInt: GraphQLScalarType =
/*#__PURE__*/ new GraphQLScalarType(GraphQLBigIntConfig);
export const GraphQLBigInt: GraphQLScalarType = /*#__PURE__*/ new GraphQLScalarType(GraphQLBigIntConfig);
15 changes: 5 additions & 10 deletions src/scalars/GUID.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { GraphQLScalarType } from 'graphql';
import { GraphQLUUIDConfig } from './UUID';
import { GraphQLUUIDConfig } from './UUID.js';

export const GraphQLGUIDConfig = /*#__PURE__*/ Object.assign(
{},
GraphQLUUIDConfig,
{
name: 'GUID',
},
);
export const GraphQLGUIDConfig = /*#__PURE__*/ Object.assign({}, GraphQLUUIDConfig, {
name: 'GUID',
});

export const GraphQLGUID: GraphQLScalarType =
/*#__PURE__*/ new GraphQLScalarType(GraphQLGUIDConfig);
export const GraphQLGUID: GraphQLScalarType = /*#__PURE__*/ new GraphQLScalarType(GraphQLGUIDConfig);
4 changes: 2 additions & 2 deletions src/scalars/IP.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Kind, GraphQLError, GraphQLScalarType } from 'graphql';
import { IPV4_REGEX } from './IPv4';
import { IPV6_REGEX } from './IPv6';
import { IPV4_REGEX } from './IPv4.js';
import { IPV6_REGEX } from './IPv6.js';

const validate = (value: any) => {
if (typeof value !== 'string') {
Expand Down
50 changes: 22 additions & 28 deletions src/scalars/Latitude.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Inspired by Geolib: https://github.com/manuelbieh/geolib
import { GraphQLError, GraphQLScalarType, Kind } from 'graphql';
import { isDecimal, isSexagesimal, sexagesimalToDecimal } from './utilities';
import { isDecimal, isSexagesimal, sexagesimalToDecimal } from './utilities.js';

// Minimum latitude
const MIN_LAT = -90.0;
Expand All @@ -21,13 +21,10 @@ const validate = (value: any): number => {
}

if (isDecimal(value)) {
const decimalValue =
typeof value === 'string' ? Number.parseFloat(value) : value;
const decimalValue = typeof value === 'string' ? Number.parseFloat(value) : value;

if (decimalValue < MIN_LAT || decimalValue > MAX_LAT) {
throw new RangeError(
`Value must be between ${MIN_LAT} and ${MAX_LAT}: ${value}`,
);
throw new RangeError(`Value must be between ${MIN_LAT} and ${MAX_LAT}: ${value}`);
}

return Number.parseFloat(decimalValue.toFixed(MAX_PRECISION));
Expand All @@ -40,30 +37,27 @@ const validate = (value: any): number => {
throw new TypeError(`Value is not a valid latitude: ${value}`);
};

export const GraphQLLatitude: GraphQLScalarType =
/*#__PURE__*/ new GraphQLScalarType({
name: `Latitude`,
export const GraphQLLatitude: GraphQLScalarType = /*#__PURE__*/ new GraphQLScalarType({
name: `Latitude`,

description: `A field whose value is a valid decimal degrees latitude number (53.471): https://en.wikipedia.org/wiki/Latitude`,
description: `A field whose value is a valid decimal degrees latitude number (53.471): https://en.wikipedia.org/wiki/Latitude`,

serialize(value) {
return validate(value);
},
serialize(value) {
return validate(value);
},

parseValue(value) {
return validate(value);
},
parseValue(value) {
return validate(value);
},

parseLiteral(ast) {
if (ast.kind !== Kind.FLOAT && ast.kind !== Kind.STRING) {
throw new GraphQLError(
`Can only validate floats or strings as latitude but got a: ${ast.kind}`,
);
}
parseLiteral(ast) {
if (ast.kind !== Kind.FLOAT && ast.kind !== Kind.STRING) {
throw new GraphQLError(`Can only validate floats or strings as latitude but got a: ${ast.kind}`);
}

return validate(ast.value);
},
extensions: {
codegenScalarType: 'string',
},
});
return validate(ast.value);
},
extensions: {
codegenScalarType: 'string',
},
});
59 changes: 28 additions & 31 deletions src/scalars/LocalEndTime.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GraphQLScalarType, Kind, GraphQLError } from 'graphql';
import { validateLocalTime } from './LocalTime';
import { validateLocalTime } from './LocalTime.js';

const LOCAL_END_TIMES = ['24:00', '24:00:00', '24:00:00.000'];

Expand All @@ -13,33 +13,30 @@ function validateLocalEndTime(value: any) {
return validateLocalTime(value);
}

export const GraphQLLocalEndTime: GraphQLScalarType =
/*#__PURE__*/ new GraphQLScalarType({
name: 'LocalEndTime',
description:
'A local time string (i.e., with no associated timezone) in 24-hr `HH:mm[:ss[.SSS]]` format, e.g. `14:25` or `14:25:06` or `14:25:06.123`. This scalar is very similar to the `LocalTime`, with the only difference being that `LocalEndTime` also allows `24:00` as a valid value to indicate midnight of the following day. This is useful when using the scalar to represent the exclusive upper bound of a time block.',

serialize(value) {
// value sent to client as string
return validateLocalEndTime(value);
},

parseValue(value) {
// value from client as json
return validateLocalEndTime(value);
},

parseLiteral(ast) {
// value from client in ast
if (ast.kind !== Kind.STRING) {
throw new GraphQLError(
`Can only validate strings as local times but got a: ${ast.kind}`,
);
}

return validateLocalEndTime(ast.value);
},
extensions: {
codegenScalarType: 'string',
},
});
export const GraphQLLocalEndTime: GraphQLScalarType = /*#__PURE__*/ new GraphQLScalarType({
name: 'LocalEndTime',
description:
'A local time string (i.e., with no associated timezone) in 24-hr `HH:mm[:ss[.SSS]]` format, e.g. `14:25` or `14:25:06` or `14:25:06.123`. This scalar is very similar to the `LocalTime`, with the only difference being that `LocalEndTime` also allows `24:00` as a valid value to indicate midnight of the following day. This is useful when using the scalar to represent the exclusive upper bound of a time block.',

serialize(value) {
// value sent to client as string
return validateLocalEndTime(value);
},

parseValue(value) {
// value from client as json
return validateLocalEndTime(value);
},

parseLiteral(ast) {
// value from client in ast
if (ast.kind !== Kind.STRING) {
throw new GraphQLError(`Can only validate strings as local times but got a: ${ast.kind}`);
}

return validateLocalEndTime(ast.value);
},
extensions: {
codegenScalarType: 'string',
},
});
Loading

0 comments on commit 5bf309c

Please sign in to comment.