Skip to content

Commit

Permalink
Use typed Koa context
Browse files Browse the repository at this point in the history
  • Loading branch information
72636c committed Dec 14, 2020
1 parent 641accc commit 29a82d2
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-cats-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'skuba': patch
---

**template/koa-rest-api:** Type context
2 changes: 1 addition & 1 deletion template/koa-rest-api/src/api/healthCheck.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Middleware } from 'koa';
import { Middleware } from 'src/types/koa';

export const healthCheckHandler: Middleware = (ctx) => {
ctx.state.skipRequestLogging = true;
Expand Down
3 changes: 1 addition & 2 deletions template/koa-rest-api/src/api/jobs/getJobs.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Middleware } from 'koa';

import { contextLogger } from 'src/framework/logging';
import { metricsClient } from 'src/framework/metrics';
import * as storage from 'src/storage/jobs';
import { Middleware } from 'src/types/koa';

export const getJobsHandler: Middleware = async (ctx) => {
const jobs = await storage.readJobs();
Expand Down
3 changes: 1 addition & 2 deletions template/koa-rest-api/src/api/jobs/postJob.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Middleware } from 'koa';

import { contextLogger } from 'src/framework/logging';
import { metricsClient } from 'src/framework/metrics';
import { validateRequestBody } from 'src/framework/validation';
import * as storage from 'src/storage/jobs';
import { filterJobInput } from 'src/types/jobs';
import { Middleware } from 'src/types/koa';

export const postJobHandler: Middleware = async (ctx) => {
const jobInput = validateRequestBody(ctx, filterJobInput);
Expand Down
2 changes: 1 addition & 1 deletion template/koa-rest-api/src/api/smokeTest.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { Middleware } from 'koa';
import { Middleware } from 'src/types/koa';

export const smokeTestHandler: Middleware = (ctx) => (ctx.body = '');
2 changes: 1 addition & 1 deletion template/koa-rest-api/src/framework/logging.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import createLogger from '@seek/logger';
import { Context } from 'koa';
import { RequestLogging } from 'seek-koala';

import { config } from 'src/config';
import { Context } from 'src/types/koa';

export const rootLogger = createLogger({
base: {
Expand Down
2 changes: 1 addition & 1 deletion template/koa-rest-api/src/framework/server.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Router from '@koa/router';
import { Middleware } from 'koa';

import { rootLogger } from 'src/testing/logging';
import { metricsClient } from 'src/testing/metrics';
import { agentFromRouter } from 'src/testing/server';
import { chance } from 'src/testing/types';
import { Middleware } from 'src/types/koa';

const middleware = jest.fn<void, Parameters<Middleware>>();

Expand Down
26 changes: 13 additions & 13 deletions template/koa-rest-api/src/framework/server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Koa, { Context, DefaultState, Middleware } from 'koa';
import Koa from 'koa';
import compose from 'koa-compose';
import {
ErrorMiddleware,
Expand All @@ -18,25 +18,25 @@ const metrics = MetricsMiddleware.create(
}),
);

const requestLogging = RequestLogging.createMiddleware<DefaultState, Context>(
(ctx, fields, err) => {
if (typeof err === 'undefined') {
// Depend on sidecar logging for happy path requests
return;
}
const requestLogging = RequestLogging.createMiddleware((ctx, fields, err) => {
if (typeof err === 'undefined') {
// Depend on sidecar logging for happy path requests
return;
}

return ctx.status < 500
? rootLogger.info(fields, 'Client error')
: rootLogger.error(fields, 'Server error');
},
);
return ctx.status < 500
? rootLogger.info(fields, 'Client error')
: rootLogger.error(fields, 'Server error');
});

const version = VersionMiddleware.create({
name: config.name,
version: config.version,
});

export const createApp = <S, C>(...middleware: Middleware<S, C>[]) =>
export const createApp = <State, Context>(
...middleware: Koa.Middleware<State, Context>[]
) =>
new Koa()
.use(requestLogging)
.use(metrics)
Expand Down
2 changes: 1 addition & 1 deletion template/koa-rest-api/src/framework/validation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context } from 'koa';
import { Context } from 'src/types/koa';

export const validate = <T>({
ctx,
Expand Down
4 changes: 2 additions & 2 deletions template/koa-rest-api/src/testing/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Router from '@koa/router';
import Koa, { Middleware } from 'koa';
import Koa from 'koa';
import request from 'supertest';

import { createApp } from 'src/framework/server';
Expand All @@ -14,7 +14,7 @@ export const agentFromApp = <State, Context>(app: Koa<State, Context>) =>
* Create a new SuperTest agent from a set of Koa middleware.
*/
export const agentFromMiddleware = <State, Context>(
...middleware: Middleware<State, Context>[]
...middleware: Koa.Middleware<State, Context>[]
) => {
const app = createApp(...middleware);

Expand Down
9 changes: 9 additions & 0 deletions template/koa-rest-api/src/types/koa.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Koa from 'koa';

export type Context = Koa.ParameterizedContext<State>;

export type Middleware = Koa.Middleware<State>;

export interface State {
skipRequestLogging?: boolean;
}

0 comments on commit 29a82d2

Please sign in to comment.