Skip to content

Commit

Permalink
refactor: introduce eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt committed Feb 18, 2023
1 parent 9dc1b7e commit 0fb7062
Show file tree
Hide file tree
Showing 34 changed files with 1,373 additions and 679 deletions.
5 changes: 5 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.yalc
.vscode
.github
node_modules
build
65 changes: 65 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": ["tsconfig.json"]
},
"plugins": [
// This enables using new lint rules powered by the TypeScript compiler API.
// https://github.com/typescript-eslint/typescript-eslint
"@typescript-eslint",
// This makes it so the IDE reports lint rejections as warnings only. This is
// better than errors because most lint rejections are not runtime errors. This
// allows IDE errors to be exclusive for e.g. static type errors which often are
// reflective of real runtime errors.
// https://github.com/bfanger/eslint-plugin-only-warn
"only-warn",
// This enables the use of a lint rule to reject function declarations. This is
// preferable as a way to encourage higher order function usage. For example it is not
// possible to wrap a function declaration with Open Telemetry instrumentation but it is
// possible to wrap an arrow function since its an expression.
// https://github.com/TristonJ/eslint-plugin-prefer-arrow
"prefer-arrow",
// This enables the use of a lint rule to reject use of @deprecated functions.
// https://github.com/gund/eslint-plugin-deprecation
"deprecation",
// Import sorting integrated into ESLint.
// https://github.com/lydell/eslint-plugin-simple-import-sort
"simple-import-sort",
// https://github.com/microsoft/tsdoc/tree/master/eslint-plugin
"tsdoc"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
"prettier"
],
"overrides": [],
"rules": {
"@typescript-eslint/consistent-type-imports": "warn",
// Enforce backticks
// Note you must disable the base rule as it can report incorrect errors.
"quotes": "off",
"@typescript-eslint/quotes": ["warn", "backtick"],
"tsdoc/syntax": "warn",
"simple-import-sort/imports": [
"warn",
{
"groups": []
}
],
"simple-import-sort/exports": "warn",
"deprecation/deprecation": "warn",
"prefer-arrow/prefer-arrow-functions": "warn",
// TypeScript makes these safe & effective
"no-case-declarations": "off",
// Same approach used by TypeScript noUnusedLocals
"@typescript-eslint/no-unused-vars": ["warn", { "varsIgnorePattern": "^_", "argsIgnorePattern": "^_" }],
// Needed when working with .mts/.cts where a lone e.g. <T> is not allowed
"@typescript-eslint/no-unnecessary-type-constraint": "off",
// Useful for organizing Types
"@typescript-eslint/no-namespace": "off"
}
}
6 changes: 3 additions & 3 deletions examples/authentication-via-http-header.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { GraphQLClient } from '../src/index.js'
;(async function () {
const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr'
;(async () => {
const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`

const graphQLClient = new GraphQLClient(endpoint, {
headers: {
authorization: 'Bearer MY_TOKEN',
authorization: `Bearer MY_TOKEN`,
},
})

Expand Down
6 changes: 3 additions & 3 deletions examples/batching-requests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { batchRequests } from '../src/index.js'
;(async function () {
const endpoint = 'https://api.spacex.land/graphql/'
const endpoint = `https://api.spacex.land/graphql/`

const query1 = /* GraphQL */ `
query ($id: ID!) {
Expand All @@ -11,7 +11,7 @@ import { batchRequests } from '../src/index.js'
}
`
const variables1 = {
id: 'C105',
id: `C105`,
}

interface TData1 {
Expand Down Expand Up @@ -41,7 +41,7 @@ import { batchRequests } from '../src/index.js'
`

const variables3 = {
id: 'B1015',
id: `B1015`,
}

interface TData3 {
Expand Down
6 changes: 3 additions & 3 deletions examples/cookie-support-for-node.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
;(global as any).fetch = require('fetch-cookie/node-fetch')(require('node-fetch'))
;(global as any).fetch = require(`fetch-cookie/node-fetch`)(require(`node-fetch`))

import { GraphQLClient } from '../src/index.js'
;(async function () {
const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr'
const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`

const graphQLClient = new GraphQLClient(endpoint, {
headers: {
authorization: 'Bearer MY_TOKEN',
authorization: `Bearer MY_TOKEN`,
},
})

Expand Down
4 changes: 2 additions & 2 deletions examples/custom-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fetch from 'cross-fetch'
import { GraphQLClient } from '../src/index.js'
import fetch from 'cross-fetch'
;(async function () {
const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr'
const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`

const graphQLClient = new GraphQLClient(endpoint, { fetch: fetch })

Expand Down
2 changes: 1 addition & 1 deletion examples/error-handling.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { request } from '../src/index.js'
;(async function () {
const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr'
const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`

const query = /* GraphQL */ `
{
Expand Down
8 changes: 4 additions & 4 deletions examples/passing-custom-header-per-request.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { GraphQLClient } from '../src/index.js'
;(async function () {
const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr'
const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`

const client = new GraphQLClient(endpoint, {
headers: {
authorization: 'Bearer MY_TOKEN',
authorization: `Bearer MY_TOKEN`,
},
})

Expand All @@ -20,8 +20,8 @@ import { GraphQLClient } from '../src/index.js'
`

const requestHeaders = {
authorization: 'Bearer MY_TOKEN_2',
'x-custom': 'foo',
authorization: `Bearer MY_TOKEN_2`,
'x-custom': `foo`,
}

interface TData {
Expand Down
6 changes: 3 additions & 3 deletions examples/passing-more-options-to-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { GraphQLClient } from '../src/index.js'
;(async function () {
const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr'
const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`

const graphQLClient = new GraphQLClient(endpoint, {
credentials: 'include',
mode: 'cors',
credentials: `include`,
mode: `cors`,
})

const query = /* GraphQL */ `
Expand Down
2 changes: 1 addition & 1 deletion examples/receiving-a-raw-response.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { rawRequest } from '../src/index.js'
;(async function () {
const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr'
const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`

const query = /* GraphQL */ `
{
Expand Down
9 changes: 4 additions & 5 deletions examples/typed-document-node.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { TypedDocumentNode } from '@graphql-typed-document-node/core'
import { GraphQLClient,request } from '../src/index.js'
import type { TypedDocumentNode } from '@graphql-typed-document-node/core'
import { parse } from 'graphql'

import { request, GraphQLClient } from '../src/index.js'
;(async function () {
const endpoint = 'https://graphql-yoga.com/api/graphql'
const endpoint = `https://graphql-yoga.com/api/graphql`

const query: TypedDocumentNode<{ greetings: string }, never | Record<any, never>> = parse(/* GraphQL */ `
query greetings {
Expand All @@ -18,7 +17,7 @@ import { request, GraphQLClient } from '../src/index.js'
console.log(data.greetings)
})().catch(console.error)
;(async function () {
const endpoint = 'https://graphql-yoga.com/api/graphql'
const endpoint = `https://graphql-yoga.com/api/graphql`

const client = new GraphQLClient(endpoint)

Expand Down
4 changes: 2 additions & 2 deletions examples/using-variables.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { request } from '../src/index.js'
;(async function () {
const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr'
const endpoint = `https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr`

const query = /* GraphQL */ `
query getMovie($title: String!) {
Expand All @@ -14,7 +14,7 @@ import { request } from '../src/index.js'
`

const variables = {
title: 'Inception',
title: `Inception`,
}

interface TData {
Expand Down
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@
"build:docs": "doctoc README.md --notitle",
"dev": "rm -rf dist && tsc --watch",
"format": "prettier --write .",
"lint": "eslint . --ext .ts,.tsx --fix",
"check:types": "pnpm tsc --noEmit",
"check:format": "prettier --check .",
"check:lint": "eslint . --ext .ts,.tsx --max-warnings 0",
"prepublishOnly": "pnpm build",
"build": "pnpm clean && pnpm build:cjs && pnpm build:esm",
"build:cjs": "pnpm tsc --project tsconfig.cjs.json && echo '{\"type\":\"commonjs\"}' > build/cjs/package.json",
Expand Down Expand Up @@ -74,11 +76,20 @@
"@types/extract-files": "^8.1.1",
"@types/node": "^18.11.18",
"@types/ws": "^8.5.4",
"@typescript-eslint/eslint-plugin": "^5.52.0",
"@typescript-eslint/parser": "^5.52.0",
"@vitest/coverage-c8": "^0.28.2",
"apollo-server-express": "^3.11.1",
"body-parser": "^1.20.1",
"doctoc": "^2.2.1",
"dripip": "^0.10.0",
"eslint": "^8.34.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-deprecation": "^1.3.3",
"eslint-plugin-only-warn": "^1.1.0",
"eslint-plugin-prefer-arrow": "^1.2.3",
"eslint-plugin-simple-import-sort": "^10.0.0",
"eslint-plugin-tsdoc": "^0.2.17",
"express": "^4.18.2",
"fetch-cookie": "^2.1.0",
"get-port": "^6.1.2",
Expand Down
Loading

0 comments on commit 0fb7062

Please sign in to comment.