Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use TypeScript #337

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions .babelrc.js

This file was deleted.

4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx lint-staged
59 changes: 23 additions & 36 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,18 @@
"module": "es/index.js",
"types": "lib/index.d.ts",
"scripts": {
"build": "npm run build:cjs && npm run build:esm && npm run build:types",
"build:cjs": "babel -d lib --delete-dir-on-start src",
"build:esm": "babel --env-name esm -d es --delete-dir-on-start src",
"build:types": "cpy types/*.d.ts lib",
"clean": "rimraf lib es",
"build": "npm run clean && npm run build:cjs && npm run build:esm",
"build:cjs": "tsc -p . --module commonjs --outDir lib",
"build:esm": "tsc -p . --module es6 --outDir es",
"format": "eslint --fix . && npm run prettier -- --write",
"lint": "eslint . && npm run prettier -- -l",
"prepublish": "npm run build",
"prettier": "prettier --ignore-path .eslintignore '**/*.{md,ts,tsx}'",
"prettier": "prettier --ignore-path .eslintignore '**/*.md'",
"tdd": "jest --watch",
"test": "npm run lint && npm run test:ts && npm run testonly -- --coverage",
"test:ts": "dtslint types",
"test": "npm run lint && npm run testonly -- --coverage",
"testonly": "jest --runInBand --verbose"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.js": "eslint --fix",
"*.{md,ts}": "prettier --write"
Expand All @@ -38,10 +32,7 @@
"trailingComma": "all"
},
"jest": {
"collectCoverageFrom": [
"src/**"
],
"testEnvironment": "node"
"preset": "ts-jest"
},
"repository": {
"type": "git",
Expand All @@ -57,28 +48,24 @@
},
"homepage": "https://github.com/taion/graphql-type-json#readme",
"devDependencies": {
"@babel/cli": "^7.12.13",
"@babel/core": "^7.12.13",
"@babel/preset-env": "^7.12.13",
"babel-jest": "^26.6.3",
"codecov": "^3.8.1",
"cpy-cli": "^3.1.1",
"dtslint": "^3.7.0",
"eslint": "^7.19.0",
"eslint-config-4catalyzer": "^1.1.5",
"eslint-config-4catalyzer-jest": "^2.0.10",
"eslint-config-prettier": "^6.15.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jest": "^23.20.0",
"eslint-plugin-prettier": "^3.3.1",
"codecov": "^3.8.3",
"eslint": "^8.10.0",
"eslint-config-4catalyzer": "^1.4.1",
"eslint-config-4catalyzer-jest": "^2.2.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-jest": "^26.1.1",
"eslint-plugin-prettier": "^4.0.0",
"graphql": "^15.5.0",
"husky": "^4.3.8",
"jest": "^26.6.3",
"lint-staged": "^10.5.4",
"prettier": "^2.2.1",
"typescript": "^4.1.3"
"husky": "^7.0.4",
"jest": "^27.5.1",
"lint-staged": "^12.3.5",
"prettier": "^2.5.1",
"rimraf": "^3.0.2",
"ts-jest": "^27.1.3",
"typescript": "^4.6.2"
},
"peerDependencies": {
"graphql": ">=0.8.0"
"graphql": ">=0.8.0 < 16"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}
19 changes: 14 additions & 5 deletions src/index.js → src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { GraphQLScalarType } from 'graphql';
import { Kind, print } from 'graphql/language';
import { Maybe } from 'graphql/jsutils/Maybe';
import { Kind, ObjectValueNode, print, ValueNode } from 'graphql/language';

function identity(value) {
function identity<T>(value: T) {
return value;
}

function ensureObject(value) {
function ensureObject(value: any) {
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
throw new TypeError(
`JSONObject cannot represent non-object value: ${value}`,
Expand All @@ -15,7 +16,11 @@ function ensureObject(value) {
return value;
}

function parseObject(typeName, ast, variables) {
function parseObject(
typeName: string,
ast: ObjectValueNode,
variables: Maybe<{ [key: string]: any }>,
) {
const value = Object.create(null);
ast.fields.forEach((field) => {
// eslint-disable-next-line no-use-before-define
Expand All @@ -25,7 +30,11 @@ function parseObject(typeName, ast, variables) {
return value;
}

function parseLiteral(typeName, ast, variables) {
function parseLiteral(
typeName: string,
ast: ValueNode,
variables: Maybe<{ [key: string]: any }>,
): any {
switch (ast.kind) {
case Kind.STRING:
case Kind.BOOLEAN:
Expand Down
37 changes: 18 additions & 19 deletions test/index.test.js → test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import {
GraphQLObjectType,
GraphQLSchema,
graphql,
GraphQLScalarType,
} from 'graphql';

// eslint-disable-next-line import/no-named-as-default
import GraphQLJSON, { GraphQLJSONObject } from '../src';

const FIXTURE = {
Expand All @@ -26,7 +25,7 @@ const FIXTURE = {
array: ['string', 3, 3.14, true, false, null],
};

function createSchema(type) {
function createSchema(type: GraphQLScalarType) {
return new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
Expand All @@ -49,7 +48,7 @@ function createSchema(type) {
}

describe('GraphQLJSON', () => {
let schema;
let schema: GraphQLSchema;

beforeEach(() => {
schema = createSchema(GraphQLJSON);
Expand All @@ -66,7 +65,7 @@ describe('GraphQLJSON', () => {
`,
FIXTURE,
).then(({ data, errors }) => {
expect(data.rootValue).toEqual(FIXTURE);
expect(data?.rootValue).toEqual(FIXTURE);
expect(errors).toBeUndefined();
}));
});
Expand All @@ -76,7 +75,7 @@ describe('GraphQLJSON', () => {
graphql(
schema,
/* GraphQL */ `
query($arg: JSON!) {
query ($arg: JSON!) {
value(arg: $arg)
}
`,
Expand All @@ -86,7 +85,7 @@ describe('GraphQLJSON', () => {
arg: FIXTURE,
},
).then(({ data, errors }) => {
expect(data.value).toEqual(FIXTURE);
expect(data?.value).toEqual(FIXTURE);
expect(errors).toBeUndefined();
}));
});
Expand All @@ -96,7 +95,7 @@ describe('GraphQLJSON', () => {
graphql(
schema,
/* GraphQL */ `
query($intValue: Int = 3) {
query ($intValue: Int = 3) {
value(
arg: {
string: "string"
Expand All @@ -119,7 +118,7 @@ describe('GraphQLJSON', () => {
}
`,
).then(({ data, errors }) => {
expect(data.value).toEqual(FIXTURE);
expect(data?.value).toEqual(FIXTURE);
expect(errors).toBeUndefined();
}));

Expand Down Expand Up @@ -173,7 +172,7 @@ describe('GraphQLJSON', () => {
});

describe('GraphQLJSONObject', () => {
let schema;
let schema: GraphQLSchema;

beforeEach(() => {
schema = createSchema(GraphQLJSONObject);
Expand All @@ -190,7 +189,7 @@ describe('GraphQLJSONObject', () => {
`,
FIXTURE,
).then(({ data, errors }) => {
expect(data.rootValue).toEqual(FIXTURE);
expect(data?.rootValue).toEqual(FIXTURE);
expect(errors).toBeUndefined();
}));

Expand All @@ -204,7 +203,7 @@ describe('GraphQLJSONObject', () => {
`,
'foo',
).then(({ data, errors }) => {
expect(data.rootValue).toBeNull();
expect(data?.rootValue).toBeNull();
expect(errors).toMatchInlineSnapshot(`
Array [
[GraphQLError: JSONObject cannot represent non-object value: foo],
Expand All @@ -222,7 +221,7 @@ describe('GraphQLJSONObject', () => {
`,
[],
).then(({ data, errors }) => {
expect(data.rootValue).toBeNull();
expect(data?.rootValue).toBeNull();
expect(errors).toMatchInlineSnapshot(`
Array [
[GraphQLError: JSONObject cannot represent non-object value: ],
Expand All @@ -236,7 +235,7 @@ describe('GraphQLJSONObject', () => {
graphql(
schema,
/* GraphQL */ `
query($arg: JSONObject!) {
query ($arg: JSONObject!) {
value(arg: $arg)
}
`,
Expand All @@ -246,15 +245,15 @@ describe('GraphQLJSONObject', () => {
arg: FIXTURE,
},
).then(({ data, errors }) => {
expect(data.value).toEqual(FIXTURE);
expect(data?.value).toEqual(FIXTURE);
expect(errors).toBeUndefined();
}));

it('should reject string value', () =>
graphql(
schema,
/* GraphQL */ `
query($arg: JSONObject!) {
query ($arg: JSONObject!) {
value(arg: $arg)
}
`,
Expand All @@ -276,7 +275,7 @@ describe('GraphQLJSONObject', () => {
graphql(
schema,
/* GraphQL */ `
query($arg: JSONObject!) {
query ($arg: JSONObject!) {
value(arg: $arg)
}
`,
Expand All @@ -300,7 +299,7 @@ describe('GraphQLJSONObject', () => {
graphql(
schema,
/* GraphQL */ `
query($intValue: Int = 3) {
query ($intValue: Int = 3) {
value(
arg: {
string: "string"
Expand All @@ -323,7 +322,7 @@ describe('GraphQLJSONObject', () => {
}
`,
).then(({ data, errors }) => {
expect(data.value).toEqual(FIXTURE);
expect(data?.value).toEqual(FIXTURE);
expect(errors).toBeUndefined();
}));

Expand Down
12 changes: 12 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"lib": ["es2015", "esnext.asynciterable"],
"strict": true,
"module": "CommonJS",
"types": ["jest"],
"declaration": true,
"moduleResolution": "node",
"baseUrl": "src"
},
"exclude": ["**/*.test.ts"]
}
11 changes: 0 additions & 11 deletions types/index.d.ts

This file was deleted.

10 changes: 0 additions & 10 deletions types/tsconfig.json

This file was deleted.

Loading