Skip to content
This repository has been archived by the owner on May 14, 2020. It is now read-only.

Use type imports #253

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions src/adaptors/in-memory-articles.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
import type {
DatasetCore, NamedNode, Quad, Quad_Object as QuadObject,
} from 'rdf-js';
import Articles from '../articles';
import type Articles from '../articles';
import ArticleNotFound from '../errors/article-not-found';
import NotAnArticle from '../errors/not-an-article';
import { rdf, schema } from '../namespaces';
Expand Down
8 changes: 4 additions & 4 deletions src/adaptors/postgres-articles.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import ParserJsonld from '@rdfjs/parser-jsonld';
import SerializerJsonld from '@rdfjs/serializer-jsonld-ext';
import { JsonLdObj } from 'jsonld/jsonld-spec';
import type { JsonLdObj } from 'jsonld/jsonld-spec';
import pEvent from 'p-event';
import {
errors, IBaseProtocol, IMain, ITask,
} from 'pg-promise';
import { fromStream, toStream } from 'rdf-dataset-ext';
import {
import type {
DatasetCore, DatasetCoreFactory, NamedNode, Quad, Quad_Object as QuadObject,
} from 'rdf-js';
import { stringToTerm, termToString } from 'rdf-string';
import toReadableStream from 'to-readable-stream';
import Articles from '../articles';
import type Articles from '../articles';
import ArticleNotFound from '../errors/article-not-found';
import NotAnArticle from '../errors/not-an-article';
import { ExtendedDataFactory } from '../middleware/dataset';
import type { ExtendedDataFactory } from '../middleware/dataset';
import { rdf, schema } from '../namespaces';

const { QueryResultError, queryResultErrorCode: { noData } } = errors;
Expand Down
6 changes: 3 additions & 3 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import cors from '@koa/cors';
import Router, { RouterContext } from '@koa/router';
import type Router from '@koa/router';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you’ve used Flow before, the syntax is fairly similar. One difference is that we’ve added a few restrictions to avoid code that might appear ambiguous.

This is a bit annoying.

import Koa, { DefaultState, Middleware } from 'koa';
import logger from 'koa-logger';
import Articles from './articles';
import type Articles from './articles';
import apiDocumentationLink from './middleware/api-documentation-link';
import setDataFactory from './middleware/data-factory';
import addDatasets, { DatasetContext, ExtendedDataFactory } from './middleware/dataset';
Expand All @@ -18,7 +18,7 @@ export type AppServiceContext = DatasetContext<{
articles: Articles;
}>;

export type AppContext = RouterContext<AppState, AppServiceContext>;
export type AppContext = Router.RouterContext<AppState, AppServiceContext>;

export type AppMiddleware = Middleware<AppState, AppContext>;

Expand Down
2 changes: 1 addition & 1 deletion src/articles.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DatasetCore, NamedNode } from 'rdf-js';
import type { DatasetCore, NamedNode } from 'rdf-js';

interface Articles extends AsyncIterable<[NamedNode, DatasetCore]> {
set(id: NamedNode, article: DatasetCore): Promise<void>;
Expand Down
4 changes: 2 additions & 2 deletions src/data-factory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import dataFactory from '@rdfjs/data-model';
import createDataset from 'rdf-dataset-indexed';
import { DatasetCore, Quad } from 'rdf-js';
import { ExtendedDataFactory } from './middleware/dataset';
import type { DatasetCore, Quad } from 'rdf-js';
import type { ExtendedDataFactory } from './middleware/dataset';

const extendedDataFactory: ExtendedDataFactory = {
...dataFactory,
Expand Down
2 changes: 1 addition & 1 deletion src/db.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConnectionConfig } from 'pg';
import type { ConnectionConfig } from 'pg';

const config: Readonly<ConnectionConfig> = {
user: process.env.DATABASE_USER,
Expand Down
2 changes: 1 addition & 1 deletion src/errors/article-not-found.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NamedNode } from 'rdf-js';
import type { NamedNode } from 'rdf-js';
import { termToString } from 'rdf-string';

export default class ArticleNotFound extends Error {
Expand Down
2 changes: 1 addition & 1 deletion src/errors/not-an-article.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Quad_Object as QuadObject } from 'rdf-js';
import type { Quad_Object as QuadObject } from 'rdf-js';
import { termToString } from 'rdf-string';
import { schema } from '../namespaces';

Expand Down
2 changes: 1 addition & 1 deletion src/middleware/api-documentation-link.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import formatLinkHeader from 'format-link-header';
import {
import type {
DefaultStateExtends, ExtendableContext, Middleware, Next,
} from 'koa';
import url from 'url';
Expand Down
4 changes: 2 additions & 2 deletions src/middleware/data-factory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
import type {
DefaultContextExtends, DefaultStateExtends, Middleware, Next,
} from 'koa';
import { BaseQuad, DataFactory } from 'rdf-js';
import type { BaseQuad, DataFactory } from 'rdf-js';

export type DataFactoryContext<Context extends DefaultContextExtends = DefaultContextExtends,
Factory extends DataFactory<BaseQuad> = DataFactory> = Context & { dataFactory: Factory };
Expand Down
6 changes: 3 additions & 3 deletions src/middleware/dataset.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {
import type {
DefaultContextExtends, DefaultStateExtends, ExtendableContext, Middleware, Next, Request, Response,
} from 'koa';
import {
import type {
BaseQuad, DataFactory, DatasetCore, DatasetCoreFactory, Quad,
} from 'rdf-js';
import { DataFactoryContext } from './data-factory';
import type { DataFactoryContext } from './data-factory';

export type WithDataset<T extends Request | Response, Q extends BaseQuad = Quad> = T & { dataset: DatasetCore<Q> };

Expand Down
2 changes: 1 addition & 1 deletion src/middleware/empty-response.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {
import type {
DefaultStateExtends, ExtendableContext, Middleware, Next, Response,
} from 'koa';

Expand Down
4 changes: 2 additions & 2 deletions src/middleware/error-handler.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import createHttpError, { HttpError } from 'http-errors';
import {
import type {
DefaultStateExtends, ExtendableContext, Middleware, Next,
} from 'koa';
import { hydra, rdf } from '../namespaces';
import { DatasetContext } from './dataset';
import type { DatasetContext } from './dataset';

const handleHttpError = (
error: HttpError,
Expand Down
6 changes: 3 additions & 3 deletions src/middleware/jsonld.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import ParserJsonld from '@rdfjs/parser-jsonld';
import SerializerJsonld from '@rdfjs/serializer-jsonld-ext';
import { format as formatContentType } from 'content-type';
import { NO_CONTENT } from 'http-status-codes';
import { Context } from 'jsonld/jsonld-spec';
import {
import type { Context } from 'jsonld/jsonld-spec';
import type {
DefaultStateExtends, Middleware, Next, Response,
} from 'koa';
import pEvent from 'p-event';
import { fromStream, toStream } from 'rdf-dataset-ext';
import { DatasetContext } from './dataset';
import type { DatasetContext } from './dataset';

const responseHasContent = (response: Response): boolean => response.body || response.status === NO_CONTENT;

Expand Down
10 changes: 5 additions & 5 deletions src/middleware/routing.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Router, { Middleware, RouterParamContext } from '@koa/router';
import type Router from '@koa/router';
import createHttpError from 'http-errors';
import { DefaultContextExtends, DefaultStateExtends, Next } from 'koa';
import type { DefaultContextExtends, DefaultStateExtends, Next } from 'koa';
import compose from 'koa-compose';

const notFound = <State extends DefaultStateExtends, Context extends DefaultContextExtends>
(): Middleware<State, Context> => (
async ({ _matchedRoute }: RouterParamContext<State, Context>, next: Next): Promise<void> => {
(): Router.Middleware<State, Context> => (
async ({ _matchedRoute }: Router.RouterParamContext<State, Context>, next: Next): Promise<void> => {
await next();

if (typeof _matchedRoute === 'undefined') {
Expand All @@ -15,7 +15,7 @@ const notFound = <State extends DefaultStateExtends, Context extends DefaultCont
);

export default <State extends DefaultStateExtends, Context extends DefaultContextExtends>
(router: Router<State, Context>): Middleware<State, Context> => (
(router: Router<State, Context>): Router.Middleware<State, Context> => (
compose([
router.routes(),
notFound<State, Context>(),
Expand Down
2 changes: 1 addition & 1 deletion src/router.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Router from '@koa/router';
import { AppServiceContext, AppState } from './app';
import type { AppServiceContext, AppState } from './app';
import Routes from './routes';
import addArticle from './routes/add-article';
import apiDocumentation from './routes/api-documentation';
Expand Down
6 changes: 3 additions & 3 deletions src/routes/add-article.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import clownface from 'clownface';
import createHttpError from 'http-errors';
import { CREATED } from 'http-status-codes';
import { Next } from 'koa';
import { Quad } from 'rdf-js';
import type { Next } from 'koa';
import type { Quad } from 'rdf-js';
import { termToString } from 'rdf-string';
import uniqueString from 'unique-string';
import url from 'url';
import { AppContext, AppMiddleware } from '../app';
import type { AppContext, AppMiddleware } from '../app';
import { rdf, schema } from '../namespaces';
import Routes from './index';

Expand Down
6 changes: 3 additions & 3 deletions src/routes/api-documentation.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import clownface, { Clownface } from 'clownface';
import { CREATED, OK } from 'http-status-codes';
import { Next } from 'koa';
import { NamedNode } from 'rdf-js';
import type { Next } from 'koa';
import type { NamedNode } from 'rdf-js';
import { toRdf } from 'rdf-literal';
import url from 'url';
import { AppContext, AppMiddleware } from '../app';
import type { AppContext, AppMiddleware } from '../app';
import {
hydra, owl, rdf, schema,
} from '../namespaces';
Expand Down
6 changes: 3 additions & 3 deletions src/routes/article-list.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import clownface, { Clownface } from 'clownface';
import { OK } from 'http-status-codes';
import all from 'it-all';
import { Next } from 'koa';
import type { Next } from 'koa';
import { addAll } from 'rdf-dataset-ext';
import { DatasetCore, NamedNode } from 'rdf-js';
import type { DatasetCore, NamedNode } from 'rdf-js';
import { toRdf } from 'rdf-literal';
import url from 'url';
import { AppContext, AppMiddleware } from '../app';
import type { AppContext, AppMiddleware } from '../app';
import { hydra, rdf, schema } from '../namespaces';
import Routes from './index';

Expand Down
4 changes: 2 additions & 2 deletions src/routes/article.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import createHttpError from 'http-errors';
import { OK } from 'http-status-codes';
import { Next } from 'koa';
import type { Next } from 'koa';
import url from 'url';
import { AppContext, AppMiddleware } from '../app';
import type { AppContext, AppMiddleware } from '../app';
import ArticleNotFound from '../errors/article-not-found';

export default (): AppMiddleware => (
Expand Down
6 changes: 3 additions & 3 deletions src/routes/entry-point.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import clownface, { Clownface } from 'clownface';
import { OK } from 'http-status-codes';
import { Next } from 'koa';
import { NamedNode } from 'rdf-js';
import type { Next } from 'koa';
import type { NamedNode } from 'rdf-js';
import url from 'url';
import { AppContext, AppMiddleware } from '../app';
import type { AppContext, AppMiddleware } from '../app';
import { hydra, rdf, schema } from '../namespaces';
import Routes from './index';

Expand Down
2 changes: 1 addition & 1 deletion test/adaptors/in-memory-articles.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { literal, namedNode, quad } from '@rdfjs/data-model';
import all from 'it-all';
import 'jest-rdf';
import { DatasetCore, NamedNode } from 'rdf-js';
import type { DatasetCore, NamedNode } from 'rdf-js';
import InMemoryArticles from '../../src/adaptors/in-memory-articles';
import ArticleNotFound from '../../src/errors/article-not-found';
import NotAnArticle from '../../src/errors/not-an-article';
Expand Down
2 changes: 1 addition & 1 deletion test/adaptors/postgres-articles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { literal, namedNode, quad } from '@rdfjs/data-model';
import all from 'it-all';
import 'jest-rdf';
import pgPromise, { IBaseProtocol, IMain } from 'pg-promise';
import { DatasetCore, NamedNode } from 'rdf-js';
import type { DatasetCore, NamedNode } from 'rdf-js';
import PostgresArticles from '../../src/adaptors/postgres-articles';
import dataFactory from '../../src/data-factory';
import db from '../../src/db';
Expand Down
12 changes: 6 additions & 6 deletions test/context.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import Router from '@koa/router';
import { UnknownError } from 'http-errors';
import type Router from '@koa/router';
import type { UnknownError } from 'http-errors';
import Koa, { Context, Request, Response } from 'koa';
import { Request as IncomingMessage, Response as ServerResponse } from 'mock-http';
import { DatasetCore } from 'rdf-js';
import type { DatasetCore } from 'rdf-js';
import InMemoryArticles from '../src/adaptors/in-memory-articles';
import { AppContext } from '../src/app';
import Articles from '../src/articles';
import type { AppContext } from '../src/app';
import type Articles from '../src/articles';
import dataFactory, { dataset } from '../src/data-factory';
import { WithDataset } from '../src/middleware/dataset';
import type { WithDataset } from '../src/middleware/dataset';

export type ErrorListener = (error: UnknownError, context: Context) => void;

Expand Down
2 changes: 1 addition & 1 deletion test/create-article.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {
import type {
DatasetCore, Literal, NamedNode, Quad_Subject as QuadSubject,
} from 'rdf-js';
import {
Expand Down
8 changes: 4 additions & 4 deletions test/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { UnknownError } from 'http-errors';
import { ExtendableContext } from 'koa';
import { Middleware } from 'koa-compose';
import { AppContext } from '../src/app';
import type { UnknownError } from 'http-errors';
import type { ExtendableContext } from 'koa';
import type { Middleware } from 'koa-compose';
import type { AppContext } from '../src/app';

export type NextMiddleware<CustomT extends ExtendableContext = AppContext> =
(context: CustomT) => Promise<void>;
Expand Down
2 changes: 1 addition & 1 deletion test/middleware/api-documentation.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Response } from 'koa';
import type { Response } from 'koa';
import parseLinkHeader from 'parse-link-header';
import apiDocumentation from '../../src/middleware/api-documentation-link';
import createContext from '../context';
Expand Down
4 changes: 2 additions & 2 deletions test/middleware/data-factory.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dataFactory from '@rdfjs/data-model';
import { Next } from 'koa';
import { DataFactory } from 'rdf-js';
import type { Next } from 'koa';
import type { DataFactory } from 'rdf-js';
import setDataFactory, { DataFactoryContext } from '../../src/middleware/data-factory';
import createContext from '../context';

Expand Down
2 changes: 1 addition & 1 deletion test/middleware/dataset.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'jest-rdf';
import { Next } from 'koa';
import type { Next } from 'koa';
import dataset, { DatasetContext } from '../../src/middleware/dataset';
import createContext from '../context';

Expand Down
2 changes: 1 addition & 1 deletion test/middleware/empty-response.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context, Response } from 'koa';
import type { Context, Response } from 'koa';
import emptyResponse from '../../src/middleware/empty-response';
import createContext from '../context';
import runMiddleware, { NextMiddleware } from '../middleware';
Expand Down
4 changes: 2 additions & 2 deletions test/middleware/error-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { blankNode, literal, quad } from '@rdfjs/data-model';
import createHttpError from 'http-errors';
import { INTERNAL_SERVER_ERROR, SERVICE_UNAVAILABLE } from 'http-status-codes';
import 'jest-rdf';
import { Response } from 'koa';
import { WithDataset } from '../../src/middleware/dataset';
import type { Response } from 'koa';
import type { WithDataset } from '../../src/middleware/dataset';
import errorHandler from '../../src/middleware/error-handler';
import { hydra, rdf } from '../../src/namespaces';
import createContext, { ErrorListener } from '../context';
Expand Down
8 changes: 4 additions & 4 deletions test/middleware/jsonld.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import namespace from '@rdfjs/namespace';
import { parse as parseContentType } from 'content-type';
import { CREATED, NO_CONTENT, OK } from 'http-status-codes';
import 'jest-rdf';
import { Context as JsonLdContext } from 'jsonld/jsonld-spec';
import type { Context as JsonLdContext } from 'jsonld/jsonld-spec';
import { addAll } from 'rdf-dataset-ext';
import { Quad } from 'rdf-js';
import { AppContext } from '../../src/app';
import type { Quad } from 'rdf-js';
import type { AppContext } from '../../src/app';
import jsonld from '../../src/middleware/jsonld';
import { rdf, schema } from '../../src/namespaces';
import createContext, { Headers } from '../context';
import { NextMiddleware } from '../middleware';
import type { NextMiddleware } from '../middleware';

const makeRequest = async (
body?: string, headers?: Headers, next: NextMiddleware = jest.fn(), jsonLdContext: JsonLdContext = {},
Expand Down
2 changes: 1 addition & 1 deletion test/middleware/routing.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Router from '@koa/router';
import createHttpError from 'http-errors';
import { OK } from 'http-status-codes';
import { Context, Next, Response } from 'koa';
import type { Context, Next, Response } from 'koa';
import routing from '../../src/middleware/routing';
import createContext from '../context';
import runMiddleware, { NextMiddleware } from '../middleware';
Expand Down
6 changes: 3 additions & 3 deletions test/routes/add-article.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import createHttpError from 'http-errors';
import { CREATED } from 'http-status-codes';
import all from 'it-all';
import 'jest-rdf';
import { Response } from 'koa';
import { DatasetCore } from 'rdf-js';
import type { Response } from 'koa';
import type { DatasetCore } from 'rdf-js';
import InMemoryArticles from '../../src/adaptors/in-memory-articles';
import Articles from '../../src/articles';
import type Articles from '../../src/articles';
import { schema } from '../../src/namespaces';
import addArticle from '../../src/routes/add-article';
import createContext from '../context';
Expand Down
Loading