Skip to content

Commit

Permalink
Fix IMiddy and ICorsOptions type definitions (#200)
Browse files Browse the repository at this point in the history
* Fix IMiddy and ICorsOptions type definitions

 - integrate typings-tester into npm:test
 - write typings-tester tests for middy and cors options
 - minor typescript version bump
 - modernize typescript setup
 - provide @types/jest to allow tests to compile

* tsc/target = es2015

* Version bump
  • Loading branch information
ossareh authored and lmammino committed Jun 7, 2018
1 parent 8f40340 commit 3f65f7b
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 14 deletions.
6 changes: 3 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ declare var middy: {
declare namespace middy {
interface IMiddy extends Handler {
use: IMiddyUseFunction;
before: IMiddyMiddlewareFunction;
after: IMiddyMiddlewareFunction;
onError: IMiddyMiddlewareFunction;
before: (callbackFn: IMiddyMiddlewareFunction) => IMiddy;
after: (callbackFn: IMiddyMiddlewareFunction) => IMiddy;
onError: (callbackFn: IMiddyMiddlewareFunction) => IMiddy;
}

type IMiddyUseFunction = (config?: object) => IMiddy;
Expand Down
6 changes: 3 additions & 3 deletions middlewares.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { HttpError } from 'http-errors'
import middy from './'

interface ICorsOptions {
origin: string;
headers: string;
credentials: boolean;
origin?: string;
headers?: string;
credentials?: boolean;
}

interface ICacheOptions {
Expand Down
14 changes: 13 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "middy",
"version": "0.15.5",
"version": "0.15.6",
"description": "🛵 The stylish Node.js middleware engine for AWS Lambda",
"main": "./index.js",
"files": [
Expand All @@ -14,10 +14,10 @@
"types": "./index.d.ts",
"scripts": {
"test:lint": "eslint --ignore-pattern='node_modules/' --ignore-pattern='coverage/' --ignore-pattern='docs/' .",
"test:typings": "typings-tester --config tsconfig.json index.d.ts middlewares.d.ts",
"test:typings": "typings-tester --config tsconfig.json index.d.ts middlewares.d.ts src/__tests__/*.types.ts src/**/__tests__/*types.ts",
"test:unit": "jest --verbose --coverage",
"test:unit:watch": "jest --verbose --coverage --watch",
"test": "npm run test:lint && npm run test:unit",
"test": "npm run test:lint && npm run test:unit && npm run test:typings",
"build:readme": "jsdoc2md --global-index-format grouped --template README.md.hb src/* > README.md",
"build:docs": "jsdoc --readme README.md --package package.json --destination docs src",
"build": "npm run build:docs && npm run build:readme",
Expand All @@ -44,6 +44,8 @@
},
"homepage": "https://github.com/middyjs/middy#readme",
"devDependencies": {
"@types/jest": "^23.0.0",
"@types/node": "^10.3.1",
"aws-sdk": "^2.233.1",
"babel-jest": "^23.0.1",
"babel-preset-env": "^1.6.1",
Expand All @@ -60,7 +62,7 @@
"jsdoc-to-markdown": "^4.0.1",
"marked": "^0.3.12",
"regenerator-runtime": "^0.11.0",
"typescript": "^2.8.1",
"typescript": "^2.8.3",
"typings-tester": "^0.3.1"
},
"dependencies": {
Expand Down
47 changes: 47 additions & 0 deletions src/__tests__/middy.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import middy from '../../';

describe('🛵 Middy types test suite', () => {
test('"before" should take a function', () => {
const beforeMiddleware = jest.fn();

const handler = middy(jest.fn());

handler.before(beforeMiddleware);
})

test('"after" should take a function', () => {
const afterMiddleware = jest.fn();

const handler = middy(jest.fn());

handler.after(afterMiddleware);
})

test('"onError" should take a function', () => {
const errorMiddleware = jest.fn();

const handler = middy(jest.fn());

handler.onError(errorMiddleware);
})

test('middleware calls can be chained', () => {
const before = jest.fn()
const after = jest.fn()
const onError = jest.fn();

const middleware = () => ({
after,
before,
onError,
});

const handler = middy(jest.fn());

handler
.use(middleware)
.after(after)
.before(before)
.onError(onError);
})
})
35 changes: 35 additions & 0 deletions src/middlewares/__tests__/cors.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import middy from '../../../';
import { cors } from '../../../middlewares';

describe('📦 Middleware Types', () => {
describe('CORS', () => {
it('has an optional argument', () => {
const handler = middy(jest.fn());
handler.use(cors());
})

it ('has an optional origin field', () => {
const handler = middy(jest.fn());
handler.use(cors({
headers: "test-case",
credentials: true,
}));
})

it('has an optional headers field', () => {
const handler = middy(jest.fn());
handler.use(cors({
origin: 'example.com',
credentials: true,
}));
})

it('has an optional credentials field', () => {
const handler = middy(jest.fn());
handler.use(cors({
origin: 'example.com',
headers: "test-case",
}));
})
})
});
20 changes: 17 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es2015"],
"target": "es2015"
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"importHelpers": true,
"lib": [
"es2018",
],
"moduleResolution": "node",
"noEmitHelpers": true,
"noEmitOnError": true,
"noImplicitReturns": true,
"target": "es2015",
"sourceMap": true,
"strict": true,
},
"exclude": [
"node_modules",
],
"files": [
"index.d.ts",
"middlewares.d.ts"
Expand Down

0 comments on commit 3f65f7b

Please sign in to comment.