Skip to content
This repository has been archived by the owner on Mar 20, 2023. It is now read-only.

Commit

Permalink
Refactor TS declaration files
Browse files Browse the repository at this point in the history
  • Loading branch information
danielrearden committed Jun 14, 2020
1 parent 6a1e317 commit 227093d
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"test:ci": "npm ci && npm run prettier:check && npm run lint && npm run check && npm run testonly:cover && npm run build && npm run check:integrations",
"testonly": "mocha src/**/__tests__/**/*.js",
"testonly:cover": "nyc npm run testonly",
"lint": "eslint src types resources integrationTests",
"lint": "eslint src resources integrationTests",
"prettier": "prettier --ignore-path .gitignore --write --list-different '**/*.{js,ts,md,json,yml}'",
"prettier:check": "prettier --ignore-path .gitignore --check '**/*.{js,ts,md,json,yml}'",
"check": "flow check",
Expand Down
3 changes: 2 additions & 1 deletion resources/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ if (require.main === module) {

const cjs = babelBuild(srcPath, { envName: 'cjs' });
fs.writeFileSync(destPath, cjs);
} else if (filepath.endsWith('d.ts')) {
fs.copyFileSync(srcPath, destPath);
}
}

fs.copyFileSync('./types/index.d.ts', './dist/index.d.ts');
fs.copyFileSync('./LICENSE', './dist/LICENSE');
fs.copyFileSync('./README.md', './dist/README.md');

Expand Down
30 changes: 30 additions & 0 deletions resources/check-ts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// @noflow

'use strict';

const fs = require('fs');
const path = require('path');

const { exec, rmdirRecursive, readdirRecursive } = require('./utils');

rmdirRecursive('./tmp');
fs.mkdirSync('./tmp');
fs.mkdirSync('./tmp/__tests__');

const srcFiles = readdirRecursive('./src');
for (const filepath of srcFiles) {
const srcPath = path.join('./src', filepath);
const destPath = path.join('./tmp', filepath);

if (filepath.endsWith('.ts')) {
fs.copyFileSync(srcPath, destPath);
}
}

fs.copyFileSync('tsconfig.json', path.join('./tmp', 'tsconfig.json'));

try {
exec('dtslint tmp', { stdio: 'inherit' });
} finally {
rmdirRecursive('./tmp');
}
26 changes: 11 additions & 15 deletions types/index.d.ts → src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ import {
GraphQLTypeResolver,
} from 'graphql';

import { GraphiQLOptions } from './renderGraphiQL';

export {};

type Request = IncomingMessage;

type Response = ServerResponse & { json?: (data: unknown) => void };

type Middleware = (request: Request, response: Response) => Promise<void>;

/**
* Used to configure the graphqlHTTP middleware by providing a schema
* and other configuration options.
Expand All @@ -37,15 +42,6 @@ export type Options =
| OptionsResult;
type OptionsResult = OptionsData | Promise<OptionsData>;

export interface GraphiQLOptions {
/**
* An optional GraphQL string to use when no query is provided and no stored
* query exists from a previous session. If undefined is provided, GraphiQL
* will use its own default query.
*/
defaultQuery?: string;
}

export interface OptionsData {
/**
* A GraphQL schema from graphql-js.
Expand Down Expand Up @@ -175,7 +171,11 @@ export interface RequestInfo {
context?: unknown;
}

type Middleware = (request: Request, response: Response) => Promise<void>;
/**
* Middleware for express; takes an options object or function as input to
* configure behavior, and returns an express middleware.
*/
export function graphqlHTTP(options: Options): Middleware;

export interface GraphQLParams {
query: string | null;
Expand All @@ -184,8 +184,4 @@ export interface GraphQLParams {
raw: boolean;
}

/**
* Middleware for express; takes an options object or function as input to
* configure behavior, and returns an express middleware.
*/
export function graphqlHTTP(options: Options): Middleware;
export function getGraphQLParams(request: Request): Promise<GraphQLParams>;
7 changes: 7 additions & 0 deletions src/parseBody.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { IncomingMessage } from 'http';

export {};

type Request = IncomingMessage & { body?: unknown };

export function parseBody(req: Request): Promise<{ [param: string]: unknown }>;
29 changes: 29 additions & 0 deletions src/renderGraphiQL.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ExecutionResult } from 'graphql';

export interface GraphiQLData {
query?: string | null;
variables?: { readonly [name: string]: unknown } | null;
operationName?: string | null;
result?: ExecutionResult;
}

export interface GraphiQLOptions {
/**
* An optional GraphQL string to use when no query is provided and no stored
* query exists from a previous session. If undefined is provided, GraphiQL
* will use its own default query.
*/
defaultQuery?: string;
}

/**
* When express-graphql receives a request which does not Accept JSON, but does
* Accept HTML, it may present GraphiQL, the in-browser GraphQL explorer IDE.
*
* When shown, it will be pre-populated with the result of having executed the
* requested query.
*/
export function renderGraphiQL(
data: GraphiQLData,
options?: GraphiQLOptions,
): string;
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es6", "esnext.asynciterable"],
"target": "es2018",
"moduleResolution": "node",
"lib": ["es2018", "esnext.asynciterable"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
Expand Down

0 comments on commit 227093d

Please sign in to comment.